[SCSI] lpfc 8.3.24: Add request-firmware support
[linux-block.git] / drivers / scsi / lpfc / lpfc_sli.c
1 /*******************************************************************
2  * This file is part of the Emulex Linux Device Driver for         *
3  * Fibre Channel Host Bus Adapters.                                *
4  * Copyright (C) 2004-2011 Emulex.  All rights reserved.           *
5  * EMULEX and SLI are trademarks of Emulex.                        *
6  * www.emulex.com                                                  *
7  * Portions Copyright (C) 2004-2005 Christoph Hellwig              *
8  *                                                                 *
9  * This program is free software; you can redistribute it and/or   *
10  * modify it under the terms of version 2 of the GNU General       *
11  * Public License as published by the Free Software Foundation.    *
12  * This program is distributed in the hope that it will be useful. *
13  * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND          *
14  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,  *
15  * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE      *
16  * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
17  * TO BE LEGALLY INVALID.  See the GNU General Public License for  *
18  * more details, a copy of which can be found in the file COPYING  *
19  * included with this package.                                     *
20  *******************************************************************/
21
22 #include <linux/blkdev.h>
23 #include <linux/pci.h>
24 #include <linux/interrupt.h>
25 #include <linux/delay.h>
26 #include <linux/slab.h>
27
28 #include <scsi/scsi.h>
29 #include <scsi/scsi_cmnd.h>
30 #include <scsi/scsi_device.h>
31 #include <scsi/scsi_host.h>
32 #include <scsi/scsi_transport_fc.h>
33 #include <scsi/fc/fc_fs.h>
34 #include <linux/aer.h>
35
36 #include "lpfc_hw4.h"
37 #include "lpfc_hw.h"
38 #include "lpfc_sli.h"
39 #include "lpfc_sli4.h"
40 #include "lpfc_nl.h"
41 #include "lpfc_disc.h"
42 #include "lpfc_scsi.h"
43 #include "lpfc.h"
44 #include "lpfc_crtn.h"
45 #include "lpfc_logmsg.h"
46 #include "lpfc_compat.h"
47 #include "lpfc_debugfs.h"
48 #include "lpfc_vport.h"
49
50 /* There are only four IOCB completion types. */
51 typedef enum _lpfc_iocb_type {
52         LPFC_UNKNOWN_IOCB,
53         LPFC_UNSOL_IOCB,
54         LPFC_SOL_IOCB,
55         LPFC_ABORT_IOCB
56 } lpfc_iocb_type;
57
58
59 /* Provide function prototypes local to this module. */
60 static int lpfc_sli_issue_mbox_s4(struct lpfc_hba *, LPFC_MBOXQ_t *,
61                                   uint32_t);
62 static int lpfc_sli4_read_rev(struct lpfc_hba *, LPFC_MBOXQ_t *,
63                               uint8_t *, uint32_t *);
64 static struct lpfc_iocbq *lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *,
65                                                          struct lpfc_iocbq *);
66 static void lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *,
67                                       struct hbq_dmabuf *);
68 static int lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *, struct lpfc_queue *,
69                                     struct lpfc_cqe *);
70
71 static IOCB_t *
72 lpfc_get_iocb_from_iocbq(struct lpfc_iocbq *iocbq)
73 {
74         return &iocbq->iocb;
75 }
76
77 /**
78  * lpfc_sli4_wq_put - Put a Work Queue Entry on an Work Queue
79  * @q: The Work Queue to operate on.
80  * @wqe: The work Queue Entry to put on the Work queue.
81  *
82  * This routine will copy the contents of @wqe to the next available entry on
83  * the @q. This function will then ring the Work Queue Doorbell to signal the
84  * HBA to start processing the Work Queue Entry. This function returns 0 if
85  * successful. If no entries are available on @q then this function will return
86  * -ENOMEM.
87  * The caller is expected to hold the hbalock when calling this routine.
88  **/
89 static uint32_t
90 lpfc_sli4_wq_put(struct lpfc_queue *q, union lpfc_wqe *wqe)
91 {
92         union lpfc_wqe *temp_wqe = q->qe[q->host_index].wqe;
93         struct lpfc_register doorbell;
94         uint32_t host_index;
95
96         /* If the host has not yet processed the next entry then we are done */
97         if (((q->host_index + 1) % q->entry_count) == q->hba_index)
98                 return -ENOMEM;
99         /* set consumption flag every once in a while */
100         if (!((q->host_index + 1) % LPFC_RELEASE_NOTIFICATION_INTERVAL))
101                 bf_set(wqe_wqec, &wqe->generic.wqe_com, 1);
102         if (q->phba->sli3_options & LPFC_SLI4_PHWQ_ENABLED)
103                 bf_set(wqe_wqid, &wqe->generic.wqe_com, q->queue_id);
104         lpfc_sli_pcimem_bcopy(wqe, temp_wqe, q->entry_size);
105
106         /* Update the host index before invoking device */
107         host_index = q->host_index;
108         q->host_index = ((q->host_index + 1) % q->entry_count);
109
110         /* Ring Doorbell */
111         doorbell.word0 = 0;
112         bf_set(lpfc_wq_doorbell_num_posted, &doorbell, 1);
113         bf_set(lpfc_wq_doorbell_index, &doorbell, host_index);
114         bf_set(lpfc_wq_doorbell_id, &doorbell, q->queue_id);
115         writel(doorbell.word0, q->phba->sli4_hba.WQDBregaddr);
116         readl(q->phba->sli4_hba.WQDBregaddr); /* Flush */
117
118         return 0;
119 }
120
121 /**
122  * lpfc_sli4_wq_release - Updates internal hba index for WQ
123  * @q: The Work Queue to operate on.
124  * @index: The index to advance the hba index to.
125  *
126  * This routine will update the HBA index of a queue to reflect consumption of
127  * Work Queue Entries by the HBA. When the HBA indicates that it has consumed
128  * an entry the host calls this function to update the queue's internal
129  * pointers. This routine returns the number of entries that were consumed by
130  * the HBA.
131  **/
132 static uint32_t
133 lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)
134 {
135         uint32_t released = 0;
136
137         if (q->hba_index == index)
138                 return 0;
139         do {
140                 q->hba_index = ((q->hba_index + 1) % q->entry_count);
141                 released++;
142         } while (q->hba_index != index);
143         return released;
144 }
145
146 /**
147  * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue
148  * @q: The Mailbox Queue to operate on.
149  * @wqe: The Mailbox Queue Entry to put on the Work queue.
150  *
151  * This routine will copy the contents of @mqe to the next available entry on
152  * the @q. This function will then ring the Work Queue Doorbell to signal the
153  * HBA to start processing the Work Queue Entry. This function returns 0 if
154  * successful. If no entries are available on @q then this function will return
155  * -ENOMEM.
156  * The caller is expected to hold the hbalock when calling this routine.
157  **/
158 static uint32_t
159 lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)
160 {
161         struct lpfc_mqe *temp_mqe = q->qe[q->host_index].mqe;
162         struct lpfc_register doorbell;
163         uint32_t host_index;
164
165         /* If the host has not yet processed the next entry then we are done */
166         if (((q->host_index + 1) % q->entry_count) == q->hba_index)
167                 return -ENOMEM;
168         lpfc_sli_pcimem_bcopy(mqe, temp_mqe, q->entry_size);
169         /* Save off the mailbox pointer for completion */
170         q->phba->mbox = (MAILBOX_t *)temp_mqe;
171
172         /* Update the host index before invoking device */
173         host_index = q->host_index;
174         q->host_index = ((q->host_index + 1) % q->entry_count);
175
176         /* Ring Doorbell */
177         doorbell.word0 = 0;
178         bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);
179         bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);
180         writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);
181         readl(q->phba->sli4_hba.MQDBregaddr); /* Flush */
182         return 0;
183 }
184
185 /**
186  * lpfc_sli4_mq_release - Updates internal hba index for MQ
187  * @q: The Mailbox Queue to operate on.
188  *
189  * This routine will update the HBA index of a queue to reflect consumption of
190  * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed
191  * an entry the host calls this function to update the queue's internal
192  * pointers. This routine returns the number of entries that were consumed by
193  * the HBA.
194  **/
195 static uint32_t
196 lpfc_sli4_mq_release(struct lpfc_queue *q)
197 {
198         /* Clear the mailbox pointer for completion */
199         q->phba->mbox = NULL;
200         q->hba_index = ((q->hba_index + 1) % q->entry_count);
201         return 1;
202 }
203
204 /**
205  * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ
206  * @q: The Event Queue to get the first valid EQE from
207  *
208  * This routine will get the first valid Event Queue Entry from @q, update
209  * the queue's internal hba index, and return the EQE. If no valid EQEs are in
210  * the Queue (no more work to do), or the Queue is full of EQEs that have been
211  * processed, but not popped back to the HBA then this routine will return NULL.
212  **/
213 static struct lpfc_eqe *
214 lpfc_sli4_eq_get(struct lpfc_queue *q)
215 {
216         struct lpfc_eqe *eqe = q->qe[q->hba_index].eqe;
217
218         /* If the next EQE is not valid then we are done */
219         if (!bf_get_le32(lpfc_eqe_valid, eqe))
220                 return NULL;
221         /* If the host has not yet processed the next entry then we are done */
222         if (((q->hba_index + 1) % q->entry_count) == q->host_index)
223                 return NULL;
224
225         q->hba_index = ((q->hba_index + 1) % q->entry_count);
226         return eqe;
227 }
228
229 /**
230  * lpfc_sli4_eq_release - Indicates the host has finished processing an EQ
231  * @q: The Event Queue that the host has completed processing for.
232  * @arm: Indicates whether the host wants to arms this CQ.
233  *
234  * This routine will mark all Event Queue Entries on @q, from the last
235  * known completed entry to the last entry that was processed, as completed
236  * by clearing the valid bit for each completion queue entry. Then it will
237  * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
238  * The internal host index in the @q will be updated by this routine to indicate
239  * that the host has finished processing the entries. The @arm parameter
240  * indicates that the queue should be rearmed when ringing the doorbell.
241  *
242  * This function will return the number of EQEs that were popped.
243  **/
244 uint32_t
245 lpfc_sli4_eq_release(struct lpfc_queue *q, bool arm)
246 {
247         uint32_t released = 0;
248         struct lpfc_eqe *temp_eqe;
249         struct lpfc_register doorbell;
250
251         /* while there are valid entries */
252         while (q->hba_index != q->host_index) {
253                 temp_eqe = q->qe[q->host_index].eqe;
254                 bf_set_le32(lpfc_eqe_valid, temp_eqe, 0);
255                 released++;
256                 q->host_index = ((q->host_index + 1) % q->entry_count);
257         }
258         if (unlikely(released == 0 && !arm))
259                 return 0;
260
261         /* ring doorbell for number popped */
262         doorbell.word0 = 0;
263         if (arm) {
264                 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
265                 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
266         }
267         bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
268         bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
269         bf_set(lpfc_eqcq_doorbell_eqid, &doorbell, q->queue_id);
270         writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
271         /* PCI read to flush PCI pipeline on re-arming for INTx mode */
272         if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
273                 readl(q->phba->sli4_hba.EQCQDBregaddr);
274         return released;
275 }
276
277 /**
278  * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
279  * @q: The Completion Queue to get the first valid CQE from
280  *
281  * This routine will get the first valid Completion Queue Entry from @q, update
282  * the queue's internal hba index, and return the CQE. If no valid CQEs are in
283  * the Queue (no more work to do), or the Queue is full of CQEs that have been
284  * processed, but not popped back to the HBA then this routine will return NULL.
285  **/
286 static struct lpfc_cqe *
287 lpfc_sli4_cq_get(struct lpfc_queue *q)
288 {
289         struct lpfc_cqe *cqe;
290
291         /* If the next CQE is not valid then we are done */
292         if (!bf_get_le32(lpfc_cqe_valid, q->qe[q->hba_index].cqe))
293                 return NULL;
294         /* If the host has not yet processed the next entry then we are done */
295         if (((q->hba_index + 1) % q->entry_count) == q->host_index)
296                 return NULL;
297
298         cqe = q->qe[q->hba_index].cqe;
299         q->hba_index = ((q->hba_index + 1) % q->entry_count);
300         return cqe;
301 }
302
303 /**
304  * lpfc_sli4_cq_release - Indicates the host has finished processing a CQ
305  * @q: The Completion Queue that the host has completed processing for.
306  * @arm: Indicates whether the host wants to arms this CQ.
307  *
308  * This routine will mark all Completion queue entries on @q, from the last
309  * known completed entry to the last entry that was processed, as completed
310  * by clearing the valid bit for each completion queue entry. Then it will
311  * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
312  * The internal host index in the @q will be updated by this routine to indicate
313  * that the host has finished processing the entries. The @arm parameter
314  * indicates that the queue should be rearmed when ringing the doorbell.
315  *
316  * This function will return the number of CQEs that were released.
317  **/
318 uint32_t
319 lpfc_sli4_cq_release(struct lpfc_queue *q, bool arm)
320 {
321         uint32_t released = 0;
322         struct lpfc_cqe *temp_qe;
323         struct lpfc_register doorbell;
324
325         /* while there are valid entries */
326         while (q->hba_index != q->host_index) {
327                 temp_qe = q->qe[q->host_index].cqe;
328                 bf_set_le32(lpfc_cqe_valid, temp_qe, 0);
329                 released++;
330                 q->host_index = ((q->host_index + 1) % q->entry_count);
331         }
332         if (unlikely(released == 0 && !arm))
333                 return 0;
334
335         /* ring doorbell for number popped */
336         doorbell.word0 = 0;
337         if (arm)
338                 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
339         bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
340         bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
341         bf_set(lpfc_eqcq_doorbell_cqid, &doorbell, q->queue_id);
342         writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
343         return released;
344 }
345
346 /**
347  * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
348  * @q: The Header Receive Queue to operate on.
349  * @wqe: The Receive Queue Entry to put on the Receive queue.
350  *
351  * This routine will copy the contents of @wqe to the next available entry on
352  * the @q. This function will then ring the Receive Queue Doorbell to signal the
353  * HBA to start processing the Receive Queue Entry. This function returns the
354  * index that the rqe was copied to if successful. If no entries are available
355  * on @q then this function will return -ENOMEM.
356  * The caller is expected to hold the hbalock when calling this routine.
357  **/
358 static int
359 lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
360                  struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
361 {
362         struct lpfc_rqe *temp_hrqe = hq->qe[hq->host_index].rqe;
363         struct lpfc_rqe *temp_drqe = dq->qe[dq->host_index].rqe;
364         struct lpfc_register doorbell;
365         int put_index = hq->host_index;
366
367         if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
368                 return -EINVAL;
369         if (hq->host_index != dq->host_index)
370                 return -EINVAL;
371         /* If the host has not yet processed the next entry then we are done */
372         if (((hq->host_index + 1) % hq->entry_count) == hq->hba_index)
373                 return -EBUSY;
374         lpfc_sli_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
375         lpfc_sli_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
376
377         /* Update the host index to point to the next slot */
378         hq->host_index = ((hq->host_index + 1) % hq->entry_count);
379         dq->host_index = ((dq->host_index + 1) % dq->entry_count);
380
381         /* Ring The Header Receive Queue Doorbell */
382         if (!(hq->host_index % LPFC_RQ_POST_BATCH)) {
383                 doorbell.word0 = 0;
384                 bf_set(lpfc_rq_doorbell_num_posted, &doorbell,
385                        LPFC_RQ_POST_BATCH);
386                 bf_set(lpfc_rq_doorbell_id, &doorbell, hq->queue_id);
387                 writel(doorbell.word0, hq->phba->sli4_hba.RQDBregaddr);
388         }
389         return put_index;
390 }
391
392 /**
393  * lpfc_sli4_rq_release - Updates internal hba index for RQ
394  * @q: The Header Receive Queue to operate on.
395  *
396  * This routine will update the HBA index of a queue to reflect consumption of
397  * one Receive Queue Entry by the HBA. When the HBA indicates that it has
398  * consumed an entry the host calls this function to update the queue's
399  * internal pointers. This routine returns the number of entries that were
400  * consumed by the HBA.
401  **/
402 static uint32_t
403 lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
404 {
405         if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
406                 return 0;
407         hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
408         dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
409         return 1;
410 }
411
412 /**
413  * lpfc_cmd_iocb - Get next command iocb entry in the ring
414  * @phba: Pointer to HBA context object.
415  * @pring: Pointer to driver SLI ring object.
416  *
417  * This function returns pointer to next command iocb entry
418  * in the command ring. The caller must hold hbalock to prevent
419  * other threads consume the next command iocb.
420  * SLI-2/SLI-3 provide different sized iocbs.
421  **/
422 static inline IOCB_t *
423 lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
424 {
425         return (IOCB_t *) (((char *) pring->cmdringaddr) +
426                            pring->cmdidx * phba->iocb_cmd_size);
427 }
428
429 /**
430  * lpfc_resp_iocb - Get next response iocb entry in the ring
431  * @phba: Pointer to HBA context object.
432  * @pring: Pointer to driver SLI ring object.
433  *
434  * This function returns pointer to next response iocb entry
435  * in the response ring. The caller must hold hbalock to make sure
436  * that no other thread consume the next response iocb.
437  * SLI-2/SLI-3 provide different sized iocbs.
438  **/
439 static inline IOCB_t *
440 lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
441 {
442         return (IOCB_t *) (((char *) pring->rspringaddr) +
443                            pring->rspidx * phba->iocb_rsp_size);
444 }
445
446 /**
447  * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
448  * @phba: Pointer to HBA context object.
449  *
450  * This function is called with hbalock held. This function
451  * allocates a new driver iocb object from the iocb pool. If the
452  * allocation is successful, it returns pointer to the newly
453  * allocated iocb object else it returns NULL.
454  **/
455 static struct lpfc_iocbq *
456 __lpfc_sli_get_iocbq(struct lpfc_hba *phba)
457 {
458         struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
459         struct lpfc_iocbq * iocbq = NULL;
460
461         list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
462
463         if (iocbq)
464                 phba->iocb_cnt++;
465         if (phba->iocb_cnt > phba->iocb_max)
466                 phba->iocb_max = phba->iocb_cnt;
467         return iocbq;
468 }
469
470 /**
471  * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
472  * @phba: Pointer to HBA context object.
473  * @xritag: XRI value.
474  *
475  * This function clears the sglq pointer from the array of acive
476  * sglq's. The xritag that is passed in is used to index into the
477  * array. Before the xritag can be used it needs to be adjusted
478  * by subtracting the xribase.
479  *
480  * Returns sglq ponter = success, NULL = Failure.
481  **/
482 static struct lpfc_sglq *
483 __lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
484 {
485         uint16_t adj_xri;
486         struct lpfc_sglq *sglq;
487         adj_xri = xritag - phba->sli4_hba.max_cfg_param.xri_base;
488         if (adj_xri > phba->sli4_hba.max_cfg_param.max_xri)
489                 return NULL;
490         sglq = phba->sli4_hba.lpfc_sglq_active_list[adj_xri];
491         phba->sli4_hba.lpfc_sglq_active_list[adj_xri] = NULL;
492         return sglq;
493 }
494
495 /**
496  * __lpfc_get_active_sglq - Get the active sglq for this XRI.
497  * @phba: Pointer to HBA context object.
498  * @xritag: XRI value.
499  *
500  * This function returns the sglq pointer from the array of acive
501  * sglq's. The xritag that is passed in is used to index into the
502  * array. Before the xritag can be used it needs to be adjusted
503  * by subtracting the xribase.
504  *
505  * Returns sglq ponter = success, NULL = Failure.
506  **/
507 struct lpfc_sglq *
508 __lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
509 {
510         uint16_t adj_xri;
511         struct lpfc_sglq *sglq;
512         adj_xri = xritag - phba->sli4_hba.max_cfg_param.xri_base;
513         if (adj_xri > phba->sli4_hba.max_cfg_param.max_xri)
514                 return NULL;
515         sglq =  phba->sli4_hba.lpfc_sglq_active_list[adj_xri];
516         return sglq;
517 }
518
519 /**
520  * __lpfc_set_rrq_active - set RRQ active bit in the ndlp's xri_bitmap.
521  * @phba: Pointer to HBA context object.
522  * @ndlp: nodelist pointer for this target.
523  * @xritag: xri used in this exchange.
524  * @rxid: Remote Exchange ID.
525  * @send_rrq: Flag used to determine if we should send rrq els cmd.
526  *
527  * This function is called with hbalock held.
528  * The active bit is set in the ndlp's active rrq xri_bitmap. Allocates an
529  * rrq struct and adds it to the active_rrq_list.
530  *
531  * returns  0 for rrq slot for this xri
532  *         < 0  Were not able to get rrq mem or invalid parameter.
533  **/
534 static int
535 __lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
536                 uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
537 {
538         uint16_t adj_xri;
539         struct lpfc_node_rrq *rrq;
540         int empty;
541         uint32_t did = 0;
542
543
544         if (!ndlp)
545                 return -EINVAL;
546
547         if (!phba->cfg_enable_rrq)
548                 return -EINVAL;
549
550         if (phba->pport->load_flag & FC_UNLOADING) {
551                 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
552                 goto out;
553         }
554         did = ndlp->nlp_DID;
555
556         /*
557          * set the active bit even if there is no mem available.
558          */
559         adj_xri = xritag - phba->sli4_hba.max_cfg_param.xri_base;
560
561         if (NLP_CHK_FREE_REQ(ndlp))
562                 goto out;
563
564         if (ndlp->vport && (ndlp->vport->load_flag & FC_UNLOADING))
565                 goto out;
566
567         if (test_and_set_bit(adj_xri, ndlp->active_rrqs.xri_bitmap))
568                 goto out;
569
570         rrq = mempool_alloc(phba->rrq_pool, GFP_KERNEL);
571         if (rrq) {
572                 rrq->send_rrq = send_rrq;
573                 rrq->xritag = xritag;
574                 rrq->rrq_stop_time = jiffies + HZ * (phba->fc_ratov + 1);
575                 rrq->ndlp = ndlp;
576                 rrq->nlp_DID = ndlp->nlp_DID;
577                 rrq->vport = ndlp->vport;
578                 rrq->rxid = rxid;
579                 empty = list_empty(&phba->active_rrq_list);
580                 rrq->send_rrq = send_rrq;
581                 list_add_tail(&rrq->list, &phba->active_rrq_list);
582                 if (!(phba->hba_flag & HBA_RRQ_ACTIVE)) {
583                         phba->hba_flag |= HBA_RRQ_ACTIVE;
584                         if (empty)
585                                 lpfc_worker_wake_up(phba);
586                 }
587                 return 0;
588         }
589 out:
590         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
591                         "2921 Can't set rrq active xri:0x%x rxid:0x%x"
592                         " DID:0x%x Send:%d\n",
593                         xritag, rxid, did, send_rrq);
594         return -EINVAL;
595 }
596
597 /**
598  * lpfc_clr_rrq_active - Clears RRQ active bit in xri_bitmap.
599  * @phba: Pointer to HBA context object.
600  * @xritag: xri used in this exchange.
601  * @rrq: The RRQ to be cleared.
602  *
603  **/
604 void
605 lpfc_clr_rrq_active(struct lpfc_hba *phba,
606                     uint16_t xritag,
607                     struct lpfc_node_rrq *rrq)
608 {
609         uint16_t adj_xri;
610         struct lpfc_nodelist *ndlp = NULL;
611
612         if ((rrq->vport) && NLP_CHK_NODE_ACT(rrq->ndlp))
613                 ndlp = lpfc_findnode_did(rrq->vport, rrq->nlp_DID);
614
615         /* The target DID could have been swapped (cable swap)
616          * we should use the ndlp from the findnode if it is
617          * available.
618          */
619         if ((!ndlp) && rrq->ndlp)
620                 ndlp = rrq->ndlp;
621
622         if (!ndlp)
623                 goto out;
624
625         adj_xri = xritag - phba->sli4_hba.max_cfg_param.xri_base;
626         if (test_and_clear_bit(adj_xri, ndlp->active_rrqs.xri_bitmap)) {
627                 rrq->send_rrq = 0;
628                 rrq->xritag = 0;
629                 rrq->rrq_stop_time = 0;
630         }
631 out:
632         mempool_free(rrq, phba->rrq_pool);
633 }
634
635 /**
636  * lpfc_handle_rrq_active - Checks if RRQ has waithed RATOV.
637  * @phba: Pointer to HBA context object.
638  *
639  * This function is called with hbalock held. This function
640  * Checks if stop_time (ratov from setting rrq active) has
641  * been reached, if it has and the send_rrq flag is set then
642  * it will call lpfc_send_rrq. If the send_rrq flag is not set
643  * then it will just call the routine to clear the rrq and
644  * free the rrq resource.
645  * The timer is set to the next rrq that is going to expire before
646  * leaving the routine.
647  *
648  **/
649 void
650 lpfc_handle_rrq_active(struct lpfc_hba *phba)
651 {
652         struct lpfc_node_rrq *rrq;
653         struct lpfc_node_rrq *nextrrq;
654         unsigned long next_time;
655         unsigned long iflags;
656         LIST_HEAD(send_rrq);
657
658         spin_lock_irqsave(&phba->hbalock, iflags);
659         phba->hba_flag &= ~HBA_RRQ_ACTIVE;
660         next_time = jiffies + HZ * (phba->fc_ratov + 1);
661         list_for_each_entry_safe(rrq, nextrrq,
662                                  &phba->active_rrq_list, list) {
663                 if (time_after(jiffies, rrq->rrq_stop_time))
664                         list_move(&rrq->list, &send_rrq);
665                 else if (time_before(rrq->rrq_stop_time, next_time))
666                         next_time = rrq->rrq_stop_time;
667         }
668         spin_unlock_irqrestore(&phba->hbalock, iflags);
669         if (!list_empty(&phba->active_rrq_list))
670                 mod_timer(&phba->rrq_tmr, next_time);
671         list_for_each_entry_safe(rrq, nextrrq, &send_rrq, list) {
672                 list_del(&rrq->list);
673                 if (!rrq->send_rrq)
674                         /* this call will free the rrq */
675                 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
676                 else if (lpfc_send_rrq(phba, rrq)) {
677                         /* if we send the rrq then the completion handler
678                         *  will clear the bit in the xribitmap.
679                         */
680                         lpfc_clr_rrq_active(phba, rrq->xritag,
681                                             rrq);
682                 }
683         }
684 }
685
686 /**
687  * lpfc_get_active_rrq - Get the active RRQ for this exchange.
688  * @vport: Pointer to vport context object.
689  * @xri: The xri used in the exchange.
690  * @did: The targets DID for this exchange.
691  *
692  * returns NULL = rrq not found in the phba->active_rrq_list.
693  *         rrq = rrq for this xri and target.
694  **/
695 struct lpfc_node_rrq *
696 lpfc_get_active_rrq(struct lpfc_vport *vport, uint16_t xri, uint32_t did)
697 {
698         struct lpfc_hba *phba = vport->phba;
699         struct lpfc_node_rrq *rrq;
700         struct lpfc_node_rrq *nextrrq;
701         unsigned long iflags;
702
703         if (phba->sli_rev != LPFC_SLI_REV4)
704                 return NULL;
705         spin_lock_irqsave(&phba->hbalock, iflags);
706         list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {
707                 if (rrq->vport == vport && rrq->xritag == xri &&
708                                 rrq->nlp_DID == did){
709                         list_del(&rrq->list);
710                         spin_unlock_irqrestore(&phba->hbalock, iflags);
711                         return rrq;
712                 }
713         }
714         spin_unlock_irqrestore(&phba->hbalock, iflags);
715         return NULL;
716 }
717
718 /**
719  * lpfc_cleanup_vports_rrqs - Remove and clear the active RRQ for this vport.
720  * @vport: Pointer to vport context object.
721  * @ndlp: Pointer to the lpfc_node_list structure.
722  * If ndlp is NULL Remove all active RRQs for this vport from the
723  * phba->active_rrq_list and clear the rrq.
724  * If ndlp is not NULL then only remove rrqs for this vport & this ndlp.
725  **/
726 void
727 lpfc_cleanup_vports_rrqs(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
728
729 {
730         struct lpfc_hba *phba = vport->phba;
731         struct lpfc_node_rrq *rrq;
732         struct lpfc_node_rrq *nextrrq;
733         unsigned long iflags;
734         LIST_HEAD(rrq_list);
735
736         if (phba->sli_rev != LPFC_SLI_REV4)
737                 return;
738         if (!ndlp) {
739                 lpfc_sli4_vport_delete_els_xri_aborted(vport);
740                 lpfc_sli4_vport_delete_fcp_xri_aborted(vport);
741         }
742         spin_lock_irqsave(&phba->hbalock, iflags);
743         list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list)
744                 if ((rrq->vport == vport) && (!ndlp  || rrq->ndlp == ndlp))
745                         list_move(&rrq->list, &rrq_list);
746         spin_unlock_irqrestore(&phba->hbalock, iflags);
747
748         list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
749                 list_del(&rrq->list);
750                 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
751         }
752 }
753
754 /**
755  * lpfc_cleanup_wt_rrqs - Remove all rrq's from the active list.
756  * @phba: Pointer to HBA context object.
757  *
758  * Remove all rrqs from the phba->active_rrq_list and free them by
759  * calling __lpfc_clr_active_rrq
760  *
761  **/
762 void
763 lpfc_cleanup_wt_rrqs(struct lpfc_hba *phba)
764 {
765         struct lpfc_node_rrq *rrq;
766         struct lpfc_node_rrq *nextrrq;
767         unsigned long next_time;
768         unsigned long iflags;
769         LIST_HEAD(rrq_list);
770
771         if (phba->sli_rev != LPFC_SLI_REV4)
772                 return;
773         spin_lock_irqsave(&phba->hbalock, iflags);
774         phba->hba_flag &= ~HBA_RRQ_ACTIVE;
775         next_time = jiffies + HZ * (phba->fc_ratov * 2);
776         list_splice_init(&phba->active_rrq_list, &rrq_list);
777         spin_unlock_irqrestore(&phba->hbalock, iflags);
778
779         list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
780                 list_del(&rrq->list);
781                 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
782         }
783         if (!list_empty(&phba->active_rrq_list))
784                 mod_timer(&phba->rrq_tmr, next_time);
785 }
786
787
788 /**
789  * lpfc_test_rrq_active - Test RRQ bit in xri_bitmap.
790  * @phba: Pointer to HBA context object.
791  * @ndlp: Targets nodelist pointer for this exchange.
792  * @xritag the xri in the bitmap to test.
793  *
794  * This function is called with hbalock held. This function
795  * returns 0 = rrq not active for this xri
796  *         1 = rrq is valid for this xri.
797  **/
798 int
799 lpfc_test_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
800                         uint16_t  xritag)
801 {
802         uint16_t adj_xri;
803
804         adj_xri = xritag - phba->sli4_hba.max_cfg_param.xri_base;
805         if (!ndlp)
806                 return 0;
807         if (test_bit(adj_xri, ndlp->active_rrqs.xri_bitmap))
808                         return 1;
809         else
810                 return 0;
811 }
812
813 /**
814  * lpfc_set_rrq_active - set RRQ active bit in xri_bitmap.
815  * @phba: Pointer to HBA context object.
816  * @ndlp: nodelist pointer for this target.
817  * @xritag: xri used in this exchange.
818  * @rxid: Remote Exchange ID.
819  * @send_rrq: Flag used to determine if we should send rrq els cmd.
820  *
821  * This function takes the hbalock.
822  * The active bit is always set in the active rrq xri_bitmap even
823  * if there is no slot avaiable for the other rrq information.
824  *
825  * returns 0 rrq actived for this xri
826  *         < 0 No memory or invalid ndlp.
827  **/
828 int
829 lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
830                         uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
831 {
832         int ret;
833         unsigned long iflags;
834
835         spin_lock_irqsave(&phba->hbalock, iflags);
836         ret = __lpfc_set_rrq_active(phba, ndlp, xritag, rxid, send_rrq);
837         spin_unlock_irqrestore(&phba->hbalock, iflags);
838         return ret;
839 }
840
841 /**
842  * __lpfc_sli_get_sglq - Allocates an iocb object from sgl pool
843  * @phba: Pointer to HBA context object.
844  * @piocb: Pointer to the iocbq.
845  *
846  * This function is called with hbalock held. This function
847  * Gets a new driver sglq object from the sglq list. If the
848  * list is not empty then it is successful, it returns pointer to the newly
849  * allocated sglq object else it returns NULL.
850  **/
851 static struct lpfc_sglq *
852 __lpfc_sli_get_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
853 {
854         struct list_head *lpfc_sgl_list = &phba->sli4_hba.lpfc_sgl_list;
855         struct lpfc_sglq *sglq = NULL;
856         struct lpfc_sglq *start_sglq = NULL;
857         uint16_t adj_xri;
858         struct lpfc_scsi_buf *lpfc_cmd;
859         struct lpfc_nodelist *ndlp;
860         int found = 0;
861
862         if (piocbq->iocb_flag &  LPFC_IO_FCP) {
863                 lpfc_cmd = (struct lpfc_scsi_buf *) piocbq->context1;
864                 ndlp = lpfc_cmd->rdata->pnode;
865         } else  if ((piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) &&
866                         !(piocbq->iocb_flag & LPFC_IO_LIBDFC))
867                 ndlp = piocbq->context_un.ndlp;
868         else
869                 ndlp = piocbq->context1;
870
871         list_remove_head(lpfc_sgl_list, sglq, struct lpfc_sglq, list);
872         start_sglq = sglq;
873         while (!found) {
874                 if (!sglq)
875                         return NULL;
876                 adj_xri = sglq->sli4_xritag -
877                                 phba->sli4_hba.max_cfg_param.xri_base;
878                 if (lpfc_test_rrq_active(phba, ndlp, sglq->sli4_xritag)) {
879                         /* This xri has an rrq outstanding for this DID.
880                          * put it back in the list and get another xri.
881                          */
882                         list_add_tail(&sglq->list, lpfc_sgl_list);
883                         sglq = NULL;
884                         list_remove_head(lpfc_sgl_list, sglq,
885                                                 struct lpfc_sglq, list);
886                         if (sglq == start_sglq) {
887                                 sglq = NULL;
888                                 break;
889                         } else
890                                 continue;
891                 }
892                 sglq->ndlp = ndlp;
893                 found = 1;
894                 phba->sli4_hba.lpfc_sglq_active_list[adj_xri] = sglq;
895                 sglq->state = SGL_ALLOCATED;
896         }
897         return sglq;
898 }
899
900 /**
901  * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
902  * @phba: Pointer to HBA context object.
903  *
904  * This function is called with no lock held. This function
905  * allocates a new driver iocb object from the iocb pool. If the
906  * allocation is successful, it returns pointer to the newly
907  * allocated iocb object else it returns NULL.
908  **/
909 struct lpfc_iocbq *
910 lpfc_sli_get_iocbq(struct lpfc_hba *phba)
911 {
912         struct lpfc_iocbq * iocbq = NULL;
913         unsigned long iflags;
914
915         spin_lock_irqsave(&phba->hbalock, iflags);
916         iocbq = __lpfc_sli_get_iocbq(phba);
917         spin_unlock_irqrestore(&phba->hbalock, iflags);
918         return iocbq;
919 }
920
921 /**
922  * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
923  * @phba: Pointer to HBA context object.
924  * @iocbq: Pointer to driver iocb object.
925  *
926  * This function is called with hbalock held to release driver
927  * iocb object to the iocb pool. The iotag in the iocb object
928  * does not change for each use of the iocb object. This function
929  * clears all other fields of the iocb object when it is freed.
930  * The sqlq structure that holds the xritag and phys and virtual
931  * mappings for the scatter gather list is retrieved from the
932  * active array of sglq. The get of the sglq pointer also clears
933  * the entry in the array. If the status of the IO indiactes that
934  * this IO was aborted then the sglq entry it put on the
935  * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
936  * IO has good status or fails for any other reason then the sglq
937  * entry is added to the free list (lpfc_sgl_list).
938  **/
939 static void
940 __lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
941 {
942         struct lpfc_sglq *sglq;
943         size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
944         unsigned long iflag = 0;
945         struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
946
947         if (iocbq->sli4_xritag == NO_XRI)
948                 sglq = NULL;
949         else
950                 sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_xritag);
951         if (sglq)  {
952                 if ((iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) &&
953                         (sglq->state != SGL_XRI_ABORTED)) {
954                         spin_lock_irqsave(&phba->sli4_hba.abts_sgl_list_lock,
955                                         iflag);
956                         list_add(&sglq->list,
957                                 &phba->sli4_hba.lpfc_abts_els_sgl_list);
958                         spin_unlock_irqrestore(
959                                 &phba->sli4_hba.abts_sgl_list_lock, iflag);
960                 } else {
961                         sglq->state = SGL_FREED;
962                         sglq->ndlp = NULL;
963                         list_add_tail(&sglq->list,
964                                 &phba->sli4_hba.lpfc_sgl_list);
965
966                         /* Check if TXQ queue needs to be serviced */
967                         if (pring->txq_cnt)
968                                 lpfc_worker_wake_up(phba);
969                 }
970         }
971
972
973         /*
974          * Clean all volatile data fields, preserve iotag and node struct.
975          */
976         memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
977         iocbq->sli4_xritag = NO_XRI;
978         list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
979 }
980
981
982 /**
983  * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
984  * @phba: Pointer to HBA context object.
985  * @iocbq: Pointer to driver iocb object.
986  *
987  * This function is called with hbalock held to release driver
988  * iocb object to the iocb pool. The iotag in the iocb object
989  * does not change for each use of the iocb object. This function
990  * clears all other fields of the iocb object when it is freed.
991  **/
992 static void
993 __lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
994 {
995         size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
996
997         /*
998          * Clean all volatile data fields, preserve iotag and node struct.
999          */
1000         memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
1001         iocbq->sli4_xritag = NO_XRI;
1002         list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1003 }
1004
1005 /**
1006  * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
1007  * @phba: Pointer to HBA context object.
1008  * @iocbq: Pointer to driver iocb object.
1009  *
1010  * This function is called with hbalock held to release driver
1011  * iocb object to the iocb pool. The iotag in the iocb object
1012  * does not change for each use of the iocb object. This function
1013  * clears all other fields of the iocb object when it is freed.
1014  **/
1015 static void
1016 __lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1017 {
1018         phba->__lpfc_sli_release_iocbq(phba, iocbq);
1019         phba->iocb_cnt--;
1020 }
1021
1022 /**
1023  * lpfc_sli_release_iocbq - Release iocb to the iocb pool
1024  * @phba: Pointer to HBA context object.
1025  * @iocbq: Pointer to driver iocb object.
1026  *
1027  * This function is called with no lock held to release the iocb to
1028  * iocb pool.
1029  **/
1030 void
1031 lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1032 {
1033         unsigned long iflags;
1034
1035         /*
1036          * Clean all volatile data fields, preserve iotag and node struct.
1037          */
1038         spin_lock_irqsave(&phba->hbalock, iflags);
1039         __lpfc_sli_release_iocbq(phba, iocbq);
1040         spin_unlock_irqrestore(&phba->hbalock, iflags);
1041 }
1042
1043 /**
1044  * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
1045  * @phba: Pointer to HBA context object.
1046  * @iocblist: List of IOCBs.
1047  * @ulpstatus: ULP status in IOCB command field.
1048  * @ulpWord4: ULP word-4 in IOCB command field.
1049  *
1050  * This function is called with a list of IOCBs to cancel. It cancels the IOCB
1051  * on the list by invoking the complete callback function associated with the
1052  * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
1053  * fields.
1054  **/
1055 void
1056 lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
1057                       uint32_t ulpstatus, uint32_t ulpWord4)
1058 {
1059         struct lpfc_iocbq *piocb;
1060
1061         while (!list_empty(iocblist)) {
1062                 list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
1063
1064                 if (!piocb->iocb_cmpl)
1065                         lpfc_sli_release_iocbq(phba, piocb);
1066                 else {
1067                         piocb->iocb.ulpStatus = ulpstatus;
1068                         piocb->iocb.un.ulpWord[4] = ulpWord4;
1069                         (piocb->iocb_cmpl) (phba, piocb, piocb);
1070                 }
1071         }
1072         return;
1073 }
1074
1075 /**
1076  * lpfc_sli_iocb_cmd_type - Get the iocb type
1077  * @iocb_cmnd: iocb command code.
1078  *
1079  * This function is called by ring event handler function to get the iocb type.
1080  * This function translates the iocb command to an iocb command type used to
1081  * decide the final disposition of each completed IOCB.
1082  * The function returns
1083  * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
1084  * LPFC_SOL_IOCB     if it is a solicited iocb completion
1085  * LPFC_ABORT_IOCB   if it is an abort iocb
1086  * LPFC_UNSOL_IOCB   if it is an unsolicited iocb
1087  *
1088  * The caller is not required to hold any lock.
1089  **/
1090 static lpfc_iocb_type
1091 lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
1092 {
1093         lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
1094
1095         if (iocb_cmnd > CMD_MAX_IOCB_CMD)
1096                 return 0;
1097
1098         switch (iocb_cmnd) {
1099         case CMD_XMIT_SEQUENCE_CR:
1100         case CMD_XMIT_SEQUENCE_CX:
1101         case CMD_XMIT_BCAST_CN:
1102         case CMD_XMIT_BCAST_CX:
1103         case CMD_ELS_REQUEST_CR:
1104         case CMD_ELS_REQUEST_CX:
1105         case CMD_CREATE_XRI_CR:
1106         case CMD_CREATE_XRI_CX:
1107         case CMD_GET_RPI_CN:
1108         case CMD_XMIT_ELS_RSP_CX:
1109         case CMD_GET_RPI_CR:
1110         case CMD_FCP_IWRITE_CR:
1111         case CMD_FCP_IWRITE_CX:
1112         case CMD_FCP_IREAD_CR:
1113         case CMD_FCP_IREAD_CX:
1114         case CMD_FCP_ICMND_CR:
1115         case CMD_FCP_ICMND_CX:
1116         case CMD_FCP_TSEND_CX:
1117         case CMD_FCP_TRSP_CX:
1118         case CMD_FCP_TRECEIVE_CX:
1119         case CMD_FCP_AUTO_TRSP_CX:
1120         case CMD_ADAPTER_MSG:
1121         case CMD_ADAPTER_DUMP:
1122         case CMD_XMIT_SEQUENCE64_CR:
1123         case CMD_XMIT_SEQUENCE64_CX:
1124         case CMD_XMIT_BCAST64_CN:
1125         case CMD_XMIT_BCAST64_CX:
1126         case CMD_ELS_REQUEST64_CR:
1127         case CMD_ELS_REQUEST64_CX:
1128         case CMD_FCP_IWRITE64_CR:
1129         case CMD_FCP_IWRITE64_CX:
1130         case CMD_FCP_IREAD64_CR:
1131         case CMD_FCP_IREAD64_CX:
1132         case CMD_FCP_ICMND64_CR:
1133         case CMD_FCP_ICMND64_CX:
1134         case CMD_FCP_TSEND64_CX:
1135         case CMD_FCP_TRSP64_CX:
1136         case CMD_FCP_TRECEIVE64_CX:
1137         case CMD_GEN_REQUEST64_CR:
1138         case CMD_GEN_REQUEST64_CX:
1139         case CMD_XMIT_ELS_RSP64_CX:
1140         case DSSCMD_IWRITE64_CR:
1141         case DSSCMD_IWRITE64_CX:
1142         case DSSCMD_IREAD64_CR:
1143         case DSSCMD_IREAD64_CX:
1144                 type = LPFC_SOL_IOCB;
1145                 break;
1146         case CMD_ABORT_XRI_CN:
1147         case CMD_ABORT_XRI_CX:
1148         case CMD_CLOSE_XRI_CN:
1149         case CMD_CLOSE_XRI_CX:
1150         case CMD_XRI_ABORTED_CX:
1151         case CMD_ABORT_MXRI64_CN:
1152         case CMD_XMIT_BLS_RSP64_CX:
1153                 type = LPFC_ABORT_IOCB;
1154                 break;
1155         case CMD_RCV_SEQUENCE_CX:
1156         case CMD_RCV_ELS_REQ_CX:
1157         case CMD_RCV_SEQUENCE64_CX:
1158         case CMD_RCV_ELS_REQ64_CX:
1159         case CMD_ASYNC_STATUS:
1160         case CMD_IOCB_RCV_SEQ64_CX:
1161         case CMD_IOCB_RCV_ELS64_CX:
1162         case CMD_IOCB_RCV_CONT64_CX:
1163         case CMD_IOCB_RET_XRI64_CX:
1164                 type = LPFC_UNSOL_IOCB;
1165                 break;
1166         case CMD_IOCB_XMIT_MSEQ64_CR:
1167         case CMD_IOCB_XMIT_MSEQ64_CX:
1168         case CMD_IOCB_RCV_SEQ_LIST64_CX:
1169         case CMD_IOCB_RCV_ELS_LIST64_CX:
1170         case CMD_IOCB_CLOSE_EXTENDED_CN:
1171         case CMD_IOCB_ABORT_EXTENDED_CN:
1172         case CMD_IOCB_RET_HBQE64_CN:
1173         case CMD_IOCB_FCP_IBIDIR64_CR:
1174         case CMD_IOCB_FCP_IBIDIR64_CX:
1175         case CMD_IOCB_FCP_ITASKMGT64_CX:
1176         case CMD_IOCB_LOGENTRY_CN:
1177         case CMD_IOCB_LOGENTRY_ASYNC_CN:
1178                 printk("%s - Unhandled SLI-3 Command x%x\n",
1179                                 __func__, iocb_cmnd);
1180                 type = LPFC_UNKNOWN_IOCB;
1181                 break;
1182         default:
1183                 type = LPFC_UNKNOWN_IOCB;
1184                 break;
1185         }
1186
1187         return type;
1188 }
1189
1190 /**
1191  * lpfc_sli_ring_map - Issue config_ring mbox for all rings
1192  * @phba: Pointer to HBA context object.
1193  *
1194  * This function is called from SLI initialization code
1195  * to configure every ring of the HBA's SLI interface. The
1196  * caller is not required to hold any lock. This function issues
1197  * a config_ring mailbox command for each ring.
1198  * This function returns zero if successful else returns a negative
1199  * error code.
1200  **/
1201 static int
1202 lpfc_sli_ring_map(struct lpfc_hba *phba)
1203 {
1204         struct lpfc_sli *psli = &phba->sli;
1205         LPFC_MBOXQ_t *pmb;
1206         MAILBOX_t *pmbox;
1207         int i, rc, ret = 0;
1208
1209         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1210         if (!pmb)
1211                 return -ENOMEM;
1212         pmbox = &pmb->u.mb;
1213         phba->link_state = LPFC_INIT_MBX_CMDS;
1214         for (i = 0; i < psli->num_rings; i++) {
1215                 lpfc_config_ring(phba, i, pmb);
1216                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
1217                 if (rc != MBX_SUCCESS) {
1218                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1219                                         "0446 Adapter failed to init (%d), "
1220                                         "mbxCmd x%x CFG_RING, mbxStatus x%x, "
1221                                         "ring %d\n",
1222                                         rc, pmbox->mbxCommand,
1223                                         pmbox->mbxStatus, i);
1224                         phba->link_state = LPFC_HBA_ERROR;
1225                         ret = -ENXIO;
1226                         break;
1227                 }
1228         }
1229         mempool_free(pmb, phba->mbox_mem_pool);
1230         return ret;
1231 }
1232
1233 /**
1234  * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
1235  * @phba: Pointer to HBA context object.
1236  * @pring: Pointer to driver SLI ring object.
1237  * @piocb: Pointer to the driver iocb object.
1238  *
1239  * This function is called with hbalock held. The function adds the
1240  * new iocb to txcmplq of the given ring. This function always returns
1241  * 0. If this function is called for ELS ring, this function checks if
1242  * there is a vport associated with the ELS command. This function also
1243  * starts els_tmofunc timer if this is an ELS command.
1244  **/
1245 static int
1246 lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1247                         struct lpfc_iocbq *piocb)
1248 {
1249         list_add_tail(&piocb->list, &pring->txcmplq);
1250         piocb->iocb_flag |= LPFC_IO_ON_Q;
1251         pring->txcmplq_cnt++;
1252         if (pring->txcmplq_cnt > pring->txcmplq_max)
1253                 pring->txcmplq_max = pring->txcmplq_cnt;
1254
1255         if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
1256            (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
1257            (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
1258                 if (!piocb->vport)
1259                         BUG();
1260                 else
1261                         mod_timer(&piocb->vport->els_tmofunc,
1262                                   jiffies + HZ * (phba->fc_ratov << 1));
1263         }
1264
1265
1266         return 0;
1267 }
1268
1269 /**
1270  * lpfc_sli_ringtx_get - Get first element of the txq
1271  * @phba: Pointer to HBA context object.
1272  * @pring: Pointer to driver SLI ring object.
1273  *
1274  * This function is called with hbalock held to get next
1275  * iocb in txq of the given ring. If there is any iocb in
1276  * the txq, the function returns first iocb in the list after
1277  * removing the iocb from the list, else it returns NULL.
1278  **/
1279 struct lpfc_iocbq *
1280 lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1281 {
1282         struct lpfc_iocbq *cmd_iocb;
1283
1284         list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
1285         if (cmd_iocb != NULL)
1286                 pring->txq_cnt--;
1287         return cmd_iocb;
1288 }
1289
1290 /**
1291  * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
1292  * @phba: Pointer to HBA context object.
1293  * @pring: Pointer to driver SLI ring object.
1294  *
1295  * This function is called with hbalock held and the caller must post the
1296  * iocb without releasing the lock. If the caller releases the lock,
1297  * iocb slot returned by the function is not guaranteed to be available.
1298  * The function returns pointer to the next available iocb slot if there
1299  * is available slot in the ring, else it returns NULL.
1300  * If the get index of the ring is ahead of the put index, the function
1301  * will post an error attention event to the worker thread to take the
1302  * HBA to offline state.
1303  **/
1304 static IOCB_t *
1305 lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1306 {
1307         struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
1308         uint32_t  max_cmd_idx = pring->numCiocb;
1309         if ((pring->next_cmdidx == pring->cmdidx) &&
1310            (++pring->next_cmdidx >= max_cmd_idx))
1311                 pring->next_cmdidx = 0;
1312
1313         if (unlikely(pring->local_getidx == pring->next_cmdidx)) {
1314
1315                 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
1316
1317                 if (unlikely(pring->local_getidx >= max_cmd_idx)) {
1318                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
1319                                         "0315 Ring %d issue: portCmdGet %d "
1320                                         "is bigger than cmd ring %d\n",
1321                                         pring->ringno,
1322                                         pring->local_getidx, max_cmd_idx);
1323
1324                         phba->link_state = LPFC_HBA_ERROR;
1325                         /*
1326                          * All error attention handlers are posted to
1327                          * worker thread
1328                          */
1329                         phba->work_ha |= HA_ERATT;
1330                         phba->work_hs = HS_FFER3;
1331
1332                         lpfc_worker_wake_up(phba);
1333
1334                         return NULL;
1335                 }
1336
1337                 if (pring->local_getidx == pring->next_cmdidx)
1338                         return NULL;
1339         }
1340
1341         return lpfc_cmd_iocb(phba, pring);
1342 }
1343
1344 /**
1345  * lpfc_sli_next_iotag - Get an iotag for the iocb
1346  * @phba: Pointer to HBA context object.
1347  * @iocbq: Pointer to driver iocb object.
1348  *
1349  * This function gets an iotag for the iocb. If there is no unused iotag and
1350  * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
1351  * array and assigns a new iotag.
1352  * The function returns the allocated iotag if successful, else returns zero.
1353  * Zero is not a valid iotag.
1354  * The caller is not required to hold any lock.
1355  **/
1356 uint16_t
1357 lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1358 {
1359         struct lpfc_iocbq **new_arr;
1360         struct lpfc_iocbq **old_arr;
1361         size_t new_len;
1362         struct lpfc_sli *psli = &phba->sli;
1363         uint16_t iotag;
1364
1365         spin_lock_irq(&phba->hbalock);
1366         iotag = psli->last_iotag;
1367         if(++iotag < psli->iocbq_lookup_len) {
1368                 psli->last_iotag = iotag;
1369                 psli->iocbq_lookup[iotag] = iocbq;
1370                 spin_unlock_irq(&phba->hbalock);
1371                 iocbq->iotag = iotag;
1372                 return iotag;
1373         } else if (psli->iocbq_lookup_len < (0xffff
1374                                            - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
1375                 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
1376                 spin_unlock_irq(&phba->hbalock);
1377                 new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
1378                                   GFP_KERNEL);
1379                 if (new_arr) {
1380                         spin_lock_irq(&phba->hbalock);
1381                         old_arr = psli->iocbq_lookup;
1382                         if (new_len <= psli->iocbq_lookup_len) {
1383                                 /* highly unprobable case */
1384                                 kfree(new_arr);
1385                                 iotag = psli->last_iotag;
1386                                 if(++iotag < psli->iocbq_lookup_len) {
1387                                         psli->last_iotag = iotag;
1388                                         psli->iocbq_lookup[iotag] = iocbq;
1389                                         spin_unlock_irq(&phba->hbalock);
1390                                         iocbq->iotag = iotag;
1391                                         return iotag;
1392                                 }
1393                                 spin_unlock_irq(&phba->hbalock);
1394                                 return 0;
1395                         }
1396                         if (psli->iocbq_lookup)
1397                                 memcpy(new_arr, old_arr,
1398                                        ((psli->last_iotag  + 1) *
1399                                         sizeof (struct lpfc_iocbq *)));
1400                         psli->iocbq_lookup = new_arr;
1401                         psli->iocbq_lookup_len = new_len;
1402                         psli->last_iotag = iotag;
1403                         psli->iocbq_lookup[iotag] = iocbq;
1404                         spin_unlock_irq(&phba->hbalock);
1405                         iocbq->iotag = iotag;
1406                         kfree(old_arr);
1407                         return iotag;
1408                 }
1409         } else
1410                 spin_unlock_irq(&phba->hbalock);
1411
1412         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
1413                         "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
1414                         psli->last_iotag);
1415
1416         return 0;
1417 }
1418
1419 /**
1420  * lpfc_sli_submit_iocb - Submit an iocb to the firmware
1421  * @phba: Pointer to HBA context object.
1422  * @pring: Pointer to driver SLI ring object.
1423  * @iocb: Pointer to iocb slot in the ring.
1424  * @nextiocb: Pointer to driver iocb object which need to be
1425  *            posted to firmware.
1426  *
1427  * This function is called with hbalock held to post a new iocb to
1428  * the firmware. This function copies the new iocb to ring iocb slot and
1429  * updates the ring pointers. It adds the new iocb to txcmplq if there is
1430  * a completion call back for this iocb else the function will free the
1431  * iocb object.
1432  **/
1433 static void
1434 lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1435                 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
1436 {
1437         /*
1438          * Set up an iotag
1439          */
1440         nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
1441
1442
1443         if (pring->ringno == LPFC_ELS_RING) {
1444                 lpfc_debugfs_slow_ring_trc(phba,
1445                         "IOCB cmd ring:   wd4:x%08x wd6:x%08x wd7:x%08x",
1446                         *(((uint32_t *) &nextiocb->iocb) + 4),
1447                         *(((uint32_t *) &nextiocb->iocb) + 6),
1448                         *(((uint32_t *) &nextiocb->iocb) + 7));
1449         }
1450
1451         /*
1452          * Issue iocb command to adapter
1453          */
1454         lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
1455         wmb();
1456         pring->stats.iocb_cmd++;
1457
1458         /*
1459          * If there is no completion routine to call, we can release the
1460          * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
1461          * that have no rsp ring completion, iocb_cmpl MUST be NULL.
1462          */
1463         if (nextiocb->iocb_cmpl)
1464                 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
1465         else
1466                 __lpfc_sli_release_iocbq(phba, nextiocb);
1467
1468         /*
1469          * Let the HBA know what IOCB slot will be the next one the
1470          * driver will put a command into.
1471          */
1472         pring->cmdidx = pring->next_cmdidx;
1473         writel(pring->cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
1474 }
1475
1476 /**
1477  * lpfc_sli_update_full_ring - Update the chip attention register
1478  * @phba: Pointer to HBA context object.
1479  * @pring: Pointer to driver SLI ring object.
1480  *
1481  * The caller is not required to hold any lock for calling this function.
1482  * This function updates the chip attention bits for the ring to inform firmware
1483  * that there are pending work to be done for this ring and requests an
1484  * interrupt when there is space available in the ring. This function is
1485  * called when the driver is unable to post more iocbs to the ring due
1486  * to unavailability of space in the ring.
1487  **/
1488 static void
1489 lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1490 {
1491         int ringno = pring->ringno;
1492
1493         pring->flag |= LPFC_CALL_RING_AVAILABLE;
1494
1495         wmb();
1496
1497         /*
1498          * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
1499          * The HBA will tell us when an IOCB entry is available.
1500          */
1501         writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
1502         readl(phba->CAregaddr); /* flush */
1503
1504         pring->stats.iocb_cmd_full++;
1505 }
1506
1507 /**
1508  * lpfc_sli_update_ring - Update chip attention register
1509  * @phba: Pointer to HBA context object.
1510  * @pring: Pointer to driver SLI ring object.
1511  *
1512  * This function updates the chip attention register bit for the
1513  * given ring to inform HBA that there is more work to be done
1514  * in this ring. The caller is not required to hold any lock.
1515  **/
1516 static void
1517 lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1518 {
1519         int ringno = pring->ringno;
1520
1521         /*
1522          * Tell the HBA that there is work to do in this ring.
1523          */
1524         if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
1525                 wmb();
1526                 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
1527                 readl(phba->CAregaddr); /* flush */
1528         }
1529 }
1530
1531 /**
1532  * lpfc_sli_resume_iocb - Process iocbs in the txq
1533  * @phba: Pointer to HBA context object.
1534  * @pring: Pointer to driver SLI ring object.
1535  *
1536  * This function is called with hbalock held to post pending iocbs
1537  * in the txq to the firmware. This function is called when driver
1538  * detects space available in the ring.
1539  **/
1540 static void
1541 lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1542 {
1543         IOCB_t *iocb;
1544         struct lpfc_iocbq *nextiocb;
1545
1546         /*
1547          * Check to see if:
1548          *  (a) there is anything on the txq to send
1549          *  (b) link is up
1550          *  (c) link attention events can be processed (fcp ring only)
1551          *  (d) IOCB processing is not blocked by the outstanding mbox command.
1552          */
1553         if (pring->txq_cnt &&
1554             lpfc_is_link_up(phba) &&
1555             (pring->ringno != phba->sli.fcp_ring ||
1556              phba->sli.sli_flag & LPFC_PROCESS_LA)) {
1557
1558                 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
1559                        (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
1560                         lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
1561
1562                 if (iocb)
1563                         lpfc_sli_update_ring(phba, pring);
1564                 else
1565                         lpfc_sli_update_full_ring(phba, pring);
1566         }
1567
1568         return;
1569 }
1570
1571 /**
1572  * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
1573  * @phba: Pointer to HBA context object.
1574  * @hbqno: HBQ number.
1575  *
1576  * This function is called with hbalock held to get the next
1577  * available slot for the given HBQ. If there is free slot
1578  * available for the HBQ it will return pointer to the next available
1579  * HBQ entry else it will return NULL.
1580  **/
1581 static struct lpfc_hbq_entry *
1582 lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
1583 {
1584         struct hbq_s *hbqp = &phba->hbqs[hbqno];
1585
1586         if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
1587             ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
1588                 hbqp->next_hbqPutIdx = 0;
1589
1590         if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
1591                 uint32_t raw_index = phba->hbq_get[hbqno];
1592                 uint32_t getidx = le32_to_cpu(raw_index);
1593
1594                 hbqp->local_hbqGetIdx = getidx;
1595
1596                 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
1597                         lpfc_printf_log(phba, KERN_ERR,
1598                                         LOG_SLI | LOG_VPORT,
1599                                         "1802 HBQ %d: local_hbqGetIdx "
1600                                         "%u is > than hbqp->entry_count %u\n",
1601                                         hbqno, hbqp->local_hbqGetIdx,
1602                                         hbqp->entry_count);
1603
1604                         phba->link_state = LPFC_HBA_ERROR;
1605                         return NULL;
1606                 }
1607
1608                 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
1609                         return NULL;
1610         }
1611
1612         return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
1613                         hbqp->hbqPutIdx;
1614 }
1615
1616 /**
1617  * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
1618  * @phba: Pointer to HBA context object.
1619  *
1620  * This function is called with no lock held to free all the
1621  * hbq buffers while uninitializing the SLI interface. It also
1622  * frees the HBQ buffers returned by the firmware but not yet
1623  * processed by the upper layers.
1624  **/
1625 void
1626 lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
1627 {
1628         struct lpfc_dmabuf *dmabuf, *next_dmabuf;
1629         struct hbq_dmabuf *hbq_buf;
1630         unsigned long flags;
1631         int i, hbq_count;
1632         uint32_t hbqno;
1633
1634         hbq_count = lpfc_sli_hbq_count();
1635         /* Return all memory used by all HBQs */
1636         spin_lock_irqsave(&phba->hbalock, flags);
1637         for (i = 0; i < hbq_count; ++i) {
1638                 list_for_each_entry_safe(dmabuf, next_dmabuf,
1639                                 &phba->hbqs[i].hbq_buffer_list, list) {
1640                         hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1641                         list_del(&hbq_buf->dbuf.list);
1642                         (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
1643                 }
1644                 phba->hbqs[i].buffer_count = 0;
1645         }
1646         /* Return all HBQ buffer that are in-fly */
1647         list_for_each_entry_safe(dmabuf, next_dmabuf, &phba->rb_pend_list,
1648                                  list) {
1649                 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1650                 list_del(&hbq_buf->dbuf.list);
1651                 if (hbq_buf->tag == -1) {
1652                         (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1653                                 (phba, hbq_buf);
1654                 } else {
1655                         hbqno = hbq_buf->tag >> 16;
1656                         if (hbqno >= LPFC_MAX_HBQS)
1657                                 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1658                                         (phba, hbq_buf);
1659                         else
1660                                 (phba->hbqs[hbqno].hbq_free_buffer)(phba,
1661                                         hbq_buf);
1662                 }
1663         }
1664
1665         /* Mark the HBQs not in use */
1666         phba->hbq_in_use = 0;
1667         spin_unlock_irqrestore(&phba->hbalock, flags);
1668 }
1669
1670 /**
1671  * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
1672  * @phba: Pointer to HBA context object.
1673  * @hbqno: HBQ number.
1674  * @hbq_buf: Pointer to HBQ buffer.
1675  *
1676  * This function is called with the hbalock held to post a
1677  * hbq buffer to the firmware. If the function finds an empty
1678  * slot in the HBQ, it will post the buffer. The function will return
1679  * pointer to the hbq entry if it successfully post the buffer
1680  * else it will return NULL.
1681  **/
1682 static int
1683 lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
1684                          struct hbq_dmabuf *hbq_buf)
1685 {
1686         return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
1687 }
1688
1689 /**
1690  * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
1691  * @phba: Pointer to HBA context object.
1692  * @hbqno: HBQ number.
1693  * @hbq_buf: Pointer to HBQ buffer.
1694  *
1695  * This function is called with the hbalock held to post a hbq buffer to the
1696  * firmware. If the function finds an empty slot in the HBQ, it will post the
1697  * buffer and place it on the hbq_buffer_list. The function will return zero if
1698  * it successfully post the buffer else it will return an error.
1699  **/
1700 static int
1701 lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
1702                             struct hbq_dmabuf *hbq_buf)
1703 {
1704         struct lpfc_hbq_entry *hbqe;
1705         dma_addr_t physaddr = hbq_buf->dbuf.phys;
1706
1707         /* Get next HBQ entry slot to use */
1708         hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
1709         if (hbqe) {
1710                 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1711
1712                 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1713                 hbqe->bde.addrLow  = le32_to_cpu(putPaddrLow(physaddr));
1714                 hbqe->bde.tus.f.bdeSize = hbq_buf->size;
1715                 hbqe->bde.tus.f.bdeFlags = 0;
1716                 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
1717                 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
1718                                 /* Sync SLIM */
1719                 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
1720                 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
1721                                 /* flush */
1722                 readl(phba->hbq_put + hbqno);
1723                 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
1724                 return 0;
1725         } else
1726                 return -ENOMEM;
1727 }
1728
1729 /**
1730  * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
1731  * @phba: Pointer to HBA context object.
1732  * @hbqno: HBQ number.
1733  * @hbq_buf: Pointer to HBQ buffer.
1734  *
1735  * This function is called with the hbalock held to post an RQE to the SLI4
1736  * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
1737  * the hbq_buffer_list and return zero, otherwise it will return an error.
1738  **/
1739 static int
1740 lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
1741                             struct hbq_dmabuf *hbq_buf)
1742 {
1743         int rc;
1744         struct lpfc_rqe hrqe;
1745         struct lpfc_rqe drqe;
1746
1747         hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
1748         hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
1749         drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
1750         drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
1751         rc = lpfc_sli4_rq_put(phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
1752                               &hrqe, &drqe);
1753         if (rc < 0)
1754                 return rc;
1755         hbq_buf->tag = rc;
1756         list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
1757         return 0;
1758 }
1759
1760 /* HBQ for ELS and CT traffic. */
1761 static struct lpfc_hbq_init lpfc_els_hbq = {
1762         .rn = 1,
1763         .entry_count = 256,
1764         .mask_count = 0,
1765         .profile = 0,
1766         .ring_mask = (1 << LPFC_ELS_RING),
1767         .buffer_count = 0,
1768         .init_count = 40,
1769         .add_count = 40,
1770 };
1771
1772 /* HBQ for the extra ring if needed */
1773 static struct lpfc_hbq_init lpfc_extra_hbq = {
1774         .rn = 1,
1775         .entry_count = 200,
1776         .mask_count = 0,
1777         .profile = 0,
1778         .ring_mask = (1 << LPFC_EXTRA_RING),
1779         .buffer_count = 0,
1780         .init_count = 0,
1781         .add_count = 5,
1782 };
1783
1784 /* Array of HBQs */
1785 struct lpfc_hbq_init *lpfc_hbq_defs[] = {
1786         &lpfc_els_hbq,
1787         &lpfc_extra_hbq,
1788 };
1789
1790 /**
1791  * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
1792  * @phba: Pointer to HBA context object.
1793  * @hbqno: HBQ number.
1794  * @count: Number of HBQ buffers to be posted.
1795  *
1796  * This function is called with no lock held to post more hbq buffers to the
1797  * given HBQ. The function returns the number of HBQ buffers successfully
1798  * posted.
1799  **/
1800 static int
1801 lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
1802 {
1803         uint32_t i, posted = 0;
1804         unsigned long flags;
1805         struct hbq_dmabuf *hbq_buffer;
1806         LIST_HEAD(hbq_buf_list);
1807         if (!phba->hbqs[hbqno].hbq_alloc_buffer)
1808                 return 0;
1809
1810         if ((phba->hbqs[hbqno].buffer_count + count) >
1811             lpfc_hbq_defs[hbqno]->entry_count)
1812                 count = lpfc_hbq_defs[hbqno]->entry_count -
1813                                         phba->hbqs[hbqno].buffer_count;
1814         if (!count)
1815                 return 0;
1816         /* Allocate HBQ entries */
1817         for (i = 0; i < count; i++) {
1818                 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
1819                 if (!hbq_buffer)
1820                         break;
1821                 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
1822         }
1823         /* Check whether HBQ is still in use */
1824         spin_lock_irqsave(&phba->hbalock, flags);
1825         if (!phba->hbq_in_use)
1826                 goto err;
1827         while (!list_empty(&hbq_buf_list)) {
1828                 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1829                                  dbuf.list);
1830                 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
1831                                       (hbqno << 16));
1832                 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
1833                         phba->hbqs[hbqno].buffer_count++;
1834                         posted++;
1835                 } else
1836                         (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1837         }
1838         spin_unlock_irqrestore(&phba->hbalock, flags);
1839         return posted;
1840 err:
1841         spin_unlock_irqrestore(&phba->hbalock, flags);
1842         while (!list_empty(&hbq_buf_list)) {
1843                 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1844                                  dbuf.list);
1845                 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1846         }
1847         return 0;
1848 }
1849
1850 /**
1851  * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
1852  * @phba: Pointer to HBA context object.
1853  * @qno: HBQ number.
1854  *
1855  * This function posts more buffers to the HBQ. This function
1856  * is called with no lock held. The function returns the number of HBQ entries
1857  * successfully allocated.
1858  **/
1859 int
1860 lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
1861 {
1862         if (phba->sli_rev == LPFC_SLI_REV4)
1863                 return 0;
1864         else
1865                 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1866                                          lpfc_hbq_defs[qno]->add_count);
1867 }
1868
1869 /**
1870  * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
1871  * @phba: Pointer to HBA context object.
1872  * @qno:  HBQ queue number.
1873  *
1874  * This function is called from SLI initialization code path with
1875  * no lock held to post initial HBQ buffers to firmware. The
1876  * function returns the number of HBQ entries successfully allocated.
1877  **/
1878 static int
1879 lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
1880 {
1881         if (phba->sli_rev == LPFC_SLI_REV4)
1882                 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1883                                          lpfc_hbq_defs[qno]->entry_count);
1884         else
1885                 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1886                                          lpfc_hbq_defs[qno]->init_count);
1887 }
1888
1889 /**
1890  * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
1891  * @phba: Pointer to HBA context object.
1892  * @hbqno: HBQ number.
1893  *
1894  * This function removes the first hbq buffer on an hbq list and returns a
1895  * pointer to that buffer. If it finds no buffers on the list it returns NULL.
1896  **/
1897 static struct hbq_dmabuf *
1898 lpfc_sli_hbqbuf_get(struct list_head *rb_list)
1899 {
1900         struct lpfc_dmabuf *d_buf;
1901
1902         list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
1903         if (!d_buf)
1904                 return NULL;
1905         return container_of(d_buf, struct hbq_dmabuf, dbuf);
1906 }
1907
1908 /**
1909  * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
1910  * @phba: Pointer to HBA context object.
1911  * @tag: Tag of the hbq buffer.
1912  *
1913  * This function is called with hbalock held. This function searches
1914  * for the hbq buffer associated with the given tag in the hbq buffer
1915  * list. If it finds the hbq buffer, it returns the hbq_buffer other wise
1916  * it returns NULL.
1917  **/
1918 static struct hbq_dmabuf *
1919 lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
1920 {
1921         struct lpfc_dmabuf *d_buf;
1922         struct hbq_dmabuf *hbq_buf;
1923         uint32_t hbqno;
1924
1925         hbqno = tag >> 16;
1926         if (hbqno >= LPFC_MAX_HBQS)
1927                 return NULL;
1928
1929         spin_lock_irq(&phba->hbalock);
1930         list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
1931                 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
1932                 if (hbq_buf->tag == tag) {
1933                         spin_unlock_irq(&phba->hbalock);
1934                         return hbq_buf;
1935                 }
1936         }
1937         spin_unlock_irq(&phba->hbalock);
1938         lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
1939                         "1803 Bad hbq tag. Data: x%x x%x\n",
1940                         tag, phba->hbqs[tag >> 16].buffer_count);
1941         return NULL;
1942 }
1943
1944 /**
1945  * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
1946  * @phba: Pointer to HBA context object.
1947  * @hbq_buffer: Pointer to HBQ buffer.
1948  *
1949  * This function is called with hbalock. This function gives back
1950  * the hbq buffer to firmware. If the HBQ does not have space to
1951  * post the buffer, it will free the buffer.
1952  **/
1953 void
1954 lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
1955 {
1956         uint32_t hbqno;
1957
1958         if (hbq_buffer) {
1959                 hbqno = hbq_buffer->tag >> 16;
1960                 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
1961                         (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1962         }
1963 }
1964
1965 /**
1966  * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
1967  * @mbxCommand: mailbox command code.
1968  *
1969  * This function is called by the mailbox event handler function to verify
1970  * that the completed mailbox command is a legitimate mailbox command. If the
1971  * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
1972  * and the mailbox event handler will take the HBA offline.
1973  **/
1974 static int
1975 lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
1976 {
1977         uint8_t ret;
1978
1979         switch (mbxCommand) {
1980         case MBX_LOAD_SM:
1981         case MBX_READ_NV:
1982         case MBX_WRITE_NV:
1983         case MBX_WRITE_VPARMS:
1984         case MBX_RUN_BIU_DIAG:
1985         case MBX_INIT_LINK:
1986         case MBX_DOWN_LINK:
1987         case MBX_CONFIG_LINK:
1988         case MBX_CONFIG_RING:
1989         case MBX_RESET_RING:
1990         case MBX_READ_CONFIG:
1991         case MBX_READ_RCONFIG:
1992         case MBX_READ_SPARM:
1993         case MBX_READ_STATUS:
1994         case MBX_READ_RPI:
1995         case MBX_READ_XRI:
1996         case MBX_READ_REV:
1997         case MBX_READ_LNK_STAT:
1998         case MBX_REG_LOGIN:
1999         case MBX_UNREG_LOGIN:
2000         case MBX_CLEAR_LA:
2001         case MBX_DUMP_MEMORY:
2002         case MBX_DUMP_CONTEXT:
2003         case MBX_RUN_DIAGS:
2004         case MBX_RESTART:
2005         case MBX_UPDATE_CFG:
2006         case MBX_DOWN_LOAD:
2007         case MBX_DEL_LD_ENTRY:
2008         case MBX_RUN_PROGRAM:
2009         case MBX_SET_MASK:
2010         case MBX_SET_VARIABLE:
2011         case MBX_UNREG_D_ID:
2012         case MBX_KILL_BOARD:
2013         case MBX_CONFIG_FARP:
2014         case MBX_BEACON:
2015         case MBX_LOAD_AREA:
2016         case MBX_RUN_BIU_DIAG64:
2017         case MBX_CONFIG_PORT:
2018         case MBX_READ_SPARM64:
2019         case MBX_READ_RPI64:
2020         case MBX_REG_LOGIN64:
2021         case MBX_READ_TOPOLOGY:
2022         case MBX_WRITE_WWN:
2023         case MBX_SET_DEBUG:
2024         case MBX_LOAD_EXP_ROM:
2025         case MBX_ASYNCEVT_ENABLE:
2026         case MBX_REG_VPI:
2027         case MBX_UNREG_VPI:
2028         case MBX_HEARTBEAT:
2029         case MBX_PORT_CAPABILITIES:
2030         case MBX_PORT_IOV_CONTROL:
2031         case MBX_SLI4_CONFIG:
2032         case MBX_SLI4_REQ_FTRS:
2033         case MBX_REG_FCFI:
2034         case MBX_UNREG_FCFI:
2035         case MBX_REG_VFI:
2036         case MBX_UNREG_VFI:
2037         case MBX_INIT_VPI:
2038         case MBX_INIT_VFI:
2039         case MBX_RESUME_RPI:
2040         case MBX_READ_EVENT_LOG_STATUS:
2041         case MBX_READ_EVENT_LOG:
2042         case MBX_SECURITY_MGMT:
2043         case MBX_AUTH_PORT:
2044                 ret = mbxCommand;
2045                 break;
2046         default:
2047                 ret = MBX_SHUTDOWN;
2048                 break;
2049         }
2050         return ret;
2051 }
2052
2053 /**
2054  * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
2055  * @phba: Pointer to HBA context object.
2056  * @pmboxq: Pointer to mailbox command.
2057  *
2058  * This is completion handler function for mailbox commands issued from
2059  * lpfc_sli_issue_mbox_wait function. This function is called by the
2060  * mailbox event handler function with no lock held. This function
2061  * will wake up thread waiting on the wait queue pointed by context1
2062  * of the mailbox.
2063  **/
2064 void
2065 lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
2066 {
2067         wait_queue_head_t *pdone_q;
2068         unsigned long drvr_flag;
2069
2070         /*
2071          * If pdone_q is empty, the driver thread gave up waiting and
2072          * continued running.
2073          */
2074         pmboxq->mbox_flag |= LPFC_MBX_WAKE;
2075         spin_lock_irqsave(&phba->hbalock, drvr_flag);
2076         pdone_q = (wait_queue_head_t *) pmboxq->context1;
2077         if (pdone_q)
2078                 wake_up_interruptible(pdone_q);
2079         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
2080         return;
2081 }
2082
2083
2084 /**
2085  * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
2086  * @phba: Pointer to HBA context object.
2087  * @pmb: Pointer to mailbox object.
2088  *
2089  * This function is the default mailbox completion handler. It
2090  * frees the memory resources associated with the completed mailbox
2091  * command. If the completed command is a REG_LOGIN mailbox command,
2092  * this function will issue a UREG_LOGIN to re-claim the RPI.
2093  **/
2094 void
2095 lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2096 {
2097         struct lpfc_vport  *vport = pmb->vport;
2098         struct lpfc_dmabuf *mp;
2099         struct lpfc_nodelist *ndlp;
2100         struct Scsi_Host *shost;
2101         uint16_t rpi, vpi;
2102         int rc;
2103
2104         mp = (struct lpfc_dmabuf *) (pmb->context1);
2105
2106         if (mp) {
2107                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2108                 kfree(mp);
2109         }
2110
2111         /*
2112          * If a REG_LOGIN succeeded  after node is destroyed or node
2113          * is in re-discovery driver need to cleanup the RPI.
2114          */
2115         if (!(phba->pport->load_flag & FC_UNLOADING) &&
2116             pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
2117             !pmb->u.mb.mbxStatus) {
2118                 rpi = pmb->u.mb.un.varWords[0];
2119                 vpi = pmb->u.mb.un.varRegLogin.vpi - phba->vpi_base;
2120                 lpfc_unreg_login(phba, vpi, rpi, pmb);
2121                 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
2122                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2123                 if (rc != MBX_NOT_FINISHED)
2124                         return;
2125         }
2126
2127         if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
2128                 !(phba->pport->load_flag & FC_UNLOADING) &&
2129                 !pmb->u.mb.mbxStatus) {
2130                 shost = lpfc_shost_from_vport(vport);
2131                 spin_lock_irq(shost->host_lock);
2132                 vport->vpi_state |= LPFC_VPI_REGISTERED;
2133                 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
2134                 spin_unlock_irq(shost->host_lock);
2135         }
2136
2137         if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
2138                 ndlp = (struct lpfc_nodelist *)pmb->context2;
2139                 lpfc_nlp_put(ndlp);
2140                 pmb->context2 = NULL;
2141         }
2142
2143         /* Check security permission status on INIT_LINK mailbox command */
2144         if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
2145             (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
2146                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
2147                                 "2860 SLI authentication is required "
2148                                 "for INIT_LINK but has not done yet\n");
2149
2150         if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
2151                 lpfc_sli4_mbox_cmd_free(phba, pmb);
2152         else
2153                 mempool_free(pmb, phba->mbox_mem_pool);
2154 }
2155
2156 /**
2157  * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
2158  * @phba: Pointer to HBA context object.
2159  *
2160  * This function is called with no lock held. This function processes all
2161  * the completed mailbox commands and gives it to upper layers. The interrupt
2162  * service routine processes mailbox completion interrupt and adds completed
2163  * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
2164  * Worker thread call lpfc_sli_handle_mb_event, which will return the
2165  * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
2166  * function returns the mailbox commands to the upper layer by calling the
2167  * completion handler function of each mailbox.
2168  **/
2169 int
2170 lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
2171 {
2172         MAILBOX_t *pmbox;
2173         LPFC_MBOXQ_t *pmb;
2174         int rc;
2175         LIST_HEAD(cmplq);
2176
2177         phba->sli.slistat.mbox_event++;
2178
2179         /* Get all completed mailboxe buffers into the cmplq */
2180         spin_lock_irq(&phba->hbalock);
2181         list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
2182         spin_unlock_irq(&phba->hbalock);
2183
2184         /* Get a Mailbox buffer to setup mailbox commands for callback */
2185         do {
2186                 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
2187                 if (pmb == NULL)
2188                         break;
2189
2190                 pmbox = &pmb->u.mb;
2191
2192                 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
2193                         if (pmb->vport) {
2194                                 lpfc_debugfs_disc_trc(pmb->vport,
2195                                         LPFC_DISC_TRC_MBOX_VPORT,
2196                                         "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
2197                                         (uint32_t)pmbox->mbxCommand,
2198                                         pmbox->un.varWords[0],
2199                                         pmbox->un.varWords[1]);
2200                         }
2201                         else {
2202                                 lpfc_debugfs_disc_trc(phba->pport,
2203                                         LPFC_DISC_TRC_MBOX,
2204                                         "MBOX cmpl:       cmd:x%x mb:x%x x%x",
2205                                         (uint32_t)pmbox->mbxCommand,
2206                                         pmbox->un.varWords[0],
2207                                         pmbox->un.varWords[1]);
2208                         }
2209                 }
2210
2211                 /*
2212                  * It is a fatal error if unknown mbox command completion.
2213                  */
2214                 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
2215                     MBX_SHUTDOWN) {
2216                         /* Unknown mailbox command compl */
2217                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
2218                                         "(%d):0323 Unknown Mailbox command "
2219                                         "x%x (x%x) Cmpl\n",
2220                                         pmb->vport ? pmb->vport->vpi : 0,
2221                                         pmbox->mbxCommand,
2222                                         lpfc_sli4_mbox_opcode_get(phba, pmb));
2223                         phba->link_state = LPFC_HBA_ERROR;
2224                         phba->work_hs = HS_FFER3;
2225                         lpfc_handle_eratt(phba);
2226                         continue;
2227                 }
2228
2229                 if (pmbox->mbxStatus) {
2230                         phba->sli.slistat.mbox_stat_err++;
2231                         if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
2232                                 /* Mbox cmd cmpl error - RETRYing */
2233                                 lpfc_printf_log(phba, KERN_INFO,
2234                                                 LOG_MBOX | LOG_SLI,
2235                                                 "(%d):0305 Mbox cmd cmpl "
2236                                                 "error - RETRYing Data: x%x "
2237                                                 "(x%x) x%x x%x x%x\n",
2238                                                 pmb->vport ? pmb->vport->vpi :0,
2239                                                 pmbox->mbxCommand,
2240                                                 lpfc_sli4_mbox_opcode_get(phba,
2241                                                                           pmb),
2242                                                 pmbox->mbxStatus,
2243                                                 pmbox->un.varWords[0],
2244                                                 pmb->vport->port_state);
2245                                 pmbox->mbxStatus = 0;
2246                                 pmbox->mbxOwner = OWN_HOST;
2247                                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2248                                 if (rc != MBX_NOT_FINISHED)
2249                                         continue;
2250                         }
2251                 }
2252
2253                 /* Mailbox cmd <cmd> Cmpl <cmpl> */
2254                 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
2255                                 "(%d):0307 Mailbox cmd x%x (x%x) Cmpl x%p "
2256                                 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x\n",
2257                                 pmb->vport ? pmb->vport->vpi : 0,
2258                                 pmbox->mbxCommand,
2259                                 lpfc_sli4_mbox_opcode_get(phba, pmb),
2260                                 pmb->mbox_cmpl,
2261                                 *((uint32_t *) pmbox),
2262                                 pmbox->un.varWords[0],
2263                                 pmbox->un.varWords[1],
2264                                 pmbox->un.varWords[2],
2265                                 pmbox->un.varWords[3],
2266                                 pmbox->un.varWords[4],
2267                                 pmbox->un.varWords[5],
2268                                 pmbox->un.varWords[6],
2269                                 pmbox->un.varWords[7]);
2270
2271                 if (pmb->mbox_cmpl)
2272                         pmb->mbox_cmpl(phba,pmb);
2273         } while (1);
2274         return 0;
2275 }
2276
2277 /**
2278  * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
2279  * @phba: Pointer to HBA context object.
2280  * @pring: Pointer to driver SLI ring object.
2281  * @tag: buffer tag.
2282  *
2283  * This function is called with no lock held. When QUE_BUFTAG_BIT bit
2284  * is set in the tag the buffer is posted for a particular exchange,
2285  * the function will return the buffer without replacing the buffer.
2286  * If the buffer is for unsolicited ELS or CT traffic, this function
2287  * returns the buffer and also posts another buffer to the firmware.
2288  **/
2289 static struct lpfc_dmabuf *
2290 lpfc_sli_get_buff(struct lpfc_hba *phba,
2291                   struct lpfc_sli_ring *pring,
2292                   uint32_t tag)
2293 {
2294         struct hbq_dmabuf *hbq_entry;
2295
2296         if (tag & QUE_BUFTAG_BIT)
2297                 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
2298         hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
2299         if (!hbq_entry)
2300                 return NULL;
2301         return &hbq_entry->dbuf;
2302 }
2303
2304 /**
2305  * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
2306  * @phba: Pointer to HBA context object.
2307  * @pring: Pointer to driver SLI ring object.
2308  * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
2309  * @fch_r_ctl: the r_ctl for the first frame of the sequence.
2310  * @fch_type: the type for the first frame of the sequence.
2311  *
2312  * This function is called with no lock held. This function uses the r_ctl and
2313  * type of the received sequence to find the correct callback function to call
2314  * to process the sequence.
2315  **/
2316 static int
2317 lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2318                          struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
2319                          uint32_t fch_type)
2320 {
2321         int i;
2322
2323         /* unSolicited Responses */
2324         if (pring->prt[0].profile) {
2325                 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
2326                         (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
2327                                                                         saveq);
2328                 return 1;
2329         }
2330         /* We must search, based on rctl / type
2331            for the right routine */
2332         for (i = 0; i < pring->num_mask; i++) {
2333                 if ((pring->prt[i].rctl == fch_r_ctl) &&
2334                     (pring->prt[i].type == fch_type)) {
2335                         if (pring->prt[i].lpfc_sli_rcv_unsol_event)
2336                                 (pring->prt[i].lpfc_sli_rcv_unsol_event)
2337                                                 (phba, pring, saveq);
2338                         return 1;
2339                 }
2340         }
2341         return 0;
2342 }
2343
2344 /**
2345  * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
2346  * @phba: Pointer to HBA context object.
2347  * @pring: Pointer to driver SLI ring object.
2348  * @saveq: Pointer to the unsolicited iocb.
2349  *
2350  * This function is called with no lock held by the ring event handler
2351  * when there is an unsolicited iocb posted to the response ring by the
2352  * firmware. This function gets the buffer associated with the iocbs
2353  * and calls the event handler for the ring. This function handles both
2354  * qring buffers and hbq buffers.
2355  * When the function returns 1 the caller can free the iocb object otherwise
2356  * upper layer functions will free the iocb objects.
2357  **/
2358 static int
2359 lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2360                             struct lpfc_iocbq *saveq)
2361 {
2362         IOCB_t           * irsp;
2363         WORD5            * w5p;
2364         uint32_t           Rctl, Type;
2365         uint32_t           match;
2366         struct lpfc_iocbq *iocbq;
2367         struct lpfc_dmabuf *dmzbuf;
2368
2369         match = 0;
2370         irsp = &(saveq->iocb);
2371
2372         if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
2373                 if (pring->lpfc_sli_rcv_async_status)
2374                         pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
2375                 else
2376                         lpfc_printf_log(phba,
2377                                         KERN_WARNING,
2378                                         LOG_SLI,
2379                                         "0316 Ring %d handler: unexpected "
2380                                         "ASYNC_STATUS iocb received evt_code "
2381                                         "0x%x\n",
2382                                         pring->ringno,
2383                                         irsp->un.asyncstat.evt_code);
2384                 return 1;
2385         }
2386
2387         if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
2388                 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
2389                 if (irsp->ulpBdeCount > 0) {
2390                         dmzbuf = lpfc_sli_get_buff(phba, pring,
2391                                         irsp->un.ulpWord[3]);
2392                         lpfc_in_buf_free(phba, dmzbuf);
2393                 }
2394
2395                 if (irsp->ulpBdeCount > 1) {
2396                         dmzbuf = lpfc_sli_get_buff(phba, pring,
2397                                         irsp->unsli3.sli3Words[3]);
2398                         lpfc_in_buf_free(phba, dmzbuf);
2399                 }
2400
2401                 if (irsp->ulpBdeCount > 2) {
2402                         dmzbuf = lpfc_sli_get_buff(phba, pring,
2403                                 irsp->unsli3.sli3Words[7]);
2404                         lpfc_in_buf_free(phba, dmzbuf);
2405                 }
2406
2407                 return 1;
2408         }
2409
2410         if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
2411                 if (irsp->ulpBdeCount != 0) {
2412                         saveq->context2 = lpfc_sli_get_buff(phba, pring,
2413                                                 irsp->un.ulpWord[3]);
2414                         if (!saveq->context2)
2415                                 lpfc_printf_log(phba,
2416                                         KERN_ERR,
2417                                         LOG_SLI,
2418                                         "0341 Ring %d Cannot find buffer for "
2419                                         "an unsolicited iocb. tag 0x%x\n",
2420                                         pring->ringno,
2421                                         irsp->un.ulpWord[3]);
2422                 }
2423                 if (irsp->ulpBdeCount == 2) {
2424                         saveq->context3 = lpfc_sli_get_buff(phba, pring,
2425                                                 irsp->unsli3.sli3Words[7]);
2426                         if (!saveq->context3)
2427                                 lpfc_printf_log(phba,
2428                                         KERN_ERR,
2429                                         LOG_SLI,
2430                                         "0342 Ring %d Cannot find buffer for an"
2431                                         " unsolicited iocb. tag 0x%x\n",
2432                                         pring->ringno,
2433                                         irsp->unsli3.sli3Words[7]);
2434                 }
2435                 list_for_each_entry(iocbq, &saveq->list, list) {
2436                         irsp = &(iocbq->iocb);
2437                         if (irsp->ulpBdeCount != 0) {
2438                                 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
2439                                                         irsp->un.ulpWord[3]);
2440                                 if (!iocbq->context2)
2441                                         lpfc_printf_log(phba,
2442                                                 KERN_ERR,
2443                                                 LOG_SLI,
2444                                                 "0343 Ring %d Cannot find "
2445                                                 "buffer for an unsolicited iocb"
2446                                                 ". tag 0x%x\n", pring->ringno,
2447                                                 irsp->un.ulpWord[3]);
2448                         }
2449                         if (irsp->ulpBdeCount == 2) {
2450                                 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
2451                                                 irsp->unsli3.sli3Words[7]);
2452                                 if (!iocbq->context3)
2453                                         lpfc_printf_log(phba,
2454                                                 KERN_ERR,
2455                                                 LOG_SLI,
2456                                                 "0344 Ring %d Cannot find "
2457                                                 "buffer for an unsolicited "
2458                                                 "iocb. tag 0x%x\n",
2459                                                 pring->ringno,
2460                                                 irsp->unsli3.sli3Words[7]);
2461                         }
2462                 }
2463         }
2464         if (irsp->ulpBdeCount != 0 &&
2465             (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
2466              irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
2467                 int found = 0;
2468
2469                 /* search continue save q for same XRI */
2470                 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
2471                         if (iocbq->iocb.ulpContext == saveq->iocb.ulpContext) {
2472                                 list_add_tail(&saveq->list, &iocbq->list);
2473                                 found = 1;
2474                                 break;
2475                         }
2476                 }
2477                 if (!found)
2478                         list_add_tail(&saveq->clist,
2479                                       &pring->iocb_continue_saveq);
2480                 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
2481                         list_del_init(&iocbq->clist);
2482                         saveq = iocbq;
2483                         irsp = &(saveq->iocb);
2484                 } else
2485                         return 0;
2486         }
2487         if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
2488             (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
2489             (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
2490                 Rctl = FC_RCTL_ELS_REQ;
2491                 Type = FC_TYPE_ELS;
2492         } else {
2493                 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
2494                 Rctl = w5p->hcsw.Rctl;
2495                 Type = w5p->hcsw.Type;
2496
2497                 /* Firmware Workaround */
2498                 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
2499                         (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
2500                          irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
2501                         Rctl = FC_RCTL_ELS_REQ;
2502                         Type = FC_TYPE_ELS;
2503                         w5p->hcsw.Rctl = Rctl;
2504                         w5p->hcsw.Type = Type;
2505                 }
2506         }
2507
2508         if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
2509                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
2510                                 "0313 Ring %d handler: unexpected Rctl x%x "
2511                                 "Type x%x received\n",
2512                                 pring->ringno, Rctl, Type);
2513
2514         return 1;
2515 }
2516
2517 /**
2518  * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
2519  * @phba: Pointer to HBA context object.
2520  * @pring: Pointer to driver SLI ring object.
2521  * @prspiocb: Pointer to response iocb object.
2522  *
2523  * This function looks up the iocb_lookup table to get the command iocb
2524  * corresponding to the given response iocb using the iotag of the
2525  * response iocb. This function is called with the hbalock held.
2526  * This function returns the command iocb object if it finds the command
2527  * iocb else returns NULL.
2528  **/
2529 static struct lpfc_iocbq *
2530 lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
2531                       struct lpfc_sli_ring *pring,
2532                       struct lpfc_iocbq *prspiocb)
2533 {
2534         struct lpfc_iocbq *cmd_iocb = NULL;
2535         uint16_t iotag;
2536
2537         iotag = prspiocb->iocb.ulpIoTag;
2538
2539         if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2540                 cmd_iocb = phba->sli.iocbq_lookup[iotag];
2541                 list_del_init(&cmd_iocb->list);
2542                 if (cmd_iocb->iocb_flag & LPFC_IO_ON_Q) {
2543                         pring->txcmplq_cnt--;
2544                         cmd_iocb->iocb_flag &= ~LPFC_IO_ON_Q;
2545                 }
2546                 return cmd_iocb;
2547         }
2548
2549         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2550                         "0317 iotag x%x is out off "
2551                         "range: max iotag x%x wd0 x%x\n",
2552                         iotag, phba->sli.last_iotag,
2553                         *(((uint32_t *) &prspiocb->iocb) + 7));
2554         return NULL;
2555 }
2556
2557 /**
2558  * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
2559  * @phba: Pointer to HBA context object.
2560  * @pring: Pointer to driver SLI ring object.
2561  * @iotag: IOCB tag.
2562  *
2563  * This function looks up the iocb_lookup table to get the command iocb
2564  * corresponding to the given iotag. This function is called with the
2565  * hbalock held.
2566  * This function returns the command iocb object if it finds the command
2567  * iocb else returns NULL.
2568  **/
2569 static struct lpfc_iocbq *
2570 lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
2571                              struct lpfc_sli_ring *pring, uint16_t iotag)
2572 {
2573         struct lpfc_iocbq *cmd_iocb;
2574
2575         if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2576                 cmd_iocb = phba->sli.iocbq_lookup[iotag];
2577                 list_del_init(&cmd_iocb->list);
2578                 if (cmd_iocb->iocb_flag & LPFC_IO_ON_Q) {
2579                         cmd_iocb->iocb_flag &= ~LPFC_IO_ON_Q;
2580                         pring->txcmplq_cnt--;
2581                 }
2582                 return cmd_iocb;
2583         }
2584
2585         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2586                         "0372 iotag x%x is out off range: max iotag (x%x)\n",
2587                         iotag, phba->sli.last_iotag);
2588         return NULL;
2589 }
2590
2591 /**
2592  * lpfc_sli_process_sol_iocb - process solicited iocb completion
2593  * @phba: Pointer to HBA context object.
2594  * @pring: Pointer to driver SLI ring object.
2595  * @saveq: Pointer to the response iocb to be processed.
2596  *
2597  * This function is called by the ring event handler for non-fcp
2598  * rings when there is a new response iocb in the response ring.
2599  * The caller is not required to hold any locks. This function
2600  * gets the command iocb associated with the response iocb and
2601  * calls the completion handler for the command iocb. If there
2602  * is no completion handler, the function will free the resources
2603  * associated with command iocb. If the response iocb is for
2604  * an already aborted command iocb, the status of the completion
2605  * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
2606  * This function always returns 1.
2607  **/
2608 static int
2609 lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2610                           struct lpfc_iocbq *saveq)
2611 {
2612         struct lpfc_iocbq *cmdiocbp;
2613         int rc = 1;
2614         unsigned long iflag;
2615
2616         /* Based on the iotag field, get the cmd IOCB from the txcmplq */
2617         spin_lock_irqsave(&phba->hbalock, iflag);
2618         cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
2619         spin_unlock_irqrestore(&phba->hbalock, iflag);
2620
2621         if (cmdiocbp) {
2622                 if (cmdiocbp->iocb_cmpl) {
2623                         /*
2624                          * If an ELS command failed send an event to mgmt
2625                          * application.
2626                          */
2627                         if (saveq->iocb.ulpStatus &&
2628                              (pring->ringno == LPFC_ELS_RING) &&
2629                              (cmdiocbp->iocb.ulpCommand ==
2630                                 CMD_ELS_REQUEST64_CR))
2631                                 lpfc_send_els_failure_event(phba,
2632                                         cmdiocbp, saveq);
2633
2634                         /*
2635                          * Post all ELS completions to the worker thread.
2636                          * All other are passed to the completion callback.
2637                          */
2638                         if (pring->ringno == LPFC_ELS_RING) {
2639                                 if ((phba->sli_rev < LPFC_SLI_REV4) &&
2640                                     (cmdiocbp->iocb_flag &
2641                                                         LPFC_DRIVER_ABORTED)) {
2642                                         spin_lock_irqsave(&phba->hbalock,
2643                                                           iflag);
2644                                         cmdiocbp->iocb_flag &=
2645                                                 ~LPFC_DRIVER_ABORTED;
2646                                         spin_unlock_irqrestore(&phba->hbalock,
2647                                                                iflag);
2648                                         saveq->iocb.ulpStatus =
2649                                                 IOSTAT_LOCAL_REJECT;
2650                                         saveq->iocb.un.ulpWord[4] =
2651                                                 IOERR_SLI_ABORTED;
2652
2653                                         /* Firmware could still be in progress
2654                                          * of DMAing payload, so don't free data
2655                                          * buffer till after a hbeat.
2656                                          */
2657                                         spin_lock_irqsave(&phba->hbalock,
2658                                                           iflag);
2659                                         saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
2660                                         spin_unlock_irqrestore(&phba->hbalock,
2661                                                                iflag);
2662                                 }
2663                                 if (phba->sli_rev == LPFC_SLI_REV4) {
2664                                         if (saveq->iocb_flag &
2665                                             LPFC_EXCHANGE_BUSY) {
2666                                                 /* Set cmdiocb flag for the
2667                                                  * exchange busy so sgl (xri)
2668                                                  * will not be released until
2669                                                  * the abort xri is received
2670                                                  * from hba.
2671                                                  */
2672                                                 spin_lock_irqsave(
2673                                                         &phba->hbalock, iflag);
2674                                                 cmdiocbp->iocb_flag |=
2675                                                         LPFC_EXCHANGE_BUSY;
2676                                                 spin_unlock_irqrestore(
2677                                                         &phba->hbalock, iflag);
2678                                         }
2679                                         if (cmdiocbp->iocb_flag &
2680                                             LPFC_DRIVER_ABORTED) {
2681                                                 /*
2682                                                  * Clear LPFC_DRIVER_ABORTED
2683                                                  * bit in case it was driver
2684                                                  * initiated abort.
2685                                                  */
2686                                                 spin_lock_irqsave(
2687                                                         &phba->hbalock, iflag);
2688                                                 cmdiocbp->iocb_flag &=
2689                                                         ~LPFC_DRIVER_ABORTED;
2690                                                 spin_unlock_irqrestore(
2691                                                         &phba->hbalock, iflag);
2692                                                 cmdiocbp->iocb.ulpStatus =
2693                                                         IOSTAT_LOCAL_REJECT;
2694                                                 cmdiocbp->iocb.un.ulpWord[4] =
2695                                                         IOERR_ABORT_REQUESTED;
2696                                                 /*
2697                                                  * For SLI4, irsiocb contains
2698                                                  * NO_XRI in sli_xritag, it
2699                                                  * shall not affect releasing
2700                                                  * sgl (xri) process.
2701                                                  */
2702                                                 saveq->iocb.ulpStatus =
2703                                                         IOSTAT_LOCAL_REJECT;
2704                                                 saveq->iocb.un.ulpWord[4] =
2705                                                         IOERR_SLI_ABORTED;
2706                                                 spin_lock_irqsave(
2707                                                         &phba->hbalock, iflag);
2708                                                 saveq->iocb_flag |=
2709                                                         LPFC_DELAY_MEM_FREE;
2710                                                 spin_unlock_irqrestore(
2711                                                         &phba->hbalock, iflag);
2712                                         }
2713                                 }
2714                         }
2715                         (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
2716                 } else
2717                         lpfc_sli_release_iocbq(phba, cmdiocbp);
2718         } else {
2719                 /*
2720                  * Unknown initiating command based on the response iotag.
2721                  * This could be the case on the ELS ring because of
2722                  * lpfc_els_abort().
2723                  */
2724                 if (pring->ringno != LPFC_ELS_RING) {
2725                         /*
2726                          * Ring <ringno> handler: unexpected completion IoTag
2727                          * <IoTag>
2728                          */
2729                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
2730                                          "0322 Ring %d handler: "
2731                                          "unexpected completion IoTag x%x "
2732                                          "Data: x%x x%x x%x x%x\n",
2733                                          pring->ringno,
2734                                          saveq->iocb.ulpIoTag,
2735                                          saveq->iocb.ulpStatus,
2736                                          saveq->iocb.un.ulpWord[4],
2737                                          saveq->iocb.ulpCommand,
2738                                          saveq->iocb.ulpContext);
2739                 }
2740         }
2741
2742         return rc;
2743 }
2744
2745 /**
2746  * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
2747  * @phba: Pointer to HBA context object.
2748  * @pring: Pointer to driver SLI ring object.
2749  *
2750  * This function is called from the iocb ring event handlers when
2751  * put pointer is ahead of the get pointer for a ring. This function signal
2752  * an error attention condition to the worker thread and the worker
2753  * thread will transition the HBA to offline state.
2754  **/
2755 static void
2756 lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
2757 {
2758         struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
2759         /*
2760          * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
2761          * rsp ring <portRspMax>
2762          */
2763         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2764                         "0312 Ring %d handler: portRspPut %d "
2765                         "is bigger than rsp ring %d\n",
2766                         pring->ringno, le32_to_cpu(pgp->rspPutInx),
2767                         pring->numRiocb);
2768
2769         phba->link_state = LPFC_HBA_ERROR;
2770
2771         /*
2772          * All error attention handlers are posted to
2773          * worker thread
2774          */
2775         phba->work_ha |= HA_ERATT;
2776         phba->work_hs = HS_FFER3;
2777
2778         lpfc_worker_wake_up(phba);
2779
2780         return;
2781 }
2782
2783 /**
2784  * lpfc_poll_eratt - Error attention polling timer timeout handler
2785  * @ptr: Pointer to address of HBA context object.
2786  *
2787  * This function is invoked by the Error Attention polling timer when the
2788  * timer times out. It will check the SLI Error Attention register for
2789  * possible attention events. If so, it will post an Error Attention event
2790  * and wake up worker thread to process it. Otherwise, it will set up the
2791  * Error Attention polling timer for the next poll.
2792  **/
2793 void lpfc_poll_eratt(unsigned long ptr)
2794 {
2795         struct lpfc_hba *phba;
2796         uint32_t eratt = 0;
2797
2798         phba = (struct lpfc_hba *)ptr;
2799
2800         /* Check chip HA register for error event */
2801         eratt = lpfc_sli_check_eratt(phba);
2802
2803         if (eratt)
2804                 /* Tell the worker thread there is work to do */
2805                 lpfc_worker_wake_up(phba);
2806         else
2807                 /* Restart the timer for next eratt poll */
2808                 mod_timer(&phba->eratt_poll, jiffies +
2809                                         HZ * LPFC_ERATT_POLL_INTERVAL);
2810         return;
2811 }
2812
2813
2814 /**
2815  * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
2816  * @phba: Pointer to HBA context object.
2817  * @pring: Pointer to driver SLI ring object.
2818  * @mask: Host attention register mask for this ring.
2819  *
2820  * This function is called from the interrupt context when there is a ring
2821  * event for the fcp ring. The caller does not hold any lock.
2822  * The function processes each response iocb in the response ring until it
2823  * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
2824  * LE bit set. The function will call the completion handler of the command iocb
2825  * if the response iocb indicates a completion for a command iocb or it is
2826  * an abort completion. The function will call lpfc_sli_process_unsol_iocb
2827  * function if this is an unsolicited iocb.
2828  * This routine presumes LPFC_FCP_RING handling and doesn't bother
2829  * to check it explicitly.
2830  */
2831 int
2832 lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
2833                                 struct lpfc_sli_ring *pring, uint32_t mask)
2834 {
2835         struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
2836         IOCB_t *irsp = NULL;
2837         IOCB_t *entry = NULL;
2838         struct lpfc_iocbq *cmdiocbq = NULL;
2839         struct lpfc_iocbq rspiocbq;
2840         uint32_t status;
2841         uint32_t portRspPut, portRspMax;
2842         int rc = 1;
2843         lpfc_iocb_type type;
2844         unsigned long iflag;
2845         uint32_t rsp_cmpl = 0;
2846
2847         spin_lock_irqsave(&phba->hbalock, iflag);
2848         pring->stats.iocb_event++;
2849
2850         /*
2851          * The next available response entry should never exceed the maximum
2852          * entries.  If it does, treat it as an adapter hardware error.
2853          */
2854         portRspMax = pring->numRiocb;
2855         portRspPut = le32_to_cpu(pgp->rspPutInx);
2856         if (unlikely(portRspPut >= portRspMax)) {
2857                 lpfc_sli_rsp_pointers_error(phba, pring);
2858                 spin_unlock_irqrestore(&phba->hbalock, iflag);
2859                 return 1;
2860         }
2861         if (phba->fcp_ring_in_use) {
2862                 spin_unlock_irqrestore(&phba->hbalock, iflag);
2863                 return 1;
2864         } else
2865                 phba->fcp_ring_in_use = 1;
2866
2867         rmb();
2868         while (pring->rspidx != portRspPut) {
2869                 /*
2870                  * Fetch an entry off the ring and copy it into a local data
2871                  * structure.  The copy involves a byte-swap since the
2872                  * network byte order and pci byte orders are different.
2873                  */
2874                 entry = lpfc_resp_iocb(phba, pring);
2875                 phba->last_completion_time = jiffies;
2876
2877                 if (++pring->rspidx >= portRspMax)
2878                         pring->rspidx = 0;
2879
2880                 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
2881                                       (uint32_t *) &rspiocbq.iocb,
2882                                       phba->iocb_rsp_size);
2883                 INIT_LIST_HEAD(&(rspiocbq.list));
2884                 irsp = &rspiocbq.iocb;
2885
2886                 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
2887                 pring->stats.iocb_rsp++;
2888                 rsp_cmpl++;
2889
2890                 if (unlikely(irsp->ulpStatus)) {
2891                         /*
2892                          * If resource errors reported from HBA, reduce
2893                          * queuedepths of the SCSI device.
2894                          */
2895                         if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
2896                                 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
2897                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
2898                                 phba->lpfc_rampdown_queue_depth(phba);
2899                                 spin_lock_irqsave(&phba->hbalock, iflag);
2900                         }
2901
2902                         /* Rsp ring <ringno> error: IOCB */
2903                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
2904                                         "0336 Rsp Ring %d error: IOCB Data: "
2905                                         "x%x x%x x%x x%x x%x x%x x%x x%x\n",
2906                                         pring->ringno,
2907                                         irsp->un.ulpWord[0],
2908                                         irsp->un.ulpWord[1],
2909                                         irsp->un.ulpWord[2],
2910                                         irsp->un.ulpWord[3],
2911                                         irsp->un.ulpWord[4],
2912                                         irsp->un.ulpWord[5],
2913                                         *(uint32_t *)&irsp->un1,
2914                                         *((uint32_t *)&irsp->un1 + 1));
2915                 }
2916
2917                 switch (type) {
2918                 case LPFC_ABORT_IOCB:
2919                 case LPFC_SOL_IOCB:
2920                         /*
2921                          * Idle exchange closed via ABTS from port.  No iocb
2922                          * resources need to be recovered.
2923                          */
2924                         if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
2925                                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
2926                                                 "0333 IOCB cmd 0x%x"
2927                                                 " processed. Skipping"
2928                                                 " completion\n",
2929                                                 irsp->ulpCommand);
2930                                 break;
2931                         }
2932
2933                         cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
2934                                                          &rspiocbq);
2935                         if (unlikely(!cmdiocbq))
2936                                 break;
2937                         if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
2938                                 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
2939                         if (cmdiocbq->iocb_cmpl) {
2940                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
2941                                 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
2942                                                       &rspiocbq);
2943                                 spin_lock_irqsave(&phba->hbalock, iflag);
2944                         }
2945                         break;
2946                 case LPFC_UNSOL_IOCB:
2947                         spin_unlock_irqrestore(&phba->hbalock, iflag);
2948                         lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
2949                         spin_lock_irqsave(&phba->hbalock, iflag);
2950                         break;
2951                 default:
2952                         if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
2953                                 char adaptermsg[LPFC_MAX_ADPTMSG];
2954                                 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
2955                                 memcpy(&adaptermsg[0], (uint8_t *) irsp,
2956                                        MAX_MSG_DATA);
2957                                 dev_warn(&((phba->pcidev)->dev),
2958                                          "lpfc%d: %s\n",
2959                                          phba->brd_no, adaptermsg);
2960                         } else {
2961                                 /* Unknown IOCB command */
2962                                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2963                                                 "0334 Unknown IOCB command "
2964                                                 "Data: x%x, x%x x%x x%x x%x\n",
2965                                                 type, irsp->ulpCommand,
2966                                                 irsp->ulpStatus,
2967                                                 irsp->ulpIoTag,
2968                                                 irsp->ulpContext);
2969                         }
2970                         break;
2971                 }
2972
2973                 /*
2974                  * The response IOCB has been processed.  Update the ring
2975                  * pointer in SLIM.  If the port response put pointer has not
2976                  * been updated, sync the pgp->rspPutInx and fetch the new port
2977                  * response put pointer.
2978                  */
2979                 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
2980
2981                 if (pring->rspidx == portRspPut)
2982                         portRspPut = le32_to_cpu(pgp->rspPutInx);
2983         }
2984
2985         if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
2986                 pring->stats.iocb_rsp_full++;
2987                 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
2988                 writel(status, phba->CAregaddr);
2989                 readl(phba->CAregaddr);
2990         }
2991         if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
2992                 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
2993                 pring->stats.iocb_cmd_empty++;
2994
2995                 /* Force update of the local copy of cmdGetInx */
2996                 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
2997                 lpfc_sli_resume_iocb(phba, pring);
2998
2999                 if ((pring->lpfc_sli_cmd_available))
3000                         (pring->lpfc_sli_cmd_available) (phba, pring);
3001
3002         }
3003
3004         phba->fcp_ring_in_use = 0;
3005         spin_unlock_irqrestore(&phba->hbalock, iflag);
3006         return rc;
3007 }
3008
3009 /**
3010  * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
3011  * @phba: Pointer to HBA context object.
3012  * @pring: Pointer to driver SLI ring object.
3013  * @rspiocbp: Pointer to driver response IOCB object.
3014  *
3015  * This function is called from the worker thread when there is a slow-path
3016  * response IOCB to process. This function chains all the response iocbs until
3017  * seeing the iocb with the LE bit set. The function will call
3018  * lpfc_sli_process_sol_iocb function if the response iocb indicates a
3019  * completion of a command iocb. The function will call the
3020  * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
3021  * The function frees the resources or calls the completion handler if this
3022  * iocb is an abort completion. The function returns NULL when the response
3023  * iocb has the LE bit set and all the chained iocbs are processed, otherwise
3024  * this function shall chain the iocb on to the iocb_continueq and return the
3025  * response iocb passed in.
3026  **/
3027 static struct lpfc_iocbq *
3028 lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3029                         struct lpfc_iocbq *rspiocbp)
3030 {
3031         struct lpfc_iocbq *saveq;
3032         struct lpfc_iocbq *cmdiocbp;
3033         struct lpfc_iocbq *next_iocb;
3034         IOCB_t *irsp = NULL;
3035         uint32_t free_saveq;
3036         uint8_t iocb_cmd_type;
3037         lpfc_iocb_type type;
3038         unsigned long iflag;
3039         int rc;
3040
3041         spin_lock_irqsave(&phba->hbalock, iflag);
3042         /* First add the response iocb to the countinueq list */
3043         list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
3044         pring->iocb_continueq_cnt++;
3045
3046         /* Now, determine whether the list is completed for processing */
3047         irsp = &rspiocbp->iocb;
3048         if (irsp->ulpLe) {
3049                 /*
3050                  * By default, the driver expects to free all resources
3051                  * associated with this iocb completion.
3052                  */
3053                 free_saveq = 1;
3054                 saveq = list_get_first(&pring->iocb_continueq,
3055                                        struct lpfc_iocbq, list);
3056                 irsp = &(saveq->iocb);
3057                 list_del_init(&pring->iocb_continueq);
3058                 pring->iocb_continueq_cnt = 0;
3059
3060                 pring->stats.iocb_rsp++;
3061
3062                 /*
3063                  * If resource errors reported from HBA, reduce
3064                  * queuedepths of the SCSI device.
3065                  */
3066                 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
3067                     (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
3068                         spin_unlock_irqrestore(&phba->hbalock, iflag);
3069                         phba->lpfc_rampdown_queue_depth(phba);
3070                         spin_lock_irqsave(&phba->hbalock, iflag);
3071                 }
3072
3073                 if (irsp->ulpStatus) {
3074                         /* Rsp ring <ringno> error: IOCB */
3075                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3076                                         "0328 Rsp Ring %d error: "
3077                                         "IOCB Data: "
3078                                         "x%x x%x x%x x%x "
3079                                         "x%x x%x x%x x%x "
3080                                         "x%x x%x x%x x%x "
3081                                         "x%x x%x x%x x%x\n",
3082                                         pring->ringno,
3083                                         irsp->un.ulpWord[0],
3084                                         irsp->un.ulpWord[1],
3085                                         irsp->un.ulpWord[2],
3086                                         irsp->un.ulpWord[3],
3087                                         irsp->un.ulpWord[4],
3088                                         irsp->un.ulpWord[5],
3089                                         *(((uint32_t *) irsp) + 6),
3090                                         *(((uint32_t *) irsp) + 7),
3091                                         *(((uint32_t *) irsp) + 8),
3092                                         *(((uint32_t *) irsp) + 9),
3093                                         *(((uint32_t *) irsp) + 10),
3094                                         *(((uint32_t *) irsp) + 11),
3095                                         *(((uint32_t *) irsp) + 12),
3096                                         *(((uint32_t *) irsp) + 13),
3097                                         *(((uint32_t *) irsp) + 14),
3098                                         *(((uint32_t *) irsp) + 15));
3099                 }
3100
3101                 /*
3102                  * Fetch the IOCB command type and call the correct completion
3103                  * routine. Solicited and Unsolicited IOCBs on the ELS ring
3104                  * get freed back to the lpfc_iocb_list by the discovery
3105                  * kernel thread.
3106                  */
3107                 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
3108                 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
3109                 switch (type) {
3110                 case LPFC_SOL_IOCB:
3111                         spin_unlock_irqrestore(&phba->hbalock, iflag);
3112                         rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
3113                         spin_lock_irqsave(&phba->hbalock, iflag);
3114                         break;
3115
3116                 case LPFC_UNSOL_IOCB:
3117                         spin_unlock_irqrestore(&phba->hbalock, iflag);
3118                         rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
3119                         spin_lock_irqsave(&phba->hbalock, iflag);
3120                         if (!rc)
3121                                 free_saveq = 0;
3122                         break;
3123
3124                 case LPFC_ABORT_IOCB:
3125                         cmdiocbp = NULL;
3126                         if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
3127                                 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
3128                                                                  saveq);
3129                         if (cmdiocbp) {
3130                                 /* Call the specified completion routine */
3131                                 if (cmdiocbp->iocb_cmpl) {
3132                                         spin_unlock_irqrestore(&phba->hbalock,
3133                                                                iflag);
3134                                         (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
3135                                                               saveq);
3136                                         spin_lock_irqsave(&phba->hbalock,
3137                                                           iflag);
3138                                 } else
3139                                         __lpfc_sli_release_iocbq(phba,
3140                                                                  cmdiocbp);
3141                         }
3142                         break;
3143
3144                 case LPFC_UNKNOWN_IOCB:
3145                         if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3146                                 char adaptermsg[LPFC_MAX_ADPTMSG];
3147                                 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3148                                 memcpy(&adaptermsg[0], (uint8_t *)irsp,
3149                                        MAX_MSG_DATA);
3150                                 dev_warn(&((phba->pcidev)->dev),
3151                                          "lpfc%d: %s\n",
3152                                          phba->brd_no, adaptermsg);
3153                         } else {
3154                                 /* Unknown IOCB command */
3155                                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3156                                                 "0335 Unknown IOCB "
3157                                                 "command Data: x%x "
3158                                                 "x%x x%x x%x\n",
3159                                                 irsp->ulpCommand,
3160                                                 irsp->ulpStatus,
3161                                                 irsp->ulpIoTag,
3162                                                 irsp->ulpContext);
3163                         }
3164                         break;
3165                 }
3166
3167                 if (free_saveq) {
3168                         list_for_each_entry_safe(rspiocbp, next_iocb,
3169                                                  &saveq->list, list) {
3170                                 list_del(&rspiocbp->list);
3171                                 __lpfc_sli_release_iocbq(phba, rspiocbp);
3172                         }
3173                         __lpfc_sli_release_iocbq(phba, saveq);
3174                 }
3175                 rspiocbp = NULL;
3176         }
3177         spin_unlock_irqrestore(&phba->hbalock, iflag);
3178         return rspiocbp;
3179 }
3180
3181 /**
3182  * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
3183  * @phba: Pointer to HBA context object.
3184  * @pring: Pointer to driver SLI ring object.
3185  * @mask: Host attention register mask for this ring.
3186  *
3187  * This routine wraps the actual slow_ring event process routine from the
3188  * API jump table function pointer from the lpfc_hba struct.
3189  **/
3190 void
3191 lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
3192                                 struct lpfc_sli_ring *pring, uint32_t mask)
3193 {
3194         phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
3195 }
3196
3197 /**
3198  * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
3199  * @phba: Pointer to HBA context object.
3200  * @pring: Pointer to driver SLI ring object.
3201  * @mask: Host attention register mask for this ring.
3202  *
3203  * This function is called from the worker thread when there is a ring event
3204  * for non-fcp rings. The caller does not hold any lock. The function will
3205  * remove each response iocb in the response ring and calls the handle
3206  * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3207  **/
3208 static void
3209 lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
3210                                    struct lpfc_sli_ring *pring, uint32_t mask)
3211 {
3212         struct lpfc_pgp *pgp;
3213         IOCB_t *entry;
3214         IOCB_t *irsp = NULL;
3215         struct lpfc_iocbq *rspiocbp = NULL;
3216         uint32_t portRspPut, portRspMax;
3217         unsigned long iflag;
3218         uint32_t status;
3219
3220         pgp = &phba->port_gp[pring->ringno];
3221         spin_lock_irqsave(&phba->hbalock, iflag);
3222         pring->stats.iocb_event++;
3223
3224         /*
3225          * The next available response entry should never exceed the maximum
3226          * entries.  If it does, treat it as an adapter hardware error.
3227          */
3228         portRspMax = pring->numRiocb;
3229         portRspPut = le32_to_cpu(pgp->rspPutInx);
3230         if (portRspPut >= portRspMax) {
3231                 /*
3232                  * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
3233                  * rsp ring <portRspMax>
3234                  */
3235                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3236                                 "0303 Ring %d handler: portRspPut %d "
3237                                 "is bigger than rsp ring %d\n",
3238                                 pring->ringno, portRspPut, portRspMax);
3239
3240                 phba->link_state = LPFC_HBA_ERROR;
3241                 spin_unlock_irqrestore(&phba->hbalock, iflag);
3242
3243                 phba->work_hs = HS_FFER3;
3244                 lpfc_handle_eratt(phba);
3245
3246                 return;
3247         }
3248
3249         rmb();
3250         while (pring->rspidx != portRspPut) {
3251                 /*
3252                  * Build a completion list and call the appropriate handler.
3253                  * The process is to get the next available response iocb, get
3254                  * a free iocb from the list, copy the response data into the
3255                  * free iocb, insert to the continuation list, and update the
3256                  * next response index to slim.  This process makes response
3257                  * iocb's in the ring available to DMA as fast as possible but
3258                  * pays a penalty for a copy operation.  Since the iocb is
3259                  * only 32 bytes, this penalty is considered small relative to
3260                  * the PCI reads for register values and a slim write.  When
3261                  * the ulpLe field is set, the entire Command has been
3262                  * received.
3263                  */
3264                 entry = lpfc_resp_iocb(phba, pring);
3265
3266                 phba->last_completion_time = jiffies;
3267                 rspiocbp = __lpfc_sli_get_iocbq(phba);
3268                 if (rspiocbp == NULL) {
3269                         printk(KERN_ERR "%s: out of buffers! Failing "
3270                                "completion.\n", __func__);
3271                         break;
3272                 }
3273
3274                 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
3275                                       phba->iocb_rsp_size);
3276                 irsp = &rspiocbp->iocb;
3277
3278                 if (++pring->rspidx >= portRspMax)
3279                         pring->rspidx = 0;
3280
3281                 if (pring->ringno == LPFC_ELS_RING) {
3282                         lpfc_debugfs_slow_ring_trc(phba,
3283                         "IOCB rsp ring:   wd4:x%08x wd6:x%08x wd7:x%08x",
3284                                 *(((uint32_t *) irsp) + 4),
3285                                 *(((uint32_t *) irsp) + 6),
3286                                 *(((uint32_t *) irsp) + 7));
3287                 }
3288
3289                 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
3290
3291                 spin_unlock_irqrestore(&phba->hbalock, iflag);
3292                 /* Handle the response IOCB */
3293                 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
3294                 spin_lock_irqsave(&phba->hbalock, iflag);
3295
3296                 /*
3297                  * If the port response put pointer has not been updated, sync
3298                  * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
3299                  * response put pointer.
3300                  */
3301                 if (pring->rspidx == portRspPut) {
3302                         portRspPut = le32_to_cpu(pgp->rspPutInx);
3303                 }
3304         } /* while (pring->rspidx != portRspPut) */
3305
3306         if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
3307                 /* At least one response entry has been freed */
3308                 pring->stats.iocb_rsp_full++;
3309                 /* SET RxRE_RSP in Chip Att register */
3310                 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3311                 writel(status, phba->CAregaddr);
3312                 readl(phba->CAregaddr); /* flush */
3313         }
3314         if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3315                 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3316                 pring->stats.iocb_cmd_empty++;
3317
3318                 /* Force update of the local copy of cmdGetInx */
3319                 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
3320                 lpfc_sli_resume_iocb(phba, pring);
3321
3322                 if ((pring->lpfc_sli_cmd_available))
3323                         (pring->lpfc_sli_cmd_available) (phba, pring);
3324
3325         }
3326
3327         spin_unlock_irqrestore(&phba->hbalock, iflag);
3328         return;
3329 }
3330
3331 /**
3332  * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
3333  * @phba: Pointer to HBA context object.
3334  * @pring: Pointer to driver SLI ring object.
3335  * @mask: Host attention register mask for this ring.
3336  *
3337  * This function is called from the worker thread when there is a pending
3338  * ELS response iocb on the driver internal slow-path response iocb worker
3339  * queue. The caller does not hold any lock. The function will remove each
3340  * response iocb from the response worker queue and calls the handle
3341  * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3342  **/
3343 static void
3344 lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
3345                                    struct lpfc_sli_ring *pring, uint32_t mask)
3346 {
3347         struct lpfc_iocbq *irspiocbq;
3348         struct hbq_dmabuf *dmabuf;
3349         struct lpfc_cq_event *cq_event;
3350         unsigned long iflag;
3351
3352         spin_lock_irqsave(&phba->hbalock, iflag);
3353         phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
3354         spin_unlock_irqrestore(&phba->hbalock, iflag);
3355         while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
3356                 /* Get the response iocb from the head of work queue */
3357                 spin_lock_irqsave(&phba->hbalock, iflag);
3358                 list_remove_head(&phba->sli4_hba.sp_queue_event,
3359                                  cq_event, struct lpfc_cq_event, list);
3360                 spin_unlock_irqrestore(&phba->hbalock, iflag);
3361
3362                 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
3363                 case CQE_CODE_COMPL_WQE:
3364                         irspiocbq = container_of(cq_event, struct lpfc_iocbq,
3365                                                  cq_event);
3366                         /* Translate ELS WCQE to response IOCBQ */
3367                         irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
3368                                                                    irspiocbq);
3369                         if (irspiocbq)
3370                                 lpfc_sli_sp_handle_rspiocb(phba, pring,
3371                                                            irspiocbq);
3372                         break;
3373                 case CQE_CODE_RECEIVE:
3374                         dmabuf = container_of(cq_event, struct hbq_dmabuf,
3375                                               cq_event);
3376                         lpfc_sli4_handle_received_buffer(phba, dmabuf);
3377                         break;
3378                 default:
3379                         break;
3380                 }
3381         }
3382 }
3383
3384 /**
3385  * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
3386  * @phba: Pointer to HBA context object.
3387  * @pring: Pointer to driver SLI ring object.
3388  *
3389  * This function aborts all iocbs in the given ring and frees all the iocb
3390  * objects in txq. This function issues an abort iocb for all the iocb commands
3391  * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3392  * the return of this function. The caller is not required to hold any locks.
3393  **/
3394 void
3395 lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3396 {
3397         LIST_HEAD(completions);
3398         struct lpfc_iocbq *iocb, *next_iocb;
3399
3400         if (pring->ringno == LPFC_ELS_RING) {
3401                 lpfc_fabric_abort_hba(phba);
3402         }
3403
3404         /* Error everything on txq and txcmplq
3405          * First do the txq.
3406          */
3407         spin_lock_irq(&phba->hbalock);
3408         list_splice_init(&pring->txq, &completions);
3409         pring->txq_cnt = 0;
3410
3411         /* Next issue ABTS for everything on the txcmplq */
3412         list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3413                 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3414
3415         spin_unlock_irq(&phba->hbalock);
3416
3417         /* Cancel all the IOCBs from the completions list */
3418         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3419                               IOERR_SLI_ABORTED);
3420 }
3421
3422 /**
3423  * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
3424  * @phba: Pointer to HBA context object.
3425  *
3426  * This function flushes all iocbs in the fcp ring and frees all the iocb
3427  * objects in txq and txcmplq. This function will not issue abort iocbs
3428  * for all the iocb commands in txcmplq, they will just be returned with
3429  * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3430  * slot has been permanently disabled.
3431  **/
3432 void
3433 lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
3434 {
3435         LIST_HEAD(txq);
3436         LIST_HEAD(txcmplq);
3437         struct lpfc_sli *psli = &phba->sli;
3438         struct lpfc_sli_ring  *pring;
3439
3440         /* Currently, only one fcp ring */
3441         pring = &psli->ring[psli->fcp_ring];
3442
3443         spin_lock_irq(&phba->hbalock);
3444         /* Retrieve everything on txq */
3445         list_splice_init(&pring->txq, &txq);
3446         pring->txq_cnt = 0;
3447
3448         /* Retrieve everything on the txcmplq */
3449         list_splice_init(&pring->txcmplq, &txcmplq);
3450         pring->txcmplq_cnt = 0;
3451         spin_unlock_irq(&phba->hbalock);
3452
3453         /* Flush the txq */
3454         lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
3455                               IOERR_SLI_DOWN);
3456
3457         /* Flush the txcmpq */
3458         lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
3459                               IOERR_SLI_DOWN);
3460 }
3461
3462 /**
3463  * lpfc_sli_brdready_s3 - Check for sli3 host ready status
3464  * @phba: Pointer to HBA context object.
3465  * @mask: Bit mask to be checked.
3466  *
3467  * This function reads the host status register and compares
3468  * with the provided bit mask to check if HBA completed
3469  * the restart. This function will wait in a loop for the
3470  * HBA to complete restart. If the HBA does not restart within
3471  * 15 iterations, the function will reset the HBA again. The
3472  * function returns 1 when HBA fail to restart otherwise returns
3473  * zero.
3474  **/
3475 static int
3476 lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
3477 {
3478         uint32_t status;
3479         int i = 0;
3480         int retval = 0;
3481
3482         /* Read the HBA Host Status Register */
3483         if (lpfc_readl(phba->HSregaddr, &status))
3484                 return 1;
3485
3486         /*
3487          * Check status register every 100ms for 5 retries, then every
3488          * 500ms for 5, then every 2.5 sec for 5, then reset board and
3489          * every 2.5 sec for 4.
3490          * Break our of the loop if errors occurred during init.
3491          */
3492         while (((status & mask) != mask) &&
3493                !(status & HS_FFERM) &&
3494                i++ < 20) {
3495
3496                 if (i <= 5)
3497                         msleep(10);
3498                 else if (i <= 10)
3499                         msleep(500);
3500                 else
3501                         msleep(2500);
3502
3503                 if (i == 15) {
3504                                 /* Do post */
3505                         phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3506                         lpfc_sli_brdrestart(phba);
3507                 }
3508                 /* Read the HBA Host Status Register */
3509                 if (lpfc_readl(phba->HSregaddr, &status)) {
3510                         retval = 1;
3511                         break;
3512                 }
3513         }
3514
3515         /* Check to see if any errors occurred during init */
3516         if ((status & HS_FFERM) || (i >= 20)) {
3517                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3518                                 "2751 Adapter failed to restart, "
3519                                 "status reg x%x, FW Data: A8 x%x AC x%x\n",
3520                                 status,
3521                                 readl(phba->MBslimaddr + 0xa8),
3522                                 readl(phba->MBslimaddr + 0xac));
3523                 phba->link_state = LPFC_HBA_ERROR;
3524                 retval = 1;
3525         }
3526
3527         return retval;
3528 }
3529
3530 /**
3531  * lpfc_sli_brdready_s4 - Check for sli4 host ready status
3532  * @phba: Pointer to HBA context object.
3533  * @mask: Bit mask to be checked.
3534  *
3535  * This function checks the host status register to check if HBA is
3536  * ready. This function will wait in a loop for the HBA to be ready
3537  * If the HBA is not ready , the function will will reset the HBA PCI
3538  * function again. The function returns 1 when HBA fail to be ready
3539  * otherwise returns zero.
3540  **/
3541 static int
3542 lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
3543 {
3544         uint32_t status;
3545         int retval = 0;
3546
3547         /* Read the HBA Host Status Register */
3548         status = lpfc_sli4_post_status_check(phba);
3549
3550         if (status) {
3551                 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3552                 lpfc_sli_brdrestart(phba);
3553                 status = lpfc_sli4_post_status_check(phba);
3554         }
3555
3556         /* Check to see if any errors occurred during init */
3557         if (status) {
3558                 phba->link_state = LPFC_HBA_ERROR;
3559                 retval = 1;
3560         } else
3561                 phba->sli4_hba.intr_enable = 0;
3562
3563         return retval;
3564 }
3565
3566 /**
3567  * lpfc_sli_brdready - Wrapper func for checking the hba readyness
3568  * @phba: Pointer to HBA context object.
3569  * @mask: Bit mask to be checked.
3570  *
3571  * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
3572  * from the API jump table function pointer from the lpfc_hba struct.
3573  **/
3574 int
3575 lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
3576 {
3577         return phba->lpfc_sli_brdready(phba, mask);
3578 }
3579
3580 #define BARRIER_TEST_PATTERN (0xdeadbeef)
3581
3582 /**
3583  * lpfc_reset_barrier - Make HBA ready for HBA reset
3584  * @phba: Pointer to HBA context object.
3585  *
3586  * This function is called before resetting an HBA. This
3587  * function requests HBA to quiesce DMAs before a reset.
3588  **/
3589 void lpfc_reset_barrier(struct lpfc_hba *phba)
3590 {
3591         uint32_t __iomem *resp_buf;
3592         uint32_t __iomem *mbox_buf;
3593         volatile uint32_t mbox;
3594         uint32_t hc_copy, ha_copy, resp_data;
3595         int  i;
3596         uint8_t hdrtype;
3597
3598         pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
3599         if (hdrtype != 0x80 ||
3600             (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
3601              FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
3602                 return;
3603
3604         /*
3605          * Tell the other part of the chip to suspend temporarily all
3606          * its DMA activity.
3607          */
3608         resp_buf = phba->MBslimaddr;
3609
3610         /* Disable the error attention */
3611         if (lpfc_readl(phba->HCregaddr, &hc_copy))
3612                 return;
3613         writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
3614         readl(phba->HCregaddr); /* flush */
3615         phba->link_flag |= LS_IGNORE_ERATT;
3616
3617         if (lpfc_readl(phba->HAregaddr, &ha_copy))
3618                 return;
3619         if (ha_copy & HA_ERATT) {
3620                 /* Clear Chip error bit */
3621                 writel(HA_ERATT, phba->HAregaddr);
3622                 phba->pport->stopped = 1;
3623         }
3624
3625         mbox = 0;
3626         ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
3627         ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
3628
3629         writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
3630         mbox_buf = phba->MBslimaddr;
3631         writel(mbox, mbox_buf);
3632
3633         for (i = 0; i < 50; i++) {
3634                 if (lpfc_readl((resp_buf + 1), &resp_data))
3635                         return;
3636                 if (resp_data != ~(BARRIER_TEST_PATTERN))
3637                         mdelay(1);
3638                 else
3639                         break;
3640         }
3641         resp_data = 0;
3642         if (lpfc_readl((resp_buf + 1), &resp_data))
3643                 return;
3644         if (resp_data  != ~(BARRIER_TEST_PATTERN)) {
3645                 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
3646                     phba->pport->stopped)
3647                         goto restore_hc;
3648                 else
3649                         goto clear_errat;
3650         }
3651
3652         ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
3653         resp_data = 0;
3654         for (i = 0; i < 500; i++) {
3655                 if (lpfc_readl(resp_buf, &resp_data))
3656                         return;
3657                 if (resp_data != mbox)
3658                         mdelay(1);
3659                 else
3660                         break;
3661         }
3662
3663 clear_errat:
3664
3665         while (++i < 500) {
3666                 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3667                         return;
3668                 if (!(ha_copy & HA_ERATT))
3669                         mdelay(1);
3670                 else
3671                         break;
3672         }
3673
3674         if (readl(phba->HAregaddr) & HA_ERATT) {
3675                 writel(HA_ERATT, phba->HAregaddr);
3676                 phba->pport->stopped = 1;
3677         }
3678
3679 restore_hc:
3680         phba->link_flag &= ~LS_IGNORE_ERATT;
3681         writel(hc_copy, phba->HCregaddr);
3682         readl(phba->HCregaddr); /* flush */
3683 }
3684
3685 /**
3686  * lpfc_sli_brdkill - Issue a kill_board mailbox command
3687  * @phba: Pointer to HBA context object.
3688  *
3689  * This function issues a kill_board mailbox command and waits for
3690  * the error attention interrupt. This function is called for stopping
3691  * the firmware processing. The caller is not required to hold any
3692  * locks. This function calls lpfc_hba_down_post function to free
3693  * any pending commands after the kill. The function will return 1 when it
3694  * fails to kill the board else will return 0.
3695  **/
3696 int
3697 lpfc_sli_brdkill(struct lpfc_hba *phba)
3698 {
3699         struct lpfc_sli *psli;
3700         LPFC_MBOXQ_t *pmb;
3701         uint32_t status;
3702         uint32_t ha_copy;
3703         int retval;
3704         int i = 0;
3705
3706         psli = &phba->sli;
3707
3708         /* Kill HBA */
3709         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3710                         "0329 Kill HBA Data: x%x x%x\n",
3711                         phba->pport->port_state, psli->sli_flag);
3712
3713         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3714         if (!pmb)
3715                 return 1;
3716
3717         /* Disable the error attention */
3718         spin_lock_irq(&phba->hbalock);
3719         if (lpfc_readl(phba->HCregaddr, &status)) {
3720                 spin_unlock_irq(&phba->hbalock);
3721                 mempool_free(pmb, phba->mbox_mem_pool);
3722                 return 1;
3723         }
3724         status &= ~HC_ERINT_ENA;
3725         writel(status, phba->HCregaddr);
3726         readl(phba->HCregaddr); /* flush */
3727         phba->link_flag |= LS_IGNORE_ERATT;
3728         spin_unlock_irq(&phba->hbalock);
3729
3730         lpfc_kill_board(phba, pmb);
3731         pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3732         retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3733
3734         if (retval != MBX_SUCCESS) {
3735                 if (retval != MBX_BUSY)
3736                         mempool_free(pmb, phba->mbox_mem_pool);
3737                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3738                                 "2752 KILL_BOARD command failed retval %d\n",
3739                                 retval);
3740                 spin_lock_irq(&phba->hbalock);
3741                 phba->link_flag &= ~LS_IGNORE_ERATT;
3742                 spin_unlock_irq(&phba->hbalock);
3743                 return 1;
3744         }
3745
3746         spin_lock_irq(&phba->hbalock);
3747         psli->sli_flag &= ~LPFC_SLI_ACTIVE;
3748         spin_unlock_irq(&phba->hbalock);
3749
3750         mempool_free(pmb, phba->mbox_mem_pool);
3751
3752         /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
3753          * attention every 100ms for 3 seconds. If we don't get ERATT after
3754          * 3 seconds we still set HBA_ERROR state because the status of the
3755          * board is now undefined.
3756          */
3757         if (lpfc_readl(phba->HAregaddr, &ha_copy))
3758                 return 1;
3759         while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
3760                 mdelay(100);
3761                 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3762                         return 1;
3763         }
3764
3765         del_timer_sync(&psli->mbox_tmo);
3766         if (ha_copy & HA_ERATT) {
3767                 writel(HA_ERATT, phba->HAregaddr);
3768                 phba->pport->stopped = 1;
3769         }
3770         spin_lock_irq(&phba->hbalock);
3771         psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
3772         psli->mbox_active = NULL;
3773         phba->link_flag &= ~LS_IGNORE_ERATT;
3774         spin_unlock_irq(&phba->hbalock);
3775
3776         lpfc_hba_down_post(phba);
3777         phba->link_state = LPFC_HBA_ERROR;
3778
3779         return ha_copy & HA_ERATT ? 0 : 1;
3780 }
3781
3782 /**
3783  * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
3784  * @phba: Pointer to HBA context object.
3785  *
3786  * This function resets the HBA by writing HC_INITFF to the control
3787  * register. After the HBA resets, this function resets all the iocb ring
3788  * indices. This function disables PCI layer parity checking during
3789  * the reset.
3790  * This function returns 0 always.
3791  * The caller is not required to hold any locks.
3792  **/
3793 int
3794 lpfc_sli_brdreset(struct lpfc_hba *phba)
3795 {
3796         struct lpfc_sli *psli;
3797         struct lpfc_sli_ring *pring;
3798         uint16_t cfg_value;
3799         int i;
3800
3801         psli = &phba->sli;
3802
3803         /* Reset HBA */
3804         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3805                         "0325 Reset HBA Data: x%x x%x\n",
3806                         phba->pport->port_state, psli->sli_flag);
3807
3808         /* perform board reset */
3809         phba->fc_eventTag = 0;
3810         phba->link_events = 0;
3811         phba->pport->fc_myDID = 0;
3812         phba->pport->fc_prevDID = 0;
3813
3814         /* Turn off parity checking and serr during the physical reset */
3815         pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3816         pci_write_config_word(phba->pcidev, PCI_COMMAND,
3817                               (cfg_value &
3818                                ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3819
3820         psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
3821
3822         /* Now toggle INITFF bit in the Host Control Register */
3823         writel(HC_INITFF, phba->HCregaddr);
3824         mdelay(1);
3825         readl(phba->HCregaddr); /* flush */
3826         writel(0, phba->HCregaddr);
3827         readl(phba->HCregaddr); /* flush */
3828
3829         /* Restore PCI cmd register */
3830         pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
3831
3832         /* Initialize relevant SLI info */
3833         for (i = 0; i < psli->num_rings; i++) {
3834                 pring = &psli->ring[i];
3835                 pring->flag = 0;
3836                 pring->rspidx = 0;
3837                 pring->next_cmdidx  = 0;
3838                 pring->local_getidx = 0;
3839                 pring->cmdidx = 0;
3840                 pring->missbufcnt = 0;
3841         }
3842
3843         phba->link_state = LPFC_WARM_START;
3844         return 0;
3845 }
3846
3847 /**
3848  * lpfc_sli4_brdreset - Reset a sli-4 HBA
3849  * @phba: Pointer to HBA context object.
3850  *
3851  * This function resets a SLI4 HBA. This function disables PCI layer parity
3852  * checking during resets the device. The caller is not required to hold
3853  * any locks.
3854  *
3855  * This function returns 0 always.
3856  **/
3857 int
3858 lpfc_sli4_brdreset(struct lpfc_hba *phba)
3859 {
3860         struct lpfc_sli *psli = &phba->sli;
3861         uint16_t cfg_value;
3862         uint8_t qindx;
3863
3864         /* Reset HBA */
3865         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3866                         "0295 Reset HBA Data: x%x x%x\n",
3867                         phba->pport->port_state, psli->sli_flag);
3868
3869         /* perform board reset */
3870         phba->fc_eventTag = 0;
3871         phba->link_events = 0;
3872         phba->pport->fc_myDID = 0;
3873         phba->pport->fc_prevDID = 0;
3874
3875         spin_lock_irq(&phba->hbalock);
3876         psli->sli_flag &= ~(LPFC_PROCESS_LA);
3877         phba->fcf.fcf_flag = 0;
3878         /* Clean up the child queue list for the CQs */
3879         list_del_init(&phba->sli4_hba.mbx_wq->list);
3880         list_del_init(&phba->sli4_hba.els_wq->list);
3881         list_del_init(&phba->sli4_hba.hdr_rq->list);
3882         list_del_init(&phba->sli4_hba.dat_rq->list);
3883         list_del_init(&phba->sli4_hba.mbx_cq->list);
3884         list_del_init(&phba->sli4_hba.els_cq->list);
3885         for (qindx = 0; qindx < phba->cfg_fcp_wq_count; qindx++)
3886                 list_del_init(&phba->sli4_hba.fcp_wq[qindx]->list);
3887         qindx = 0;
3888         do
3889                 list_del_init(&phba->sli4_hba.fcp_cq[qindx]->list);
3890         while (++qindx < phba->cfg_fcp_eq_count);
3891         spin_unlock_irq(&phba->hbalock);
3892
3893         /* Now physically reset the device */
3894         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3895                         "0389 Performing PCI function reset!\n");
3896
3897         /* Turn off parity checking and serr during the physical reset */
3898         pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3899         pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
3900                               ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3901
3902         /* Perform FCoE PCI function reset */
3903         lpfc_pci_function_reset(phba);
3904
3905         /* Restore PCI cmd register */
3906         pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
3907
3908         return 0;
3909 }
3910
3911 /**
3912  * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
3913  * @phba: Pointer to HBA context object.
3914  *
3915  * This function is called in the SLI initialization code path to
3916  * restart the HBA. The caller is not required to hold any lock.
3917  * This function writes MBX_RESTART mailbox command to the SLIM and
3918  * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
3919  * function to free any pending commands. The function enables
3920  * POST only during the first initialization. The function returns zero.
3921  * The function does not guarantee completion of MBX_RESTART mailbox
3922  * command before the return of this function.
3923  **/
3924 static int
3925 lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
3926 {
3927         MAILBOX_t *mb;
3928         struct lpfc_sli *psli;
3929         volatile uint32_t word0;
3930         void __iomem *to_slim;
3931         uint32_t hba_aer_enabled;
3932
3933         spin_lock_irq(&phba->hbalock);
3934
3935         /* Take PCIe device Advanced Error Reporting (AER) state */
3936         hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
3937
3938         psli = &phba->sli;
3939
3940         /* Restart HBA */
3941         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3942                         "0337 Restart HBA Data: x%x x%x\n",
3943                         phba->pport->port_state, psli->sli_flag);
3944
3945         word0 = 0;
3946         mb = (MAILBOX_t *) &word0;
3947         mb->mbxCommand = MBX_RESTART;
3948         mb->mbxHc = 1;
3949
3950         lpfc_reset_barrier(phba);
3951
3952         to_slim = phba->MBslimaddr;
3953         writel(*(uint32_t *) mb, to_slim);
3954         readl(to_slim); /* flush */
3955
3956         /* Only skip post after fc_ffinit is completed */
3957         if (phba->pport->port_state)
3958                 word0 = 1;      /* This is really setting up word1 */
3959         else
3960                 word0 = 0;      /* This is really setting up word1 */
3961         to_slim = phba->MBslimaddr + sizeof (uint32_t);
3962         writel(*(uint32_t *) mb, to_slim);
3963         readl(to_slim); /* flush */
3964
3965         lpfc_sli_brdreset(phba);
3966         phba->pport->stopped = 0;
3967         phba->link_state = LPFC_INIT_START;
3968         phba->hba_flag = 0;
3969         spin_unlock_irq(&phba->hbalock);
3970
3971         memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
3972         psli->stats_start = get_seconds();
3973
3974         /* Give the INITFF and Post time to settle. */
3975         mdelay(100);
3976
3977         /* Reset HBA AER if it was enabled, note hba_flag was reset above */
3978         if (hba_aer_enabled)
3979                 pci_disable_pcie_error_reporting(phba->pcidev);
3980
3981         lpfc_hba_down_post(phba);
3982
3983         return 0;
3984 }
3985
3986 /**
3987  * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
3988  * @phba: Pointer to HBA context object.
3989  *
3990  * This function is called in the SLI initialization code path to restart
3991  * a SLI4 HBA. The caller is not required to hold any lock.
3992  * At the end of the function, it calls lpfc_hba_down_post function to
3993  * free any pending commands.
3994  **/
3995 static int
3996 lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
3997 {
3998         struct lpfc_sli *psli = &phba->sli;
3999         uint32_t hba_aer_enabled;
4000
4001         /* Restart HBA */
4002         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4003                         "0296 Restart HBA Data: x%x x%x\n",
4004                         phba->pport->port_state, psli->sli_flag);
4005
4006         /* Take PCIe device Advanced Error Reporting (AER) state */
4007         hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4008
4009         lpfc_sli4_brdreset(phba);
4010
4011         spin_lock_irq(&phba->hbalock);
4012         phba->pport->stopped = 0;
4013         phba->link_state = LPFC_INIT_START;
4014         phba->hba_flag = 0;
4015         spin_unlock_irq(&phba->hbalock);
4016
4017         memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4018         psli->stats_start = get_seconds();
4019
4020         /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4021         if (hba_aer_enabled)
4022                 pci_disable_pcie_error_reporting(phba->pcidev);
4023
4024         lpfc_hba_down_post(phba);
4025
4026         return 0;
4027 }
4028
4029 /**
4030  * lpfc_sli_brdrestart - Wrapper func for restarting hba
4031  * @phba: Pointer to HBA context object.
4032  *
4033  * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
4034  * API jump table function pointer from the lpfc_hba struct.
4035 **/
4036 int
4037 lpfc_sli_brdrestart(struct lpfc_hba *phba)
4038 {
4039         return phba->lpfc_sli_brdrestart(phba);
4040 }
4041
4042 /**
4043  * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
4044  * @phba: Pointer to HBA context object.
4045  *
4046  * This function is called after a HBA restart to wait for successful
4047  * restart of the HBA. Successful restart of the HBA is indicated by
4048  * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
4049  * iteration, the function will restart the HBA again. The function returns
4050  * zero if HBA successfully restarted else returns negative error code.
4051  **/
4052 static int
4053 lpfc_sli_chipset_init(struct lpfc_hba *phba)
4054 {
4055         uint32_t status, i = 0;
4056
4057         /* Read the HBA Host Status Register */
4058         if (lpfc_readl(phba->HSregaddr, &status))
4059                 return -EIO;
4060
4061         /* Check status register to see what current state is */
4062         i = 0;
4063         while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
4064
4065                 /* Check every 10ms for 10 retries, then every 100ms for 90
4066                  * retries, then every 1 sec for 50 retires for a total of
4067                  * ~60 seconds before reset the board again and check every
4068                  * 1 sec for 50 retries. The up to 60 seconds before the
4069                  * board ready is required by the Falcon FIPS zeroization
4070                  * complete, and any reset the board in between shall cause
4071                  * restart of zeroization, further delay the board ready.
4072                  */
4073                 if (i++ >= 200) {
4074                         /* Adapter failed to init, timeout, status reg
4075                            <status> */
4076                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4077                                         "0436 Adapter failed to init, "
4078                                         "timeout, status reg x%x, "
4079                                         "FW Data: A8 x%x AC x%x\n", status,
4080                                         readl(phba->MBslimaddr + 0xa8),
4081                                         readl(phba->MBslimaddr + 0xac));
4082                         phba->link_state = LPFC_HBA_ERROR;
4083                         return -ETIMEDOUT;
4084                 }
4085
4086                 /* Check to see if any errors occurred during init */
4087                 if (status & HS_FFERM) {
4088                         /* ERROR: During chipset initialization */
4089                         /* Adapter failed to init, chipset, status reg
4090                            <status> */
4091                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4092                                         "0437 Adapter failed to init, "
4093                                         "chipset, status reg x%x, "
4094                                         "FW Data: A8 x%x AC x%x\n", status,
4095                                         readl(phba->MBslimaddr + 0xa8),
4096                                         readl(phba->MBslimaddr + 0xac));
4097                         phba->link_state = LPFC_HBA_ERROR;
4098                         return -EIO;
4099                 }
4100
4101                 if (i <= 10)
4102                         msleep(10);
4103                 else if (i <= 100)
4104                         msleep(100);
4105                 else
4106                         msleep(1000);
4107
4108                 if (i == 150) {
4109                         /* Do post */
4110                         phba->pport->port_state = LPFC_VPORT_UNKNOWN;
4111                         lpfc_sli_brdrestart(phba);
4112                 }
4113                 /* Read the HBA Host Status Register */
4114                 if (lpfc_readl(phba->HSregaddr, &status))
4115                         return -EIO;
4116         }
4117
4118         /* Check to see if any errors occurred during init */
4119         if (status & HS_FFERM) {
4120                 /* ERROR: During chipset initialization */
4121                 /* Adapter failed to init, chipset, status reg <status> */
4122                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4123                                 "0438 Adapter failed to init, chipset, "
4124                                 "status reg x%x, "
4125                                 "FW Data: A8 x%x AC x%x\n", status,
4126                                 readl(phba->MBslimaddr + 0xa8),
4127                                 readl(phba->MBslimaddr + 0xac));
4128                 phba->link_state = LPFC_HBA_ERROR;
4129                 return -EIO;
4130         }
4131
4132         /* Clear all interrupt enable conditions */
4133         writel(0, phba->HCregaddr);
4134         readl(phba->HCregaddr); /* flush */
4135
4136         /* setup host attn register */
4137         writel(0xffffffff, phba->HAregaddr);
4138         readl(phba->HAregaddr); /* flush */
4139         return 0;
4140 }
4141
4142 /**
4143  * lpfc_sli_hbq_count - Get the number of HBQs to be configured
4144  *
4145  * This function calculates and returns the number of HBQs required to be
4146  * configured.
4147  **/
4148 int
4149 lpfc_sli_hbq_count(void)
4150 {
4151         return ARRAY_SIZE(lpfc_hbq_defs);
4152 }
4153
4154 /**
4155  * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
4156  *
4157  * This function adds the number of hbq entries in every HBQ to get
4158  * the total number of hbq entries required for the HBA and returns
4159  * the total count.
4160  **/
4161 static int
4162 lpfc_sli_hbq_entry_count(void)
4163 {
4164         int  hbq_count = lpfc_sli_hbq_count();
4165         int  count = 0;
4166         int  i;
4167
4168         for (i = 0; i < hbq_count; ++i)
4169                 count += lpfc_hbq_defs[i]->entry_count;
4170         return count;
4171 }
4172
4173 /**
4174  * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
4175  *
4176  * This function calculates amount of memory required for all hbq entries
4177  * to be configured and returns the total memory required.
4178  **/
4179 int
4180 lpfc_sli_hbq_size(void)
4181 {
4182         return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
4183 }
4184
4185 /**
4186  * lpfc_sli_hbq_setup - configure and initialize HBQs
4187  * @phba: Pointer to HBA context object.
4188  *
4189  * This function is called during the SLI initialization to configure
4190  * all the HBQs and post buffers to the HBQ. The caller is not
4191  * required to hold any locks. This function will return zero if successful
4192  * else it will return negative error code.
4193  **/
4194 static int
4195 lpfc_sli_hbq_setup(struct lpfc_hba *phba)
4196 {
4197         int  hbq_count = lpfc_sli_hbq_count();
4198         LPFC_MBOXQ_t *pmb;
4199         MAILBOX_t *pmbox;
4200         uint32_t hbqno;
4201         uint32_t hbq_entry_index;
4202
4203                                 /* Get a Mailbox buffer to setup mailbox
4204                                  * commands for HBA initialization
4205                                  */
4206         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4207
4208         if (!pmb)
4209                 return -ENOMEM;
4210
4211         pmbox = &pmb->u.mb;
4212
4213         /* Initialize the struct lpfc_sli_hbq structure for each hbq */
4214         phba->link_state = LPFC_INIT_MBX_CMDS;
4215         phba->hbq_in_use = 1;
4216
4217         hbq_entry_index = 0;
4218         for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
4219                 phba->hbqs[hbqno].next_hbqPutIdx = 0;
4220                 phba->hbqs[hbqno].hbqPutIdx      = 0;
4221                 phba->hbqs[hbqno].local_hbqGetIdx   = 0;
4222                 phba->hbqs[hbqno].entry_count =
4223                         lpfc_hbq_defs[hbqno]->entry_count;
4224                 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
4225                         hbq_entry_index, pmb);
4226                 hbq_entry_index += phba->hbqs[hbqno].entry_count;
4227
4228                 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
4229                         /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
4230                            mbxStatus <status>, ring <num> */
4231
4232                         lpfc_printf_log(phba, KERN_ERR,
4233                                         LOG_SLI | LOG_VPORT,
4234                                         "1805 Adapter failed to init. "
4235                                         "Data: x%x x%x x%x\n",
4236                                         pmbox->mbxCommand,
4237                                         pmbox->mbxStatus, hbqno);
4238
4239                         phba->link_state = LPFC_HBA_ERROR;
4240                         mempool_free(pmb, phba->mbox_mem_pool);
4241                         return -ENXIO;
4242                 }
4243         }
4244         phba->hbq_count = hbq_count;
4245
4246         mempool_free(pmb, phba->mbox_mem_pool);
4247
4248         /* Initially populate or replenish the HBQs */
4249         for (hbqno = 0; hbqno < hbq_count; ++hbqno)
4250                 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
4251         return 0;
4252 }
4253
4254 /**
4255  * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
4256  * @phba: Pointer to HBA context object.
4257  *
4258  * This function is called during the SLI initialization to configure
4259  * all the HBQs and post buffers to the HBQ. The caller is not
4260  * required to hold any locks. This function will return zero if successful
4261  * else it will return negative error code.
4262  **/
4263 static int
4264 lpfc_sli4_rb_setup(struct lpfc_hba *phba)
4265 {
4266         phba->hbq_in_use = 1;
4267         phba->hbqs[0].entry_count = lpfc_hbq_defs[0]->entry_count;
4268         phba->hbq_count = 1;
4269         /* Initially populate or replenish the HBQs */
4270         lpfc_sli_hbqbuf_init_hbqs(phba, 0);
4271         return 0;
4272 }
4273
4274 /**
4275  * lpfc_sli_config_port - Issue config port mailbox command
4276  * @phba: Pointer to HBA context object.
4277  * @sli_mode: sli mode - 2/3
4278  *
4279  * This function is called by the sli intialization code path
4280  * to issue config_port mailbox command. This function restarts the
4281  * HBA firmware and issues a config_port mailbox command to configure
4282  * the SLI interface in the sli mode specified by sli_mode
4283  * variable. The caller is not required to hold any locks.
4284  * The function returns 0 if successful, else returns negative error
4285  * code.
4286  **/
4287 int
4288 lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
4289 {
4290         LPFC_MBOXQ_t *pmb;
4291         uint32_t resetcount = 0, rc = 0, done = 0;
4292
4293         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4294         if (!pmb) {
4295                 phba->link_state = LPFC_HBA_ERROR;
4296                 return -ENOMEM;
4297         }
4298
4299         phba->sli_rev = sli_mode;
4300         while (resetcount < 2 && !done) {
4301                 spin_lock_irq(&phba->hbalock);
4302                 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
4303                 spin_unlock_irq(&phba->hbalock);
4304                 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
4305                 lpfc_sli_brdrestart(phba);
4306                 rc = lpfc_sli_chipset_init(phba);
4307                 if (rc)
4308                         break;
4309
4310                 spin_lock_irq(&phba->hbalock);
4311                 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
4312                 spin_unlock_irq(&phba->hbalock);
4313                 resetcount++;
4314
4315                 /* Call pre CONFIG_PORT mailbox command initialization.  A
4316                  * value of 0 means the call was successful.  Any other
4317                  * nonzero value is a failure, but if ERESTART is returned,
4318                  * the driver may reset the HBA and try again.
4319                  */
4320                 rc = lpfc_config_port_prep(phba);
4321                 if (rc == -ERESTART) {
4322                         phba->link_state = LPFC_LINK_UNKNOWN;
4323                         continue;
4324                 } else if (rc)
4325                         break;
4326                 phba->link_state = LPFC_INIT_MBX_CMDS;
4327                 lpfc_config_port(phba, pmb);
4328                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
4329                 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
4330                                         LPFC_SLI3_HBQ_ENABLED |
4331                                         LPFC_SLI3_CRP_ENABLED |
4332                                         LPFC_SLI3_BG_ENABLED |
4333                                         LPFC_SLI3_DSS_ENABLED);
4334                 if (rc != MBX_SUCCESS) {
4335                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4336                                 "0442 Adapter failed to init, mbxCmd x%x "
4337                                 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
4338                                 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
4339                         spin_lock_irq(&phba->hbalock);
4340                         phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
4341                         spin_unlock_irq(&phba->hbalock);
4342                         rc = -ENXIO;
4343                 } else {
4344                         /* Allow asynchronous mailbox command to go through */
4345                         spin_lock_irq(&phba->hbalock);
4346                         phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4347                         spin_unlock_irq(&phba->hbalock);
4348                         done = 1;
4349                 }
4350         }
4351         if (!done) {
4352                 rc = -EINVAL;
4353                 goto do_prep_failed;
4354         }
4355         if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
4356                 if (!pmb->u.mb.un.varCfgPort.cMA) {
4357                         rc = -ENXIO;
4358                         goto do_prep_failed;
4359                 }
4360                 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
4361                         phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
4362                         phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
4363                         phba->max_vports = (phba->max_vpi > phba->max_vports) ?
4364                                 phba->max_vpi : phba->max_vports;
4365
4366                 } else
4367                         phba->max_vpi = 0;
4368                 phba->fips_level = 0;
4369                 phba->fips_spec_rev = 0;
4370                 if (pmb->u.mb.un.varCfgPort.gdss) {
4371                         phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
4372                         phba->fips_level = pmb->u.mb.un.varCfgPort.fips_level;
4373                         phba->fips_spec_rev = pmb->u.mb.un.varCfgPort.fips_rev;
4374                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4375                                         "2850 Security Crypto Active. FIPS x%d "
4376                                         "(Spec Rev: x%d)",
4377                                         phba->fips_level, phba->fips_spec_rev);
4378                 }
4379                 if (pmb->u.mb.un.varCfgPort.sec_err) {
4380                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4381                                         "2856 Config Port Security Crypto "
4382                                         "Error: x%x ",
4383                                         pmb->u.mb.un.varCfgPort.sec_err);
4384                 }
4385                 if (pmb->u.mb.un.varCfgPort.gerbm)
4386                         phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
4387                 if (pmb->u.mb.un.varCfgPort.gcrp)
4388                         phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
4389
4390                 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
4391                 phba->port_gp = phba->mbox->us.s3_pgp.port;
4392
4393                 if (phba->cfg_enable_bg) {
4394                         if (pmb->u.mb.un.varCfgPort.gbg)
4395                                 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
4396                         else
4397                                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4398                                                 "0443 Adapter did not grant "
4399                                                 "BlockGuard\n");
4400                 }
4401         } else {
4402                 phba->hbq_get = NULL;
4403                 phba->port_gp = phba->mbox->us.s2.port;
4404                 phba->max_vpi = 0;
4405         }
4406 do_prep_failed:
4407         mempool_free(pmb, phba->mbox_mem_pool);
4408         return rc;
4409 }
4410
4411
4412 /**
4413  * lpfc_sli_hba_setup - SLI intialization function
4414  * @phba: Pointer to HBA context object.
4415  *
4416  * This function is the main SLI intialization function. This function
4417  * is called by the HBA intialization code, HBA reset code and HBA
4418  * error attention handler code. Caller is not required to hold any
4419  * locks. This function issues config_port mailbox command to configure
4420  * the SLI, setup iocb rings and HBQ rings. In the end the function
4421  * calls the config_port_post function to issue init_link mailbox
4422  * command and to start the discovery. The function will return zero
4423  * if successful, else it will return negative error code.
4424  **/
4425 int
4426 lpfc_sli_hba_setup(struct lpfc_hba *phba)
4427 {
4428         uint32_t rc;
4429         int  mode = 3;
4430
4431         switch (lpfc_sli_mode) {
4432         case 2:
4433                 if (phba->cfg_enable_npiv) {
4434                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
4435                                 "1824 NPIV enabled: Override lpfc_sli_mode "
4436                                 "parameter (%d) to auto (0).\n",
4437                                 lpfc_sli_mode);
4438                         break;
4439                 }
4440                 mode = 2;
4441                 break;
4442         case 0:
4443         case 3:
4444                 break;
4445         default:
4446                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
4447                                 "1819 Unrecognized lpfc_sli_mode "
4448                                 "parameter: %d.\n", lpfc_sli_mode);
4449
4450                 break;
4451         }
4452
4453         rc = lpfc_sli_config_port(phba, mode);
4454
4455         if (rc && lpfc_sli_mode == 3)
4456                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
4457                                 "1820 Unable to select SLI-3.  "
4458                                 "Not supported by adapter.\n");
4459         if (rc && mode != 2)
4460                 rc = lpfc_sli_config_port(phba, 2);
4461         if (rc)
4462                 goto lpfc_sli_hba_setup_error;
4463
4464         /* Enable PCIe device Advanced Error Reporting (AER) if configured */
4465         if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
4466                 rc = pci_enable_pcie_error_reporting(phba->pcidev);
4467                 if (!rc) {
4468                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4469                                         "2709 This device supports "
4470                                         "Advanced Error Reporting (AER)\n");
4471                         spin_lock_irq(&phba->hbalock);
4472                         phba->hba_flag |= HBA_AER_ENABLED;
4473                         spin_unlock_irq(&phba->hbalock);
4474                 } else {
4475                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4476                                         "2708 This device does not support "
4477                                         "Advanced Error Reporting (AER)\n");
4478                         phba->cfg_aer_support = 0;
4479                 }
4480         }
4481
4482         if (phba->sli_rev == 3) {
4483                 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
4484                 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
4485         } else {
4486                 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
4487                 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
4488                 phba->sli3_options = 0;
4489         }
4490
4491         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4492                         "0444 Firmware in SLI %x mode. Max_vpi %d\n",
4493                         phba->sli_rev, phba->max_vpi);
4494         rc = lpfc_sli_ring_map(phba);
4495
4496         if (rc)
4497                 goto lpfc_sli_hba_setup_error;
4498
4499         /* Init HBQs */
4500         if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
4501                 rc = lpfc_sli_hbq_setup(phba);
4502                 if (rc)
4503                         goto lpfc_sli_hba_setup_error;
4504         }
4505         spin_lock_irq(&phba->hbalock);
4506         phba->sli.sli_flag |= LPFC_PROCESS_LA;
4507         spin_unlock_irq(&phba->hbalock);
4508
4509         rc = lpfc_config_port_post(phba);
4510         if (rc)
4511                 goto lpfc_sli_hba_setup_error;
4512
4513         return rc;
4514
4515 lpfc_sli_hba_setup_error:
4516         phba->link_state = LPFC_HBA_ERROR;
4517         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4518                         "0445 Firmware initialization failed\n");
4519         return rc;
4520 }
4521
4522 /**
4523  * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
4524  * @phba: Pointer to HBA context object.
4525  * @mboxq: mailbox pointer.
4526  * This function issue a dump mailbox command to read config region
4527  * 23 and parse the records in the region and populate driver
4528  * data structure.
4529  **/
4530 static int
4531 lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba,
4532                 LPFC_MBOXQ_t *mboxq)
4533 {
4534         struct lpfc_dmabuf *mp;
4535         struct lpfc_mqe *mqe;
4536         uint32_t data_length;
4537         int rc;
4538
4539         /* Program the default value of vlan_id and fc_map */
4540         phba->valid_vlan = 0;
4541         phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4542         phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4543         phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
4544
4545         mqe = &mboxq->u.mqe;
4546         if (lpfc_dump_fcoe_param(phba, mboxq))
4547                 return -ENOMEM;
4548
4549         mp = (struct lpfc_dmabuf *) mboxq->context1;
4550         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4551
4552         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4553                         "(%d):2571 Mailbox cmd x%x Status x%x "
4554                         "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4555                         "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4556                         "CQ: x%x x%x x%x x%x\n",
4557                         mboxq->vport ? mboxq->vport->vpi : 0,
4558                         bf_get(lpfc_mqe_command, mqe),
4559                         bf_get(lpfc_mqe_status, mqe),
4560                         mqe->un.mb_words[0], mqe->un.mb_words[1],
4561                         mqe->un.mb_words[2], mqe->un.mb_words[3],
4562                         mqe->un.mb_words[4], mqe->un.mb_words[5],
4563                         mqe->un.mb_words[6], mqe->un.mb_words[7],
4564                         mqe->un.mb_words[8], mqe->un.mb_words[9],
4565                         mqe->un.mb_words[10], mqe->un.mb_words[11],
4566                         mqe->un.mb_words[12], mqe->un.mb_words[13],
4567                         mqe->un.mb_words[14], mqe->un.mb_words[15],
4568                         mqe->un.mb_words[16], mqe->un.mb_words[50],
4569                         mboxq->mcqe.word0,
4570                         mboxq->mcqe.mcqe_tag0,  mboxq->mcqe.mcqe_tag1,
4571                         mboxq->mcqe.trailer);
4572
4573         if (rc) {
4574                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4575                 kfree(mp);
4576                 return -EIO;
4577         }
4578         data_length = mqe->un.mb_words[5];
4579         if (data_length > DMP_RGN23_SIZE) {
4580                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4581                 kfree(mp);
4582                 return -EIO;
4583         }
4584
4585         lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
4586         lpfc_mbuf_free(phba, mp->virt, mp->phys);
4587         kfree(mp);
4588         return 0;
4589 }
4590
4591 /**
4592  * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
4593  * @phba: pointer to lpfc hba data structure.
4594  * @mboxq: pointer to the LPFC_MBOXQ_t structure.
4595  * @vpd: pointer to the memory to hold resulting port vpd data.
4596  * @vpd_size: On input, the number of bytes allocated to @vpd.
4597  *            On output, the number of data bytes in @vpd.
4598  *
4599  * This routine executes a READ_REV SLI4 mailbox command.  In
4600  * addition, this routine gets the port vpd data.
4601  *
4602  * Return codes
4603  *      0 - successful
4604  *      -ENOMEM - could not allocated memory.
4605  **/
4606 static int
4607 lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
4608                     uint8_t *vpd, uint32_t *vpd_size)
4609 {
4610         int rc = 0;
4611         uint32_t dma_size;
4612         struct lpfc_dmabuf *dmabuf;
4613         struct lpfc_mqe *mqe;
4614
4615         dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4616         if (!dmabuf)
4617                 return -ENOMEM;
4618
4619         /*
4620          * Get a DMA buffer for the vpd data resulting from the READ_REV
4621          * mailbox command.
4622          */
4623         dma_size = *vpd_size;
4624         dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
4625                                           dma_size,
4626                                           &dmabuf->phys,
4627                                           GFP_KERNEL);
4628         if (!dmabuf->virt) {
4629                 kfree(dmabuf);
4630                 return -ENOMEM;
4631         }
4632         memset(dmabuf->virt, 0, dma_size);
4633
4634         /*
4635          * The SLI4 implementation of READ_REV conflicts at word1,
4636          * bits 31:16 and SLI4 adds vpd functionality not present
4637          * in SLI3.  This code corrects the conflicts.
4638          */
4639         lpfc_read_rev(phba, mboxq);
4640         mqe = &mboxq->u.mqe;
4641         mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
4642         mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
4643         mqe->un.read_rev.word1 &= 0x0000FFFF;
4644         bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
4645         bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
4646
4647         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4648         if (rc) {
4649                 dma_free_coherent(&phba->pcidev->dev, dma_size,
4650                                   dmabuf->virt, dmabuf->phys);
4651                 kfree(dmabuf);
4652                 return -EIO;
4653         }
4654
4655         /*
4656          * The available vpd length cannot be bigger than the
4657          * DMA buffer passed to the port.  Catch the less than
4658          * case and update the caller's size.
4659          */
4660         if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
4661                 *vpd_size = mqe->un.read_rev.avail_vpd_len;
4662
4663         memcpy(vpd, dmabuf->virt, *vpd_size);
4664
4665         dma_free_coherent(&phba->pcidev->dev, dma_size,
4666                           dmabuf->virt, dmabuf->phys);
4667         kfree(dmabuf);
4668         return 0;
4669 }
4670
4671 /**
4672  * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
4673  * @phba: pointer to lpfc hba data structure.
4674  *
4675  * This routine is called to explicitly arm the SLI4 device's completion and
4676  * event queues
4677  **/
4678 static void
4679 lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
4680 {
4681         uint8_t fcp_eqidx;
4682
4683         lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
4684         lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
4685         fcp_eqidx = 0;
4686         do
4687                 lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[fcp_eqidx],
4688                                      LPFC_QUEUE_REARM);
4689         while (++fcp_eqidx < phba->cfg_fcp_eq_count);
4690         lpfc_sli4_eq_release(phba->sli4_hba.sp_eq, LPFC_QUEUE_REARM);
4691         for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++)
4692                 lpfc_sli4_eq_release(phba->sli4_hba.fp_eq[fcp_eqidx],
4693                                      LPFC_QUEUE_REARM);
4694 }
4695
4696 /**
4697  * lpfc_sli4_hba_setup - SLI4 device intialization PCI function
4698  * @phba: Pointer to HBA context object.
4699  *
4700  * This function is the main SLI4 device intialization PCI function. This
4701  * function is called by the HBA intialization code, HBA reset code and
4702  * HBA error attention handler code. Caller is not required to hold any
4703  * locks.
4704  **/
4705 int
4706 lpfc_sli4_hba_setup(struct lpfc_hba *phba)
4707 {
4708         int rc;
4709         LPFC_MBOXQ_t *mboxq;
4710         struct lpfc_mqe *mqe;
4711         uint8_t *vpd;
4712         uint32_t vpd_size;
4713         uint32_t ftr_rsp = 0;
4714         struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
4715         struct lpfc_vport *vport = phba->pport;
4716         struct lpfc_dmabuf *mp;
4717
4718         /*
4719          * TODO:  Why does this routine execute these task in a different
4720          * order from probe?
4721          */
4722         /* Perform a PCI function reset to start from clean */
4723         rc = lpfc_pci_function_reset(phba);
4724         if (unlikely(rc))
4725                 return -ENODEV;
4726
4727         /* Check the HBA Host Status Register for readyness */
4728         rc = lpfc_sli4_post_status_check(phba);
4729         if (unlikely(rc))
4730                 return -ENODEV;
4731         else {
4732                 spin_lock_irq(&phba->hbalock);
4733                 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
4734                 spin_unlock_irq(&phba->hbalock);
4735         }
4736
4737         /*
4738          * Allocate a single mailbox container for initializing the
4739          * port.
4740          */
4741         mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4742         if (!mboxq)
4743                 return -ENOMEM;
4744
4745         /*
4746          * Continue initialization with default values even if driver failed
4747          * to read FCoE param config regions
4748          */
4749         if (lpfc_sli4_read_fcoe_params(phba, mboxq))
4750                 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
4751                         "2570 Failed to read FCoE parameters\n");
4752
4753         /* Issue READ_REV to collect vpd and FW information. */
4754         vpd_size = SLI4_PAGE_SIZE;
4755         vpd = kzalloc(vpd_size, GFP_KERNEL);
4756         if (!vpd) {
4757                 rc = -ENOMEM;
4758                 goto out_free_mbox;
4759         }
4760
4761         rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
4762         if (unlikely(rc)) {
4763                 kfree(vpd);
4764                 goto out_free_mbox;
4765         }
4766         mqe = &mboxq->u.mqe;
4767         phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
4768         if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev))
4769                 phba->hba_flag |= HBA_FCOE_MODE;
4770         else
4771                 phba->hba_flag &= ~HBA_FCOE_MODE;
4772
4773         if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
4774                 LPFC_DCBX_CEE_MODE)
4775                 phba->hba_flag |= HBA_FIP_SUPPORT;
4776         else
4777                 phba->hba_flag &= ~HBA_FIP_SUPPORT;
4778
4779         if (phba->sli_rev != LPFC_SLI_REV4) {
4780                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4781                         "0376 READ_REV Error. SLI Level %d "
4782                         "FCoE enabled %d\n",
4783                         phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
4784                 rc = -EIO;
4785                 kfree(vpd);
4786                 goto out_free_mbox;
4787         }
4788         /*
4789          * Evaluate the read rev and vpd data. Populate the driver
4790          * state with the results. If this routine fails, the failure
4791          * is not fatal as the driver will use generic values.
4792          */
4793         rc = lpfc_parse_vpd(phba, vpd, vpd_size);
4794         if (unlikely(!rc)) {
4795                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4796                                 "0377 Error %d parsing vpd. "
4797                                 "Using defaults.\n", rc);
4798                 rc = 0;
4799         }
4800         kfree(vpd);
4801
4802         /* Save information as VPD data */
4803         phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
4804         phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
4805         phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
4806         phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
4807                                          &mqe->un.read_rev);
4808         phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
4809                                        &mqe->un.read_rev);
4810         phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
4811                                             &mqe->un.read_rev);
4812         phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
4813                                            &mqe->un.read_rev);
4814         phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
4815         memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
4816         phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
4817         memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
4818         phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
4819         memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
4820         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4821                         "(%d):0380 READ_REV Status x%x "
4822                         "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
4823                         mboxq->vport ? mboxq->vport->vpi : 0,
4824                         bf_get(lpfc_mqe_status, mqe),
4825                         phba->vpd.rev.opFwName,
4826                         phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
4827                         phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
4828
4829         /*
4830          * Discover the port's supported feature set and match it against the
4831          * hosts requests.
4832          */
4833         lpfc_request_features(phba, mboxq);
4834         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4835         if (unlikely(rc)) {
4836                 rc = -EIO;
4837                 goto out_free_mbox;
4838         }
4839
4840         /*
4841          * The port must support FCP initiator mode as this is the
4842          * only mode running in the host.
4843          */
4844         if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
4845                 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
4846                                 "0378 No support for fcpi mode.\n");
4847                 ftr_rsp++;
4848         }
4849         if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
4850                 phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
4851         else
4852                 phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
4853         /*
4854          * If the port cannot support the host's requested features
4855          * then turn off the global config parameters to disable the
4856          * feature in the driver.  This is not a fatal error.
4857          */
4858         if ((phba->cfg_enable_bg) &&
4859             !(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
4860                 ftr_rsp++;
4861
4862         if (phba->max_vpi && phba->cfg_enable_npiv &&
4863             !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
4864                 ftr_rsp++;
4865
4866         if (ftr_rsp) {
4867                 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
4868                                 "0379 Feature Mismatch Data: x%08x %08x "
4869                                 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
4870                                 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
4871                                 phba->cfg_enable_npiv, phba->max_vpi);
4872                 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
4873                         phba->cfg_enable_bg = 0;
4874                 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
4875                         phba->cfg_enable_npiv = 0;
4876         }
4877
4878         /* These SLI3 features are assumed in SLI4 */
4879         spin_lock_irq(&phba->hbalock);
4880         phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
4881         spin_unlock_irq(&phba->hbalock);
4882
4883         /* Read the port's service parameters. */
4884         rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
4885         if (rc) {
4886                 phba->link_state = LPFC_HBA_ERROR;
4887                 rc = -ENOMEM;
4888                 goto out_free_mbox;
4889         }
4890
4891         mboxq->vport = vport;
4892         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4893         mp = (struct lpfc_dmabuf *) mboxq->context1;
4894         if (rc == MBX_SUCCESS) {
4895                 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
4896                 rc = 0;
4897         }
4898
4899         /*
4900          * This memory was allocated by the lpfc_read_sparam routine. Release
4901          * it to the mbuf pool.
4902          */
4903         lpfc_mbuf_free(phba, mp->virt, mp->phys);
4904         kfree(mp);
4905         mboxq->context1 = NULL;
4906         if (unlikely(rc)) {
4907                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4908                                 "0382 READ_SPARAM command failed "
4909                                 "status %d, mbxStatus x%x\n",
4910                                 rc, bf_get(lpfc_mqe_status, mqe));
4911                 phba->link_state = LPFC_HBA_ERROR;
4912                 rc = -EIO;
4913                 goto out_free_mbox;
4914         }
4915
4916         lpfc_update_vport_wwn(vport);
4917
4918         /* Update the fc_host data structures with new wwn. */
4919         fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
4920         fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
4921
4922         /* Register SGL pool to the device using non-embedded mailbox command */
4923         rc = lpfc_sli4_post_sgl_list(phba);
4924         if (unlikely(rc)) {
4925                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4926                                 "0582 Error %d during sgl post operation\n",
4927                                         rc);
4928                 rc = -ENODEV;
4929                 goto out_free_mbox;
4930         }
4931
4932         /* Register SCSI SGL pool to the device */
4933         rc = lpfc_sli4_repost_scsi_sgl_list(phba);
4934         if (unlikely(rc)) {
4935                 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
4936                                 "0383 Error %d during scsi sgl post "
4937                                 "operation\n", rc);
4938                 /* Some Scsi buffers were moved to the abort scsi list */
4939                 /* A pci function reset will repost them */
4940                 rc = -ENODEV;
4941                 goto out_free_mbox;
4942         }
4943
4944         /* Post the rpi header region to the device. */
4945         rc = lpfc_sli4_post_all_rpi_hdrs(phba);
4946         if (unlikely(rc)) {
4947                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4948                                 "0393 Error %d during rpi post operation\n",
4949                                 rc);
4950                 rc = -ENODEV;
4951                 goto out_free_mbox;
4952         }
4953
4954         /* Set up all the queues to the device */
4955         rc = lpfc_sli4_queue_setup(phba);
4956         if (unlikely(rc)) {
4957                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4958                                 "0381 Error %d during queue setup.\n ", rc);
4959                 goto out_stop_timers;
4960         }
4961
4962         /* Arm the CQs and then EQs on device */
4963         lpfc_sli4_arm_cqeq_intr(phba);
4964
4965         /* Indicate device interrupt mode */
4966         phba->sli4_hba.intr_enable = 1;
4967
4968         /* Allow asynchronous mailbox command to go through */
4969         spin_lock_irq(&phba->hbalock);
4970         phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4971         spin_unlock_irq(&phba->hbalock);
4972
4973         /* Post receive buffers to the device */
4974         lpfc_sli4_rb_setup(phba);
4975
4976         /* Reset HBA FCF states after HBA reset */
4977         phba->fcf.fcf_flag = 0;
4978         phba->fcf.current_rec.flag = 0;
4979
4980         /* Start the ELS watchdog timer */
4981         mod_timer(&vport->els_tmofunc,
4982                   jiffies + HZ * (phba->fc_ratov * 2));
4983
4984         /* Start heart beat timer */
4985         mod_timer(&phba->hb_tmofunc,
4986                   jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
4987         phba->hb_outstanding = 0;
4988         phba->last_completion_time = jiffies;
4989
4990         /* Start error attention (ERATT) polling timer */
4991         mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
4992
4993         /* Enable PCIe device Advanced Error Reporting (AER) if configured */
4994         if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
4995                 rc = pci_enable_pcie_error_reporting(phba->pcidev);
4996                 if (!rc) {
4997                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4998                                         "2829 This device supports "
4999                                         "Advanced Error Reporting (AER)\n");
5000                         spin_lock_irq(&phba->hbalock);
5001                         phba->hba_flag |= HBA_AER_ENABLED;
5002                         spin_unlock_irq(&phba->hbalock);
5003                 } else {
5004                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5005                                         "2830 This device does not support "
5006                                         "Advanced Error Reporting (AER)\n");
5007                         phba->cfg_aer_support = 0;
5008                 }
5009         }
5010
5011         if (!(phba->hba_flag & HBA_FCOE_MODE)) {
5012                 /*
5013                  * The FC Port needs to register FCFI (index 0)
5014                  */
5015                 lpfc_reg_fcfi(phba, mboxq);
5016                 mboxq->vport = phba->pport;
5017                 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5018                 if (rc != MBX_SUCCESS)
5019                         goto out_unset_queue;
5020                 rc = 0;
5021                 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
5022                                         &mboxq->u.mqe.un.reg_fcfi);
5023         }
5024         /*
5025          * The port is ready, set the host's link state to LINK_DOWN
5026          * in preparation for link interrupts.
5027          */
5028         spin_lock_irq(&phba->hbalock);
5029         phba->link_state = LPFC_LINK_DOWN;
5030         spin_unlock_irq(&phba->hbalock);
5031         if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK)
5032                 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
5033 out_unset_queue:
5034         /* Unset all the queues set up in this routine when error out */
5035         if (rc)
5036                 lpfc_sli4_queue_unset(phba);
5037 out_stop_timers:
5038         if (rc)
5039                 lpfc_stop_hba_timers(phba);
5040 out_free_mbox:
5041         mempool_free(mboxq, phba->mbox_mem_pool);
5042         return rc;
5043 }
5044
5045 /**
5046  * lpfc_mbox_timeout - Timeout call back function for mbox timer
5047  * @ptr: context object - pointer to hba structure.
5048  *
5049  * This is the callback function for mailbox timer. The mailbox
5050  * timer is armed when a new mailbox command is issued and the timer
5051  * is deleted when the mailbox complete. The function is called by
5052  * the kernel timer code when a mailbox does not complete within
5053  * expected time. This function wakes up the worker thread to
5054  * process the mailbox timeout and returns. All the processing is
5055  * done by the worker thread function lpfc_mbox_timeout_handler.
5056  **/
5057 void
5058 lpfc_mbox_timeout(unsigned long ptr)
5059 {
5060         struct lpfc_hba  *phba = (struct lpfc_hba *) ptr;
5061         unsigned long iflag;
5062         uint32_t tmo_posted;
5063
5064         spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
5065         tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
5066         if (!tmo_posted)
5067                 phba->pport->work_port_events |= WORKER_MBOX_TMO;
5068         spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
5069
5070         if (!tmo_posted)
5071                 lpfc_worker_wake_up(phba);
5072         return;
5073 }
5074
5075
5076 /**
5077  * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
5078  * @phba: Pointer to HBA context object.
5079  *
5080  * This function is called from worker thread when a mailbox command times out.
5081  * The caller is not required to hold any locks. This function will reset the
5082  * HBA and recover all the pending commands.
5083  **/
5084 void
5085 lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
5086 {
5087         LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
5088         MAILBOX_t *mb = &pmbox->u.mb;
5089         struct lpfc_sli *psli = &phba->sli;
5090         struct lpfc_sli_ring *pring;
5091
5092         /* Check the pmbox pointer first.  There is a race condition
5093          * between the mbox timeout handler getting executed in the
5094          * worklist and the mailbox actually completing. When this
5095          * race condition occurs, the mbox_active will be NULL.
5096          */
5097         spin_lock_irq(&phba->hbalock);
5098         if (pmbox == NULL) {
5099                 lpfc_printf_log(phba, KERN_WARNING,
5100                                 LOG_MBOX | LOG_SLI,
5101                                 "0353 Active Mailbox cleared - mailbox timeout "
5102                                 "exiting\n");
5103                 spin_unlock_irq(&phba->hbalock);
5104                 return;
5105         }
5106
5107         /* Mbox cmd <mbxCommand> timeout */
5108         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5109                         "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
5110                         mb->mbxCommand,
5111                         phba->pport->port_state,
5112                         phba->sli.sli_flag,
5113                         phba->sli.mbox_active);
5114         spin_unlock_irq(&phba->hbalock);
5115
5116         /* Setting state unknown so lpfc_sli_abort_iocb_ring
5117          * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
5118          * it to fail all outstanding SCSI IO.
5119          */
5120         spin_lock_irq(&phba->pport->work_port_lock);
5121         phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
5122         spin_unlock_irq(&phba->pport->work_port_lock);
5123         spin_lock_irq(&phba->hbalock);
5124         phba->link_state = LPFC_LINK_UNKNOWN;
5125         psli->sli_flag &= ~LPFC_SLI_ACTIVE;
5126         spin_unlock_irq(&phba->hbalock);
5127
5128         pring = &psli->ring[psli->fcp_ring];
5129         lpfc_sli_abort_iocb_ring(phba, pring);
5130
5131         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5132                         "0345 Resetting board due to mailbox timeout\n");
5133
5134         /* Reset the HBA device */
5135         lpfc_reset_hba(phba);
5136 }
5137
5138 /**
5139  * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
5140  * @phba: Pointer to HBA context object.
5141  * @pmbox: Pointer to mailbox object.
5142  * @flag: Flag indicating how the mailbox need to be processed.
5143  *
5144  * This function is called by discovery code and HBA management code
5145  * to submit a mailbox command to firmware with SLI-3 interface spec. This
5146  * function gets the hbalock to protect the data structures.
5147  * The mailbox command can be submitted in polling mode, in which case
5148  * this function will wait in a polling loop for the completion of the
5149  * mailbox.
5150  * If the mailbox is submitted in no_wait mode (not polling) the
5151  * function will submit the command and returns immediately without waiting
5152  * for the mailbox completion. The no_wait is supported only when HBA
5153  * is in SLI2/SLI3 mode - interrupts are enabled.
5154  * The SLI interface allows only one mailbox pending at a time. If the
5155  * mailbox is issued in polling mode and there is already a mailbox
5156  * pending, then the function will return an error. If the mailbox is issued
5157  * in NO_WAIT mode and there is a mailbox pending already, the function
5158  * will return MBX_BUSY after queuing the mailbox into mailbox queue.
5159  * The sli layer owns the mailbox object until the completion of mailbox
5160  * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
5161  * return codes the caller owns the mailbox command after the return of
5162  * the function.
5163  **/
5164 static int
5165 lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
5166                        uint32_t flag)
5167 {
5168         MAILBOX_t *mb;
5169         struct lpfc_sli *psli = &phba->sli;
5170         uint32_t status, evtctr;
5171         uint32_t ha_copy, hc_copy;
5172         int i;
5173         unsigned long timeout;
5174         unsigned long drvr_flag = 0;
5175         uint32_t word0, ldata;
5176         void __iomem *to_slim;
5177         int processing_queue = 0;
5178
5179         spin_lock_irqsave(&phba->hbalock, drvr_flag);
5180         if (!pmbox) {
5181                 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5182                 /* processing mbox queue from intr_handler */
5183                 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
5184                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
5185                         return MBX_SUCCESS;
5186                 }
5187                 processing_queue = 1;
5188                 pmbox = lpfc_mbox_get(phba);
5189                 if (!pmbox) {
5190                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
5191                         return MBX_SUCCESS;
5192                 }
5193         }
5194
5195         if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
5196                 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
5197                 if(!pmbox->vport) {
5198                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
5199                         lpfc_printf_log(phba, KERN_ERR,
5200                                         LOG_MBOX | LOG_VPORT,
5201                                         "1806 Mbox x%x failed. No vport\n",
5202                                         pmbox->u.mb.mbxCommand);
5203                         dump_stack();
5204                         goto out_not_finished;
5205                 }
5206         }
5207
5208         /* If the PCI channel is in offline state, do not post mbox. */
5209         if (unlikely(pci_channel_offline(phba->pcidev))) {
5210                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
5211                 goto out_not_finished;
5212         }
5213
5214         /* If HBA has a deferred error attention, fail the iocb. */
5215         if (unlikely(phba->hba_flag & DEFER_ERATT)) {
5216                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
5217                 goto out_not_finished;
5218         }
5219
5220         psli = &phba->sli;
5221
5222         mb = &pmbox->u.mb;
5223         status = MBX_SUCCESS;
5224
5225         if (phba->link_state == LPFC_HBA_ERROR) {
5226                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
5227
5228                 /* Mbox command <mbxCommand> cannot issue */
5229                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5230                                 "(%d):0311 Mailbox command x%x cannot "
5231                                 "issue Data: x%x x%x\n",
5232                                 pmbox->vport ? pmbox->vport->vpi : 0,
5233                                 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
5234                 goto out_not_finished;
5235         }
5236
5237         if (mb->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
5238                 if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
5239                         !(hc_copy & HC_MBINT_ENA)) {
5240                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
5241                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5242                                 "(%d):2528 Mailbox command x%x cannot "
5243                                 "issue Data: x%x x%x\n",
5244                                 pmbox->vport ? pmbox->vport->vpi : 0,
5245                                 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
5246                         goto out_not_finished;
5247                 }
5248         }
5249
5250         if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
5251                 /* Polling for a mbox command when another one is already active
5252                  * is not allowed in SLI. Also, the driver must have established
5253                  * SLI2 mode to queue and process multiple mbox commands.
5254                  */
5255
5256                 if (flag & MBX_POLL) {
5257                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
5258
5259                         /* Mbox command <mbxCommand> cannot issue */
5260                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5261                                         "(%d):2529 Mailbox command x%x "
5262                                         "cannot issue Data: x%x x%x\n",
5263                                         pmbox->vport ? pmbox->vport->vpi : 0,
5264                                         pmbox->u.mb.mbxCommand,
5265                                         psli->sli_flag, flag);
5266                         goto out_not_finished;
5267                 }
5268
5269                 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
5270                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
5271                         /* Mbox command <mbxCommand> cannot issue */
5272                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5273                                         "(%d):2530 Mailbox command x%x "
5274                                         "cannot issue Data: x%x x%x\n",
5275                                         pmbox->vport ? pmbox->vport->vpi : 0,
5276                                         pmbox->u.mb.mbxCommand,
5277                                         psli->sli_flag, flag);
5278                         goto out_not_finished;
5279                 }
5280
5281                 /* Another mailbox command is still being processed, queue this
5282                  * command to be processed later.
5283                  */
5284                 lpfc_mbox_put(phba, pmbox);
5285
5286                 /* Mbox cmd issue - BUSY */
5287                 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5288                                 "(%d):0308 Mbox cmd issue - BUSY Data: "
5289                                 "x%x x%x x%x x%x\n",
5290                                 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
5291                                 mb->mbxCommand, phba->pport->port_state,
5292                                 psli->sli_flag, flag);
5293
5294                 psli->slistat.mbox_busy++;
5295                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
5296
5297                 if (pmbox->vport) {
5298                         lpfc_debugfs_disc_trc(pmbox->vport,
5299                                 LPFC_DISC_TRC_MBOX_VPORT,
5300                                 "MBOX Bsy vport:  cmd:x%x mb:x%x x%x",
5301                                 (uint32_t)mb->mbxCommand,
5302                                 mb->un.varWords[0], mb->un.varWords[1]);
5303                 }
5304                 else {
5305                         lpfc_debugfs_disc_trc(phba->pport,
5306                                 LPFC_DISC_TRC_MBOX,
5307                                 "MBOX Bsy:        cmd:x%x mb:x%x x%x",
5308                                 (uint32_t)mb->mbxCommand,
5309                                 mb->un.varWords[0], mb->un.varWords[1]);
5310                 }
5311
5312                 return MBX_BUSY;
5313         }
5314
5315         psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
5316
5317         /* If we are not polling, we MUST be in SLI2 mode */
5318         if (flag != MBX_POLL) {
5319                 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
5320                     (mb->mbxCommand != MBX_KILL_BOARD)) {
5321                         psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5322                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
5323                         /* Mbox command <mbxCommand> cannot issue */
5324                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5325                                         "(%d):2531 Mailbox command x%x "
5326                                         "cannot issue Data: x%x x%x\n",
5327                                         pmbox->vport ? pmbox->vport->vpi : 0,
5328                                         pmbox->u.mb.mbxCommand,
5329                                         psli->sli_flag, flag);
5330                         goto out_not_finished;
5331                 }
5332                 /* timeout active mbox command */
5333                 mod_timer(&psli->mbox_tmo, (jiffies +
5334                                (HZ * lpfc_mbox_tmo_val(phba, mb->mbxCommand))));
5335         }
5336
5337         /* Mailbox cmd <cmd> issue */
5338         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5339                         "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
5340                         "x%x\n",
5341                         pmbox->vport ? pmbox->vport->vpi : 0,
5342                         mb->mbxCommand, phba->pport->port_state,
5343                         psli->sli_flag, flag);
5344
5345         if (mb->mbxCommand != MBX_HEARTBEAT) {
5346                 if (pmbox->vport) {
5347                         lpfc_debugfs_disc_trc(pmbox->vport,
5348                                 LPFC_DISC_TRC_MBOX_VPORT,
5349                                 "MBOX Send vport: cmd:x%x mb:x%x x%x",
5350                                 (uint32_t)mb->mbxCommand,
5351                                 mb->un.varWords[0], mb->un.varWords[1]);
5352                 }
5353                 else {
5354                         lpfc_debugfs_disc_trc(phba->pport,
5355                                 LPFC_DISC_TRC_MBOX,
5356                                 "MBOX Send:       cmd:x%x mb:x%x x%x",
5357                                 (uint32_t)mb->mbxCommand,
5358                                 mb->un.varWords[0], mb->un.varWords[1]);
5359                 }
5360         }
5361
5362         psli->slistat.mbox_cmd++;
5363         evtctr = psli->slistat.mbox_event;
5364
5365         /* next set own bit for the adapter and copy over command word */
5366         mb->mbxOwner = OWN_CHIP;
5367
5368         if (psli->sli_flag & LPFC_SLI_ACTIVE) {
5369                 /* Populate mbox extension offset word. */
5370                 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
5371                         *(((uint32_t *)mb) + pmbox->mbox_offset_word)
5372                                 = (uint8_t *)phba->mbox_ext
5373                                   - (uint8_t *)phba->mbox;
5374                 }
5375
5376                 /* Copy the mailbox extension data */
5377                 if (pmbox->in_ext_byte_len && pmbox->context2) {
5378                         lpfc_sli_pcimem_bcopy(pmbox->context2,
5379                                 (uint8_t *)phba->mbox_ext,
5380                                 pmbox->in_ext_byte_len);
5381                 }
5382                 /* Copy command data to host SLIM area */
5383                 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
5384         } else {
5385                 /* Populate mbox extension offset word. */
5386                 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
5387                         *(((uint32_t *)mb) + pmbox->mbox_offset_word)
5388                                 = MAILBOX_HBA_EXT_OFFSET;
5389
5390                 /* Copy the mailbox extension data */
5391                 if (pmbox->in_ext_byte_len && pmbox->context2) {
5392                         lpfc_memcpy_to_slim(phba->MBslimaddr +
5393                                 MAILBOX_HBA_EXT_OFFSET,
5394                                 pmbox->context2, pmbox->in_ext_byte_len);
5395
5396                 }
5397                 if (mb->mbxCommand == MBX_CONFIG_PORT) {
5398                         /* copy command data into host mbox for cmpl */
5399                         lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
5400                 }
5401
5402                 /* First copy mbox command data to HBA SLIM, skip past first
5403                    word */
5404                 to_slim = phba->MBslimaddr + sizeof (uint32_t);
5405                 lpfc_memcpy_to_slim(to_slim, &mb->un.varWords[0],
5406                             MAILBOX_CMD_SIZE - sizeof (uint32_t));
5407
5408                 /* Next copy over first word, with mbxOwner set */
5409                 ldata = *((uint32_t *)mb);
5410                 to_slim = phba->MBslimaddr;
5411                 writel(ldata, to_slim);
5412                 readl(to_slim); /* flush */
5413
5414                 if (mb->mbxCommand == MBX_CONFIG_PORT) {
5415                         /* switch over to host mailbox */
5416                         psli->sli_flag |= LPFC_SLI_ACTIVE;
5417                 }
5418         }
5419
5420         wmb();
5421
5422         switch (flag) {
5423         case MBX_NOWAIT:
5424                 /* Set up reference to mailbox command */
5425                 psli->mbox_active = pmbox;
5426                 /* Interrupt board to do it */
5427                 writel(CA_MBATT, phba->CAregaddr);
5428                 readl(phba->CAregaddr); /* flush */
5429                 /* Don't wait for it to finish, just return */
5430                 break;
5431
5432         case MBX_POLL:
5433                 /* Set up null reference to mailbox command */
5434                 psli->mbox_active = NULL;
5435                 /* Interrupt board to do it */
5436                 writel(CA_MBATT, phba->CAregaddr);
5437                 readl(phba->CAregaddr); /* flush */
5438
5439                 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
5440                         /* First read mbox status word */
5441                         word0 = *((uint32_t *)phba->mbox);
5442                         word0 = le32_to_cpu(word0);
5443                 } else {
5444                         /* First read mbox status word */
5445                         if (lpfc_readl(phba->MBslimaddr, &word0)) {
5446                                 spin_unlock_irqrestore(&phba->hbalock,
5447                                                        drvr_flag);
5448                                 goto out_not_finished;
5449                         }
5450                 }
5451
5452                 /* Read the HBA Host Attention Register */
5453                 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
5454                         spin_unlock_irqrestore(&phba->hbalock,
5455                                                        drvr_flag);
5456                         goto out_not_finished;
5457                 }
5458                 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
5459                                                              mb->mbxCommand) *
5460                                            1000) + jiffies;
5461                 i = 0;
5462                 /* Wait for command to complete */
5463                 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
5464                        (!(ha_copy & HA_MBATT) &&
5465                         (phba->link_state > LPFC_WARM_START))) {
5466                         if (time_after(jiffies, timeout)) {
5467                                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5468                                 spin_unlock_irqrestore(&phba->hbalock,
5469                                                        drvr_flag);
5470                                 goto out_not_finished;
5471                         }
5472
5473                         /* Check if we took a mbox interrupt while we were
5474                            polling */
5475                         if (((word0 & OWN_CHIP) != OWN_CHIP)
5476                             && (evtctr != psli->slistat.mbox_event))
5477                                 break;
5478
5479                         if (i++ > 10) {
5480                                 spin_unlock_irqrestore(&phba->hbalock,
5481                                                        drvr_flag);
5482                                 msleep(1);
5483                                 spin_lock_irqsave(&phba->hbalock, drvr_flag);
5484                         }
5485
5486                         if (psli->sli_flag & LPFC_SLI_ACTIVE) {
5487                                 /* First copy command data */
5488                                 word0 = *((uint32_t *)phba->mbox);
5489                                 word0 = le32_to_cpu(word0);
5490                                 if (mb->mbxCommand == MBX_CONFIG_PORT) {
5491                                         MAILBOX_t *slimmb;
5492                                         uint32_t slimword0;
5493                                         /* Check real SLIM for any errors */
5494                                         slimword0 = readl(phba->MBslimaddr);
5495                                         slimmb = (MAILBOX_t *) & slimword0;
5496                                         if (((slimword0 & OWN_CHIP) != OWN_CHIP)
5497                                             && slimmb->mbxStatus) {
5498                                                 psli->sli_flag &=
5499                                                     ~LPFC_SLI_ACTIVE;
5500                                                 word0 = slimword0;
5501                                         }
5502                                 }
5503                         } else {
5504                                 /* First copy command data */
5505                                 word0 = readl(phba->MBslimaddr);
5506                         }
5507                         /* Read the HBA Host Attention Register */
5508                         if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
5509                                 spin_unlock_irqrestore(&phba->hbalock,
5510                                                        drvr_flag);
5511                                 goto out_not_finished;
5512                         }
5513                 }
5514
5515                 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
5516                         /* copy results back to user */
5517                         lpfc_sli_pcimem_bcopy(phba->mbox, mb, MAILBOX_CMD_SIZE);
5518                         /* Copy the mailbox extension data */
5519                         if (pmbox->out_ext_byte_len && pmbox->context2) {
5520                                 lpfc_sli_pcimem_bcopy(phba->mbox_ext,
5521                                                       pmbox->context2,
5522                                                       pmbox->out_ext_byte_len);
5523                         }
5524                 } else {
5525                         /* First copy command data */
5526                         lpfc_memcpy_from_slim(mb, phba->MBslimaddr,
5527                                                         MAILBOX_CMD_SIZE);
5528                         /* Copy the mailbox extension data */
5529                         if (pmbox->out_ext_byte_len && pmbox->context2) {
5530                                 lpfc_memcpy_from_slim(pmbox->context2,
5531                                         phba->MBslimaddr +
5532                                         MAILBOX_HBA_EXT_OFFSET,
5533                                         pmbox->out_ext_byte_len);
5534                         }
5535                 }
5536
5537                 writel(HA_MBATT, phba->HAregaddr);
5538                 readl(phba->HAregaddr); /* flush */
5539
5540                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5541                 status = mb->mbxStatus;
5542         }
5543
5544         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
5545         return status;
5546
5547 out_not_finished:
5548         if (processing_queue) {
5549                 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
5550                 lpfc_mbox_cmpl_put(phba, pmbox);
5551         }
5552         return MBX_NOT_FINISHED;
5553 }
5554
5555 /**
5556  * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
5557  * @phba: Pointer to HBA context object.
5558  *
5559  * The function blocks the posting of SLI4 asynchronous mailbox commands from
5560  * the driver internal pending mailbox queue. It will then try to wait out the
5561  * possible outstanding mailbox command before return.
5562  *
5563  * Returns:
5564  *      0 - the outstanding mailbox command completed; otherwise, the wait for
5565  *      the outstanding mailbox command timed out.
5566  **/
5567 static int
5568 lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
5569 {
5570         struct lpfc_sli *psli = &phba->sli;
5571         uint8_t actcmd = MBX_HEARTBEAT;
5572         int rc = 0;
5573         unsigned long timeout;
5574
5575         /* Mark the asynchronous mailbox command posting as blocked */
5576         spin_lock_irq(&phba->hbalock);
5577         psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
5578         if (phba->sli.mbox_active)
5579                 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
5580         spin_unlock_irq(&phba->hbalock);
5581         /* Determine how long we might wait for the active mailbox
5582          * command to be gracefully completed by firmware.
5583          */
5584         timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, actcmd) * 1000) +
5585                                    jiffies;
5586         /* Wait for the outstnading mailbox command to complete */
5587         while (phba->sli.mbox_active) {
5588                 /* Check active mailbox complete status every 2ms */
5589                 msleep(2);
5590                 if (time_after(jiffies, timeout)) {
5591                         /* Timeout, marked the outstanding cmd not complete */
5592                         rc = 1;
5593                         break;
5594                 }
5595         }
5596
5597         /* Can not cleanly block async mailbox command, fails it */
5598         if (rc) {
5599                 spin_lock_irq(&phba->hbalock);
5600                 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
5601                 spin_unlock_irq(&phba->hbalock);
5602         }
5603         return rc;
5604 }
5605
5606 /**
5607  * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
5608  * @phba: Pointer to HBA context object.
5609  *
5610  * The function unblocks and resume posting of SLI4 asynchronous mailbox
5611  * commands from the driver internal pending mailbox queue. It makes sure
5612  * that there is no outstanding mailbox command before resuming posting
5613  * asynchronous mailbox commands. If, for any reason, there is outstanding
5614  * mailbox command, it will try to wait it out before resuming asynchronous
5615  * mailbox command posting.
5616  **/
5617 static void
5618 lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
5619 {
5620         struct lpfc_sli *psli = &phba->sli;
5621
5622         spin_lock_irq(&phba->hbalock);
5623         if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
5624                 /* Asynchronous mailbox posting is not blocked, do nothing */
5625                 spin_unlock_irq(&phba->hbalock);
5626                 return;
5627         }
5628
5629         /* Outstanding synchronous mailbox command is guaranteed to be done,
5630          * successful or timeout, after timing-out the outstanding mailbox
5631          * command shall always be removed, so just unblock posting async
5632          * mailbox command and resume
5633          */
5634         psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
5635         spin_unlock_irq(&phba->hbalock);
5636
5637         /* wake up worker thread to post asynchronlous mailbox command */
5638         lpfc_worker_wake_up(phba);
5639 }
5640
5641 /**
5642  * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
5643  * @phba: Pointer to HBA context object.
5644  * @mboxq: Pointer to mailbox object.
5645  *
5646  * The function posts a mailbox to the port.  The mailbox is expected
5647  * to be comletely filled in and ready for the port to operate on it.
5648  * This routine executes a synchronous completion operation on the
5649  * mailbox by polling for its completion.
5650  *
5651  * The caller must not be holding any locks when calling this routine.
5652  *
5653  * Returns:
5654  *      MBX_SUCCESS - mailbox posted successfully
5655  *      Any of the MBX error values.
5656  **/
5657 static int
5658 lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
5659 {
5660         int rc = MBX_SUCCESS;
5661         unsigned long iflag;
5662         uint32_t db_ready;
5663         uint32_t mcqe_status;
5664         uint32_t mbx_cmnd;
5665         unsigned long timeout;
5666         struct lpfc_sli *psli = &phba->sli;
5667         struct lpfc_mqe *mb = &mboxq->u.mqe;
5668         struct lpfc_bmbx_create *mbox_rgn;
5669         struct dma_address *dma_address;
5670         struct lpfc_register bmbx_reg;
5671
5672         /*
5673          * Only one mailbox can be active to the bootstrap mailbox region
5674          * at a time and there is no queueing provided.
5675          */
5676         spin_lock_irqsave(&phba->hbalock, iflag);
5677         if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
5678                 spin_unlock_irqrestore(&phba->hbalock, iflag);
5679                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5680                                 "(%d):2532 Mailbox command x%x (x%x) "
5681                                 "cannot issue Data: x%x x%x\n",
5682                                 mboxq->vport ? mboxq->vport->vpi : 0,
5683                                 mboxq->u.mb.mbxCommand,
5684                                 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5685                                 psli->sli_flag, MBX_POLL);
5686                 return MBXERR_ERROR;
5687         }
5688         /* The server grabs the token and owns it until release */
5689         psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
5690         phba->sli.mbox_active = mboxq;
5691         spin_unlock_irqrestore(&phba->hbalock, iflag);
5692
5693         /*
5694          * Initialize the bootstrap memory region to avoid stale data areas
5695          * in the mailbox post.  Then copy the caller's mailbox contents to
5696          * the bmbx mailbox region.
5697          */
5698         mbx_cmnd = bf_get(lpfc_mqe_command, mb);
5699         memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
5700         lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
5701                               sizeof(struct lpfc_mqe));
5702
5703         /* Post the high mailbox dma address to the port and wait for ready. */
5704         dma_address = &phba->sli4_hba.bmbx.dma_address;
5705         writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
5706
5707         timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mbx_cmnd)
5708                                    * 1000) + jiffies;
5709         do {
5710                 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
5711                 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
5712                 if (!db_ready)
5713                         msleep(2);
5714
5715                 if (time_after(jiffies, timeout)) {
5716                         rc = MBXERR_ERROR;
5717                         goto exit;
5718                 }
5719         } while (!db_ready);
5720
5721         /* Post the low mailbox dma address to the port. */
5722         writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
5723         timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mbx_cmnd)
5724                                    * 1000) + jiffies;
5725         do {
5726                 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
5727                 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
5728                 if (!db_ready)
5729                         msleep(2);
5730
5731                 if (time_after(jiffies, timeout)) {
5732                         rc = MBXERR_ERROR;
5733                         goto exit;
5734                 }
5735         } while (!db_ready);
5736
5737         /*
5738          * Read the CQ to ensure the mailbox has completed.
5739          * If so, update the mailbox status so that the upper layers
5740          * can complete the request normally.
5741          */
5742         lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
5743                               sizeof(struct lpfc_mqe));
5744         mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
5745         lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
5746                               sizeof(struct lpfc_mcqe));
5747         mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
5748         /*
5749          * When the CQE status indicates a failure and the mailbox status
5750          * indicates success then copy the CQE status into the mailbox status
5751          * (and prefix it with x4000).
5752          */
5753         if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
5754                 if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
5755                         bf_set(lpfc_mqe_status, mb,
5756                                (LPFC_MBX_ERROR_RANGE | mcqe_status));
5757                 rc = MBXERR_ERROR;
5758         } else
5759                 lpfc_sli4_swap_str(phba, mboxq);
5760
5761         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5762                         "(%d):0356 Mailbox cmd x%x (x%x) Status x%x "
5763                         "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
5764                         " x%x x%x CQ: x%x x%x x%x x%x\n",
5765                         mboxq->vport ? mboxq->vport->vpi : 0,
5766                         mbx_cmnd, lpfc_sli4_mbox_opcode_get(phba, mboxq),
5767                         bf_get(lpfc_mqe_status, mb),
5768                         mb->un.mb_words[0], mb->un.mb_words[1],
5769                         mb->un.mb_words[2], mb->un.mb_words[3],
5770                         mb->un.mb_words[4], mb->un.mb_words[5],
5771                         mb->un.mb_words[6], mb->un.mb_words[7],
5772                         mb->un.mb_words[8], mb->un.mb_words[9],
5773                         mb->un.mb_words[10], mb->un.mb_words[11],
5774                         mb->un.mb_words[12], mboxq->mcqe.word0,
5775                         mboxq->mcqe.mcqe_tag0,  mboxq->mcqe.mcqe_tag1,
5776                         mboxq->mcqe.trailer);
5777 exit:
5778         /* We are holding the token, no needed for lock when release */
5779         spin_lock_irqsave(&phba->hbalock, iflag);
5780         psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5781         phba->sli.mbox_active = NULL;
5782         spin_unlock_irqrestore(&phba->hbalock, iflag);
5783         return rc;
5784 }
5785
5786 /**
5787  * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
5788  * @phba: Pointer to HBA context object.
5789  * @pmbox: Pointer to mailbox object.
5790  * @flag: Flag indicating how the mailbox need to be processed.
5791  *
5792  * This function is called by discovery code and HBA management code to submit
5793  * a mailbox command to firmware with SLI-4 interface spec.
5794  *
5795  * Return codes the caller owns the mailbox command after the return of the
5796  * function.
5797  **/
5798 static int
5799 lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
5800                        uint32_t flag)
5801 {
5802         struct lpfc_sli *psli = &phba->sli;
5803         unsigned long iflags;
5804         int rc;
5805
5806         rc = lpfc_mbox_dev_check(phba);
5807         if (unlikely(rc)) {
5808                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5809                                 "(%d):2544 Mailbox command x%x (x%x) "
5810                                 "cannot issue Data: x%x x%x\n",
5811                                 mboxq->vport ? mboxq->vport->vpi : 0,
5812                                 mboxq->u.mb.mbxCommand,
5813                                 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5814                                 psli->sli_flag, flag);
5815                 goto out_not_finished;
5816         }
5817
5818         /* Detect polling mode and jump to a handler */
5819         if (!phba->sli4_hba.intr_enable) {
5820                 if (flag == MBX_POLL)
5821                         rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
5822                 else
5823                         rc = -EIO;
5824                 if (rc != MBX_SUCCESS)
5825                         lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
5826                                         "(%d):2541 Mailbox command x%x "
5827                                         "(x%x) cannot issue Data: x%x x%x\n",
5828                                         mboxq->vport ? mboxq->vport->vpi : 0,
5829                                         mboxq->u.mb.mbxCommand,
5830                                         lpfc_sli4_mbox_opcode_get(phba, mboxq),
5831                                         psli->sli_flag, flag);
5832                 return rc;
5833         } else if (flag == MBX_POLL) {
5834                 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
5835                                 "(%d):2542 Try to issue mailbox command "
5836                                 "x%x (x%x) synchronously ahead of async"
5837                                 "mailbox command queue: x%x x%x\n",
5838                                 mboxq->vport ? mboxq->vport->vpi : 0,
5839                                 mboxq->u.mb.mbxCommand,
5840                                 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5841                                 psli->sli_flag, flag);
5842                 /* Try to block the asynchronous mailbox posting */
5843                 rc = lpfc_sli4_async_mbox_block(phba);
5844                 if (!rc) {
5845                         /* Successfully blocked, now issue sync mbox cmd */
5846                         rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
5847                         if (rc != MBX_SUCCESS)
5848                                 lpfc_printf_log(phba, KERN_ERR,
5849                                                 LOG_MBOX | LOG_SLI,
5850                                                 "(%d):2597 Mailbox command "
5851                                                 "x%x (x%x) cannot issue "
5852                                                 "Data: x%x x%x\n",
5853                                                 mboxq->vport ?
5854                                                 mboxq->vport->vpi : 0,
5855                                                 mboxq->u.mb.mbxCommand,
5856                                                 lpfc_sli4_mbox_opcode_get(phba,
5857                                                                 mboxq),
5858                                                 psli->sli_flag, flag);
5859                         /* Unblock the async mailbox posting afterward */
5860                         lpfc_sli4_async_mbox_unblock(phba);
5861                 }
5862                 return rc;
5863         }
5864
5865         /* Now, interrupt mode asynchrous mailbox command */
5866         rc = lpfc_mbox_cmd_check(phba, mboxq);
5867         if (rc) {
5868                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5869                                 "(%d):2543 Mailbox command x%x (x%x) "
5870                                 "cannot issue Data: x%x x%x\n",
5871                                 mboxq->vport ? mboxq->vport->vpi : 0,
5872                                 mboxq->u.mb.mbxCommand,
5873                                 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5874                                 psli->sli_flag, flag);
5875                 goto out_not_finished;
5876         }
5877
5878         /* Put the mailbox command to the driver internal FIFO */
5879         psli->slistat.mbox_busy++;
5880         spin_lock_irqsave(&phba->hbalock, iflags);
5881         lpfc_mbox_put(phba, mboxq);
5882         spin_unlock_irqrestore(&phba->hbalock, iflags);
5883         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5884                         "(%d):0354 Mbox cmd issue - Enqueue Data: "
5885                         "x%x (x%x) x%x x%x x%x\n",
5886                         mboxq->vport ? mboxq->vport->vpi : 0xffffff,
5887                         bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5888                         lpfc_sli4_mbox_opcode_get(phba, mboxq),
5889                         phba->pport->port_state,
5890                         psli->sli_flag, MBX_NOWAIT);
5891         /* Wake up worker thread to transport mailbox command from head */
5892         lpfc_worker_wake_up(phba);
5893
5894         return MBX_BUSY;
5895
5896 out_not_finished:
5897         return MBX_NOT_FINISHED;
5898 }
5899
5900 /**
5901  * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
5902  * @phba: Pointer to HBA context object.
5903  *
5904  * This function is called by worker thread to send a mailbox command to
5905  * SLI4 HBA firmware.
5906  *
5907  **/
5908 int
5909 lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
5910 {
5911         struct lpfc_sli *psli = &phba->sli;
5912         LPFC_MBOXQ_t *mboxq;
5913         int rc = MBX_SUCCESS;
5914         unsigned long iflags;
5915         struct lpfc_mqe *mqe;
5916         uint32_t mbx_cmnd;
5917
5918         /* Check interrupt mode before post async mailbox command */
5919         if (unlikely(!phba->sli4_hba.intr_enable))
5920                 return MBX_NOT_FINISHED;
5921
5922         /* Check for mailbox command service token */
5923         spin_lock_irqsave(&phba->hbalock, iflags);
5924         if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
5925                 spin_unlock_irqrestore(&phba->hbalock, iflags);
5926                 return MBX_NOT_FINISHED;
5927         }
5928         if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
5929                 spin_unlock_irqrestore(&phba->hbalock, iflags);
5930                 return MBX_NOT_FINISHED;
5931         }
5932         if (unlikely(phba->sli.mbox_active)) {
5933                 spin_unlock_irqrestore(&phba->hbalock, iflags);
5934                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5935                                 "0384 There is pending active mailbox cmd\n");
5936                 return MBX_NOT_FINISHED;
5937         }
5938         /* Take the mailbox command service token */
5939         psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
5940
5941         /* Get the next mailbox command from head of queue */
5942         mboxq = lpfc_mbox_get(phba);
5943
5944         /* If no more mailbox command waiting for post, we're done */
5945         if (!mboxq) {
5946                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5947                 spin_unlock_irqrestore(&phba->hbalock, iflags);
5948                 return MBX_SUCCESS;
5949         }
5950         phba->sli.mbox_active = mboxq;
5951         spin_unlock_irqrestore(&phba->hbalock, iflags);
5952
5953         /* Check device readiness for posting mailbox command */
5954         rc = lpfc_mbox_dev_check(phba);
5955         if (unlikely(rc))
5956                 /* Driver clean routine will clean up pending mailbox */
5957                 goto out_not_finished;
5958
5959         /* Prepare the mbox command to be posted */
5960         mqe = &mboxq->u.mqe;
5961         mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
5962
5963         /* Start timer for the mbox_tmo and log some mailbox post messages */
5964         mod_timer(&psli->mbox_tmo, (jiffies +
5965                   (HZ * lpfc_mbox_tmo_val(phba, mbx_cmnd))));
5966
5967         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5968                         "(%d):0355 Mailbox cmd x%x (x%x) issue Data: "
5969                         "x%x x%x\n",
5970                         mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
5971                         lpfc_sli4_mbox_opcode_get(phba, mboxq),
5972                         phba->pport->port_state, psli->sli_flag);
5973
5974         if (mbx_cmnd != MBX_HEARTBEAT) {
5975                 if (mboxq->vport) {
5976                         lpfc_debugfs_disc_trc(mboxq->vport,
5977                                 LPFC_DISC_TRC_MBOX_VPORT,
5978                                 "MBOX Send vport: cmd:x%x mb:x%x x%x",
5979                                 mbx_cmnd, mqe->un.mb_words[0],
5980                                 mqe->un.mb_words[1]);
5981                 } else {
5982                         lpfc_debugfs_disc_trc(phba->pport,
5983                                 LPFC_DISC_TRC_MBOX,
5984                                 "MBOX Send: cmd:x%x mb:x%x x%x",
5985                                 mbx_cmnd, mqe->un.mb_words[0],
5986                                 mqe->un.mb_words[1]);
5987                 }
5988         }
5989         psli->slistat.mbox_cmd++;
5990
5991         /* Post the mailbox command to the port */
5992         rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
5993         if (rc != MBX_SUCCESS) {
5994                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5995                                 "(%d):2533 Mailbox command x%x (x%x) "
5996                                 "cannot issue Data: x%x x%x\n",
5997                                 mboxq->vport ? mboxq->vport->vpi : 0,
5998                                 mboxq->u.mb.mbxCommand,
5999                                 lpfc_sli4_mbox_opcode_get(phba, mboxq),
6000                                 psli->sli_flag, MBX_NOWAIT);
6001                 goto out_not_finished;
6002         }
6003
6004         return rc;
6005
6006 out_not_finished:
6007         spin_lock_irqsave(&phba->hbalock, iflags);
6008         mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
6009         __lpfc_mbox_cmpl_put(phba, mboxq);
6010         /* Release the token */
6011         psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
6012         phba->sli.mbox_active = NULL;
6013         spin_unlock_irqrestore(&phba->hbalock, iflags);
6014
6015         return MBX_NOT_FINISHED;
6016 }
6017
6018 /**
6019  * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
6020  * @phba: Pointer to HBA context object.
6021  * @pmbox: Pointer to mailbox object.
6022  * @flag: Flag indicating how the mailbox need to be processed.
6023  *
6024  * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
6025  * the API jump table function pointer from the lpfc_hba struct.
6026  *
6027  * Return codes the caller owns the mailbox command after the return of the
6028  * function.
6029  **/
6030 int
6031 lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
6032 {
6033         return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
6034 }
6035
6036 /**
6037  * lpfc_mbox_api_table_setup - Set up mbox api function jump table
6038  * @phba: The hba struct for which this call is being executed.
6039  * @dev_grp: The HBA PCI-Device group number.
6040  *
6041  * This routine sets up the mbox interface API function jump table in @phba
6042  * struct.
6043  * Returns: 0 - success, -ENODEV - failure.
6044  **/
6045 int
6046 lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
6047 {
6048
6049         switch (dev_grp) {
6050         case LPFC_PCI_DEV_LP:
6051                 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
6052                 phba->lpfc_sli_handle_slow_ring_event =
6053                                 lpfc_sli_handle_slow_ring_event_s3;
6054                 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
6055                 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
6056                 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
6057                 break;
6058         case LPFC_PCI_DEV_OC:
6059                 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
6060                 phba->lpfc_sli_handle_slow_ring_event =
6061                                 lpfc_sli_handle_slow_ring_event_s4;
6062                 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
6063                 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
6064                 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
6065                 break;
6066         default:
6067                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6068                                 "1420 Invalid HBA PCI-device group: 0x%x\n",
6069                                 dev_grp);
6070                 return -ENODEV;
6071                 break;
6072         }
6073         return 0;
6074 }
6075
6076 /**
6077  * __lpfc_sli_ringtx_put - Add an iocb to the txq
6078  * @phba: Pointer to HBA context object.
6079  * @pring: Pointer to driver SLI ring object.
6080  * @piocb: Pointer to address of newly added command iocb.
6081  *
6082  * This function is called with hbalock held to add a command
6083  * iocb to the txq when SLI layer cannot submit the command iocb
6084  * to the ring.
6085  **/
6086 void
6087 __lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
6088                     struct lpfc_iocbq *piocb)
6089 {
6090         /* Insert the caller's iocb in the txq tail for later processing. */
6091         list_add_tail(&piocb->list, &pring->txq);
6092         pring->txq_cnt++;
6093 }
6094
6095 /**
6096  * lpfc_sli_next_iocb - Get the next iocb in the txq
6097  * @phba: Pointer to HBA context object.
6098  * @pring: Pointer to driver SLI ring object.
6099  * @piocb: Pointer to address of newly added command iocb.
6100  *
6101  * This function is called with hbalock held before a new
6102  * iocb is submitted to the firmware. This function checks
6103  * txq to flush the iocbs in txq to Firmware before
6104  * submitting new iocbs to the Firmware.
6105  * If there are iocbs in the txq which need to be submitted
6106  * to firmware, lpfc_sli_next_iocb returns the first element
6107  * of the txq after dequeuing it from txq.
6108  * If there is no iocb in the txq then the function will return
6109  * *piocb and *piocb is set to NULL. Caller needs to check
6110  * *piocb to find if there are more commands in the txq.
6111  **/
6112 static struct lpfc_iocbq *
6113 lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
6114                    struct lpfc_iocbq **piocb)
6115 {
6116         struct lpfc_iocbq * nextiocb;
6117
6118         nextiocb = lpfc_sli_ringtx_get(phba, pring);
6119         if (!nextiocb) {
6120                 nextiocb = *piocb;
6121                 *piocb = NULL;
6122         }
6123
6124         return nextiocb;
6125 }
6126
6127 /**
6128  * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
6129  * @phba: Pointer to HBA context object.
6130  * @ring_number: SLI ring number to issue iocb on.
6131  * @piocb: Pointer to command iocb.
6132  * @flag: Flag indicating if this command can be put into txq.
6133  *
6134  * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
6135  * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
6136  * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
6137  * flag is turned on, the function returns IOCB_ERROR. When the link is down,
6138  * this function allows only iocbs for posting buffers. This function finds
6139  * next available slot in the command ring and posts the command to the
6140  * available slot and writes the port attention register to request HBA start
6141  * processing new iocb. If there is no slot available in the ring and
6142  * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
6143  * the function returns IOCB_BUSY.
6144  *
6145  * This function is called with hbalock held. The function will return success
6146  * after it successfully submit the iocb to firmware or after adding to the
6147  * txq.
6148  **/
6149 static int
6150 __lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
6151                     struct lpfc_iocbq *piocb, uint32_t flag)
6152 {
6153         struct lpfc_iocbq *nextiocb;
6154         IOCB_t *iocb;
6155         struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
6156
6157         if (piocb->iocb_cmpl && (!piocb->vport) &&
6158            (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
6159            (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
6160                 lpfc_printf_log(phba, KERN_ERR,
6161                                 LOG_SLI | LOG_VPORT,
6162                                 "1807 IOCB x%x failed. No vport\n",
6163                                 piocb->iocb.ulpCommand);
6164                 dump_stack();
6165                 return IOCB_ERROR;
6166         }
6167
6168
6169         /* If the PCI channel is in offline state, do not post iocbs. */
6170         if (unlikely(pci_channel_offline(phba->pcidev)))
6171                 return IOCB_ERROR;
6172
6173         /* If HBA has a deferred error attention, fail the iocb. */
6174         if (unlikely(phba->hba_flag & DEFER_ERATT))
6175                 return IOCB_ERROR;
6176
6177         /*
6178          * We should never get an IOCB if we are in a < LINK_DOWN state
6179          */
6180         if (unlikely(phba->link_state < LPFC_LINK_DOWN))
6181                 return IOCB_ERROR;
6182
6183         /*
6184          * Check to see if we are blocking IOCB processing because of a
6185          * outstanding event.
6186          */
6187         if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
6188                 goto iocb_busy;
6189
6190         if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
6191                 /*
6192                  * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
6193                  * can be issued if the link is not up.
6194                  */
6195                 switch (piocb->iocb.ulpCommand) {
6196                 case CMD_GEN_REQUEST64_CR:
6197                 case CMD_GEN_REQUEST64_CX:
6198                         if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
6199                                 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
6200                                         FC_RCTL_DD_UNSOL_CMD) ||
6201                                 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
6202                                         MENLO_TRANSPORT_TYPE))
6203
6204                                 goto iocb_busy;
6205                         break;
6206                 case CMD_QUE_RING_BUF_CN:
6207                 case CMD_QUE_RING_BUF64_CN:
6208                         /*
6209                          * For IOCBs, like QUE_RING_BUF, that have no rsp ring
6210                          * completion, iocb_cmpl MUST be 0.
6211                          */
6212                         if (piocb->iocb_cmpl)
6213                                 piocb->iocb_cmpl = NULL;
6214                         /*FALLTHROUGH*/
6215                 case CMD_CREATE_XRI_CR:
6216                 case CMD_CLOSE_XRI_CN:
6217                 case CMD_CLOSE_XRI_CX:
6218                         break;
6219                 default:
6220                         goto iocb_busy;
6221                 }
6222
6223         /*
6224          * For FCP commands, we must be in a state where we can process link
6225          * attention events.
6226          */
6227         } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
6228                             !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
6229                 goto iocb_busy;
6230         }
6231
6232         while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
6233                (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
6234                 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
6235
6236         if (iocb)
6237                 lpfc_sli_update_ring(phba, pring);
6238         else
6239                 lpfc_sli_update_full_ring(phba, pring);
6240
6241         if (!piocb)
6242                 return IOCB_SUCCESS;
6243
6244         goto out_busy;
6245
6246  iocb_busy:
6247         pring->stats.iocb_cmd_delay++;
6248
6249  out_busy:
6250
6251         if (!(flag & SLI_IOCB_RET_IOCB)) {
6252                 __lpfc_sli_ringtx_put(phba, pring, piocb);
6253                 return IOCB_SUCCESS;
6254         }
6255
6256         return IOCB_BUSY;
6257 }
6258
6259 /**
6260  * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
6261  * @phba: Pointer to HBA context object.
6262  * @piocb: Pointer to command iocb.
6263  * @sglq: Pointer to the scatter gather queue object.
6264  *
6265  * This routine converts the bpl or bde that is in the IOCB
6266  * to a sgl list for the sli4 hardware. The physical address
6267  * of the bpl/bde is converted back to a virtual address.
6268  * If the IOCB contains a BPL then the list of BDE's is
6269  * converted to sli4_sge's. If the IOCB contains a single
6270  * BDE then it is converted to a single sli_sge.
6271  * The IOCB is still in cpu endianess so the contents of
6272  * the bpl can be used without byte swapping.
6273  *
6274  * Returns valid XRI = Success, NO_XRI = Failure.
6275 **/
6276 static uint16_t
6277 lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
6278                 struct lpfc_sglq *sglq)
6279 {
6280         uint16_t xritag = NO_XRI;
6281         struct ulp_bde64 *bpl = NULL;
6282         struct ulp_bde64 bde;
6283         struct sli4_sge *sgl  = NULL;
6284         IOCB_t *icmd;
6285         int numBdes = 0;
6286         int i = 0;
6287         uint32_t offset = 0; /* accumulated offset in the sg request list */
6288         int inbound = 0; /* number of sg reply entries inbound from firmware */
6289
6290         if (!piocbq || !sglq)
6291                 return xritag;
6292
6293         sgl  = (struct sli4_sge *)sglq->sgl;
6294         icmd = &piocbq->iocb;
6295         if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
6296                 numBdes = icmd->un.genreq64.bdl.bdeSize /
6297                                 sizeof(struct ulp_bde64);
6298                 /* The addrHigh and addrLow fields within the IOCB
6299                  * have not been byteswapped yet so there is no
6300                  * need to swap them back.
6301                  */
6302                 bpl  = (struct ulp_bde64 *)
6303                         ((struct lpfc_dmabuf *)piocbq->context3)->virt;
6304
6305                 if (!bpl)
6306                         return xritag;
6307
6308                 for (i = 0; i < numBdes; i++) {
6309                         /* Should already be byte swapped. */
6310                         sgl->addr_hi = bpl->addrHigh;
6311                         sgl->addr_lo = bpl->addrLow;
6312
6313                         sgl->word2 = le32_to_cpu(sgl->word2);
6314                         if ((i+1) == numBdes)
6315                                 bf_set(lpfc_sli4_sge_last, sgl, 1);
6316                         else
6317                                 bf_set(lpfc_sli4_sge_last, sgl, 0);
6318                         /* swap the size field back to the cpu so we
6319                          * can assign it to the sgl.
6320                          */
6321                         bde.tus.w = le32_to_cpu(bpl->tus.w);
6322                         sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
6323                         /* The offsets in the sgl need to be accumulated
6324                          * separately for the request and reply lists.
6325                          * The request is always first, the reply follows.
6326                          */
6327                         if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
6328                                 /* add up the reply sg entries */
6329                                 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
6330                                         inbound++;
6331                                 /* first inbound? reset the offset */
6332                                 if (inbound == 1)
6333                                         offset = 0;
6334                                 bf_set(lpfc_sli4_sge_offset, sgl, offset);
6335                                 offset += bde.tus.f.bdeSize;
6336                         }
6337                         sgl->word2 = cpu_to_le32(sgl->word2);
6338                         bpl++;
6339                         sgl++;
6340                 }
6341         } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
6342                         /* The addrHigh and addrLow fields of the BDE have not
6343                          * been byteswapped yet so they need to be swapped
6344                          * before putting them in the sgl.
6345                          */
6346                         sgl->addr_hi =
6347                                 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
6348                         sgl->addr_lo =
6349                                 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
6350                         sgl->word2 = le32_to_cpu(sgl->word2);
6351                         bf_set(lpfc_sli4_sge_last, sgl, 1);
6352                         sgl->word2 = cpu_to_le32(sgl->word2);
6353                         sgl->sge_len =
6354                                 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
6355         }
6356         return sglq->sli4_xritag;
6357 }
6358
6359 /**
6360  * lpfc_sli4_scmd_to_wqidx_distr - scsi command to SLI4 WQ index distribution
6361  * @phba: Pointer to HBA context object.
6362  *
6363  * This routine performs a roundrobin SCSI command to SLI4 FCP WQ index
6364  * distribution.  This is called by __lpfc_sli_issue_iocb_s4() with the hbalock
6365  * held.
6366  *
6367  * Return: index into SLI4 fast-path FCP queue index.
6368  **/
6369 static uint32_t
6370 lpfc_sli4_scmd_to_wqidx_distr(struct lpfc_hba *phba)
6371 {
6372         ++phba->fcp_qidx;
6373         if (phba->fcp_qidx >= phba->cfg_fcp_wq_count)
6374                 phba->fcp_qidx = 0;
6375
6376         return phba->fcp_qidx;
6377 }
6378
6379 /**
6380  * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
6381  * @phba: Pointer to HBA context object.
6382  * @piocb: Pointer to command iocb.
6383  * @wqe: Pointer to the work queue entry.
6384  *
6385  * This routine converts the iocb command to its Work Queue Entry
6386  * equivalent. The wqe pointer should not have any fields set when
6387  * this routine is called because it will memcpy over them.
6388  * This routine does not set the CQ_ID or the WQEC bits in the
6389  * wqe.
6390  *
6391  * Returns: 0 = Success, IOCB_ERROR = Failure.
6392  **/
6393 static int
6394 lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
6395                 union lpfc_wqe *wqe)
6396 {
6397         uint32_t xmit_len = 0, total_len = 0;
6398         uint8_t ct = 0;
6399         uint32_t fip;
6400         uint32_t abort_tag;
6401         uint8_t command_type = ELS_COMMAND_NON_FIP;
6402         uint8_t cmnd;
6403         uint16_t xritag;
6404         uint16_t abrt_iotag;
6405         struct lpfc_iocbq *abrtiocbq;
6406         struct ulp_bde64 *bpl = NULL;
6407         uint32_t els_id = LPFC_ELS_ID_DEFAULT;
6408         int numBdes, i;
6409         struct ulp_bde64 bde;
6410         struct lpfc_nodelist *ndlp;
6411
6412         fip = phba->hba_flag & HBA_FIP_SUPPORT;
6413         /* The fcp commands will set command type */
6414         if (iocbq->iocb_flag &  LPFC_IO_FCP)
6415                 command_type = FCP_COMMAND;
6416         else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
6417                 command_type = ELS_COMMAND_FIP;
6418         else
6419                 command_type = ELS_COMMAND_NON_FIP;
6420
6421         /* Some of the fields are in the right position already */
6422         memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
6423         abort_tag = (uint32_t) iocbq->iotag;
6424         xritag = iocbq->sli4_xritag;
6425         wqe->generic.wqe_com.word7 = 0; /* The ct field has moved so reset */
6426         /* words0-2 bpl convert bde */
6427         if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
6428                 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
6429                                 sizeof(struct ulp_bde64);
6430                 bpl  = (struct ulp_bde64 *)
6431                         ((struct lpfc_dmabuf *)iocbq->context3)->virt;
6432                 if (!bpl)
6433                         return IOCB_ERROR;
6434
6435                 /* Should already be byte swapped. */
6436                 wqe->generic.bde.addrHigh =  le32_to_cpu(bpl->addrHigh);
6437                 wqe->generic.bde.addrLow =  le32_to_cpu(bpl->addrLow);
6438                 /* swap the size field back to the cpu so we
6439                  * can assign it to the sgl.
6440                  */
6441                 wqe->generic.bde.tus.w  = le32_to_cpu(bpl->tus.w);
6442                 xmit_len = wqe->generic.bde.tus.f.bdeSize;
6443                 total_len = 0;
6444                 for (i = 0; i < numBdes; i++) {
6445                         bde.tus.w  = le32_to_cpu(bpl[i].tus.w);
6446                         total_len += bde.tus.f.bdeSize;
6447                 }
6448         } else
6449                 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
6450
6451         iocbq->iocb.ulpIoTag = iocbq->iotag;
6452         cmnd = iocbq->iocb.ulpCommand;
6453
6454         switch (iocbq->iocb.ulpCommand) {
6455         case CMD_ELS_REQUEST64_CR:
6456                 ndlp = (struct lpfc_nodelist *)iocbq->context1;
6457                 if (!iocbq->iocb.ulpLe) {
6458                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6459                                 "2007 Only Limited Edition cmd Format"
6460                                 " supported 0x%x\n",
6461                                 iocbq->iocb.ulpCommand);
6462                         return IOCB_ERROR;
6463                 }
6464                 wqe->els_req.payload_len = xmit_len;
6465                 /* Els_reguest64 has a TMO */
6466                 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
6467                         iocbq->iocb.ulpTimeout);
6468                 /* Need a VF for word 4 set the vf bit*/
6469                 bf_set(els_req64_vf, &wqe->els_req, 0);
6470                 /* And a VFID for word 12 */
6471                 bf_set(els_req64_vfid, &wqe->els_req, 0);
6472                 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
6473                 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
6474                        iocbq->iocb.ulpContext);
6475                 bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
6476                 bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
6477                 /* CCP CCPE PV PRI in word10 were set in the memcpy */
6478                 if (command_type == ELS_COMMAND_FIP) {
6479                         els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
6480                                         >> LPFC_FIP_ELS_ID_SHIFT);
6481                 }
6482                 bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com, ndlp->nlp_rpi);
6483                 bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
6484                 bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
6485                 bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
6486                 bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
6487                 bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
6488                 bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
6489         break;
6490         case CMD_XMIT_SEQUENCE64_CX:
6491                 bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
6492                        iocbq->iocb.un.ulpWord[3]);
6493                 bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
6494                        iocbq->iocb.ulpContext);
6495                 /* The entire sequence is transmitted for this IOCB */
6496                 xmit_len = total_len;
6497                 cmnd = CMD_XMIT_SEQUENCE64_CR;
6498         case CMD_XMIT_SEQUENCE64_CR:
6499                 /* word3 iocb=io_tag32 wqe=reserved */
6500                 wqe->xmit_sequence.rsvd3 = 0;
6501                 /* word4 relative_offset memcpy */
6502                 /* word5 r_ctl/df_ctl memcpy */
6503                 bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
6504                 bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
6505                 bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
6506                        LPFC_WQE_IOD_WRITE);
6507                 bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
6508                        LPFC_WQE_LENLOC_WORD12);
6509                 bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
6510                 wqe->xmit_sequence.xmit_len = xmit_len;
6511                 command_type = OTHER_COMMAND;
6512         break;
6513         case CMD_XMIT_BCAST64_CN:
6514                 /* word3 iocb=iotag32 wqe=seq_payload_len */
6515                 wqe->xmit_bcast64.seq_payload_len = xmit_len;
6516                 /* word4 iocb=rsvd wqe=rsvd */
6517                 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
6518                 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
6519                 bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
6520                         ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
6521                 bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
6522                 bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
6523                 bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
6524                        LPFC_WQE_LENLOC_WORD3);
6525                 bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
6526         break;
6527         case CMD_FCP_IWRITE64_CR:
6528                 command_type = FCP_COMMAND_DATA_OUT;
6529                 /* word3 iocb=iotag wqe=payload_offset_len */
6530                 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
6531                 wqe->fcp_iwrite.payload_offset_len =
6532                         xmit_len + sizeof(struct fcp_rsp);
6533                 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
6534                 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
6535                 bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
6536                        iocbq->iocb.ulpFCP2Rcvy);
6537                 bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
6538                 /* Always open the exchange */
6539                 bf_set(wqe_xc, &wqe->fcp_iwrite.wqe_com, 0);
6540                 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
6541                 bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
6542                 bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
6543                        LPFC_WQE_LENLOC_WORD4);
6544                 bf_set(wqe_ebde_cnt, &wqe->fcp_iwrite.wqe_com, 0);
6545                 bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
6546         break;
6547         case CMD_FCP_IREAD64_CR:
6548                 /* word3 iocb=iotag wqe=payload_offset_len */
6549                 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
6550                 wqe->fcp_iread.payload_offset_len =
6551                         xmit_len + sizeof(struct fcp_rsp);
6552                 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
6553                 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
6554                 bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
6555                        iocbq->iocb.ulpFCP2Rcvy);
6556                 bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
6557                 /* Always open the exchange */
6558                 bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
6559                 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
6560                 bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
6561                 bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
6562                        LPFC_WQE_LENLOC_WORD4);
6563                 bf_set(wqe_ebde_cnt, &wqe->fcp_iread.wqe_com, 0);
6564                 bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
6565         break;
6566         case CMD_FCP_ICMND64_CR:
6567                 /* word3 iocb=IO_TAG wqe=reserved */
6568                 wqe->fcp_icmd.rsrvd3 = 0;
6569                 bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
6570                 /* Always open the exchange */
6571                 bf_set(wqe_xc, &wqe->fcp_icmd.wqe_com, 0);
6572                 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
6573                 bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
6574                 bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
6575                 bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
6576                        LPFC_WQE_LENLOC_NONE);
6577                 bf_set(wqe_ebde_cnt, &wqe->fcp_icmd.wqe_com, 0);
6578         break;
6579         case CMD_GEN_REQUEST64_CR:
6580                 /* For this command calculate the xmit length of the
6581                  * request bde.
6582                  */
6583                 xmit_len = 0;
6584                 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
6585                         sizeof(struct ulp_bde64);
6586                 for (i = 0; i < numBdes; i++) {
6587                         bde.tus.w = le32_to_cpu(bpl[i].tus.w);
6588                         if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
6589                                 break;
6590                         xmit_len += bde.tus.f.bdeSize;
6591                 }
6592                 /* word3 iocb=IO_TAG wqe=request_payload_len */
6593                 wqe->gen_req.request_payload_len = xmit_len;
6594                 /* word4 iocb=parameter wqe=relative_offset memcpy */
6595                 /* word5 [rctl, type, df_ctl, la] copied in memcpy */
6596                 /* word6 context tag copied in memcpy */
6597                 if (iocbq->iocb.ulpCt_h  || iocbq->iocb.ulpCt_l) {
6598                         ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
6599                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6600                                 "2015 Invalid CT %x command 0x%x\n",
6601                                 ct, iocbq->iocb.ulpCommand);
6602                         return IOCB_ERROR;
6603                 }
6604                 bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
6605                 bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
6606                 bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
6607                 bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
6608                 bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
6609                 bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
6610                 bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
6611                 bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
6612                 command_type = OTHER_COMMAND;
6613         break;
6614         case CMD_XMIT_ELS_RSP64_CX:
6615                 ndlp = (struct lpfc_nodelist *)iocbq->context1;
6616                 /* words0-2 BDE memcpy */
6617                 /* word3 iocb=iotag32 wqe=response_payload_len */
6618                 wqe->xmit_els_rsp.response_payload_len = xmit_len;
6619                 /* word4 iocb=did wge=rsvd. */
6620                 wqe->xmit_els_rsp.rsvd4 = 0;
6621                 /* word5 iocb=rsvd wge=did */
6622                 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
6623                          iocbq->iocb.un.elsreq64.remoteID);
6624                 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
6625                        ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
6626                 bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
6627                 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
6628                        iocbq->iocb.ulpContext);
6629                 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
6630                         bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
6631                                iocbq->vport->vpi + phba->vpi_base);
6632                 bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
6633                 bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
6634                 bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
6635                 bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
6636                        LPFC_WQE_LENLOC_WORD3);
6637                 bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
6638                 bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp, ndlp->nlp_rpi);
6639                 command_type = OTHER_COMMAND;
6640         break;
6641         case CMD_CLOSE_XRI_CN:
6642         case CMD_ABORT_XRI_CN:
6643         case CMD_ABORT_XRI_CX:
6644                 /* words 0-2 memcpy should be 0 rserved */
6645                 /* port will send abts */
6646                 abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
6647                 if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
6648                         abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
6649                         fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
6650                 } else
6651                         fip = 0;
6652
6653                 if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
6654                         /*
6655                          * The link is down, or the command was ELS_FIP
6656                          * so the fw does not need to send abts
6657                          * on the wire.
6658                          */
6659                         bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
6660                 else
6661                         bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
6662                 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
6663                 /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
6664                 wqe->abort_cmd.rsrvd5 = 0;
6665                 bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
6666                         ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
6667                 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
6668                 /*
6669                  * The abort handler will send us CMD_ABORT_XRI_CN or
6670                  * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
6671                  */
6672                 bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
6673                 bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
6674                 bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
6675                        LPFC_WQE_LENLOC_NONE);
6676                 cmnd = CMD_ABORT_XRI_CX;
6677                 command_type = OTHER_COMMAND;
6678                 xritag = 0;
6679         break;
6680         case CMD_XMIT_BLS_RSP64_CX:
6681                 /* As BLS ABTS RSP WQE is very different from other WQEs,
6682                  * we re-construct this WQE here based on information in
6683                  * iocbq from scratch.
6684                  */
6685                 memset(wqe, 0, sizeof(union lpfc_wqe));
6686                 /* OX_ID is invariable to who sent ABTS to CT exchange */
6687                 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
6688                        bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
6689                 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
6690                     LPFC_ABTS_UNSOL_INT) {
6691                         /* ABTS sent by initiator to CT exchange, the
6692                          * RX_ID field will be filled with the newly
6693                          * allocated responder XRI.
6694                          */
6695                         bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
6696                                iocbq->sli4_xritag);
6697                 } else {
6698                         /* ABTS sent by responder to CT exchange, the
6699                          * RX_ID field will be filled with the responder
6700                          * RX_ID from ABTS.
6701                          */
6702                         bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
6703                                bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
6704                 }
6705                 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
6706                 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
6707                 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
6708                        iocbq->iocb.ulpContext);
6709                 bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
6710                 bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
6711                        LPFC_WQE_LENLOC_NONE);
6712                 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
6713                 command_type = OTHER_COMMAND;
6714                 if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
6715                         bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
6716                                bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
6717                         bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
6718                                bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
6719                         bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
6720                                bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
6721                 }
6722
6723         break;
6724         case CMD_XRI_ABORTED_CX:
6725         case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
6726         case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
6727         case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
6728         case CMD_FCP_TRSP64_CX: /* Target mode rcv */
6729         case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
6730         default:
6731                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6732                                 "2014 Invalid command 0x%x\n",
6733                                 iocbq->iocb.ulpCommand);
6734                 return IOCB_ERROR;
6735         break;
6736         }
6737         bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
6738         bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
6739         wqe->generic.wqe_com.abort_tag = abort_tag;
6740         bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
6741         bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
6742         bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
6743         bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
6744         return 0;
6745 }
6746
6747 /**
6748  * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
6749  * @phba: Pointer to HBA context object.
6750  * @ring_number: SLI ring number to issue iocb on.
6751  * @piocb: Pointer to command iocb.
6752  * @flag: Flag indicating if this command can be put into txq.
6753  *
6754  * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
6755  * an iocb command to an HBA with SLI-4 interface spec.
6756  *
6757  * This function is called with hbalock held. The function will return success
6758  * after it successfully submit the iocb to firmware or after adding to the
6759  * txq.
6760  **/
6761 static int
6762 __lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
6763                          struct lpfc_iocbq *piocb, uint32_t flag)
6764 {
6765         struct lpfc_sglq *sglq;
6766         union lpfc_wqe wqe;
6767         struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
6768
6769         if (piocb->sli4_xritag == NO_XRI) {
6770                 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
6771                     piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN ||
6772                     piocb->iocb.ulpCommand == CMD_XMIT_BLS_RSP64_CX)
6773                         sglq = NULL;
6774                 else {
6775                         if (pring->txq_cnt) {
6776                                 if (!(flag & SLI_IOCB_RET_IOCB)) {
6777                                         __lpfc_sli_ringtx_put(phba,
6778                                                 pring, piocb);
6779                                         return IOCB_SUCCESS;
6780                                 } else {
6781                                         return IOCB_BUSY;
6782                                 }
6783                         } else {
6784                         sglq = __lpfc_sli_get_sglq(phba, piocb);
6785                                 if (!sglq) {
6786                                         if (!(flag & SLI_IOCB_RET_IOCB)) {
6787                                                 __lpfc_sli_ringtx_put(phba,
6788                                                                 pring,
6789                                                                 piocb);
6790                                                 return IOCB_SUCCESS;
6791                                         } else
6792                                                 return IOCB_BUSY;
6793                                 }
6794                         }
6795                 }
6796         } else if (piocb->iocb_flag &  LPFC_IO_FCP) {
6797                 sglq = NULL; /* These IO's already have an XRI and
6798                               * a mapped sgl.
6799                               */
6800         } else {
6801                 /* This is a continuation of a commandi,(CX) so this
6802                  * sglq is on the active list
6803                  */
6804                 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_xritag);
6805                 if (!sglq)
6806                         return IOCB_ERROR;
6807         }
6808
6809         if (sglq) {
6810                 piocb->sli4_xritag = sglq->sli4_xritag;
6811
6812                 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
6813                         return IOCB_ERROR;
6814         }
6815
6816         if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
6817                 return IOCB_ERROR;
6818
6819         if ((piocb->iocb_flag & LPFC_IO_FCP) ||
6820                 (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
6821                 /*
6822                  * For FCP command IOCB, get a new WQ index to distribute
6823                  * WQE across the WQsr. On the other hand, for abort IOCB,
6824                  * it carries the same WQ index to the original command
6825                  * IOCB.
6826                  */
6827                 if (piocb->iocb_flag & LPFC_IO_FCP)
6828                         piocb->fcp_wqidx = lpfc_sli4_scmd_to_wqidx_distr(phba);
6829                 if (lpfc_sli4_wq_put(phba->sli4_hba.fcp_wq[piocb->fcp_wqidx],
6830                                      &wqe))
6831                         return IOCB_ERROR;
6832         } else {
6833                 if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
6834                         return IOCB_ERROR;
6835         }
6836         lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
6837
6838         return 0;
6839 }
6840
6841 /**
6842  * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
6843  *
6844  * This routine wraps the actual lockless version for issusing IOCB function
6845  * pointer from the lpfc_hba struct.
6846  *
6847  * Return codes:
6848  *      IOCB_ERROR - Error
6849  *      IOCB_SUCCESS - Success
6850  *      IOCB_BUSY - Busy
6851  **/
6852 int
6853 __lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
6854                 struct lpfc_iocbq *piocb, uint32_t flag)
6855 {
6856         return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
6857 }
6858
6859 /**
6860  * lpfc_sli_api_table_setup - Set up sli api function jump table
6861  * @phba: The hba struct for which this call is being executed.
6862  * @dev_grp: The HBA PCI-Device group number.
6863  *
6864  * This routine sets up the SLI interface API function jump table in @phba
6865  * struct.
6866  * Returns: 0 - success, -ENODEV - failure.
6867  **/
6868 int
6869 lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
6870 {
6871
6872         switch (dev_grp) {
6873         case LPFC_PCI_DEV_LP:
6874                 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
6875                 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
6876                 break;
6877         case LPFC_PCI_DEV_OC:
6878                 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
6879                 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
6880                 break;
6881         default:
6882                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6883                                 "1419 Invalid HBA PCI-device group: 0x%x\n",
6884                                 dev_grp);
6885                 return -ENODEV;
6886                 break;
6887         }
6888         phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
6889         return 0;
6890 }
6891
6892 /**
6893  * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
6894  * @phba: Pointer to HBA context object.
6895  * @pring: Pointer to driver SLI ring object.
6896  * @piocb: Pointer to command iocb.
6897  * @flag: Flag indicating if this command can be put into txq.
6898  *
6899  * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
6900  * function. This function gets the hbalock and calls
6901  * __lpfc_sli_issue_iocb function and will return the error returned
6902  * by __lpfc_sli_issue_iocb function. This wrapper is used by
6903  * functions which do not hold hbalock.
6904  **/
6905 int
6906 lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
6907                     struct lpfc_iocbq *piocb, uint32_t flag)
6908 {
6909         unsigned long iflags;
6910         int rc;
6911
6912         spin_lock_irqsave(&phba->hbalock, iflags);
6913         rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
6914         spin_unlock_irqrestore(&phba->hbalock, iflags);
6915
6916         return rc;
6917 }
6918
6919 /**
6920  * lpfc_extra_ring_setup - Extra ring setup function
6921  * @phba: Pointer to HBA context object.
6922  *
6923  * This function is called while driver attaches with the
6924  * HBA to setup the extra ring. The extra ring is used
6925  * only when driver needs to support target mode functionality
6926  * or IP over FC functionalities.
6927  *
6928  * This function is called with no lock held.
6929  **/
6930 static int
6931 lpfc_extra_ring_setup( struct lpfc_hba *phba)
6932 {
6933         struct lpfc_sli *psli;
6934         struct lpfc_sli_ring *pring;
6935
6936         psli = &phba->sli;
6937
6938         /* Adjust cmd/rsp ring iocb entries more evenly */
6939
6940         /* Take some away from the FCP ring */
6941         pring = &psli->ring[psli->fcp_ring];
6942         pring->numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
6943         pring->numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
6944         pring->numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
6945         pring->numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
6946
6947         /* and give them to the extra ring */
6948         pring = &psli->ring[psli->extra_ring];
6949
6950         pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
6951         pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
6952         pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
6953         pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
6954
6955         /* Setup default profile for this ring */
6956         pring->iotag_max = 4096;
6957         pring->num_mask = 1;
6958         pring->prt[0].profile = 0;      /* Mask 0 */
6959         pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
6960         pring->prt[0].type = phba->cfg_multi_ring_type;
6961         pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
6962         return 0;
6963 }
6964
6965 /**
6966  * lpfc_sli_async_event_handler - ASYNC iocb handler function
6967  * @phba: Pointer to HBA context object.
6968  * @pring: Pointer to driver SLI ring object.
6969  * @iocbq: Pointer to iocb object.
6970  *
6971  * This function is called by the slow ring event handler
6972  * function when there is an ASYNC event iocb in the ring.
6973  * This function is called with no lock held.
6974  * Currently this function handles only temperature related
6975  * ASYNC events. The function decodes the temperature sensor
6976  * event message and posts events for the management applications.
6977  **/
6978 static void
6979 lpfc_sli_async_event_handler(struct lpfc_hba * phba,
6980         struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
6981 {
6982         IOCB_t *icmd;
6983         uint16_t evt_code;
6984         uint16_t temp;
6985         struct temp_event temp_event_data;
6986         struct Scsi_Host *shost;
6987         uint32_t *iocb_w;
6988
6989         icmd = &iocbq->iocb;
6990         evt_code = icmd->un.asyncstat.evt_code;
6991         temp = icmd->ulpContext;
6992
6993         if ((evt_code != ASYNC_TEMP_WARN) &&
6994                 (evt_code != ASYNC_TEMP_SAFE)) {
6995                 iocb_w = (uint32_t *) icmd;
6996                 lpfc_printf_log(phba,
6997                         KERN_ERR,
6998                         LOG_SLI,
6999                         "0346 Ring %d handler: unexpected ASYNC_STATUS"
7000                         " evt_code 0x%x\n"
7001                         "W0  0x%08x W1  0x%08x W2  0x%08x W3  0x%08x\n"
7002                         "W4  0x%08x W5  0x%08x W6  0x%08x W7  0x%08x\n"
7003                         "W8  0x%08x W9  0x%08x W10 0x%08x W11 0x%08x\n"
7004                         "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
7005                         pring->ringno,
7006                         icmd->un.asyncstat.evt_code,
7007                         iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
7008                         iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
7009                         iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
7010                         iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
7011
7012                 return;
7013         }
7014         temp_event_data.data = (uint32_t)temp;
7015         temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
7016         if (evt_code == ASYNC_TEMP_WARN) {
7017                 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
7018                 lpfc_printf_log(phba,
7019                                 KERN_ERR,
7020                                 LOG_TEMP,
7021                                 "0347 Adapter is very hot, please take "
7022                                 "corrective action. temperature : %d Celsius\n",
7023                                 temp);
7024         }
7025         if (evt_code == ASYNC_TEMP_SAFE) {
7026                 temp_event_data.event_code = LPFC_NORMAL_TEMP;
7027                 lpfc_printf_log(phba,
7028                                 KERN_ERR,
7029                                 LOG_TEMP,
7030                                 "0340 Adapter temperature is OK now. "
7031                                 "temperature : %d Celsius\n",
7032                                 temp);
7033         }
7034
7035         /* Send temperature change event to applications */
7036         shost = lpfc_shost_from_vport(phba->pport);
7037         fc_host_post_vendor_event(shost, fc_get_event_number(),
7038                 sizeof(temp_event_data), (char *) &temp_event_data,
7039                 LPFC_NL_VENDOR_ID);
7040
7041 }
7042
7043
7044 /**
7045  * lpfc_sli_setup - SLI ring setup function
7046  * @phba: Pointer to HBA context object.
7047  *
7048  * lpfc_sli_setup sets up rings of the SLI interface with
7049  * number of iocbs per ring and iotags. This function is
7050  * called while driver attach to the HBA and before the
7051  * interrupts are enabled. So there is no need for locking.
7052  *
7053  * This function always returns 0.
7054  **/
7055 int
7056 lpfc_sli_setup(struct lpfc_hba *phba)
7057 {
7058         int i, totiocbsize = 0;
7059         struct lpfc_sli *psli = &phba->sli;
7060         struct lpfc_sli_ring *pring;
7061
7062         psli->num_rings = MAX_CONFIGURED_RINGS;
7063         psli->sli_flag = 0;
7064         psli->fcp_ring = LPFC_FCP_RING;
7065         psli->next_ring = LPFC_FCP_NEXT_RING;
7066         psli->extra_ring = LPFC_EXTRA_RING;
7067
7068         psli->iocbq_lookup = NULL;
7069         psli->iocbq_lookup_len = 0;
7070         psli->last_iotag = 0;
7071
7072         for (i = 0; i < psli->num_rings; i++) {
7073                 pring = &psli->ring[i];
7074                 switch (i) {
7075                 case LPFC_FCP_RING:     /* ring 0 - FCP */
7076                         /* numCiocb and numRiocb are used in config_port */
7077                         pring->numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
7078                         pring->numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
7079                         pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
7080                         pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
7081                         pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
7082                         pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
7083                         pring->sizeCiocb = (phba->sli_rev == 3) ?
7084                                                         SLI3_IOCB_CMD_SIZE :
7085                                                         SLI2_IOCB_CMD_SIZE;
7086                         pring->sizeRiocb = (phba->sli_rev == 3) ?
7087                                                         SLI3_IOCB_RSP_SIZE :
7088                                                         SLI2_IOCB_RSP_SIZE;
7089                         pring->iotag_ctr = 0;
7090                         pring->iotag_max =
7091                             (phba->cfg_hba_queue_depth * 2);
7092                         pring->fast_iotag = pring->iotag_max;
7093                         pring->num_mask = 0;
7094                         break;
7095                 case LPFC_EXTRA_RING:   /* ring 1 - EXTRA */
7096                         /* numCiocb and numRiocb are used in config_port */
7097                         pring->numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
7098                         pring->numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
7099                         pring->sizeCiocb = (phba->sli_rev == 3) ?
7100                                                         SLI3_IOCB_CMD_SIZE :
7101                                                         SLI2_IOCB_CMD_SIZE;
7102                         pring->sizeRiocb = (phba->sli_rev == 3) ?
7103                                                         SLI3_IOCB_RSP_SIZE :
7104                                                         SLI2_IOCB_RSP_SIZE;
7105                         pring->iotag_max = phba->cfg_hba_queue_depth;
7106                         pring->num_mask = 0;
7107                         break;
7108                 case LPFC_ELS_RING:     /* ring 2 - ELS / CT */
7109                         /* numCiocb and numRiocb are used in config_port */
7110                         pring->numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
7111                         pring->numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
7112                         pring->sizeCiocb = (phba->sli_rev == 3) ?
7113                                                         SLI3_IOCB_CMD_SIZE :
7114                                                         SLI2_IOCB_CMD_SIZE;
7115                         pring->sizeRiocb = (phba->sli_rev == 3) ?
7116                                                         SLI3_IOCB_RSP_SIZE :
7117                                                         SLI2_IOCB_RSP_SIZE;
7118                         pring->fast_iotag = 0;
7119                         pring->iotag_ctr = 0;
7120                         pring->iotag_max = 4096;
7121                         pring->lpfc_sli_rcv_async_status =
7122                                 lpfc_sli_async_event_handler;
7123                         pring->num_mask = LPFC_MAX_RING_MASK;
7124                         pring->prt[0].profile = 0;      /* Mask 0 */
7125                         pring->prt[0].rctl = FC_RCTL_ELS_REQ;
7126                         pring->prt[0].type = FC_TYPE_ELS;
7127                         pring->prt[0].lpfc_sli_rcv_unsol_event =
7128                             lpfc_els_unsol_event;
7129                         pring->prt[1].profile = 0;      /* Mask 1 */
7130                         pring->prt[1].rctl = FC_RCTL_ELS_REP;
7131                         pring->prt[1].type = FC_TYPE_ELS;
7132                         pring->prt[1].lpfc_sli_rcv_unsol_event =
7133                             lpfc_els_unsol_event;
7134                         pring->prt[2].profile = 0;      /* Mask 2 */
7135                         /* NameServer Inquiry */
7136                         pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
7137                         /* NameServer */
7138                         pring->prt[2].type = FC_TYPE_CT;
7139                         pring->prt[2].lpfc_sli_rcv_unsol_event =
7140                             lpfc_ct_unsol_event;
7141                         pring->prt[3].profile = 0;      /* Mask 3 */
7142                         /* NameServer response */
7143                         pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
7144                         /* NameServer */
7145                         pring->prt[3].type = FC_TYPE_CT;
7146                         pring->prt[3].lpfc_sli_rcv_unsol_event =
7147                             lpfc_ct_unsol_event;
7148                         /* abort unsolicited sequence */
7149                         pring->prt[4].profile = 0;      /* Mask 4 */
7150                         pring->prt[4].rctl = FC_RCTL_BA_ABTS;
7151                         pring->prt[4].type = FC_TYPE_BLS;
7152                         pring->prt[4].lpfc_sli_rcv_unsol_event =
7153                             lpfc_sli4_ct_abort_unsol_event;
7154                         break;
7155                 }
7156                 totiocbsize += (pring->numCiocb * pring->sizeCiocb) +
7157                                 (pring->numRiocb * pring->sizeRiocb);
7158         }
7159         if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
7160                 /* Too many cmd / rsp ring entries in SLI2 SLIM */
7161                 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
7162                        "SLI2 SLIM Data: x%x x%lx\n",
7163                        phba->brd_no, totiocbsize,
7164                        (unsigned long) MAX_SLIM_IOCB_SIZE);
7165         }
7166         if (phba->cfg_multi_ring_support == 2)
7167                 lpfc_extra_ring_setup(phba);
7168
7169         return 0;
7170 }
7171
7172 /**
7173  * lpfc_sli_queue_setup - Queue initialization function
7174  * @phba: Pointer to HBA context object.
7175  *
7176  * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
7177  * ring. This function also initializes ring indices of each ring.
7178  * This function is called during the initialization of the SLI
7179  * interface of an HBA.
7180  * This function is called with no lock held and always returns
7181  * 1.
7182  **/
7183 int
7184 lpfc_sli_queue_setup(struct lpfc_hba *phba)
7185 {
7186         struct lpfc_sli *psli;
7187         struct lpfc_sli_ring *pring;
7188         int i;
7189
7190         psli = &phba->sli;
7191         spin_lock_irq(&phba->hbalock);
7192         INIT_LIST_HEAD(&psli->mboxq);
7193         INIT_LIST_HEAD(&psli->mboxq_cmpl);
7194         /* Initialize list headers for txq and txcmplq as double linked lists */
7195         for (i = 0; i < psli->num_rings; i++) {
7196                 pring = &psli->ring[i];
7197                 pring->ringno = i;
7198                 pring->next_cmdidx  = 0;
7199                 pring->local_getidx = 0;
7200                 pring->cmdidx = 0;
7201                 INIT_LIST_HEAD(&pring->txq);
7202                 INIT_LIST_HEAD(&pring->txcmplq);
7203                 INIT_LIST_HEAD(&pring->iocb_continueq);
7204                 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
7205                 INIT_LIST_HEAD(&pring->postbufq);
7206         }
7207         spin_unlock_irq(&phba->hbalock);
7208         return 1;
7209 }
7210
7211 /**
7212  * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
7213  * @phba: Pointer to HBA context object.
7214  *
7215  * This routine flushes the mailbox command subsystem. It will unconditionally
7216  * flush all the mailbox commands in the three possible stages in the mailbox
7217  * command sub-system: pending mailbox command queue; the outstanding mailbox
7218  * command; and completed mailbox command queue. It is caller's responsibility
7219  * to make sure that the driver is in the proper state to flush the mailbox
7220  * command sub-system. Namely, the posting of mailbox commands into the
7221  * pending mailbox command queue from the various clients must be stopped;
7222  * either the HBA is in a state that it will never works on the outstanding
7223  * mailbox command (such as in EEH or ERATT conditions) or the outstanding
7224  * mailbox command has been completed.
7225  **/
7226 static void
7227 lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
7228 {
7229         LIST_HEAD(completions);
7230         struct lpfc_sli *psli = &phba->sli;
7231         LPFC_MBOXQ_t *pmb;
7232         unsigned long iflag;
7233
7234         /* Flush all the mailbox commands in the mbox system */
7235         spin_lock_irqsave(&phba->hbalock, iflag);
7236         /* The pending mailbox command queue */
7237         list_splice_init(&phba->sli.mboxq, &completions);
7238         /* The outstanding active mailbox command */
7239         if (psli->mbox_active) {
7240                 list_add_tail(&psli->mbox_active->list, &completions);
7241                 psli->mbox_active = NULL;
7242                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7243         }
7244         /* The completed mailbox command queue */
7245         list_splice_init(&phba->sli.mboxq_cmpl, &completions);
7246         spin_unlock_irqrestore(&phba->hbalock, iflag);
7247
7248         /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
7249         while (!list_empty(&completions)) {
7250                 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
7251                 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
7252                 if (pmb->mbox_cmpl)
7253                         pmb->mbox_cmpl(phba, pmb);
7254         }
7255 }
7256
7257 /**
7258  * lpfc_sli_host_down - Vport cleanup function
7259  * @vport: Pointer to virtual port object.
7260  *
7261  * lpfc_sli_host_down is called to clean up the resources
7262  * associated with a vport before destroying virtual
7263  * port data structures.
7264  * This function does following operations:
7265  * - Free discovery resources associated with this virtual
7266  *   port.
7267  * - Free iocbs associated with this virtual port in
7268  *   the txq.
7269  * - Send abort for all iocb commands associated with this
7270  *   vport in txcmplq.
7271  *
7272  * This function is called with no lock held and always returns 1.
7273  **/
7274 int
7275 lpfc_sli_host_down(struct lpfc_vport *vport)
7276 {
7277         LIST_HEAD(completions);
7278         struct lpfc_hba *phba = vport->phba;
7279         struct lpfc_sli *psli = &phba->sli;
7280         struct lpfc_sli_ring *pring;
7281         struct lpfc_iocbq *iocb, *next_iocb;
7282         int i;
7283         unsigned long flags = 0;
7284         uint16_t prev_pring_flag;
7285
7286         lpfc_cleanup_discovery_resources(vport);
7287
7288         spin_lock_irqsave(&phba->hbalock, flags);
7289         for (i = 0; i < psli->num_rings; i++) {
7290                 pring = &psli->ring[i];
7291                 prev_pring_flag = pring->flag;
7292                 /* Only slow rings */
7293                 if (pring->ringno == LPFC_ELS_RING) {
7294                         pring->flag |= LPFC_DEFERRED_RING_EVENT;
7295                         /* Set the lpfc data pending flag */
7296                         set_bit(LPFC_DATA_READY, &phba->data_flags);
7297                 }
7298                 /*
7299                  * Error everything on the txq since these iocbs have not been
7300                  * given to the FW yet.
7301                  */
7302                 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
7303                         if (iocb->vport != vport)
7304                                 continue;
7305                         list_move_tail(&iocb->list, &completions);
7306                         pring->txq_cnt--;
7307                 }
7308
7309                 /* Next issue ABTS for everything on the txcmplq */
7310                 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
7311                                                                         list) {
7312                         if (iocb->vport != vport)
7313                                 continue;
7314                         lpfc_sli_issue_abort_iotag(phba, pring, iocb);
7315                 }
7316
7317                 pring->flag = prev_pring_flag;
7318         }
7319
7320         spin_unlock_irqrestore(&phba->hbalock, flags);
7321
7322         /* Cancel all the IOCBs from the completions list */
7323         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
7324                               IOERR_SLI_DOWN);
7325         return 1;
7326 }
7327
7328 /**
7329  * lpfc_sli_hba_down - Resource cleanup function for the HBA
7330  * @phba: Pointer to HBA context object.
7331  *
7332  * This function cleans up all iocb, buffers, mailbox commands
7333  * while shutting down the HBA. This function is called with no
7334  * lock held and always returns 1.
7335  * This function does the following to cleanup driver resources:
7336  * - Free discovery resources for each virtual port
7337  * - Cleanup any pending fabric iocbs
7338  * - Iterate through the iocb txq and free each entry
7339  *   in the list.
7340  * - Free up any buffer posted to the HBA
7341  * - Free mailbox commands in the mailbox queue.
7342  **/
7343 int
7344 lpfc_sli_hba_down(struct lpfc_hba *phba)
7345 {
7346         LIST_HEAD(completions);
7347         struct lpfc_sli *psli = &phba->sli;
7348         struct lpfc_sli_ring *pring;
7349         struct lpfc_dmabuf *buf_ptr;
7350         unsigned long flags = 0;
7351         int i;
7352
7353         /* Shutdown the mailbox command sub-system */
7354         lpfc_sli_mbox_sys_shutdown(phba);
7355
7356         lpfc_hba_down_prep(phba);
7357
7358         lpfc_fabric_abort_hba(phba);
7359
7360         spin_lock_irqsave(&phba->hbalock, flags);
7361         for (i = 0; i < psli->num_rings; i++) {
7362                 pring = &psli->ring[i];
7363                 /* Only slow rings */
7364                 if (pring->ringno == LPFC_ELS_RING) {
7365                         pring->flag |= LPFC_DEFERRED_RING_EVENT;
7366                         /* Set the lpfc data pending flag */
7367                         set_bit(LPFC_DATA_READY, &phba->data_flags);
7368                 }
7369
7370                 /*
7371                  * Error everything on the txq since these iocbs have not been
7372                  * given to the FW yet.
7373                  */
7374                 list_splice_init(&pring->txq, &completions);
7375                 pring->txq_cnt = 0;
7376
7377         }
7378         spin_unlock_irqrestore(&phba->hbalock, flags);
7379
7380         /* Cancel all the IOCBs from the completions list */
7381         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
7382                               IOERR_SLI_DOWN);
7383
7384         spin_lock_irqsave(&phba->hbalock, flags);
7385         list_splice_init(&phba->elsbuf, &completions);
7386         phba->elsbuf_cnt = 0;
7387         phba->elsbuf_prev_cnt = 0;
7388         spin_unlock_irqrestore(&phba->hbalock, flags);
7389
7390         while (!list_empty(&completions)) {
7391                 list_remove_head(&completions, buf_ptr,
7392                         struct lpfc_dmabuf, list);
7393                 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
7394                 kfree(buf_ptr);
7395         }
7396
7397         /* Return any active mbox cmds */
7398         del_timer_sync(&psli->mbox_tmo);
7399
7400         spin_lock_irqsave(&phba->pport->work_port_lock, flags);
7401         phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
7402         spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
7403
7404         return 1;
7405 }
7406
7407 /**
7408  * lpfc_sli_pcimem_bcopy - SLI memory copy function
7409  * @srcp: Source memory pointer.
7410  * @destp: Destination memory pointer.
7411  * @cnt: Number of words required to be copied.
7412  *
7413  * This function is used for copying data between driver memory
7414  * and the SLI memory. This function also changes the endianness
7415  * of each word if native endianness is different from SLI
7416  * endianness. This function can be called with or without
7417  * lock.
7418  **/
7419 void
7420 lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
7421 {
7422         uint32_t *src = srcp;
7423         uint32_t *dest = destp;
7424         uint32_t ldata;
7425         int i;
7426
7427         for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
7428                 ldata = *src;
7429                 ldata = le32_to_cpu(ldata);
7430                 *dest = ldata;
7431                 src++;
7432                 dest++;
7433         }
7434 }
7435
7436
7437 /**
7438  * lpfc_sli_bemem_bcopy - SLI memory copy function
7439  * @srcp: Source memory pointer.
7440  * @destp: Destination memory pointer.
7441  * @cnt: Number of words required to be copied.
7442  *
7443  * This function is used for copying data between a data structure
7444  * with big endian representation to local endianness.
7445  * This function can be called with or without lock.
7446  **/
7447 void
7448 lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
7449 {
7450         uint32_t *src = srcp;
7451         uint32_t *dest = destp;
7452         uint32_t ldata;
7453         int i;
7454
7455         for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
7456                 ldata = *src;
7457                 ldata = be32_to_cpu(ldata);
7458                 *dest = ldata;
7459                 src++;
7460                 dest++;
7461         }
7462 }
7463
7464 /**
7465  * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
7466  * @phba: Pointer to HBA context object.
7467  * @pring: Pointer to driver SLI ring object.
7468  * @mp: Pointer to driver buffer object.
7469  *
7470  * This function is called with no lock held.
7471  * It always return zero after adding the buffer to the postbufq
7472  * buffer list.
7473  **/
7474 int
7475 lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7476                          struct lpfc_dmabuf *mp)
7477 {
7478         /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
7479            later */
7480         spin_lock_irq(&phba->hbalock);
7481         list_add_tail(&mp->list, &pring->postbufq);
7482         pring->postbufq_cnt++;
7483         spin_unlock_irq(&phba->hbalock);
7484         return 0;
7485 }
7486
7487 /**
7488  * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
7489  * @phba: Pointer to HBA context object.
7490  *
7491  * When HBQ is enabled, buffers are searched based on tags. This function
7492  * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
7493  * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
7494  * does not conflict with tags of buffer posted for unsolicited events.
7495  * The function returns the allocated tag. The function is called with
7496  * no locks held.
7497  **/
7498 uint32_t
7499 lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
7500 {
7501         spin_lock_irq(&phba->hbalock);
7502         phba->buffer_tag_count++;
7503         /*
7504          * Always set the QUE_BUFTAG_BIT to distiguish between
7505          * a tag assigned by HBQ.
7506          */
7507         phba->buffer_tag_count |= QUE_BUFTAG_BIT;
7508         spin_unlock_irq(&phba->hbalock);
7509         return phba->buffer_tag_count;
7510 }
7511
7512 /**
7513  * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
7514  * @phba: Pointer to HBA context object.
7515  * @pring: Pointer to driver SLI ring object.
7516  * @tag: Buffer tag.
7517  *
7518  * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
7519  * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
7520  * iocb is posted to the response ring with the tag of the buffer.
7521  * This function searches the pring->postbufq list using the tag
7522  * to find buffer associated with CMD_IOCB_RET_XRI64_CX
7523  * iocb. If the buffer is found then lpfc_dmabuf object of the
7524  * buffer is returned to the caller else NULL is returned.
7525  * This function is called with no lock held.
7526  **/
7527 struct lpfc_dmabuf *
7528 lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7529                         uint32_t tag)
7530 {
7531         struct lpfc_dmabuf *mp, *next_mp;
7532         struct list_head *slp = &pring->postbufq;
7533
7534         /* Search postbufq, from the beginning, looking for a match on tag */
7535         spin_lock_irq(&phba->hbalock);
7536         list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
7537                 if (mp->buffer_tag == tag) {
7538                         list_del_init(&mp->list);
7539                         pring->postbufq_cnt--;
7540                         spin_unlock_irq(&phba->hbalock);
7541                         return mp;
7542                 }
7543         }
7544
7545         spin_unlock_irq(&phba->hbalock);
7546         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7547                         "0402 Cannot find virtual addr for buffer tag on "
7548                         "ring %d Data x%lx x%p x%p x%x\n",
7549                         pring->ringno, (unsigned long) tag,
7550                         slp->next, slp->prev, pring->postbufq_cnt);
7551
7552         return NULL;
7553 }
7554
7555 /**
7556  * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
7557  * @phba: Pointer to HBA context object.
7558  * @pring: Pointer to driver SLI ring object.
7559  * @phys: DMA address of the buffer.
7560  *
7561  * This function searches the buffer list using the dma_address
7562  * of unsolicited event to find the driver's lpfc_dmabuf object
7563  * corresponding to the dma_address. The function returns the
7564  * lpfc_dmabuf object if a buffer is found else it returns NULL.
7565  * This function is called by the ct and els unsolicited event
7566  * handlers to get the buffer associated with the unsolicited
7567  * event.
7568  *
7569  * This function is called with no lock held.
7570  **/
7571 struct lpfc_dmabuf *
7572 lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7573                          dma_addr_t phys)
7574 {
7575         struct lpfc_dmabuf *mp, *next_mp;
7576         struct list_head *slp = &pring->postbufq;
7577
7578         /* Search postbufq, from the beginning, looking for a match on phys */
7579         spin_lock_irq(&phba->hbalock);
7580         list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
7581                 if (mp->phys == phys) {
7582                         list_del_init(&mp->list);
7583                         pring->postbufq_cnt--;
7584                         spin_unlock_irq(&phba->hbalock);
7585                         return mp;
7586                 }
7587         }
7588
7589         spin_unlock_irq(&phba->hbalock);
7590         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7591                         "0410 Cannot find virtual addr for mapped buf on "
7592                         "ring %d Data x%llx x%p x%p x%x\n",
7593                         pring->ringno, (unsigned long long)phys,
7594                         slp->next, slp->prev, pring->postbufq_cnt);
7595         return NULL;
7596 }
7597
7598 /**
7599  * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
7600  * @phba: Pointer to HBA context object.
7601  * @cmdiocb: Pointer to driver command iocb object.
7602  * @rspiocb: Pointer to driver response iocb object.
7603  *
7604  * This function is the completion handler for the abort iocbs for
7605  * ELS commands. This function is called from the ELS ring event
7606  * handler with no lock held. This function frees memory resources
7607  * associated with the abort iocb.
7608  **/
7609 static void
7610 lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7611                         struct lpfc_iocbq *rspiocb)
7612 {
7613         IOCB_t *irsp = &rspiocb->iocb;
7614         uint16_t abort_iotag, abort_context;
7615         struct lpfc_iocbq *abort_iocb;
7616         struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
7617
7618         abort_iocb = NULL;
7619
7620         if (irsp->ulpStatus) {
7621                 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
7622                 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
7623
7624                 spin_lock_irq(&phba->hbalock);
7625                 if (phba->sli_rev < LPFC_SLI_REV4) {
7626                         if (abort_iotag != 0 &&
7627                                 abort_iotag <= phba->sli.last_iotag)
7628                                 abort_iocb =
7629                                         phba->sli.iocbq_lookup[abort_iotag];
7630                 } else
7631                         /* For sli4 the abort_tag is the XRI,
7632                          * so the abort routine puts the iotag  of the iocb
7633                          * being aborted in the context field of the abort
7634                          * IOCB.
7635                          */
7636                         abort_iocb = phba->sli.iocbq_lookup[abort_context];
7637
7638                 /*
7639                  *  If the iocb is not found in Firmware queue the iocb
7640                  *  might have completed already. Do not free it again.
7641                  */
7642                 if (irsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
7643                         if (irsp->un.ulpWord[4] != IOERR_NO_XRI) {
7644                                 spin_unlock_irq(&phba->hbalock);
7645                                 lpfc_sli_release_iocbq(phba, cmdiocb);
7646                                 return;
7647                         }
7648                         /* For SLI4 the ulpContext field for abort IOCB
7649                          * holds the iotag of the IOCB being aborted so
7650                          * the local abort_context needs to be reset to
7651                          * match the aborted IOCBs ulpContext.
7652                          */
7653                         if (abort_iocb && phba->sli_rev == LPFC_SLI_REV4)
7654                                 abort_context = abort_iocb->iocb.ulpContext;
7655                 }
7656
7657                 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
7658                                 "0327 Cannot abort els iocb %p "
7659                                 "with tag %x context %x, abort status %x, "
7660                                 "abort code %x\n",
7661                                 abort_iocb, abort_iotag, abort_context,
7662                                 irsp->ulpStatus, irsp->un.ulpWord[4]);
7663                 /*
7664                  * make sure we have the right iocbq before taking it
7665                  * off the txcmplq and try to call completion routine.
7666                  */
7667                 if (!abort_iocb ||
7668                     abort_iocb->iocb.ulpContext != abort_context ||
7669                     (abort_iocb->iocb_flag & LPFC_DRIVER_ABORTED) == 0)
7670                         spin_unlock_irq(&phba->hbalock);
7671                 else if (phba->sli_rev < LPFC_SLI_REV4) {
7672                         /*
7673                          * leave the SLI4 aborted command on the txcmplq
7674                          * list and the command complete WCQE's XB bit
7675                          * will tell whether the SGL (XRI) can be released
7676                          * immediately or to the aborted SGL list for the
7677                          * following abort XRI from the HBA.
7678                          */
7679                         list_del_init(&abort_iocb->list);
7680                         if (abort_iocb->iocb_flag & LPFC_IO_ON_Q) {
7681                                 abort_iocb->iocb_flag &= ~LPFC_IO_ON_Q;
7682                                 pring->txcmplq_cnt--;
7683                         }
7684
7685                         /* Firmware could still be in progress of DMAing
7686                          * payload, so don't free data buffer till after
7687                          * a hbeat.
7688                          */
7689                         abort_iocb->iocb_flag |= LPFC_DELAY_MEM_FREE;
7690                         abort_iocb->iocb_flag &= ~LPFC_DRIVER_ABORTED;
7691                         spin_unlock_irq(&phba->hbalock);
7692
7693                         abort_iocb->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
7694                         abort_iocb->iocb.un.ulpWord[4] = IOERR_ABORT_REQUESTED;
7695                         (abort_iocb->iocb_cmpl)(phba, abort_iocb, abort_iocb);
7696                 } else
7697                         spin_unlock_irq(&phba->hbalock);
7698         }
7699
7700         lpfc_sli_release_iocbq(phba, cmdiocb);
7701         return;
7702 }
7703
7704 /**
7705  * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
7706  * @phba: Pointer to HBA context object.
7707  * @cmdiocb: Pointer to driver command iocb object.
7708  * @rspiocb: Pointer to driver response iocb object.
7709  *
7710  * The function is called from SLI ring event handler with no
7711  * lock held. This function is the completion handler for ELS commands
7712  * which are aborted. The function frees memory resources used for
7713  * the aborted ELS commands.
7714  **/
7715 static void
7716 lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7717                      struct lpfc_iocbq *rspiocb)
7718 {
7719         IOCB_t *irsp = &rspiocb->iocb;
7720
7721         /* ELS cmd tag <ulpIoTag> completes */
7722         lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
7723                         "0139 Ignoring ELS cmd tag x%x completion Data: "
7724                         "x%x x%x x%x\n",
7725                         irsp->ulpIoTag, irsp->ulpStatus,
7726                         irsp->un.ulpWord[4], irsp->ulpTimeout);
7727         if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
7728                 lpfc_ct_free_iocb(phba, cmdiocb);
7729         else
7730                 lpfc_els_free_iocb(phba, cmdiocb);
7731         return;
7732 }
7733
7734 /**
7735  * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
7736  * @phba: Pointer to HBA context object.
7737  * @pring: Pointer to driver SLI ring object.
7738  * @cmdiocb: Pointer to driver command iocb object.
7739  *
7740  * This function issues an abort iocb for the provided command iocb down to
7741  * the port. Other than the case the outstanding command iocb is an abort
7742  * request, this function issues abort out unconditionally. This function is
7743  * called with hbalock held. The function returns 0 when it fails due to
7744  * memory allocation failure or when the command iocb is an abort request.
7745  **/
7746 static int
7747 lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7748                            struct lpfc_iocbq *cmdiocb)
7749 {
7750         struct lpfc_vport *vport = cmdiocb->vport;
7751         struct lpfc_iocbq *abtsiocbp;
7752         IOCB_t *icmd = NULL;
7753         IOCB_t *iabt = NULL;
7754         int retval;
7755
7756         /*
7757          * There are certain command types we don't want to abort.  And we
7758          * don't want to abort commands that are already in the process of
7759          * being aborted.
7760          */
7761         icmd = &cmdiocb->iocb;
7762         if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
7763             icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
7764             (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
7765                 return 0;
7766
7767         /* issue ABTS for this IOCB based on iotag */
7768         abtsiocbp = __lpfc_sli_get_iocbq(phba);
7769         if (abtsiocbp == NULL)
7770                 return 0;
7771
7772         /* This signals the response to set the correct status
7773          * before calling the completion handler
7774          */
7775         cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
7776
7777         iabt = &abtsiocbp->iocb;
7778         iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
7779         iabt->un.acxri.abortContextTag = icmd->ulpContext;
7780         if (phba->sli_rev == LPFC_SLI_REV4) {
7781                 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
7782                 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
7783         }
7784         else
7785                 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
7786         iabt->ulpLe = 1;
7787         iabt->ulpClass = icmd->ulpClass;
7788
7789         /* ABTS WQE must go to the same WQ as the WQE to be aborted */
7790         abtsiocbp->fcp_wqidx = cmdiocb->fcp_wqidx;
7791         if (cmdiocb->iocb_flag & LPFC_IO_FCP)
7792                 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
7793
7794         if (phba->link_state >= LPFC_LINK_UP)
7795                 iabt->ulpCommand = CMD_ABORT_XRI_CN;
7796         else
7797                 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
7798
7799         abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
7800
7801         lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
7802                          "0339 Abort xri x%x, original iotag x%x, "
7803                          "abort cmd iotag x%x\n",
7804                          iabt->un.acxri.abortIoTag,
7805                          iabt->un.acxri.abortContextTag,
7806                          abtsiocbp->iotag);
7807         retval = __lpfc_sli_issue_iocb(phba, pring->ringno, abtsiocbp, 0);
7808
7809         if (retval)
7810                 __lpfc_sli_release_iocbq(phba, abtsiocbp);
7811
7812         /*
7813          * Caller to this routine should check for IOCB_ERROR
7814          * and handle it properly.  This routine no longer removes
7815          * iocb off txcmplq and call compl in case of IOCB_ERROR.
7816          */
7817         return retval;
7818 }
7819
7820 /**
7821  * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
7822  * @phba: Pointer to HBA context object.
7823  * @pring: Pointer to driver SLI ring object.
7824  * @cmdiocb: Pointer to driver command iocb object.
7825  *
7826  * This function issues an abort iocb for the provided command iocb. In case
7827  * of unloading, the abort iocb will not be issued to commands on the ELS
7828  * ring. Instead, the callback function shall be changed to those commands
7829  * so that nothing happens when them finishes. This function is called with
7830  * hbalock held. The function returns 0 when the command iocb is an abort
7831  * request.
7832  **/
7833 int
7834 lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7835                            struct lpfc_iocbq *cmdiocb)
7836 {
7837         struct lpfc_vport *vport = cmdiocb->vport;
7838         int retval = IOCB_ERROR;
7839         IOCB_t *icmd = NULL;
7840
7841         /*
7842          * There are certain command types we don't want to abort.  And we
7843          * don't want to abort commands that are already in the process of
7844          * being aborted.
7845          */
7846         icmd = &cmdiocb->iocb;
7847         if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
7848             icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
7849             (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
7850                 return 0;
7851
7852         /*
7853          * If we're unloading, don't abort iocb on the ELS ring, but change
7854          * the callback so that nothing happens when it finishes.
7855          */
7856         if ((vport->load_flag & FC_UNLOADING) &&
7857             (pring->ringno == LPFC_ELS_RING)) {
7858                 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
7859                         cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
7860                 else
7861                         cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
7862                 goto abort_iotag_exit;
7863         }
7864
7865         /* Now, we try to issue the abort to the cmdiocb out */
7866         retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
7867
7868 abort_iotag_exit:
7869         /*
7870          * Caller to this routine should check for IOCB_ERROR
7871          * and handle it properly.  This routine no longer removes
7872          * iocb off txcmplq and call compl in case of IOCB_ERROR.
7873          */
7874         return retval;
7875 }
7876
7877 /**
7878  * lpfc_sli_iocb_ring_abort - Unconditionally abort all iocbs on an iocb ring
7879  * @phba: Pointer to HBA context object.
7880  * @pring: Pointer to driver SLI ring object.
7881  *
7882  * This function aborts all iocbs in the given ring and frees all the iocb
7883  * objects in txq. This function issues abort iocbs unconditionally for all
7884  * the iocb commands in txcmplq. The iocbs in the txcmplq is not guaranteed
7885  * to complete before the return of this function. The caller is not required
7886  * to hold any locks.
7887  **/
7888 static void
7889 lpfc_sli_iocb_ring_abort(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
7890 {
7891         LIST_HEAD(completions);
7892         struct lpfc_iocbq *iocb, *next_iocb;
7893
7894         if (pring->ringno == LPFC_ELS_RING)
7895                 lpfc_fabric_abort_hba(phba);
7896
7897         spin_lock_irq(&phba->hbalock);
7898
7899         /* Take off all the iocbs on txq for cancelling */
7900         list_splice_init(&pring->txq, &completions);
7901         pring->txq_cnt = 0;
7902
7903         /* Next issue ABTS for everything on the txcmplq */
7904         list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
7905                 lpfc_sli_abort_iotag_issue(phba, pring, iocb);
7906
7907         spin_unlock_irq(&phba->hbalock);
7908
7909         /* Cancel all the IOCBs from the completions list */
7910         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
7911                               IOERR_SLI_ABORTED);
7912 }
7913
7914 /**
7915  * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
7916  * @phba: pointer to lpfc HBA data structure.
7917  *
7918  * This routine will abort all pending and outstanding iocbs to an HBA.
7919  **/
7920 void
7921 lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
7922 {
7923         struct lpfc_sli *psli = &phba->sli;
7924         struct lpfc_sli_ring *pring;
7925         int i;
7926
7927         for (i = 0; i < psli->num_rings; i++) {
7928                 pring = &psli->ring[i];
7929                 lpfc_sli_iocb_ring_abort(phba, pring);
7930         }
7931 }
7932
7933 /**
7934  * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
7935  * @iocbq: Pointer to driver iocb object.
7936  * @vport: Pointer to driver virtual port object.
7937  * @tgt_id: SCSI ID of the target.
7938  * @lun_id: LUN ID of the scsi device.
7939  * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
7940  *
7941  * This function acts as an iocb filter for functions which abort or count
7942  * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
7943  * 0 if the filtering criteria is met for the given iocb and will return
7944  * 1 if the filtering criteria is not met.
7945  * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
7946  * given iocb is for the SCSI device specified by vport, tgt_id and
7947  * lun_id parameter.
7948  * If ctx_cmd == LPFC_CTX_TGT,  the function returns 0 only if the
7949  * given iocb is for the SCSI target specified by vport and tgt_id
7950  * parameters.
7951  * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
7952  * given iocb is for the SCSI host associated with the given vport.
7953  * This function is called with no locks held.
7954  **/
7955 static int
7956 lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
7957                            uint16_t tgt_id, uint64_t lun_id,
7958                            lpfc_ctx_cmd ctx_cmd)
7959 {
7960         struct lpfc_scsi_buf *lpfc_cmd;
7961         int rc = 1;
7962
7963         if (!(iocbq->iocb_flag &  LPFC_IO_FCP))
7964                 return rc;
7965
7966         if (iocbq->vport != vport)
7967                 return rc;
7968
7969         lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
7970
7971         if (lpfc_cmd->pCmd == NULL)
7972                 return rc;
7973
7974         switch (ctx_cmd) {
7975         case LPFC_CTX_LUN:
7976                 if ((lpfc_cmd->rdata->pnode) &&
7977                     (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
7978                     (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
7979                         rc = 0;
7980                 break;
7981         case LPFC_CTX_TGT:
7982                 if ((lpfc_cmd->rdata->pnode) &&
7983                     (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
7984                         rc = 0;
7985                 break;
7986         case LPFC_CTX_HOST:
7987                 rc = 0;
7988                 break;
7989         default:
7990                 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
7991                         __func__, ctx_cmd);
7992                 break;
7993         }
7994
7995         return rc;
7996 }
7997
7998 /**
7999  * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
8000  * @vport: Pointer to virtual port.
8001  * @tgt_id: SCSI ID of the target.
8002  * @lun_id: LUN ID of the scsi device.
8003  * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
8004  *
8005  * This function returns number of FCP commands pending for the vport.
8006  * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
8007  * commands pending on the vport associated with SCSI device specified
8008  * by tgt_id and lun_id parameters.
8009  * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
8010  * commands pending on the vport associated with SCSI target specified
8011  * by tgt_id parameter.
8012  * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
8013  * commands pending on the vport.
8014  * This function returns the number of iocbs which satisfy the filter.
8015  * This function is called without any lock held.
8016  **/
8017 int
8018 lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
8019                   lpfc_ctx_cmd ctx_cmd)
8020 {
8021         struct lpfc_hba *phba = vport->phba;
8022         struct lpfc_iocbq *iocbq;
8023         int sum, i;
8024
8025         for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
8026                 iocbq = phba->sli.iocbq_lookup[i];
8027
8028                 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
8029                                                 ctx_cmd) == 0)
8030                         sum++;
8031         }
8032
8033         return sum;
8034 }
8035
8036 /**
8037  * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
8038  * @phba: Pointer to HBA context object
8039  * @cmdiocb: Pointer to command iocb object.
8040  * @rspiocb: Pointer to response iocb object.
8041  *
8042  * This function is called when an aborted FCP iocb completes. This
8043  * function is called by the ring event handler with no lock held.
8044  * This function frees the iocb.
8045  **/
8046 void
8047 lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
8048                         struct lpfc_iocbq *rspiocb)
8049 {
8050         lpfc_sli_release_iocbq(phba, cmdiocb);
8051         return;
8052 }
8053
8054 /**
8055  * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
8056  * @vport: Pointer to virtual port.
8057  * @pring: Pointer to driver SLI ring object.
8058  * @tgt_id: SCSI ID of the target.
8059  * @lun_id: LUN ID of the scsi device.
8060  * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
8061  *
8062  * This function sends an abort command for every SCSI command
8063  * associated with the given virtual port pending on the ring
8064  * filtered by lpfc_sli_validate_fcp_iocb function.
8065  * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
8066  * FCP iocbs associated with lun specified by tgt_id and lun_id
8067  * parameters
8068  * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
8069  * FCP iocbs associated with SCSI target specified by tgt_id parameter.
8070  * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
8071  * FCP iocbs associated with virtual port.
8072  * This function returns number of iocbs it failed to abort.
8073  * This function is called with no locks held.
8074  **/
8075 int
8076 lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
8077                     uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
8078 {
8079         struct lpfc_hba *phba = vport->phba;
8080         struct lpfc_iocbq *iocbq;
8081         struct lpfc_iocbq *abtsiocb;
8082         IOCB_t *cmd = NULL;
8083         int errcnt = 0, ret_val = 0;
8084         int i;
8085
8086         for (i = 1; i <= phba->sli.last_iotag; i++) {
8087                 iocbq = phba->sli.iocbq_lookup[i];
8088
8089                 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
8090                                                abort_cmd) != 0)
8091                         continue;
8092
8093                 /* issue ABTS for this IOCB based on iotag */
8094                 abtsiocb = lpfc_sli_get_iocbq(phba);
8095                 if (abtsiocb == NULL) {
8096                         errcnt++;
8097                         continue;
8098                 }
8099
8100                 cmd = &iocbq->iocb;
8101                 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
8102                 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
8103                 if (phba->sli_rev == LPFC_SLI_REV4)
8104                         abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
8105                 else
8106                         abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
8107                 abtsiocb->iocb.ulpLe = 1;
8108                 abtsiocb->iocb.ulpClass = cmd->ulpClass;
8109                 abtsiocb->vport = phba->pport;
8110
8111                 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
8112                 abtsiocb->fcp_wqidx = iocbq->fcp_wqidx;
8113                 if (iocbq->iocb_flag & LPFC_IO_FCP)
8114                         abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
8115
8116                 if (lpfc_is_link_up(phba))
8117                         abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
8118                 else
8119                         abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
8120
8121                 /* Setup callback routine and issue the command. */
8122                 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
8123                 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
8124                                               abtsiocb, 0);
8125                 if (ret_val == IOCB_ERROR) {
8126                         lpfc_sli_release_iocbq(phba, abtsiocb);
8127                         errcnt++;
8128                         continue;
8129                 }
8130         }
8131
8132         return errcnt;
8133 }
8134
8135 /**
8136  * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
8137  * @phba: Pointer to HBA context object.
8138  * @cmdiocbq: Pointer to command iocb.
8139  * @rspiocbq: Pointer to response iocb.
8140  *
8141  * This function is the completion handler for iocbs issued using
8142  * lpfc_sli_issue_iocb_wait function. This function is called by the
8143  * ring event handler function without any lock held. This function
8144  * can be called from both worker thread context and interrupt
8145  * context. This function also can be called from other thread which
8146  * cleans up the SLI layer objects.
8147  * This function copy the contents of the response iocb to the
8148  * response iocb memory object provided by the caller of
8149  * lpfc_sli_issue_iocb_wait and then wakes up the thread which
8150  * sleeps for the iocb completion.
8151  **/
8152 static void
8153 lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
8154                         struct lpfc_iocbq *cmdiocbq,
8155                         struct lpfc_iocbq *rspiocbq)
8156 {
8157         wait_queue_head_t *pdone_q;
8158         unsigned long iflags;
8159         struct lpfc_scsi_buf *lpfc_cmd;
8160
8161         spin_lock_irqsave(&phba->hbalock, iflags);
8162         cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
8163         if (cmdiocbq->context2 && rspiocbq)
8164                 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
8165                        &rspiocbq->iocb, sizeof(IOCB_t));
8166
8167         /* Set the exchange busy flag for task management commands */
8168         if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
8169                 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
8170                 lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
8171                         cur_iocbq);
8172                 lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
8173         }
8174
8175         pdone_q = cmdiocbq->context_un.wait_queue;
8176         if (pdone_q)
8177                 wake_up(pdone_q);
8178         spin_unlock_irqrestore(&phba->hbalock, iflags);
8179         return;
8180 }
8181
8182 /**
8183  * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
8184  * @phba: Pointer to HBA context object..
8185  * @piocbq: Pointer to command iocb.
8186  * @flag: Flag to test.
8187  *
8188  * This routine grabs the hbalock and then test the iocb_flag to
8189  * see if the passed in flag is set.
8190  * Returns:
8191  * 1 if flag is set.
8192  * 0 if flag is not set.
8193  **/
8194 static int
8195 lpfc_chk_iocb_flg(struct lpfc_hba *phba,
8196                  struct lpfc_iocbq *piocbq, uint32_t flag)
8197 {
8198         unsigned long iflags;
8199         int ret;
8200
8201         spin_lock_irqsave(&phba->hbalock, iflags);
8202         ret = piocbq->iocb_flag & flag;
8203         spin_unlock_irqrestore(&phba->hbalock, iflags);
8204         return ret;
8205
8206 }
8207
8208 /**
8209  * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
8210  * @phba: Pointer to HBA context object..
8211  * @pring: Pointer to sli ring.
8212  * @piocb: Pointer to command iocb.
8213  * @prspiocbq: Pointer to response iocb.
8214  * @timeout: Timeout in number of seconds.
8215  *
8216  * This function issues the iocb to firmware and waits for the
8217  * iocb to complete. If the iocb command is not
8218  * completed within timeout seconds, it returns IOCB_TIMEDOUT.
8219  * Caller should not free the iocb resources if this function
8220  * returns IOCB_TIMEDOUT.
8221  * The function waits for the iocb completion using an
8222  * non-interruptible wait.
8223  * This function will sleep while waiting for iocb completion.
8224  * So, this function should not be called from any context which
8225  * does not allow sleeping. Due to the same reason, this function
8226  * cannot be called with interrupt disabled.
8227  * This function assumes that the iocb completions occur while
8228  * this function sleep. So, this function cannot be called from
8229  * the thread which process iocb completion for this ring.
8230  * This function clears the iocb_flag of the iocb object before
8231  * issuing the iocb and the iocb completion handler sets this
8232  * flag and wakes this thread when the iocb completes.
8233  * The contents of the response iocb will be copied to prspiocbq
8234  * by the completion handler when the command completes.
8235  * This function returns IOCB_SUCCESS when success.
8236  * This function is called with no lock held.
8237  **/
8238 int
8239 lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
8240                          uint32_t ring_number,
8241                          struct lpfc_iocbq *piocb,
8242                          struct lpfc_iocbq *prspiocbq,
8243                          uint32_t timeout)
8244 {
8245         DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
8246         long timeleft, timeout_req = 0;
8247         int retval = IOCB_SUCCESS;
8248         uint32_t creg_val;
8249         struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
8250         /*
8251          * If the caller has provided a response iocbq buffer, then context2
8252          * is NULL or its an error.
8253          */
8254         if (prspiocbq) {
8255                 if (piocb->context2)
8256                         return IOCB_ERROR;
8257                 piocb->context2 = prspiocbq;
8258         }
8259
8260         piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
8261         piocb->context_un.wait_queue = &done_q;
8262         piocb->iocb_flag &= ~LPFC_IO_WAKE;
8263
8264         if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
8265                 if (lpfc_readl(phba->HCregaddr, &creg_val))
8266                         return IOCB_ERROR;
8267                 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
8268                 writel(creg_val, phba->HCregaddr);
8269                 readl(phba->HCregaddr); /* flush */
8270         }
8271
8272         retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
8273                                      SLI_IOCB_RET_IOCB);
8274         if (retval == IOCB_SUCCESS) {
8275                 timeout_req = timeout * HZ;
8276                 timeleft = wait_event_timeout(done_q,
8277                                 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
8278                                 timeout_req);
8279
8280                 if (piocb->iocb_flag & LPFC_IO_WAKE) {
8281                         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8282                                         "0331 IOCB wake signaled\n");
8283                 } else if (timeleft == 0) {
8284                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8285                                         "0338 IOCB wait timeout error - no "
8286                                         "wake response Data x%x\n", timeout);
8287                         retval = IOCB_TIMEDOUT;
8288                 } else {
8289                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8290                                         "0330 IOCB wake NOT set, "
8291                                         "Data x%x x%lx\n",
8292                                         timeout, (timeleft / jiffies));
8293                         retval = IOCB_TIMEDOUT;
8294                 }
8295         } else if (retval == IOCB_BUSY) {
8296                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8297                         "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
8298                         phba->iocb_cnt, pring->txq_cnt, pring->txcmplq_cnt);
8299                 return retval;
8300         } else {
8301                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8302                                 "0332 IOCB wait issue failed, Data x%x\n",
8303                                 retval);
8304                 retval = IOCB_ERROR;
8305         }
8306
8307         if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
8308                 if (lpfc_readl(phba->HCregaddr, &creg_val))
8309                         return IOCB_ERROR;
8310                 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
8311                 writel(creg_val, phba->HCregaddr);
8312                 readl(phba->HCregaddr); /* flush */
8313         }
8314
8315         if (prspiocbq)
8316                 piocb->context2 = NULL;
8317
8318         piocb->context_un.wait_queue = NULL;
8319         piocb->iocb_cmpl = NULL;
8320         return retval;
8321 }
8322
8323 /**
8324  * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
8325  * @phba: Pointer to HBA context object.
8326  * @pmboxq: Pointer to driver mailbox object.
8327  * @timeout: Timeout in number of seconds.
8328  *
8329  * This function issues the mailbox to firmware and waits for the
8330  * mailbox command to complete. If the mailbox command is not
8331  * completed within timeout seconds, it returns MBX_TIMEOUT.
8332  * The function waits for the mailbox completion using an
8333  * interruptible wait. If the thread is woken up due to a
8334  * signal, MBX_TIMEOUT error is returned to the caller. Caller
8335  * should not free the mailbox resources, if this function returns
8336  * MBX_TIMEOUT.
8337  * This function will sleep while waiting for mailbox completion.
8338  * So, this function should not be called from any context which
8339  * does not allow sleeping. Due to the same reason, this function
8340  * cannot be called with interrupt disabled.
8341  * This function assumes that the mailbox completion occurs while
8342  * this function sleep. So, this function cannot be called from
8343  * the worker thread which processes mailbox completion.
8344  * This function is called in the context of HBA management
8345  * applications.
8346  * This function returns MBX_SUCCESS when successful.
8347  * This function is called with no lock held.
8348  **/
8349 int
8350 lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
8351                          uint32_t timeout)
8352 {
8353         DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
8354         int retval;
8355         unsigned long flag;
8356
8357         /* The caller must leave context1 empty. */
8358         if (pmboxq->context1)
8359                 return MBX_NOT_FINISHED;
8360
8361         pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
8362         /* setup wake call as IOCB callback */
8363         pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
8364         /* setup context field to pass wait_queue pointer to wake function  */
8365         pmboxq->context1 = &done_q;
8366
8367         /* now issue the command */
8368         retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
8369
8370         if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
8371                 wait_event_interruptible_timeout(done_q,
8372                                 pmboxq->mbox_flag & LPFC_MBX_WAKE,
8373                                 timeout * HZ);
8374
8375                 spin_lock_irqsave(&phba->hbalock, flag);
8376                 pmboxq->context1 = NULL;
8377                 /*
8378                  * if LPFC_MBX_WAKE flag is set the mailbox is completed
8379                  * else do not free the resources.
8380                  */
8381                 if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
8382                         retval = MBX_SUCCESS;
8383                         lpfc_sli4_swap_str(phba, pmboxq);
8384                 } else {
8385                         retval = MBX_TIMEOUT;
8386                         pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
8387                 }
8388                 spin_unlock_irqrestore(&phba->hbalock, flag);
8389         }
8390
8391         return retval;
8392 }
8393
8394 /**
8395  * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
8396  * @phba: Pointer to HBA context.
8397  *
8398  * This function is called to shutdown the driver's mailbox sub-system.
8399  * It first marks the mailbox sub-system is in a block state to prevent
8400  * the asynchronous mailbox command from issued off the pending mailbox
8401  * command queue. If the mailbox command sub-system shutdown is due to
8402  * HBA error conditions such as EEH or ERATT, this routine shall invoke
8403  * the mailbox sub-system flush routine to forcefully bring down the
8404  * mailbox sub-system. Otherwise, if it is due to normal condition (such
8405  * as with offline or HBA function reset), this routine will wait for the
8406  * outstanding mailbox command to complete before invoking the mailbox
8407  * sub-system flush routine to gracefully bring down mailbox sub-system.
8408  **/
8409 void
8410 lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba)
8411 {
8412         struct lpfc_sli *psli = &phba->sli;
8413         uint8_t actcmd = MBX_HEARTBEAT;
8414         unsigned long timeout;
8415
8416         spin_lock_irq(&phba->hbalock);
8417         psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
8418         spin_unlock_irq(&phba->hbalock);
8419
8420         if (psli->sli_flag & LPFC_SLI_ACTIVE) {
8421                 spin_lock_irq(&phba->hbalock);
8422                 if (phba->sli.mbox_active)
8423                         actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
8424                 spin_unlock_irq(&phba->hbalock);
8425                 /* Determine how long we might wait for the active mailbox
8426                  * command to be gracefully completed by firmware.
8427                  */
8428                 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, actcmd) *
8429                                            1000) + jiffies;
8430                 while (phba->sli.mbox_active) {
8431                         /* Check active mailbox complete status every 2ms */
8432                         msleep(2);
8433                         if (time_after(jiffies, timeout))
8434                                 /* Timeout, let the mailbox flush routine to
8435                                  * forcefully release active mailbox command
8436                                  */
8437                                 break;
8438                 }
8439         }
8440         lpfc_sli_mbox_sys_flush(phba);
8441 }
8442
8443 /**
8444  * lpfc_sli_eratt_read - read sli-3 error attention events
8445  * @phba: Pointer to HBA context.
8446  *
8447  * This function is called to read the SLI3 device error attention registers
8448  * for possible error attention events. The caller must hold the hostlock
8449  * with spin_lock_irq().
8450  *
8451  * This function returns 1 when there is Error Attention in the Host Attention
8452  * Register and returns 0 otherwise.
8453  **/
8454 static int
8455 lpfc_sli_eratt_read(struct lpfc_hba *phba)
8456 {
8457         uint32_t ha_copy;
8458
8459         /* Read chip Host Attention (HA) register */
8460         if (lpfc_readl(phba->HAregaddr, &ha_copy))
8461                 goto unplug_err;
8462
8463         if (ha_copy & HA_ERATT) {
8464                 /* Read host status register to retrieve error event */
8465                 if (lpfc_sli_read_hs(phba))
8466                         goto unplug_err;
8467
8468                 /* Check if there is a deferred error condition is active */
8469                 if ((HS_FFER1 & phba->work_hs) &&
8470                     ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
8471                       HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
8472                         phba->hba_flag |= DEFER_ERATT;
8473                         /* Clear all interrupt enable conditions */
8474                         writel(0, phba->HCregaddr);
8475                         readl(phba->HCregaddr);
8476                 }
8477
8478                 /* Set the driver HA work bitmap */
8479                 phba->work_ha |= HA_ERATT;
8480                 /* Indicate polling handles this ERATT */
8481                 phba->hba_flag |= HBA_ERATT_HANDLED;
8482                 return 1;
8483         }
8484         return 0;
8485
8486 unplug_err:
8487         /* Set the driver HS work bitmap */
8488         phba->work_hs |= UNPLUG_ERR;
8489         /* Set the driver HA work bitmap */
8490         phba->work_ha |= HA_ERATT;
8491         /* Indicate polling handles this ERATT */
8492         phba->hba_flag |= HBA_ERATT_HANDLED;
8493         return 1;
8494 }
8495
8496 /**
8497  * lpfc_sli4_eratt_read - read sli-4 error attention events
8498  * @phba: Pointer to HBA context.
8499  *
8500  * This function is called to read the SLI4 device error attention registers
8501  * for possible error attention events. The caller must hold the hostlock
8502  * with spin_lock_irq().
8503  *
8504  * This function returns 1 when there is Error Attention in the Host Attention
8505  * Register and returns 0 otherwise.
8506  **/
8507 static int
8508 lpfc_sli4_eratt_read(struct lpfc_hba *phba)
8509 {
8510         uint32_t uerr_sta_hi, uerr_sta_lo;
8511         uint32_t if_type, portsmphr;
8512         struct lpfc_register portstat_reg;
8513
8514         /*
8515          * For now, use the SLI4 device internal unrecoverable error
8516          * registers for error attention. This can be changed later.
8517          */
8518         if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
8519         switch (if_type) {
8520         case LPFC_SLI_INTF_IF_TYPE_0:
8521                 if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
8522                         &uerr_sta_lo) ||
8523                         lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
8524                         &uerr_sta_hi)) {
8525                         phba->work_hs |= UNPLUG_ERR;
8526                         phba->work_ha |= HA_ERATT;
8527                         phba->hba_flag |= HBA_ERATT_HANDLED;
8528                         return 1;
8529                 }
8530                 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
8531                     (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
8532                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8533                                         "1423 HBA Unrecoverable error: "
8534                                         "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
8535                                         "ue_mask_lo_reg=0x%x, "
8536                                         "ue_mask_hi_reg=0x%x\n",
8537                                         uerr_sta_lo, uerr_sta_hi,
8538                                         phba->sli4_hba.ue_mask_lo,
8539                                         phba->sli4_hba.ue_mask_hi);
8540                         phba->work_status[0] = uerr_sta_lo;
8541                         phba->work_status[1] = uerr_sta_hi;
8542                         phba->work_ha |= HA_ERATT;
8543                         phba->hba_flag |= HBA_ERATT_HANDLED;
8544                         return 1;
8545                 }
8546                 break;
8547         case LPFC_SLI_INTF_IF_TYPE_2:
8548                 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
8549                         &portstat_reg.word0) ||
8550                         lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
8551                         &portsmphr)){
8552                         phba->work_hs |= UNPLUG_ERR;
8553                         phba->work_ha |= HA_ERATT;
8554                         phba->hba_flag |= HBA_ERATT_HANDLED;
8555                         return 1;
8556                 }
8557                 if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
8558                         phba->work_status[0] =
8559                                 readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
8560                         phba->work_status[1] =
8561                                 readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
8562                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8563                                         "2885 Port Error Detected: "
8564                                         "port status reg 0x%x, "
8565                                         "port smphr reg 0x%x, "
8566                                         "error 1=0x%x, error 2=0x%x\n",
8567                                         portstat_reg.word0,
8568                                         portsmphr,
8569                                         phba->work_status[0],
8570                                         phba->work_status[1]);
8571                         phba->work_ha |= HA_ERATT;
8572                         phba->hba_flag |= HBA_ERATT_HANDLED;
8573                         return 1;
8574                 }
8575                 break;
8576         case LPFC_SLI_INTF_IF_TYPE_1:
8577         default:
8578                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8579                                 "2886 HBA Error Attention on unsupported "
8580                                 "if type %d.", if_type);
8581                 return 1;
8582         }
8583
8584         return 0;
8585 }
8586
8587 /**
8588  * lpfc_sli_check_eratt - check error attention events
8589  * @phba: Pointer to HBA context.
8590  *
8591  * This function is called from timer soft interrupt context to check HBA's
8592  * error attention register bit for error attention events.
8593  *
8594  * This function returns 1 when there is Error Attention in the Host Attention
8595  * Register and returns 0 otherwise.
8596  **/
8597 int
8598 lpfc_sli_check_eratt(struct lpfc_hba *phba)
8599 {
8600         uint32_t ha_copy;
8601
8602         /* If somebody is waiting to handle an eratt, don't process it
8603          * here. The brdkill function will do this.
8604          */
8605         if (phba->link_flag & LS_IGNORE_ERATT)
8606                 return 0;
8607
8608         /* Check if interrupt handler handles this ERATT */
8609         spin_lock_irq(&phba->hbalock);
8610         if (phba->hba_flag & HBA_ERATT_HANDLED) {
8611                 /* Interrupt handler has handled ERATT */
8612                 spin_unlock_irq(&phba->hbalock);
8613                 return 0;
8614         }
8615
8616         /*
8617          * If there is deferred error attention, do not check for error
8618          * attention
8619          */
8620         if (unlikely(phba->hba_flag & DEFER_ERATT)) {
8621                 spin_unlock_irq(&phba->hbalock);
8622                 return 0;
8623         }
8624
8625         /* If PCI channel is offline, don't process it */
8626         if (unlikely(pci_channel_offline(phba->pcidev))) {
8627                 spin_unlock_irq(&phba->hbalock);
8628                 return 0;
8629         }
8630
8631         switch (phba->sli_rev) {
8632         case LPFC_SLI_REV2:
8633         case LPFC_SLI_REV3:
8634                 /* Read chip Host Attention (HA) register */
8635                 ha_copy = lpfc_sli_eratt_read(phba);
8636                 break;
8637         case LPFC_SLI_REV4:
8638                 /* Read device Uncoverable Error (UERR) registers */
8639                 ha_copy = lpfc_sli4_eratt_read(phba);
8640                 break;
8641         default:
8642                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8643                                 "0299 Invalid SLI revision (%d)\n",
8644                                 phba->sli_rev);
8645                 ha_copy = 0;
8646                 break;
8647         }
8648         spin_unlock_irq(&phba->hbalock);
8649
8650         return ha_copy;
8651 }
8652
8653 /**
8654  * lpfc_intr_state_check - Check device state for interrupt handling
8655  * @phba: Pointer to HBA context.
8656  *
8657  * This inline routine checks whether a device or its PCI slot is in a state
8658  * that the interrupt should be handled.
8659  *
8660  * This function returns 0 if the device or the PCI slot is in a state that
8661  * interrupt should be handled, otherwise -EIO.
8662  */
8663 static inline int
8664 lpfc_intr_state_check(struct lpfc_hba *phba)
8665 {
8666         /* If the pci channel is offline, ignore all the interrupts */
8667         if (unlikely(pci_channel_offline(phba->pcidev)))
8668                 return -EIO;
8669
8670         /* Update device level interrupt statistics */
8671         phba->sli.slistat.sli_intr++;
8672
8673         /* Ignore all interrupts during initialization. */
8674         if (unlikely(phba->link_state < LPFC_LINK_DOWN))
8675                 return -EIO;
8676
8677         return 0;
8678 }
8679
8680 /**
8681  * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
8682  * @irq: Interrupt number.
8683  * @dev_id: The device context pointer.
8684  *
8685  * This function is directly called from the PCI layer as an interrupt
8686  * service routine when device with SLI-3 interface spec is enabled with
8687  * MSI-X multi-message interrupt mode and there are slow-path events in
8688  * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
8689  * interrupt mode, this function is called as part of the device-level
8690  * interrupt handler. When the PCI slot is in error recovery or the HBA
8691  * is undergoing initialization, the interrupt handler will not process
8692  * the interrupt. The link attention and ELS ring attention events are
8693  * handled by the worker thread. The interrupt handler signals the worker
8694  * thread and returns for these events. This function is called without
8695  * any lock held. It gets the hbalock to access and update SLI data
8696  * structures.
8697  *
8698  * This function returns IRQ_HANDLED when interrupt is handled else it
8699  * returns IRQ_NONE.
8700  **/
8701 irqreturn_t
8702 lpfc_sli_sp_intr_handler(int irq, void *dev_id)
8703 {
8704         struct lpfc_hba  *phba;
8705         uint32_t ha_copy, hc_copy;
8706         uint32_t work_ha_copy;
8707         unsigned long status;
8708         unsigned long iflag;
8709         uint32_t control;
8710
8711         MAILBOX_t *mbox, *pmbox;
8712         struct lpfc_vport *vport;
8713         struct lpfc_nodelist *ndlp;
8714         struct lpfc_dmabuf *mp;
8715         LPFC_MBOXQ_t *pmb;
8716         int rc;
8717
8718         /*
8719          * Get the driver's phba structure from the dev_id and
8720          * assume the HBA is not interrupting.
8721          */
8722         phba = (struct lpfc_hba *)dev_id;
8723
8724         if (unlikely(!phba))
8725                 return IRQ_NONE;
8726
8727         /*
8728          * Stuff needs to be attented to when this function is invoked as an
8729          * individual interrupt handler in MSI-X multi-message interrupt mode
8730          */
8731         if (phba->intr_type == MSIX) {
8732                 /* Check device state for handling interrupt */
8733                 if (lpfc_intr_state_check(phba))
8734                         return IRQ_NONE;
8735                 /* Need to read HA REG for slow-path events */
8736                 spin_lock_irqsave(&phba->hbalock, iflag);
8737                 if (lpfc_readl(phba->HAregaddr, &ha_copy))
8738                         goto unplug_error;
8739                 /* If somebody is waiting to handle an eratt don't process it
8740                  * here. The brdkill function will do this.
8741                  */
8742                 if (phba->link_flag & LS_IGNORE_ERATT)
8743                         ha_copy &= ~HA_ERATT;
8744                 /* Check the need for handling ERATT in interrupt handler */
8745                 if (ha_copy & HA_ERATT) {
8746                         if (phba->hba_flag & HBA_ERATT_HANDLED)
8747                                 /* ERATT polling has handled ERATT */
8748                                 ha_copy &= ~HA_ERATT;
8749                         else
8750                                 /* Indicate interrupt handler handles ERATT */
8751                                 phba->hba_flag |= HBA_ERATT_HANDLED;
8752                 }
8753
8754                 /*
8755                  * If there is deferred error attention, do not check for any
8756                  * interrupt.
8757                  */
8758                 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
8759                         spin_unlock_irqrestore(&phba->hbalock, iflag);
8760                         return IRQ_NONE;
8761                 }
8762
8763                 /* Clear up only attention source related to slow-path */
8764                 if (lpfc_readl(phba->HCregaddr, &hc_copy))
8765                         goto unplug_error;
8766
8767                 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
8768                         HC_LAINT_ENA | HC_ERINT_ENA),
8769                         phba->HCregaddr);
8770                 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
8771                         phba->HAregaddr);
8772                 writel(hc_copy, phba->HCregaddr);
8773                 readl(phba->HAregaddr); /* flush */
8774                 spin_unlock_irqrestore(&phba->hbalock, iflag);
8775         } else
8776                 ha_copy = phba->ha_copy;
8777
8778         work_ha_copy = ha_copy & phba->work_ha_mask;
8779
8780         if (work_ha_copy) {
8781                 if (work_ha_copy & HA_LATT) {
8782                         if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
8783                                 /*
8784                                  * Turn off Link Attention interrupts
8785                                  * until CLEAR_LA done
8786                                  */
8787                                 spin_lock_irqsave(&phba->hbalock, iflag);
8788                                 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
8789                                 if (lpfc_readl(phba->HCregaddr, &control))
8790                                         goto unplug_error;
8791                                 control &= ~HC_LAINT_ENA;
8792                                 writel(control, phba->HCregaddr);
8793                                 readl(phba->HCregaddr); /* flush */
8794                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
8795                         }
8796                         else
8797                                 work_ha_copy &= ~HA_LATT;
8798                 }
8799
8800                 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
8801                         /*
8802                          * Turn off Slow Rings interrupts, LPFC_ELS_RING is
8803                          * the only slow ring.
8804                          */
8805                         status = (work_ha_copy &
8806                                 (HA_RXMASK  << (4*LPFC_ELS_RING)));
8807                         status >>= (4*LPFC_ELS_RING);
8808                         if (status & HA_RXMASK) {
8809                                 spin_lock_irqsave(&phba->hbalock, iflag);
8810                                 if (lpfc_readl(phba->HCregaddr, &control))
8811                                         goto unplug_error;
8812
8813                                 lpfc_debugfs_slow_ring_trc(phba,
8814                                 "ISR slow ring:   ctl:x%x stat:x%x isrcnt:x%x",
8815                                 control, status,
8816                                 (uint32_t)phba->sli.slistat.sli_intr);
8817
8818                                 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
8819                                         lpfc_debugfs_slow_ring_trc(phba,
8820                                                 "ISR Disable ring:"
8821                                                 "pwork:x%x hawork:x%x wait:x%x",
8822                                                 phba->work_ha, work_ha_copy,
8823                                                 (uint32_t)((unsigned long)
8824                                                 &phba->work_waitq));
8825
8826                                         control &=
8827                                             ~(HC_R0INT_ENA << LPFC_ELS_RING);
8828                                         writel(control, phba->HCregaddr);
8829                                         readl(phba->HCregaddr); /* flush */
8830                                 }
8831                                 else {
8832                                         lpfc_debugfs_slow_ring_trc(phba,
8833                                                 "ISR slow ring:   pwork:"
8834                                                 "x%x hawork:x%x wait:x%x",
8835                                                 phba->work_ha, work_ha_copy,
8836                                                 (uint32_t)((unsigned long)
8837                                                 &phba->work_waitq));
8838                                 }
8839                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
8840                         }
8841                 }
8842                 spin_lock_irqsave(&phba->hbalock, iflag);
8843                 if (work_ha_copy & HA_ERATT) {
8844                         if (lpfc_sli_read_hs(phba))
8845                                 goto unplug_error;
8846                         /*
8847                          * Check if there is a deferred error condition
8848                          * is active
8849                          */
8850                         if ((HS_FFER1 & phba->work_hs) &&
8851                                 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
8852                                   HS_FFER6 | HS_FFER7 | HS_FFER8) &
8853                                   phba->work_hs)) {
8854                                 phba->hba_flag |= DEFER_ERATT;
8855                                 /* Clear all interrupt enable conditions */
8856                                 writel(0, phba->HCregaddr);
8857                                 readl(phba->HCregaddr);
8858                         }
8859                 }
8860
8861                 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
8862                         pmb = phba->sli.mbox_active;
8863                         pmbox = &pmb->u.mb;
8864                         mbox = phba->mbox;
8865                         vport = pmb->vport;
8866
8867                         /* First check out the status word */
8868                         lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
8869                         if (pmbox->mbxOwner != OWN_HOST) {
8870                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
8871                                 /*
8872                                  * Stray Mailbox Interrupt, mbxCommand <cmd>
8873                                  * mbxStatus <status>
8874                                  */
8875                                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
8876                                                 LOG_SLI,
8877                                                 "(%d):0304 Stray Mailbox "
8878                                                 "Interrupt mbxCommand x%x "
8879                                                 "mbxStatus x%x\n",
8880                                                 (vport ? vport->vpi : 0),
8881                                                 pmbox->mbxCommand,
8882                                                 pmbox->mbxStatus);
8883                                 /* clear mailbox attention bit */
8884                                 work_ha_copy &= ~HA_MBATT;
8885                         } else {
8886                                 phba->sli.mbox_active = NULL;
8887                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
8888                                 phba->last_completion_time = jiffies;
8889                                 del_timer(&phba->sli.mbox_tmo);
8890                                 if (pmb->mbox_cmpl) {
8891                                         lpfc_sli_pcimem_bcopy(mbox, pmbox,
8892                                                         MAILBOX_CMD_SIZE);
8893                                         if (pmb->out_ext_byte_len &&
8894                                                 pmb->context2)
8895                                                 lpfc_sli_pcimem_bcopy(
8896                                                 phba->mbox_ext,
8897                                                 pmb->context2,
8898                                                 pmb->out_ext_byte_len);
8899                                 }
8900                                 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
8901                                         pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
8902
8903                                         lpfc_debugfs_disc_trc(vport,
8904                                                 LPFC_DISC_TRC_MBOX_VPORT,
8905                                                 "MBOX dflt rpi: : "
8906                                                 "status:x%x rpi:x%x",
8907                                                 (uint32_t)pmbox->mbxStatus,
8908                                                 pmbox->un.varWords[0], 0);
8909
8910                                         if (!pmbox->mbxStatus) {
8911                                                 mp = (struct lpfc_dmabuf *)
8912                                                         (pmb->context1);
8913                                                 ndlp = (struct lpfc_nodelist *)
8914                                                         pmb->context2;
8915
8916                                                 /* Reg_LOGIN of dflt RPI was
8917                                                  * successful. new lets get
8918                                                  * rid of the RPI using the
8919                                                  * same mbox buffer.
8920                                                  */
8921                                                 lpfc_unreg_login(phba,
8922                                                         vport->vpi,
8923                                                         pmbox->un.varWords[0],
8924                                                         pmb);
8925                                                 pmb->mbox_cmpl =
8926                                                         lpfc_mbx_cmpl_dflt_rpi;
8927                                                 pmb->context1 = mp;
8928                                                 pmb->context2 = ndlp;
8929                                                 pmb->vport = vport;
8930                                                 rc = lpfc_sli_issue_mbox(phba,
8931                                                                 pmb,
8932                                                                 MBX_NOWAIT);
8933                                                 if (rc != MBX_BUSY)
8934                                                         lpfc_printf_log(phba,
8935                                                         KERN_ERR,
8936                                                         LOG_MBOX | LOG_SLI,
8937                                                         "0350 rc should have"
8938                                                         "been MBX_BUSY\n");
8939                                                 if (rc != MBX_NOT_FINISHED)
8940                                                         goto send_current_mbox;
8941                                         }
8942                                 }
8943                                 spin_lock_irqsave(
8944                                                 &phba->pport->work_port_lock,
8945                                                 iflag);
8946                                 phba->pport->work_port_events &=
8947                                         ~WORKER_MBOX_TMO;
8948                                 spin_unlock_irqrestore(
8949                                                 &phba->pport->work_port_lock,
8950                                                 iflag);
8951                                 lpfc_mbox_cmpl_put(phba, pmb);
8952                         }
8953                 } else
8954                         spin_unlock_irqrestore(&phba->hbalock, iflag);
8955
8956                 if ((work_ha_copy & HA_MBATT) &&
8957                     (phba->sli.mbox_active == NULL)) {
8958 send_current_mbox:
8959                         /* Process next mailbox command if there is one */
8960                         do {
8961                                 rc = lpfc_sli_issue_mbox(phba, NULL,
8962                                                          MBX_NOWAIT);
8963                         } while (rc == MBX_NOT_FINISHED);
8964                         if (rc != MBX_SUCCESS)
8965                                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
8966                                                 LOG_SLI, "0349 rc should be "
8967                                                 "MBX_SUCCESS\n");
8968                 }
8969
8970                 spin_lock_irqsave(&phba->hbalock, iflag);
8971                 phba->work_ha |= work_ha_copy;
8972                 spin_unlock_irqrestore(&phba->hbalock, iflag);
8973                 lpfc_worker_wake_up(phba);
8974         }
8975         return IRQ_HANDLED;
8976 unplug_error:
8977         spin_unlock_irqrestore(&phba->hbalock, iflag);
8978         return IRQ_HANDLED;
8979
8980 } /* lpfc_sli_sp_intr_handler */
8981
8982 /**
8983  * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
8984  * @irq: Interrupt number.
8985  * @dev_id: The device context pointer.
8986  *
8987  * This function is directly called from the PCI layer as an interrupt
8988  * service routine when device with SLI-3 interface spec is enabled with
8989  * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
8990  * ring event in the HBA. However, when the device is enabled with either
8991  * MSI or Pin-IRQ interrupt mode, this function is called as part of the
8992  * device-level interrupt handler. When the PCI slot is in error recovery
8993  * or the HBA is undergoing initialization, the interrupt handler will not
8994  * process the interrupt. The SCSI FCP fast-path ring event are handled in
8995  * the intrrupt context. This function is called without any lock held.
8996  * It gets the hbalock to access and update SLI data structures.
8997  *
8998  * This function returns IRQ_HANDLED when interrupt is handled else it
8999  * returns IRQ_NONE.
9000  **/
9001 irqreturn_t
9002 lpfc_sli_fp_intr_handler(int irq, void *dev_id)
9003 {
9004         struct lpfc_hba  *phba;
9005         uint32_t ha_copy;
9006         unsigned long status;
9007         unsigned long iflag;
9008
9009         /* Get the driver's phba structure from the dev_id and
9010          * assume the HBA is not interrupting.
9011          */
9012         phba = (struct lpfc_hba *) dev_id;
9013
9014         if (unlikely(!phba))
9015                 return IRQ_NONE;
9016
9017         /*
9018          * Stuff needs to be attented to when this function is invoked as an
9019          * individual interrupt handler in MSI-X multi-message interrupt mode
9020          */
9021         if (phba->intr_type == MSIX) {
9022                 /* Check device state for handling interrupt */
9023                 if (lpfc_intr_state_check(phba))
9024                         return IRQ_NONE;
9025                 /* Need to read HA REG for FCP ring and other ring events */
9026                 if (lpfc_readl(phba->HAregaddr, &ha_copy))
9027                         return IRQ_HANDLED;
9028                 /* Clear up only attention source related to fast-path */
9029                 spin_lock_irqsave(&phba->hbalock, iflag);
9030                 /*
9031                  * If there is deferred error attention, do not check for
9032                  * any interrupt.
9033                  */
9034                 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
9035                         spin_unlock_irqrestore(&phba->hbalock, iflag);
9036                         return IRQ_NONE;
9037                 }
9038                 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
9039                         phba->HAregaddr);
9040                 readl(phba->HAregaddr); /* flush */
9041                 spin_unlock_irqrestore(&phba->hbalock, iflag);
9042         } else
9043                 ha_copy = phba->ha_copy;
9044
9045         /*
9046          * Process all events on FCP ring. Take the optimized path for FCP IO.
9047          */
9048         ha_copy &= ~(phba->work_ha_mask);
9049
9050         status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
9051         status >>= (4*LPFC_FCP_RING);
9052         if (status & HA_RXMASK)
9053                 lpfc_sli_handle_fast_ring_event(phba,
9054                                                 &phba->sli.ring[LPFC_FCP_RING],
9055                                                 status);
9056
9057         if (phba->cfg_multi_ring_support == 2) {
9058                 /*
9059                  * Process all events on extra ring. Take the optimized path
9060                  * for extra ring IO.
9061                  */
9062                 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
9063                 status >>= (4*LPFC_EXTRA_RING);
9064                 if (status & HA_RXMASK) {
9065                         lpfc_sli_handle_fast_ring_event(phba,
9066                                         &phba->sli.ring[LPFC_EXTRA_RING],
9067                                         status);
9068                 }
9069         }
9070         return IRQ_HANDLED;
9071 }  /* lpfc_sli_fp_intr_handler */
9072
9073 /**
9074  * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
9075  * @irq: Interrupt number.
9076  * @dev_id: The device context pointer.
9077  *
9078  * This function is the HBA device-level interrupt handler to device with
9079  * SLI-3 interface spec, called from the PCI layer when either MSI or
9080  * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
9081  * requires driver attention. This function invokes the slow-path interrupt
9082  * attention handling function and fast-path interrupt attention handling
9083  * function in turn to process the relevant HBA attention events. This
9084  * function is called without any lock held. It gets the hbalock to access
9085  * and update SLI data structures.
9086  *
9087  * This function returns IRQ_HANDLED when interrupt is handled, else it
9088  * returns IRQ_NONE.
9089  **/
9090 irqreturn_t
9091 lpfc_sli_intr_handler(int irq, void *dev_id)
9092 {
9093         struct lpfc_hba  *phba;
9094         irqreturn_t sp_irq_rc, fp_irq_rc;
9095         unsigned long status1, status2;
9096         uint32_t hc_copy;
9097
9098         /*
9099          * Get the driver's phba structure from the dev_id and
9100          * assume the HBA is not interrupting.
9101          */
9102         phba = (struct lpfc_hba *) dev_id;
9103
9104         if (unlikely(!phba))
9105                 return IRQ_NONE;
9106
9107         /* Check device state for handling interrupt */
9108         if (lpfc_intr_state_check(phba))
9109                 return IRQ_NONE;
9110
9111         spin_lock(&phba->hbalock);
9112         if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
9113                 spin_unlock(&phba->hbalock);
9114                 return IRQ_HANDLED;
9115         }
9116
9117         if (unlikely(!phba->ha_copy)) {
9118                 spin_unlock(&phba->hbalock);
9119                 return IRQ_NONE;
9120         } else if (phba->ha_copy & HA_ERATT) {
9121                 if (phba->hba_flag & HBA_ERATT_HANDLED)
9122                         /* ERATT polling has handled ERATT */
9123                         phba->ha_copy &= ~HA_ERATT;
9124                 else
9125                         /* Indicate interrupt handler handles ERATT */
9126                         phba->hba_flag |= HBA_ERATT_HANDLED;
9127         }
9128
9129         /*
9130          * If there is deferred error attention, do not check for any interrupt.
9131          */
9132         if (unlikely(phba->hba_flag & DEFER_ERATT)) {
9133                 spin_unlock(&phba->hbalock);
9134                 return IRQ_NONE;
9135         }
9136
9137         /* Clear attention sources except link and error attentions */
9138         if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
9139                 spin_unlock(&phba->hbalock);
9140                 return IRQ_HANDLED;
9141         }
9142         writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
9143                 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
9144                 phba->HCregaddr);
9145         writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
9146         writel(hc_copy, phba->HCregaddr);
9147         readl(phba->HAregaddr); /* flush */
9148         spin_unlock(&phba->hbalock);
9149
9150         /*
9151          * Invokes slow-path host attention interrupt handling as appropriate.
9152          */
9153
9154         /* status of events with mailbox and link attention */
9155         status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
9156
9157         /* status of events with ELS ring */
9158         status2 = (phba->ha_copy & (HA_RXMASK  << (4*LPFC_ELS_RING)));
9159         status2 >>= (4*LPFC_ELS_RING);
9160
9161         if (status1 || (status2 & HA_RXMASK))
9162                 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
9163         else
9164                 sp_irq_rc = IRQ_NONE;
9165
9166         /*
9167          * Invoke fast-path host attention interrupt handling as appropriate.
9168          */
9169
9170         /* status of events with FCP ring */
9171         status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
9172         status1 >>= (4*LPFC_FCP_RING);
9173
9174         /* status of events with extra ring */
9175         if (phba->cfg_multi_ring_support == 2) {
9176                 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
9177                 status2 >>= (4*LPFC_EXTRA_RING);
9178         } else
9179                 status2 = 0;
9180
9181         if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
9182                 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
9183         else
9184                 fp_irq_rc = IRQ_NONE;
9185
9186         /* Return device-level interrupt handling status */
9187         return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
9188 }  /* lpfc_sli_intr_handler */
9189
9190 /**
9191  * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
9192  * @phba: pointer to lpfc hba data structure.
9193  *
9194  * This routine is invoked by the worker thread to process all the pending
9195  * SLI4 FCP abort XRI events.
9196  **/
9197 void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
9198 {
9199         struct lpfc_cq_event *cq_event;
9200
9201         /* First, declare the fcp xri abort event has been handled */
9202         spin_lock_irq(&phba->hbalock);
9203         phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
9204         spin_unlock_irq(&phba->hbalock);
9205         /* Now, handle all the fcp xri abort events */
9206         while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
9207                 /* Get the first event from the head of the event queue */
9208                 spin_lock_irq(&phba->hbalock);
9209                 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
9210                                  cq_event, struct lpfc_cq_event, list);
9211                 spin_unlock_irq(&phba->hbalock);
9212                 /* Notify aborted XRI for FCP work queue */
9213                 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
9214                 /* Free the event processed back to the free pool */
9215                 lpfc_sli4_cq_event_release(phba, cq_event);
9216         }
9217 }
9218
9219 /**
9220  * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
9221  * @phba: pointer to lpfc hba data structure.
9222  *
9223  * This routine is invoked by the worker thread to process all the pending
9224  * SLI4 els abort xri events.
9225  **/
9226 void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
9227 {
9228         struct lpfc_cq_event *cq_event;
9229
9230         /* First, declare the els xri abort event has been handled */
9231         spin_lock_irq(&phba->hbalock);
9232         phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
9233         spin_unlock_irq(&phba->hbalock);
9234         /* Now, handle all the els xri abort events */
9235         while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
9236                 /* Get the first event from the head of the event queue */
9237                 spin_lock_irq(&phba->hbalock);
9238                 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
9239                                  cq_event, struct lpfc_cq_event, list);
9240                 spin_unlock_irq(&phba->hbalock);
9241                 /* Notify aborted XRI for ELS work queue */
9242                 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
9243                 /* Free the event processed back to the free pool */
9244                 lpfc_sli4_cq_event_release(phba, cq_event);
9245         }
9246 }
9247
9248 /**
9249  * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
9250  * @phba: pointer to lpfc hba data structure
9251  * @pIocbIn: pointer to the rspiocbq
9252  * @pIocbOut: pointer to the cmdiocbq
9253  * @wcqe: pointer to the complete wcqe
9254  *
9255  * This routine transfers the fields of a command iocbq to a response iocbq
9256  * by copying all the IOCB fields from command iocbq and transferring the
9257  * completion status information from the complete wcqe.
9258  **/
9259 static void
9260 lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
9261                               struct lpfc_iocbq *pIocbIn,
9262                               struct lpfc_iocbq *pIocbOut,
9263                               struct lpfc_wcqe_complete *wcqe)
9264 {
9265         unsigned long iflags;
9266         size_t offset = offsetof(struct lpfc_iocbq, iocb);
9267
9268         memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
9269                sizeof(struct lpfc_iocbq) - offset);
9270         /* Map WCQE parameters into irspiocb parameters */
9271         pIocbIn->iocb.ulpStatus = bf_get(lpfc_wcqe_c_status, wcqe);
9272         if (pIocbOut->iocb_flag & LPFC_IO_FCP)
9273                 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
9274                         pIocbIn->iocb.un.fcpi.fcpi_parm =
9275                                         pIocbOut->iocb.un.fcpi.fcpi_parm -
9276                                         wcqe->total_data_placed;
9277                 else
9278                         pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
9279         else {
9280                 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
9281                 pIocbIn->iocb.un.genreq64.bdl.bdeSize = wcqe->total_data_placed;
9282         }
9283
9284         /* Pick up HBA exchange busy condition */
9285         if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
9286                 spin_lock_irqsave(&phba->hbalock, iflags);
9287                 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
9288                 spin_unlock_irqrestore(&phba->hbalock, iflags);
9289         }
9290 }
9291
9292 /**
9293  * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
9294  * @phba: Pointer to HBA context object.
9295  * @wcqe: Pointer to work-queue completion queue entry.
9296  *
9297  * This routine handles an ELS work-queue completion event and construct
9298  * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
9299  * discovery engine to handle.
9300  *
9301  * Return: Pointer to the receive IOCBQ, NULL otherwise.
9302  **/
9303 static struct lpfc_iocbq *
9304 lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
9305                                struct lpfc_iocbq *irspiocbq)
9306 {
9307         struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
9308         struct lpfc_iocbq *cmdiocbq;
9309         struct lpfc_wcqe_complete *wcqe;
9310         unsigned long iflags;
9311
9312         wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
9313         spin_lock_irqsave(&phba->hbalock, iflags);
9314         pring->stats.iocb_event++;
9315         /* Look up the ELS command IOCB and create pseudo response IOCB */
9316         cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
9317                                 bf_get(lpfc_wcqe_c_request_tag, wcqe));
9318         spin_unlock_irqrestore(&phba->hbalock, iflags);
9319
9320         if (unlikely(!cmdiocbq)) {
9321                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9322                                 "0386 ELS complete with no corresponding "
9323                                 "cmdiocb: iotag (%d)\n",
9324                                 bf_get(lpfc_wcqe_c_request_tag, wcqe));
9325                 lpfc_sli_release_iocbq(phba, irspiocbq);
9326                 return NULL;
9327         }
9328
9329         /* Fake the irspiocbq and copy necessary response information */
9330         lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
9331
9332         return irspiocbq;
9333 }
9334
9335 /**
9336  * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
9337  * @phba: Pointer to HBA context object.
9338  * @cqe: Pointer to mailbox completion queue entry.
9339  *
9340  * This routine process a mailbox completion queue entry with asynchrous
9341  * event.
9342  *
9343  * Return: true if work posted to worker thread, otherwise false.
9344  **/
9345 static bool
9346 lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
9347 {
9348         struct lpfc_cq_event *cq_event;
9349         unsigned long iflags;
9350
9351         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9352                         "0392 Async Event: word0:x%x, word1:x%x, "
9353                         "word2:x%x, word3:x%x\n", mcqe->word0,
9354                         mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
9355
9356         /* Allocate a new internal CQ_EVENT entry */
9357         cq_event = lpfc_sli4_cq_event_alloc(phba);
9358         if (!cq_event) {
9359                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9360                                 "0394 Failed to allocate CQ_EVENT entry\n");
9361                 return false;
9362         }
9363
9364         /* Move the CQE into an asynchronous event entry */
9365         memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
9366         spin_lock_irqsave(&phba->hbalock, iflags);
9367         list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
9368         /* Set the async event flag */
9369         phba->hba_flag |= ASYNC_EVENT;
9370         spin_unlock_irqrestore(&phba->hbalock, iflags);
9371
9372         return true;
9373 }
9374
9375 /**
9376  * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
9377  * @phba: Pointer to HBA context object.
9378  * @cqe: Pointer to mailbox completion queue entry.
9379  *
9380  * This routine process a mailbox completion queue entry with mailbox
9381  * completion event.
9382  *
9383  * Return: true if work posted to worker thread, otherwise false.
9384  **/
9385 static bool
9386 lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
9387 {
9388         uint32_t mcqe_status;
9389         MAILBOX_t *mbox, *pmbox;
9390         struct lpfc_mqe *mqe;
9391         struct lpfc_vport *vport;
9392         struct lpfc_nodelist *ndlp;
9393         struct lpfc_dmabuf *mp;
9394         unsigned long iflags;
9395         LPFC_MBOXQ_t *pmb;
9396         bool workposted = false;
9397         int rc;
9398
9399         /* If not a mailbox complete MCQE, out by checking mailbox consume */
9400         if (!bf_get(lpfc_trailer_completed, mcqe))
9401                 goto out_no_mqe_complete;
9402
9403         /* Get the reference to the active mbox command */
9404         spin_lock_irqsave(&phba->hbalock, iflags);
9405         pmb = phba->sli.mbox_active;
9406         if (unlikely(!pmb)) {
9407                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
9408                                 "1832 No pending MBOX command to handle\n");
9409                 spin_unlock_irqrestore(&phba->hbalock, iflags);
9410                 goto out_no_mqe_complete;
9411         }
9412         spin_unlock_irqrestore(&phba->hbalock, iflags);
9413         mqe = &pmb->u.mqe;
9414         pmbox = (MAILBOX_t *)&pmb->u.mqe;
9415         mbox = phba->mbox;
9416         vport = pmb->vport;
9417
9418         /* Reset heartbeat timer */
9419         phba->last_completion_time = jiffies;
9420         del_timer(&phba->sli.mbox_tmo);
9421
9422         /* Move mbox data to caller's mailbox region, do endian swapping */
9423         if (pmb->mbox_cmpl && mbox)
9424                 lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
9425         /* Set the mailbox status with SLI4 range 0x4000 */
9426         mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
9427         if (mcqe_status != MB_CQE_STATUS_SUCCESS)
9428                 bf_set(lpfc_mqe_status, mqe,
9429                        (LPFC_MBX_ERROR_RANGE | mcqe_status));
9430
9431         if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
9432                 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
9433                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
9434                                       "MBOX dflt rpi: status:x%x rpi:x%x",
9435                                       mcqe_status,
9436                                       pmbox->un.varWords[0], 0);
9437                 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
9438                         mp = (struct lpfc_dmabuf *)(pmb->context1);
9439                         ndlp = (struct lpfc_nodelist *)pmb->context2;
9440                         /* Reg_LOGIN of dflt RPI was successful. Now lets get
9441                          * RID of the PPI using the same mbox buffer.
9442                          */
9443                         lpfc_unreg_login(phba, vport->vpi,
9444                                          pmbox->un.varWords[0], pmb);
9445                         pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
9446                         pmb->context1 = mp;
9447                         pmb->context2 = ndlp;
9448                         pmb->vport = vport;
9449                         rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
9450                         if (rc != MBX_BUSY)
9451                                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
9452                                                 LOG_SLI, "0385 rc should "
9453                                                 "have been MBX_BUSY\n");
9454                         if (rc != MBX_NOT_FINISHED)
9455                                 goto send_current_mbox;
9456                 }
9457         }
9458         spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
9459         phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
9460         spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
9461
9462         /* There is mailbox completion work to do */
9463         spin_lock_irqsave(&phba->hbalock, iflags);
9464         __lpfc_mbox_cmpl_put(phba, pmb);
9465         phba->work_ha |= HA_MBATT;
9466         spin_unlock_irqrestore(&phba->hbalock, iflags);
9467         workposted = true;
9468
9469 send_current_mbox:
9470         spin_lock_irqsave(&phba->hbalock, iflags);
9471         /* Release the mailbox command posting token */
9472         phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
9473         /* Setting active mailbox pointer need to be in sync to flag clear */
9474         phba->sli.mbox_active = NULL;
9475         spin_unlock_irqrestore(&phba->hbalock, iflags);
9476         /* Wake up worker thread to post the next pending mailbox command */
9477         lpfc_worker_wake_up(phba);
9478 out_no_mqe_complete:
9479         if (bf_get(lpfc_trailer_consumed, mcqe))
9480                 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
9481         return workposted;
9482 }
9483
9484 /**
9485  * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
9486  * @phba: Pointer to HBA context object.
9487  * @cqe: Pointer to mailbox completion queue entry.
9488  *
9489  * This routine process a mailbox completion queue entry, it invokes the
9490  * proper mailbox complete handling or asynchrous event handling routine
9491  * according to the MCQE's async bit.
9492  *
9493  * Return: true if work posted to worker thread, otherwise false.
9494  **/
9495 static bool
9496 lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
9497 {
9498         struct lpfc_mcqe mcqe;
9499         bool workposted;
9500
9501         /* Copy the mailbox MCQE and convert endian order as needed */
9502         lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
9503
9504         /* Invoke the proper event handling routine */
9505         if (!bf_get(lpfc_trailer_async, &mcqe))
9506                 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
9507         else
9508                 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
9509         return workposted;
9510 }
9511
9512 /**
9513  * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
9514  * @phba: Pointer to HBA context object.
9515  * @wcqe: Pointer to work-queue completion queue entry.
9516  *
9517  * This routine handles an ELS work-queue completion event.
9518  *
9519  * Return: true if work posted to worker thread, otherwise false.
9520  **/
9521 static bool
9522 lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba,
9523                              struct lpfc_wcqe_complete *wcqe)
9524 {
9525         struct lpfc_iocbq *irspiocbq;
9526         unsigned long iflags;
9527         struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
9528
9529         /* Get an irspiocbq for later ELS response processing use */
9530         irspiocbq = lpfc_sli_get_iocbq(phba);
9531         if (!irspiocbq) {
9532                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9533                         "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
9534                         "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
9535                         pring->txq_cnt, phba->iocb_cnt,
9536                         phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt,
9537                         phba->sli.ring[LPFC_ELS_RING].txcmplq_cnt);
9538                 return false;
9539         }
9540
9541         /* Save off the slow-path queue event for work thread to process */
9542         memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
9543         spin_lock_irqsave(&phba->hbalock, iflags);
9544         list_add_tail(&irspiocbq->cq_event.list,
9545                       &phba->sli4_hba.sp_queue_event);
9546         phba->hba_flag |= HBA_SP_QUEUE_EVT;
9547         spin_unlock_irqrestore(&phba->hbalock, iflags);
9548
9549         return true;
9550 }
9551
9552 /**
9553  * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
9554  * @phba: Pointer to HBA context object.
9555  * @wcqe: Pointer to work-queue completion queue entry.
9556  *
9557  * This routine handles slow-path WQ entry comsumed event by invoking the
9558  * proper WQ release routine to the slow-path WQ.
9559  **/
9560 static void
9561 lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
9562                              struct lpfc_wcqe_release *wcqe)
9563 {
9564         /* Check for the slow-path ELS work queue */
9565         if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
9566                 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
9567                                      bf_get(lpfc_wcqe_r_wqe_index, wcqe));
9568         else
9569                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9570                                 "2579 Slow-path wqe consume event carries "
9571                                 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
9572                                 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
9573                                 phba->sli4_hba.els_wq->queue_id);
9574 }
9575
9576 /**
9577  * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
9578  * @phba: Pointer to HBA context object.
9579  * @cq: Pointer to a WQ completion queue.
9580  * @wcqe: Pointer to work-queue completion queue entry.
9581  *
9582  * This routine handles an XRI abort event.
9583  *
9584  * Return: true if work posted to worker thread, otherwise false.
9585  **/
9586 static bool
9587 lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
9588                                    struct lpfc_queue *cq,
9589                                    struct sli4_wcqe_xri_aborted *wcqe)
9590 {
9591         bool workposted = false;
9592         struct lpfc_cq_event *cq_event;
9593         unsigned long iflags;
9594
9595         /* Allocate a new internal CQ_EVENT entry */
9596         cq_event = lpfc_sli4_cq_event_alloc(phba);
9597         if (!cq_event) {
9598                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9599                                 "0602 Failed to allocate CQ_EVENT entry\n");
9600                 return false;
9601         }
9602
9603         /* Move the CQE into the proper xri abort event list */
9604         memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
9605         switch (cq->subtype) {
9606         case LPFC_FCP:
9607                 spin_lock_irqsave(&phba->hbalock, iflags);
9608                 list_add_tail(&cq_event->list,
9609                               &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
9610                 /* Set the fcp xri abort event flag */
9611                 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
9612                 spin_unlock_irqrestore(&phba->hbalock, iflags);
9613                 workposted = true;
9614                 break;
9615         case LPFC_ELS:
9616                 spin_lock_irqsave(&phba->hbalock, iflags);
9617                 list_add_tail(&cq_event->list,
9618                               &phba->sli4_hba.sp_els_xri_aborted_work_queue);
9619                 /* Set the els xri abort event flag */
9620                 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
9621                 spin_unlock_irqrestore(&phba->hbalock, iflags);
9622                 workposted = true;
9623                 break;
9624         default:
9625                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9626                                 "0603 Invalid work queue CQE subtype (x%x)\n",
9627                                 cq->subtype);
9628                 workposted = false;
9629                 break;
9630         }
9631         return workposted;
9632 }
9633
9634 /**
9635  * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
9636  * @phba: Pointer to HBA context object.
9637  * @rcqe: Pointer to receive-queue completion queue entry.
9638  *
9639  * This routine process a receive-queue completion queue entry.
9640  *
9641  * Return: true if work posted to worker thread, otherwise false.
9642  **/
9643 static bool
9644 lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
9645 {
9646         bool workposted = false;
9647         struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
9648         struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
9649         struct hbq_dmabuf *dma_buf;
9650         uint32_t status;
9651         unsigned long iflags;
9652
9653         if (bf_get(lpfc_rcqe_rq_id, rcqe) != hrq->queue_id)
9654                 goto out;
9655
9656         status = bf_get(lpfc_rcqe_status, rcqe);
9657         switch (status) {
9658         case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
9659                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9660                                 "2537 Receive Frame Truncated!!\n");
9661         case FC_STATUS_RQ_SUCCESS:
9662                 lpfc_sli4_rq_release(hrq, drq);
9663                 spin_lock_irqsave(&phba->hbalock, iflags);
9664                 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
9665                 if (!dma_buf) {
9666                         spin_unlock_irqrestore(&phba->hbalock, iflags);
9667                         goto out;
9668                 }
9669                 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
9670                 /* save off the frame for the word thread to process */
9671                 list_add_tail(&dma_buf->cq_event.list,
9672                               &phba->sli4_hba.sp_queue_event);
9673                 /* Frame received */
9674                 phba->hba_flag |= HBA_SP_QUEUE_EVT;
9675                 spin_unlock_irqrestore(&phba->hbalock, iflags);
9676                 workposted = true;
9677                 break;
9678         case FC_STATUS_INSUFF_BUF_NEED_BUF:
9679         case FC_STATUS_INSUFF_BUF_FRM_DISC:
9680                 /* Post more buffers if possible */
9681                 spin_lock_irqsave(&phba->hbalock, iflags);
9682                 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
9683                 spin_unlock_irqrestore(&phba->hbalock, iflags);
9684                 workposted = true;
9685                 break;
9686         }
9687 out:
9688         return workposted;
9689 }
9690
9691 /**
9692  * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
9693  * @phba: Pointer to HBA context object.
9694  * @cq: Pointer to the completion queue.
9695  * @wcqe: Pointer to a completion queue entry.
9696  *
9697  * This routine process a slow-path work-queue or receive queue completion queue
9698  * entry.
9699  *
9700  * Return: true if work posted to worker thread, otherwise false.
9701  **/
9702 static bool
9703 lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
9704                          struct lpfc_cqe *cqe)
9705 {
9706         struct lpfc_cqe cqevt;
9707         bool workposted = false;
9708
9709         /* Copy the work queue CQE and convert endian order if needed */
9710         lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
9711
9712         /* Check and process for different type of WCQE and dispatch */
9713         switch (bf_get(lpfc_cqe_code, &cqevt)) {
9714         case CQE_CODE_COMPL_WQE:
9715                 /* Process the WQ/RQ complete event */
9716                 phba->last_completion_time = jiffies;
9717                 workposted = lpfc_sli4_sp_handle_els_wcqe(phba,
9718                                 (struct lpfc_wcqe_complete *)&cqevt);
9719                 break;
9720         case CQE_CODE_RELEASE_WQE:
9721                 /* Process the WQ release event */
9722                 lpfc_sli4_sp_handle_rel_wcqe(phba,
9723                                 (struct lpfc_wcqe_release *)&cqevt);
9724                 break;
9725         case CQE_CODE_XRI_ABORTED:
9726                 /* Process the WQ XRI abort event */
9727                 phba->last_completion_time = jiffies;
9728                 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
9729                                 (struct sli4_wcqe_xri_aborted *)&cqevt);
9730                 break;
9731         case CQE_CODE_RECEIVE:
9732                 /* Process the RQ event */
9733                 phba->last_completion_time = jiffies;
9734                 workposted = lpfc_sli4_sp_handle_rcqe(phba,
9735                                 (struct lpfc_rcqe *)&cqevt);
9736                 break;
9737         default:
9738                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9739                                 "0388 Not a valid WCQE code: x%x\n",
9740                                 bf_get(lpfc_cqe_code, &cqevt));
9741                 break;
9742         }
9743         return workposted;
9744 }
9745
9746 /**
9747  * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
9748  * @phba: Pointer to HBA context object.
9749  * @eqe: Pointer to fast-path event queue entry.
9750  *
9751  * This routine process a event queue entry from the slow-path event queue.
9752  * It will check the MajorCode and MinorCode to determine this is for a
9753  * completion event on a completion queue, if not, an error shall be logged
9754  * and just return. Otherwise, it will get to the corresponding completion
9755  * queue and process all the entries on that completion queue, rearm the
9756  * completion queue, and then return.
9757  *
9758  **/
9759 static void
9760 lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
9761 {
9762         struct lpfc_queue *cq = NULL, *childq, *speq;
9763         struct lpfc_cqe *cqe;
9764         bool workposted = false;
9765         int ecount = 0;
9766         uint16_t cqid;
9767
9768         if (bf_get_le32(lpfc_eqe_major_code, eqe) != 0) {
9769                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9770                                 "0359 Not a valid slow-path completion "
9771                                 "event: majorcode=x%x, minorcode=x%x\n",
9772                                 bf_get_le32(lpfc_eqe_major_code, eqe),
9773                                 bf_get_le32(lpfc_eqe_minor_code, eqe));
9774                 return;
9775         }
9776
9777         /* Get the reference to the corresponding CQ */
9778         cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
9779
9780         /* Search for completion queue pointer matching this cqid */
9781         speq = phba->sli4_hba.sp_eq;
9782         list_for_each_entry(childq, &speq->child_list, list) {
9783                 if (childq->queue_id == cqid) {
9784                         cq = childq;
9785                         break;
9786                 }
9787         }
9788         if (unlikely(!cq)) {
9789                 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
9790                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9791                                         "0365 Slow-path CQ identifier "
9792                                         "(%d) does not exist\n", cqid);
9793                 return;
9794         }
9795
9796         /* Process all the entries to the CQ */
9797         switch (cq->type) {
9798         case LPFC_MCQ:
9799                 while ((cqe = lpfc_sli4_cq_get(cq))) {
9800                         workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
9801                         if (!(++ecount % LPFC_GET_QE_REL_INT))
9802                                 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
9803                 }
9804                 break;
9805         case LPFC_WCQ:
9806                 while ((cqe = lpfc_sli4_cq_get(cq))) {
9807                         if (cq->subtype == LPFC_FCP)
9808                                 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq,
9809                                                                        cqe);
9810                         else
9811                                 workposted |= lpfc_sli4_sp_handle_cqe(phba, cq,
9812                                                                       cqe);
9813                         if (!(++ecount % LPFC_GET_QE_REL_INT))
9814                                 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
9815                 }
9816                 break;
9817         default:
9818                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9819                                 "0370 Invalid completion queue type (%d)\n",
9820                                 cq->type);
9821                 return;
9822         }
9823
9824         /* Catch the no cq entry condition, log an error */
9825         if (unlikely(ecount == 0))
9826                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9827                                 "0371 No entry from the CQ: identifier "
9828                                 "(x%x), type (%d)\n", cq->queue_id, cq->type);
9829
9830         /* In any case, flash and re-arm the RCQ */
9831         lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
9832
9833         /* wake up worker thread if there are works to be done */
9834         if (workposted)
9835                 lpfc_worker_wake_up(phba);
9836 }
9837
9838 /**
9839  * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
9840  * @eqe: Pointer to fast-path completion queue entry.
9841  *
9842  * This routine process a fast-path work queue completion entry from fast-path
9843  * event queue for FCP command response completion.
9844  **/
9845 static void
9846 lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba,
9847                              struct lpfc_wcqe_complete *wcqe)
9848 {
9849         struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
9850         struct lpfc_iocbq *cmdiocbq;
9851         struct lpfc_iocbq irspiocbq;
9852         unsigned long iflags;
9853
9854         spin_lock_irqsave(&phba->hbalock, iflags);
9855         pring->stats.iocb_event++;
9856         spin_unlock_irqrestore(&phba->hbalock, iflags);
9857
9858         /* Check for response status */
9859         if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
9860                 /* If resource errors reported from HBA, reduce queue
9861                  * depth of the SCSI device.
9862                  */
9863                 if ((bf_get(lpfc_wcqe_c_status, wcqe) ==
9864                      IOSTAT_LOCAL_REJECT) &&
9865                     (wcqe->parameter == IOERR_NO_RESOURCES)) {
9866                         phba->lpfc_rampdown_queue_depth(phba);
9867                 }
9868                 /* Log the error status */
9869                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9870                                 "0373 FCP complete error: status=x%x, "
9871                                 "hw_status=x%x, total_data_specified=%d, "
9872                                 "parameter=x%x, word3=x%x\n",
9873                                 bf_get(lpfc_wcqe_c_status, wcqe),
9874                                 bf_get(lpfc_wcqe_c_hw_status, wcqe),
9875                                 wcqe->total_data_placed, wcqe->parameter,
9876                                 wcqe->word3);
9877         }
9878
9879         /* Look up the FCP command IOCB and create pseudo response IOCB */
9880         spin_lock_irqsave(&phba->hbalock, iflags);
9881         cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
9882                                 bf_get(lpfc_wcqe_c_request_tag, wcqe));
9883         spin_unlock_irqrestore(&phba->hbalock, iflags);
9884         if (unlikely(!cmdiocbq)) {
9885                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9886                                 "0374 FCP complete with no corresponding "
9887                                 "cmdiocb: iotag (%d)\n",
9888                                 bf_get(lpfc_wcqe_c_request_tag, wcqe));
9889                 return;
9890         }
9891         if (unlikely(!cmdiocbq->iocb_cmpl)) {
9892                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9893                                 "0375 FCP cmdiocb not callback function "
9894                                 "iotag: (%d)\n",
9895                                 bf_get(lpfc_wcqe_c_request_tag, wcqe));
9896                 return;
9897         }
9898
9899         /* Fake the irspiocb and copy necessary response information */
9900         lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
9901
9902         if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
9903                 spin_lock_irqsave(&phba->hbalock, iflags);
9904                 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
9905                 spin_unlock_irqrestore(&phba->hbalock, iflags);
9906         }
9907
9908         /* Pass the cmd_iocb and the rsp state to the upper layer */
9909         (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
9910 }
9911
9912 /**
9913  * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
9914  * @phba: Pointer to HBA context object.
9915  * @cq: Pointer to completion queue.
9916  * @wcqe: Pointer to work-queue completion queue entry.
9917  *
9918  * This routine handles an fast-path WQ entry comsumed event by invoking the
9919  * proper WQ release routine to the slow-path WQ.
9920  **/
9921 static void
9922 lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
9923                              struct lpfc_wcqe_release *wcqe)
9924 {
9925         struct lpfc_queue *childwq;
9926         bool wqid_matched = false;
9927         uint16_t fcp_wqid;
9928
9929         /* Check for fast-path FCP work queue release */
9930         fcp_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
9931         list_for_each_entry(childwq, &cq->child_list, list) {
9932                 if (childwq->queue_id == fcp_wqid) {
9933                         lpfc_sli4_wq_release(childwq,
9934                                         bf_get(lpfc_wcqe_r_wqe_index, wcqe));
9935                         wqid_matched = true;
9936                         break;
9937                 }
9938         }
9939         /* Report warning log message if no match found */
9940         if (wqid_matched != true)
9941                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9942                                 "2580 Fast-path wqe consume event carries "
9943                                 "miss-matched qid: wcqe-qid=x%x\n", fcp_wqid);
9944 }
9945
9946 /**
9947  * lpfc_sli4_fp_handle_wcqe - Process fast-path work queue completion entry
9948  * @cq: Pointer to the completion queue.
9949  * @eqe: Pointer to fast-path completion queue entry.
9950  *
9951  * This routine process a fast-path work queue completion entry from fast-path
9952  * event queue for FCP command response completion.
9953  **/
9954 static int
9955 lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
9956                          struct lpfc_cqe *cqe)
9957 {
9958         struct lpfc_wcqe_release wcqe;
9959         bool workposted = false;
9960
9961         /* Copy the work queue CQE and convert endian order if needed */
9962         lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
9963
9964         /* Check and process for different type of WCQE and dispatch */
9965         switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
9966         case CQE_CODE_COMPL_WQE:
9967                 /* Process the WQ complete event */
9968                 phba->last_completion_time = jiffies;
9969                 lpfc_sli4_fp_handle_fcp_wcqe(phba,
9970                                 (struct lpfc_wcqe_complete *)&wcqe);
9971                 break;
9972         case CQE_CODE_RELEASE_WQE:
9973                 /* Process the WQ release event */
9974                 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
9975                                 (struct lpfc_wcqe_release *)&wcqe);
9976                 break;
9977         case CQE_CODE_XRI_ABORTED:
9978                 /* Process the WQ XRI abort event */
9979                 phba->last_completion_time = jiffies;
9980                 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
9981                                 (struct sli4_wcqe_xri_aborted *)&wcqe);
9982                 break;
9983         default:
9984                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9985                                 "0144 Not a valid WCQE code: x%x\n",
9986                                 bf_get(lpfc_wcqe_c_code, &wcqe));
9987                 break;
9988         }
9989         return workposted;
9990 }
9991
9992 /**
9993  * lpfc_sli4_fp_handle_eqe - Process a fast-path event queue entry
9994  * @phba: Pointer to HBA context object.
9995  * @eqe: Pointer to fast-path event queue entry.
9996  *
9997  * This routine process a event queue entry from the fast-path event queue.
9998  * It will check the MajorCode and MinorCode to determine this is for a
9999  * completion event on a completion queue, if not, an error shall be logged
10000  * and just return. Otherwise, it will get to the corresponding completion
10001  * queue and process all the entries on the completion queue, rearm the
10002  * completion queue, and then return.
10003  **/
10004 static void
10005 lpfc_sli4_fp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
10006                         uint32_t fcp_cqidx)
10007 {
10008         struct lpfc_queue *cq;
10009         struct lpfc_cqe *cqe;
10010         bool workposted = false;
10011         uint16_t cqid;
10012         int ecount = 0;
10013
10014         if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
10015                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10016                                 "0366 Not a valid fast-path completion "
10017                                 "event: majorcode=x%x, minorcode=x%x\n",
10018                                 bf_get_le32(lpfc_eqe_major_code, eqe),
10019                                 bf_get_le32(lpfc_eqe_minor_code, eqe));
10020                 return;
10021         }
10022
10023         cq = phba->sli4_hba.fcp_cq[fcp_cqidx];
10024         if (unlikely(!cq)) {
10025                 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
10026                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10027                                         "0367 Fast-path completion queue "
10028                                         "does not exist\n");
10029                 return;
10030         }
10031
10032         /* Get the reference to the corresponding CQ */
10033         cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
10034         if (unlikely(cqid != cq->queue_id)) {
10035                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10036                                 "0368 Miss-matched fast-path completion "
10037                                 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
10038                                 cqid, cq->queue_id);
10039                 return;
10040         }
10041
10042         /* Process all the entries to the CQ */
10043         while ((cqe = lpfc_sli4_cq_get(cq))) {
10044                 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
10045                 if (!(++ecount % LPFC_GET_QE_REL_INT))
10046                         lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
10047         }
10048
10049         /* Catch the no cq entry condition */
10050         if (unlikely(ecount == 0))
10051                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10052                                 "0369 No entry from fast-path completion "
10053                                 "queue fcpcqid=%d\n", cq->queue_id);
10054
10055         /* In any case, flash and re-arm the CQ */
10056         lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
10057
10058         /* wake up worker thread if there are works to be done */
10059         if (workposted)
10060                 lpfc_worker_wake_up(phba);
10061 }
10062
10063 static void
10064 lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
10065 {
10066         struct lpfc_eqe *eqe;
10067
10068         /* walk all the EQ entries and drop on the floor */
10069         while ((eqe = lpfc_sli4_eq_get(eq)))
10070                 ;
10071
10072         /* Clear and re-arm the EQ */
10073         lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
10074 }
10075
10076 /**
10077  * lpfc_sli4_sp_intr_handler - Slow-path interrupt handler to SLI-4 device
10078  * @irq: Interrupt number.
10079  * @dev_id: The device context pointer.
10080  *
10081  * This function is directly called from the PCI layer as an interrupt
10082  * service routine when device with SLI-4 interface spec is enabled with
10083  * MSI-X multi-message interrupt mode and there are slow-path events in
10084  * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
10085  * interrupt mode, this function is called as part of the device-level
10086  * interrupt handler. When the PCI slot is in error recovery or the HBA is
10087  * undergoing initialization, the interrupt handler will not process the
10088  * interrupt. The link attention and ELS ring attention events are handled
10089  * by the worker thread. The interrupt handler signals the worker thread
10090  * and returns for these events. This function is called without any lock
10091  * held. It gets the hbalock to access and update SLI data structures.
10092  *
10093  * This function returns IRQ_HANDLED when interrupt is handled else it
10094  * returns IRQ_NONE.
10095  **/
10096 irqreturn_t
10097 lpfc_sli4_sp_intr_handler(int irq, void *dev_id)
10098 {
10099         struct lpfc_hba *phba;
10100         struct lpfc_queue *speq;
10101         struct lpfc_eqe *eqe;
10102         unsigned long iflag;
10103         int ecount = 0;
10104
10105         /*
10106          * Get the driver's phba structure from the dev_id
10107          */
10108         phba = (struct lpfc_hba *)dev_id;
10109
10110         if (unlikely(!phba))
10111                 return IRQ_NONE;
10112
10113         /* Get to the EQ struct associated with this vector */
10114         speq = phba->sli4_hba.sp_eq;
10115
10116         /* Check device state for handling interrupt */
10117         if (unlikely(lpfc_intr_state_check(phba))) {
10118                 /* Check again for link_state with lock held */
10119                 spin_lock_irqsave(&phba->hbalock, iflag);
10120                 if (phba->link_state < LPFC_LINK_DOWN)
10121                         /* Flush, clear interrupt, and rearm the EQ */
10122                         lpfc_sli4_eq_flush(phba, speq);
10123                 spin_unlock_irqrestore(&phba->hbalock, iflag);
10124                 return IRQ_NONE;
10125         }
10126
10127         /*
10128          * Process all the event on FCP slow-path EQ
10129          */
10130         while ((eqe = lpfc_sli4_eq_get(speq))) {
10131                 lpfc_sli4_sp_handle_eqe(phba, eqe);
10132                 if (!(++ecount % LPFC_GET_QE_REL_INT))
10133                         lpfc_sli4_eq_release(speq, LPFC_QUEUE_NOARM);
10134         }
10135
10136         /* Always clear and re-arm the slow-path EQ */
10137         lpfc_sli4_eq_release(speq, LPFC_QUEUE_REARM);
10138
10139         /* Catch the no cq entry condition */
10140         if (unlikely(ecount == 0)) {
10141                 if (phba->intr_type == MSIX)
10142                         /* MSI-X treated interrupt served as no EQ share INT */
10143                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
10144                                         "0357 MSI-X interrupt with no EQE\n");
10145                 else
10146                         /* Non MSI-X treated on interrupt as EQ share INT */
10147                         return IRQ_NONE;
10148         }
10149
10150         return IRQ_HANDLED;
10151 } /* lpfc_sli4_sp_intr_handler */
10152
10153 /**
10154  * lpfc_sli4_fp_intr_handler - Fast-path interrupt handler to SLI-4 device
10155  * @irq: Interrupt number.
10156  * @dev_id: The device context pointer.
10157  *
10158  * This function is directly called from the PCI layer as an interrupt
10159  * service routine when device with SLI-4 interface spec is enabled with
10160  * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
10161  * ring event in the HBA. However, when the device is enabled with either
10162  * MSI or Pin-IRQ interrupt mode, this function is called as part of the
10163  * device-level interrupt handler. When the PCI slot is in error recovery
10164  * or the HBA is undergoing initialization, the interrupt handler will not
10165  * process the interrupt. The SCSI FCP fast-path ring event are handled in
10166  * the intrrupt context. This function is called without any lock held.
10167  * It gets the hbalock to access and update SLI data structures. Note that,
10168  * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
10169  * equal to that of FCP CQ index.
10170  *
10171  * This function returns IRQ_HANDLED when interrupt is handled else it
10172  * returns IRQ_NONE.
10173  **/
10174 irqreturn_t
10175 lpfc_sli4_fp_intr_handler(int irq, void *dev_id)
10176 {
10177         struct lpfc_hba *phba;
10178         struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
10179         struct lpfc_queue *fpeq;
10180         struct lpfc_eqe *eqe;
10181         unsigned long iflag;
10182         int ecount = 0;
10183         uint32_t fcp_eqidx;
10184
10185         /* Get the driver's phba structure from the dev_id */
10186         fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
10187         phba = fcp_eq_hdl->phba;
10188         fcp_eqidx = fcp_eq_hdl->idx;
10189
10190         if (unlikely(!phba))
10191                 return IRQ_NONE;
10192
10193         /* Get to the EQ struct associated with this vector */
10194         fpeq = phba->sli4_hba.fp_eq[fcp_eqidx];
10195
10196         /* Check device state for handling interrupt */
10197         if (unlikely(lpfc_intr_state_check(phba))) {
10198                 /* Check again for link_state with lock held */
10199                 spin_lock_irqsave(&phba->hbalock, iflag);
10200                 if (phba->link_state < LPFC_LINK_DOWN)
10201                         /* Flush, clear interrupt, and rearm the EQ */
10202                         lpfc_sli4_eq_flush(phba, fpeq);
10203                 spin_unlock_irqrestore(&phba->hbalock, iflag);
10204                 return IRQ_NONE;
10205         }
10206
10207         /*
10208          * Process all the event on FCP fast-path EQ
10209          */
10210         while ((eqe = lpfc_sli4_eq_get(fpeq))) {
10211                 lpfc_sli4_fp_handle_eqe(phba, eqe, fcp_eqidx);
10212                 if (!(++ecount % LPFC_GET_QE_REL_INT))
10213                         lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
10214         }
10215
10216         /* Always clear and re-arm the fast-path EQ */
10217         lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
10218
10219         if (unlikely(ecount == 0)) {
10220                 if (phba->intr_type == MSIX)
10221                         /* MSI-X treated interrupt served as no EQ share INT */
10222                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
10223                                         "0358 MSI-X interrupt with no EQE\n");
10224                 else
10225                         /* Non MSI-X treated on interrupt as EQ share INT */
10226                         return IRQ_NONE;
10227         }
10228
10229         return IRQ_HANDLED;
10230 } /* lpfc_sli4_fp_intr_handler */
10231
10232 /**
10233  * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
10234  * @irq: Interrupt number.
10235  * @dev_id: The device context pointer.
10236  *
10237  * This function is the device-level interrupt handler to device with SLI-4
10238  * interface spec, called from the PCI layer when either MSI or Pin-IRQ
10239  * interrupt mode is enabled and there is an event in the HBA which requires
10240  * driver attention. This function invokes the slow-path interrupt attention
10241  * handling function and fast-path interrupt attention handling function in
10242  * turn to process the relevant HBA attention events. This function is called
10243  * without any lock held. It gets the hbalock to access and update SLI data
10244  * structures.
10245  *
10246  * This function returns IRQ_HANDLED when interrupt is handled, else it
10247  * returns IRQ_NONE.
10248  **/
10249 irqreturn_t
10250 lpfc_sli4_intr_handler(int irq, void *dev_id)
10251 {
10252         struct lpfc_hba  *phba;
10253         irqreturn_t sp_irq_rc, fp_irq_rc;
10254         bool fp_handled = false;
10255         uint32_t fcp_eqidx;
10256
10257         /* Get the driver's phba structure from the dev_id */
10258         phba = (struct lpfc_hba *)dev_id;
10259
10260         if (unlikely(!phba))
10261                 return IRQ_NONE;
10262
10263         /*
10264          * Invokes slow-path host attention interrupt handling as appropriate.
10265          */
10266         sp_irq_rc = lpfc_sli4_sp_intr_handler(irq, dev_id);
10267
10268         /*
10269          * Invoke fast-path host attention interrupt handling as appropriate.
10270          */
10271         for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
10272                 fp_irq_rc = lpfc_sli4_fp_intr_handler(irq,
10273                                         &phba->sli4_hba.fcp_eq_hdl[fcp_eqidx]);
10274                 if (fp_irq_rc == IRQ_HANDLED)
10275                         fp_handled |= true;
10276         }
10277
10278         return (fp_handled == true) ? IRQ_HANDLED : sp_irq_rc;
10279 } /* lpfc_sli4_intr_handler */
10280
10281 /**
10282  * lpfc_sli4_queue_free - free a queue structure and associated memory
10283  * @queue: The queue structure to free.
10284  *
10285  * This function frees a queue structure and the DMAable memory used for
10286  * the host resident queue. This function must be called after destroying the
10287  * queue on the HBA.
10288  **/
10289 void
10290 lpfc_sli4_queue_free(struct lpfc_queue *queue)
10291 {
10292         struct lpfc_dmabuf *dmabuf;
10293
10294         if (!queue)
10295                 return;
10296
10297         while (!list_empty(&queue->page_list)) {
10298                 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
10299                                  list);
10300                 dma_free_coherent(&queue->phba->pcidev->dev, SLI4_PAGE_SIZE,
10301                                   dmabuf->virt, dmabuf->phys);
10302                 kfree(dmabuf);
10303         }
10304         kfree(queue);
10305         return;
10306 }
10307
10308 /**
10309  * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
10310  * @phba: The HBA that this queue is being created on.
10311  * @entry_size: The size of each queue entry for this queue.
10312  * @entry count: The number of entries that this queue will handle.
10313  *
10314  * This function allocates a queue structure and the DMAable memory used for
10315  * the host resident queue. This function must be called before creating the
10316  * queue on the HBA.
10317  **/
10318 struct lpfc_queue *
10319 lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
10320                       uint32_t entry_count)
10321 {
10322         struct lpfc_queue *queue;
10323         struct lpfc_dmabuf *dmabuf;
10324         int x, total_qe_count;
10325         void *dma_pointer;
10326         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
10327
10328         if (!phba->sli4_hba.pc_sli4_params.supported)
10329                 hw_page_size = SLI4_PAGE_SIZE;
10330
10331         queue = kzalloc(sizeof(struct lpfc_queue) +
10332                         (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
10333         if (!queue)
10334                 return NULL;
10335         queue->page_count = (ALIGN(entry_size * entry_count,
10336                         hw_page_size))/hw_page_size;
10337         INIT_LIST_HEAD(&queue->list);
10338         INIT_LIST_HEAD(&queue->page_list);
10339         INIT_LIST_HEAD(&queue->child_list);
10340         for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
10341                 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
10342                 if (!dmabuf)
10343                         goto out_fail;
10344                 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
10345                                                   hw_page_size, &dmabuf->phys,
10346                                                   GFP_KERNEL);
10347                 if (!dmabuf->virt) {
10348                         kfree(dmabuf);
10349                         goto out_fail;
10350                 }
10351                 memset(dmabuf->virt, 0, hw_page_size);
10352                 dmabuf->buffer_tag = x;
10353                 list_add_tail(&dmabuf->list, &queue->page_list);
10354                 /* initialize queue's entry array */
10355                 dma_pointer = dmabuf->virt;
10356                 for (; total_qe_count < entry_count &&
10357                      dma_pointer < (hw_page_size + dmabuf->virt);
10358                      total_qe_count++, dma_pointer += entry_size) {
10359                         queue->qe[total_qe_count].address = dma_pointer;
10360                 }
10361         }
10362         queue->entry_size = entry_size;
10363         queue->entry_count = entry_count;
10364         queue->phba = phba;
10365
10366         return queue;
10367 out_fail:
10368         lpfc_sli4_queue_free(queue);
10369         return NULL;
10370 }
10371
10372 /**
10373  * lpfc_eq_create - Create an Event Queue on the HBA
10374  * @phba: HBA structure that indicates port to create a queue on.
10375  * @eq: The queue structure to use to create the event queue.
10376  * @imax: The maximum interrupt per second limit.
10377  *
10378  * This function creates an event queue, as detailed in @eq, on a port,
10379  * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
10380  *
10381  * The @phba struct is used to send mailbox command to HBA. The @eq struct
10382  * is used to get the entry count and entry size that are necessary to
10383  * determine the number of pages to allocate and use for this queue. This
10384  * function will send the EQ_CREATE mailbox command to the HBA to setup the
10385  * event queue. This function is asynchronous and will wait for the mailbox
10386  * command to finish before continuing.
10387  *
10388  * On success this function will return a zero. If unable to allocate enough
10389  * memory this function will return -ENOMEM. If the queue create mailbox command
10390  * fails this function will return -ENXIO.
10391  **/
10392 uint32_t
10393 lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint16_t imax)
10394 {
10395         struct lpfc_mbx_eq_create *eq_create;
10396         LPFC_MBOXQ_t *mbox;
10397         int rc, length, status = 0;
10398         struct lpfc_dmabuf *dmabuf;
10399         uint32_t shdr_status, shdr_add_status;
10400         union lpfc_sli4_cfg_shdr *shdr;
10401         uint16_t dmult;
10402         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
10403
10404         if (!phba->sli4_hba.pc_sli4_params.supported)
10405                 hw_page_size = SLI4_PAGE_SIZE;
10406
10407         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10408         if (!mbox)
10409                 return -ENOMEM;
10410         length = (sizeof(struct lpfc_mbx_eq_create) -
10411                   sizeof(struct lpfc_sli4_cfg_mhdr));
10412         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10413                          LPFC_MBOX_OPCODE_EQ_CREATE,
10414                          length, LPFC_SLI4_MBX_EMBED);
10415         eq_create = &mbox->u.mqe.un.eq_create;
10416         bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
10417                eq->page_count);
10418         bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
10419                LPFC_EQE_SIZE);
10420         bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
10421         /* Calculate delay multiper from maximum interrupt per second */
10422         dmult = LPFC_DMULT_CONST/imax - 1;
10423         bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
10424                dmult);
10425         switch (eq->entry_count) {
10426         default:
10427                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10428                                 "0360 Unsupported EQ count. (%d)\n",
10429                                 eq->entry_count);
10430                 if (eq->entry_count < 256)
10431                         return -EINVAL;
10432                 /* otherwise default to smallest count (drop through) */
10433         case 256:
10434                 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
10435                        LPFC_EQ_CNT_256);
10436                 break;
10437         case 512:
10438                 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
10439                        LPFC_EQ_CNT_512);
10440                 break;
10441         case 1024:
10442                 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
10443                        LPFC_EQ_CNT_1024);
10444                 break;
10445         case 2048:
10446                 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
10447                        LPFC_EQ_CNT_2048);
10448                 break;
10449         case 4096:
10450                 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
10451                        LPFC_EQ_CNT_4096);
10452                 break;
10453         }
10454         list_for_each_entry(dmabuf, &eq->page_list, list) {
10455                 memset(dmabuf->virt, 0, hw_page_size);
10456                 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
10457                                         putPaddrLow(dmabuf->phys);
10458                 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
10459                                         putPaddrHigh(dmabuf->phys);
10460         }
10461         mbox->vport = phba->pport;
10462         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10463         mbox->context1 = NULL;
10464         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10465         shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
10466         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10467         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10468         if (shdr_status || shdr_add_status || rc) {
10469                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10470                                 "2500 EQ_CREATE mailbox failed with "
10471                                 "status x%x add_status x%x, mbx status x%x\n",
10472                                 shdr_status, shdr_add_status, rc);
10473                 status = -ENXIO;
10474         }
10475         eq->type = LPFC_EQ;
10476         eq->subtype = LPFC_NONE;
10477         eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
10478         if (eq->queue_id == 0xFFFF)
10479                 status = -ENXIO;
10480         eq->host_index = 0;
10481         eq->hba_index = 0;
10482
10483         mempool_free(mbox, phba->mbox_mem_pool);
10484         return status;
10485 }
10486
10487 /**
10488  * lpfc_cq_create - Create a Completion Queue on the HBA
10489  * @phba: HBA structure that indicates port to create a queue on.
10490  * @cq: The queue structure to use to create the completion queue.
10491  * @eq: The event queue to bind this completion queue to.
10492  *
10493  * This function creates a completion queue, as detailed in @wq, on a port,
10494  * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
10495  *
10496  * The @phba struct is used to send mailbox command to HBA. The @cq struct
10497  * is used to get the entry count and entry size that are necessary to
10498  * determine the number of pages to allocate and use for this queue. The @eq
10499  * is used to indicate which event queue to bind this completion queue to. This
10500  * function will send the CQ_CREATE mailbox command to the HBA to setup the
10501  * completion queue. This function is asynchronous and will wait for the mailbox
10502  * command to finish before continuing.
10503  *
10504  * On success this function will return a zero. If unable to allocate enough
10505  * memory this function will return -ENOMEM. If the queue create mailbox command
10506  * fails this function will return -ENXIO.
10507  **/
10508 uint32_t
10509 lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
10510                struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
10511 {
10512         struct lpfc_mbx_cq_create *cq_create;
10513         struct lpfc_dmabuf *dmabuf;
10514         LPFC_MBOXQ_t *mbox;
10515         int rc, length, status = 0;
10516         uint32_t shdr_status, shdr_add_status;
10517         union lpfc_sli4_cfg_shdr *shdr;
10518         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
10519
10520         if (!phba->sli4_hba.pc_sli4_params.supported)
10521                 hw_page_size = SLI4_PAGE_SIZE;
10522
10523         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10524         if (!mbox)
10525                 return -ENOMEM;
10526         length = (sizeof(struct lpfc_mbx_cq_create) -
10527                   sizeof(struct lpfc_sli4_cfg_mhdr));
10528         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10529                          LPFC_MBOX_OPCODE_CQ_CREATE,
10530                          length, LPFC_SLI4_MBX_EMBED);
10531         cq_create = &mbox->u.mqe.un.cq_create;
10532         shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
10533         bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
10534                     cq->page_count);
10535         bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
10536         bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
10537         bf_set(lpfc_mbox_hdr_version, &shdr->request,
10538                phba->sli4_hba.pc_sli4_params.cqv);
10539         if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
10540                 /* FW only supports 1. Should be PAGE_SIZE/SLI4_PAGE_SIZE */
10541                 bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request, 1);
10542                 bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
10543                        eq->queue_id);
10544         } else {
10545                 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
10546                        eq->queue_id);
10547         }
10548         switch (cq->entry_count) {
10549         default:
10550                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10551                                 "0361 Unsupported CQ count. (%d)\n",
10552                                 cq->entry_count);
10553                 if (cq->entry_count < 256)
10554                         return -EINVAL;
10555                 /* otherwise default to smallest count (drop through) */
10556         case 256:
10557                 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
10558                        LPFC_CQ_CNT_256);
10559                 break;
10560         case 512:
10561                 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
10562                        LPFC_CQ_CNT_512);
10563                 break;
10564         case 1024:
10565                 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
10566                        LPFC_CQ_CNT_1024);
10567                 break;
10568         }
10569         list_for_each_entry(dmabuf, &cq->page_list, list) {
10570                 memset(dmabuf->virt, 0, hw_page_size);
10571                 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
10572                                         putPaddrLow(dmabuf->phys);
10573                 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
10574                                         putPaddrHigh(dmabuf->phys);
10575         }
10576         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10577
10578         /* The IOCTL status is embedded in the mailbox subheader. */
10579         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10580         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10581         if (shdr_status || shdr_add_status || rc) {
10582                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10583                                 "2501 CQ_CREATE mailbox failed with "
10584                                 "status x%x add_status x%x, mbx status x%x\n",
10585                                 shdr_status, shdr_add_status, rc);
10586                 status = -ENXIO;
10587                 goto out;
10588         }
10589         cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
10590         if (cq->queue_id == 0xFFFF) {
10591                 status = -ENXIO;
10592                 goto out;
10593         }
10594         /* link the cq onto the parent eq child list */
10595         list_add_tail(&cq->list, &eq->child_list);
10596         /* Set up completion queue's type and subtype */
10597         cq->type = type;
10598         cq->subtype = subtype;
10599         cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
10600         cq->assoc_qid = eq->queue_id;
10601         cq->host_index = 0;
10602         cq->hba_index = 0;
10603
10604 out:
10605         mempool_free(mbox, phba->mbox_mem_pool);
10606         return status;
10607 }
10608
10609 /**
10610  * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
10611  * @phba: HBA structure that indicates port to create a queue on.
10612  * @mq: The queue structure to use to create the mailbox queue.
10613  * @mbox: An allocated pointer to type LPFC_MBOXQ_t
10614  * @cq: The completion queue to associate with this cq.
10615  *
10616  * This function provides failback (fb) functionality when the
10617  * mq_create_ext fails on older FW generations.  It's purpose is identical
10618  * to mq_create_ext otherwise.
10619  *
10620  * This routine cannot fail as all attributes were previously accessed and
10621  * initialized in mq_create_ext.
10622  **/
10623 static void
10624 lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
10625                        LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
10626 {
10627         struct lpfc_mbx_mq_create *mq_create;
10628         struct lpfc_dmabuf *dmabuf;
10629         int length;
10630
10631         length = (sizeof(struct lpfc_mbx_mq_create) -
10632                   sizeof(struct lpfc_sli4_cfg_mhdr));
10633         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10634                          LPFC_MBOX_OPCODE_MQ_CREATE,
10635                          length, LPFC_SLI4_MBX_EMBED);
10636         mq_create = &mbox->u.mqe.un.mq_create;
10637         bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
10638                mq->page_count);
10639         bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
10640                cq->queue_id);
10641         bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
10642         switch (mq->entry_count) {
10643         case 16:
10644                 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
10645                        LPFC_MQ_RING_SIZE_16);
10646                 break;
10647         case 32:
10648                 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
10649                        LPFC_MQ_RING_SIZE_32);
10650                 break;
10651         case 64:
10652                 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
10653                        LPFC_MQ_RING_SIZE_64);
10654                 break;
10655         case 128:
10656                 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
10657                        LPFC_MQ_RING_SIZE_128);
10658                 break;
10659         }
10660         list_for_each_entry(dmabuf, &mq->page_list, list) {
10661                 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
10662                         putPaddrLow(dmabuf->phys);
10663                 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
10664                         putPaddrHigh(dmabuf->phys);
10665         }
10666 }
10667
10668 /**
10669  * lpfc_mq_create - Create a mailbox Queue on the HBA
10670  * @phba: HBA structure that indicates port to create a queue on.
10671  * @mq: The queue structure to use to create the mailbox queue.
10672  * @cq: The completion queue to associate with this cq.
10673  * @subtype: The queue's subtype.
10674  *
10675  * This function creates a mailbox queue, as detailed in @mq, on a port,
10676  * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
10677  *
10678  * The @phba struct is used to send mailbox command to HBA. The @cq struct
10679  * is used to get the entry count and entry size that are necessary to
10680  * determine the number of pages to allocate and use for this queue. This
10681  * function will send the MQ_CREATE mailbox command to the HBA to setup the
10682  * mailbox queue. This function is asynchronous and will wait for the mailbox
10683  * command to finish before continuing.
10684  *
10685  * On success this function will return a zero. If unable to allocate enough
10686  * memory this function will return -ENOMEM. If the queue create mailbox command
10687  * fails this function will return -ENXIO.
10688  **/
10689 int32_t
10690 lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
10691                struct lpfc_queue *cq, uint32_t subtype)
10692 {
10693         struct lpfc_mbx_mq_create *mq_create;
10694         struct lpfc_mbx_mq_create_ext *mq_create_ext;
10695         struct lpfc_dmabuf *dmabuf;
10696         LPFC_MBOXQ_t *mbox;
10697         int rc, length, status = 0;
10698         uint32_t shdr_status, shdr_add_status;
10699         union lpfc_sli4_cfg_shdr *shdr;
10700         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
10701
10702         if (!phba->sli4_hba.pc_sli4_params.supported)
10703                 hw_page_size = SLI4_PAGE_SIZE;
10704
10705         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10706         if (!mbox)
10707                 return -ENOMEM;
10708         length = (sizeof(struct lpfc_mbx_mq_create_ext) -
10709                   sizeof(struct lpfc_sli4_cfg_mhdr));
10710         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10711                          LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
10712                          length, LPFC_SLI4_MBX_EMBED);
10713
10714         mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
10715         shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
10716         bf_set(lpfc_mbx_mq_create_ext_num_pages,
10717                &mq_create_ext->u.request, mq->page_count);
10718         bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
10719                &mq_create_ext->u.request, 1);
10720         bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
10721                &mq_create_ext->u.request, 1);
10722         bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
10723                &mq_create_ext->u.request, 1);
10724         bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
10725                &mq_create_ext->u.request, 1);
10726         bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
10727                &mq_create_ext->u.request, 1);
10728         bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
10729         bf_set(lpfc_mbox_hdr_version, &shdr->request,
10730                phba->sli4_hba.pc_sli4_params.mqv);
10731         if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
10732                 bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
10733                        cq->queue_id);
10734         else
10735                 bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
10736                        cq->queue_id);
10737         switch (mq->entry_count) {
10738         default:
10739                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10740                                 "0362 Unsupported MQ count. (%d)\n",
10741                                 mq->entry_count);
10742                 if (mq->entry_count < 16)
10743                         return -EINVAL;
10744                 /* otherwise default to smallest count (drop through) */
10745         case 16:
10746                 bf_set(lpfc_mq_context_ring_size,
10747                        &mq_create_ext->u.request.context,
10748                        LPFC_MQ_RING_SIZE_16);
10749                 break;
10750         case 32:
10751                 bf_set(lpfc_mq_context_ring_size,
10752                        &mq_create_ext->u.request.context,
10753                        LPFC_MQ_RING_SIZE_32);
10754                 break;
10755         case 64:
10756                 bf_set(lpfc_mq_context_ring_size,
10757                        &mq_create_ext->u.request.context,
10758                        LPFC_MQ_RING_SIZE_64);
10759                 break;
10760         case 128:
10761                 bf_set(lpfc_mq_context_ring_size,
10762                        &mq_create_ext->u.request.context,
10763                        LPFC_MQ_RING_SIZE_128);
10764                 break;
10765         }
10766         list_for_each_entry(dmabuf, &mq->page_list, list) {
10767                 memset(dmabuf->virt, 0, hw_page_size);
10768                 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
10769                                         putPaddrLow(dmabuf->phys);
10770                 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
10771                                         putPaddrHigh(dmabuf->phys);
10772         }
10773         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10774         mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
10775                               &mq_create_ext->u.response);
10776         if (rc != MBX_SUCCESS) {
10777                 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10778                                 "2795 MQ_CREATE_EXT failed with "
10779                                 "status x%x. Failback to MQ_CREATE.\n",
10780                                 rc);
10781                 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
10782                 mq_create = &mbox->u.mqe.un.mq_create;
10783                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10784                 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
10785                 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
10786                                       &mq_create->u.response);
10787         }
10788
10789         /* The IOCTL status is embedded in the mailbox subheader. */
10790         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10791         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10792         if (shdr_status || shdr_add_status || rc) {
10793                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10794                                 "2502 MQ_CREATE mailbox failed with "
10795                                 "status x%x add_status x%x, mbx status x%x\n",
10796                                 shdr_status, shdr_add_status, rc);
10797                 status = -ENXIO;
10798                 goto out;
10799         }
10800         if (mq->queue_id == 0xFFFF) {
10801                 status = -ENXIO;
10802                 goto out;
10803         }
10804         mq->type = LPFC_MQ;
10805         mq->assoc_qid = cq->queue_id;
10806         mq->subtype = subtype;
10807         mq->host_index = 0;
10808         mq->hba_index = 0;
10809
10810         /* link the mq onto the parent cq child list */
10811         list_add_tail(&mq->list, &cq->child_list);
10812 out:
10813         mempool_free(mbox, phba->mbox_mem_pool);
10814         return status;
10815 }
10816
10817 /**
10818  * lpfc_wq_create - Create a Work Queue on the HBA
10819  * @phba: HBA structure that indicates port to create a queue on.
10820  * @wq: The queue structure to use to create the work queue.
10821  * @cq: The completion queue to bind this work queue to.
10822  * @subtype: The subtype of the work queue indicating its functionality.
10823  *
10824  * This function creates a work queue, as detailed in @wq, on a port, described
10825  * by @phba by sending a WQ_CREATE mailbox command to the HBA.
10826  *
10827  * The @phba struct is used to send mailbox command to HBA. The @wq struct
10828  * is used to get the entry count and entry size that are necessary to
10829  * determine the number of pages to allocate and use for this queue. The @cq
10830  * is used to indicate which completion queue to bind this work queue to. This
10831  * function will send the WQ_CREATE mailbox command to the HBA to setup the
10832  * work queue. This function is asynchronous and will wait for the mailbox
10833  * command to finish before continuing.
10834  *
10835  * On success this function will return a zero. If unable to allocate enough
10836  * memory this function will return -ENOMEM. If the queue create mailbox command
10837  * fails this function will return -ENXIO.
10838  **/
10839 uint32_t
10840 lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
10841                struct lpfc_queue *cq, uint32_t subtype)
10842 {
10843         struct lpfc_mbx_wq_create *wq_create;
10844         struct lpfc_dmabuf *dmabuf;
10845         LPFC_MBOXQ_t *mbox;
10846         int rc, length, status = 0;
10847         uint32_t shdr_status, shdr_add_status;
10848         union lpfc_sli4_cfg_shdr *shdr;
10849         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
10850         struct dma_address *page;
10851
10852         if (!phba->sli4_hba.pc_sli4_params.supported)
10853                 hw_page_size = SLI4_PAGE_SIZE;
10854
10855         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10856         if (!mbox)
10857                 return -ENOMEM;
10858         length = (sizeof(struct lpfc_mbx_wq_create) -
10859                   sizeof(struct lpfc_sli4_cfg_mhdr));
10860         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10861                          LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
10862                          length, LPFC_SLI4_MBX_EMBED);
10863         wq_create = &mbox->u.mqe.un.wq_create;
10864         shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
10865         bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
10866                     wq->page_count);
10867         bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
10868                     cq->queue_id);
10869         bf_set(lpfc_mbox_hdr_version, &shdr->request,
10870                phba->sli4_hba.pc_sli4_params.wqv);
10871         if (phba->sli4_hba.pc_sli4_params.wqv == LPFC_Q_CREATE_VERSION_1) {
10872                 bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
10873                        wq->entry_count);
10874                 switch (wq->entry_size) {
10875                 default:
10876                 case 64:
10877                         bf_set(lpfc_mbx_wq_create_wqe_size,
10878                                &wq_create->u.request_1,
10879                                LPFC_WQ_WQE_SIZE_64);
10880                         break;
10881                 case 128:
10882                         bf_set(lpfc_mbx_wq_create_wqe_size,
10883                                &wq_create->u.request_1,
10884                                LPFC_WQ_WQE_SIZE_128);
10885                         break;
10886                 }
10887                 bf_set(lpfc_mbx_wq_create_page_size, &wq_create->u.request_1,
10888                        (PAGE_SIZE/SLI4_PAGE_SIZE));
10889                 page = wq_create->u.request_1.page;
10890         } else {
10891                 page = wq_create->u.request.page;
10892         }
10893         list_for_each_entry(dmabuf, &wq->page_list, list) {
10894                 memset(dmabuf->virt, 0, hw_page_size);
10895                 page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
10896                 page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
10897         }
10898         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10899         /* The IOCTL status is embedded in the mailbox subheader. */
10900         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10901         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10902         if (shdr_status || shdr_add_status || rc) {
10903                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10904                                 "2503 WQ_CREATE mailbox failed with "
10905                                 "status x%x add_status x%x, mbx status x%x\n",
10906                                 shdr_status, shdr_add_status, rc);
10907                 status = -ENXIO;
10908                 goto out;
10909         }
10910         wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
10911         if (wq->queue_id == 0xFFFF) {
10912                 status = -ENXIO;
10913                 goto out;
10914         }
10915         wq->type = LPFC_WQ;
10916         wq->assoc_qid = cq->queue_id;
10917         wq->subtype = subtype;
10918         wq->host_index = 0;
10919         wq->hba_index = 0;
10920
10921         /* link the wq onto the parent cq child list */
10922         list_add_tail(&wq->list, &cq->child_list);
10923 out:
10924         mempool_free(mbox, phba->mbox_mem_pool);
10925         return status;
10926 }
10927
10928 /**
10929  * lpfc_rq_create - Create a Receive Queue on the HBA
10930  * @phba: HBA structure that indicates port to create a queue on.
10931  * @hrq: The queue structure to use to create the header receive queue.
10932  * @drq: The queue structure to use to create the data receive queue.
10933  * @cq: The completion queue to bind this work queue to.
10934  *
10935  * This function creates a receive buffer queue pair , as detailed in @hrq and
10936  * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
10937  * to the HBA.
10938  *
10939  * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
10940  * struct is used to get the entry count that is necessary to determine the
10941  * number of pages to use for this queue. The @cq is used to indicate which
10942  * completion queue to bind received buffers that are posted to these queues to.
10943  * This function will send the RQ_CREATE mailbox command to the HBA to setup the
10944  * receive queue pair. This function is asynchronous and will wait for the
10945  * mailbox command to finish before continuing.
10946  *
10947  * On success this function will return a zero. If unable to allocate enough
10948  * memory this function will return -ENOMEM. If the queue create mailbox command
10949  * fails this function will return -ENXIO.
10950  **/
10951 uint32_t
10952 lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
10953                struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
10954 {
10955         struct lpfc_mbx_rq_create *rq_create;
10956         struct lpfc_dmabuf *dmabuf;
10957         LPFC_MBOXQ_t *mbox;
10958         int rc, length, status = 0;
10959         uint32_t shdr_status, shdr_add_status;
10960         union lpfc_sli4_cfg_shdr *shdr;
10961         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
10962
10963         if (!phba->sli4_hba.pc_sli4_params.supported)
10964                 hw_page_size = SLI4_PAGE_SIZE;
10965
10966         if (hrq->entry_count != drq->entry_count)
10967                 return -EINVAL;
10968         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10969         if (!mbox)
10970                 return -ENOMEM;
10971         length = (sizeof(struct lpfc_mbx_rq_create) -
10972                   sizeof(struct lpfc_sli4_cfg_mhdr));
10973         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10974                          LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
10975                          length, LPFC_SLI4_MBX_EMBED);
10976         rq_create = &mbox->u.mqe.un.rq_create;
10977         shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
10978         bf_set(lpfc_mbox_hdr_version, &shdr->request,
10979                phba->sli4_hba.pc_sli4_params.rqv);
10980         if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
10981                 bf_set(lpfc_rq_context_rqe_count_1,
10982                        &rq_create->u.request.context,
10983                        hrq->entry_count);
10984                 rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
10985                 bf_set(lpfc_rq_context_rqe_size,
10986                        &rq_create->u.request.context,
10987                        LPFC_RQE_SIZE_8);
10988                 bf_set(lpfc_rq_context_page_size,
10989                        &rq_create->u.request.context,
10990                        (PAGE_SIZE/SLI4_PAGE_SIZE));
10991         } else {
10992                 switch (hrq->entry_count) {
10993                 default:
10994                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10995                                         "2535 Unsupported RQ count. (%d)\n",
10996                                         hrq->entry_count);
10997                         if (hrq->entry_count < 512)
10998                                 return -EINVAL;
10999                         /* otherwise default to smallest count (drop through) */
11000                 case 512:
11001                         bf_set(lpfc_rq_context_rqe_count,
11002                                &rq_create->u.request.context,
11003                                LPFC_RQ_RING_SIZE_512);
11004                         break;
11005                 case 1024:
11006                         bf_set(lpfc_rq_context_rqe_count,
11007                                &rq_create->u.request.context,
11008                                LPFC_RQ_RING_SIZE_1024);
11009                         break;
11010                 case 2048:
11011                         bf_set(lpfc_rq_context_rqe_count,
11012                                &rq_create->u.request.context,
11013                                LPFC_RQ_RING_SIZE_2048);
11014                         break;
11015                 case 4096:
11016                         bf_set(lpfc_rq_context_rqe_count,
11017                                &rq_create->u.request.context,
11018                                LPFC_RQ_RING_SIZE_4096);
11019                         break;
11020                 }
11021                 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
11022                        LPFC_HDR_BUF_SIZE);
11023         }
11024         bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
11025                cq->queue_id);
11026         bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
11027                hrq->page_count);
11028         list_for_each_entry(dmabuf, &hrq->page_list, list) {
11029                 memset(dmabuf->virt, 0, hw_page_size);
11030                 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
11031                                         putPaddrLow(dmabuf->phys);
11032                 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
11033                                         putPaddrHigh(dmabuf->phys);
11034         }
11035         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
11036         /* The IOCTL status is embedded in the mailbox subheader. */
11037         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11038         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11039         if (shdr_status || shdr_add_status || rc) {
11040                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11041                                 "2504 RQ_CREATE mailbox failed with "
11042                                 "status x%x add_status x%x, mbx status x%x\n",
11043                                 shdr_status, shdr_add_status, rc);
11044                 status = -ENXIO;
11045                 goto out;
11046         }
11047         hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
11048         if (hrq->queue_id == 0xFFFF) {
11049                 status = -ENXIO;
11050                 goto out;
11051         }
11052         hrq->type = LPFC_HRQ;
11053         hrq->assoc_qid = cq->queue_id;
11054         hrq->subtype = subtype;
11055         hrq->host_index = 0;
11056         hrq->hba_index = 0;
11057
11058         /* now create the data queue */
11059         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
11060                          LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
11061                          length, LPFC_SLI4_MBX_EMBED);
11062         bf_set(lpfc_mbox_hdr_version, &shdr->request,
11063                phba->sli4_hba.pc_sli4_params.rqv);
11064         if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
11065                 bf_set(lpfc_rq_context_rqe_count_1,
11066                        &rq_create->u.request.context, hrq->entry_count);
11067                 rq_create->u.request.context.buffer_size = LPFC_DATA_BUF_SIZE;
11068                 bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
11069                        LPFC_RQE_SIZE_8);
11070                 bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
11071                        (PAGE_SIZE/SLI4_PAGE_SIZE));
11072         } else {
11073                 switch (drq->entry_count) {
11074                 default:
11075                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11076                                         "2536 Unsupported RQ count. (%d)\n",
11077                                         drq->entry_count);
11078                         if (drq->entry_count < 512)
11079                                 return -EINVAL;
11080                         /* otherwise default to smallest count (drop through) */
11081                 case 512:
11082                         bf_set(lpfc_rq_context_rqe_count,
11083                                &rq_create->u.request.context,
11084                                LPFC_RQ_RING_SIZE_512);
11085                         break;
11086                 case 1024:
11087                         bf_set(lpfc_rq_context_rqe_count,
11088                                &rq_create->u.request.context,
11089                                LPFC_RQ_RING_SIZE_1024);
11090                         break;
11091                 case 2048:
11092                         bf_set(lpfc_rq_context_rqe_count,
11093                                &rq_create->u.request.context,
11094                                LPFC_RQ_RING_SIZE_2048);
11095                         break;
11096                 case 4096:
11097                         bf_set(lpfc_rq_context_rqe_count,
11098                                &rq_create->u.request.context,
11099                                LPFC_RQ_RING_SIZE_4096);
11100                         break;
11101                 }
11102                 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
11103                        LPFC_DATA_BUF_SIZE);
11104         }
11105         bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
11106                cq->queue_id);
11107         bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
11108                drq->page_count);
11109         list_for_each_entry(dmabuf, &drq->page_list, list) {
11110                 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
11111                                         putPaddrLow(dmabuf->phys);
11112                 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
11113                                         putPaddrHigh(dmabuf->phys);
11114         }
11115         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
11116         /* The IOCTL status is embedded in the mailbox subheader. */
11117         shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
11118         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11119         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11120         if (shdr_status || shdr_add_status || rc) {
11121                 status = -ENXIO;
11122                 goto out;
11123         }
11124         drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
11125         if (drq->queue_id == 0xFFFF) {
11126                 status = -ENXIO;
11127                 goto out;
11128         }
11129         drq->type = LPFC_DRQ;
11130         drq->assoc_qid = cq->queue_id;
11131         drq->subtype = subtype;
11132         drq->host_index = 0;
11133         drq->hba_index = 0;
11134
11135         /* link the header and data RQs onto the parent cq child list */
11136         list_add_tail(&hrq->list, &cq->child_list);
11137         list_add_tail(&drq->list, &cq->child_list);
11138
11139 out:
11140         mempool_free(mbox, phba->mbox_mem_pool);
11141         return status;
11142 }
11143
11144 /**
11145  * lpfc_eq_destroy - Destroy an event Queue on the HBA
11146  * @eq: The queue structure associated with the queue to destroy.
11147  *
11148  * This function destroys a queue, as detailed in @eq by sending an mailbox
11149  * command, specific to the type of queue, to the HBA.
11150  *
11151  * The @eq struct is used to get the queue ID of the queue to destroy.
11152  *
11153  * On success this function will return a zero. If the queue destroy mailbox
11154  * command fails this function will return -ENXIO.
11155  **/
11156 uint32_t
11157 lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
11158 {
11159         LPFC_MBOXQ_t *mbox;
11160         int rc, length, status = 0;
11161         uint32_t shdr_status, shdr_add_status;
11162         union lpfc_sli4_cfg_shdr *shdr;
11163
11164         if (!eq)
11165                 return -ENODEV;
11166         mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
11167         if (!mbox)
11168                 return -ENOMEM;
11169         length = (sizeof(struct lpfc_mbx_eq_destroy) -
11170                   sizeof(struct lpfc_sli4_cfg_mhdr));
11171         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
11172                          LPFC_MBOX_OPCODE_EQ_DESTROY,
11173                          length, LPFC_SLI4_MBX_EMBED);
11174         bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
11175                eq->queue_id);
11176         mbox->vport = eq->phba->pport;
11177         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
11178
11179         rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
11180         /* The IOCTL status is embedded in the mailbox subheader. */
11181         shdr = (union lpfc_sli4_cfg_shdr *)
11182                 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
11183         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11184         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11185         if (shdr_status || shdr_add_status || rc) {
11186                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11187                                 "2505 EQ_DESTROY mailbox failed with "
11188                                 "status x%x add_status x%x, mbx status x%x\n",
11189                                 shdr_status, shdr_add_status, rc);
11190                 status = -ENXIO;
11191         }
11192
11193         /* Remove eq from any list */
11194         list_del_init(&eq->list);
11195         mempool_free(mbox, eq->phba->mbox_mem_pool);
11196         return status;
11197 }
11198
11199 /**
11200  * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
11201  * @cq: The queue structure associated with the queue to destroy.
11202  *
11203  * This function destroys a queue, as detailed in @cq by sending an mailbox
11204  * command, specific to the type of queue, to the HBA.
11205  *
11206  * The @cq struct is used to get the queue ID of the queue to destroy.
11207  *
11208  * On success this function will return a zero. If the queue destroy mailbox
11209  * command fails this function will return -ENXIO.
11210  **/
11211 uint32_t
11212 lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
11213 {
11214         LPFC_MBOXQ_t *mbox;
11215         int rc, length, status = 0;
11216         uint32_t shdr_status, shdr_add_status;
11217         union lpfc_sli4_cfg_shdr *shdr;
11218
11219         if (!cq)
11220                 return -ENODEV;
11221         mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
11222         if (!mbox)
11223                 return -ENOMEM;
11224         length = (sizeof(struct lpfc_mbx_cq_destroy) -
11225                   sizeof(struct lpfc_sli4_cfg_mhdr));
11226         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
11227                          LPFC_MBOX_OPCODE_CQ_DESTROY,
11228                          length, LPFC_SLI4_MBX_EMBED);
11229         bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
11230                cq->queue_id);
11231         mbox->vport = cq->phba->pport;
11232         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
11233         rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
11234         /* The IOCTL status is embedded in the mailbox subheader. */
11235         shdr = (union lpfc_sli4_cfg_shdr *)
11236                 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
11237         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11238         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11239         if (shdr_status || shdr_add_status || rc) {
11240                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11241                                 "2506 CQ_DESTROY mailbox failed with "
11242                                 "status x%x add_status x%x, mbx status x%x\n",
11243                                 shdr_status, shdr_add_status, rc);
11244                 status = -ENXIO;
11245         }
11246         /* Remove cq from any list */
11247         list_del_init(&cq->list);
11248         mempool_free(mbox, cq->phba->mbox_mem_pool);
11249         return status;
11250 }
11251
11252 /**
11253  * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
11254  * @qm: The queue structure associated with the queue to destroy.
11255  *
11256  * This function destroys a queue, as detailed in @mq by sending an mailbox
11257  * command, specific to the type of queue, to the HBA.
11258  *
11259  * The @mq struct is used to get the queue ID of the queue to destroy.
11260  *
11261  * On success this function will return a zero. If the queue destroy mailbox
11262  * command fails this function will return -ENXIO.
11263  **/
11264 uint32_t
11265 lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
11266 {
11267         LPFC_MBOXQ_t *mbox;
11268         int rc, length, status = 0;
11269         uint32_t shdr_status, shdr_add_status;
11270         union lpfc_sli4_cfg_shdr *shdr;
11271
11272         if (!mq)
11273                 return -ENODEV;
11274         mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
11275         if (!mbox)
11276                 return -ENOMEM;
11277         length = (sizeof(struct lpfc_mbx_mq_destroy) -
11278                   sizeof(struct lpfc_sli4_cfg_mhdr));
11279         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
11280                          LPFC_MBOX_OPCODE_MQ_DESTROY,
11281                          length, LPFC_SLI4_MBX_EMBED);
11282         bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
11283                mq->queue_id);
11284         mbox->vport = mq->phba->pport;
11285         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
11286         rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
11287         /* The IOCTL status is embedded in the mailbox subheader. */
11288         shdr = (union lpfc_sli4_cfg_shdr *)
11289                 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
11290         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11291         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11292         if (shdr_status || shdr_add_status || rc) {
11293                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11294                                 "2507 MQ_DESTROY mailbox failed with "
11295                                 "status x%x add_status x%x, mbx status x%x\n",
11296                                 shdr_status, shdr_add_status, rc);
11297                 status = -ENXIO;
11298         }
11299         /* Remove mq from any list */
11300         list_del_init(&mq->list);
11301         mempool_free(mbox, mq->phba->mbox_mem_pool);
11302         return status;
11303 }
11304
11305 /**
11306  * lpfc_wq_destroy - Destroy a Work Queue on the HBA
11307  * @wq: The queue structure associated with the queue to destroy.
11308  *
11309  * This function destroys a queue, as detailed in @wq by sending an mailbox
11310  * command, specific to the type of queue, to the HBA.
11311  *
11312  * The @wq struct is used to get the queue ID of the queue to destroy.
11313  *
11314  * On success this function will return a zero. If the queue destroy mailbox
11315  * command fails this function will return -ENXIO.
11316  **/
11317 uint32_t
11318 lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
11319 {
11320         LPFC_MBOXQ_t *mbox;
11321         int rc, length, status = 0;
11322         uint32_t shdr_status, shdr_add_status;
11323         union lpfc_sli4_cfg_shdr *shdr;
11324
11325         if (!wq)
11326                 return -ENODEV;
11327         mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
11328         if (!mbox)
11329                 return -ENOMEM;
11330         length = (sizeof(struct lpfc_mbx_wq_destroy) -
11331                   sizeof(struct lpfc_sli4_cfg_mhdr));
11332         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
11333                          LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
11334                          length, LPFC_SLI4_MBX_EMBED);
11335         bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
11336                wq->queue_id);
11337         mbox->vport = wq->phba->pport;
11338         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
11339         rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
11340         shdr = (union lpfc_sli4_cfg_shdr *)
11341                 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
11342         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11343         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11344         if (shdr_status || shdr_add_status || rc) {
11345                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11346                                 "2508 WQ_DESTROY mailbox failed with "
11347                                 "status x%x add_status x%x, mbx status x%x\n",
11348                                 shdr_status, shdr_add_status, rc);
11349                 status = -ENXIO;
11350         }
11351         /* Remove wq from any list */
11352         list_del_init(&wq->list);
11353         mempool_free(mbox, wq->phba->mbox_mem_pool);
11354         return status;
11355 }
11356
11357 /**
11358  * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
11359  * @rq: The queue structure associated with the queue to destroy.
11360  *
11361  * This function destroys a queue, as detailed in @rq by sending an mailbox
11362  * command, specific to the type of queue, to the HBA.
11363  *
11364  * The @rq struct is used to get the queue ID of the queue to destroy.
11365  *
11366  * On success this function will return a zero. If the queue destroy mailbox
11367  * command fails this function will return -ENXIO.
11368  **/
11369 uint32_t
11370 lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
11371                 struct lpfc_queue *drq)
11372 {
11373         LPFC_MBOXQ_t *mbox;
11374         int rc, length, status = 0;
11375         uint32_t shdr_status, shdr_add_status;
11376         union lpfc_sli4_cfg_shdr *shdr;
11377
11378         if (!hrq || !drq)
11379                 return -ENODEV;
11380         mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
11381         if (!mbox)
11382                 return -ENOMEM;
11383         length = (sizeof(struct lpfc_mbx_rq_destroy) -
11384                   sizeof(struct lpfc_sli4_cfg_mhdr));
11385         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
11386                          LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
11387                          length, LPFC_SLI4_MBX_EMBED);
11388         bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
11389                hrq->queue_id);
11390         mbox->vport = hrq->phba->pport;
11391         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
11392         rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
11393         /* The IOCTL status is embedded in the mailbox subheader. */
11394         shdr = (union lpfc_sli4_cfg_shdr *)
11395                 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
11396         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11397         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11398         if (shdr_status || shdr_add_status || rc) {
11399                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11400                                 "2509 RQ_DESTROY mailbox failed with "
11401                                 "status x%x add_status x%x, mbx status x%x\n",
11402                                 shdr_status, shdr_add_status, rc);
11403                 if (rc != MBX_TIMEOUT)
11404                         mempool_free(mbox, hrq->phba->mbox_mem_pool);
11405                 return -ENXIO;
11406         }
11407         bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
11408                drq->queue_id);
11409         rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
11410         shdr = (union lpfc_sli4_cfg_shdr *)
11411                 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
11412         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11413         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11414         if (shdr_status || shdr_add_status || rc) {
11415                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11416                                 "2510 RQ_DESTROY mailbox failed with "
11417                                 "status x%x add_status x%x, mbx status x%x\n",
11418                                 shdr_status, shdr_add_status, rc);
11419                 status = -ENXIO;
11420         }
11421         list_del_init(&hrq->list);
11422         list_del_init(&drq->list);
11423         mempool_free(mbox, hrq->phba->mbox_mem_pool);
11424         return status;
11425 }
11426
11427 /**
11428  * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
11429  * @phba: The virtual port for which this call being executed.
11430  * @pdma_phys_addr0: Physical address of the 1st SGL page.
11431  * @pdma_phys_addr1: Physical address of the 2nd SGL page.
11432  * @xritag: the xritag that ties this io to the SGL pages.
11433  *
11434  * This routine will post the sgl pages for the IO that has the xritag
11435  * that is in the iocbq structure. The xritag is assigned during iocbq
11436  * creation and persists for as long as the driver is loaded.
11437  * if the caller has fewer than 256 scatter gather segments to map then
11438  * pdma_phys_addr1 should be 0.
11439  * If the caller needs to map more than 256 scatter gather segment then
11440  * pdma_phys_addr1 should be a valid physical address.
11441  * physical address for SGLs must be 64 byte aligned.
11442  * If you are going to map 2 SGL's then the first one must have 256 entries
11443  * the second sgl can have between 1 and 256 entries.
11444  *
11445  * Return codes:
11446  *      0 - Success
11447  *      -ENXIO, -ENOMEM - Failure
11448  **/
11449 int
11450 lpfc_sli4_post_sgl(struct lpfc_hba *phba,
11451                 dma_addr_t pdma_phys_addr0,
11452                 dma_addr_t pdma_phys_addr1,
11453                 uint16_t xritag)
11454 {
11455         struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
11456         LPFC_MBOXQ_t *mbox;
11457         int rc;
11458         uint32_t shdr_status, shdr_add_status;
11459         union lpfc_sli4_cfg_shdr *shdr;
11460
11461         if (xritag == NO_XRI) {
11462                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11463                                 "0364 Invalid param:\n");
11464                 return -EINVAL;
11465         }
11466
11467         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11468         if (!mbox)
11469                 return -ENOMEM;
11470
11471         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
11472                         LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
11473                         sizeof(struct lpfc_mbx_post_sgl_pages) -
11474                         sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
11475
11476         post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
11477                                 &mbox->u.mqe.un.post_sgl_pages;
11478         bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
11479         bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
11480
11481         post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
11482                                 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
11483         post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
11484                                 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
11485
11486         post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
11487                                 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
11488         post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
11489                                 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
11490         if (!phba->sli4_hba.intr_enable)
11491                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
11492         else
11493                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
11494         /* The IOCTL status is embedded in the mailbox subheader. */
11495         shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
11496         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11497         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11498         if (rc != MBX_TIMEOUT)
11499                 mempool_free(mbox, phba->mbox_mem_pool);
11500         if (shdr_status || shdr_add_status || rc) {
11501                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11502                                 "2511 POST_SGL mailbox failed with "
11503                                 "status x%x add_status x%x, mbx status x%x\n",
11504                                 shdr_status, shdr_add_status, rc);
11505                 rc = -ENXIO;
11506         }
11507         return 0;
11508 }
11509
11510 /**
11511  * lpfc_sli4_next_xritag - Get an xritag for the io
11512  * @phba: Pointer to HBA context object.
11513  *
11514  * This function gets an xritag for the iocb. If there is no unused xritag
11515  * it will return 0xffff.
11516  * The function returns the allocated xritag if successful, else returns zero.
11517  * Zero is not a valid xritag.
11518  * The caller is not required to hold any lock.
11519  **/
11520 uint16_t
11521 lpfc_sli4_next_xritag(struct lpfc_hba *phba)
11522 {
11523         uint16_t xritag;
11524
11525         spin_lock_irq(&phba->hbalock);
11526         xritag = phba->sli4_hba.next_xri;
11527         if ((xritag != (uint16_t) -1) && xritag <
11528                 (phba->sli4_hba.max_cfg_param.max_xri
11529                         + phba->sli4_hba.max_cfg_param.xri_base)) {
11530                 phba->sli4_hba.next_xri++;
11531                 phba->sli4_hba.max_cfg_param.xri_used++;
11532                 spin_unlock_irq(&phba->hbalock);
11533                 return xritag;
11534         }
11535         spin_unlock_irq(&phba->hbalock);
11536         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11537                         "2004 Failed to allocate XRI.last XRITAG is %d"
11538                         " Max XRI is %d, Used XRI is %d\n",
11539                         phba->sli4_hba.next_xri,
11540                         phba->sli4_hba.max_cfg_param.max_xri,
11541                         phba->sli4_hba.max_cfg_param.xri_used);
11542         return -1;
11543 }
11544
11545 /**
11546  * lpfc_sli4_post_sgl_list - post a block of sgl list to the firmware.
11547  * @phba: pointer to lpfc hba data structure.
11548  *
11549  * This routine is invoked to post a block of driver's sgl pages to the
11550  * HBA using non-embedded mailbox command. No Lock is held. This routine
11551  * is only called when the driver is loading and after all IO has been
11552  * stopped.
11553  **/
11554 int
11555 lpfc_sli4_post_sgl_list(struct lpfc_hba *phba)
11556 {
11557         struct lpfc_sglq *sglq_entry;
11558         struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
11559         struct sgl_page_pairs *sgl_pg_pairs;
11560         void *viraddr;
11561         LPFC_MBOXQ_t *mbox;
11562         uint32_t reqlen, alloclen, pg_pairs;
11563         uint32_t mbox_tmo;
11564         uint16_t xritag_start = 0;
11565         int els_xri_cnt, rc = 0;
11566         uint32_t shdr_status, shdr_add_status;
11567         union lpfc_sli4_cfg_shdr *shdr;
11568
11569         /* The number of sgls to be posted */
11570         els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
11571
11572         reqlen = els_xri_cnt * sizeof(struct sgl_page_pairs) +
11573                  sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
11574         if (reqlen > SLI4_PAGE_SIZE) {
11575                 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
11576                                 "2559 Block sgl registration required DMA "
11577                                 "size (%d) great than a page\n", reqlen);
11578                 return -ENOMEM;
11579         }
11580         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11581         if (!mbox) {
11582                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11583                                 "2560 Failed to allocate mbox cmd memory\n");
11584                 return -ENOMEM;
11585         }
11586
11587         /* Allocate DMA memory and set up the non-embedded mailbox command */
11588         alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
11589                          LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
11590                          LPFC_SLI4_MBX_NEMBED);
11591
11592         if (alloclen < reqlen) {
11593                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11594                                 "0285 Allocated DMA memory size (%d) is "
11595                                 "less than the requested DMA memory "
11596                                 "size (%d)\n", alloclen, reqlen);
11597                 lpfc_sli4_mbox_cmd_free(phba, mbox);
11598                 return -ENOMEM;
11599         }
11600         /* Get the first SGE entry from the non-embedded DMA memory */
11601         viraddr = mbox->sge_array->addr[0];
11602
11603         /* Set up the SGL pages in the non-embedded DMA pages */
11604         sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
11605         sgl_pg_pairs = &sgl->sgl_pg_pairs;
11606
11607         for (pg_pairs = 0; pg_pairs < els_xri_cnt; pg_pairs++) {
11608                 sglq_entry = phba->sli4_hba.lpfc_els_sgl_array[pg_pairs];
11609                 /* Set up the sge entry */
11610                 sgl_pg_pairs->sgl_pg0_addr_lo =
11611                                 cpu_to_le32(putPaddrLow(sglq_entry->phys));
11612                 sgl_pg_pairs->sgl_pg0_addr_hi =
11613                                 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
11614                 sgl_pg_pairs->sgl_pg1_addr_lo =
11615                                 cpu_to_le32(putPaddrLow(0));
11616                 sgl_pg_pairs->sgl_pg1_addr_hi =
11617                                 cpu_to_le32(putPaddrHigh(0));
11618                 /* Keep the first xritag on the list */
11619                 if (pg_pairs == 0)
11620                         xritag_start = sglq_entry->sli4_xritag;
11621                 sgl_pg_pairs++;
11622         }
11623         bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
11624         bf_set(lpfc_post_sgl_pages_xricnt, sgl, els_xri_cnt);
11625         /* Perform endian conversion if necessary */
11626         sgl->word0 = cpu_to_le32(sgl->word0);
11627
11628         if (!phba->sli4_hba.intr_enable)
11629                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
11630         else {
11631                 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
11632                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
11633         }
11634         shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
11635         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11636         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11637         if (rc != MBX_TIMEOUT)
11638                 lpfc_sli4_mbox_cmd_free(phba, mbox);
11639         if (shdr_status || shdr_add_status || rc) {
11640                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11641                                 "2513 POST_SGL_BLOCK mailbox command failed "
11642                                 "status x%x add_status x%x mbx status x%x\n",
11643                                 shdr_status, shdr_add_status, rc);
11644                 rc = -ENXIO;
11645         }
11646         return rc;
11647 }
11648
11649 /**
11650  * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
11651  * @phba: pointer to lpfc hba data structure.
11652  * @sblist: pointer to scsi buffer list.
11653  * @count: number of scsi buffers on the list.
11654  *
11655  * This routine is invoked to post a block of @count scsi sgl pages from a
11656  * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
11657  * No Lock is held.
11658  *
11659  **/
11660 int
11661 lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba, struct list_head *sblist,
11662                               int cnt)
11663 {
11664         struct lpfc_scsi_buf *psb;
11665         struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
11666         struct sgl_page_pairs *sgl_pg_pairs;
11667         void *viraddr;
11668         LPFC_MBOXQ_t *mbox;
11669         uint32_t reqlen, alloclen, pg_pairs;
11670         uint32_t mbox_tmo;
11671         uint16_t xritag_start = 0;
11672         int rc = 0;
11673         uint32_t shdr_status, shdr_add_status;
11674         dma_addr_t pdma_phys_bpl1;
11675         union lpfc_sli4_cfg_shdr *shdr;
11676
11677         /* Calculate the requested length of the dma memory */
11678         reqlen = cnt * sizeof(struct sgl_page_pairs) +
11679                  sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
11680         if (reqlen > SLI4_PAGE_SIZE) {
11681                 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
11682                                 "0217 Block sgl registration required DMA "
11683                                 "size (%d) great than a page\n", reqlen);
11684                 return -ENOMEM;
11685         }
11686         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11687         if (!mbox) {
11688                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11689                                 "0283 Failed to allocate mbox cmd memory\n");
11690                 return -ENOMEM;
11691         }
11692
11693         /* Allocate DMA memory and set up the non-embedded mailbox command */
11694         alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
11695                                 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
11696                                 LPFC_SLI4_MBX_NEMBED);
11697
11698         if (alloclen < reqlen) {
11699                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11700                                 "2561 Allocated DMA memory size (%d) is "
11701                                 "less than the requested DMA memory "
11702                                 "size (%d)\n", alloclen, reqlen);
11703                 lpfc_sli4_mbox_cmd_free(phba, mbox);
11704                 return -ENOMEM;
11705         }
11706         /* Get the first SGE entry from the non-embedded DMA memory */
11707         viraddr = mbox->sge_array->addr[0];
11708
11709         /* Set up the SGL pages in the non-embedded DMA pages */
11710         sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
11711         sgl_pg_pairs = &sgl->sgl_pg_pairs;
11712
11713         pg_pairs = 0;
11714         list_for_each_entry(psb, sblist, list) {
11715                 /* Set up the sge entry */
11716                 sgl_pg_pairs->sgl_pg0_addr_lo =
11717                         cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
11718                 sgl_pg_pairs->sgl_pg0_addr_hi =
11719                         cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
11720                 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
11721                         pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
11722                 else
11723                         pdma_phys_bpl1 = 0;
11724                 sgl_pg_pairs->sgl_pg1_addr_lo =
11725                         cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
11726                 sgl_pg_pairs->sgl_pg1_addr_hi =
11727                         cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
11728                 /* Keep the first xritag on the list */
11729                 if (pg_pairs == 0)
11730                         xritag_start = psb->cur_iocbq.sli4_xritag;
11731                 sgl_pg_pairs++;
11732                 pg_pairs++;
11733         }
11734         bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
11735         bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
11736         /* Perform endian conversion if necessary */
11737         sgl->word0 = cpu_to_le32(sgl->word0);
11738
11739         if (!phba->sli4_hba.intr_enable)
11740                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
11741         else {
11742                 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
11743                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
11744         }
11745         shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
11746         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11747         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11748         if (rc != MBX_TIMEOUT)
11749                 lpfc_sli4_mbox_cmd_free(phba, mbox);
11750         if (shdr_status || shdr_add_status || rc) {
11751                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11752                                 "2564 POST_SGL_BLOCK mailbox command failed "
11753                                 "status x%x add_status x%x mbx status x%x\n",
11754                                 shdr_status, shdr_add_status, rc);
11755                 rc = -ENXIO;
11756         }
11757         return rc;
11758 }
11759
11760 /**
11761  * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
11762  * @phba: pointer to lpfc_hba struct that the frame was received on
11763  * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
11764  *
11765  * This function checks the fields in the @fc_hdr to see if the FC frame is a
11766  * valid type of frame that the LPFC driver will handle. This function will
11767  * return a zero if the frame is a valid frame or a non zero value when the
11768  * frame does not pass the check.
11769  **/
11770 static int
11771 lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
11772 {
11773         /*  make rctl_names static to save stack space */
11774         static char *rctl_names[] = FC_RCTL_NAMES_INIT;
11775         char *type_names[] = FC_TYPE_NAMES_INIT;
11776         struct fc_vft_header *fc_vft_hdr;
11777         uint32_t *header = (uint32_t *) fc_hdr;
11778
11779         switch (fc_hdr->fh_r_ctl) {
11780         case FC_RCTL_DD_UNCAT:          /* uncategorized information */
11781         case FC_RCTL_DD_SOL_DATA:       /* solicited data */
11782         case FC_RCTL_DD_UNSOL_CTL:      /* unsolicited control */
11783         case FC_RCTL_DD_SOL_CTL:        /* solicited control or reply */
11784         case FC_RCTL_DD_UNSOL_DATA:     /* unsolicited data */
11785         case FC_RCTL_DD_DATA_DESC:      /* data descriptor */
11786         case FC_RCTL_DD_UNSOL_CMD:      /* unsolicited command */
11787         case FC_RCTL_DD_CMD_STATUS:     /* command status */
11788         case FC_RCTL_ELS_REQ:   /* extended link services request */
11789         case FC_RCTL_ELS_REP:   /* extended link services reply */
11790         case FC_RCTL_ELS4_REQ:  /* FC-4 ELS request */
11791         case FC_RCTL_ELS4_REP:  /* FC-4 ELS reply */
11792         case FC_RCTL_BA_NOP:    /* basic link service NOP */
11793         case FC_RCTL_BA_ABTS:   /* basic link service abort */
11794         case FC_RCTL_BA_RMC:    /* remove connection */
11795         case FC_RCTL_BA_ACC:    /* basic accept */
11796         case FC_RCTL_BA_RJT:    /* basic reject */
11797         case FC_RCTL_BA_PRMT:
11798         case FC_RCTL_ACK_1:     /* acknowledge_1 */
11799         case FC_RCTL_ACK_0:     /* acknowledge_0 */
11800         case FC_RCTL_P_RJT:     /* port reject */
11801         case FC_RCTL_F_RJT:     /* fabric reject */
11802         case FC_RCTL_P_BSY:     /* port busy */
11803         case FC_RCTL_F_BSY:     /* fabric busy to data frame */
11804         case FC_RCTL_F_BSYL:    /* fabric busy to link control frame */
11805         case FC_RCTL_LCR:       /* link credit reset */
11806         case FC_RCTL_END:       /* end */
11807                 break;
11808         case FC_RCTL_VFTH:      /* Virtual Fabric tagging Header */
11809                 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
11810                 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
11811                 return lpfc_fc_frame_check(phba, fc_hdr);
11812         default:
11813                 goto drop;
11814         }
11815         switch (fc_hdr->fh_type) {
11816         case FC_TYPE_BLS:
11817         case FC_TYPE_ELS:
11818         case FC_TYPE_FCP:
11819         case FC_TYPE_CT:
11820                 break;
11821         case FC_TYPE_IP:
11822         case FC_TYPE_ILS:
11823         default:
11824                 goto drop;
11825         }
11826
11827         lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
11828                         "2538 Received frame rctl:%s type:%s "
11829                         "Frame Data:%08x %08x %08x %08x %08x %08x\n",
11830                         rctl_names[fc_hdr->fh_r_ctl],
11831                         type_names[fc_hdr->fh_type],
11832                         be32_to_cpu(header[0]), be32_to_cpu(header[1]),
11833                         be32_to_cpu(header[2]), be32_to_cpu(header[3]),
11834                         be32_to_cpu(header[4]), be32_to_cpu(header[5]));
11835         return 0;
11836 drop:
11837         lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
11838                         "2539 Dropped frame rctl:%s type:%s\n",
11839                         rctl_names[fc_hdr->fh_r_ctl],
11840                         type_names[fc_hdr->fh_type]);
11841         return 1;
11842 }
11843
11844 /**
11845  * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
11846  * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
11847  *
11848  * This function processes the FC header to retrieve the VFI from the VF
11849  * header, if one exists. This function will return the VFI if one exists
11850  * or 0 if no VSAN Header exists.
11851  **/
11852 static uint32_t
11853 lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
11854 {
11855         struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
11856
11857         if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
11858                 return 0;
11859         return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
11860 }
11861
11862 /**
11863  * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
11864  * @phba: Pointer to the HBA structure to search for the vport on
11865  * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
11866  * @fcfi: The FC Fabric ID that the frame came from
11867  *
11868  * This function searches the @phba for a vport that matches the content of the
11869  * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
11870  * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
11871  * returns the matching vport pointer or NULL if unable to match frame to a
11872  * vport.
11873  **/
11874 static struct lpfc_vport *
11875 lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
11876                        uint16_t fcfi)
11877 {
11878         struct lpfc_vport **vports;
11879         struct lpfc_vport *vport = NULL;
11880         int i;
11881         uint32_t did = (fc_hdr->fh_d_id[0] << 16 |
11882                         fc_hdr->fh_d_id[1] << 8 |
11883                         fc_hdr->fh_d_id[2]);
11884
11885         vports = lpfc_create_vport_work_array(phba);
11886         if (vports != NULL)
11887                 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
11888                         if (phba->fcf.fcfi == fcfi &&
11889                             vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
11890                             vports[i]->fc_myDID == did) {
11891                                 vport = vports[i];
11892                                 break;
11893                         }
11894                 }
11895         lpfc_destroy_vport_work_array(phba, vports);
11896         return vport;
11897 }
11898
11899 /**
11900  * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
11901  * @vport: The vport to work on.
11902  *
11903  * This function updates the receive sequence time stamp for this vport. The
11904  * receive sequence time stamp indicates the time that the last frame of the
11905  * the sequence that has been idle for the longest amount of time was received.
11906  * the driver uses this time stamp to indicate if any received sequences have
11907  * timed out.
11908  **/
11909 void
11910 lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
11911 {
11912         struct lpfc_dmabuf *h_buf;
11913         struct hbq_dmabuf *dmabuf = NULL;
11914
11915         /* get the oldest sequence on the rcv list */
11916         h_buf = list_get_first(&vport->rcv_buffer_list,
11917                                struct lpfc_dmabuf, list);
11918         if (!h_buf)
11919                 return;
11920         dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11921         vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
11922 }
11923
11924 /**
11925  * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
11926  * @vport: The vport that the received sequences were sent to.
11927  *
11928  * This function cleans up all outstanding received sequences. This is called
11929  * by the driver when a link event or user action invalidates all the received
11930  * sequences.
11931  **/
11932 void
11933 lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
11934 {
11935         struct lpfc_dmabuf *h_buf, *hnext;
11936         struct lpfc_dmabuf *d_buf, *dnext;
11937         struct hbq_dmabuf *dmabuf = NULL;
11938
11939         /* start with the oldest sequence on the rcv list */
11940         list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
11941                 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11942                 list_del_init(&dmabuf->hbuf.list);
11943                 list_for_each_entry_safe(d_buf, dnext,
11944                                          &dmabuf->dbuf.list, list) {
11945                         list_del_init(&d_buf->list);
11946                         lpfc_in_buf_free(vport->phba, d_buf);
11947                 }
11948                 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
11949         }
11950 }
11951
11952 /**
11953  * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
11954  * @vport: The vport that the received sequences were sent to.
11955  *
11956  * This function determines whether any received sequences have timed out by
11957  * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
11958  * indicates that there is at least one timed out sequence this routine will
11959  * go through the received sequences one at a time from most inactive to most
11960  * active to determine which ones need to be cleaned up. Once it has determined
11961  * that a sequence needs to be cleaned up it will simply free up the resources
11962  * without sending an abort.
11963  **/
11964 void
11965 lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
11966 {
11967         struct lpfc_dmabuf *h_buf, *hnext;
11968         struct lpfc_dmabuf *d_buf, *dnext;
11969         struct hbq_dmabuf *dmabuf = NULL;
11970         unsigned long timeout;
11971         int abort_count = 0;
11972
11973         timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
11974                    vport->rcv_buffer_time_stamp);
11975         if (list_empty(&vport->rcv_buffer_list) ||
11976             time_before(jiffies, timeout))
11977                 return;
11978         /* start with the oldest sequence on the rcv list */
11979         list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
11980                 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11981                 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
11982                            dmabuf->time_stamp);
11983                 if (time_before(jiffies, timeout))
11984                         break;
11985                 abort_count++;
11986                 list_del_init(&dmabuf->hbuf.list);
11987                 list_for_each_entry_safe(d_buf, dnext,
11988                                          &dmabuf->dbuf.list, list) {
11989                         list_del_init(&d_buf->list);
11990                         lpfc_in_buf_free(vport->phba, d_buf);
11991                 }
11992                 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
11993         }
11994         if (abort_count)
11995                 lpfc_update_rcv_time_stamp(vport);
11996 }
11997
11998 /**
11999  * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
12000  * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
12001  *
12002  * This function searches through the existing incomplete sequences that have
12003  * been sent to this @vport. If the frame matches one of the incomplete
12004  * sequences then the dbuf in the @dmabuf is added to the list of frames that
12005  * make up that sequence. If no sequence is found that matches this frame then
12006  * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
12007  * This function returns a pointer to the first dmabuf in the sequence list that
12008  * the frame was linked to.
12009  **/
12010 static struct hbq_dmabuf *
12011 lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
12012 {
12013         struct fc_frame_header *new_hdr;
12014         struct fc_frame_header *temp_hdr;
12015         struct lpfc_dmabuf *d_buf;
12016         struct lpfc_dmabuf *h_buf;
12017         struct hbq_dmabuf *seq_dmabuf = NULL;
12018         struct hbq_dmabuf *temp_dmabuf = NULL;
12019
12020         INIT_LIST_HEAD(&dmabuf->dbuf.list);
12021         dmabuf->time_stamp = jiffies;
12022         new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
12023         /* Use the hdr_buf to find the sequence that this frame belongs to */
12024         list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
12025                 temp_hdr = (struct fc_frame_header *)h_buf->virt;
12026                 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
12027                     (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
12028                     (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
12029                         continue;
12030                 /* found a pending sequence that matches this frame */
12031                 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
12032                 break;
12033         }
12034         if (!seq_dmabuf) {
12035                 /*
12036                  * This indicates first frame received for this sequence.
12037                  * Queue the buffer on the vport's rcv_buffer_list.
12038                  */
12039                 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
12040                 lpfc_update_rcv_time_stamp(vport);
12041                 return dmabuf;
12042         }
12043         temp_hdr = seq_dmabuf->hbuf.virt;
12044         if (be16_to_cpu(new_hdr->fh_seq_cnt) <
12045                 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
12046                 list_del_init(&seq_dmabuf->hbuf.list);
12047                 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
12048                 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
12049                 lpfc_update_rcv_time_stamp(vport);
12050                 return dmabuf;
12051         }
12052         /* move this sequence to the tail to indicate a young sequence */
12053         list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
12054         seq_dmabuf->time_stamp = jiffies;
12055         lpfc_update_rcv_time_stamp(vport);
12056         if (list_empty(&seq_dmabuf->dbuf.list)) {
12057                 temp_hdr = dmabuf->hbuf.virt;
12058                 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
12059                 return seq_dmabuf;
12060         }
12061         /* find the correct place in the sequence to insert this frame */
12062         list_for_each_entry_reverse(d_buf, &seq_dmabuf->dbuf.list, list) {
12063                 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
12064                 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
12065                 /*
12066                  * If the frame's sequence count is greater than the frame on
12067                  * the list then insert the frame right after this frame
12068                  */
12069                 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
12070                         be16_to_cpu(temp_hdr->fh_seq_cnt)) {
12071                         list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
12072                         return seq_dmabuf;
12073                 }
12074         }
12075         return NULL;
12076 }
12077
12078 /**
12079  * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
12080  * @vport: pointer to a vitural port
12081  * @dmabuf: pointer to a dmabuf that describes the FC sequence
12082  *
12083  * This function tries to abort from the partially assembed sequence, described
12084  * by the information from basic abbort @dmabuf. It checks to see whether such
12085  * partially assembled sequence held by the driver. If so, it shall free up all
12086  * the frames from the partially assembled sequence.
12087  *
12088  * Return
12089  * true  -- if there is matching partially assembled sequence present and all
12090  *          the frames freed with the sequence;
12091  * false -- if there is no matching partially assembled sequence present so
12092  *          nothing got aborted in the lower layer driver
12093  **/
12094 static bool
12095 lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
12096                             struct hbq_dmabuf *dmabuf)
12097 {
12098         struct fc_frame_header *new_hdr;
12099         struct fc_frame_header *temp_hdr;
12100         struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
12101         struct hbq_dmabuf *seq_dmabuf = NULL;
12102
12103         /* Use the hdr_buf to find the sequence that matches this frame */
12104         INIT_LIST_HEAD(&dmabuf->dbuf.list);
12105         INIT_LIST_HEAD(&dmabuf->hbuf.list);
12106         new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
12107         list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
12108                 temp_hdr = (struct fc_frame_header *)h_buf->virt;
12109                 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
12110                     (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
12111                     (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
12112                         continue;
12113                 /* found a pending sequence that matches this frame */
12114                 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
12115                 break;
12116         }
12117
12118         /* Free up all the frames from the partially assembled sequence */
12119         if (seq_dmabuf) {
12120                 list_for_each_entry_safe(d_buf, n_buf,
12121                                          &seq_dmabuf->dbuf.list, list) {
12122                         list_del_init(&d_buf->list);
12123                         lpfc_in_buf_free(vport->phba, d_buf);
12124                 }
12125                 return true;
12126         }
12127         return false;
12128 }
12129
12130 /**
12131  * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
12132  * @phba: Pointer to HBA context object.
12133  * @cmd_iocbq: pointer to the command iocbq structure.
12134  * @rsp_iocbq: pointer to the response iocbq structure.
12135  *
12136  * This function handles the sequence abort response iocb command complete
12137  * event. It properly releases the memory allocated to the sequence abort
12138  * accept iocb.
12139  **/
12140 static void
12141 lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
12142                              struct lpfc_iocbq *cmd_iocbq,
12143                              struct lpfc_iocbq *rsp_iocbq)
12144 {
12145         if (cmd_iocbq)
12146                 lpfc_sli_release_iocbq(phba, cmd_iocbq);
12147 }
12148
12149 /**
12150  * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
12151  * @phba: Pointer to HBA context object.
12152  * @fc_hdr: pointer to a FC frame header.
12153  *
12154  * This function sends a basic response to a previous unsol sequence abort
12155  * event after aborting the sequence handling.
12156  **/
12157 static void
12158 lpfc_sli4_seq_abort_rsp(struct lpfc_hba *phba,
12159                         struct fc_frame_header *fc_hdr)
12160 {
12161         struct lpfc_iocbq *ctiocb = NULL;
12162         struct lpfc_nodelist *ndlp;
12163         uint16_t oxid, rxid;
12164         uint32_t sid, fctl;
12165         IOCB_t *icmd;
12166         int rc;
12167
12168         if (!lpfc_is_link_up(phba))
12169                 return;
12170
12171         sid = sli4_sid_from_fc_hdr(fc_hdr);
12172         oxid = be16_to_cpu(fc_hdr->fh_ox_id);
12173         rxid = be16_to_cpu(fc_hdr->fh_rx_id);
12174
12175         ndlp = lpfc_findnode_did(phba->pport, sid);
12176         if (!ndlp) {
12177                 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
12178                                 "1268 Find ndlp returned NULL for oxid:x%x "
12179                                 "SID:x%x\n", oxid, sid);
12180                 return;
12181         }
12182         if (rxid >= phba->sli4_hba.max_cfg_param.xri_base
12183                 && rxid <= (phba->sli4_hba.max_cfg_param.max_xri
12184                 + phba->sli4_hba.max_cfg_param.xri_base))
12185                 lpfc_set_rrq_active(phba, ndlp, rxid, oxid, 0);
12186
12187         /* Allocate buffer for rsp iocb */
12188         ctiocb = lpfc_sli_get_iocbq(phba);
12189         if (!ctiocb)
12190                 return;
12191
12192         /* Extract the F_CTL field from FC_HDR */
12193         fctl = sli4_fctl_from_fc_hdr(fc_hdr);
12194
12195         icmd = &ctiocb->iocb;
12196         icmd->un.xseq64.bdl.bdeSize = 0;
12197         icmd->un.xseq64.bdl.ulpIoTag32 = 0;
12198         icmd->un.xseq64.w5.hcsw.Dfctl = 0;
12199         icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
12200         icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
12201
12202         /* Fill in the rest of iocb fields */
12203         icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
12204         icmd->ulpBdeCount = 0;
12205         icmd->ulpLe = 1;
12206         icmd->ulpClass = CLASS3;
12207         icmd->ulpContext = ndlp->nlp_rpi;
12208         ctiocb->context1 = ndlp;
12209
12210         ctiocb->iocb_cmpl = NULL;
12211         ctiocb->vport = phba->pport;
12212         ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
12213         ctiocb->sli4_xritag = NO_XRI;
12214
12215         /* If the oxid maps to the FCP XRI range or if it is out of range,
12216          * send a BLS_RJT.  The driver no longer has that exchange.
12217          * Override the IOCB for a BA_RJT.
12218          */
12219         if (oxid > (phba->sli4_hba.max_cfg_param.max_xri +
12220                     phba->sli4_hba.max_cfg_param.xri_base) ||
12221             oxid > (lpfc_sli4_get_els_iocb_cnt(phba) +
12222                     phba->sli4_hba.max_cfg_param.xri_base)) {
12223                 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
12224                 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
12225                 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
12226                 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
12227         }
12228
12229         if (fctl & FC_FC_EX_CTX) {
12230                 /* ABTS sent by responder to CT exchange, construction
12231                  * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
12232                  * field and RX_ID from ABTS for RX_ID field.
12233                  */
12234                 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
12235                 bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
12236         } else {
12237                 /* ABTS sent by initiator to CT exchange, construction
12238                  * of BA_ACC will need to allocate a new XRI as for the
12239                  * XRI_TAG and RX_ID fields.
12240                  */
12241                 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
12242                 bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, NO_XRI);
12243         }
12244         bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
12245
12246         /* Xmit CT abts response on exchange <xid> */
12247         lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
12248                         "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
12249                         icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
12250
12251         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
12252         if (rc == IOCB_ERROR) {
12253                 lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
12254                                 "2925 Failed to issue CT ABTS RSP x%x on "
12255                                 "xri x%x, Data x%x\n",
12256                                 icmd->un.xseq64.w5.hcsw.Rctl, oxid,
12257                                 phba->link_state);
12258                 lpfc_sli_release_iocbq(phba, ctiocb);
12259         }
12260 }
12261
12262 /**
12263  * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
12264  * @vport: Pointer to the vport on which this sequence was received
12265  * @dmabuf: pointer to a dmabuf that describes the FC sequence
12266  *
12267  * This function handles an SLI-4 unsolicited abort event. If the unsolicited
12268  * receive sequence is only partially assembed by the driver, it shall abort
12269  * the partially assembled frames for the sequence. Otherwise, if the
12270  * unsolicited receive sequence has been completely assembled and passed to
12271  * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
12272  * unsolicited sequence has been aborted. After that, it will issue a basic
12273  * accept to accept the abort.
12274  **/
12275 void
12276 lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
12277                              struct hbq_dmabuf *dmabuf)
12278 {
12279         struct lpfc_hba *phba = vport->phba;
12280         struct fc_frame_header fc_hdr;
12281         uint32_t fctl;
12282         bool abts_par;
12283
12284         /* Make a copy of fc_hdr before the dmabuf being released */
12285         memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
12286         fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
12287
12288         if (fctl & FC_FC_EX_CTX) {
12289                 /*
12290                  * ABTS sent by responder to exchange, just free the buffer
12291                  */
12292                 lpfc_in_buf_free(phba, &dmabuf->dbuf);
12293         } else {
12294                 /*
12295                  * ABTS sent by initiator to exchange, need to do cleanup
12296                  */
12297                 /* Try to abort partially assembled seq */
12298                 abts_par = lpfc_sli4_abort_partial_seq(vport, dmabuf);
12299
12300                 /* Send abort to ULP if partially seq abort failed */
12301                 if (abts_par == false)
12302                         lpfc_sli4_send_seq_to_ulp(vport, dmabuf);
12303                 else
12304                         lpfc_in_buf_free(phba, &dmabuf->dbuf);
12305         }
12306         /* Send basic accept (BA_ACC) to the abort requester */
12307         lpfc_sli4_seq_abort_rsp(phba, &fc_hdr);
12308 }
12309
12310 /**
12311  * lpfc_seq_complete - Indicates if a sequence is complete
12312  * @dmabuf: pointer to a dmabuf that describes the FC sequence
12313  *
12314  * This function checks the sequence, starting with the frame described by
12315  * @dmabuf, to see if all the frames associated with this sequence are present.
12316  * the frames associated with this sequence are linked to the @dmabuf using the
12317  * dbuf list. This function looks for two major things. 1) That the first frame
12318  * has a sequence count of zero. 2) There is a frame with last frame of sequence
12319  * set. 3) That there are no holes in the sequence count. The function will
12320  * return 1 when the sequence is complete, otherwise it will return 0.
12321  **/
12322 static int
12323 lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
12324 {
12325         struct fc_frame_header *hdr;
12326         struct lpfc_dmabuf *d_buf;
12327         struct hbq_dmabuf *seq_dmabuf;
12328         uint32_t fctl;
12329         int seq_count = 0;
12330
12331         hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
12332         /* make sure first fame of sequence has a sequence count of zero */
12333         if (hdr->fh_seq_cnt != seq_count)
12334                 return 0;
12335         fctl = (hdr->fh_f_ctl[0] << 16 |
12336                 hdr->fh_f_ctl[1] << 8 |
12337                 hdr->fh_f_ctl[2]);
12338         /* If last frame of sequence we can return success. */
12339         if (fctl & FC_FC_END_SEQ)
12340                 return 1;
12341         list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
12342                 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
12343                 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
12344                 /* If there is a hole in the sequence count then fail. */
12345                 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
12346                         return 0;
12347                 fctl = (hdr->fh_f_ctl[0] << 16 |
12348                         hdr->fh_f_ctl[1] << 8 |
12349                         hdr->fh_f_ctl[2]);
12350                 /* If last frame of sequence we can return success. */
12351                 if (fctl & FC_FC_END_SEQ)
12352                         return 1;
12353         }
12354         return 0;
12355 }
12356
12357 /**
12358  * lpfc_prep_seq - Prep sequence for ULP processing
12359  * @vport: Pointer to the vport on which this sequence was received
12360  * @dmabuf: pointer to a dmabuf that describes the FC sequence
12361  *
12362  * This function takes a sequence, described by a list of frames, and creates
12363  * a list of iocbq structures to describe the sequence. This iocbq list will be
12364  * used to issue to the generic unsolicited sequence handler. This routine
12365  * returns a pointer to the first iocbq in the list. If the function is unable
12366  * to allocate an iocbq then it throw out the received frames that were not
12367  * able to be described and return a pointer to the first iocbq. If unable to
12368  * allocate any iocbqs (including the first) this function will return NULL.
12369  **/
12370 static struct lpfc_iocbq *
12371 lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
12372 {
12373         struct lpfc_dmabuf *d_buf, *n_buf;
12374         struct lpfc_iocbq *first_iocbq, *iocbq;
12375         struct fc_frame_header *fc_hdr;
12376         uint32_t sid;
12377         struct ulp_bde64 *pbde;
12378
12379         fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
12380         /* remove from receive buffer list */
12381         list_del_init(&seq_dmabuf->hbuf.list);
12382         lpfc_update_rcv_time_stamp(vport);
12383         /* get the Remote Port's SID */
12384         sid = sli4_sid_from_fc_hdr(fc_hdr);
12385         /* Get an iocbq struct to fill in. */
12386         first_iocbq = lpfc_sli_get_iocbq(vport->phba);
12387         if (first_iocbq) {
12388                 /* Initialize the first IOCB. */
12389                 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
12390                 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
12391                 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
12392                 first_iocbq->iocb.ulpContext = be16_to_cpu(fc_hdr->fh_ox_id);
12393                 first_iocbq->iocb.unsli3.rcvsli3.vpi =
12394                                         vport->vpi + vport->phba->vpi_base;
12395                 /* put the first buffer into the first IOCBq */
12396                 first_iocbq->context2 = &seq_dmabuf->dbuf;
12397                 first_iocbq->context3 = NULL;
12398                 first_iocbq->iocb.ulpBdeCount = 1;
12399                 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
12400                                                         LPFC_DATA_BUF_SIZE;
12401                 first_iocbq->iocb.un.rcvels.remoteID = sid;
12402                 first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
12403                                 bf_get(lpfc_rcqe_length,
12404                                        &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
12405         }
12406         iocbq = first_iocbq;
12407         /*
12408          * Each IOCBq can have two Buffers assigned, so go through the list
12409          * of buffers for this sequence and save two buffers in each IOCBq
12410          */
12411         list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
12412                 if (!iocbq) {
12413                         lpfc_in_buf_free(vport->phba, d_buf);
12414                         continue;
12415                 }
12416                 if (!iocbq->context3) {
12417                         iocbq->context3 = d_buf;
12418                         iocbq->iocb.ulpBdeCount++;
12419                         pbde = (struct ulp_bde64 *)
12420                                         &iocbq->iocb.unsli3.sli3Words[4];
12421                         pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
12422                         first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
12423                                 bf_get(lpfc_rcqe_length,
12424                                        &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
12425                 } else {
12426                         iocbq = lpfc_sli_get_iocbq(vport->phba);
12427                         if (!iocbq) {
12428                                 if (first_iocbq) {
12429                                         first_iocbq->iocb.ulpStatus =
12430                                                         IOSTAT_FCP_RSP_ERROR;
12431                                         first_iocbq->iocb.un.ulpWord[4] =
12432                                                         IOERR_NO_RESOURCES;
12433                                 }
12434                                 lpfc_in_buf_free(vport->phba, d_buf);
12435                                 continue;
12436                         }
12437                         iocbq->context2 = d_buf;
12438                         iocbq->context3 = NULL;
12439                         iocbq->iocb.ulpBdeCount = 1;
12440                         iocbq->iocb.un.cont64[0].tus.f.bdeSize =
12441                                                         LPFC_DATA_BUF_SIZE;
12442                         first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
12443                                 bf_get(lpfc_rcqe_length,
12444                                        &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
12445                         iocbq->iocb.un.rcvels.remoteID = sid;
12446                         list_add_tail(&iocbq->list, &first_iocbq->list);
12447                 }
12448         }
12449         return first_iocbq;
12450 }
12451
12452 static void
12453 lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
12454                           struct hbq_dmabuf *seq_dmabuf)
12455 {
12456         struct fc_frame_header *fc_hdr;
12457         struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
12458         struct lpfc_hba *phba = vport->phba;
12459
12460         fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
12461         iocbq = lpfc_prep_seq(vport, seq_dmabuf);
12462         if (!iocbq) {
12463                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12464                                 "2707 Ring %d handler: Failed to allocate "
12465                                 "iocb Rctl x%x Type x%x received\n",
12466                                 LPFC_ELS_RING,
12467                                 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
12468                 return;
12469         }
12470         if (!lpfc_complete_unsol_iocb(phba,
12471                                       &phba->sli.ring[LPFC_ELS_RING],
12472                                       iocbq, fc_hdr->fh_r_ctl,
12473                                       fc_hdr->fh_type))
12474                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12475                                 "2540 Ring %d handler: unexpected Rctl "
12476                                 "x%x Type x%x received\n",
12477                                 LPFC_ELS_RING,
12478                                 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
12479
12480         /* Free iocb created in lpfc_prep_seq */
12481         list_for_each_entry_safe(curr_iocb, next_iocb,
12482                 &iocbq->list, list) {
12483                 list_del_init(&curr_iocb->list);
12484                 lpfc_sli_release_iocbq(phba, curr_iocb);
12485         }
12486         lpfc_sli_release_iocbq(phba, iocbq);
12487 }
12488
12489 /**
12490  * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
12491  * @phba: Pointer to HBA context object.
12492  *
12493  * This function is called with no lock held. This function processes all
12494  * the received buffers and gives it to upper layers when a received buffer
12495  * indicates that it is the final frame in the sequence. The interrupt
12496  * service routine processes received buffers at interrupt contexts and adds
12497  * received dma buffers to the rb_pend_list queue and signals the worker thread.
12498  * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
12499  * appropriate receive function when the final frame in a sequence is received.
12500  **/
12501 void
12502 lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
12503                                  struct hbq_dmabuf *dmabuf)
12504 {
12505         struct hbq_dmabuf *seq_dmabuf;
12506         struct fc_frame_header *fc_hdr;
12507         struct lpfc_vport *vport;
12508         uint32_t fcfi;
12509
12510         /* Process each received buffer */
12511         fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
12512         /* check to see if this a valid type of frame */
12513         if (lpfc_fc_frame_check(phba, fc_hdr)) {
12514                 lpfc_in_buf_free(phba, &dmabuf->dbuf);
12515                 return;
12516         }
12517         fcfi = bf_get(lpfc_rcqe_fcf_id, &dmabuf->cq_event.cqe.rcqe_cmpl);
12518         vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi);
12519         if (!vport || !(vport->vpi_state & LPFC_VPI_REGISTERED)) {
12520                 /* throw out the frame */
12521                 lpfc_in_buf_free(phba, &dmabuf->dbuf);
12522                 return;
12523         }
12524         /* Handle the basic abort sequence (BA_ABTS) event */
12525         if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
12526                 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
12527                 return;
12528         }
12529
12530         /* Link this frame */
12531         seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
12532         if (!seq_dmabuf) {
12533                 /* unable to add frame to vport - throw it out */
12534                 lpfc_in_buf_free(phba, &dmabuf->dbuf);
12535                 return;
12536         }
12537         /* If not last frame in sequence continue processing frames. */
12538         if (!lpfc_seq_complete(seq_dmabuf))
12539                 return;
12540
12541         /* Send the complete sequence to the upper layer protocol */
12542         lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
12543 }
12544
12545 /**
12546  * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
12547  * @phba: pointer to lpfc hba data structure.
12548  *
12549  * This routine is invoked to post rpi header templates to the
12550  * HBA consistent with the SLI-4 interface spec.  This routine
12551  * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
12552  * SLI4_PAGE_SIZE modulo 64 rpi context headers.
12553  *
12554  * This routine does not require any locks.  It's usage is expected
12555  * to be driver load or reset recovery when the driver is
12556  * sequential.
12557  *
12558  * Return codes
12559  *      0 - successful
12560  *      -EIO - The mailbox failed to complete successfully.
12561  *      When this error occurs, the driver is not guaranteed
12562  *      to have any rpi regions posted to the device and
12563  *      must either attempt to repost the regions or take a
12564  *      fatal error.
12565  **/
12566 int
12567 lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
12568 {
12569         struct lpfc_rpi_hdr *rpi_page;
12570         uint32_t rc = 0;
12571
12572         /* Post all rpi memory regions to the port. */
12573         list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
12574                 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
12575                 if (rc != MBX_SUCCESS) {
12576                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12577                                         "2008 Error %d posting all rpi "
12578                                         "headers\n", rc);
12579                         rc = -EIO;
12580                         break;
12581                 }
12582         }
12583
12584         return rc;
12585 }
12586
12587 /**
12588  * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
12589  * @phba: pointer to lpfc hba data structure.
12590  * @rpi_page:  pointer to the rpi memory region.
12591  *
12592  * This routine is invoked to post a single rpi header to the
12593  * HBA consistent with the SLI-4 interface spec.  This memory region
12594  * maps up to 64 rpi context regions.
12595  *
12596  * Return codes
12597  *      0 - successful
12598  *      -ENOMEM - No available memory
12599  *      -EIO - The mailbox failed to complete successfully.
12600  **/
12601 int
12602 lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
12603 {
12604         LPFC_MBOXQ_t *mboxq;
12605         struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
12606         uint32_t rc = 0;
12607         uint32_t mbox_tmo;
12608         uint32_t shdr_status, shdr_add_status;
12609         union lpfc_sli4_cfg_shdr *shdr;
12610
12611         /* The port is notified of the header region via a mailbox command. */
12612         mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12613         if (!mboxq) {
12614                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12615                                 "2001 Unable to allocate memory for issuing "
12616                                 "SLI_CONFIG_SPECIAL mailbox command\n");
12617                 return -ENOMEM;
12618         }
12619
12620         /* Post all rpi memory regions to the port. */
12621         hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
12622         mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
12623         lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
12624                          LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
12625                          sizeof(struct lpfc_mbx_post_hdr_tmpl) -
12626                          sizeof(struct lpfc_sli4_cfg_mhdr),
12627                          LPFC_SLI4_MBX_EMBED);
12628         bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
12629                hdr_tmpl, rpi_page->page_count);
12630         bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
12631                rpi_page->start_rpi);
12632         hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
12633         hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
12634         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
12635         shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
12636         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12637         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12638         if (rc != MBX_TIMEOUT)
12639                 mempool_free(mboxq, phba->mbox_mem_pool);
12640         if (shdr_status || shdr_add_status || rc) {
12641                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12642                                 "2514 POST_RPI_HDR mailbox failed with "
12643                                 "status x%x add_status x%x, mbx status x%x\n",
12644                                 shdr_status, shdr_add_status, rc);
12645                 rc = -ENXIO;
12646         }
12647         return rc;
12648 }
12649
12650 /**
12651  * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
12652  * @phba: pointer to lpfc hba data structure.
12653  *
12654  * This routine is invoked to post rpi header templates to the
12655  * HBA consistent with the SLI-4 interface spec.  This routine
12656  * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
12657  * SLI4_PAGE_SIZE modulo 64 rpi context headers.
12658  *
12659  * Returns
12660  *      A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
12661  *      LPFC_RPI_ALLOC_ERROR if no rpis are available.
12662  **/
12663 int
12664 lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
12665 {
12666         int rpi;
12667         uint16_t max_rpi, rpi_base, rpi_limit;
12668         uint16_t rpi_remaining;
12669         struct lpfc_rpi_hdr *rpi_hdr;
12670
12671         max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
12672         rpi_base = phba->sli4_hba.max_cfg_param.rpi_base;
12673         rpi_limit = phba->sli4_hba.next_rpi;
12674
12675         /*
12676          * The valid rpi range is not guaranteed to be zero-based.  Start
12677          * the search at the rpi_base as reported by the port.
12678          */
12679         spin_lock_irq(&phba->hbalock);
12680         rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, rpi_base);
12681         if (rpi >= rpi_limit || rpi < rpi_base)
12682                 rpi = LPFC_RPI_ALLOC_ERROR;
12683         else {
12684                 set_bit(rpi, phba->sli4_hba.rpi_bmask);
12685                 phba->sli4_hba.max_cfg_param.rpi_used++;
12686                 phba->sli4_hba.rpi_count++;
12687         }
12688
12689         /*
12690          * Don't try to allocate more rpi header regions if the device limit
12691          * on available rpis max has been exhausted.
12692          */
12693         if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
12694             (phba->sli4_hba.rpi_count >= max_rpi)) {
12695                 spin_unlock_irq(&phba->hbalock);
12696                 return rpi;
12697         }
12698
12699         /*
12700          * If the driver is running low on rpi resources, allocate another
12701          * page now.  Note that the next_rpi value is used because
12702          * it represents how many are actually in use whereas max_rpi notes
12703          * how many are supported max by the device.
12704          */
12705         rpi_remaining = phba->sli4_hba.next_rpi - rpi_base -
12706                 phba->sli4_hba.rpi_count;
12707         spin_unlock_irq(&phba->hbalock);
12708         if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
12709                 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
12710                 if (!rpi_hdr) {
12711                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12712                                         "2002 Error Could not grow rpi "
12713                                         "count\n");
12714                 } else {
12715                         lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
12716                 }
12717         }
12718
12719         return rpi;
12720 }
12721
12722 /**
12723  * lpfc_sli4_free_rpi - Release an rpi for reuse.
12724  * @phba: pointer to lpfc hba data structure.
12725  *
12726  * This routine is invoked to release an rpi to the pool of
12727  * available rpis maintained by the driver.
12728  **/
12729 void
12730 __lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
12731 {
12732         if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
12733                 phba->sli4_hba.rpi_count--;
12734                 phba->sli4_hba.max_cfg_param.rpi_used--;
12735         }
12736 }
12737
12738 /**
12739  * lpfc_sli4_free_rpi - Release an rpi for reuse.
12740  * @phba: pointer to lpfc hba data structure.
12741  *
12742  * This routine is invoked to release an rpi to the pool of
12743  * available rpis maintained by the driver.
12744  **/
12745 void
12746 lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
12747 {
12748         spin_lock_irq(&phba->hbalock);
12749         __lpfc_sli4_free_rpi(phba, rpi);
12750         spin_unlock_irq(&phba->hbalock);
12751 }
12752
12753 /**
12754  * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
12755  * @phba: pointer to lpfc hba data structure.
12756  *
12757  * This routine is invoked to remove the memory region that
12758  * provided rpi via a bitmask.
12759  **/
12760 void
12761 lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
12762 {
12763         kfree(phba->sli4_hba.rpi_bmask);
12764 }
12765
12766 /**
12767  * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
12768  * @phba: pointer to lpfc hba data structure.
12769  *
12770  * This routine is invoked to remove the memory region that
12771  * provided rpi via a bitmask.
12772  **/
12773 int
12774 lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp)
12775 {
12776         LPFC_MBOXQ_t *mboxq;
12777         struct lpfc_hba *phba = ndlp->phba;
12778         int rc;
12779
12780         /* The port is notified of the header region via a mailbox command. */
12781         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12782         if (!mboxq)
12783                 return -ENOMEM;
12784
12785         /* Post all rpi memory regions to the port. */
12786         lpfc_resume_rpi(mboxq, ndlp);
12787         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
12788         if (rc == MBX_NOT_FINISHED) {
12789                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12790                                 "2010 Resume RPI Mailbox failed "
12791                                 "status %d, mbxStatus x%x\n", rc,
12792                                 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
12793                 mempool_free(mboxq, phba->mbox_mem_pool);
12794                 return -EIO;
12795         }
12796         return 0;
12797 }
12798
12799 /**
12800  * lpfc_sli4_init_vpi - Initialize a vpi with the port
12801  * @vport: Pointer to the vport for which the vpi is being initialized
12802  *
12803  * This routine is invoked to activate a vpi with the port.
12804  *
12805  * Returns:
12806  *    0 success
12807  *    -Evalue otherwise
12808  **/
12809 int
12810 lpfc_sli4_init_vpi(struct lpfc_vport *vport)
12811 {
12812         LPFC_MBOXQ_t *mboxq;
12813         int rc = 0;
12814         int retval = MBX_SUCCESS;
12815         uint32_t mbox_tmo;
12816         struct lpfc_hba *phba = vport->phba;
12817         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12818         if (!mboxq)
12819                 return -ENOMEM;
12820         lpfc_init_vpi(phba, mboxq, vport->vpi);
12821         mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_INIT_VPI);
12822         rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
12823         if (rc != MBX_SUCCESS) {
12824                 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
12825                                 "2022 INIT VPI Mailbox failed "
12826                                 "status %d, mbxStatus x%x\n", rc,
12827                                 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
12828                 retval = -EIO;
12829         }
12830         if (rc != MBX_TIMEOUT)
12831                 mempool_free(mboxq, vport->phba->mbox_mem_pool);
12832
12833         return retval;
12834 }
12835
12836 /**
12837  * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
12838  * @phba: pointer to lpfc hba data structure.
12839  * @mboxq: Pointer to mailbox object.
12840  *
12841  * This routine is invoked to manually add a single FCF record. The caller
12842  * must pass a completely initialized FCF_Record.  This routine takes
12843  * care of the nonembedded mailbox operations.
12844  **/
12845 static void
12846 lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
12847 {
12848         void *virt_addr;
12849         union lpfc_sli4_cfg_shdr *shdr;
12850         uint32_t shdr_status, shdr_add_status;
12851
12852         virt_addr = mboxq->sge_array->addr[0];
12853         /* The IOCTL status is embedded in the mailbox subheader. */
12854         shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
12855         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12856         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12857
12858         if ((shdr_status || shdr_add_status) &&
12859                 (shdr_status != STATUS_FCF_IN_USE))
12860                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12861                         "2558 ADD_FCF_RECORD mailbox failed with "
12862                         "status x%x add_status x%x\n",
12863                         shdr_status, shdr_add_status);
12864
12865         lpfc_sli4_mbox_cmd_free(phba, mboxq);
12866 }
12867
12868 /**
12869  * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
12870  * @phba: pointer to lpfc hba data structure.
12871  * @fcf_record:  pointer to the initialized fcf record to add.
12872  *
12873  * This routine is invoked to manually add a single FCF record. The caller
12874  * must pass a completely initialized FCF_Record.  This routine takes
12875  * care of the nonembedded mailbox operations.
12876  **/
12877 int
12878 lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
12879 {
12880         int rc = 0;
12881         LPFC_MBOXQ_t *mboxq;
12882         uint8_t *bytep;
12883         void *virt_addr;
12884         dma_addr_t phys_addr;
12885         struct lpfc_mbx_sge sge;
12886         uint32_t alloc_len, req_len;
12887         uint32_t fcfindex;
12888
12889         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12890         if (!mboxq) {
12891                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12892                         "2009 Failed to allocate mbox for ADD_FCF cmd\n");
12893                 return -ENOMEM;
12894         }
12895
12896         req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
12897                   sizeof(uint32_t);
12898
12899         /* Allocate DMA memory and set up the non-embedded mailbox command */
12900         alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
12901                                      LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
12902                                      req_len, LPFC_SLI4_MBX_NEMBED);
12903         if (alloc_len < req_len) {
12904                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12905                         "2523 Allocated DMA memory size (x%x) is "
12906                         "less than the requested DMA memory "
12907                         "size (x%x)\n", alloc_len, req_len);
12908                 lpfc_sli4_mbox_cmd_free(phba, mboxq);
12909                 return -ENOMEM;
12910         }
12911
12912         /*
12913          * Get the first SGE entry from the non-embedded DMA memory.  This
12914          * routine only uses a single SGE.
12915          */
12916         lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
12917         phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
12918         virt_addr = mboxq->sge_array->addr[0];
12919         /*
12920          * Configure the FCF record for FCFI 0.  This is the driver's
12921          * hardcoded default and gets used in nonFIP mode.
12922          */
12923         fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
12924         bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
12925         lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
12926
12927         /*
12928          * Copy the fcf_index and the FCF Record Data. The data starts after
12929          * the FCoE header plus word10. The data copy needs to be endian
12930          * correct.
12931          */
12932         bytep += sizeof(uint32_t);
12933         lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
12934         mboxq->vport = phba->pport;
12935         mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
12936         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
12937         if (rc == MBX_NOT_FINISHED) {
12938                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12939                         "2515 ADD_FCF_RECORD mailbox failed with "
12940                         "status 0x%x\n", rc);
12941                 lpfc_sli4_mbox_cmd_free(phba, mboxq);
12942                 rc = -EIO;
12943         } else
12944                 rc = 0;
12945
12946         return rc;
12947 }
12948
12949 /**
12950  * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
12951  * @phba: pointer to lpfc hba data structure.
12952  * @fcf_record:  pointer to the fcf record to write the default data.
12953  * @fcf_index: FCF table entry index.
12954  *
12955  * This routine is invoked to build the driver's default FCF record.  The
12956  * values used are hardcoded.  This routine handles memory initialization.
12957  *
12958  **/
12959 void
12960 lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
12961                                 struct fcf_record *fcf_record,
12962                                 uint16_t fcf_index)
12963 {
12964         memset(fcf_record, 0, sizeof(struct fcf_record));
12965         fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
12966         fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
12967         fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
12968         bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
12969         bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
12970         bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
12971         bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
12972         bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
12973         bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
12974         bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
12975         bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
12976         bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
12977         bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
12978         bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
12979         bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
12980         bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
12981                 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
12982         /* Set the VLAN bit map */
12983         if (phba->valid_vlan) {
12984                 fcf_record->vlan_bitmap[phba->vlan_id / 8]
12985                         = 1 << (phba->vlan_id % 8);
12986         }
12987 }
12988
12989 /**
12990  * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
12991  * @phba: pointer to lpfc hba data structure.
12992  * @fcf_index: FCF table entry offset.
12993  *
12994  * This routine is invoked to scan the entire FCF table by reading FCF
12995  * record and processing it one at a time starting from the @fcf_index
12996  * for initial FCF discovery or fast FCF failover rediscovery.
12997  *
12998  * Return 0 if the mailbox command is submitted successfully, none 0
12999  * otherwise.
13000  **/
13001 int
13002 lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
13003 {
13004         int rc = 0, error;
13005         LPFC_MBOXQ_t *mboxq;
13006
13007         phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
13008         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13009         if (!mboxq) {
13010                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13011                                 "2000 Failed to allocate mbox for "
13012                                 "READ_FCF cmd\n");
13013                 error = -ENOMEM;
13014                 goto fail_fcf_scan;
13015         }
13016         /* Construct the read FCF record mailbox command */
13017         rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
13018         if (rc) {
13019                 error = -EINVAL;
13020                 goto fail_fcf_scan;
13021         }
13022         /* Issue the mailbox command asynchronously */
13023         mboxq->vport = phba->pport;
13024         mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
13025
13026         spin_lock_irq(&phba->hbalock);
13027         phba->hba_flag |= FCF_TS_INPROG;
13028         spin_unlock_irq(&phba->hbalock);
13029
13030         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
13031         if (rc == MBX_NOT_FINISHED)
13032                 error = -EIO;
13033         else {
13034                 /* Reset eligible FCF count for new scan */
13035                 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
13036                         phba->fcf.eligible_fcf_cnt = 0;
13037                 error = 0;
13038         }
13039 fail_fcf_scan:
13040         if (error) {
13041                 if (mboxq)
13042                         lpfc_sli4_mbox_cmd_free(phba, mboxq);
13043                 /* FCF scan failed, clear FCF_TS_INPROG flag */
13044                 spin_lock_irq(&phba->hbalock);
13045                 phba->hba_flag &= ~FCF_TS_INPROG;
13046                 spin_unlock_irq(&phba->hbalock);
13047         }
13048         return error;
13049 }
13050
13051 /**
13052  * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
13053  * @phba: pointer to lpfc hba data structure.
13054  * @fcf_index: FCF table entry offset.
13055  *
13056  * This routine is invoked to read an FCF record indicated by @fcf_index
13057  * and to use it for FLOGI roundrobin FCF failover.
13058  *
13059  * Return 0 if the mailbox command is submitted successfully, none 0
13060  * otherwise.
13061  **/
13062 int
13063 lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
13064 {
13065         int rc = 0, error;
13066         LPFC_MBOXQ_t *mboxq;
13067
13068         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13069         if (!mboxq) {
13070                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
13071                                 "2763 Failed to allocate mbox for "
13072                                 "READ_FCF cmd\n");
13073                 error = -ENOMEM;
13074                 goto fail_fcf_read;
13075         }
13076         /* Construct the read FCF record mailbox command */
13077         rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
13078         if (rc) {
13079                 error = -EINVAL;
13080                 goto fail_fcf_read;
13081         }
13082         /* Issue the mailbox command asynchronously */
13083         mboxq->vport = phba->pport;
13084         mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
13085         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
13086         if (rc == MBX_NOT_FINISHED)
13087                 error = -EIO;
13088         else
13089                 error = 0;
13090
13091 fail_fcf_read:
13092         if (error && mboxq)
13093                 lpfc_sli4_mbox_cmd_free(phba, mboxq);
13094         return error;
13095 }
13096
13097 /**
13098  * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
13099  * @phba: pointer to lpfc hba data structure.
13100  * @fcf_index: FCF table entry offset.
13101  *
13102  * This routine is invoked to read an FCF record indicated by @fcf_index to
13103  * determine whether it's eligible for FLOGI roundrobin failover list.
13104  *
13105  * Return 0 if the mailbox command is submitted successfully, none 0
13106  * otherwise.
13107  **/
13108 int
13109 lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
13110 {
13111         int rc = 0, error;
13112         LPFC_MBOXQ_t *mboxq;
13113
13114         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13115         if (!mboxq) {
13116                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
13117                                 "2758 Failed to allocate mbox for "
13118                                 "READ_FCF cmd\n");
13119                                 error = -ENOMEM;
13120                                 goto fail_fcf_read;
13121         }
13122         /* Construct the read FCF record mailbox command */
13123         rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
13124         if (rc) {
13125                 error = -EINVAL;
13126                 goto fail_fcf_read;
13127         }
13128         /* Issue the mailbox command asynchronously */
13129         mboxq->vport = phba->pport;
13130         mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
13131         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
13132         if (rc == MBX_NOT_FINISHED)
13133                 error = -EIO;
13134         else
13135                 error = 0;
13136
13137 fail_fcf_read:
13138         if (error && mboxq)
13139                 lpfc_sli4_mbox_cmd_free(phba, mboxq);
13140         return error;
13141 }
13142
13143 /**
13144  * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
13145  * @phba: pointer to lpfc hba data structure.
13146  *
13147  * This routine is to get the next eligible FCF record index in a round
13148  * robin fashion. If the next eligible FCF record index equals to the
13149  * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
13150  * shall be returned, otherwise, the next eligible FCF record's index
13151  * shall be returned.
13152  **/
13153 uint16_t
13154 lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
13155 {
13156         uint16_t next_fcf_index;
13157
13158         /* Search start from next bit of currently registered FCF index */
13159         next_fcf_index = (phba->fcf.current_rec.fcf_indx + 1) %
13160                                         LPFC_SLI4_FCF_TBL_INDX_MAX;
13161         next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
13162                                        LPFC_SLI4_FCF_TBL_INDX_MAX,
13163                                        next_fcf_index);
13164
13165         /* Wrap around condition on phba->fcf.fcf_rr_bmask */
13166         if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
13167                 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
13168                                                LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
13169
13170         /* Check roundrobin failover list empty condition */
13171         if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
13172                 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
13173                                 "2844 No roundrobin failover FCF available\n");
13174                 return LPFC_FCOE_FCF_NEXT_NONE;
13175         }
13176
13177         lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
13178                         "2845 Get next roundrobin failover FCF (x%x)\n",
13179                         next_fcf_index);
13180
13181         return next_fcf_index;
13182 }
13183
13184 /**
13185  * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
13186  * @phba: pointer to lpfc hba data structure.
13187  *
13188  * This routine sets the FCF record index in to the eligible bmask for
13189  * roundrobin failover search. It checks to make sure that the index
13190  * does not go beyond the range of the driver allocated bmask dimension
13191  * before setting the bit.
13192  *
13193  * Returns 0 if the index bit successfully set, otherwise, it returns
13194  * -EINVAL.
13195  **/
13196 int
13197 lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
13198 {
13199         if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
13200                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
13201                                 "2610 FCF (x%x) reached driver's book "
13202                                 "keeping dimension:x%x\n",
13203                                 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
13204                 return -EINVAL;
13205         }
13206         /* Set the eligible FCF record index bmask */
13207         set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
13208
13209         lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
13210                         "2790 Set FCF (x%x) to roundrobin FCF failover "
13211                         "bmask\n", fcf_index);
13212
13213         return 0;
13214 }
13215
13216 /**
13217  * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
13218  * @phba: pointer to lpfc hba data structure.
13219  *
13220  * This routine clears the FCF record index from the eligible bmask for
13221  * roundrobin failover search. It checks to make sure that the index
13222  * does not go beyond the range of the driver allocated bmask dimension
13223  * before clearing the bit.
13224  **/
13225 void
13226 lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
13227 {
13228         if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
13229                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
13230                                 "2762 FCF (x%x) reached driver's book "
13231                                 "keeping dimension:x%x\n",
13232                                 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
13233                 return;
13234         }
13235         /* Clear the eligible FCF record index bmask */
13236         clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
13237
13238         lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
13239                         "2791 Clear FCF (x%x) from roundrobin failover "
13240                         "bmask\n", fcf_index);
13241 }
13242
13243 /**
13244  * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
13245  * @phba: pointer to lpfc hba data structure.
13246  *
13247  * This routine is the completion routine for the rediscover FCF table mailbox
13248  * command. If the mailbox command returned failure, it will try to stop the
13249  * FCF rediscover wait timer.
13250  **/
13251 void
13252 lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
13253 {
13254         struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
13255         uint32_t shdr_status, shdr_add_status;
13256
13257         redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
13258
13259         shdr_status = bf_get(lpfc_mbox_hdr_status,
13260                              &redisc_fcf->header.cfg_shdr.response);
13261         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
13262                              &redisc_fcf->header.cfg_shdr.response);
13263         if (shdr_status || shdr_add_status) {
13264                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
13265                                 "2746 Requesting for FCF rediscovery failed "
13266                                 "status x%x add_status x%x\n",
13267                                 shdr_status, shdr_add_status);
13268                 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
13269                         spin_lock_irq(&phba->hbalock);
13270                         phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
13271                         spin_unlock_irq(&phba->hbalock);
13272                         /*
13273                          * CVL event triggered FCF rediscover request failed,
13274                          * last resort to re-try current registered FCF entry.
13275                          */
13276                         lpfc_retry_pport_discovery(phba);
13277                 } else {
13278                         spin_lock_irq(&phba->hbalock);
13279                         phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
13280                         spin_unlock_irq(&phba->hbalock);
13281                         /*
13282                          * DEAD FCF event triggered FCF rediscover request
13283                          * failed, last resort to fail over as a link down
13284                          * to FCF registration.
13285                          */
13286                         lpfc_sli4_fcf_dead_failthrough(phba);
13287                 }
13288         } else {
13289                 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
13290                                 "2775 Start FCF rediscover quiescent timer\n");
13291                 /*
13292                  * Start FCF rediscovery wait timer for pending FCF
13293                  * before rescan FCF record table.
13294                  */
13295                 lpfc_fcf_redisc_wait_start_timer(phba);
13296         }
13297
13298         mempool_free(mbox, phba->mbox_mem_pool);
13299 }
13300
13301 /**
13302  * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
13303  * @phba: pointer to lpfc hba data structure.
13304  *
13305  * This routine is invoked to request for rediscovery of the entire FCF table
13306  * by the port.
13307  **/
13308 int
13309 lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
13310 {
13311         LPFC_MBOXQ_t *mbox;
13312         struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
13313         int rc, length;
13314
13315         /* Cancel retry delay timers to all vports before FCF rediscover */
13316         lpfc_cancel_all_vport_retry_delay_timer(phba);
13317
13318         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13319         if (!mbox) {
13320                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13321                                 "2745 Failed to allocate mbox for "
13322                                 "requesting FCF rediscover.\n");
13323                 return -ENOMEM;
13324         }
13325
13326         length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
13327                   sizeof(struct lpfc_sli4_cfg_mhdr));
13328         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13329                          LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
13330                          length, LPFC_SLI4_MBX_EMBED);
13331
13332         redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
13333         /* Set count to 0 for invalidating the entire FCF database */
13334         bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
13335
13336         /* Issue the mailbox command asynchronously */
13337         mbox->vport = phba->pport;
13338         mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
13339         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
13340
13341         if (rc == MBX_NOT_FINISHED) {
13342                 mempool_free(mbox, phba->mbox_mem_pool);
13343                 return -EIO;
13344         }
13345         return 0;
13346 }
13347
13348 /**
13349  * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
13350  * @phba: pointer to lpfc hba data structure.
13351  *
13352  * This function is the failover routine as a last resort to the FCF DEAD
13353  * event when driver failed to perform fast FCF failover.
13354  **/
13355 void
13356 lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
13357 {
13358         uint32_t link_state;
13359
13360         /*
13361          * Last resort as FCF DEAD event failover will treat this as
13362          * a link down, but save the link state because we don't want
13363          * it to be changed to Link Down unless it is already down.
13364          */
13365         link_state = phba->link_state;
13366         lpfc_linkdown(phba);
13367         phba->link_state = link_state;
13368
13369         /* Unregister FCF if no devices connected to it */
13370         lpfc_unregister_unused_fcf(phba);
13371 }
13372
13373 /**
13374  * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
13375  * @phba: pointer to lpfc hba data structure.
13376  *
13377  * This function read region 23 and parse TLV for port status to
13378  * decide if the user disaled the port. If the TLV indicates the
13379  * port is disabled, the hba_flag is set accordingly.
13380  **/
13381 void
13382 lpfc_sli_read_link_ste(struct lpfc_hba *phba)
13383 {
13384         LPFC_MBOXQ_t *pmb = NULL;
13385         MAILBOX_t *mb;
13386         uint8_t *rgn23_data = NULL;
13387         uint32_t offset = 0, data_size, sub_tlv_len, tlv_offset;
13388         int rc;
13389
13390         pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13391         if (!pmb) {
13392                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13393                         "2600 lpfc_sli_read_serdes_param failed to"
13394                         " allocate mailbox memory\n");
13395                 goto out;
13396         }
13397         mb = &pmb->u.mb;
13398
13399         /* Get adapter Region 23 data */
13400         rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
13401         if (!rgn23_data)
13402                 goto out;
13403
13404         do {
13405                 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
13406                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
13407
13408                 if (rc != MBX_SUCCESS) {
13409                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
13410                                 "2601 lpfc_sli_read_link_ste failed to"
13411                                 " read config region 23 rc 0x%x Status 0x%x\n",
13412                                 rc, mb->mbxStatus);
13413                         mb->un.varDmp.word_cnt = 0;
13414                 }
13415                 /*
13416                  * dump mem may return a zero when finished or we got a
13417                  * mailbox error, either way we are done.
13418                  */
13419                 if (mb->un.varDmp.word_cnt == 0)
13420                         break;
13421                 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
13422                         mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
13423
13424                 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
13425                         rgn23_data + offset,
13426                         mb->un.varDmp.word_cnt);
13427                 offset += mb->un.varDmp.word_cnt;
13428         } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
13429
13430         data_size = offset;
13431         offset = 0;
13432
13433         if (!data_size)
13434                 goto out;
13435
13436         /* Check the region signature first */
13437         if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
13438                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13439                         "2619 Config region 23 has bad signature\n");
13440                         goto out;
13441         }
13442         offset += 4;
13443
13444         /* Check the data structure version */
13445         if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
13446                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13447                         "2620 Config region 23 has bad version\n");
13448                 goto out;
13449         }
13450         offset += 4;
13451
13452         /* Parse TLV entries in the region */
13453         while (offset < data_size) {
13454                 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
13455                         break;
13456                 /*
13457                  * If the TLV is not driver specific TLV or driver id is
13458                  * not linux driver id, skip the record.
13459                  */
13460                 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
13461                     (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
13462                     (rgn23_data[offset + 3] != 0)) {
13463                         offset += rgn23_data[offset + 1] * 4 + 4;
13464                         continue;
13465                 }
13466
13467                 /* Driver found a driver specific TLV in the config region */
13468                 sub_tlv_len = rgn23_data[offset + 1] * 4;
13469                 offset += 4;
13470                 tlv_offset = 0;
13471
13472                 /*
13473                  * Search for configured port state sub-TLV.
13474                  */
13475                 while ((offset < data_size) &&
13476                         (tlv_offset < sub_tlv_len)) {
13477                         if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
13478                                 offset += 4;
13479                                 tlv_offset += 4;
13480                                 break;
13481                         }
13482                         if (rgn23_data[offset] != PORT_STE_TYPE) {
13483                                 offset += rgn23_data[offset + 1] * 4 + 4;
13484                                 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
13485                                 continue;
13486                         }
13487
13488                         /* This HBA contains PORT_STE configured */
13489                         if (!rgn23_data[offset + 2])
13490                                 phba->hba_flag |= LINK_DISABLED;
13491
13492                         goto out;
13493                 }
13494         }
13495 out:
13496         if (pmb)
13497                 mempool_free(pmb, phba->mbox_mem_pool);
13498         kfree(rgn23_data);
13499         return;
13500 }
13501
13502 /**
13503  * lpfc_wr_object - write an object to the firmware
13504  * @phba: HBA structure that indicates port to create a queue on.
13505  * @dmabuf_list: list of dmabufs to write to the port.
13506  * @size: the total byte value of the objects to write to the port.
13507  * @offset: the current offset to be used to start the transfer.
13508  *
13509  * This routine will create a wr_object mailbox command to send to the port.
13510  * the mailbox command will be constructed using the dma buffers described in
13511  * @dmabuf_list to create a list of BDEs. This routine will fill in as many
13512  * BDEs that the imbedded mailbox can support. The @offset variable will be
13513  * used to indicate the starting offset of the transfer and will also return
13514  * the offset after the write object mailbox has completed. @size is used to
13515  * determine the end of the object and whether the eof bit should be set.
13516  *
13517  * Return 0 is successful and offset will contain the the new offset to use
13518  * for the next write.
13519  * Return negative value for error cases.
13520  **/
13521 int
13522 lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
13523                uint32_t size, uint32_t *offset)
13524 {
13525         struct lpfc_mbx_wr_object *wr_object;
13526         LPFC_MBOXQ_t *mbox;
13527         int rc = 0, i = 0;
13528         uint32_t shdr_status, shdr_add_status;
13529         uint32_t mbox_tmo;
13530         union lpfc_sli4_cfg_shdr *shdr;
13531         struct lpfc_dmabuf *dmabuf;
13532         uint32_t written = 0;
13533
13534         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13535         if (!mbox)
13536                 return -ENOMEM;
13537
13538         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13539                         LPFC_MBOX_OPCODE_WRITE_OBJECT,
13540                         sizeof(struct lpfc_mbx_wr_object) -
13541                         sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
13542
13543         wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
13544         wr_object->u.request.write_offset = *offset;
13545         sprintf((uint8_t *)wr_object->u.request.object_name, "/");
13546         wr_object->u.request.object_name[0] =
13547                 cpu_to_le32(wr_object->u.request.object_name[0]);
13548         bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
13549         list_for_each_entry(dmabuf, dmabuf_list, list) {
13550                 if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
13551                         break;
13552                 wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
13553                 wr_object->u.request.bde[i].addrHigh =
13554                         putPaddrHigh(dmabuf->phys);
13555                 if (written + SLI4_PAGE_SIZE >= size) {
13556                         wr_object->u.request.bde[i].tus.f.bdeSize =
13557                                 (size - written);
13558                         written += (size - written);
13559                         bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
13560                 } else {
13561                         wr_object->u.request.bde[i].tus.f.bdeSize =
13562                                 SLI4_PAGE_SIZE;
13563                         written += SLI4_PAGE_SIZE;
13564                 }
13565                 i++;
13566         }
13567         wr_object->u.request.bde_count = i;
13568         bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
13569         if (!phba->sli4_hba.intr_enable)
13570                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13571         else {
13572                 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
13573                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
13574         }
13575         /* The IOCTL status is embedded in the mailbox subheader. */
13576         shdr = (union lpfc_sli4_cfg_shdr *) &wr_object->header.cfg_shdr;
13577         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13578         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13579         if (rc != MBX_TIMEOUT)
13580                 mempool_free(mbox, phba->mbox_mem_pool);
13581         if (shdr_status || shdr_add_status || rc) {
13582                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13583                                 "3025 Write Object mailbox failed with "
13584                                 "status x%x add_status x%x, mbx status x%x\n",
13585                                 shdr_status, shdr_add_status, rc);
13586                 rc = -ENXIO;
13587         } else
13588                 *offset += wr_object->u.response.actual_write_length;
13589         return rc;
13590 }
13591
13592 /**
13593  * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
13594  * @vport: pointer to vport data structure.
13595  *
13596  * This function iterate through the mailboxq and clean up all REG_LOGIN
13597  * and REG_VPI mailbox commands associated with the vport. This function
13598  * is called when driver want to restart discovery of the vport due to
13599  * a Clear Virtual Link event.
13600  **/
13601 void
13602 lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
13603 {
13604         struct lpfc_hba *phba = vport->phba;
13605         LPFC_MBOXQ_t *mb, *nextmb;
13606         struct lpfc_dmabuf *mp;
13607         struct lpfc_nodelist *ndlp;
13608         struct lpfc_nodelist *act_mbx_ndlp = NULL;
13609         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
13610         LIST_HEAD(mbox_cmd_list);
13611         uint8_t restart_loop;
13612
13613         /* Clean up internally queued mailbox commands with the vport */
13614         spin_lock_irq(&phba->hbalock);
13615         list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
13616                 if (mb->vport != vport)
13617                         continue;
13618
13619                 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
13620                         (mb->u.mb.mbxCommand != MBX_REG_VPI))
13621                         continue;
13622
13623                 list_del(&mb->list);
13624                 list_add_tail(&mb->list, &mbox_cmd_list);
13625         }
13626         /* Clean up active mailbox command with the vport */
13627         mb = phba->sli.mbox_active;
13628         if (mb && (mb->vport == vport)) {
13629                 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
13630                         (mb->u.mb.mbxCommand == MBX_REG_VPI))
13631                         mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13632                 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
13633                         act_mbx_ndlp = (struct lpfc_nodelist *)mb->context2;
13634                         /* Put reference count for delayed processing */
13635                         act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
13636                         /* Unregister the RPI when mailbox complete */
13637                         mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
13638                 }
13639         }
13640         /* Cleanup any mailbox completions which are not yet processed */
13641         do {
13642                 restart_loop = 0;
13643                 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
13644                         /*
13645                          * If this mailox is already processed or it is
13646                          * for another vport ignore it.
13647                          */
13648                         if ((mb->vport != vport) ||
13649                                 (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
13650                                 continue;
13651
13652                         if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
13653                                 (mb->u.mb.mbxCommand != MBX_REG_VPI))
13654                                 continue;
13655
13656                         mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13657                         if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
13658                                 ndlp = (struct lpfc_nodelist *)mb->context2;
13659                                 /* Unregister the RPI when mailbox complete */
13660                                 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
13661                                 restart_loop = 1;
13662                                 spin_unlock_irq(&phba->hbalock);
13663                                 spin_lock(shost->host_lock);
13664                                 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
13665                                 spin_unlock(shost->host_lock);
13666                                 spin_lock_irq(&phba->hbalock);
13667                                 break;
13668                         }
13669                 }
13670         } while (restart_loop);
13671
13672         spin_unlock_irq(&phba->hbalock);
13673
13674         /* Release the cleaned-up mailbox commands */
13675         while (!list_empty(&mbox_cmd_list)) {
13676                 list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
13677                 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
13678                         mp = (struct lpfc_dmabuf *) (mb->context1);
13679                         if (mp) {
13680                                 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
13681                                 kfree(mp);
13682                         }
13683                         ndlp = (struct lpfc_nodelist *) mb->context2;
13684                         mb->context2 = NULL;
13685                         if (ndlp) {
13686                                 spin_lock(shost->host_lock);
13687                                 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
13688                                 spin_unlock(shost->host_lock);
13689                                 lpfc_nlp_put(ndlp);
13690                         }
13691                 }
13692                 mempool_free(mb, phba->mbox_mem_pool);
13693         }
13694
13695         /* Release the ndlp with the cleaned-up active mailbox command */
13696         if (act_mbx_ndlp) {
13697                 spin_lock(shost->host_lock);
13698                 act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
13699                 spin_unlock(shost->host_lock);
13700                 lpfc_nlp_put(act_mbx_ndlp);
13701         }
13702 }
13703
13704 /**
13705  * lpfc_drain_txq - Drain the txq
13706  * @phba: Pointer to HBA context object.
13707  *
13708  * This function attempt to submit IOCBs on the txq
13709  * to the adapter.  For SLI4 adapters, the txq contains
13710  * ELS IOCBs that have been deferred because the there
13711  * are no SGLs.  This congestion can occur with large
13712  * vport counts during node discovery.
13713  **/
13714
13715 uint32_t
13716 lpfc_drain_txq(struct lpfc_hba *phba)
13717 {
13718         LIST_HEAD(completions);
13719         struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
13720         struct lpfc_iocbq *piocbq = 0;
13721         unsigned long iflags = 0;
13722         char *fail_msg = NULL;
13723         struct lpfc_sglq *sglq;
13724         union lpfc_wqe wqe;
13725
13726         spin_lock_irqsave(&phba->hbalock, iflags);
13727         if (pring->txq_cnt > pring->txq_max)
13728                 pring->txq_max = pring->txq_cnt;
13729
13730         spin_unlock_irqrestore(&phba->hbalock, iflags);
13731
13732         while (pring->txq_cnt) {
13733                 spin_lock_irqsave(&phba->hbalock, iflags);
13734
13735                 piocbq = lpfc_sli_ringtx_get(phba, pring);
13736                 sglq = __lpfc_sli_get_sglq(phba, piocbq);
13737                 if (!sglq) {
13738                         __lpfc_sli_ringtx_put(phba, pring, piocbq);
13739                         spin_unlock_irqrestore(&phba->hbalock, iflags);
13740                         break;
13741                 } else {
13742                         if (!piocbq) {
13743                                 /* The txq_cnt out of sync. This should
13744                                  * never happen
13745                                  */
13746                                 sglq = __lpfc_clear_active_sglq(phba,
13747                                                  sglq->sli4_xritag);
13748                                 spin_unlock_irqrestore(&phba->hbalock, iflags);
13749                                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13750                                         "2823 txq empty and txq_cnt is %d\n ",
13751                                         pring->txq_cnt);
13752                                 break;
13753                         }
13754                 }
13755
13756                 /* The xri and iocb resources secured,
13757                  * attempt to issue request
13758                  */
13759                 piocbq->sli4_xritag = sglq->sli4_xritag;
13760                 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
13761                         fail_msg = "to convert bpl to sgl";
13762                 else if (lpfc_sli4_iocb2wqe(phba, piocbq, &wqe))
13763                         fail_msg = "to convert iocb to wqe";
13764                 else if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
13765                         fail_msg = " - Wq is full";
13766                 else
13767                         lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
13768
13769                 if (fail_msg) {
13770                         /* Failed means we can't issue and need to cancel */
13771                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13772                                         "2822 IOCB failed %s iotag 0x%x "
13773                                         "xri 0x%x\n",
13774                                         fail_msg,
13775                                         piocbq->iotag, piocbq->sli4_xritag);
13776                         list_add_tail(&piocbq->list, &completions);
13777                 }
13778                 spin_unlock_irqrestore(&phba->hbalock, iflags);
13779         }
13780
13781         /* Cancel all the IOCBs that cannot be issued */
13782         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
13783                                 IOERR_SLI_ABORTED);
13784
13785         return pring->txq_cnt;
13786 }