Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/sage/ceph...
[pandora-kernel.git] / drivers / scsi / lpfc / lpfc_sli.c
1 /*******************************************************************
2  * This file is part of the Emulex Linux Device Driver for         *
3  * Fibre Channel Host Bus Adapters.                                *
4  * Copyright (C) 2004-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         if (iocbq)
463                 phba->iocb_cnt++;
464         if (phba->iocb_cnt > phba->iocb_max)
465                 phba->iocb_max = phba->iocb_cnt;
466         return iocbq;
467 }
468
469 /**
470  * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
471  * @phba: Pointer to HBA context object.
472  * @xritag: XRI value.
473  *
474  * This function clears the sglq pointer from the array of acive
475  * sglq's. The xritag that is passed in is used to index into the
476  * array. Before the xritag can be used it needs to be adjusted
477  * by subtracting the xribase.
478  *
479  * Returns sglq ponter = success, NULL = Failure.
480  **/
481 static struct lpfc_sglq *
482 __lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
483 {
484         struct lpfc_sglq *sglq;
485
486         sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
487         phba->sli4_hba.lpfc_sglq_active_list[xritag] = NULL;
488         return sglq;
489 }
490
491 /**
492  * __lpfc_get_active_sglq - Get the active sglq for this XRI.
493  * @phba: Pointer to HBA context object.
494  * @xritag: XRI value.
495  *
496  * This function returns the sglq pointer from the array of acive
497  * sglq's. The xritag that is passed in is used to index into the
498  * array. Before the xritag can be used it needs to be adjusted
499  * by subtracting the xribase.
500  *
501  * Returns sglq ponter = success, NULL = Failure.
502  **/
503 struct lpfc_sglq *
504 __lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
505 {
506         struct lpfc_sglq *sglq;
507
508         sglq =  phba->sli4_hba.lpfc_sglq_active_list[xritag];
509         return sglq;
510 }
511
512 /**
513  * __lpfc_set_rrq_active - set RRQ active bit in the ndlp's xri_bitmap.
514  * @phba: Pointer to HBA context object.
515  * @ndlp: nodelist pointer for this target.
516  * @xritag: xri used in this exchange.
517  * @rxid: Remote Exchange ID.
518  * @send_rrq: Flag used to determine if we should send rrq els cmd.
519  *
520  * This function is called with hbalock held.
521  * The active bit is set in the ndlp's active rrq xri_bitmap. Allocates an
522  * rrq struct and adds it to the active_rrq_list.
523  *
524  * returns  0 for rrq slot for this xri
525  *         < 0  Were not able to get rrq mem or invalid parameter.
526  **/
527 static int
528 __lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
529                 uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
530 {
531         struct lpfc_node_rrq *rrq;
532         int empty;
533         uint32_t did = 0;
534
535
536         if (!ndlp)
537                 return -EINVAL;
538
539         if (!phba->cfg_enable_rrq)
540                 return -EINVAL;
541
542         if (phba->pport->load_flag & FC_UNLOADING) {
543                 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
544                 goto out;
545         }
546         did = ndlp->nlp_DID;
547
548         /*
549          * set the active bit even if there is no mem available.
550          */
551         if (NLP_CHK_FREE_REQ(ndlp))
552                 goto out;
553
554         if (ndlp->vport && (ndlp->vport->load_flag & FC_UNLOADING))
555                 goto out;
556
557         if (test_and_set_bit(xritag, ndlp->active_rrqs.xri_bitmap))
558                 goto out;
559
560         rrq = mempool_alloc(phba->rrq_pool, GFP_KERNEL);
561         if (rrq) {
562                 rrq->send_rrq = send_rrq;
563                 rrq->xritag = phba->sli4_hba.xri_ids[xritag];
564                 rrq->rrq_stop_time = jiffies + HZ * (phba->fc_ratov + 1);
565                 rrq->ndlp = ndlp;
566                 rrq->nlp_DID = ndlp->nlp_DID;
567                 rrq->vport = ndlp->vport;
568                 rrq->rxid = rxid;
569                 empty = list_empty(&phba->active_rrq_list);
570                 rrq->send_rrq = send_rrq;
571                 list_add_tail(&rrq->list, &phba->active_rrq_list);
572                 if (!(phba->hba_flag & HBA_RRQ_ACTIVE)) {
573                         phba->hba_flag |= HBA_RRQ_ACTIVE;
574                         if (empty)
575                                 lpfc_worker_wake_up(phba);
576                 }
577                 return 0;
578         }
579 out:
580         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
581                         "2921 Can't set rrq active xri:0x%x rxid:0x%x"
582                         " DID:0x%x Send:%d\n",
583                         xritag, rxid, did, send_rrq);
584         return -EINVAL;
585 }
586
587 /**
588  * lpfc_clr_rrq_active - Clears RRQ active bit in xri_bitmap.
589  * @phba: Pointer to HBA context object.
590  * @xritag: xri used in this exchange.
591  * @rrq: The RRQ to be cleared.
592  *
593  **/
594 void
595 lpfc_clr_rrq_active(struct lpfc_hba *phba,
596                     uint16_t xritag,
597                     struct lpfc_node_rrq *rrq)
598 {
599         struct lpfc_nodelist *ndlp = NULL;
600
601         if ((rrq->vport) && NLP_CHK_NODE_ACT(rrq->ndlp))
602                 ndlp = lpfc_findnode_did(rrq->vport, rrq->nlp_DID);
603
604         /* The target DID could have been swapped (cable swap)
605          * we should use the ndlp from the findnode if it is
606          * available.
607          */
608         if ((!ndlp) && rrq->ndlp)
609                 ndlp = rrq->ndlp;
610
611         if (!ndlp)
612                 goto out;
613
614         if (test_and_clear_bit(xritag, ndlp->active_rrqs.xri_bitmap)) {
615                 rrq->send_rrq = 0;
616                 rrq->xritag = 0;
617                 rrq->rrq_stop_time = 0;
618         }
619 out:
620         mempool_free(rrq, phba->rrq_pool);
621 }
622
623 /**
624  * lpfc_handle_rrq_active - Checks if RRQ has waithed RATOV.
625  * @phba: Pointer to HBA context object.
626  *
627  * This function is called with hbalock held. This function
628  * Checks if stop_time (ratov from setting rrq active) has
629  * been reached, if it has and the send_rrq flag is set then
630  * it will call lpfc_send_rrq. If the send_rrq flag is not set
631  * then it will just call the routine to clear the rrq and
632  * free the rrq resource.
633  * The timer is set to the next rrq that is going to expire before
634  * leaving the routine.
635  *
636  **/
637 void
638 lpfc_handle_rrq_active(struct lpfc_hba *phba)
639 {
640         struct lpfc_node_rrq *rrq;
641         struct lpfc_node_rrq *nextrrq;
642         unsigned long next_time;
643         unsigned long iflags;
644         LIST_HEAD(send_rrq);
645
646         spin_lock_irqsave(&phba->hbalock, iflags);
647         phba->hba_flag &= ~HBA_RRQ_ACTIVE;
648         next_time = jiffies + HZ * (phba->fc_ratov + 1);
649         list_for_each_entry_safe(rrq, nextrrq,
650                                  &phba->active_rrq_list, list) {
651                 if (time_after(jiffies, rrq->rrq_stop_time))
652                         list_move(&rrq->list, &send_rrq);
653                 else if (time_before(rrq->rrq_stop_time, next_time))
654                         next_time = rrq->rrq_stop_time;
655         }
656         spin_unlock_irqrestore(&phba->hbalock, iflags);
657         if (!list_empty(&phba->active_rrq_list))
658                 mod_timer(&phba->rrq_tmr, next_time);
659         list_for_each_entry_safe(rrq, nextrrq, &send_rrq, list) {
660                 list_del(&rrq->list);
661                 if (!rrq->send_rrq)
662                         /* this call will free the rrq */
663                 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
664                 else if (lpfc_send_rrq(phba, rrq)) {
665                         /* if we send the rrq then the completion handler
666                         *  will clear the bit in the xribitmap.
667                         */
668                         lpfc_clr_rrq_active(phba, rrq->xritag,
669                                             rrq);
670                 }
671         }
672 }
673
674 /**
675  * lpfc_get_active_rrq - Get the active RRQ for this exchange.
676  * @vport: Pointer to vport context object.
677  * @xri: The xri used in the exchange.
678  * @did: The targets DID for this exchange.
679  *
680  * returns NULL = rrq not found in the phba->active_rrq_list.
681  *         rrq = rrq for this xri and target.
682  **/
683 struct lpfc_node_rrq *
684 lpfc_get_active_rrq(struct lpfc_vport *vport, uint16_t xri, uint32_t did)
685 {
686         struct lpfc_hba *phba = vport->phba;
687         struct lpfc_node_rrq *rrq;
688         struct lpfc_node_rrq *nextrrq;
689         unsigned long iflags;
690
691         if (phba->sli_rev != LPFC_SLI_REV4)
692                 return NULL;
693         spin_lock_irqsave(&phba->hbalock, iflags);
694         list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {
695                 if (rrq->vport == vport && rrq->xritag == xri &&
696                                 rrq->nlp_DID == did){
697                         list_del(&rrq->list);
698                         spin_unlock_irqrestore(&phba->hbalock, iflags);
699                         return rrq;
700                 }
701         }
702         spin_unlock_irqrestore(&phba->hbalock, iflags);
703         return NULL;
704 }
705
706 /**
707  * lpfc_cleanup_vports_rrqs - Remove and clear the active RRQ for this vport.
708  * @vport: Pointer to vport context object.
709  * @ndlp: Pointer to the lpfc_node_list structure.
710  * If ndlp is NULL Remove all active RRQs for this vport from the
711  * phba->active_rrq_list and clear the rrq.
712  * If ndlp is not NULL then only remove rrqs for this vport & this ndlp.
713  **/
714 void
715 lpfc_cleanup_vports_rrqs(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
716
717 {
718         struct lpfc_hba *phba = vport->phba;
719         struct lpfc_node_rrq *rrq;
720         struct lpfc_node_rrq *nextrrq;
721         unsigned long iflags;
722         LIST_HEAD(rrq_list);
723
724         if (phba->sli_rev != LPFC_SLI_REV4)
725                 return;
726         if (!ndlp) {
727                 lpfc_sli4_vport_delete_els_xri_aborted(vport);
728                 lpfc_sli4_vport_delete_fcp_xri_aborted(vport);
729         }
730         spin_lock_irqsave(&phba->hbalock, iflags);
731         list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list)
732                 if ((rrq->vport == vport) && (!ndlp  || rrq->ndlp == ndlp))
733                         list_move(&rrq->list, &rrq_list);
734         spin_unlock_irqrestore(&phba->hbalock, iflags);
735
736         list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
737                 list_del(&rrq->list);
738                 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
739         }
740 }
741
742 /**
743  * lpfc_cleanup_wt_rrqs - Remove all rrq's from the active list.
744  * @phba: Pointer to HBA context object.
745  *
746  * Remove all rrqs from the phba->active_rrq_list and free them by
747  * calling __lpfc_clr_active_rrq
748  *
749  **/
750 void
751 lpfc_cleanup_wt_rrqs(struct lpfc_hba *phba)
752 {
753         struct lpfc_node_rrq *rrq;
754         struct lpfc_node_rrq *nextrrq;
755         unsigned long next_time;
756         unsigned long iflags;
757         LIST_HEAD(rrq_list);
758
759         if (phba->sli_rev != LPFC_SLI_REV4)
760                 return;
761         spin_lock_irqsave(&phba->hbalock, iflags);
762         phba->hba_flag &= ~HBA_RRQ_ACTIVE;
763         next_time = jiffies + HZ * (phba->fc_ratov * 2);
764         list_splice_init(&phba->active_rrq_list, &rrq_list);
765         spin_unlock_irqrestore(&phba->hbalock, iflags);
766
767         list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
768                 list_del(&rrq->list);
769                 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
770         }
771         if (!list_empty(&phba->active_rrq_list))
772                 mod_timer(&phba->rrq_tmr, next_time);
773 }
774
775
776 /**
777  * lpfc_test_rrq_active - Test RRQ bit in xri_bitmap.
778  * @phba: Pointer to HBA context object.
779  * @ndlp: Targets nodelist pointer for this exchange.
780  * @xritag the xri in the bitmap to test.
781  *
782  * This function is called with hbalock held. This function
783  * returns 0 = rrq not active for this xri
784  *         1 = rrq is valid for this xri.
785  **/
786 int
787 lpfc_test_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
788                         uint16_t  xritag)
789 {
790         if (!ndlp)
791                 return 0;
792         if (test_bit(xritag, ndlp->active_rrqs.xri_bitmap))
793                         return 1;
794         else
795                 return 0;
796 }
797
798 /**
799  * lpfc_set_rrq_active - set RRQ active bit in xri_bitmap.
800  * @phba: Pointer to HBA context object.
801  * @ndlp: nodelist pointer for this target.
802  * @xritag: xri used in this exchange.
803  * @rxid: Remote Exchange ID.
804  * @send_rrq: Flag used to determine if we should send rrq els cmd.
805  *
806  * This function takes the hbalock.
807  * The active bit is always set in the active rrq xri_bitmap even
808  * if there is no slot avaiable for the other rrq information.
809  *
810  * returns 0 rrq actived for this xri
811  *         < 0 No memory or invalid ndlp.
812  **/
813 int
814 lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
815                         uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
816 {
817         int ret;
818         unsigned long iflags;
819
820         spin_lock_irqsave(&phba->hbalock, iflags);
821         ret = __lpfc_set_rrq_active(phba, ndlp, xritag, rxid, send_rrq);
822         spin_unlock_irqrestore(&phba->hbalock, iflags);
823         return ret;
824 }
825
826 /**
827  * __lpfc_sli_get_sglq - Allocates an iocb object from sgl pool
828  * @phba: Pointer to HBA context object.
829  * @piocb: Pointer to the iocbq.
830  *
831  * This function is called with hbalock held. This function
832  * gets a new driver sglq object from the sglq list. If the
833  * list is not empty then it is successful, it returns pointer to the newly
834  * allocated sglq object else it returns NULL.
835  **/
836 static struct lpfc_sglq *
837 __lpfc_sli_get_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
838 {
839         struct list_head *lpfc_sgl_list = &phba->sli4_hba.lpfc_sgl_list;
840         struct lpfc_sglq *sglq = NULL;
841         struct lpfc_sglq *start_sglq = NULL;
842         struct lpfc_scsi_buf *lpfc_cmd;
843         struct lpfc_nodelist *ndlp;
844         int found = 0;
845
846         if (piocbq->iocb_flag &  LPFC_IO_FCP) {
847                 lpfc_cmd = (struct lpfc_scsi_buf *) piocbq->context1;
848                 ndlp = lpfc_cmd->rdata->pnode;
849         } else  if ((piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) &&
850                         !(piocbq->iocb_flag & LPFC_IO_LIBDFC))
851                 ndlp = piocbq->context_un.ndlp;
852         else
853                 ndlp = piocbq->context1;
854
855         list_remove_head(lpfc_sgl_list, sglq, struct lpfc_sglq, list);
856         start_sglq = sglq;
857         while (!found) {
858                 if (!sglq)
859                         return NULL;
860                 if (lpfc_test_rrq_active(phba, ndlp, sglq->sli4_xritag)) {
861                         /* This xri has an rrq outstanding for this DID.
862                          * put it back in the list and get another xri.
863                          */
864                         list_add_tail(&sglq->list, lpfc_sgl_list);
865                         sglq = NULL;
866                         list_remove_head(lpfc_sgl_list, sglq,
867                                                 struct lpfc_sglq, list);
868                         if (sglq == start_sglq) {
869                                 sglq = NULL;
870                                 break;
871                         } else
872                                 continue;
873                 }
874                 sglq->ndlp = ndlp;
875                 found = 1;
876                 phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
877                 sglq->state = SGL_ALLOCATED;
878         }
879         return sglq;
880 }
881
882 /**
883  * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
884  * @phba: Pointer to HBA context object.
885  *
886  * This function is called with no lock held. This function
887  * allocates a new driver iocb object from the iocb pool. If the
888  * allocation is successful, it returns pointer to the newly
889  * allocated iocb object else it returns NULL.
890  **/
891 struct lpfc_iocbq *
892 lpfc_sli_get_iocbq(struct lpfc_hba *phba)
893 {
894         struct lpfc_iocbq * iocbq = NULL;
895         unsigned long iflags;
896
897         spin_lock_irqsave(&phba->hbalock, iflags);
898         iocbq = __lpfc_sli_get_iocbq(phba);
899         spin_unlock_irqrestore(&phba->hbalock, iflags);
900         return iocbq;
901 }
902
903 /**
904  * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
905  * @phba: Pointer to HBA context object.
906  * @iocbq: Pointer to driver iocb object.
907  *
908  * This function is called with hbalock held to release driver
909  * iocb object to the iocb pool. The iotag in the iocb object
910  * does not change for each use of the iocb object. This function
911  * clears all other fields of the iocb object when it is freed.
912  * The sqlq structure that holds the xritag and phys and virtual
913  * mappings for the scatter gather list is retrieved from the
914  * active array of sglq. The get of the sglq pointer also clears
915  * the entry in the array. If the status of the IO indiactes that
916  * this IO was aborted then the sglq entry it put on the
917  * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
918  * IO has good status or fails for any other reason then the sglq
919  * entry is added to the free list (lpfc_sgl_list).
920  **/
921 static void
922 __lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
923 {
924         struct lpfc_sglq *sglq;
925         size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
926         unsigned long iflag = 0;
927         struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
928
929         if (iocbq->sli4_xritag == NO_XRI)
930                 sglq = NULL;
931         else
932                 sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_lxritag);
933
934         if (sglq)  {
935                 if ((iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) &&
936                         (sglq->state != SGL_XRI_ABORTED)) {
937                         spin_lock_irqsave(&phba->sli4_hba.abts_sgl_list_lock,
938                                         iflag);
939                         list_add(&sglq->list,
940                                 &phba->sli4_hba.lpfc_abts_els_sgl_list);
941                         spin_unlock_irqrestore(
942                                 &phba->sli4_hba.abts_sgl_list_lock, iflag);
943                 } else {
944                         sglq->state = SGL_FREED;
945                         sglq->ndlp = NULL;
946                         list_add_tail(&sglq->list,
947                                 &phba->sli4_hba.lpfc_sgl_list);
948
949                         /* Check if TXQ queue needs to be serviced */
950                         if (pring->txq_cnt)
951                                 lpfc_worker_wake_up(phba);
952                 }
953         }
954
955
956         /*
957          * Clean all volatile data fields, preserve iotag and node struct.
958          */
959         memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
960         iocbq->sli4_lxritag = NO_XRI;
961         iocbq->sli4_xritag = NO_XRI;
962         list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
963 }
964
965
966 /**
967  * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
968  * @phba: Pointer to HBA context object.
969  * @iocbq: Pointer to driver iocb object.
970  *
971  * This function is called with hbalock held to release driver
972  * iocb object to the iocb pool. The iotag in the iocb object
973  * does not change for each use of the iocb object. This function
974  * clears all other fields of the iocb object when it is freed.
975  **/
976 static void
977 __lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
978 {
979         size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
980
981         /*
982          * Clean all volatile data fields, preserve iotag and node struct.
983          */
984         memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
985         iocbq->sli4_xritag = NO_XRI;
986         list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
987 }
988
989 /**
990  * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
991  * @phba: Pointer to HBA context object.
992  * @iocbq: Pointer to driver iocb object.
993  *
994  * This function is called with hbalock held to release driver
995  * iocb object to the iocb pool. The iotag in the iocb object
996  * does not change for each use of the iocb object. This function
997  * clears all other fields of the iocb object when it is freed.
998  **/
999 static void
1000 __lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1001 {
1002         phba->__lpfc_sli_release_iocbq(phba, iocbq);
1003         phba->iocb_cnt--;
1004 }
1005
1006 /**
1007  * lpfc_sli_release_iocbq - Release iocb to the iocb pool
1008  * @phba: Pointer to HBA context object.
1009  * @iocbq: Pointer to driver iocb object.
1010  *
1011  * This function is called with no lock held to release the iocb to
1012  * iocb pool.
1013  **/
1014 void
1015 lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1016 {
1017         unsigned long iflags;
1018
1019         /*
1020          * Clean all volatile data fields, preserve iotag and node struct.
1021          */
1022         spin_lock_irqsave(&phba->hbalock, iflags);
1023         __lpfc_sli_release_iocbq(phba, iocbq);
1024         spin_unlock_irqrestore(&phba->hbalock, iflags);
1025 }
1026
1027 /**
1028  * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
1029  * @phba: Pointer to HBA context object.
1030  * @iocblist: List of IOCBs.
1031  * @ulpstatus: ULP status in IOCB command field.
1032  * @ulpWord4: ULP word-4 in IOCB command field.
1033  *
1034  * This function is called with a list of IOCBs to cancel. It cancels the IOCB
1035  * on the list by invoking the complete callback function associated with the
1036  * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
1037  * fields.
1038  **/
1039 void
1040 lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
1041                       uint32_t ulpstatus, uint32_t ulpWord4)
1042 {
1043         struct lpfc_iocbq *piocb;
1044
1045         while (!list_empty(iocblist)) {
1046                 list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
1047
1048                 if (!piocb->iocb_cmpl)
1049                         lpfc_sli_release_iocbq(phba, piocb);
1050                 else {
1051                         piocb->iocb.ulpStatus = ulpstatus;
1052                         piocb->iocb.un.ulpWord[4] = ulpWord4;
1053                         (piocb->iocb_cmpl) (phba, piocb, piocb);
1054                 }
1055         }
1056         return;
1057 }
1058
1059 /**
1060  * lpfc_sli_iocb_cmd_type - Get the iocb type
1061  * @iocb_cmnd: iocb command code.
1062  *
1063  * This function is called by ring event handler function to get the iocb type.
1064  * This function translates the iocb command to an iocb command type used to
1065  * decide the final disposition of each completed IOCB.
1066  * The function returns
1067  * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
1068  * LPFC_SOL_IOCB     if it is a solicited iocb completion
1069  * LPFC_ABORT_IOCB   if it is an abort iocb
1070  * LPFC_UNSOL_IOCB   if it is an unsolicited iocb
1071  *
1072  * The caller is not required to hold any lock.
1073  **/
1074 static lpfc_iocb_type
1075 lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
1076 {
1077         lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
1078
1079         if (iocb_cmnd > CMD_MAX_IOCB_CMD)
1080                 return 0;
1081
1082         switch (iocb_cmnd) {
1083         case CMD_XMIT_SEQUENCE_CR:
1084         case CMD_XMIT_SEQUENCE_CX:
1085         case CMD_XMIT_BCAST_CN:
1086         case CMD_XMIT_BCAST_CX:
1087         case CMD_ELS_REQUEST_CR:
1088         case CMD_ELS_REQUEST_CX:
1089         case CMD_CREATE_XRI_CR:
1090         case CMD_CREATE_XRI_CX:
1091         case CMD_GET_RPI_CN:
1092         case CMD_XMIT_ELS_RSP_CX:
1093         case CMD_GET_RPI_CR:
1094         case CMD_FCP_IWRITE_CR:
1095         case CMD_FCP_IWRITE_CX:
1096         case CMD_FCP_IREAD_CR:
1097         case CMD_FCP_IREAD_CX:
1098         case CMD_FCP_ICMND_CR:
1099         case CMD_FCP_ICMND_CX:
1100         case CMD_FCP_TSEND_CX:
1101         case CMD_FCP_TRSP_CX:
1102         case CMD_FCP_TRECEIVE_CX:
1103         case CMD_FCP_AUTO_TRSP_CX:
1104         case CMD_ADAPTER_MSG:
1105         case CMD_ADAPTER_DUMP:
1106         case CMD_XMIT_SEQUENCE64_CR:
1107         case CMD_XMIT_SEQUENCE64_CX:
1108         case CMD_XMIT_BCAST64_CN:
1109         case CMD_XMIT_BCAST64_CX:
1110         case CMD_ELS_REQUEST64_CR:
1111         case CMD_ELS_REQUEST64_CX:
1112         case CMD_FCP_IWRITE64_CR:
1113         case CMD_FCP_IWRITE64_CX:
1114         case CMD_FCP_IREAD64_CR:
1115         case CMD_FCP_IREAD64_CX:
1116         case CMD_FCP_ICMND64_CR:
1117         case CMD_FCP_ICMND64_CX:
1118         case CMD_FCP_TSEND64_CX:
1119         case CMD_FCP_TRSP64_CX:
1120         case CMD_FCP_TRECEIVE64_CX:
1121         case CMD_GEN_REQUEST64_CR:
1122         case CMD_GEN_REQUEST64_CX:
1123         case CMD_XMIT_ELS_RSP64_CX:
1124         case DSSCMD_IWRITE64_CR:
1125         case DSSCMD_IWRITE64_CX:
1126         case DSSCMD_IREAD64_CR:
1127         case DSSCMD_IREAD64_CX:
1128                 type = LPFC_SOL_IOCB;
1129                 break;
1130         case CMD_ABORT_XRI_CN:
1131         case CMD_ABORT_XRI_CX:
1132         case CMD_CLOSE_XRI_CN:
1133         case CMD_CLOSE_XRI_CX:
1134         case CMD_XRI_ABORTED_CX:
1135         case CMD_ABORT_MXRI64_CN:
1136         case CMD_XMIT_BLS_RSP64_CX:
1137                 type = LPFC_ABORT_IOCB;
1138                 break;
1139         case CMD_RCV_SEQUENCE_CX:
1140         case CMD_RCV_ELS_REQ_CX:
1141         case CMD_RCV_SEQUENCE64_CX:
1142         case CMD_RCV_ELS_REQ64_CX:
1143         case CMD_ASYNC_STATUS:
1144         case CMD_IOCB_RCV_SEQ64_CX:
1145         case CMD_IOCB_RCV_ELS64_CX:
1146         case CMD_IOCB_RCV_CONT64_CX:
1147         case CMD_IOCB_RET_XRI64_CX:
1148                 type = LPFC_UNSOL_IOCB;
1149                 break;
1150         case CMD_IOCB_XMIT_MSEQ64_CR:
1151         case CMD_IOCB_XMIT_MSEQ64_CX:
1152         case CMD_IOCB_RCV_SEQ_LIST64_CX:
1153         case CMD_IOCB_RCV_ELS_LIST64_CX:
1154         case CMD_IOCB_CLOSE_EXTENDED_CN:
1155         case CMD_IOCB_ABORT_EXTENDED_CN:
1156         case CMD_IOCB_RET_HBQE64_CN:
1157         case CMD_IOCB_FCP_IBIDIR64_CR:
1158         case CMD_IOCB_FCP_IBIDIR64_CX:
1159         case CMD_IOCB_FCP_ITASKMGT64_CX:
1160         case CMD_IOCB_LOGENTRY_CN:
1161         case CMD_IOCB_LOGENTRY_ASYNC_CN:
1162                 printk("%s - Unhandled SLI-3 Command x%x\n",
1163                                 __func__, iocb_cmnd);
1164                 type = LPFC_UNKNOWN_IOCB;
1165                 break;
1166         default:
1167                 type = LPFC_UNKNOWN_IOCB;
1168                 break;
1169         }
1170
1171         return type;
1172 }
1173
1174 /**
1175  * lpfc_sli_ring_map - Issue config_ring mbox for all rings
1176  * @phba: Pointer to HBA context object.
1177  *
1178  * This function is called from SLI initialization code
1179  * to configure every ring of the HBA's SLI interface. The
1180  * caller is not required to hold any lock. This function issues
1181  * a config_ring mailbox command for each ring.
1182  * This function returns zero if successful else returns a negative
1183  * error code.
1184  **/
1185 static int
1186 lpfc_sli_ring_map(struct lpfc_hba *phba)
1187 {
1188         struct lpfc_sli *psli = &phba->sli;
1189         LPFC_MBOXQ_t *pmb;
1190         MAILBOX_t *pmbox;
1191         int i, rc, ret = 0;
1192
1193         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1194         if (!pmb)
1195                 return -ENOMEM;
1196         pmbox = &pmb->u.mb;
1197         phba->link_state = LPFC_INIT_MBX_CMDS;
1198         for (i = 0; i < psli->num_rings; i++) {
1199                 lpfc_config_ring(phba, i, pmb);
1200                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
1201                 if (rc != MBX_SUCCESS) {
1202                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1203                                         "0446 Adapter failed to init (%d), "
1204                                         "mbxCmd x%x CFG_RING, mbxStatus x%x, "
1205                                         "ring %d\n",
1206                                         rc, pmbox->mbxCommand,
1207                                         pmbox->mbxStatus, i);
1208                         phba->link_state = LPFC_HBA_ERROR;
1209                         ret = -ENXIO;
1210                         break;
1211                 }
1212         }
1213         mempool_free(pmb, phba->mbox_mem_pool);
1214         return ret;
1215 }
1216
1217 /**
1218  * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
1219  * @phba: Pointer to HBA context object.
1220  * @pring: Pointer to driver SLI ring object.
1221  * @piocb: Pointer to the driver iocb object.
1222  *
1223  * This function is called with hbalock held. The function adds the
1224  * new iocb to txcmplq of the given ring. This function always returns
1225  * 0. If this function is called for ELS ring, this function checks if
1226  * there is a vport associated with the ELS command. This function also
1227  * starts els_tmofunc timer if this is an ELS command.
1228  **/
1229 static int
1230 lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1231                         struct lpfc_iocbq *piocb)
1232 {
1233         list_add_tail(&piocb->list, &pring->txcmplq);
1234         piocb->iocb_flag |= LPFC_IO_ON_Q;
1235         pring->txcmplq_cnt++;
1236         if (pring->txcmplq_cnt > pring->txcmplq_max)
1237                 pring->txcmplq_max = pring->txcmplq_cnt;
1238
1239         if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
1240            (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
1241            (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
1242                 if (!piocb->vport)
1243                         BUG();
1244                 else
1245                         mod_timer(&piocb->vport->els_tmofunc,
1246                                   jiffies + HZ * (phba->fc_ratov << 1));
1247         }
1248
1249
1250         return 0;
1251 }
1252
1253 /**
1254  * lpfc_sli_ringtx_get - Get first element of the txq
1255  * @phba: Pointer to HBA context object.
1256  * @pring: Pointer to driver SLI ring object.
1257  *
1258  * This function is called with hbalock held to get next
1259  * iocb in txq of the given ring. If there is any iocb in
1260  * the txq, the function returns first iocb in the list after
1261  * removing the iocb from the list, else it returns NULL.
1262  **/
1263 struct lpfc_iocbq *
1264 lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1265 {
1266         struct lpfc_iocbq *cmd_iocb;
1267
1268         list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
1269         if (cmd_iocb != NULL)
1270                 pring->txq_cnt--;
1271         return cmd_iocb;
1272 }
1273
1274 /**
1275  * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
1276  * @phba: Pointer to HBA context object.
1277  * @pring: Pointer to driver SLI ring object.
1278  *
1279  * This function is called with hbalock held and the caller must post the
1280  * iocb without releasing the lock. If the caller releases the lock,
1281  * iocb slot returned by the function is not guaranteed to be available.
1282  * The function returns pointer to the next available iocb slot if there
1283  * is available slot in the ring, else it returns NULL.
1284  * If the get index of the ring is ahead of the put index, the function
1285  * will post an error attention event to the worker thread to take the
1286  * HBA to offline state.
1287  **/
1288 static IOCB_t *
1289 lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1290 {
1291         struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
1292         uint32_t  max_cmd_idx = pring->numCiocb;
1293         if ((pring->next_cmdidx == pring->cmdidx) &&
1294            (++pring->next_cmdidx >= max_cmd_idx))
1295                 pring->next_cmdidx = 0;
1296
1297         if (unlikely(pring->local_getidx == pring->next_cmdidx)) {
1298
1299                 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
1300
1301                 if (unlikely(pring->local_getidx >= max_cmd_idx)) {
1302                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
1303                                         "0315 Ring %d issue: portCmdGet %d "
1304                                         "is bigger than cmd ring %d\n",
1305                                         pring->ringno,
1306                                         pring->local_getidx, max_cmd_idx);
1307
1308                         phba->link_state = LPFC_HBA_ERROR;
1309                         /*
1310                          * All error attention handlers are posted to
1311                          * worker thread
1312                          */
1313                         phba->work_ha |= HA_ERATT;
1314                         phba->work_hs = HS_FFER3;
1315
1316                         lpfc_worker_wake_up(phba);
1317
1318                         return NULL;
1319                 }
1320
1321                 if (pring->local_getidx == pring->next_cmdidx)
1322                         return NULL;
1323         }
1324
1325         return lpfc_cmd_iocb(phba, pring);
1326 }
1327
1328 /**
1329  * lpfc_sli_next_iotag - Get an iotag for the iocb
1330  * @phba: Pointer to HBA context object.
1331  * @iocbq: Pointer to driver iocb object.
1332  *
1333  * This function gets an iotag for the iocb. If there is no unused iotag and
1334  * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
1335  * array and assigns a new iotag.
1336  * The function returns the allocated iotag if successful, else returns zero.
1337  * Zero is not a valid iotag.
1338  * The caller is not required to hold any lock.
1339  **/
1340 uint16_t
1341 lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1342 {
1343         struct lpfc_iocbq **new_arr;
1344         struct lpfc_iocbq **old_arr;
1345         size_t new_len;
1346         struct lpfc_sli *psli = &phba->sli;
1347         uint16_t iotag;
1348
1349         spin_lock_irq(&phba->hbalock);
1350         iotag = psli->last_iotag;
1351         if(++iotag < psli->iocbq_lookup_len) {
1352                 psli->last_iotag = iotag;
1353                 psli->iocbq_lookup[iotag] = iocbq;
1354                 spin_unlock_irq(&phba->hbalock);
1355                 iocbq->iotag = iotag;
1356                 return iotag;
1357         } else if (psli->iocbq_lookup_len < (0xffff
1358                                            - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
1359                 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
1360                 spin_unlock_irq(&phba->hbalock);
1361                 new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
1362                                   GFP_KERNEL);
1363                 if (new_arr) {
1364                         spin_lock_irq(&phba->hbalock);
1365                         old_arr = psli->iocbq_lookup;
1366                         if (new_len <= psli->iocbq_lookup_len) {
1367                                 /* highly unprobable case */
1368                                 kfree(new_arr);
1369                                 iotag = psli->last_iotag;
1370                                 if(++iotag < psli->iocbq_lookup_len) {
1371                                         psli->last_iotag = iotag;
1372                                         psli->iocbq_lookup[iotag] = iocbq;
1373                                         spin_unlock_irq(&phba->hbalock);
1374                                         iocbq->iotag = iotag;
1375                                         return iotag;
1376                                 }
1377                                 spin_unlock_irq(&phba->hbalock);
1378                                 return 0;
1379                         }
1380                         if (psli->iocbq_lookup)
1381                                 memcpy(new_arr, old_arr,
1382                                        ((psli->last_iotag  + 1) *
1383                                         sizeof (struct lpfc_iocbq *)));
1384                         psli->iocbq_lookup = new_arr;
1385                         psli->iocbq_lookup_len = new_len;
1386                         psli->last_iotag = iotag;
1387                         psli->iocbq_lookup[iotag] = iocbq;
1388                         spin_unlock_irq(&phba->hbalock);
1389                         iocbq->iotag = iotag;
1390                         kfree(old_arr);
1391                         return iotag;
1392                 }
1393         } else
1394                 spin_unlock_irq(&phba->hbalock);
1395
1396         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
1397                         "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
1398                         psli->last_iotag);
1399
1400         return 0;
1401 }
1402
1403 /**
1404  * lpfc_sli_submit_iocb - Submit an iocb to the firmware
1405  * @phba: Pointer to HBA context object.
1406  * @pring: Pointer to driver SLI ring object.
1407  * @iocb: Pointer to iocb slot in the ring.
1408  * @nextiocb: Pointer to driver iocb object which need to be
1409  *            posted to firmware.
1410  *
1411  * This function is called with hbalock held to post a new iocb to
1412  * the firmware. This function copies the new iocb to ring iocb slot and
1413  * updates the ring pointers. It adds the new iocb to txcmplq if there is
1414  * a completion call back for this iocb else the function will free the
1415  * iocb object.
1416  **/
1417 static void
1418 lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1419                 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
1420 {
1421         /*
1422          * Set up an iotag
1423          */
1424         nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
1425
1426
1427         if (pring->ringno == LPFC_ELS_RING) {
1428                 lpfc_debugfs_slow_ring_trc(phba,
1429                         "IOCB cmd ring:   wd4:x%08x wd6:x%08x wd7:x%08x",
1430                         *(((uint32_t *) &nextiocb->iocb) + 4),
1431                         *(((uint32_t *) &nextiocb->iocb) + 6),
1432                         *(((uint32_t *) &nextiocb->iocb) + 7));
1433         }
1434
1435         /*
1436          * Issue iocb command to adapter
1437          */
1438         lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
1439         wmb();
1440         pring->stats.iocb_cmd++;
1441
1442         /*
1443          * If there is no completion routine to call, we can release the
1444          * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
1445          * that have no rsp ring completion, iocb_cmpl MUST be NULL.
1446          */
1447         if (nextiocb->iocb_cmpl)
1448                 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
1449         else
1450                 __lpfc_sli_release_iocbq(phba, nextiocb);
1451
1452         /*
1453          * Let the HBA know what IOCB slot will be the next one the
1454          * driver will put a command into.
1455          */
1456         pring->cmdidx = pring->next_cmdidx;
1457         writel(pring->cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
1458 }
1459
1460 /**
1461  * lpfc_sli_update_full_ring - Update the chip attention register
1462  * @phba: Pointer to HBA context object.
1463  * @pring: Pointer to driver SLI ring object.
1464  *
1465  * The caller is not required to hold any lock for calling this function.
1466  * This function updates the chip attention bits for the ring to inform firmware
1467  * that there are pending work to be done for this ring and requests an
1468  * interrupt when there is space available in the ring. This function is
1469  * called when the driver is unable to post more iocbs to the ring due
1470  * to unavailability of space in the ring.
1471  **/
1472 static void
1473 lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1474 {
1475         int ringno = pring->ringno;
1476
1477         pring->flag |= LPFC_CALL_RING_AVAILABLE;
1478
1479         wmb();
1480
1481         /*
1482          * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
1483          * The HBA will tell us when an IOCB entry is available.
1484          */
1485         writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
1486         readl(phba->CAregaddr); /* flush */
1487
1488         pring->stats.iocb_cmd_full++;
1489 }
1490
1491 /**
1492  * lpfc_sli_update_ring - Update chip attention register
1493  * @phba: Pointer to HBA context object.
1494  * @pring: Pointer to driver SLI ring object.
1495  *
1496  * This function updates the chip attention register bit for the
1497  * given ring to inform HBA that there is more work to be done
1498  * in this ring. The caller is not required to hold any lock.
1499  **/
1500 static void
1501 lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1502 {
1503         int ringno = pring->ringno;
1504
1505         /*
1506          * Tell the HBA that there is work to do in this ring.
1507          */
1508         if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
1509                 wmb();
1510                 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
1511                 readl(phba->CAregaddr); /* flush */
1512         }
1513 }
1514
1515 /**
1516  * lpfc_sli_resume_iocb - Process iocbs in the txq
1517  * @phba: Pointer to HBA context object.
1518  * @pring: Pointer to driver SLI ring object.
1519  *
1520  * This function is called with hbalock held to post pending iocbs
1521  * in the txq to the firmware. This function is called when driver
1522  * detects space available in the ring.
1523  **/
1524 static void
1525 lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1526 {
1527         IOCB_t *iocb;
1528         struct lpfc_iocbq *nextiocb;
1529
1530         /*
1531          * Check to see if:
1532          *  (a) there is anything on the txq to send
1533          *  (b) link is up
1534          *  (c) link attention events can be processed (fcp ring only)
1535          *  (d) IOCB processing is not blocked by the outstanding mbox command.
1536          */
1537         if (pring->txq_cnt &&
1538             lpfc_is_link_up(phba) &&
1539             (pring->ringno != phba->sli.fcp_ring ||
1540              phba->sli.sli_flag & LPFC_PROCESS_LA)) {
1541
1542                 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
1543                        (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
1544                         lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
1545
1546                 if (iocb)
1547                         lpfc_sli_update_ring(phba, pring);
1548                 else
1549                         lpfc_sli_update_full_ring(phba, pring);
1550         }
1551
1552         return;
1553 }
1554
1555 /**
1556  * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
1557  * @phba: Pointer to HBA context object.
1558  * @hbqno: HBQ number.
1559  *
1560  * This function is called with hbalock held to get the next
1561  * available slot for the given HBQ. If there is free slot
1562  * available for the HBQ it will return pointer to the next available
1563  * HBQ entry else it will return NULL.
1564  **/
1565 static struct lpfc_hbq_entry *
1566 lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
1567 {
1568         struct hbq_s *hbqp = &phba->hbqs[hbqno];
1569
1570         if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
1571             ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
1572                 hbqp->next_hbqPutIdx = 0;
1573
1574         if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
1575                 uint32_t raw_index = phba->hbq_get[hbqno];
1576                 uint32_t getidx = le32_to_cpu(raw_index);
1577
1578                 hbqp->local_hbqGetIdx = getidx;
1579
1580                 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
1581                         lpfc_printf_log(phba, KERN_ERR,
1582                                         LOG_SLI | LOG_VPORT,
1583                                         "1802 HBQ %d: local_hbqGetIdx "
1584                                         "%u is > than hbqp->entry_count %u\n",
1585                                         hbqno, hbqp->local_hbqGetIdx,
1586                                         hbqp->entry_count);
1587
1588                         phba->link_state = LPFC_HBA_ERROR;
1589                         return NULL;
1590                 }
1591
1592                 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
1593                         return NULL;
1594         }
1595
1596         return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
1597                         hbqp->hbqPutIdx;
1598 }
1599
1600 /**
1601  * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
1602  * @phba: Pointer to HBA context object.
1603  *
1604  * This function is called with no lock held to free all the
1605  * hbq buffers while uninitializing the SLI interface. It also
1606  * frees the HBQ buffers returned by the firmware but not yet
1607  * processed by the upper layers.
1608  **/
1609 void
1610 lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
1611 {
1612         struct lpfc_dmabuf *dmabuf, *next_dmabuf;
1613         struct hbq_dmabuf *hbq_buf;
1614         unsigned long flags;
1615         int i, hbq_count;
1616         uint32_t hbqno;
1617
1618         hbq_count = lpfc_sli_hbq_count();
1619         /* Return all memory used by all HBQs */
1620         spin_lock_irqsave(&phba->hbalock, flags);
1621         for (i = 0; i < hbq_count; ++i) {
1622                 list_for_each_entry_safe(dmabuf, next_dmabuf,
1623                                 &phba->hbqs[i].hbq_buffer_list, list) {
1624                         hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1625                         list_del(&hbq_buf->dbuf.list);
1626                         (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
1627                 }
1628                 phba->hbqs[i].buffer_count = 0;
1629         }
1630         /* Return all HBQ buffer that are in-fly */
1631         list_for_each_entry_safe(dmabuf, next_dmabuf, &phba->rb_pend_list,
1632                                  list) {
1633                 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1634                 list_del(&hbq_buf->dbuf.list);
1635                 if (hbq_buf->tag == -1) {
1636                         (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1637                                 (phba, hbq_buf);
1638                 } else {
1639                         hbqno = hbq_buf->tag >> 16;
1640                         if (hbqno >= LPFC_MAX_HBQS)
1641                                 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1642                                         (phba, hbq_buf);
1643                         else
1644                                 (phba->hbqs[hbqno].hbq_free_buffer)(phba,
1645                                         hbq_buf);
1646                 }
1647         }
1648
1649         /* Mark the HBQs not in use */
1650         phba->hbq_in_use = 0;
1651         spin_unlock_irqrestore(&phba->hbalock, flags);
1652 }
1653
1654 /**
1655  * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
1656  * @phba: Pointer to HBA context object.
1657  * @hbqno: HBQ number.
1658  * @hbq_buf: Pointer to HBQ buffer.
1659  *
1660  * This function is called with the hbalock held to post a
1661  * hbq buffer to the firmware. If the function finds an empty
1662  * slot in the HBQ, it will post the buffer. The function will return
1663  * pointer to the hbq entry if it successfully post the buffer
1664  * else it will return NULL.
1665  **/
1666 static int
1667 lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
1668                          struct hbq_dmabuf *hbq_buf)
1669 {
1670         return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
1671 }
1672
1673 /**
1674  * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
1675  * @phba: Pointer to HBA context object.
1676  * @hbqno: HBQ number.
1677  * @hbq_buf: Pointer to HBQ buffer.
1678  *
1679  * This function is called with the hbalock held to post a hbq buffer to the
1680  * firmware. If the function finds an empty slot in the HBQ, it will post the
1681  * buffer and place it on the hbq_buffer_list. The function will return zero if
1682  * it successfully post the buffer else it will return an error.
1683  **/
1684 static int
1685 lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
1686                             struct hbq_dmabuf *hbq_buf)
1687 {
1688         struct lpfc_hbq_entry *hbqe;
1689         dma_addr_t physaddr = hbq_buf->dbuf.phys;
1690
1691         /* Get next HBQ entry slot to use */
1692         hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
1693         if (hbqe) {
1694                 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1695
1696                 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1697                 hbqe->bde.addrLow  = le32_to_cpu(putPaddrLow(physaddr));
1698                 hbqe->bde.tus.f.bdeSize = hbq_buf->size;
1699                 hbqe->bde.tus.f.bdeFlags = 0;
1700                 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
1701                 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
1702                                 /* Sync SLIM */
1703                 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
1704                 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
1705                                 /* flush */
1706                 readl(phba->hbq_put + hbqno);
1707                 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
1708                 return 0;
1709         } else
1710                 return -ENOMEM;
1711 }
1712
1713 /**
1714  * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
1715  * @phba: Pointer to HBA context object.
1716  * @hbqno: HBQ number.
1717  * @hbq_buf: Pointer to HBQ buffer.
1718  *
1719  * This function is called with the hbalock held to post an RQE to the SLI4
1720  * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
1721  * the hbq_buffer_list and return zero, otherwise it will return an error.
1722  **/
1723 static int
1724 lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
1725                             struct hbq_dmabuf *hbq_buf)
1726 {
1727         int rc;
1728         struct lpfc_rqe hrqe;
1729         struct lpfc_rqe drqe;
1730
1731         hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
1732         hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
1733         drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
1734         drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
1735         rc = lpfc_sli4_rq_put(phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
1736                               &hrqe, &drqe);
1737         if (rc < 0)
1738                 return rc;
1739         hbq_buf->tag = rc;
1740         list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
1741         return 0;
1742 }
1743
1744 /* HBQ for ELS and CT traffic. */
1745 static struct lpfc_hbq_init lpfc_els_hbq = {
1746         .rn = 1,
1747         .entry_count = 256,
1748         .mask_count = 0,
1749         .profile = 0,
1750         .ring_mask = (1 << LPFC_ELS_RING),
1751         .buffer_count = 0,
1752         .init_count = 40,
1753         .add_count = 40,
1754 };
1755
1756 /* HBQ for the extra ring if needed */
1757 static struct lpfc_hbq_init lpfc_extra_hbq = {
1758         .rn = 1,
1759         .entry_count = 200,
1760         .mask_count = 0,
1761         .profile = 0,
1762         .ring_mask = (1 << LPFC_EXTRA_RING),
1763         .buffer_count = 0,
1764         .init_count = 0,
1765         .add_count = 5,
1766 };
1767
1768 /* Array of HBQs */
1769 struct lpfc_hbq_init *lpfc_hbq_defs[] = {
1770         &lpfc_els_hbq,
1771         &lpfc_extra_hbq,
1772 };
1773
1774 /**
1775  * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
1776  * @phba: Pointer to HBA context object.
1777  * @hbqno: HBQ number.
1778  * @count: Number of HBQ buffers to be posted.
1779  *
1780  * This function is called with no lock held to post more hbq buffers to the
1781  * given HBQ. The function returns the number of HBQ buffers successfully
1782  * posted.
1783  **/
1784 static int
1785 lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
1786 {
1787         uint32_t i, posted = 0;
1788         unsigned long flags;
1789         struct hbq_dmabuf *hbq_buffer;
1790         LIST_HEAD(hbq_buf_list);
1791         if (!phba->hbqs[hbqno].hbq_alloc_buffer)
1792                 return 0;
1793
1794         if ((phba->hbqs[hbqno].buffer_count + count) >
1795             lpfc_hbq_defs[hbqno]->entry_count)
1796                 count = lpfc_hbq_defs[hbqno]->entry_count -
1797                                         phba->hbqs[hbqno].buffer_count;
1798         if (!count)
1799                 return 0;
1800         /* Allocate HBQ entries */
1801         for (i = 0; i < count; i++) {
1802                 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
1803                 if (!hbq_buffer)
1804                         break;
1805                 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
1806         }
1807         /* Check whether HBQ is still in use */
1808         spin_lock_irqsave(&phba->hbalock, flags);
1809         if (!phba->hbq_in_use)
1810                 goto err;
1811         while (!list_empty(&hbq_buf_list)) {
1812                 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1813                                  dbuf.list);
1814                 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
1815                                       (hbqno << 16));
1816                 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
1817                         phba->hbqs[hbqno].buffer_count++;
1818                         posted++;
1819                 } else
1820                         (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1821         }
1822         spin_unlock_irqrestore(&phba->hbalock, flags);
1823         return posted;
1824 err:
1825         spin_unlock_irqrestore(&phba->hbalock, flags);
1826         while (!list_empty(&hbq_buf_list)) {
1827                 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1828                                  dbuf.list);
1829                 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1830         }
1831         return 0;
1832 }
1833
1834 /**
1835  * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
1836  * @phba: Pointer to HBA context object.
1837  * @qno: HBQ number.
1838  *
1839  * This function posts more buffers to the HBQ. This function
1840  * is called with no lock held. The function returns the number of HBQ entries
1841  * successfully allocated.
1842  **/
1843 int
1844 lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
1845 {
1846         if (phba->sli_rev == LPFC_SLI_REV4)
1847                 return 0;
1848         else
1849                 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1850                                          lpfc_hbq_defs[qno]->add_count);
1851 }
1852
1853 /**
1854  * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
1855  * @phba: Pointer to HBA context object.
1856  * @qno:  HBQ queue number.
1857  *
1858  * This function is called from SLI initialization code path with
1859  * no lock held to post initial HBQ buffers to firmware. The
1860  * function returns the number of HBQ entries successfully allocated.
1861  **/
1862 static int
1863 lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
1864 {
1865         if (phba->sli_rev == LPFC_SLI_REV4)
1866                 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1867                                          lpfc_hbq_defs[qno]->entry_count);
1868         else
1869                 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1870                                          lpfc_hbq_defs[qno]->init_count);
1871 }
1872
1873 /**
1874  * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
1875  * @phba: Pointer to HBA context object.
1876  * @hbqno: HBQ number.
1877  *
1878  * This function removes the first hbq buffer on an hbq list and returns a
1879  * pointer to that buffer. If it finds no buffers on the list it returns NULL.
1880  **/
1881 static struct hbq_dmabuf *
1882 lpfc_sli_hbqbuf_get(struct list_head *rb_list)
1883 {
1884         struct lpfc_dmabuf *d_buf;
1885
1886         list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
1887         if (!d_buf)
1888                 return NULL;
1889         return container_of(d_buf, struct hbq_dmabuf, dbuf);
1890 }
1891
1892 /**
1893  * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
1894  * @phba: Pointer to HBA context object.
1895  * @tag: Tag of the hbq buffer.
1896  *
1897  * This function is called with hbalock held. This function searches
1898  * for the hbq buffer associated with the given tag in the hbq buffer
1899  * list. If it finds the hbq buffer, it returns the hbq_buffer other wise
1900  * it returns NULL.
1901  **/
1902 static struct hbq_dmabuf *
1903 lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
1904 {
1905         struct lpfc_dmabuf *d_buf;
1906         struct hbq_dmabuf *hbq_buf;
1907         uint32_t hbqno;
1908
1909         hbqno = tag >> 16;
1910         if (hbqno >= LPFC_MAX_HBQS)
1911                 return NULL;
1912
1913         spin_lock_irq(&phba->hbalock);
1914         list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
1915                 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
1916                 if (hbq_buf->tag == tag) {
1917                         spin_unlock_irq(&phba->hbalock);
1918                         return hbq_buf;
1919                 }
1920         }
1921         spin_unlock_irq(&phba->hbalock);
1922         lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
1923                         "1803 Bad hbq tag. Data: x%x x%x\n",
1924                         tag, phba->hbqs[tag >> 16].buffer_count);
1925         return NULL;
1926 }
1927
1928 /**
1929  * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
1930  * @phba: Pointer to HBA context object.
1931  * @hbq_buffer: Pointer to HBQ buffer.
1932  *
1933  * This function is called with hbalock. This function gives back
1934  * the hbq buffer to firmware. If the HBQ does not have space to
1935  * post the buffer, it will free the buffer.
1936  **/
1937 void
1938 lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
1939 {
1940         uint32_t hbqno;
1941
1942         if (hbq_buffer) {
1943                 hbqno = hbq_buffer->tag >> 16;
1944                 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
1945                         (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1946         }
1947 }
1948
1949 /**
1950  * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
1951  * @mbxCommand: mailbox command code.
1952  *
1953  * This function is called by the mailbox event handler function to verify
1954  * that the completed mailbox command is a legitimate mailbox command. If the
1955  * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
1956  * and the mailbox event handler will take the HBA offline.
1957  **/
1958 static int
1959 lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
1960 {
1961         uint8_t ret;
1962
1963         switch (mbxCommand) {
1964         case MBX_LOAD_SM:
1965         case MBX_READ_NV:
1966         case MBX_WRITE_NV:
1967         case MBX_WRITE_VPARMS:
1968         case MBX_RUN_BIU_DIAG:
1969         case MBX_INIT_LINK:
1970         case MBX_DOWN_LINK:
1971         case MBX_CONFIG_LINK:
1972         case MBX_CONFIG_RING:
1973         case MBX_RESET_RING:
1974         case MBX_READ_CONFIG:
1975         case MBX_READ_RCONFIG:
1976         case MBX_READ_SPARM:
1977         case MBX_READ_STATUS:
1978         case MBX_READ_RPI:
1979         case MBX_READ_XRI:
1980         case MBX_READ_REV:
1981         case MBX_READ_LNK_STAT:
1982         case MBX_REG_LOGIN:
1983         case MBX_UNREG_LOGIN:
1984         case MBX_CLEAR_LA:
1985         case MBX_DUMP_MEMORY:
1986         case MBX_DUMP_CONTEXT:
1987         case MBX_RUN_DIAGS:
1988         case MBX_RESTART:
1989         case MBX_UPDATE_CFG:
1990         case MBX_DOWN_LOAD:
1991         case MBX_DEL_LD_ENTRY:
1992         case MBX_RUN_PROGRAM:
1993         case MBX_SET_MASK:
1994         case MBX_SET_VARIABLE:
1995         case MBX_UNREG_D_ID:
1996         case MBX_KILL_BOARD:
1997         case MBX_CONFIG_FARP:
1998         case MBX_BEACON:
1999         case MBX_LOAD_AREA:
2000         case MBX_RUN_BIU_DIAG64:
2001         case MBX_CONFIG_PORT:
2002         case MBX_READ_SPARM64:
2003         case MBX_READ_RPI64:
2004         case MBX_REG_LOGIN64:
2005         case MBX_READ_TOPOLOGY:
2006         case MBX_WRITE_WWN:
2007         case MBX_SET_DEBUG:
2008         case MBX_LOAD_EXP_ROM:
2009         case MBX_ASYNCEVT_ENABLE:
2010         case MBX_REG_VPI:
2011         case MBX_UNREG_VPI:
2012         case MBX_HEARTBEAT:
2013         case MBX_PORT_CAPABILITIES:
2014         case MBX_PORT_IOV_CONTROL:
2015         case MBX_SLI4_CONFIG:
2016         case MBX_SLI4_REQ_FTRS:
2017         case MBX_REG_FCFI:
2018         case MBX_UNREG_FCFI:
2019         case MBX_REG_VFI:
2020         case MBX_UNREG_VFI:
2021         case MBX_INIT_VPI:
2022         case MBX_INIT_VFI:
2023         case MBX_RESUME_RPI:
2024         case MBX_READ_EVENT_LOG_STATUS:
2025         case MBX_READ_EVENT_LOG:
2026         case MBX_SECURITY_MGMT:
2027         case MBX_AUTH_PORT:
2028                 ret = mbxCommand;
2029                 break;
2030         default:
2031                 ret = MBX_SHUTDOWN;
2032                 break;
2033         }
2034         return ret;
2035 }
2036
2037 /**
2038  * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
2039  * @phba: Pointer to HBA context object.
2040  * @pmboxq: Pointer to mailbox command.
2041  *
2042  * This is completion handler function for mailbox commands issued from
2043  * lpfc_sli_issue_mbox_wait function. This function is called by the
2044  * mailbox event handler function with no lock held. This function
2045  * will wake up thread waiting on the wait queue pointed by context1
2046  * of the mailbox.
2047  **/
2048 void
2049 lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
2050 {
2051         wait_queue_head_t *pdone_q;
2052         unsigned long drvr_flag;
2053
2054         /*
2055          * If pdone_q is empty, the driver thread gave up waiting and
2056          * continued running.
2057          */
2058         pmboxq->mbox_flag |= LPFC_MBX_WAKE;
2059         spin_lock_irqsave(&phba->hbalock, drvr_flag);
2060         pdone_q = (wait_queue_head_t *) pmboxq->context1;
2061         if (pdone_q)
2062                 wake_up_interruptible(pdone_q);
2063         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
2064         return;
2065 }
2066
2067
2068 /**
2069  * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
2070  * @phba: Pointer to HBA context object.
2071  * @pmb: Pointer to mailbox object.
2072  *
2073  * This function is the default mailbox completion handler. It
2074  * frees the memory resources associated with the completed mailbox
2075  * command. If the completed command is a REG_LOGIN mailbox command,
2076  * this function will issue a UREG_LOGIN to re-claim the RPI.
2077  **/
2078 void
2079 lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2080 {
2081         struct lpfc_vport  *vport = pmb->vport;
2082         struct lpfc_dmabuf *mp;
2083         struct lpfc_nodelist *ndlp;
2084         struct Scsi_Host *shost;
2085         uint16_t rpi, vpi;
2086         int rc;
2087
2088         mp = (struct lpfc_dmabuf *) (pmb->context1);
2089
2090         if (mp) {
2091                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2092                 kfree(mp);
2093         }
2094
2095         /*
2096          * If a REG_LOGIN succeeded  after node is destroyed or node
2097          * is in re-discovery driver need to cleanup the RPI.
2098          */
2099         if (!(phba->pport->load_flag & FC_UNLOADING) &&
2100             pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
2101             !pmb->u.mb.mbxStatus) {
2102                 rpi = pmb->u.mb.un.varWords[0];
2103                 vpi = pmb->u.mb.un.varRegLogin.vpi;
2104                 lpfc_unreg_login(phba, vpi, rpi, pmb);
2105                 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
2106                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2107                 if (rc != MBX_NOT_FINISHED)
2108                         return;
2109         }
2110
2111         if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
2112                 !(phba->pport->load_flag & FC_UNLOADING) &&
2113                 !pmb->u.mb.mbxStatus) {
2114                 shost = lpfc_shost_from_vport(vport);
2115                 spin_lock_irq(shost->host_lock);
2116                 vport->vpi_state |= LPFC_VPI_REGISTERED;
2117                 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
2118                 spin_unlock_irq(shost->host_lock);
2119         }
2120
2121         if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
2122                 ndlp = (struct lpfc_nodelist *)pmb->context2;
2123                 lpfc_nlp_put(ndlp);
2124                 pmb->context2 = NULL;
2125         }
2126
2127         /* Check security permission status on INIT_LINK mailbox command */
2128         if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
2129             (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
2130                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
2131                                 "2860 SLI authentication is required "
2132                                 "for INIT_LINK but has not done yet\n");
2133
2134         if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
2135                 lpfc_sli4_mbox_cmd_free(phba, pmb);
2136         else
2137                 mempool_free(pmb, phba->mbox_mem_pool);
2138 }
2139
2140 /**
2141  * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
2142  * @phba: Pointer to HBA context object.
2143  *
2144  * This function is called with no lock held. This function processes all
2145  * the completed mailbox commands and gives it to upper layers. The interrupt
2146  * service routine processes mailbox completion interrupt and adds completed
2147  * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
2148  * Worker thread call lpfc_sli_handle_mb_event, which will return the
2149  * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
2150  * function returns the mailbox commands to the upper layer by calling the
2151  * completion handler function of each mailbox.
2152  **/
2153 int
2154 lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
2155 {
2156         MAILBOX_t *pmbox;
2157         LPFC_MBOXQ_t *pmb;
2158         int rc;
2159         LIST_HEAD(cmplq);
2160
2161         phba->sli.slistat.mbox_event++;
2162
2163         /* Get all completed mailboxe buffers into the cmplq */
2164         spin_lock_irq(&phba->hbalock);
2165         list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
2166         spin_unlock_irq(&phba->hbalock);
2167
2168         /* Get a Mailbox buffer to setup mailbox commands for callback */
2169         do {
2170                 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
2171                 if (pmb == NULL)
2172                         break;
2173
2174                 pmbox = &pmb->u.mb;
2175
2176                 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
2177                         if (pmb->vport) {
2178                                 lpfc_debugfs_disc_trc(pmb->vport,
2179                                         LPFC_DISC_TRC_MBOX_VPORT,
2180                                         "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
2181                                         (uint32_t)pmbox->mbxCommand,
2182                                         pmbox->un.varWords[0],
2183                                         pmbox->un.varWords[1]);
2184                         }
2185                         else {
2186                                 lpfc_debugfs_disc_trc(phba->pport,
2187                                         LPFC_DISC_TRC_MBOX,
2188                                         "MBOX cmpl:       cmd:x%x mb:x%x x%x",
2189                                         (uint32_t)pmbox->mbxCommand,
2190                                         pmbox->un.varWords[0],
2191                                         pmbox->un.varWords[1]);
2192                         }
2193                 }
2194
2195                 /*
2196                  * It is a fatal error if unknown mbox command completion.
2197                  */
2198                 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
2199                     MBX_SHUTDOWN) {
2200                         /* Unknown mailbox command compl */
2201                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
2202                                         "(%d):0323 Unknown Mailbox command "
2203                                         "x%x (x%x) Cmpl\n",
2204                                         pmb->vport ? pmb->vport->vpi : 0,
2205                                         pmbox->mbxCommand,
2206                                         lpfc_sli4_mbox_opcode_get(phba, pmb));
2207                         phba->link_state = LPFC_HBA_ERROR;
2208                         phba->work_hs = HS_FFER3;
2209                         lpfc_handle_eratt(phba);
2210                         continue;
2211                 }
2212
2213                 if (pmbox->mbxStatus) {
2214                         phba->sli.slistat.mbox_stat_err++;
2215                         if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
2216                                 /* Mbox cmd cmpl error - RETRYing */
2217                                 lpfc_printf_log(phba, KERN_INFO,
2218                                                 LOG_MBOX | LOG_SLI,
2219                                                 "(%d):0305 Mbox cmd cmpl "
2220                                                 "error - RETRYing Data: x%x "
2221                                                 "(x%x) x%x x%x x%x\n",
2222                                                 pmb->vport ? pmb->vport->vpi :0,
2223                                                 pmbox->mbxCommand,
2224                                                 lpfc_sli4_mbox_opcode_get(phba,
2225                                                                           pmb),
2226                                                 pmbox->mbxStatus,
2227                                                 pmbox->un.varWords[0],
2228                                                 pmb->vport->port_state);
2229                                 pmbox->mbxStatus = 0;
2230                                 pmbox->mbxOwner = OWN_HOST;
2231                                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2232                                 if (rc != MBX_NOT_FINISHED)
2233                                         continue;
2234                         }
2235                 }
2236
2237                 /* Mailbox cmd <cmd> Cmpl <cmpl> */
2238                 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
2239                                 "(%d):0307 Mailbox cmd x%x (x%x) Cmpl x%p "
2240                                 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x\n",
2241                                 pmb->vport ? pmb->vport->vpi : 0,
2242                                 pmbox->mbxCommand,
2243                                 lpfc_sli4_mbox_opcode_get(phba, pmb),
2244                                 pmb->mbox_cmpl,
2245                                 *((uint32_t *) pmbox),
2246                                 pmbox->un.varWords[0],
2247                                 pmbox->un.varWords[1],
2248                                 pmbox->un.varWords[2],
2249                                 pmbox->un.varWords[3],
2250                                 pmbox->un.varWords[4],
2251                                 pmbox->un.varWords[5],
2252                                 pmbox->un.varWords[6],
2253                                 pmbox->un.varWords[7]);
2254
2255                 if (pmb->mbox_cmpl)
2256                         pmb->mbox_cmpl(phba,pmb);
2257         } while (1);
2258         return 0;
2259 }
2260
2261 /**
2262  * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
2263  * @phba: Pointer to HBA context object.
2264  * @pring: Pointer to driver SLI ring object.
2265  * @tag: buffer tag.
2266  *
2267  * This function is called with no lock held. When QUE_BUFTAG_BIT bit
2268  * is set in the tag the buffer is posted for a particular exchange,
2269  * the function will return the buffer without replacing the buffer.
2270  * If the buffer is for unsolicited ELS or CT traffic, this function
2271  * returns the buffer and also posts another buffer to the firmware.
2272  **/
2273 static struct lpfc_dmabuf *
2274 lpfc_sli_get_buff(struct lpfc_hba *phba,
2275                   struct lpfc_sli_ring *pring,
2276                   uint32_t tag)
2277 {
2278         struct hbq_dmabuf *hbq_entry;
2279
2280         if (tag & QUE_BUFTAG_BIT)
2281                 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
2282         hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
2283         if (!hbq_entry)
2284                 return NULL;
2285         return &hbq_entry->dbuf;
2286 }
2287
2288 /**
2289  * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
2290  * @phba: Pointer to HBA context object.
2291  * @pring: Pointer to driver SLI ring object.
2292  * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
2293  * @fch_r_ctl: the r_ctl for the first frame of the sequence.
2294  * @fch_type: the type for the first frame of the sequence.
2295  *
2296  * This function is called with no lock held. This function uses the r_ctl and
2297  * type of the received sequence to find the correct callback function to call
2298  * to process the sequence.
2299  **/
2300 static int
2301 lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2302                          struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
2303                          uint32_t fch_type)
2304 {
2305         int i;
2306
2307         /* unSolicited Responses */
2308         if (pring->prt[0].profile) {
2309                 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
2310                         (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
2311                                                                         saveq);
2312                 return 1;
2313         }
2314         /* We must search, based on rctl / type
2315            for the right routine */
2316         for (i = 0; i < pring->num_mask; i++) {
2317                 if ((pring->prt[i].rctl == fch_r_ctl) &&
2318                     (pring->prt[i].type == fch_type)) {
2319                         if (pring->prt[i].lpfc_sli_rcv_unsol_event)
2320                                 (pring->prt[i].lpfc_sli_rcv_unsol_event)
2321                                                 (phba, pring, saveq);
2322                         return 1;
2323                 }
2324         }
2325         return 0;
2326 }
2327
2328 /**
2329  * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
2330  * @phba: Pointer to HBA context object.
2331  * @pring: Pointer to driver SLI ring object.
2332  * @saveq: Pointer to the unsolicited iocb.
2333  *
2334  * This function is called with no lock held by the ring event handler
2335  * when there is an unsolicited iocb posted to the response ring by the
2336  * firmware. This function gets the buffer associated with the iocbs
2337  * and calls the event handler for the ring. This function handles both
2338  * qring buffers and hbq buffers.
2339  * When the function returns 1 the caller can free the iocb object otherwise
2340  * upper layer functions will free the iocb objects.
2341  **/
2342 static int
2343 lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2344                             struct lpfc_iocbq *saveq)
2345 {
2346         IOCB_t           * irsp;
2347         WORD5            * w5p;
2348         uint32_t           Rctl, Type;
2349         uint32_t           match;
2350         struct lpfc_iocbq *iocbq;
2351         struct lpfc_dmabuf *dmzbuf;
2352
2353         match = 0;
2354         irsp = &(saveq->iocb);
2355
2356         if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
2357                 if (pring->lpfc_sli_rcv_async_status)
2358                         pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
2359                 else
2360                         lpfc_printf_log(phba,
2361                                         KERN_WARNING,
2362                                         LOG_SLI,
2363                                         "0316 Ring %d handler: unexpected "
2364                                         "ASYNC_STATUS iocb received evt_code "
2365                                         "0x%x\n",
2366                                         pring->ringno,
2367                                         irsp->un.asyncstat.evt_code);
2368                 return 1;
2369         }
2370
2371         if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
2372                 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
2373                 if (irsp->ulpBdeCount > 0) {
2374                         dmzbuf = lpfc_sli_get_buff(phba, pring,
2375                                         irsp->un.ulpWord[3]);
2376                         lpfc_in_buf_free(phba, dmzbuf);
2377                 }
2378
2379                 if (irsp->ulpBdeCount > 1) {
2380                         dmzbuf = lpfc_sli_get_buff(phba, pring,
2381                                         irsp->unsli3.sli3Words[3]);
2382                         lpfc_in_buf_free(phba, dmzbuf);
2383                 }
2384
2385                 if (irsp->ulpBdeCount > 2) {
2386                         dmzbuf = lpfc_sli_get_buff(phba, pring,
2387                                 irsp->unsli3.sli3Words[7]);
2388                         lpfc_in_buf_free(phba, dmzbuf);
2389                 }
2390
2391                 return 1;
2392         }
2393
2394         if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
2395                 if (irsp->ulpBdeCount != 0) {
2396                         saveq->context2 = lpfc_sli_get_buff(phba, pring,
2397                                                 irsp->un.ulpWord[3]);
2398                         if (!saveq->context2)
2399                                 lpfc_printf_log(phba,
2400                                         KERN_ERR,
2401                                         LOG_SLI,
2402                                         "0341 Ring %d Cannot find buffer for "
2403                                         "an unsolicited iocb. tag 0x%x\n",
2404                                         pring->ringno,
2405                                         irsp->un.ulpWord[3]);
2406                 }
2407                 if (irsp->ulpBdeCount == 2) {
2408                         saveq->context3 = lpfc_sli_get_buff(phba, pring,
2409                                                 irsp->unsli3.sli3Words[7]);
2410                         if (!saveq->context3)
2411                                 lpfc_printf_log(phba,
2412                                         KERN_ERR,
2413                                         LOG_SLI,
2414                                         "0342 Ring %d Cannot find buffer for an"
2415                                         " unsolicited iocb. tag 0x%x\n",
2416                                         pring->ringno,
2417                                         irsp->unsli3.sli3Words[7]);
2418                 }
2419                 list_for_each_entry(iocbq, &saveq->list, list) {
2420                         irsp = &(iocbq->iocb);
2421                         if (irsp->ulpBdeCount != 0) {
2422                                 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
2423                                                         irsp->un.ulpWord[3]);
2424                                 if (!iocbq->context2)
2425                                         lpfc_printf_log(phba,
2426                                                 KERN_ERR,
2427                                                 LOG_SLI,
2428                                                 "0343 Ring %d Cannot find "
2429                                                 "buffer for an unsolicited iocb"
2430                                                 ". tag 0x%x\n", pring->ringno,
2431                                                 irsp->un.ulpWord[3]);
2432                         }
2433                         if (irsp->ulpBdeCount == 2) {
2434                                 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
2435                                                 irsp->unsli3.sli3Words[7]);
2436                                 if (!iocbq->context3)
2437                                         lpfc_printf_log(phba,
2438                                                 KERN_ERR,
2439                                                 LOG_SLI,
2440                                                 "0344 Ring %d Cannot find "
2441                                                 "buffer for an unsolicited "
2442                                                 "iocb. tag 0x%x\n",
2443                                                 pring->ringno,
2444                                                 irsp->unsli3.sli3Words[7]);
2445                         }
2446                 }
2447         }
2448         if (irsp->ulpBdeCount != 0 &&
2449             (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
2450              irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
2451                 int found = 0;
2452
2453                 /* search continue save q for same XRI */
2454                 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
2455                         if (iocbq->iocb.ulpContext == saveq->iocb.ulpContext) {
2456                                 list_add_tail(&saveq->list, &iocbq->list);
2457                                 found = 1;
2458                                 break;
2459                         }
2460                 }
2461                 if (!found)
2462                         list_add_tail(&saveq->clist,
2463                                       &pring->iocb_continue_saveq);
2464                 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
2465                         list_del_init(&iocbq->clist);
2466                         saveq = iocbq;
2467                         irsp = &(saveq->iocb);
2468                 } else
2469                         return 0;
2470         }
2471         if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
2472             (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
2473             (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
2474                 Rctl = FC_RCTL_ELS_REQ;
2475                 Type = FC_TYPE_ELS;
2476         } else {
2477                 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
2478                 Rctl = w5p->hcsw.Rctl;
2479                 Type = w5p->hcsw.Type;
2480
2481                 /* Firmware Workaround */
2482                 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
2483                         (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
2484                          irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
2485                         Rctl = FC_RCTL_ELS_REQ;
2486                         Type = FC_TYPE_ELS;
2487                         w5p->hcsw.Rctl = Rctl;
2488                         w5p->hcsw.Type = Type;
2489                 }
2490         }
2491
2492         if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
2493                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
2494                                 "0313 Ring %d handler: unexpected Rctl x%x "
2495                                 "Type x%x received\n",
2496                                 pring->ringno, Rctl, Type);
2497
2498         return 1;
2499 }
2500
2501 /**
2502  * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
2503  * @phba: Pointer to HBA context object.
2504  * @pring: Pointer to driver SLI ring object.
2505  * @prspiocb: Pointer to response iocb object.
2506  *
2507  * This function looks up the iocb_lookup table to get the command iocb
2508  * corresponding to the given response iocb using the iotag of the
2509  * response iocb. This function is called with the hbalock held.
2510  * This function returns the command iocb object if it finds the command
2511  * iocb else returns NULL.
2512  **/
2513 static struct lpfc_iocbq *
2514 lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
2515                       struct lpfc_sli_ring *pring,
2516                       struct lpfc_iocbq *prspiocb)
2517 {
2518         struct lpfc_iocbq *cmd_iocb = NULL;
2519         uint16_t iotag;
2520
2521         iotag = prspiocb->iocb.ulpIoTag;
2522
2523         if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2524                 cmd_iocb = phba->sli.iocbq_lookup[iotag];
2525                 list_del_init(&cmd_iocb->list);
2526                 if (cmd_iocb->iocb_flag & LPFC_IO_ON_Q) {
2527                         pring->txcmplq_cnt--;
2528                         cmd_iocb->iocb_flag &= ~LPFC_IO_ON_Q;
2529                 }
2530                 return cmd_iocb;
2531         }
2532
2533         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2534                         "0317 iotag x%x is out off "
2535                         "range: max iotag x%x wd0 x%x\n",
2536                         iotag, phba->sli.last_iotag,
2537                         *(((uint32_t *) &prspiocb->iocb) + 7));
2538         return NULL;
2539 }
2540
2541 /**
2542  * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
2543  * @phba: Pointer to HBA context object.
2544  * @pring: Pointer to driver SLI ring object.
2545  * @iotag: IOCB tag.
2546  *
2547  * This function looks up the iocb_lookup table to get the command iocb
2548  * corresponding to the given iotag. This function is called with the
2549  * hbalock held.
2550  * This function returns the command iocb object if it finds the command
2551  * iocb else returns NULL.
2552  **/
2553 static struct lpfc_iocbq *
2554 lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
2555                              struct lpfc_sli_ring *pring, uint16_t iotag)
2556 {
2557         struct lpfc_iocbq *cmd_iocb;
2558
2559         if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2560                 cmd_iocb = phba->sli.iocbq_lookup[iotag];
2561                 list_del_init(&cmd_iocb->list);
2562                 if (cmd_iocb->iocb_flag & LPFC_IO_ON_Q) {
2563                         cmd_iocb->iocb_flag &= ~LPFC_IO_ON_Q;
2564                         pring->txcmplq_cnt--;
2565                 }
2566                 return cmd_iocb;
2567         }
2568
2569         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2570                         "0372 iotag x%x is out off range: max iotag (x%x)\n",
2571                         iotag, phba->sli.last_iotag);
2572         return NULL;
2573 }
2574
2575 /**
2576  * lpfc_sli_process_sol_iocb - process solicited iocb completion
2577  * @phba: Pointer to HBA context object.
2578  * @pring: Pointer to driver SLI ring object.
2579  * @saveq: Pointer to the response iocb to be processed.
2580  *
2581  * This function is called by the ring event handler for non-fcp
2582  * rings when there is a new response iocb in the response ring.
2583  * The caller is not required to hold any locks. This function
2584  * gets the command iocb associated with the response iocb and
2585  * calls the completion handler for the command iocb. If there
2586  * is no completion handler, the function will free the resources
2587  * associated with command iocb. If the response iocb is for
2588  * an already aborted command iocb, the status of the completion
2589  * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
2590  * This function always returns 1.
2591  **/
2592 static int
2593 lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2594                           struct lpfc_iocbq *saveq)
2595 {
2596         struct lpfc_iocbq *cmdiocbp;
2597         int rc = 1;
2598         unsigned long iflag;
2599
2600         /* Based on the iotag field, get the cmd IOCB from the txcmplq */
2601         spin_lock_irqsave(&phba->hbalock, iflag);
2602         cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
2603         spin_unlock_irqrestore(&phba->hbalock, iflag);
2604
2605         if (cmdiocbp) {
2606                 if (cmdiocbp->iocb_cmpl) {
2607                         /*
2608                          * If an ELS command failed send an event to mgmt
2609                          * application.
2610                          */
2611                         if (saveq->iocb.ulpStatus &&
2612                              (pring->ringno == LPFC_ELS_RING) &&
2613                              (cmdiocbp->iocb.ulpCommand ==
2614                                 CMD_ELS_REQUEST64_CR))
2615                                 lpfc_send_els_failure_event(phba,
2616                                         cmdiocbp, saveq);
2617
2618                         /*
2619                          * Post all ELS completions to the worker thread.
2620                          * All other are passed to the completion callback.
2621                          */
2622                         if (pring->ringno == LPFC_ELS_RING) {
2623                                 if ((phba->sli_rev < LPFC_SLI_REV4) &&
2624                                     (cmdiocbp->iocb_flag &
2625                                                         LPFC_DRIVER_ABORTED)) {
2626                                         spin_lock_irqsave(&phba->hbalock,
2627                                                           iflag);
2628                                         cmdiocbp->iocb_flag &=
2629                                                 ~LPFC_DRIVER_ABORTED;
2630                                         spin_unlock_irqrestore(&phba->hbalock,
2631                                                                iflag);
2632                                         saveq->iocb.ulpStatus =
2633                                                 IOSTAT_LOCAL_REJECT;
2634                                         saveq->iocb.un.ulpWord[4] =
2635                                                 IOERR_SLI_ABORTED;
2636
2637                                         /* Firmware could still be in progress
2638                                          * of DMAing payload, so don't free data
2639                                          * buffer till after a hbeat.
2640                                          */
2641                                         spin_lock_irqsave(&phba->hbalock,
2642                                                           iflag);
2643                                         saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
2644                                         spin_unlock_irqrestore(&phba->hbalock,
2645                                                                iflag);
2646                                 }
2647                                 if (phba->sli_rev == LPFC_SLI_REV4) {
2648                                         if (saveq->iocb_flag &
2649                                             LPFC_EXCHANGE_BUSY) {
2650                                                 /* Set cmdiocb flag for the
2651                                                  * exchange busy so sgl (xri)
2652                                                  * will not be released until
2653                                                  * the abort xri is received
2654                                                  * from hba.
2655                                                  */
2656                                                 spin_lock_irqsave(
2657                                                         &phba->hbalock, iflag);
2658                                                 cmdiocbp->iocb_flag |=
2659                                                         LPFC_EXCHANGE_BUSY;
2660                                                 spin_unlock_irqrestore(
2661                                                         &phba->hbalock, iflag);
2662                                         }
2663                                         if (cmdiocbp->iocb_flag &
2664                                             LPFC_DRIVER_ABORTED) {
2665                                                 /*
2666                                                  * Clear LPFC_DRIVER_ABORTED
2667                                                  * bit in case it was driver
2668                                                  * initiated abort.
2669                                                  */
2670                                                 spin_lock_irqsave(
2671                                                         &phba->hbalock, iflag);
2672                                                 cmdiocbp->iocb_flag &=
2673                                                         ~LPFC_DRIVER_ABORTED;
2674                                                 spin_unlock_irqrestore(
2675                                                         &phba->hbalock, iflag);
2676                                                 cmdiocbp->iocb.ulpStatus =
2677                                                         IOSTAT_LOCAL_REJECT;
2678                                                 cmdiocbp->iocb.un.ulpWord[4] =
2679                                                         IOERR_ABORT_REQUESTED;
2680                                                 /*
2681                                                  * For SLI4, irsiocb contains
2682                                                  * NO_XRI in sli_xritag, it
2683                                                  * shall not affect releasing
2684                                                  * sgl (xri) process.
2685                                                  */
2686                                                 saveq->iocb.ulpStatus =
2687                                                         IOSTAT_LOCAL_REJECT;
2688                                                 saveq->iocb.un.ulpWord[4] =
2689                                                         IOERR_SLI_ABORTED;
2690                                                 spin_lock_irqsave(
2691                                                         &phba->hbalock, iflag);
2692                                                 saveq->iocb_flag |=
2693                                                         LPFC_DELAY_MEM_FREE;
2694                                                 spin_unlock_irqrestore(
2695                                                         &phba->hbalock, iflag);
2696                                         }
2697                                 }
2698                         }
2699                         (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
2700                 } else
2701                         lpfc_sli_release_iocbq(phba, cmdiocbp);
2702         } else {
2703                 /*
2704                  * Unknown initiating command based on the response iotag.
2705                  * This could be the case on the ELS ring because of
2706                  * lpfc_els_abort().
2707                  */
2708                 if (pring->ringno != LPFC_ELS_RING) {
2709                         /*
2710                          * Ring <ringno> handler: unexpected completion IoTag
2711                          * <IoTag>
2712                          */
2713                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
2714                                          "0322 Ring %d handler: "
2715                                          "unexpected completion IoTag x%x "
2716                                          "Data: x%x x%x x%x x%x\n",
2717                                          pring->ringno,
2718                                          saveq->iocb.ulpIoTag,
2719                                          saveq->iocb.ulpStatus,
2720                                          saveq->iocb.un.ulpWord[4],
2721                                          saveq->iocb.ulpCommand,
2722                                          saveq->iocb.ulpContext);
2723                 }
2724         }
2725
2726         return rc;
2727 }
2728
2729 /**
2730  * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
2731  * @phba: Pointer to HBA context object.
2732  * @pring: Pointer to driver SLI ring object.
2733  *
2734  * This function is called from the iocb ring event handlers when
2735  * put pointer is ahead of the get pointer for a ring. This function signal
2736  * an error attention condition to the worker thread and the worker
2737  * thread will transition the HBA to offline state.
2738  **/
2739 static void
2740 lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
2741 {
2742         struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
2743         /*
2744          * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
2745          * rsp ring <portRspMax>
2746          */
2747         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2748                         "0312 Ring %d handler: portRspPut %d "
2749                         "is bigger than rsp ring %d\n",
2750                         pring->ringno, le32_to_cpu(pgp->rspPutInx),
2751                         pring->numRiocb);
2752
2753         phba->link_state = LPFC_HBA_ERROR;
2754
2755         /*
2756          * All error attention handlers are posted to
2757          * worker thread
2758          */
2759         phba->work_ha |= HA_ERATT;
2760         phba->work_hs = HS_FFER3;
2761
2762         lpfc_worker_wake_up(phba);
2763
2764         return;
2765 }
2766
2767 /**
2768  * lpfc_poll_eratt - Error attention polling timer timeout handler
2769  * @ptr: Pointer to address of HBA context object.
2770  *
2771  * This function is invoked by the Error Attention polling timer when the
2772  * timer times out. It will check the SLI Error Attention register for
2773  * possible attention events. If so, it will post an Error Attention event
2774  * and wake up worker thread to process it. Otherwise, it will set up the
2775  * Error Attention polling timer for the next poll.
2776  **/
2777 void lpfc_poll_eratt(unsigned long ptr)
2778 {
2779         struct lpfc_hba *phba;
2780         uint32_t eratt = 0;
2781
2782         phba = (struct lpfc_hba *)ptr;
2783
2784         /* Check chip HA register for error event */
2785         eratt = lpfc_sli_check_eratt(phba);
2786
2787         if (eratt)
2788                 /* Tell the worker thread there is work to do */
2789                 lpfc_worker_wake_up(phba);
2790         else
2791                 /* Restart the timer for next eratt poll */
2792                 mod_timer(&phba->eratt_poll, jiffies +
2793                                         HZ * LPFC_ERATT_POLL_INTERVAL);
2794         return;
2795 }
2796
2797
2798 /**
2799  * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
2800  * @phba: Pointer to HBA context object.
2801  * @pring: Pointer to driver SLI ring object.
2802  * @mask: Host attention register mask for this ring.
2803  *
2804  * This function is called from the interrupt context when there is a ring
2805  * event for the fcp ring. The caller does not hold any lock.
2806  * The function processes each response iocb in the response ring until it
2807  * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
2808  * LE bit set. The function will call the completion handler of the command iocb
2809  * if the response iocb indicates a completion for a command iocb or it is
2810  * an abort completion. The function will call lpfc_sli_process_unsol_iocb
2811  * function if this is an unsolicited iocb.
2812  * This routine presumes LPFC_FCP_RING handling and doesn't bother
2813  * to check it explicitly.
2814  */
2815 int
2816 lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
2817                                 struct lpfc_sli_ring *pring, uint32_t mask)
2818 {
2819         struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
2820         IOCB_t *irsp = NULL;
2821         IOCB_t *entry = NULL;
2822         struct lpfc_iocbq *cmdiocbq = NULL;
2823         struct lpfc_iocbq rspiocbq;
2824         uint32_t status;
2825         uint32_t portRspPut, portRspMax;
2826         int rc = 1;
2827         lpfc_iocb_type type;
2828         unsigned long iflag;
2829         uint32_t rsp_cmpl = 0;
2830
2831         spin_lock_irqsave(&phba->hbalock, iflag);
2832         pring->stats.iocb_event++;
2833
2834         /*
2835          * The next available response entry should never exceed the maximum
2836          * entries.  If it does, treat it as an adapter hardware error.
2837          */
2838         portRspMax = pring->numRiocb;
2839         portRspPut = le32_to_cpu(pgp->rspPutInx);
2840         if (unlikely(portRspPut >= portRspMax)) {
2841                 lpfc_sli_rsp_pointers_error(phba, pring);
2842                 spin_unlock_irqrestore(&phba->hbalock, iflag);
2843                 return 1;
2844         }
2845         if (phba->fcp_ring_in_use) {
2846                 spin_unlock_irqrestore(&phba->hbalock, iflag);
2847                 return 1;
2848         } else
2849                 phba->fcp_ring_in_use = 1;
2850
2851         rmb();
2852         while (pring->rspidx != portRspPut) {
2853                 /*
2854                  * Fetch an entry off the ring and copy it into a local data
2855                  * structure.  The copy involves a byte-swap since the
2856                  * network byte order and pci byte orders are different.
2857                  */
2858                 entry = lpfc_resp_iocb(phba, pring);
2859                 phba->last_completion_time = jiffies;
2860
2861                 if (++pring->rspidx >= portRspMax)
2862                         pring->rspidx = 0;
2863
2864                 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
2865                                       (uint32_t *) &rspiocbq.iocb,
2866                                       phba->iocb_rsp_size);
2867                 INIT_LIST_HEAD(&(rspiocbq.list));
2868                 irsp = &rspiocbq.iocb;
2869
2870                 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
2871                 pring->stats.iocb_rsp++;
2872                 rsp_cmpl++;
2873
2874                 if (unlikely(irsp->ulpStatus)) {
2875                         /*
2876                          * If resource errors reported from HBA, reduce
2877                          * queuedepths of the SCSI device.
2878                          */
2879                         if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
2880                                 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
2881                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
2882                                 phba->lpfc_rampdown_queue_depth(phba);
2883                                 spin_lock_irqsave(&phba->hbalock, iflag);
2884                         }
2885
2886                         /* Rsp ring <ringno> error: IOCB */
2887                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
2888                                         "0336 Rsp Ring %d error: IOCB Data: "
2889                                         "x%x x%x x%x x%x x%x x%x x%x x%x\n",
2890                                         pring->ringno,
2891                                         irsp->un.ulpWord[0],
2892                                         irsp->un.ulpWord[1],
2893                                         irsp->un.ulpWord[2],
2894                                         irsp->un.ulpWord[3],
2895                                         irsp->un.ulpWord[4],
2896                                         irsp->un.ulpWord[5],
2897                                         *(uint32_t *)&irsp->un1,
2898                                         *((uint32_t *)&irsp->un1 + 1));
2899                 }
2900
2901                 switch (type) {
2902                 case LPFC_ABORT_IOCB:
2903                 case LPFC_SOL_IOCB:
2904                         /*
2905                          * Idle exchange closed via ABTS from port.  No iocb
2906                          * resources need to be recovered.
2907                          */
2908                         if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
2909                                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
2910                                                 "0333 IOCB cmd 0x%x"
2911                                                 " processed. Skipping"
2912                                                 " completion\n",
2913                                                 irsp->ulpCommand);
2914                                 break;
2915                         }
2916
2917                         cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
2918                                                          &rspiocbq);
2919                         if (unlikely(!cmdiocbq))
2920                                 break;
2921                         if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
2922                                 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
2923                         if (cmdiocbq->iocb_cmpl) {
2924                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
2925                                 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
2926                                                       &rspiocbq);
2927                                 spin_lock_irqsave(&phba->hbalock, iflag);
2928                         }
2929                         break;
2930                 case LPFC_UNSOL_IOCB:
2931                         spin_unlock_irqrestore(&phba->hbalock, iflag);
2932                         lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
2933                         spin_lock_irqsave(&phba->hbalock, iflag);
2934                         break;
2935                 default:
2936                         if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
2937                                 char adaptermsg[LPFC_MAX_ADPTMSG];
2938                                 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
2939                                 memcpy(&adaptermsg[0], (uint8_t *) irsp,
2940                                        MAX_MSG_DATA);
2941                                 dev_warn(&((phba->pcidev)->dev),
2942                                          "lpfc%d: %s\n",
2943                                          phba->brd_no, adaptermsg);
2944                         } else {
2945                                 /* Unknown IOCB command */
2946                                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2947                                                 "0334 Unknown IOCB command "
2948                                                 "Data: x%x, x%x x%x x%x x%x\n",
2949                                                 type, irsp->ulpCommand,
2950                                                 irsp->ulpStatus,
2951                                                 irsp->ulpIoTag,
2952                                                 irsp->ulpContext);
2953                         }
2954                         break;
2955                 }
2956
2957                 /*
2958                  * The response IOCB has been processed.  Update the ring
2959                  * pointer in SLIM.  If the port response put pointer has not
2960                  * been updated, sync the pgp->rspPutInx and fetch the new port
2961                  * response put pointer.
2962                  */
2963                 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
2964
2965                 if (pring->rspidx == portRspPut)
2966                         portRspPut = le32_to_cpu(pgp->rspPutInx);
2967         }
2968
2969         if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
2970                 pring->stats.iocb_rsp_full++;
2971                 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
2972                 writel(status, phba->CAregaddr);
2973                 readl(phba->CAregaddr);
2974         }
2975         if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
2976                 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
2977                 pring->stats.iocb_cmd_empty++;
2978
2979                 /* Force update of the local copy of cmdGetInx */
2980                 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
2981                 lpfc_sli_resume_iocb(phba, pring);
2982
2983                 if ((pring->lpfc_sli_cmd_available))
2984                         (pring->lpfc_sli_cmd_available) (phba, pring);
2985
2986         }
2987
2988         phba->fcp_ring_in_use = 0;
2989         spin_unlock_irqrestore(&phba->hbalock, iflag);
2990         return rc;
2991 }
2992
2993 /**
2994  * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
2995  * @phba: Pointer to HBA context object.
2996  * @pring: Pointer to driver SLI ring object.
2997  * @rspiocbp: Pointer to driver response IOCB object.
2998  *
2999  * This function is called from the worker thread when there is a slow-path
3000  * response IOCB to process. This function chains all the response iocbs until
3001  * seeing the iocb with the LE bit set. The function will call
3002  * lpfc_sli_process_sol_iocb function if the response iocb indicates a
3003  * completion of a command iocb. The function will call the
3004  * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
3005  * The function frees the resources or calls the completion handler if this
3006  * iocb is an abort completion. The function returns NULL when the response
3007  * iocb has the LE bit set and all the chained iocbs are processed, otherwise
3008  * this function shall chain the iocb on to the iocb_continueq and return the
3009  * response iocb passed in.
3010  **/
3011 static struct lpfc_iocbq *
3012 lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3013                         struct lpfc_iocbq *rspiocbp)
3014 {
3015         struct lpfc_iocbq *saveq;
3016         struct lpfc_iocbq *cmdiocbp;
3017         struct lpfc_iocbq *next_iocb;
3018         IOCB_t *irsp = NULL;
3019         uint32_t free_saveq;
3020         uint8_t iocb_cmd_type;
3021         lpfc_iocb_type type;
3022         unsigned long iflag;
3023         int rc;
3024
3025         spin_lock_irqsave(&phba->hbalock, iflag);
3026         /* First add the response iocb to the countinueq list */
3027         list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
3028         pring->iocb_continueq_cnt++;
3029
3030         /* Now, determine whether the list is completed for processing */
3031         irsp = &rspiocbp->iocb;
3032         if (irsp->ulpLe) {
3033                 /*
3034                  * By default, the driver expects to free all resources
3035                  * associated with this iocb completion.
3036                  */
3037                 free_saveq = 1;
3038                 saveq = list_get_first(&pring->iocb_continueq,
3039                                        struct lpfc_iocbq, list);
3040                 irsp = &(saveq->iocb);
3041                 list_del_init(&pring->iocb_continueq);
3042                 pring->iocb_continueq_cnt = 0;
3043
3044                 pring->stats.iocb_rsp++;
3045
3046                 /*
3047                  * If resource errors reported from HBA, reduce
3048                  * queuedepths of the SCSI device.
3049                  */
3050                 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
3051                     (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
3052                         spin_unlock_irqrestore(&phba->hbalock, iflag);
3053                         phba->lpfc_rampdown_queue_depth(phba);
3054                         spin_lock_irqsave(&phba->hbalock, iflag);
3055                 }
3056
3057                 if (irsp->ulpStatus) {
3058                         /* Rsp ring <ringno> error: IOCB */
3059                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3060                                         "0328 Rsp Ring %d error: "
3061                                         "IOCB Data: "
3062                                         "x%x x%x x%x x%x "
3063                                         "x%x x%x x%x x%x "
3064                                         "x%x x%x x%x x%x "
3065                                         "x%x x%x x%x x%x\n",
3066                                         pring->ringno,
3067                                         irsp->un.ulpWord[0],
3068                                         irsp->un.ulpWord[1],
3069                                         irsp->un.ulpWord[2],
3070                                         irsp->un.ulpWord[3],
3071                                         irsp->un.ulpWord[4],
3072                                         irsp->un.ulpWord[5],
3073                                         *(((uint32_t *) irsp) + 6),
3074                                         *(((uint32_t *) irsp) + 7),
3075                                         *(((uint32_t *) irsp) + 8),
3076                                         *(((uint32_t *) irsp) + 9),
3077                                         *(((uint32_t *) irsp) + 10),
3078                                         *(((uint32_t *) irsp) + 11),
3079                                         *(((uint32_t *) irsp) + 12),
3080                                         *(((uint32_t *) irsp) + 13),
3081                                         *(((uint32_t *) irsp) + 14),
3082                                         *(((uint32_t *) irsp) + 15));
3083                 }
3084
3085                 /*
3086                  * Fetch the IOCB command type and call the correct completion
3087                  * routine. Solicited and Unsolicited IOCBs on the ELS ring
3088                  * get freed back to the lpfc_iocb_list by the discovery
3089                  * kernel thread.
3090                  */
3091                 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
3092                 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
3093                 switch (type) {
3094                 case LPFC_SOL_IOCB:
3095                         spin_unlock_irqrestore(&phba->hbalock, iflag);
3096                         rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
3097                         spin_lock_irqsave(&phba->hbalock, iflag);
3098                         break;
3099
3100                 case LPFC_UNSOL_IOCB:
3101                         spin_unlock_irqrestore(&phba->hbalock, iflag);
3102                         rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
3103                         spin_lock_irqsave(&phba->hbalock, iflag);
3104                         if (!rc)
3105                                 free_saveq = 0;
3106                         break;
3107
3108                 case LPFC_ABORT_IOCB:
3109                         cmdiocbp = NULL;
3110                         if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
3111                                 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
3112                                                                  saveq);
3113                         if (cmdiocbp) {
3114                                 /* Call the specified completion routine */
3115                                 if (cmdiocbp->iocb_cmpl) {
3116                                         spin_unlock_irqrestore(&phba->hbalock,
3117                                                                iflag);
3118                                         (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
3119                                                               saveq);
3120                                         spin_lock_irqsave(&phba->hbalock,
3121                                                           iflag);
3122                                 } else
3123                                         __lpfc_sli_release_iocbq(phba,
3124                                                                  cmdiocbp);
3125                         }
3126                         break;
3127
3128                 case LPFC_UNKNOWN_IOCB:
3129                         if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3130                                 char adaptermsg[LPFC_MAX_ADPTMSG];
3131                                 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3132                                 memcpy(&adaptermsg[0], (uint8_t *)irsp,
3133                                        MAX_MSG_DATA);
3134                                 dev_warn(&((phba->pcidev)->dev),
3135                                          "lpfc%d: %s\n",
3136                                          phba->brd_no, adaptermsg);
3137                         } else {
3138                                 /* Unknown IOCB command */
3139                                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3140                                                 "0335 Unknown IOCB "
3141                                                 "command Data: x%x "
3142                                                 "x%x x%x x%x\n",
3143                                                 irsp->ulpCommand,
3144                                                 irsp->ulpStatus,
3145                                                 irsp->ulpIoTag,
3146                                                 irsp->ulpContext);
3147                         }
3148                         break;
3149                 }
3150
3151                 if (free_saveq) {
3152                         list_for_each_entry_safe(rspiocbp, next_iocb,
3153                                                  &saveq->list, list) {
3154                                 list_del(&rspiocbp->list);
3155                                 __lpfc_sli_release_iocbq(phba, rspiocbp);
3156                         }
3157                         __lpfc_sli_release_iocbq(phba, saveq);
3158                 }
3159                 rspiocbp = NULL;
3160         }
3161         spin_unlock_irqrestore(&phba->hbalock, iflag);
3162         return rspiocbp;
3163 }
3164
3165 /**
3166  * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
3167  * @phba: Pointer to HBA context object.
3168  * @pring: Pointer to driver SLI ring object.
3169  * @mask: Host attention register mask for this ring.
3170  *
3171  * This routine wraps the actual slow_ring event process routine from the
3172  * API jump table function pointer from the lpfc_hba struct.
3173  **/
3174 void
3175 lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
3176                                 struct lpfc_sli_ring *pring, uint32_t mask)
3177 {
3178         phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
3179 }
3180
3181 /**
3182  * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
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 function is called from the worker thread when there is a ring event
3188  * for non-fcp rings. The caller does not hold any lock. The function will
3189  * remove each response iocb in the response ring and calls the handle
3190  * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3191  **/
3192 static void
3193 lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
3194                                    struct lpfc_sli_ring *pring, uint32_t mask)
3195 {
3196         struct lpfc_pgp *pgp;
3197         IOCB_t *entry;
3198         IOCB_t *irsp = NULL;
3199         struct lpfc_iocbq *rspiocbp = NULL;
3200         uint32_t portRspPut, portRspMax;
3201         unsigned long iflag;
3202         uint32_t status;
3203
3204         pgp = &phba->port_gp[pring->ringno];
3205         spin_lock_irqsave(&phba->hbalock, iflag);
3206         pring->stats.iocb_event++;
3207
3208         /*
3209          * The next available response entry should never exceed the maximum
3210          * entries.  If it does, treat it as an adapter hardware error.
3211          */
3212         portRspMax = pring->numRiocb;
3213         portRspPut = le32_to_cpu(pgp->rspPutInx);
3214         if (portRspPut >= portRspMax) {
3215                 /*
3216                  * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
3217                  * rsp ring <portRspMax>
3218                  */
3219                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3220                                 "0303 Ring %d handler: portRspPut %d "
3221                                 "is bigger than rsp ring %d\n",
3222                                 pring->ringno, portRspPut, portRspMax);
3223
3224                 phba->link_state = LPFC_HBA_ERROR;
3225                 spin_unlock_irqrestore(&phba->hbalock, iflag);
3226
3227                 phba->work_hs = HS_FFER3;
3228                 lpfc_handle_eratt(phba);
3229
3230                 return;
3231         }
3232
3233         rmb();
3234         while (pring->rspidx != portRspPut) {
3235                 /*
3236                  * Build a completion list and call the appropriate handler.
3237                  * The process is to get the next available response iocb, get
3238                  * a free iocb from the list, copy the response data into the
3239                  * free iocb, insert to the continuation list, and update the
3240                  * next response index to slim.  This process makes response
3241                  * iocb's in the ring available to DMA as fast as possible but
3242                  * pays a penalty for a copy operation.  Since the iocb is
3243                  * only 32 bytes, this penalty is considered small relative to
3244                  * the PCI reads for register values and a slim write.  When
3245                  * the ulpLe field is set, the entire Command has been
3246                  * received.
3247                  */
3248                 entry = lpfc_resp_iocb(phba, pring);
3249
3250                 phba->last_completion_time = jiffies;
3251                 rspiocbp = __lpfc_sli_get_iocbq(phba);
3252                 if (rspiocbp == NULL) {
3253                         printk(KERN_ERR "%s: out of buffers! Failing "
3254                                "completion.\n", __func__);
3255                         break;
3256                 }
3257
3258                 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
3259                                       phba->iocb_rsp_size);
3260                 irsp = &rspiocbp->iocb;
3261
3262                 if (++pring->rspidx >= portRspMax)
3263                         pring->rspidx = 0;
3264
3265                 if (pring->ringno == LPFC_ELS_RING) {
3266                         lpfc_debugfs_slow_ring_trc(phba,
3267                         "IOCB rsp ring:   wd4:x%08x wd6:x%08x wd7:x%08x",
3268                                 *(((uint32_t *) irsp) + 4),
3269                                 *(((uint32_t *) irsp) + 6),
3270                                 *(((uint32_t *) irsp) + 7));
3271                 }
3272
3273                 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
3274
3275                 spin_unlock_irqrestore(&phba->hbalock, iflag);
3276                 /* Handle the response IOCB */
3277                 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
3278                 spin_lock_irqsave(&phba->hbalock, iflag);
3279
3280                 /*
3281                  * If the port response put pointer has not been updated, sync
3282                  * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
3283                  * response put pointer.
3284                  */
3285                 if (pring->rspidx == portRspPut) {
3286                         portRspPut = le32_to_cpu(pgp->rspPutInx);
3287                 }
3288         } /* while (pring->rspidx != portRspPut) */
3289
3290         if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
3291                 /* At least one response entry has been freed */
3292                 pring->stats.iocb_rsp_full++;
3293                 /* SET RxRE_RSP in Chip Att register */
3294                 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3295                 writel(status, phba->CAregaddr);
3296                 readl(phba->CAregaddr); /* flush */
3297         }
3298         if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3299                 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3300                 pring->stats.iocb_cmd_empty++;
3301
3302                 /* Force update of the local copy of cmdGetInx */
3303                 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
3304                 lpfc_sli_resume_iocb(phba, pring);
3305
3306                 if ((pring->lpfc_sli_cmd_available))
3307                         (pring->lpfc_sli_cmd_available) (phba, pring);
3308
3309         }
3310
3311         spin_unlock_irqrestore(&phba->hbalock, iflag);
3312         return;
3313 }
3314
3315 /**
3316  * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
3317  * @phba: Pointer to HBA context object.
3318  * @pring: Pointer to driver SLI ring object.
3319  * @mask: Host attention register mask for this ring.
3320  *
3321  * This function is called from the worker thread when there is a pending
3322  * ELS response iocb on the driver internal slow-path response iocb worker
3323  * queue. The caller does not hold any lock. The function will remove each
3324  * response iocb from the response worker queue and calls the handle
3325  * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3326  **/
3327 static void
3328 lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
3329                                    struct lpfc_sli_ring *pring, uint32_t mask)
3330 {
3331         struct lpfc_iocbq *irspiocbq;
3332         struct hbq_dmabuf *dmabuf;
3333         struct lpfc_cq_event *cq_event;
3334         unsigned long iflag;
3335
3336         spin_lock_irqsave(&phba->hbalock, iflag);
3337         phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
3338         spin_unlock_irqrestore(&phba->hbalock, iflag);
3339         while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
3340                 /* Get the response iocb from the head of work queue */
3341                 spin_lock_irqsave(&phba->hbalock, iflag);
3342                 list_remove_head(&phba->sli4_hba.sp_queue_event,
3343                                  cq_event, struct lpfc_cq_event, list);
3344                 spin_unlock_irqrestore(&phba->hbalock, iflag);
3345
3346                 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
3347                 case CQE_CODE_COMPL_WQE:
3348                         irspiocbq = container_of(cq_event, struct lpfc_iocbq,
3349                                                  cq_event);
3350                         /* Translate ELS WCQE to response IOCBQ */
3351                         irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
3352                                                                    irspiocbq);
3353                         if (irspiocbq)
3354                                 lpfc_sli_sp_handle_rspiocb(phba, pring,
3355                                                            irspiocbq);
3356                         break;
3357                 case CQE_CODE_RECEIVE:
3358                         dmabuf = container_of(cq_event, struct hbq_dmabuf,
3359                                               cq_event);
3360                         lpfc_sli4_handle_received_buffer(phba, dmabuf);
3361                         break;
3362                 default:
3363                         break;
3364                 }
3365         }
3366 }
3367
3368 /**
3369  * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
3370  * @phba: Pointer to HBA context object.
3371  * @pring: Pointer to driver SLI ring object.
3372  *
3373  * This function aborts all iocbs in the given ring and frees all the iocb
3374  * objects in txq. This function issues an abort iocb for all the iocb commands
3375  * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3376  * the return of this function. The caller is not required to hold any locks.
3377  **/
3378 void
3379 lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3380 {
3381         LIST_HEAD(completions);
3382         struct lpfc_iocbq *iocb, *next_iocb;
3383
3384         if (pring->ringno == LPFC_ELS_RING) {
3385                 lpfc_fabric_abort_hba(phba);
3386         }
3387
3388         /* Error everything on txq and txcmplq
3389          * First do the txq.
3390          */
3391         spin_lock_irq(&phba->hbalock);
3392         list_splice_init(&pring->txq, &completions);
3393         pring->txq_cnt = 0;
3394
3395         /* Next issue ABTS for everything on the txcmplq */
3396         list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3397                 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3398
3399         spin_unlock_irq(&phba->hbalock);
3400
3401         /* Cancel all the IOCBs from the completions list */
3402         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3403                               IOERR_SLI_ABORTED);
3404 }
3405
3406 /**
3407  * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
3408  * @phba: Pointer to HBA context object.
3409  *
3410  * This function flushes all iocbs in the fcp ring and frees all the iocb
3411  * objects in txq and txcmplq. This function will not issue abort iocbs
3412  * for all the iocb commands in txcmplq, they will just be returned with
3413  * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3414  * slot has been permanently disabled.
3415  **/
3416 void
3417 lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
3418 {
3419         LIST_HEAD(txq);
3420         LIST_HEAD(txcmplq);
3421         struct lpfc_sli *psli = &phba->sli;
3422         struct lpfc_sli_ring  *pring;
3423
3424         /* Currently, only one fcp ring */
3425         pring = &psli->ring[psli->fcp_ring];
3426
3427         spin_lock_irq(&phba->hbalock);
3428         /* Retrieve everything on txq */
3429         list_splice_init(&pring->txq, &txq);
3430         pring->txq_cnt = 0;
3431
3432         /* Retrieve everything on the txcmplq */
3433         list_splice_init(&pring->txcmplq, &txcmplq);
3434         pring->txcmplq_cnt = 0;
3435         spin_unlock_irq(&phba->hbalock);
3436
3437         /* Flush the txq */
3438         lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
3439                               IOERR_SLI_DOWN);
3440
3441         /* Flush the txcmpq */
3442         lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
3443                               IOERR_SLI_DOWN);
3444 }
3445
3446 /**
3447  * lpfc_sli_brdready_s3 - Check for sli3 host ready status
3448  * @phba: Pointer to HBA context object.
3449  * @mask: Bit mask to be checked.
3450  *
3451  * This function reads the host status register and compares
3452  * with the provided bit mask to check if HBA completed
3453  * the restart. This function will wait in a loop for the
3454  * HBA to complete restart. If the HBA does not restart within
3455  * 15 iterations, the function will reset the HBA again. The
3456  * function returns 1 when HBA fail to restart otherwise returns
3457  * zero.
3458  **/
3459 static int
3460 lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
3461 {
3462         uint32_t status;
3463         int i = 0;
3464         int retval = 0;
3465
3466         /* Read the HBA Host Status Register */
3467         if (lpfc_readl(phba->HSregaddr, &status))
3468                 return 1;
3469
3470         /*
3471          * Check status register every 100ms for 5 retries, then every
3472          * 500ms for 5, then every 2.5 sec for 5, then reset board and
3473          * every 2.5 sec for 4.
3474          * Break our of the loop if errors occurred during init.
3475          */
3476         while (((status & mask) != mask) &&
3477                !(status & HS_FFERM) &&
3478                i++ < 20) {
3479
3480                 if (i <= 5)
3481                         msleep(10);
3482                 else if (i <= 10)
3483                         msleep(500);
3484                 else
3485                         msleep(2500);
3486
3487                 if (i == 15) {
3488                                 /* Do post */
3489                         phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3490                         lpfc_sli_brdrestart(phba);
3491                 }
3492                 /* Read the HBA Host Status Register */
3493                 if (lpfc_readl(phba->HSregaddr, &status)) {
3494                         retval = 1;
3495                         break;
3496                 }
3497         }
3498
3499         /* Check to see if any errors occurred during init */
3500         if ((status & HS_FFERM) || (i >= 20)) {
3501                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3502                                 "2751 Adapter failed to restart, "
3503                                 "status reg x%x, FW Data: A8 x%x AC x%x\n",
3504                                 status,
3505                                 readl(phba->MBslimaddr + 0xa8),
3506                                 readl(phba->MBslimaddr + 0xac));
3507                 phba->link_state = LPFC_HBA_ERROR;
3508                 retval = 1;
3509         }
3510
3511         return retval;
3512 }
3513
3514 /**
3515  * lpfc_sli_brdready_s4 - Check for sli4 host ready status
3516  * @phba: Pointer to HBA context object.
3517  * @mask: Bit mask to be checked.
3518  *
3519  * This function checks the host status register to check if HBA is
3520  * ready. This function will wait in a loop for the HBA to be ready
3521  * If the HBA is not ready , the function will will reset the HBA PCI
3522  * function again. The function returns 1 when HBA fail to be ready
3523  * otherwise returns zero.
3524  **/
3525 static int
3526 lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
3527 {
3528         uint32_t status;
3529         int retval = 0;
3530
3531         /* Read the HBA Host Status Register */
3532         status = lpfc_sli4_post_status_check(phba);
3533
3534         if (status) {
3535                 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3536                 lpfc_sli_brdrestart(phba);
3537                 status = lpfc_sli4_post_status_check(phba);
3538         }
3539
3540         /* Check to see if any errors occurred during init */
3541         if (status) {
3542                 phba->link_state = LPFC_HBA_ERROR;
3543                 retval = 1;
3544         } else
3545                 phba->sli4_hba.intr_enable = 0;
3546
3547         return retval;
3548 }
3549
3550 /**
3551  * lpfc_sli_brdready - Wrapper func for checking the hba readyness
3552  * @phba: Pointer to HBA context object.
3553  * @mask: Bit mask to be checked.
3554  *
3555  * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
3556  * from the API jump table function pointer from the lpfc_hba struct.
3557  **/
3558 int
3559 lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
3560 {
3561         return phba->lpfc_sli_brdready(phba, mask);
3562 }
3563
3564 #define BARRIER_TEST_PATTERN (0xdeadbeef)
3565
3566 /**
3567  * lpfc_reset_barrier - Make HBA ready for HBA reset
3568  * @phba: Pointer to HBA context object.
3569  *
3570  * This function is called before resetting an HBA. This
3571  * function requests HBA to quiesce DMAs before a reset.
3572  **/
3573 void lpfc_reset_barrier(struct lpfc_hba *phba)
3574 {
3575         uint32_t __iomem *resp_buf;
3576         uint32_t __iomem *mbox_buf;
3577         volatile uint32_t mbox;
3578         uint32_t hc_copy, ha_copy, resp_data;
3579         int  i;
3580         uint8_t hdrtype;
3581
3582         pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
3583         if (hdrtype != 0x80 ||
3584             (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
3585              FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
3586                 return;
3587
3588         /*
3589          * Tell the other part of the chip to suspend temporarily all
3590          * its DMA activity.
3591          */
3592         resp_buf = phba->MBslimaddr;
3593
3594         /* Disable the error attention */
3595         if (lpfc_readl(phba->HCregaddr, &hc_copy))
3596                 return;
3597         writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
3598         readl(phba->HCregaddr); /* flush */
3599         phba->link_flag |= LS_IGNORE_ERATT;
3600
3601         if (lpfc_readl(phba->HAregaddr, &ha_copy))
3602                 return;
3603         if (ha_copy & HA_ERATT) {
3604                 /* Clear Chip error bit */
3605                 writel(HA_ERATT, phba->HAregaddr);
3606                 phba->pport->stopped = 1;
3607         }
3608
3609         mbox = 0;
3610         ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
3611         ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
3612
3613         writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
3614         mbox_buf = phba->MBslimaddr;
3615         writel(mbox, mbox_buf);
3616
3617         for (i = 0; i < 50; i++) {
3618                 if (lpfc_readl((resp_buf + 1), &resp_data))
3619                         return;
3620                 if (resp_data != ~(BARRIER_TEST_PATTERN))
3621                         mdelay(1);
3622                 else
3623                         break;
3624         }
3625         resp_data = 0;
3626         if (lpfc_readl((resp_buf + 1), &resp_data))
3627                 return;
3628         if (resp_data  != ~(BARRIER_TEST_PATTERN)) {
3629                 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
3630                     phba->pport->stopped)
3631                         goto restore_hc;
3632                 else
3633                         goto clear_errat;
3634         }
3635
3636         ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
3637         resp_data = 0;
3638         for (i = 0; i < 500; i++) {
3639                 if (lpfc_readl(resp_buf, &resp_data))
3640                         return;
3641                 if (resp_data != mbox)
3642                         mdelay(1);
3643                 else
3644                         break;
3645         }
3646
3647 clear_errat:
3648
3649         while (++i < 500) {
3650                 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3651                         return;
3652                 if (!(ha_copy & HA_ERATT))
3653                         mdelay(1);
3654                 else
3655                         break;
3656         }
3657
3658         if (readl(phba->HAregaddr) & HA_ERATT) {
3659                 writel(HA_ERATT, phba->HAregaddr);
3660                 phba->pport->stopped = 1;
3661         }
3662
3663 restore_hc:
3664         phba->link_flag &= ~LS_IGNORE_ERATT;
3665         writel(hc_copy, phba->HCregaddr);
3666         readl(phba->HCregaddr); /* flush */
3667 }
3668
3669 /**
3670  * lpfc_sli_brdkill - Issue a kill_board mailbox command
3671  * @phba: Pointer to HBA context object.
3672  *
3673  * This function issues a kill_board mailbox command and waits for
3674  * the error attention interrupt. This function is called for stopping
3675  * the firmware processing. The caller is not required to hold any
3676  * locks. This function calls lpfc_hba_down_post function to free
3677  * any pending commands after the kill. The function will return 1 when it
3678  * fails to kill the board else will return 0.
3679  **/
3680 int
3681 lpfc_sli_brdkill(struct lpfc_hba *phba)
3682 {
3683         struct lpfc_sli *psli;
3684         LPFC_MBOXQ_t *pmb;
3685         uint32_t status;
3686         uint32_t ha_copy;
3687         int retval;
3688         int i = 0;
3689
3690         psli = &phba->sli;
3691
3692         /* Kill HBA */
3693         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3694                         "0329 Kill HBA Data: x%x x%x\n",
3695                         phba->pport->port_state, psli->sli_flag);
3696
3697         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3698         if (!pmb)
3699                 return 1;
3700
3701         /* Disable the error attention */
3702         spin_lock_irq(&phba->hbalock);
3703         if (lpfc_readl(phba->HCregaddr, &status)) {
3704                 spin_unlock_irq(&phba->hbalock);
3705                 mempool_free(pmb, phba->mbox_mem_pool);
3706                 return 1;
3707         }
3708         status &= ~HC_ERINT_ENA;
3709         writel(status, phba->HCregaddr);
3710         readl(phba->HCregaddr); /* flush */
3711         phba->link_flag |= LS_IGNORE_ERATT;
3712         spin_unlock_irq(&phba->hbalock);
3713
3714         lpfc_kill_board(phba, pmb);
3715         pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3716         retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3717
3718         if (retval != MBX_SUCCESS) {
3719                 if (retval != MBX_BUSY)
3720                         mempool_free(pmb, phba->mbox_mem_pool);
3721                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3722                                 "2752 KILL_BOARD command failed retval %d\n",
3723                                 retval);
3724                 spin_lock_irq(&phba->hbalock);
3725                 phba->link_flag &= ~LS_IGNORE_ERATT;
3726                 spin_unlock_irq(&phba->hbalock);
3727                 return 1;
3728         }
3729
3730         spin_lock_irq(&phba->hbalock);
3731         psli->sli_flag &= ~LPFC_SLI_ACTIVE;
3732         spin_unlock_irq(&phba->hbalock);
3733
3734         mempool_free(pmb, phba->mbox_mem_pool);
3735
3736         /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
3737          * attention every 100ms for 3 seconds. If we don't get ERATT after
3738          * 3 seconds we still set HBA_ERROR state because the status of the
3739          * board is now undefined.
3740          */
3741         if (lpfc_readl(phba->HAregaddr, &ha_copy))
3742                 return 1;
3743         while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
3744                 mdelay(100);
3745                 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3746                         return 1;
3747         }
3748
3749         del_timer_sync(&psli->mbox_tmo);
3750         if (ha_copy & HA_ERATT) {
3751                 writel(HA_ERATT, phba->HAregaddr);
3752                 phba->pport->stopped = 1;
3753         }
3754         spin_lock_irq(&phba->hbalock);
3755         psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
3756         psli->mbox_active = NULL;
3757         phba->link_flag &= ~LS_IGNORE_ERATT;
3758         spin_unlock_irq(&phba->hbalock);
3759
3760         lpfc_hba_down_post(phba);
3761         phba->link_state = LPFC_HBA_ERROR;
3762
3763         return ha_copy & HA_ERATT ? 0 : 1;
3764 }
3765
3766 /**
3767  * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
3768  * @phba: Pointer to HBA context object.
3769  *
3770  * This function resets the HBA by writing HC_INITFF to the control
3771  * register. After the HBA resets, this function resets all the iocb ring
3772  * indices. This function disables PCI layer parity checking during
3773  * the reset.
3774  * This function returns 0 always.
3775  * The caller is not required to hold any locks.
3776  **/
3777 int
3778 lpfc_sli_brdreset(struct lpfc_hba *phba)
3779 {
3780         struct lpfc_sli *psli;
3781         struct lpfc_sli_ring *pring;
3782         uint16_t cfg_value;
3783         int i;
3784
3785         psli = &phba->sli;
3786
3787         /* Reset HBA */
3788         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3789                         "0325 Reset HBA Data: x%x x%x\n",
3790                         phba->pport->port_state, psli->sli_flag);
3791
3792         /* perform board reset */
3793         phba->fc_eventTag = 0;
3794         phba->link_events = 0;
3795         phba->pport->fc_myDID = 0;
3796         phba->pport->fc_prevDID = 0;
3797
3798         /* Turn off parity checking and serr during the physical reset */
3799         pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3800         pci_write_config_word(phba->pcidev, PCI_COMMAND,
3801                               (cfg_value &
3802                                ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3803
3804         psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
3805
3806         /* Now toggle INITFF bit in the Host Control Register */
3807         writel(HC_INITFF, phba->HCregaddr);
3808         mdelay(1);
3809         readl(phba->HCregaddr); /* flush */
3810         writel(0, phba->HCregaddr);
3811         readl(phba->HCregaddr); /* flush */
3812
3813         /* Restore PCI cmd register */
3814         pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
3815
3816         /* Initialize relevant SLI info */
3817         for (i = 0; i < psli->num_rings; i++) {
3818                 pring = &psli->ring[i];
3819                 pring->flag = 0;
3820                 pring->rspidx = 0;
3821                 pring->next_cmdidx  = 0;
3822                 pring->local_getidx = 0;
3823                 pring->cmdidx = 0;
3824                 pring->missbufcnt = 0;
3825         }
3826
3827         phba->link_state = LPFC_WARM_START;
3828         return 0;
3829 }
3830
3831 /**
3832  * lpfc_sli4_brdreset - Reset a sli-4 HBA
3833  * @phba: Pointer to HBA context object.
3834  *
3835  * This function resets a SLI4 HBA. This function disables PCI layer parity
3836  * checking during resets the device. The caller is not required to hold
3837  * any locks.
3838  *
3839  * This function returns 0 always.
3840  **/
3841 int
3842 lpfc_sli4_brdreset(struct lpfc_hba *phba)
3843 {
3844         struct lpfc_sli *psli = &phba->sli;
3845         uint16_t cfg_value;
3846         uint8_t qindx;
3847
3848         /* Reset HBA */
3849         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3850                         "0295 Reset HBA Data: x%x x%x\n",
3851                         phba->pport->port_state, psli->sli_flag);
3852
3853         /* perform board reset */
3854         phba->fc_eventTag = 0;
3855         phba->link_events = 0;
3856         phba->pport->fc_myDID = 0;
3857         phba->pport->fc_prevDID = 0;
3858
3859         spin_lock_irq(&phba->hbalock);
3860         psli->sli_flag &= ~(LPFC_PROCESS_LA);
3861         phba->fcf.fcf_flag = 0;
3862         /* Clean up the child queue list for the CQs */
3863         list_del_init(&phba->sli4_hba.mbx_wq->list);
3864         list_del_init(&phba->sli4_hba.els_wq->list);
3865         list_del_init(&phba->sli4_hba.hdr_rq->list);
3866         list_del_init(&phba->sli4_hba.dat_rq->list);
3867         list_del_init(&phba->sli4_hba.mbx_cq->list);
3868         list_del_init(&phba->sli4_hba.els_cq->list);
3869         for (qindx = 0; qindx < phba->cfg_fcp_wq_count; qindx++)
3870                 list_del_init(&phba->sli4_hba.fcp_wq[qindx]->list);
3871         qindx = 0;
3872         do
3873                 list_del_init(&phba->sli4_hba.fcp_cq[qindx]->list);
3874         while (++qindx < phba->cfg_fcp_eq_count);
3875         spin_unlock_irq(&phba->hbalock);
3876
3877         /* Now physically reset the device */
3878         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3879                         "0389 Performing PCI function reset!\n");
3880
3881         /* Turn off parity checking and serr during the physical reset */
3882         pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3883         pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
3884                               ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3885
3886         /* Perform FCoE PCI function reset */
3887         lpfc_pci_function_reset(phba);
3888
3889         /* Restore PCI cmd register */
3890         pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
3891
3892         return 0;
3893 }
3894
3895 /**
3896  * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
3897  * @phba: Pointer to HBA context object.
3898  *
3899  * This function is called in the SLI initialization code path to
3900  * restart the HBA. The caller is not required to hold any lock.
3901  * This function writes MBX_RESTART mailbox command to the SLIM and
3902  * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
3903  * function to free any pending commands. The function enables
3904  * POST only during the first initialization. The function returns zero.
3905  * The function does not guarantee completion of MBX_RESTART mailbox
3906  * command before the return of this function.
3907  **/
3908 static int
3909 lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
3910 {
3911         MAILBOX_t *mb;
3912         struct lpfc_sli *psli;
3913         volatile uint32_t word0;
3914         void __iomem *to_slim;
3915         uint32_t hba_aer_enabled;
3916
3917         spin_lock_irq(&phba->hbalock);
3918
3919         /* Take PCIe device Advanced Error Reporting (AER) state */
3920         hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
3921
3922         psli = &phba->sli;
3923
3924         /* Restart HBA */
3925         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3926                         "0337 Restart HBA Data: x%x x%x\n",
3927                         phba->pport->port_state, psli->sli_flag);
3928
3929         word0 = 0;
3930         mb = (MAILBOX_t *) &word0;
3931         mb->mbxCommand = MBX_RESTART;
3932         mb->mbxHc = 1;
3933
3934         lpfc_reset_barrier(phba);
3935
3936         to_slim = phba->MBslimaddr;
3937         writel(*(uint32_t *) mb, to_slim);
3938         readl(to_slim); /* flush */
3939
3940         /* Only skip post after fc_ffinit is completed */
3941         if (phba->pport->port_state)
3942                 word0 = 1;      /* This is really setting up word1 */
3943         else
3944                 word0 = 0;      /* This is really setting up word1 */
3945         to_slim = phba->MBslimaddr + sizeof (uint32_t);
3946         writel(*(uint32_t *) mb, to_slim);
3947         readl(to_slim); /* flush */
3948
3949         lpfc_sli_brdreset(phba);
3950         phba->pport->stopped = 0;
3951         phba->link_state = LPFC_INIT_START;
3952         phba->hba_flag = 0;
3953         spin_unlock_irq(&phba->hbalock);
3954
3955         memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
3956         psli->stats_start = get_seconds();
3957
3958         /* Give the INITFF and Post time to settle. */
3959         mdelay(100);
3960
3961         /* Reset HBA AER if it was enabled, note hba_flag was reset above */
3962         if (hba_aer_enabled)
3963                 pci_disable_pcie_error_reporting(phba->pcidev);
3964
3965         lpfc_hba_down_post(phba);
3966
3967         return 0;
3968 }
3969
3970 /**
3971  * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
3972  * @phba: Pointer to HBA context object.
3973  *
3974  * This function is called in the SLI initialization code path to restart
3975  * a SLI4 HBA. The caller is not required to hold any lock.
3976  * At the end of the function, it calls lpfc_hba_down_post function to
3977  * free any pending commands.
3978  **/
3979 static int
3980 lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
3981 {
3982         struct lpfc_sli *psli = &phba->sli;
3983         uint32_t hba_aer_enabled;
3984
3985         /* Restart HBA */
3986         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3987                         "0296 Restart HBA Data: x%x x%x\n",
3988                         phba->pport->port_state, psli->sli_flag);
3989
3990         /* Take PCIe device Advanced Error Reporting (AER) state */
3991         hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
3992
3993         lpfc_sli4_brdreset(phba);
3994
3995         spin_lock_irq(&phba->hbalock);
3996         phba->pport->stopped = 0;
3997         phba->link_state = LPFC_INIT_START;
3998         phba->hba_flag = 0;
3999         spin_unlock_irq(&phba->hbalock);
4000
4001         memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4002         psli->stats_start = get_seconds();
4003
4004         /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4005         if (hba_aer_enabled)
4006                 pci_disable_pcie_error_reporting(phba->pcidev);
4007
4008         lpfc_hba_down_post(phba);
4009
4010         return 0;
4011 }
4012
4013 /**
4014  * lpfc_sli_brdrestart - Wrapper func for restarting hba
4015  * @phba: Pointer to HBA context object.
4016  *
4017  * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
4018  * API jump table function pointer from the lpfc_hba struct.
4019 **/
4020 int
4021 lpfc_sli_brdrestart(struct lpfc_hba *phba)
4022 {
4023         return phba->lpfc_sli_brdrestart(phba);
4024 }
4025
4026 /**
4027  * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
4028  * @phba: Pointer to HBA context object.
4029  *
4030  * This function is called after a HBA restart to wait for successful
4031  * restart of the HBA. Successful restart of the HBA is indicated by
4032  * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
4033  * iteration, the function will restart the HBA again. The function returns
4034  * zero if HBA successfully restarted else returns negative error code.
4035  **/
4036 static int
4037 lpfc_sli_chipset_init(struct lpfc_hba *phba)
4038 {
4039         uint32_t status, i = 0;
4040
4041         /* Read the HBA Host Status Register */
4042         if (lpfc_readl(phba->HSregaddr, &status))
4043                 return -EIO;
4044
4045         /* Check status register to see what current state is */
4046         i = 0;
4047         while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
4048
4049                 /* Check every 10ms for 10 retries, then every 100ms for 90
4050                  * retries, then every 1 sec for 50 retires for a total of
4051                  * ~60 seconds before reset the board again and check every
4052                  * 1 sec for 50 retries. The up to 60 seconds before the
4053                  * board ready is required by the Falcon FIPS zeroization
4054                  * complete, and any reset the board in between shall cause
4055                  * restart of zeroization, further delay the board ready.
4056                  */
4057                 if (i++ >= 200) {
4058                         /* Adapter failed to init, timeout, status reg
4059                            <status> */
4060                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4061                                         "0436 Adapter failed to init, "
4062                                         "timeout, status reg x%x, "
4063                                         "FW Data: A8 x%x AC x%x\n", status,
4064                                         readl(phba->MBslimaddr + 0xa8),
4065                                         readl(phba->MBslimaddr + 0xac));
4066                         phba->link_state = LPFC_HBA_ERROR;
4067                         return -ETIMEDOUT;
4068                 }
4069
4070                 /* Check to see if any errors occurred during init */
4071                 if (status & HS_FFERM) {
4072                         /* ERROR: During chipset initialization */
4073                         /* Adapter failed to init, chipset, status reg
4074                            <status> */
4075                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4076                                         "0437 Adapter failed to init, "
4077                                         "chipset, status reg x%x, "
4078                                         "FW Data: A8 x%x AC x%x\n", status,
4079                                         readl(phba->MBslimaddr + 0xa8),
4080                                         readl(phba->MBslimaddr + 0xac));
4081                         phba->link_state = LPFC_HBA_ERROR;
4082                         return -EIO;
4083                 }
4084
4085                 if (i <= 10)
4086                         msleep(10);
4087                 else if (i <= 100)
4088                         msleep(100);
4089                 else
4090                         msleep(1000);
4091
4092                 if (i == 150) {
4093                         /* Do post */
4094                         phba->pport->port_state = LPFC_VPORT_UNKNOWN;
4095                         lpfc_sli_brdrestart(phba);
4096                 }
4097                 /* Read the HBA Host Status Register */
4098                 if (lpfc_readl(phba->HSregaddr, &status))
4099                         return -EIO;
4100         }
4101
4102         /* Check to see if any errors occurred during init */
4103         if (status & HS_FFERM) {
4104                 /* ERROR: During chipset initialization */
4105                 /* Adapter failed to init, chipset, status reg <status> */
4106                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4107                                 "0438 Adapter failed to init, chipset, "
4108                                 "status reg x%x, "
4109                                 "FW Data: A8 x%x AC x%x\n", status,
4110                                 readl(phba->MBslimaddr + 0xa8),
4111                                 readl(phba->MBslimaddr + 0xac));
4112                 phba->link_state = LPFC_HBA_ERROR;
4113                 return -EIO;
4114         }
4115
4116         /* Clear all interrupt enable conditions */
4117         writel(0, phba->HCregaddr);
4118         readl(phba->HCregaddr); /* flush */
4119
4120         /* setup host attn register */
4121         writel(0xffffffff, phba->HAregaddr);
4122         readl(phba->HAregaddr); /* flush */
4123         return 0;
4124 }
4125
4126 /**
4127  * lpfc_sli_hbq_count - Get the number of HBQs to be configured
4128  *
4129  * This function calculates and returns the number of HBQs required to be
4130  * configured.
4131  **/
4132 int
4133 lpfc_sli_hbq_count(void)
4134 {
4135         return ARRAY_SIZE(lpfc_hbq_defs);
4136 }
4137
4138 /**
4139  * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
4140  *
4141  * This function adds the number of hbq entries in every HBQ to get
4142  * the total number of hbq entries required for the HBA and returns
4143  * the total count.
4144  **/
4145 static int
4146 lpfc_sli_hbq_entry_count(void)
4147 {
4148         int  hbq_count = lpfc_sli_hbq_count();
4149         int  count = 0;
4150         int  i;
4151
4152         for (i = 0; i < hbq_count; ++i)
4153                 count += lpfc_hbq_defs[i]->entry_count;
4154         return count;
4155 }
4156
4157 /**
4158  * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
4159  *
4160  * This function calculates amount of memory required for all hbq entries
4161  * to be configured and returns the total memory required.
4162  **/
4163 int
4164 lpfc_sli_hbq_size(void)
4165 {
4166         return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
4167 }
4168
4169 /**
4170  * lpfc_sli_hbq_setup - configure and initialize HBQs
4171  * @phba: Pointer to HBA context object.
4172  *
4173  * This function is called during the SLI initialization to configure
4174  * all the HBQs and post buffers to the HBQ. The caller is not
4175  * required to hold any locks. This function will return zero if successful
4176  * else it will return negative error code.
4177  **/
4178 static int
4179 lpfc_sli_hbq_setup(struct lpfc_hba *phba)
4180 {
4181         int  hbq_count = lpfc_sli_hbq_count();
4182         LPFC_MBOXQ_t *pmb;
4183         MAILBOX_t *pmbox;
4184         uint32_t hbqno;
4185         uint32_t hbq_entry_index;
4186
4187                                 /* Get a Mailbox buffer to setup mailbox
4188                                  * commands for HBA initialization
4189                                  */
4190         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4191
4192         if (!pmb)
4193                 return -ENOMEM;
4194
4195         pmbox = &pmb->u.mb;
4196
4197         /* Initialize the struct lpfc_sli_hbq structure for each hbq */
4198         phba->link_state = LPFC_INIT_MBX_CMDS;
4199         phba->hbq_in_use = 1;
4200
4201         hbq_entry_index = 0;
4202         for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
4203                 phba->hbqs[hbqno].next_hbqPutIdx = 0;
4204                 phba->hbqs[hbqno].hbqPutIdx      = 0;
4205                 phba->hbqs[hbqno].local_hbqGetIdx   = 0;
4206                 phba->hbqs[hbqno].entry_count =
4207                         lpfc_hbq_defs[hbqno]->entry_count;
4208                 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
4209                         hbq_entry_index, pmb);
4210                 hbq_entry_index += phba->hbqs[hbqno].entry_count;
4211
4212                 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
4213                         /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
4214                            mbxStatus <status>, ring <num> */
4215
4216                         lpfc_printf_log(phba, KERN_ERR,
4217                                         LOG_SLI | LOG_VPORT,
4218                                         "1805 Adapter failed to init. "
4219                                         "Data: x%x x%x x%x\n",
4220                                         pmbox->mbxCommand,
4221                                         pmbox->mbxStatus, hbqno);
4222
4223                         phba->link_state = LPFC_HBA_ERROR;
4224                         mempool_free(pmb, phba->mbox_mem_pool);
4225                         return -ENXIO;
4226                 }
4227         }
4228         phba->hbq_count = hbq_count;
4229
4230         mempool_free(pmb, phba->mbox_mem_pool);
4231
4232         /* Initially populate or replenish the HBQs */
4233         for (hbqno = 0; hbqno < hbq_count; ++hbqno)
4234                 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
4235         return 0;
4236 }
4237
4238 /**
4239  * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
4240  * @phba: Pointer to HBA context object.
4241  *
4242  * This function is called during the SLI initialization to configure
4243  * all the HBQs and post buffers to the HBQ. The caller is not
4244  * required to hold any locks. This function will return zero if successful
4245  * else it will return negative error code.
4246  **/
4247 static int
4248 lpfc_sli4_rb_setup(struct lpfc_hba *phba)
4249 {
4250         phba->hbq_in_use = 1;
4251         phba->hbqs[0].entry_count = lpfc_hbq_defs[0]->entry_count;
4252         phba->hbq_count = 1;
4253         /* Initially populate or replenish the HBQs */
4254         lpfc_sli_hbqbuf_init_hbqs(phba, 0);
4255         return 0;
4256 }
4257
4258 /**
4259  * lpfc_sli_config_port - Issue config port mailbox command
4260  * @phba: Pointer to HBA context object.
4261  * @sli_mode: sli mode - 2/3
4262  *
4263  * This function is called by the sli intialization code path
4264  * to issue config_port mailbox command. This function restarts the
4265  * HBA firmware and issues a config_port mailbox command to configure
4266  * the SLI interface in the sli mode specified by sli_mode
4267  * variable. The caller is not required to hold any locks.
4268  * The function returns 0 if successful, else returns negative error
4269  * code.
4270  **/
4271 int
4272 lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
4273 {
4274         LPFC_MBOXQ_t *pmb;
4275         uint32_t resetcount = 0, rc = 0, done = 0;
4276
4277         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4278         if (!pmb) {
4279                 phba->link_state = LPFC_HBA_ERROR;
4280                 return -ENOMEM;
4281         }
4282
4283         phba->sli_rev = sli_mode;
4284         while (resetcount < 2 && !done) {
4285                 spin_lock_irq(&phba->hbalock);
4286                 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
4287                 spin_unlock_irq(&phba->hbalock);
4288                 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
4289                 lpfc_sli_brdrestart(phba);
4290                 rc = lpfc_sli_chipset_init(phba);
4291                 if (rc)
4292                         break;
4293
4294                 spin_lock_irq(&phba->hbalock);
4295                 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
4296                 spin_unlock_irq(&phba->hbalock);
4297                 resetcount++;
4298
4299                 /* Call pre CONFIG_PORT mailbox command initialization.  A
4300                  * value of 0 means the call was successful.  Any other
4301                  * nonzero value is a failure, but if ERESTART is returned,
4302                  * the driver may reset the HBA and try again.
4303                  */
4304                 rc = lpfc_config_port_prep(phba);
4305                 if (rc == -ERESTART) {
4306                         phba->link_state = LPFC_LINK_UNKNOWN;
4307                         continue;
4308                 } else if (rc)
4309                         break;
4310
4311                 phba->link_state = LPFC_INIT_MBX_CMDS;
4312                 lpfc_config_port(phba, pmb);
4313                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
4314                 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
4315                                         LPFC_SLI3_HBQ_ENABLED |
4316                                         LPFC_SLI3_CRP_ENABLED |
4317                                         LPFC_SLI3_BG_ENABLED |
4318                                         LPFC_SLI3_DSS_ENABLED);
4319                 if (rc != MBX_SUCCESS) {
4320                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4321                                 "0442 Adapter failed to init, mbxCmd x%x "
4322                                 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
4323                                 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
4324                         spin_lock_irq(&phba->hbalock);
4325                         phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
4326                         spin_unlock_irq(&phba->hbalock);
4327                         rc = -ENXIO;
4328                 } else {
4329                         /* Allow asynchronous mailbox command to go through */
4330                         spin_lock_irq(&phba->hbalock);
4331                         phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4332                         spin_unlock_irq(&phba->hbalock);
4333                         done = 1;
4334                 }
4335         }
4336         if (!done) {
4337                 rc = -EINVAL;
4338                 goto do_prep_failed;
4339         }
4340         if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
4341                 if (!pmb->u.mb.un.varCfgPort.cMA) {
4342                         rc = -ENXIO;
4343                         goto do_prep_failed;
4344                 }
4345                 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
4346                         phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
4347                         phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
4348                         phba->max_vports = (phba->max_vpi > phba->max_vports) ?
4349                                 phba->max_vpi : phba->max_vports;
4350
4351                 } else
4352                         phba->max_vpi = 0;
4353                 phba->fips_level = 0;
4354                 phba->fips_spec_rev = 0;
4355                 if (pmb->u.mb.un.varCfgPort.gdss) {
4356                         phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
4357                         phba->fips_level = pmb->u.mb.un.varCfgPort.fips_level;
4358                         phba->fips_spec_rev = pmb->u.mb.un.varCfgPort.fips_rev;
4359                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4360                                         "2850 Security Crypto Active. FIPS x%d "
4361                                         "(Spec Rev: x%d)",
4362                                         phba->fips_level, phba->fips_spec_rev);
4363                 }
4364                 if (pmb->u.mb.un.varCfgPort.sec_err) {
4365                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4366                                         "2856 Config Port Security Crypto "
4367                                         "Error: x%x ",
4368                                         pmb->u.mb.un.varCfgPort.sec_err);
4369                 }
4370                 if (pmb->u.mb.un.varCfgPort.gerbm)
4371                         phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
4372                 if (pmb->u.mb.un.varCfgPort.gcrp)
4373                         phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
4374
4375                 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
4376                 phba->port_gp = phba->mbox->us.s3_pgp.port;
4377
4378                 if (phba->cfg_enable_bg) {
4379                         if (pmb->u.mb.un.varCfgPort.gbg)
4380                                 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
4381                         else
4382                                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4383                                                 "0443 Adapter did not grant "
4384                                                 "BlockGuard\n");
4385                 }
4386         } else {
4387                 phba->hbq_get = NULL;
4388                 phba->port_gp = phba->mbox->us.s2.port;
4389                 phba->max_vpi = 0;
4390         }
4391 do_prep_failed:
4392         mempool_free(pmb, phba->mbox_mem_pool);
4393         return rc;
4394 }
4395
4396
4397 /**
4398  * lpfc_sli_hba_setup - SLI intialization function
4399  * @phba: Pointer to HBA context object.
4400  *
4401  * This function is the main SLI intialization function. This function
4402  * is called by the HBA intialization code, HBA reset code and HBA
4403  * error attention handler code. Caller is not required to hold any
4404  * locks. This function issues config_port mailbox command to configure
4405  * the SLI, setup iocb rings and HBQ rings. In the end the function
4406  * calls the config_port_post function to issue init_link mailbox
4407  * command and to start the discovery. The function will return zero
4408  * if successful, else it will return negative error code.
4409  **/
4410 int
4411 lpfc_sli_hba_setup(struct lpfc_hba *phba)
4412 {
4413         uint32_t rc;
4414         int  mode = 3, i;
4415         int longs;
4416
4417         switch (lpfc_sli_mode) {
4418         case 2:
4419                 if (phba->cfg_enable_npiv) {
4420                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
4421                                 "1824 NPIV enabled: Override lpfc_sli_mode "
4422                                 "parameter (%d) to auto (0).\n",
4423                                 lpfc_sli_mode);
4424                         break;
4425                 }
4426                 mode = 2;
4427                 break;
4428         case 0:
4429         case 3:
4430                 break;
4431         default:
4432                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
4433                                 "1819 Unrecognized lpfc_sli_mode "
4434                                 "parameter: %d.\n", lpfc_sli_mode);
4435
4436                 break;
4437         }
4438
4439         rc = lpfc_sli_config_port(phba, mode);
4440
4441         if (rc && lpfc_sli_mode == 3)
4442                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
4443                                 "1820 Unable to select SLI-3.  "
4444                                 "Not supported by adapter.\n");
4445         if (rc && mode != 2)
4446                 rc = lpfc_sli_config_port(phba, 2);
4447         if (rc)
4448                 goto lpfc_sli_hba_setup_error;
4449
4450         /* Enable PCIe device Advanced Error Reporting (AER) if configured */
4451         if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
4452                 rc = pci_enable_pcie_error_reporting(phba->pcidev);
4453                 if (!rc) {
4454                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4455                                         "2709 This device supports "
4456                                         "Advanced Error Reporting (AER)\n");
4457                         spin_lock_irq(&phba->hbalock);
4458                         phba->hba_flag |= HBA_AER_ENABLED;
4459                         spin_unlock_irq(&phba->hbalock);
4460                 } else {
4461                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4462                                         "2708 This device does not support "
4463                                         "Advanced Error Reporting (AER)\n");
4464                         phba->cfg_aer_support = 0;
4465                 }
4466         }
4467
4468         if (phba->sli_rev == 3) {
4469                 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
4470                 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
4471         } else {
4472                 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
4473                 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
4474                 phba->sli3_options = 0;
4475         }
4476
4477         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4478                         "0444 Firmware in SLI %x mode. Max_vpi %d\n",
4479                         phba->sli_rev, phba->max_vpi);
4480         rc = lpfc_sli_ring_map(phba);
4481
4482         if (rc)
4483                 goto lpfc_sli_hba_setup_error;
4484
4485         /* Initialize VPIs. */
4486         if (phba->sli_rev == LPFC_SLI_REV3) {
4487                 /*
4488                  * The VPI bitmask and physical ID array are allocated
4489                  * and initialized once only - at driver load.  A port
4490                  * reset doesn't need to reinitialize this memory.
4491                  */
4492                 if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
4493                         longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
4494                         phba->vpi_bmask = kzalloc(longs * sizeof(unsigned long),
4495                                                   GFP_KERNEL);
4496                         if (!phba->vpi_bmask) {
4497                                 rc = -ENOMEM;
4498                                 goto lpfc_sli_hba_setup_error;
4499                         }
4500
4501                         phba->vpi_ids = kzalloc(
4502                                         (phba->max_vpi+1) * sizeof(uint16_t),
4503                                         GFP_KERNEL);
4504                         if (!phba->vpi_ids) {
4505                                 kfree(phba->vpi_bmask);
4506                                 rc = -ENOMEM;
4507                                 goto lpfc_sli_hba_setup_error;
4508                         }
4509                         for (i = 0; i < phba->max_vpi; i++)
4510                                 phba->vpi_ids[i] = i;
4511                 }
4512         }
4513
4514         /* Init HBQs */
4515         if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
4516                 rc = lpfc_sli_hbq_setup(phba);
4517                 if (rc)
4518                         goto lpfc_sli_hba_setup_error;
4519         }
4520         spin_lock_irq(&phba->hbalock);
4521         phba->sli.sli_flag |= LPFC_PROCESS_LA;
4522         spin_unlock_irq(&phba->hbalock);
4523
4524         rc = lpfc_config_port_post(phba);
4525         if (rc)
4526                 goto lpfc_sli_hba_setup_error;
4527
4528         return rc;
4529
4530 lpfc_sli_hba_setup_error:
4531         phba->link_state = LPFC_HBA_ERROR;
4532         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4533                         "0445 Firmware initialization failed\n");
4534         return rc;
4535 }
4536
4537 /**
4538  * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
4539  * @phba: Pointer to HBA context object.
4540  * @mboxq: mailbox pointer.
4541  * This function issue a dump mailbox command to read config region
4542  * 23 and parse the records in the region and populate driver
4543  * data structure.
4544  **/
4545 static int
4546 lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba,
4547                 LPFC_MBOXQ_t *mboxq)
4548 {
4549         struct lpfc_dmabuf *mp;
4550         struct lpfc_mqe *mqe;
4551         uint32_t data_length;
4552         int rc;
4553
4554         /* Program the default value of vlan_id and fc_map */
4555         phba->valid_vlan = 0;
4556         phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4557         phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4558         phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
4559
4560         mqe = &mboxq->u.mqe;
4561         if (lpfc_dump_fcoe_param(phba, mboxq))
4562                 return -ENOMEM;
4563
4564         mp = (struct lpfc_dmabuf *) mboxq->context1;
4565         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4566
4567         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4568                         "(%d):2571 Mailbox cmd x%x Status x%x "
4569                         "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4570                         "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4571                         "CQ: x%x x%x x%x x%x\n",
4572                         mboxq->vport ? mboxq->vport->vpi : 0,
4573                         bf_get(lpfc_mqe_command, mqe),
4574                         bf_get(lpfc_mqe_status, mqe),
4575                         mqe->un.mb_words[0], mqe->un.mb_words[1],
4576                         mqe->un.mb_words[2], mqe->un.mb_words[3],
4577                         mqe->un.mb_words[4], mqe->un.mb_words[5],
4578                         mqe->un.mb_words[6], mqe->un.mb_words[7],
4579                         mqe->un.mb_words[8], mqe->un.mb_words[9],
4580                         mqe->un.mb_words[10], mqe->un.mb_words[11],
4581                         mqe->un.mb_words[12], mqe->un.mb_words[13],
4582                         mqe->un.mb_words[14], mqe->un.mb_words[15],
4583                         mqe->un.mb_words[16], mqe->un.mb_words[50],
4584                         mboxq->mcqe.word0,
4585                         mboxq->mcqe.mcqe_tag0,  mboxq->mcqe.mcqe_tag1,
4586                         mboxq->mcqe.trailer);
4587
4588         if (rc) {
4589                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4590                 kfree(mp);
4591                 return -EIO;
4592         }
4593         data_length = mqe->un.mb_words[5];
4594         if (data_length > DMP_RGN23_SIZE) {
4595                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4596                 kfree(mp);
4597                 return -EIO;
4598         }
4599
4600         lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
4601         lpfc_mbuf_free(phba, mp->virt, mp->phys);
4602         kfree(mp);
4603         return 0;
4604 }
4605
4606 /**
4607  * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
4608  * @phba: pointer to lpfc hba data structure.
4609  * @mboxq: pointer to the LPFC_MBOXQ_t structure.
4610  * @vpd: pointer to the memory to hold resulting port vpd data.
4611  * @vpd_size: On input, the number of bytes allocated to @vpd.
4612  *            On output, the number of data bytes in @vpd.
4613  *
4614  * This routine executes a READ_REV SLI4 mailbox command.  In
4615  * addition, this routine gets the port vpd data.
4616  *
4617  * Return codes
4618  *      0 - successful
4619  *      -ENOMEM - could not allocated memory.
4620  **/
4621 static int
4622 lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
4623                     uint8_t *vpd, uint32_t *vpd_size)
4624 {
4625         int rc = 0;
4626         uint32_t dma_size;
4627         struct lpfc_dmabuf *dmabuf;
4628         struct lpfc_mqe *mqe;
4629
4630         dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4631         if (!dmabuf)
4632                 return -ENOMEM;
4633
4634         /*
4635          * Get a DMA buffer for the vpd data resulting from the READ_REV
4636          * mailbox command.
4637          */
4638         dma_size = *vpd_size;
4639         dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
4640                                           dma_size,
4641                                           &dmabuf->phys,
4642                                           GFP_KERNEL);
4643         if (!dmabuf->virt) {
4644                 kfree(dmabuf);
4645                 return -ENOMEM;
4646         }
4647         memset(dmabuf->virt, 0, dma_size);
4648
4649         /*
4650          * The SLI4 implementation of READ_REV conflicts at word1,
4651          * bits 31:16 and SLI4 adds vpd functionality not present
4652          * in SLI3.  This code corrects the conflicts.
4653          */
4654         lpfc_read_rev(phba, mboxq);
4655         mqe = &mboxq->u.mqe;
4656         mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
4657         mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
4658         mqe->un.read_rev.word1 &= 0x0000FFFF;
4659         bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
4660         bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
4661
4662         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4663         if (rc) {
4664                 dma_free_coherent(&phba->pcidev->dev, dma_size,
4665                                   dmabuf->virt, dmabuf->phys);
4666                 kfree(dmabuf);
4667                 return -EIO;
4668         }
4669
4670         /*
4671          * The available vpd length cannot be bigger than the
4672          * DMA buffer passed to the port.  Catch the less than
4673          * case and update the caller's size.
4674          */
4675         if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
4676                 *vpd_size = mqe->un.read_rev.avail_vpd_len;
4677
4678         memcpy(vpd, dmabuf->virt, *vpd_size);
4679
4680         dma_free_coherent(&phba->pcidev->dev, dma_size,
4681                           dmabuf->virt, dmabuf->phys);
4682         kfree(dmabuf);
4683         return 0;
4684 }
4685
4686 /**
4687  * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
4688  * @phba: pointer to lpfc hba data structure.
4689  *
4690  * This routine is called to explicitly arm the SLI4 device's completion and
4691  * event queues
4692  **/
4693 static void
4694 lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
4695 {
4696         uint8_t fcp_eqidx;
4697
4698         lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
4699         lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
4700         fcp_eqidx = 0;
4701         do
4702                 lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[fcp_eqidx],
4703                                      LPFC_QUEUE_REARM);
4704         while (++fcp_eqidx < phba->cfg_fcp_eq_count);
4705         lpfc_sli4_eq_release(phba->sli4_hba.sp_eq, LPFC_QUEUE_REARM);
4706         for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++)
4707                 lpfc_sli4_eq_release(phba->sli4_hba.fp_eq[fcp_eqidx],
4708                                      LPFC_QUEUE_REARM);
4709 }
4710
4711 /**
4712  * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
4713  * @phba: Pointer to HBA context object.
4714  * @type: The resource extent type.
4715  *
4716  * This function allocates all SLI4 resource identifiers.
4717  **/
4718 static int
4719 lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
4720                                uint16_t *extnt_count, uint16_t *extnt_size)
4721 {
4722         int rc = 0;
4723         uint32_t length;
4724         uint32_t mbox_tmo;
4725         struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
4726         LPFC_MBOXQ_t *mbox;
4727
4728         mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4729         if (!mbox)
4730                 return -ENOMEM;
4731
4732         /* Find out how many extents are available for this resource type */
4733         length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
4734                   sizeof(struct lpfc_sli4_cfg_mhdr));
4735         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
4736                          LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
4737                          length, LPFC_SLI4_MBX_EMBED);
4738
4739         /* Send an extents count of 0 - the GET doesn't use it. */
4740         rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
4741                                         LPFC_SLI4_MBX_EMBED);
4742         if (unlikely(rc)) {
4743                 rc = -EIO;
4744                 goto err_exit;
4745         }
4746
4747         if (!phba->sli4_hba.intr_enable)
4748                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
4749         else {
4750                 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
4751                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
4752         }
4753         if (unlikely(rc)) {
4754                 rc = -EIO;
4755                 goto err_exit;
4756         }
4757
4758         rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
4759         if (bf_get(lpfc_mbox_hdr_status,
4760                    &rsrc_info->header.cfg_shdr.response)) {
4761                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
4762                                 "2930 Failed to get resource extents "
4763                                 "Status 0x%x Add'l Status 0x%x\n",
4764                                 bf_get(lpfc_mbox_hdr_status,
4765                                        &rsrc_info->header.cfg_shdr.response),
4766                                 bf_get(lpfc_mbox_hdr_add_status,
4767                                        &rsrc_info->header.cfg_shdr.response));
4768                 rc = -EIO;
4769                 goto err_exit;
4770         }
4771
4772         *extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
4773                               &rsrc_info->u.rsp);
4774         *extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
4775                              &rsrc_info->u.rsp);
4776  err_exit:
4777         mempool_free(mbox, phba->mbox_mem_pool);
4778         return rc;
4779 }
4780
4781 /**
4782  * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
4783  * @phba: Pointer to HBA context object.
4784  * @type: The extent type to check.
4785  *
4786  * This function reads the current available extents from the port and checks
4787  * if the extent count or extent size has changed since the last access.
4788  * Callers use this routine post port reset to understand if there is a
4789  * extent reprovisioning requirement.
4790  *
4791  * Returns:
4792  *   -Error: error indicates problem.
4793  *   1: Extent count or size has changed.
4794  *   0: No changes.
4795  **/
4796 static int
4797 lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
4798 {
4799         uint16_t curr_ext_cnt, rsrc_ext_cnt;
4800         uint16_t size_diff, rsrc_ext_size;
4801         int rc = 0;
4802         struct lpfc_rsrc_blks *rsrc_entry;
4803         struct list_head *rsrc_blk_list = NULL;
4804
4805         size_diff = 0;
4806         curr_ext_cnt = 0;
4807         rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
4808                                             &rsrc_ext_cnt,
4809                                             &rsrc_ext_size);
4810         if (unlikely(rc))
4811                 return -EIO;
4812
4813         switch (type) {
4814         case LPFC_RSC_TYPE_FCOE_RPI:
4815                 rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
4816                 break;
4817         case LPFC_RSC_TYPE_FCOE_VPI:
4818                 rsrc_blk_list = &phba->lpfc_vpi_blk_list;
4819                 break;
4820         case LPFC_RSC_TYPE_FCOE_XRI:
4821                 rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
4822                 break;
4823         case LPFC_RSC_TYPE_FCOE_VFI:
4824                 rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
4825                 break;
4826         default:
4827                 break;
4828         }
4829
4830         list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
4831                 curr_ext_cnt++;
4832                 if (rsrc_entry->rsrc_size != rsrc_ext_size)
4833                         size_diff++;
4834         }
4835
4836         if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
4837                 rc = 1;
4838
4839         return rc;
4840 }
4841
4842 /**
4843  * lpfc_sli4_cfg_post_extnts -
4844  * @phba: Pointer to HBA context object.
4845  * @extnt_cnt - number of available extents.
4846  * @type - the extent type (rpi, xri, vfi, vpi).
4847  * @emb - buffer to hold either MBX_EMBED or MBX_NEMBED operation.
4848  * @mbox - pointer to the caller's allocated mailbox structure.
4849  *
4850  * This function executes the extents allocation request.  It also
4851  * takes care of the amount of memory needed to allocate or get the
4852  * allocated extents. It is the caller's responsibility to evaluate
4853  * the response.
4854  *
4855  * Returns:
4856  *   -Error:  Error value describes the condition found.
4857  *   0: if successful
4858  **/
4859 static int
4860 lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t *extnt_cnt,
4861                           uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
4862 {
4863         int rc = 0;
4864         uint32_t req_len;
4865         uint32_t emb_len;
4866         uint32_t alloc_len, mbox_tmo;
4867
4868         /* Calculate the total requested length of the dma memory */
4869         req_len = *extnt_cnt * sizeof(uint16_t);
4870
4871         /*
4872          * Calculate the size of an embedded mailbox.  The uint32_t
4873          * accounts for extents-specific word.
4874          */
4875         emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
4876                 sizeof(uint32_t);
4877
4878         /*
4879          * Presume the allocation and response will fit into an embedded
4880          * mailbox.  If not true, reconfigure to a non-embedded mailbox.
4881          */
4882         *emb = LPFC_SLI4_MBX_EMBED;
4883         if (req_len > emb_len) {
4884                 req_len = *extnt_cnt * sizeof(uint16_t) +
4885                         sizeof(union lpfc_sli4_cfg_shdr) +
4886                         sizeof(uint32_t);
4887                 *emb = LPFC_SLI4_MBX_NEMBED;
4888         }
4889
4890         alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
4891                                      LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
4892                                      req_len, *emb);
4893         if (alloc_len < req_len) {
4894                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4895                         "9000 Allocated DMA memory size (x%x) is "
4896                         "less than the requested DMA memory "
4897                         "size (x%x)\n", alloc_len, req_len);
4898                 return -ENOMEM;
4899         }
4900         rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, *extnt_cnt, type, *emb);
4901         if (unlikely(rc))
4902                 return -EIO;
4903
4904         if (!phba->sli4_hba.intr_enable)
4905                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
4906         else {
4907                 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
4908                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
4909         }
4910
4911         if (unlikely(rc))
4912                 rc = -EIO;
4913         return rc;
4914 }
4915
4916 /**
4917  * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
4918  * @phba: Pointer to HBA context object.
4919  * @type:  The resource extent type to allocate.
4920  *
4921  * This function allocates the number of elements for the specified
4922  * resource type.
4923  **/
4924 static int
4925 lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
4926 {
4927         bool emb = false;
4928         uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
4929         uint16_t rsrc_id, rsrc_start, j, k;
4930         uint16_t *ids;
4931         int i, rc;
4932         unsigned long longs;
4933         unsigned long *bmask;
4934         struct lpfc_rsrc_blks *rsrc_blks;
4935         LPFC_MBOXQ_t *mbox;
4936         uint32_t length;
4937         struct lpfc_id_range *id_array = NULL;
4938         void *virtaddr = NULL;
4939         struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
4940         struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
4941         struct list_head *ext_blk_list;
4942
4943         rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
4944                                             &rsrc_cnt,
4945                                             &rsrc_size);
4946         if (unlikely(rc))
4947                 return -EIO;
4948
4949         if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
4950                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
4951                         "3009 No available Resource Extents "
4952                         "for resource type 0x%x: Count: 0x%x, "
4953                         "Size 0x%x\n", type, rsrc_cnt,
4954                         rsrc_size);
4955                 return -ENOMEM;
4956         }
4957
4958         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT,
4959                         "2903 Available Resource Extents "
4960                         "for resource type 0x%x: Count: 0x%x, "
4961                         "Size 0x%x\n", type, rsrc_cnt,
4962                         rsrc_size);
4963
4964         mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4965         if (!mbox)
4966                 return -ENOMEM;
4967
4968         rc = lpfc_sli4_cfg_post_extnts(phba, &rsrc_cnt, type, &emb, mbox);
4969         if (unlikely(rc)) {
4970                 rc = -EIO;
4971                 goto err_exit;
4972         }
4973
4974         /*
4975          * Figure out where the response is located.  Then get local pointers
4976          * to the response data.  The port does not guarantee to respond to
4977          * all extents counts request so update the local variable with the
4978          * allocated count from the port.
4979          */
4980         if (emb == LPFC_SLI4_MBX_EMBED) {
4981                 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
4982                 id_array = &rsrc_ext->u.rsp.id[0];
4983                 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
4984         } else {
4985                 virtaddr = mbox->sge_array->addr[0];
4986                 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
4987                 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
4988                 id_array = &n_rsrc->id;
4989         }
4990
4991         longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
4992         rsrc_id_cnt = rsrc_cnt * rsrc_size;
4993
4994         /*
4995          * Based on the resource size and count, correct the base and max
4996          * resource values.
4997          */
4998         length = sizeof(struct lpfc_rsrc_blks);
4999         switch (type) {
5000         case LPFC_RSC_TYPE_FCOE_RPI:
5001                 phba->sli4_hba.rpi_bmask = kzalloc(longs *
5002                                                    sizeof(unsigned long),
5003                                                    GFP_KERNEL);
5004                 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5005                         rc = -ENOMEM;
5006                         goto err_exit;
5007                 }
5008                 phba->sli4_hba.rpi_ids = kzalloc(rsrc_id_cnt *
5009                                                  sizeof(uint16_t),
5010                                                  GFP_KERNEL);
5011                 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5012                         kfree(phba->sli4_hba.rpi_bmask);
5013                         rc = -ENOMEM;
5014                         goto err_exit;
5015                 }
5016
5017                 /*
5018                  * The next_rpi was initialized with the maximum available
5019                  * count but the port may allocate a smaller number.  Catch
5020                  * that case and update the next_rpi.
5021                  */
5022                 phba->sli4_hba.next_rpi = rsrc_id_cnt;
5023
5024                 /* Initialize local ptrs for common extent processing later. */
5025                 bmask = phba->sli4_hba.rpi_bmask;
5026                 ids = phba->sli4_hba.rpi_ids;
5027                 ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5028                 break;
5029         case LPFC_RSC_TYPE_FCOE_VPI:
5030                 phba->vpi_bmask = kzalloc(longs *
5031                                           sizeof(unsigned long),
5032                                           GFP_KERNEL);
5033                 if (unlikely(!phba->vpi_bmask)) {
5034                         rc = -ENOMEM;
5035                         goto err_exit;
5036                 }
5037                 phba->vpi_ids = kzalloc(rsrc_id_cnt *
5038                                          sizeof(uint16_t),
5039                                          GFP_KERNEL);
5040                 if (unlikely(!phba->vpi_ids)) {
5041                         kfree(phba->vpi_bmask);
5042                         rc = -ENOMEM;
5043                         goto err_exit;
5044                 }
5045
5046                 /* Initialize local ptrs for common extent processing later. */
5047                 bmask = phba->vpi_bmask;
5048                 ids = phba->vpi_ids;
5049                 ext_blk_list = &phba->lpfc_vpi_blk_list;
5050                 break;
5051         case LPFC_RSC_TYPE_FCOE_XRI:
5052                 phba->sli4_hba.xri_bmask = kzalloc(longs *
5053                                                    sizeof(unsigned long),
5054                                                    GFP_KERNEL);
5055                 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5056                         rc = -ENOMEM;
5057                         goto err_exit;
5058                 }
5059                 phba->sli4_hba.xri_ids = kzalloc(rsrc_id_cnt *
5060                                                  sizeof(uint16_t),
5061                                                  GFP_KERNEL);
5062                 if (unlikely(!phba->sli4_hba.xri_ids)) {
5063                         kfree(phba->sli4_hba.xri_bmask);
5064                         rc = -ENOMEM;
5065                         goto err_exit;
5066                 }
5067
5068                 /* Initialize local ptrs for common extent processing later. */
5069                 bmask = phba->sli4_hba.xri_bmask;
5070                 ids = phba->sli4_hba.xri_ids;
5071                 ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5072                 break;
5073         case LPFC_RSC_TYPE_FCOE_VFI:
5074                 phba->sli4_hba.vfi_bmask = kzalloc(longs *
5075                                                    sizeof(unsigned long),
5076                                                    GFP_KERNEL);
5077                 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5078                         rc = -ENOMEM;
5079                         goto err_exit;
5080                 }
5081                 phba->sli4_hba.vfi_ids = kzalloc(rsrc_id_cnt *
5082                                                  sizeof(uint16_t),
5083                                                  GFP_KERNEL);
5084                 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5085                         kfree(phba->sli4_hba.vfi_bmask);
5086                         rc = -ENOMEM;
5087                         goto err_exit;
5088                 }
5089
5090                 /* Initialize local ptrs for common extent processing later. */
5091                 bmask = phba->sli4_hba.vfi_bmask;
5092                 ids = phba->sli4_hba.vfi_ids;
5093                 ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5094                 break;
5095         default:
5096                 /* Unsupported Opcode.  Fail call. */
5097                 id_array = NULL;
5098                 bmask = NULL;
5099                 ids = NULL;
5100                 ext_blk_list = NULL;
5101                 goto err_exit;
5102         }
5103
5104         /*
5105          * Complete initializing the extent configuration with the
5106          * allocated ids assigned to this function.  The bitmask serves
5107          * as an index into the array and manages the available ids.  The
5108          * array just stores the ids communicated to the port via the wqes.
5109          */
5110         for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
5111                 if ((i % 2) == 0)
5112                         rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
5113                                          &id_array[k]);
5114                 else
5115                         rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
5116                                          &id_array[k]);
5117
5118                 rsrc_blks = kzalloc(length, GFP_KERNEL);
5119                 if (unlikely(!rsrc_blks)) {
5120                         rc = -ENOMEM;
5121                         kfree(bmask);
5122                         kfree(ids);
5123                         goto err_exit;
5124                 }
5125                 rsrc_blks->rsrc_start = rsrc_id;
5126                 rsrc_blks->rsrc_size = rsrc_size;
5127                 list_add_tail(&rsrc_blks->list, ext_blk_list);
5128                 rsrc_start = rsrc_id;
5129                 if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0))
5130                         phba->sli4_hba.scsi_xri_start = rsrc_start +
5131                                 lpfc_sli4_get_els_iocb_cnt(phba);
5132
5133                 while (rsrc_id < (rsrc_start + rsrc_size)) {
5134                         ids[j] = rsrc_id;
5135                         rsrc_id++;
5136                         j++;
5137                 }
5138                 /* Entire word processed.  Get next word.*/
5139                 if ((i % 2) == 1)
5140                         k++;
5141         }
5142  err_exit:
5143         lpfc_sli4_mbox_cmd_free(phba, mbox);
5144         return rc;
5145 }
5146
5147 /**
5148  * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
5149  * @phba: Pointer to HBA context object.
5150  * @type: the extent's type.
5151  *
5152  * This function deallocates all extents of a particular resource type.
5153  * SLI4 does not allow for deallocating a particular extent range.  It
5154  * is the caller's responsibility to release all kernel memory resources.
5155  **/
5156 static int
5157 lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
5158 {
5159         int rc;
5160         uint32_t length, mbox_tmo = 0;
5161         LPFC_MBOXQ_t *mbox;
5162         struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
5163         struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
5164
5165         mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5166         if (!mbox)
5167                 return -ENOMEM;
5168
5169         /*
5170          * This function sends an embedded mailbox because it only sends the
5171          * the resource type.  All extents of this type are released by the
5172          * port.
5173          */
5174         length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
5175                   sizeof(struct lpfc_sli4_cfg_mhdr));
5176         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5177                          LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
5178                          length, LPFC_SLI4_MBX_EMBED);
5179
5180         /* Send an extents count of 0 - the dealloc doesn't use it. */
5181         rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5182                                         LPFC_SLI4_MBX_EMBED);
5183         if (unlikely(rc)) {
5184                 rc = -EIO;
5185                 goto out_free_mbox;
5186         }
5187         if (!phba->sli4_hba.intr_enable)
5188                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5189         else {
5190                 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox_tmo);
5191                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5192         }
5193         if (unlikely(rc)) {
5194                 rc = -EIO;
5195                 goto out_free_mbox;
5196         }
5197
5198         dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
5199         if (bf_get(lpfc_mbox_hdr_status,
5200                    &dealloc_rsrc->header.cfg_shdr.response)) {
5201                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5202                                 "2919 Failed to release resource extents "
5203                                 "for type %d - Status 0x%x Add'l Status 0x%x. "
5204                                 "Resource memory not released.\n",
5205                                 type,
5206                                 bf_get(lpfc_mbox_hdr_status,
5207                                     &dealloc_rsrc->header.cfg_shdr.response),
5208                                 bf_get(lpfc_mbox_hdr_add_status,
5209                                     &dealloc_rsrc->header.cfg_shdr.response));
5210                 rc = -EIO;
5211                 goto out_free_mbox;
5212         }
5213
5214         /* Release kernel memory resources for the specific type. */
5215         switch (type) {
5216         case LPFC_RSC_TYPE_FCOE_VPI:
5217                 kfree(phba->vpi_bmask);
5218                 kfree(phba->vpi_ids);
5219                 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5220                 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5221                                     &phba->lpfc_vpi_blk_list, list) {
5222                         list_del_init(&rsrc_blk->list);
5223                         kfree(rsrc_blk);
5224                 }
5225                 break;
5226         case LPFC_RSC_TYPE_FCOE_XRI:
5227                 kfree(phba->sli4_hba.xri_bmask);
5228                 kfree(phba->sli4_hba.xri_ids);
5229                 bf_set(lpfc_xri_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5230                 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5231                                     &phba->sli4_hba.lpfc_xri_blk_list, list) {
5232                         list_del_init(&rsrc_blk->list);
5233                         kfree(rsrc_blk);
5234                 }
5235                 break;
5236         case LPFC_RSC_TYPE_FCOE_VFI:
5237                 kfree(phba->sli4_hba.vfi_bmask);
5238                 kfree(phba->sli4_hba.vfi_ids);
5239                 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5240                 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5241                                     &phba->sli4_hba.lpfc_vfi_blk_list, list) {
5242                         list_del_init(&rsrc_blk->list);
5243                         kfree(rsrc_blk);
5244                 }
5245                 break;
5246         case LPFC_RSC_TYPE_FCOE_RPI:
5247                 /* RPI bitmask and physical id array are cleaned up earlier. */
5248                 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5249                                     &phba->sli4_hba.lpfc_rpi_blk_list, list) {
5250                         list_del_init(&rsrc_blk->list);
5251                         kfree(rsrc_blk);
5252                 }
5253                 break;
5254         default:
5255                 break;
5256         }
5257
5258         bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5259
5260  out_free_mbox:
5261         mempool_free(mbox, phba->mbox_mem_pool);
5262         return rc;
5263 }
5264
5265 /**
5266  * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
5267  * @phba: Pointer to HBA context object.
5268  *
5269  * This function allocates all SLI4 resource identifiers.
5270  **/
5271 int
5272 lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
5273 {
5274         int i, rc, error = 0;
5275         uint16_t count, base;
5276         unsigned long longs;
5277
5278         if (phba->sli4_hba.extents_in_use) {
5279                 /*
5280                  * The port supports resource extents. The XRI, VPI, VFI, RPI
5281                  * resource extent count must be read and allocated before
5282                  * provisioning the resource id arrays.
5283                  */
5284                 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
5285                     LPFC_IDX_RSRC_RDY) {
5286                         /*
5287                          * Extent-based resources are set - the driver could
5288                          * be in a port reset. Figure out if any corrective
5289                          * actions need to be taken.
5290                          */
5291                         rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5292                                                  LPFC_RSC_TYPE_FCOE_VFI);
5293                         if (rc != 0)
5294                                 error++;
5295                         rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5296                                                  LPFC_RSC_TYPE_FCOE_VPI);
5297                         if (rc != 0)
5298                                 error++;
5299                         rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5300                                                  LPFC_RSC_TYPE_FCOE_XRI);
5301                         if (rc != 0)
5302                                 error++;
5303                         rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5304                                                  LPFC_RSC_TYPE_FCOE_RPI);
5305                         if (rc != 0)
5306                                 error++;
5307
5308                         /*
5309                          * It's possible that the number of resources
5310                          * provided to this port instance changed between
5311                          * resets.  Detect this condition and reallocate
5312                          * resources.  Otherwise, there is no action.
5313                          */
5314                         if (error) {
5315                                 lpfc_printf_log(phba, KERN_INFO,
5316                                                 LOG_MBOX | LOG_INIT,
5317                                                 "2931 Detected extent resource "
5318                                                 "change.  Reallocating all "
5319                                                 "extents.\n");
5320                                 rc = lpfc_sli4_dealloc_extent(phba,
5321                                                  LPFC_RSC_TYPE_FCOE_VFI);
5322                                 rc = lpfc_sli4_dealloc_extent(phba,
5323                                                  LPFC_RSC_TYPE_FCOE_VPI);
5324                                 rc = lpfc_sli4_dealloc_extent(phba,
5325                                                  LPFC_RSC_TYPE_FCOE_XRI);
5326                                 rc = lpfc_sli4_dealloc_extent(phba,
5327                                                  LPFC_RSC_TYPE_FCOE_RPI);
5328                         } else
5329                                 return 0;
5330                 }
5331
5332                 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
5333                 if (unlikely(rc))
5334                         goto err_exit;
5335
5336                 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
5337                 if (unlikely(rc))
5338                         goto err_exit;
5339
5340                 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
5341                 if (unlikely(rc))
5342                         goto err_exit;
5343
5344                 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
5345                 if (unlikely(rc))
5346                         goto err_exit;
5347                 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
5348                        LPFC_IDX_RSRC_RDY);
5349                 return rc;
5350         } else {
5351                 /*
5352                  * The port does not support resource extents.  The XRI, VPI,
5353                  * VFI, RPI resource ids were determined from READ_CONFIG.
5354                  * Just allocate the bitmasks and provision the resource id
5355                  * arrays.  If a port reset is active, the resources don't
5356                  * need any action - just exit.
5357                  */
5358                 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
5359                     LPFC_IDX_RSRC_RDY)
5360                         return 0;
5361
5362                 /* RPIs. */
5363                 count = phba->sli4_hba.max_cfg_param.max_rpi;
5364                 base = phba->sli4_hba.max_cfg_param.rpi_base;
5365                 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5366                 phba->sli4_hba.rpi_bmask = kzalloc(longs *
5367                                                    sizeof(unsigned long),
5368                                                    GFP_KERNEL);
5369                 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5370                         rc = -ENOMEM;
5371                         goto err_exit;
5372                 }
5373                 phba->sli4_hba.rpi_ids = kzalloc(count *
5374                                                  sizeof(uint16_t),
5375                                                  GFP_KERNEL);
5376                 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5377                         rc = -ENOMEM;
5378                         goto free_rpi_bmask;
5379                 }
5380
5381                 for (i = 0; i < count; i++)
5382                         phba->sli4_hba.rpi_ids[i] = base + i;
5383
5384                 /* VPIs. */
5385                 count = phba->sli4_hba.max_cfg_param.max_vpi;
5386                 base = phba->sli4_hba.max_cfg_param.vpi_base;
5387                 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5388                 phba->vpi_bmask = kzalloc(longs *
5389                                           sizeof(unsigned long),
5390                                           GFP_KERNEL);
5391                 if (unlikely(!phba->vpi_bmask)) {
5392                         rc = -ENOMEM;
5393                         goto free_rpi_ids;
5394                 }
5395                 phba->vpi_ids = kzalloc(count *
5396                                         sizeof(uint16_t),
5397                                         GFP_KERNEL);
5398                 if (unlikely(!phba->vpi_ids)) {
5399                         rc = -ENOMEM;
5400                         goto free_vpi_bmask;
5401                 }
5402
5403                 for (i = 0; i < count; i++)
5404                         phba->vpi_ids[i] = base + i;
5405
5406                 /* XRIs. */
5407                 count = phba->sli4_hba.max_cfg_param.max_xri;
5408                 base = phba->sli4_hba.max_cfg_param.xri_base;
5409                 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5410                 phba->sli4_hba.xri_bmask = kzalloc(longs *
5411                                                    sizeof(unsigned long),
5412                                                    GFP_KERNEL);
5413                 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5414                         rc = -ENOMEM;
5415                         goto free_vpi_ids;
5416                 }
5417                 phba->sli4_hba.xri_ids = kzalloc(count *
5418                                                  sizeof(uint16_t),
5419                                                  GFP_KERNEL);
5420                 if (unlikely(!phba->sli4_hba.xri_ids)) {
5421                         rc = -ENOMEM;
5422                         goto free_xri_bmask;
5423                 }
5424
5425                 for (i = 0; i < count; i++)
5426                         phba->sli4_hba.xri_ids[i] = base + i;
5427
5428                 /* VFIs. */
5429                 count = phba->sli4_hba.max_cfg_param.max_vfi;
5430                 base = phba->sli4_hba.max_cfg_param.vfi_base;
5431                 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5432                 phba->sli4_hba.vfi_bmask = kzalloc(longs *
5433                                                    sizeof(unsigned long),
5434                                                    GFP_KERNEL);
5435                 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5436                         rc = -ENOMEM;
5437                         goto free_xri_ids;
5438                 }
5439                 phba->sli4_hba.vfi_ids = kzalloc(count *
5440                                                  sizeof(uint16_t),
5441                                                  GFP_KERNEL);
5442                 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5443                         rc = -ENOMEM;
5444                         goto free_vfi_bmask;
5445                 }
5446
5447                 for (i = 0; i < count; i++)
5448                         phba->sli4_hba.vfi_ids[i] = base + i;
5449
5450                 /*
5451                  * Mark all resources ready.  An HBA reset doesn't need
5452                  * to reset the initialization.
5453                  */
5454                 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
5455                        LPFC_IDX_RSRC_RDY);
5456                 return 0;
5457         }
5458
5459  free_vfi_bmask:
5460         kfree(phba->sli4_hba.vfi_bmask);
5461  free_xri_ids:
5462         kfree(phba->sli4_hba.xri_ids);
5463  free_xri_bmask:
5464         kfree(phba->sli4_hba.xri_bmask);
5465  free_vpi_ids:
5466         kfree(phba->vpi_ids);
5467  free_vpi_bmask:
5468         kfree(phba->vpi_bmask);
5469  free_rpi_ids:
5470         kfree(phba->sli4_hba.rpi_ids);
5471  free_rpi_bmask:
5472         kfree(phba->sli4_hba.rpi_bmask);
5473  err_exit:
5474         return rc;
5475 }
5476
5477 /**
5478  * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
5479  * @phba: Pointer to HBA context object.
5480  *
5481  * This function allocates the number of elements for the specified
5482  * resource type.
5483  **/
5484 int
5485 lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
5486 {
5487         if (phba->sli4_hba.extents_in_use) {
5488                 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
5489                 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
5490                 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
5491                 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
5492         } else {
5493                 kfree(phba->vpi_bmask);
5494                 kfree(phba->vpi_ids);
5495                 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5496                 kfree(phba->sli4_hba.xri_bmask);
5497                 kfree(phba->sli4_hba.xri_ids);
5498                 bf_set(lpfc_xri_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5499                 kfree(phba->sli4_hba.vfi_bmask);
5500                 kfree(phba->sli4_hba.vfi_ids);
5501                 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5502                 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5503         }
5504
5505         return 0;
5506 }
5507
5508 /**
5509  * lpfc_sli4_hba_setup - SLI4 device intialization PCI function
5510  * @phba: Pointer to HBA context object.
5511  *
5512  * This function is the main SLI4 device intialization PCI function. This
5513  * function is called by the HBA intialization code, HBA reset code and
5514  * HBA error attention handler code. Caller is not required to hold any
5515  * locks.
5516  **/
5517 int
5518 lpfc_sli4_hba_setup(struct lpfc_hba *phba)
5519 {
5520         int rc;
5521         LPFC_MBOXQ_t *mboxq;
5522         struct lpfc_mqe *mqe;
5523         uint8_t *vpd;
5524         uint32_t vpd_size;
5525         uint32_t ftr_rsp = 0;
5526         struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
5527         struct lpfc_vport *vport = phba->pport;
5528         struct lpfc_dmabuf *mp;
5529
5530         /* Perform a PCI function reset to start from clean */
5531         rc = lpfc_pci_function_reset(phba);
5532         if (unlikely(rc))
5533                 return -ENODEV;
5534
5535         /* Check the HBA Host Status Register for readyness */
5536         rc = lpfc_sli4_post_status_check(phba);
5537         if (unlikely(rc))
5538                 return -ENODEV;
5539         else {
5540                 spin_lock_irq(&phba->hbalock);
5541                 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
5542                 spin_unlock_irq(&phba->hbalock);
5543         }
5544
5545         /*
5546          * Allocate a single mailbox container for initializing the
5547          * port.
5548          */
5549         mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5550         if (!mboxq)
5551                 return -ENOMEM;
5552
5553         /*
5554          * Continue initialization with default values even if driver failed
5555          * to read FCoE param config regions
5556          */
5557         if (lpfc_sli4_read_fcoe_params(phba, mboxq))
5558                 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
5559                         "2570 Failed to read FCoE parameters\n");
5560
5561         /* Issue READ_REV to collect vpd and FW information. */
5562         vpd_size = SLI4_PAGE_SIZE;
5563         vpd = kzalloc(vpd_size, GFP_KERNEL);
5564         if (!vpd) {
5565                 rc = -ENOMEM;
5566                 goto out_free_mbox;
5567         }
5568
5569         rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
5570         if (unlikely(rc)) {
5571                 kfree(vpd);
5572                 goto out_free_mbox;
5573         }
5574         mqe = &mboxq->u.mqe;
5575         phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
5576         if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev))
5577                 phba->hba_flag |= HBA_FCOE_MODE;
5578         else
5579                 phba->hba_flag &= ~HBA_FCOE_MODE;
5580
5581         if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
5582                 LPFC_DCBX_CEE_MODE)
5583                 phba->hba_flag |= HBA_FIP_SUPPORT;
5584         else
5585                 phba->hba_flag &= ~HBA_FIP_SUPPORT;
5586
5587         if (phba->sli_rev != LPFC_SLI_REV4) {
5588                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5589                         "0376 READ_REV Error. SLI Level %d "
5590                         "FCoE enabled %d\n",
5591                         phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
5592                 rc = -EIO;
5593                 kfree(vpd);
5594                 goto out_free_mbox;
5595         }
5596         /*
5597          * Evaluate the read rev and vpd data. Populate the driver
5598          * state with the results. If this routine fails, the failure
5599          * is not fatal as the driver will use generic values.
5600          */
5601         rc = lpfc_parse_vpd(phba, vpd, vpd_size);
5602         if (unlikely(!rc)) {
5603                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5604                                 "0377 Error %d parsing vpd. "
5605                                 "Using defaults.\n", rc);
5606                 rc = 0;
5607         }
5608         kfree(vpd);
5609
5610         /* Save information as VPD data */
5611         phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
5612         phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
5613         phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
5614         phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
5615                                          &mqe->un.read_rev);
5616         phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
5617                                        &mqe->un.read_rev);
5618         phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
5619                                             &mqe->un.read_rev);
5620         phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
5621                                            &mqe->un.read_rev);
5622         phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
5623         memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
5624         phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
5625         memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
5626         phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
5627         memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
5628         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5629                         "(%d):0380 READ_REV Status x%x "
5630                         "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
5631                         mboxq->vport ? mboxq->vport->vpi : 0,
5632                         bf_get(lpfc_mqe_status, mqe),
5633                         phba->vpd.rev.opFwName,
5634                         phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
5635                         phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
5636
5637         /*
5638          * Discover the port's supported feature set and match it against the
5639          * hosts requests.
5640          */
5641         lpfc_request_features(phba, mboxq);
5642         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5643         if (unlikely(rc)) {
5644                 rc = -EIO;
5645                 goto out_free_mbox;
5646         }
5647
5648         /*
5649          * The port must support FCP initiator mode as this is the
5650          * only mode running in the host.
5651          */
5652         if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
5653                 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
5654                                 "0378 No support for fcpi mode.\n");
5655                 ftr_rsp++;
5656         }
5657         if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
5658                 phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
5659         else
5660                 phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
5661         /*
5662          * If the port cannot support the host's requested features
5663          * then turn off the global config parameters to disable the
5664          * feature in the driver.  This is not a fatal error.
5665          */
5666         if ((phba->cfg_enable_bg) &&
5667             !(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
5668                 ftr_rsp++;
5669
5670         if (phba->max_vpi && phba->cfg_enable_npiv &&
5671             !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
5672                 ftr_rsp++;
5673
5674         if (ftr_rsp) {
5675                 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
5676                                 "0379 Feature Mismatch Data: x%08x %08x "
5677                                 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
5678                                 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
5679                                 phba->cfg_enable_npiv, phba->max_vpi);
5680                 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
5681                         phba->cfg_enable_bg = 0;
5682                 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
5683                         phba->cfg_enable_npiv = 0;
5684         }
5685
5686         /* These SLI3 features are assumed in SLI4 */
5687         spin_lock_irq(&phba->hbalock);
5688         phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
5689         spin_unlock_irq(&phba->hbalock);
5690
5691         /*
5692          * Allocate all resources (xri,rpi,vpi,vfi) now.  Subsequent
5693          * calls depends on these resources to complete port setup.
5694          */
5695         rc = lpfc_sli4_alloc_resource_identifiers(phba);
5696         if (rc) {
5697                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5698                                 "2920 Failed to alloc Resource IDs "
5699                                 "rc = x%x\n", rc);
5700                 goto out_free_mbox;
5701         }
5702
5703         /* Read the port's service parameters. */
5704         rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
5705         if (rc) {
5706                 phba->link_state = LPFC_HBA_ERROR;
5707                 rc = -ENOMEM;
5708                 goto out_free_mbox;
5709         }
5710
5711         mboxq->vport = vport;
5712         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5713         mp = (struct lpfc_dmabuf *) mboxq->context1;
5714         if (rc == MBX_SUCCESS) {
5715                 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
5716                 rc = 0;
5717         }
5718
5719         /*
5720          * This memory was allocated by the lpfc_read_sparam routine. Release
5721          * it to the mbuf pool.
5722          */
5723         lpfc_mbuf_free(phba, mp->virt, mp->phys);
5724         kfree(mp);
5725         mboxq->context1 = NULL;
5726         if (unlikely(rc)) {
5727                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5728                                 "0382 READ_SPARAM command failed "
5729                                 "status %d, mbxStatus x%x\n",
5730                                 rc, bf_get(lpfc_mqe_status, mqe));
5731                 phba->link_state = LPFC_HBA_ERROR;
5732                 rc = -EIO;
5733                 goto out_free_mbox;
5734         }
5735
5736         lpfc_update_vport_wwn(vport);
5737
5738         /* Update the fc_host data structures with new wwn. */
5739         fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
5740         fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
5741
5742         /* Register SGL pool to the device using non-embedded mailbox command */
5743         if (!phba->sli4_hba.extents_in_use) {
5744                 rc = lpfc_sli4_post_els_sgl_list(phba);
5745                 if (unlikely(rc)) {
5746                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5747                                         "0582 Error %d during els sgl post "
5748                                         "operation\n", rc);
5749                         rc = -ENODEV;
5750                         goto out_free_mbox;
5751                 }
5752         } else {
5753                 rc = lpfc_sli4_post_els_sgl_list_ext(phba);
5754                 if (unlikely(rc)) {
5755                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5756                                         "2560 Error %d during els sgl post "
5757                                         "operation\n", rc);
5758                         rc = -ENODEV;
5759                         goto out_free_mbox;
5760                 }
5761         }
5762
5763         /* Register SCSI SGL pool to the device */
5764         rc = lpfc_sli4_repost_scsi_sgl_list(phba);
5765         if (unlikely(rc)) {
5766                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5767                                 "0383 Error %d during scsi sgl post "
5768                                 "operation\n", rc);
5769                 /* Some Scsi buffers were moved to the abort scsi list */
5770                 /* A pci function reset will repost them */
5771                 rc = -ENODEV;
5772                 goto out_free_mbox;
5773         }
5774
5775         /* Post the rpi header region to the device. */
5776         rc = lpfc_sli4_post_all_rpi_hdrs(phba);
5777         if (unlikely(rc)) {
5778                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5779                                 "0393 Error %d during rpi post operation\n",
5780                                 rc);
5781                 rc = -ENODEV;
5782                 goto out_free_mbox;
5783         }
5784
5785         /* Set up all the queues to the device */
5786         rc = lpfc_sli4_queue_setup(phba);
5787         if (unlikely(rc)) {
5788                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5789                                 "0381 Error %d during queue setup.\n ", rc);
5790                 goto out_stop_timers;
5791         }
5792
5793         /* Arm the CQs and then EQs on device */
5794         lpfc_sli4_arm_cqeq_intr(phba);
5795
5796         /* Indicate device interrupt mode */
5797         phba->sli4_hba.intr_enable = 1;
5798
5799         /* Allow asynchronous mailbox command to go through */
5800         spin_lock_irq(&phba->hbalock);
5801         phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
5802         spin_unlock_irq(&phba->hbalock);
5803
5804         /* Post receive buffers to the device */
5805         lpfc_sli4_rb_setup(phba);
5806
5807         /* Reset HBA FCF states after HBA reset */
5808         phba->fcf.fcf_flag = 0;
5809         phba->fcf.current_rec.flag = 0;
5810
5811         /* Start the ELS watchdog timer */
5812         mod_timer(&vport->els_tmofunc,
5813                   jiffies + HZ * (phba->fc_ratov * 2));
5814
5815         /* Start heart beat timer */
5816         mod_timer(&phba->hb_tmofunc,
5817                   jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
5818         phba->hb_outstanding = 0;
5819         phba->last_completion_time = jiffies;
5820
5821         /* Start error attention (ERATT) polling timer */
5822         mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
5823
5824         /* Enable PCIe device Advanced Error Reporting (AER) if configured */
5825         if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
5826                 rc = pci_enable_pcie_error_reporting(phba->pcidev);
5827                 if (!rc) {
5828                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5829                                         "2829 This device supports "
5830                                         "Advanced Error Reporting (AER)\n");
5831                         spin_lock_irq(&phba->hbalock);
5832                         phba->hba_flag |= HBA_AER_ENABLED;
5833                         spin_unlock_irq(&phba->hbalock);
5834                 } else {
5835                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5836                                         "2830 This device does not support "
5837                                         "Advanced Error Reporting (AER)\n");
5838                         phba->cfg_aer_support = 0;
5839                 }
5840         }
5841
5842         if (!(phba->hba_flag & HBA_FCOE_MODE)) {
5843                 /*
5844                  * The FC Port needs to register FCFI (index 0)
5845                  */
5846                 lpfc_reg_fcfi(phba, mboxq);
5847                 mboxq->vport = phba->pport;
5848                 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5849                 if (rc != MBX_SUCCESS)
5850                         goto out_unset_queue;
5851                 rc = 0;
5852                 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
5853                                         &mboxq->u.mqe.un.reg_fcfi);
5854         }
5855         /*
5856          * The port is ready, set the host's link state to LINK_DOWN
5857          * in preparation for link interrupts.
5858          */
5859         spin_lock_irq(&phba->hbalock);
5860         phba->link_state = LPFC_LINK_DOWN;
5861         spin_unlock_irq(&phba->hbalock);
5862         if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK)
5863                 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
5864 out_unset_queue:
5865         /* Unset all the queues set up in this routine when error out */
5866         if (rc)
5867                 lpfc_sli4_queue_unset(phba);
5868 out_stop_timers:
5869         if (rc)
5870                 lpfc_stop_hba_timers(phba);
5871 out_free_mbox:
5872         mempool_free(mboxq, phba->mbox_mem_pool);
5873         return rc;
5874 }
5875
5876 /**
5877  * lpfc_mbox_timeout - Timeout call back function for mbox timer
5878  * @ptr: context object - pointer to hba structure.
5879  *
5880  * This is the callback function for mailbox timer. The mailbox
5881  * timer is armed when a new mailbox command is issued and the timer
5882  * is deleted when the mailbox complete. The function is called by
5883  * the kernel timer code when a mailbox does not complete within
5884  * expected time. This function wakes up the worker thread to
5885  * process the mailbox timeout and returns. All the processing is
5886  * done by the worker thread function lpfc_mbox_timeout_handler.
5887  **/
5888 void
5889 lpfc_mbox_timeout(unsigned long ptr)
5890 {
5891         struct lpfc_hba  *phba = (struct lpfc_hba *) ptr;
5892         unsigned long iflag;
5893         uint32_t tmo_posted;
5894
5895         spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
5896         tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
5897         if (!tmo_posted)
5898                 phba->pport->work_port_events |= WORKER_MBOX_TMO;
5899         spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
5900
5901         if (!tmo_posted)
5902                 lpfc_worker_wake_up(phba);
5903         return;
5904 }
5905
5906
5907 /**
5908  * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
5909  * @phba: Pointer to HBA context object.
5910  *
5911  * This function is called from worker thread when a mailbox command times out.
5912  * The caller is not required to hold any locks. This function will reset the
5913  * HBA and recover all the pending commands.
5914  **/
5915 void
5916 lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
5917 {
5918         LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
5919         MAILBOX_t *mb = &pmbox->u.mb;
5920         struct lpfc_sli *psli = &phba->sli;
5921         struct lpfc_sli_ring *pring;
5922
5923         /* Check the pmbox pointer first.  There is a race condition
5924          * between the mbox timeout handler getting executed in the
5925          * worklist and the mailbox actually completing. When this
5926          * race condition occurs, the mbox_active will be NULL.
5927          */
5928         spin_lock_irq(&phba->hbalock);
5929         if (pmbox == NULL) {
5930                 lpfc_printf_log(phba, KERN_WARNING,
5931                                 LOG_MBOX | LOG_SLI,
5932                                 "0353 Active Mailbox cleared - mailbox timeout "
5933                                 "exiting\n");
5934                 spin_unlock_irq(&phba->hbalock);
5935                 return;
5936         }
5937
5938         /* Mbox cmd <mbxCommand> timeout */
5939         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5940                         "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
5941                         mb->mbxCommand,
5942                         phba->pport->port_state,
5943                         phba->sli.sli_flag,
5944                         phba->sli.mbox_active);
5945         spin_unlock_irq(&phba->hbalock);
5946
5947         /* Setting state unknown so lpfc_sli_abort_iocb_ring
5948          * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
5949          * it to fail all outstanding SCSI IO.
5950          */
5951         spin_lock_irq(&phba->pport->work_port_lock);
5952         phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
5953         spin_unlock_irq(&phba->pport->work_port_lock);
5954         spin_lock_irq(&phba->hbalock);
5955         phba->link_state = LPFC_LINK_UNKNOWN;
5956         psli->sli_flag &= ~LPFC_SLI_ACTIVE;
5957         spin_unlock_irq(&phba->hbalock);
5958
5959         pring = &psli->ring[psli->fcp_ring];
5960         lpfc_sli_abort_iocb_ring(phba, pring);
5961
5962         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5963                         "0345 Resetting board due to mailbox timeout\n");
5964
5965         /* Reset the HBA device */
5966         lpfc_reset_hba(phba);
5967 }
5968
5969 /**
5970  * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
5971  * @phba: Pointer to HBA context object.
5972  * @pmbox: Pointer to mailbox object.
5973  * @flag: Flag indicating how the mailbox need to be processed.
5974  *
5975  * This function is called by discovery code and HBA management code
5976  * to submit a mailbox command to firmware with SLI-3 interface spec. This
5977  * function gets the hbalock to protect the data structures.
5978  * The mailbox command can be submitted in polling mode, in which case
5979  * this function will wait in a polling loop for the completion of the
5980  * mailbox.
5981  * If the mailbox is submitted in no_wait mode (not polling) the
5982  * function will submit the command and returns immediately without waiting
5983  * for the mailbox completion. The no_wait is supported only when HBA
5984  * is in SLI2/SLI3 mode - interrupts are enabled.
5985  * The SLI interface allows only one mailbox pending at a time. If the
5986  * mailbox is issued in polling mode and there is already a mailbox
5987  * pending, then the function will return an error. If the mailbox is issued
5988  * in NO_WAIT mode and there is a mailbox pending already, the function
5989  * will return MBX_BUSY after queuing the mailbox into mailbox queue.
5990  * The sli layer owns the mailbox object until the completion of mailbox
5991  * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
5992  * return codes the caller owns the mailbox command after the return of
5993  * the function.
5994  **/
5995 static int
5996 lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
5997                        uint32_t flag)
5998 {
5999         MAILBOX_t *mb;
6000         struct lpfc_sli *psli = &phba->sli;
6001         uint32_t status, evtctr;
6002         uint32_t ha_copy, hc_copy;
6003         int i;
6004         unsigned long timeout;
6005         unsigned long drvr_flag = 0;
6006         uint32_t word0, ldata;
6007         void __iomem *to_slim;
6008         int processing_queue = 0;
6009
6010         spin_lock_irqsave(&phba->hbalock, drvr_flag);
6011         if (!pmbox) {
6012                 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
6013                 /* processing mbox queue from intr_handler */
6014                 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
6015                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6016                         return MBX_SUCCESS;
6017                 }
6018                 processing_queue = 1;
6019                 pmbox = lpfc_mbox_get(phba);
6020                 if (!pmbox) {
6021                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6022                         return MBX_SUCCESS;
6023                 }
6024         }
6025
6026         if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
6027                 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
6028                 if(!pmbox->vport) {
6029                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6030                         lpfc_printf_log(phba, KERN_ERR,
6031                                         LOG_MBOX | LOG_VPORT,
6032                                         "1806 Mbox x%x failed. No vport\n",
6033                                         pmbox->u.mb.mbxCommand);
6034                         dump_stack();
6035                         goto out_not_finished;
6036                 }
6037         }
6038
6039         /* If the PCI channel is in offline state, do not post mbox. */
6040         if (unlikely(pci_channel_offline(phba->pcidev))) {
6041                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6042                 goto out_not_finished;
6043         }
6044
6045         /* If HBA has a deferred error attention, fail the iocb. */
6046         if (unlikely(phba->hba_flag & DEFER_ERATT)) {
6047                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6048                 goto out_not_finished;
6049         }
6050
6051         psli = &phba->sli;
6052
6053         mb = &pmbox->u.mb;
6054         status = MBX_SUCCESS;
6055
6056         if (phba->link_state == LPFC_HBA_ERROR) {
6057                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6058
6059                 /* Mbox command <mbxCommand> cannot issue */
6060                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6061                                 "(%d):0311 Mailbox command x%x cannot "
6062                                 "issue Data: x%x x%x\n",
6063                                 pmbox->vport ? pmbox->vport->vpi : 0,
6064                                 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
6065                 goto out_not_finished;
6066         }
6067
6068         if (mb->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
6069                 if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
6070                         !(hc_copy & HC_MBINT_ENA)) {
6071                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6072                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6073                                 "(%d):2528 Mailbox command x%x cannot "
6074                                 "issue Data: x%x x%x\n",
6075                                 pmbox->vport ? pmbox->vport->vpi : 0,
6076                                 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
6077                         goto out_not_finished;
6078                 }
6079         }
6080
6081         if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
6082                 /* Polling for a mbox command when another one is already active
6083                  * is not allowed in SLI. Also, the driver must have established
6084                  * SLI2 mode to queue and process multiple mbox commands.
6085                  */
6086
6087                 if (flag & MBX_POLL) {
6088                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6089
6090                         /* Mbox command <mbxCommand> cannot issue */
6091                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6092                                         "(%d):2529 Mailbox command x%x "
6093                                         "cannot issue Data: x%x x%x\n",
6094                                         pmbox->vport ? pmbox->vport->vpi : 0,
6095                                         pmbox->u.mb.mbxCommand,
6096                                         psli->sli_flag, flag);
6097                         goto out_not_finished;
6098                 }
6099
6100                 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
6101                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6102                         /* Mbox command <mbxCommand> cannot issue */
6103                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6104                                         "(%d):2530 Mailbox command x%x "
6105                                         "cannot issue Data: x%x x%x\n",
6106                                         pmbox->vport ? pmbox->vport->vpi : 0,
6107                                         pmbox->u.mb.mbxCommand,
6108                                         psli->sli_flag, flag);
6109                         goto out_not_finished;
6110                 }
6111
6112                 /* Another mailbox command is still being processed, queue this
6113                  * command to be processed later.
6114                  */
6115                 lpfc_mbox_put(phba, pmbox);
6116
6117                 /* Mbox cmd issue - BUSY */
6118                 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6119                                 "(%d):0308 Mbox cmd issue - BUSY Data: "
6120                                 "x%x x%x x%x x%x\n",
6121                                 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
6122                                 mb->mbxCommand, phba->pport->port_state,
6123                                 psli->sli_flag, flag);
6124
6125                 psli->slistat.mbox_busy++;
6126                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6127
6128                 if (pmbox->vport) {
6129                         lpfc_debugfs_disc_trc(pmbox->vport,
6130                                 LPFC_DISC_TRC_MBOX_VPORT,
6131                                 "MBOX Bsy vport:  cmd:x%x mb:x%x x%x",
6132                                 (uint32_t)mb->mbxCommand,
6133                                 mb->un.varWords[0], mb->un.varWords[1]);
6134                 }
6135                 else {
6136                         lpfc_debugfs_disc_trc(phba->pport,
6137                                 LPFC_DISC_TRC_MBOX,
6138                                 "MBOX Bsy:        cmd:x%x mb:x%x x%x",
6139                                 (uint32_t)mb->mbxCommand,
6140                                 mb->un.varWords[0], mb->un.varWords[1]);
6141                 }
6142
6143                 return MBX_BUSY;
6144         }
6145
6146         psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
6147
6148         /* If we are not polling, we MUST be in SLI2 mode */
6149         if (flag != MBX_POLL) {
6150                 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
6151                     (mb->mbxCommand != MBX_KILL_BOARD)) {
6152                         psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
6153                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6154                         /* Mbox command <mbxCommand> cannot issue */
6155                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6156                                         "(%d):2531 Mailbox command x%x "
6157                                         "cannot issue Data: x%x x%x\n",
6158                                         pmbox->vport ? pmbox->vport->vpi : 0,
6159                                         pmbox->u.mb.mbxCommand,
6160                                         psli->sli_flag, flag);
6161                         goto out_not_finished;
6162                 }
6163                 /* timeout active mbox command */
6164                 mod_timer(&psli->mbox_tmo, (jiffies +
6165                                (HZ * lpfc_mbox_tmo_val(phba, mb->mbxCommand))));
6166         }
6167
6168         /* Mailbox cmd <cmd> issue */
6169         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6170                         "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
6171                         "x%x\n",
6172                         pmbox->vport ? pmbox->vport->vpi : 0,
6173                         mb->mbxCommand, phba->pport->port_state,
6174                         psli->sli_flag, flag);
6175
6176         if (mb->mbxCommand != MBX_HEARTBEAT) {
6177                 if (pmbox->vport) {
6178                         lpfc_debugfs_disc_trc(pmbox->vport,
6179                                 LPFC_DISC_TRC_MBOX_VPORT,
6180                                 "MBOX Send vport: cmd:x%x mb:x%x x%x",
6181                                 (uint32_t)mb->mbxCommand,
6182                                 mb->un.varWords[0], mb->un.varWords[1]);
6183                 }
6184                 else {
6185                         lpfc_debugfs_disc_trc(phba->pport,
6186                                 LPFC_DISC_TRC_MBOX,
6187                                 "MBOX Send:       cmd:x%x mb:x%x x%x",
6188                                 (uint32_t)mb->mbxCommand,
6189                                 mb->un.varWords[0], mb->un.varWords[1]);
6190                 }
6191         }
6192
6193         psli->slistat.mbox_cmd++;
6194         evtctr = psli->slistat.mbox_event;
6195
6196         /* next set own bit for the adapter and copy over command word */
6197         mb->mbxOwner = OWN_CHIP;
6198
6199         if (psli->sli_flag & LPFC_SLI_ACTIVE) {
6200                 /* Populate mbox extension offset word. */
6201                 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
6202                         *(((uint32_t *)mb) + pmbox->mbox_offset_word)
6203                                 = (uint8_t *)phba->mbox_ext
6204                                   - (uint8_t *)phba->mbox;
6205                 }
6206
6207                 /* Copy the mailbox extension data */
6208                 if (pmbox->in_ext_byte_len && pmbox->context2) {
6209                         lpfc_sli_pcimem_bcopy(pmbox->context2,
6210                                 (uint8_t *)phba->mbox_ext,
6211                                 pmbox->in_ext_byte_len);
6212                 }
6213                 /* Copy command data to host SLIM area */
6214                 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
6215         } else {
6216                 /* Populate mbox extension offset word. */
6217                 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
6218                         *(((uint32_t *)mb) + pmbox->mbox_offset_word)
6219                                 = MAILBOX_HBA_EXT_OFFSET;
6220
6221                 /* Copy the mailbox extension data */
6222                 if (pmbox->in_ext_byte_len && pmbox->context2) {
6223                         lpfc_memcpy_to_slim(phba->MBslimaddr +
6224                                 MAILBOX_HBA_EXT_OFFSET,
6225                                 pmbox->context2, pmbox->in_ext_byte_len);
6226
6227                 }
6228                 if (mb->mbxCommand == MBX_CONFIG_PORT) {
6229                         /* copy command data into host mbox for cmpl */
6230                         lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
6231                 }
6232
6233                 /* First copy mbox command data to HBA SLIM, skip past first
6234                    word */
6235                 to_slim = phba->MBslimaddr + sizeof (uint32_t);
6236                 lpfc_memcpy_to_slim(to_slim, &mb->un.varWords[0],
6237                             MAILBOX_CMD_SIZE - sizeof (uint32_t));
6238
6239                 /* Next copy over first word, with mbxOwner set */
6240                 ldata = *((uint32_t *)mb);
6241                 to_slim = phba->MBslimaddr;
6242                 writel(ldata, to_slim);
6243                 readl(to_slim); /* flush */
6244
6245                 if (mb->mbxCommand == MBX_CONFIG_PORT) {
6246                         /* switch over to host mailbox */
6247                         psli->sli_flag |= LPFC_SLI_ACTIVE;
6248                 }
6249         }
6250
6251         wmb();
6252
6253         switch (flag) {
6254         case MBX_NOWAIT:
6255                 /* Set up reference to mailbox command */
6256                 psli->mbox_active = pmbox;
6257                 /* Interrupt board to do it */
6258                 writel(CA_MBATT, phba->CAregaddr);
6259                 readl(phba->CAregaddr); /* flush */
6260                 /* Don't wait for it to finish, just return */
6261                 break;
6262
6263         case MBX_POLL:
6264                 /* Set up null reference to mailbox command */
6265                 psli->mbox_active = NULL;
6266                 /* Interrupt board to do it */
6267                 writel(CA_MBATT, phba->CAregaddr);
6268                 readl(phba->CAregaddr); /* flush */
6269
6270                 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
6271                         /* First read mbox status word */
6272                         word0 = *((uint32_t *)phba->mbox);
6273                         word0 = le32_to_cpu(word0);
6274                 } else {
6275                         /* First read mbox status word */
6276                         if (lpfc_readl(phba->MBslimaddr, &word0)) {
6277                                 spin_unlock_irqrestore(&phba->hbalock,
6278                                                        drvr_flag);
6279                                 goto out_not_finished;
6280                         }
6281                 }
6282
6283                 /* Read the HBA Host Attention Register */
6284                 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
6285                         spin_unlock_irqrestore(&phba->hbalock,
6286                                                        drvr_flag);
6287                         goto out_not_finished;
6288                 }
6289                 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
6290                                                              mb->mbxCommand) *
6291                                            1000) + jiffies;
6292                 i = 0;
6293                 /* Wait for command to complete */
6294                 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
6295                        (!(ha_copy & HA_MBATT) &&
6296                         (phba->link_state > LPFC_WARM_START))) {
6297                         if (time_after(jiffies, timeout)) {
6298                                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
6299                                 spin_unlock_irqrestore(&phba->hbalock,
6300                                                        drvr_flag);
6301                                 goto out_not_finished;
6302                         }
6303
6304                         /* Check if we took a mbox interrupt while we were
6305                            polling */
6306                         if (((word0 & OWN_CHIP) != OWN_CHIP)
6307                             && (evtctr != psli->slistat.mbox_event))
6308                                 break;
6309
6310                         if (i++ > 10) {
6311                                 spin_unlock_irqrestore(&phba->hbalock,
6312                                                        drvr_flag);
6313                                 msleep(1);
6314                                 spin_lock_irqsave(&phba->hbalock, drvr_flag);
6315                         }
6316
6317                         if (psli->sli_flag & LPFC_SLI_ACTIVE) {
6318                                 /* First copy command data */
6319                                 word0 = *((uint32_t *)phba->mbox);
6320                                 word0 = le32_to_cpu(word0);
6321                                 if (mb->mbxCommand == MBX_CONFIG_PORT) {
6322                                         MAILBOX_t *slimmb;
6323                                         uint32_t slimword0;
6324                                         /* Check real SLIM for any errors */
6325                                         slimword0 = readl(phba->MBslimaddr);
6326                                         slimmb = (MAILBOX_t *) & slimword0;
6327                                         if (((slimword0 & OWN_CHIP) != OWN_CHIP)
6328                                             && slimmb->mbxStatus) {
6329                                                 psli->sli_flag &=
6330                                                     ~LPFC_SLI_ACTIVE;
6331                                                 word0 = slimword0;
6332                                         }
6333                                 }
6334                         } else {
6335                                 /* First copy command data */
6336                                 word0 = readl(phba->MBslimaddr);
6337                         }
6338                         /* Read the HBA Host Attention Register */
6339                         if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
6340                                 spin_unlock_irqrestore(&phba->hbalock,
6341                                                        drvr_flag);
6342                                 goto out_not_finished;
6343                         }
6344                 }
6345
6346                 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
6347                         /* copy results back to user */
6348                         lpfc_sli_pcimem_bcopy(phba->mbox, mb, MAILBOX_CMD_SIZE);
6349                         /* Copy the mailbox extension data */
6350                         if (pmbox->out_ext_byte_len && pmbox->context2) {
6351                                 lpfc_sli_pcimem_bcopy(phba->mbox_ext,
6352                                                       pmbox->context2,
6353                                                       pmbox->out_ext_byte_len);
6354                         }
6355                 } else {
6356                         /* First copy command data */
6357                         lpfc_memcpy_from_slim(mb, phba->MBslimaddr,
6358                                                         MAILBOX_CMD_SIZE);
6359                         /* Copy the mailbox extension data */
6360                         if (pmbox->out_ext_byte_len && pmbox->context2) {
6361                                 lpfc_memcpy_from_slim(pmbox->context2,
6362                                         phba->MBslimaddr +
6363                                         MAILBOX_HBA_EXT_OFFSET,
6364                                         pmbox->out_ext_byte_len);
6365                         }
6366                 }
6367
6368                 writel(HA_MBATT, phba->HAregaddr);
6369                 readl(phba->HAregaddr); /* flush */
6370
6371                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
6372                 status = mb->mbxStatus;
6373         }
6374
6375         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6376         return status;
6377
6378 out_not_finished:
6379         if (processing_queue) {
6380                 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
6381                 lpfc_mbox_cmpl_put(phba, pmbox);
6382         }
6383         return MBX_NOT_FINISHED;
6384 }
6385
6386 /**
6387  * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
6388  * @phba: Pointer to HBA context object.
6389  *
6390  * The function blocks the posting of SLI4 asynchronous mailbox commands from
6391  * the driver internal pending mailbox queue. It will then try to wait out the
6392  * possible outstanding mailbox command before return.
6393  *
6394  * Returns:
6395  *      0 - the outstanding mailbox command completed; otherwise, the wait for
6396  *      the outstanding mailbox command timed out.
6397  **/
6398 static int
6399 lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
6400 {
6401         struct lpfc_sli *psli = &phba->sli;
6402         uint8_t actcmd = MBX_HEARTBEAT;
6403         int rc = 0;
6404         unsigned long timeout;
6405
6406         /* Mark the asynchronous mailbox command posting as blocked */
6407         spin_lock_irq(&phba->hbalock);
6408         psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
6409         if (phba->sli.mbox_active)
6410                 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
6411         spin_unlock_irq(&phba->hbalock);
6412         /* Determine how long we might wait for the active mailbox
6413          * command to be gracefully completed by firmware.
6414          */
6415         timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, actcmd) * 1000) +
6416                                    jiffies;
6417         /* Wait for the outstnading mailbox command to complete */
6418         while (phba->sli.mbox_active) {
6419                 /* Check active mailbox complete status every 2ms */
6420                 msleep(2);
6421                 if (time_after(jiffies, timeout)) {
6422                         /* Timeout, marked the outstanding cmd not complete */
6423                         rc = 1;
6424                         break;
6425                 }
6426         }
6427
6428         /* Can not cleanly block async mailbox command, fails it */
6429         if (rc) {
6430                 spin_lock_irq(&phba->hbalock);
6431                 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
6432                 spin_unlock_irq(&phba->hbalock);
6433         }
6434         return rc;
6435 }
6436
6437 /**
6438  * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
6439  * @phba: Pointer to HBA context object.
6440  *
6441  * The function unblocks and resume posting of SLI4 asynchronous mailbox
6442  * commands from the driver internal pending mailbox queue. It makes sure
6443  * that there is no outstanding mailbox command before resuming posting
6444  * asynchronous mailbox commands. If, for any reason, there is outstanding
6445  * mailbox command, it will try to wait it out before resuming asynchronous
6446  * mailbox command posting.
6447  **/
6448 static void
6449 lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
6450 {
6451         struct lpfc_sli *psli = &phba->sli;
6452
6453         spin_lock_irq(&phba->hbalock);
6454         if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
6455                 /* Asynchronous mailbox posting is not blocked, do nothing */
6456                 spin_unlock_irq(&phba->hbalock);
6457                 return;
6458         }
6459
6460         /* Outstanding synchronous mailbox command is guaranteed to be done,
6461          * successful or timeout, after timing-out the outstanding mailbox
6462          * command shall always be removed, so just unblock posting async
6463          * mailbox command and resume
6464          */
6465         psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
6466         spin_unlock_irq(&phba->hbalock);
6467
6468         /* wake up worker thread to post asynchronlous mailbox command */
6469         lpfc_worker_wake_up(phba);
6470 }
6471
6472 /**
6473  * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
6474  * @phba: Pointer to HBA context object.
6475  * @mboxq: Pointer to mailbox object.
6476  *
6477  * The function posts a mailbox to the port.  The mailbox is expected
6478  * to be comletely filled in and ready for the port to operate on it.
6479  * This routine executes a synchronous completion operation on the
6480  * mailbox by polling for its completion.
6481  *
6482  * The caller must not be holding any locks when calling this routine.
6483  *
6484  * Returns:
6485  *      MBX_SUCCESS - mailbox posted successfully
6486  *      Any of the MBX error values.
6487  **/
6488 static int
6489 lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
6490 {
6491         int rc = MBX_SUCCESS;
6492         unsigned long iflag;
6493         uint32_t db_ready;
6494         uint32_t mcqe_status;
6495         uint32_t mbx_cmnd;
6496         unsigned long timeout;
6497         struct lpfc_sli *psli = &phba->sli;
6498         struct lpfc_mqe *mb = &mboxq->u.mqe;
6499         struct lpfc_bmbx_create *mbox_rgn;
6500         struct dma_address *dma_address;
6501         struct lpfc_register bmbx_reg;
6502
6503         /*
6504          * Only one mailbox can be active to the bootstrap mailbox region
6505          * at a time and there is no queueing provided.
6506          */
6507         spin_lock_irqsave(&phba->hbalock, iflag);
6508         if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
6509                 spin_unlock_irqrestore(&phba->hbalock, iflag);
6510                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6511                                 "(%d):2532 Mailbox command x%x (x%x) "
6512                                 "cannot issue Data: x%x x%x\n",
6513                                 mboxq->vport ? mboxq->vport->vpi : 0,
6514                                 mboxq->u.mb.mbxCommand,
6515                                 lpfc_sli4_mbox_opcode_get(phba, mboxq),
6516                                 psli->sli_flag, MBX_POLL);
6517                 return MBXERR_ERROR;
6518         }
6519         /* The server grabs the token and owns it until release */
6520         psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
6521         phba->sli.mbox_active = mboxq;
6522         spin_unlock_irqrestore(&phba->hbalock, iflag);
6523
6524         /*
6525          * Initialize the bootstrap memory region to avoid stale data areas
6526          * in the mailbox post.  Then copy the caller's mailbox contents to
6527          * the bmbx mailbox region.
6528          */
6529         mbx_cmnd = bf_get(lpfc_mqe_command, mb);
6530         memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
6531         lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
6532                               sizeof(struct lpfc_mqe));
6533
6534         /* Post the high mailbox dma address to the port and wait for ready. */
6535         dma_address = &phba->sli4_hba.bmbx.dma_address;
6536         writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
6537
6538         timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mbx_cmnd)
6539                                    * 1000) + jiffies;
6540         do {
6541                 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
6542                 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
6543                 if (!db_ready)
6544                         msleep(2);
6545
6546                 if (time_after(jiffies, timeout)) {
6547                         rc = MBXERR_ERROR;
6548                         goto exit;
6549                 }
6550         } while (!db_ready);
6551
6552         /* Post the low mailbox dma address to the port. */
6553         writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
6554         timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mbx_cmnd)
6555                                    * 1000) + jiffies;
6556         do {
6557                 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
6558                 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
6559                 if (!db_ready)
6560                         msleep(2);
6561
6562                 if (time_after(jiffies, timeout)) {
6563                         rc = MBXERR_ERROR;
6564                         goto exit;
6565                 }
6566         } while (!db_ready);
6567
6568         /*
6569          * Read the CQ to ensure the mailbox has completed.
6570          * If so, update the mailbox status so that the upper layers
6571          * can complete the request normally.
6572          */
6573         lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
6574                               sizeof(struct lpfc_mqe));
6575         mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
6576         lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
6577                               sizeof(struct lpfc_mcqe));
6578         mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
6579         /*
6580          * When the CQE status indicates a failure and the mailbox status
6581          * indicates success then copy the CQE status into the mailbox status
6582          * (and prefix it with x4000).
6583          */
6584         if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
6585                 if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
6586                         bf_set(lpfc_mqe_status, mb,
6587                                (LPFC_MBX_ERROR_RANGE | mcqe_status));
6588                 rc = MBXERR_ERROR;
6589         } else
6590                 lpfc_sli4_swap_str(phba, mboxq);
6591
6592         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6593                         "(%d):0356 Mailbox cmd x%x (x%x) Status x%x "
6594                         "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
6595                         " x%x x%x CQ: x%x x%x x%x x%x\n",
6596                         mboxq->vport ? mboxq->vport->vpi : 0,
6597                         mbx_cmnd, lpfc_sli4_mbox_opcode_get(phba, mboxq),
6598                         bf_get(lpfc_mqe_status, mb),
6599                         mb->un.mb_words[0], mb->un.mb_words[1],
6600                         mb->un.mb_words[2], mb->un.mb_words[3],
6601                         mb->un.mb_words[4], mb->un.mb_words[5],
6602                         mb->un.mb_words[6], mb->un.mb_words[7],
6603                         mb->un.mb_words[8], mb->un.mb_words[9],
6604                         mb->un.mb_words[10], mb->un.mb_words[11],
6605                         mb->un.mb_words[12], mboxq->mcqe.word0,
6606                         mboxq->mcqe.mcqe_tag0,  mboxq->mcqe.mcqe_tag1,
6607                         mboxq->mcqe.trailer);
6608 exit:
6609         /* We are holding the token, no needed for lock when release */
6610         spin_lock_irqsave(&phba->hbalock, iflag);
6611         psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
6612         phba->sli.mbox_active = NULL;
6613         spin_unlock_irqrestore(&phba->hbalock, iflag);
6614         return rc;
6615 }
6616
6617 /**
6618  * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
6619  * @phba: Pointer to HBA context object.
6620  * @pmbox: Pointer to mailbox object.
6621  * @flag: Flag indicating how the mailbox need to be processed.
6622  *
6623  * This function is called by discovery code and HBA management code to submit
6624  * a mailbox command to firmware with SLI-4 interface spec.
6625  *
6626  * Return codes the caller owns the mailbox command after the return of the
6627  * function.
6628  **/
6629 static int
6630 lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
6631                        uint32_t flag)
6632 {
6633         struct lpfc_sli *psli = &phba->sli;
6634         unsigned long iflags;
6635         int rc;
6636
6637         rc = lpfc_mbox_dev_check(phba);
6638         if (unlikely(rc)) {
6639                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6640                                 "(%d):2544 Mailbox command x%x (x%x) "
6641                                 "cannot issue Data: x%x x%x\n",
6642                                 mboxq->vport ? mboxq->vport->vpi : 0,
6643                                 mboxq->u.mb.mbxCommand,
6644                                 lpfc_sli4_mbox_opcode_get(phba, mboxq),
6645                                 psli->sli_flag, flag);
6646                 goto out_not_finished;
6647         }
6648
6649         /* Detect polling mode and jump to a handler */
6650         if (!phba->sli4_hba.intr_enable) {
6651                 if (flag == MBX_POLL)
6652                         rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
6653                 else
6654                         rc = -EIO;
6655                 if (rc != MBX_SUCCESS)
6656                         lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6657                                         "(%d):2541 Mailbox command x%x "
6658                                         "(x%x) cannot issue Data: x%x x%x\n",
6659                                         mboxq->vport ? mboxq->vport->vpi : 0,
6660                                         mboxq->u.mb.mbxCommand,
6661                                         lpfc_sli4_mbox_opcode_get(phba, mboxq),
6662                                         psli->sli_flag, flag);
6663                 return rc;
6664         } else if (flag == MBX_POLL) {
6665                 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6666                                 "(%d):2542 Try to issue mailbox command "
6667                                 "x%x (x%x) synchronously ahead of async"
6668                                 "mailbox command queue: x%x x%x\n",
6669                                 mboxq->vport ? mboxq->vport->vpi : 0,
6670                                 mboxq->u.mb.mbxCommand,
6671                                 lpfc_sli4_mbox_opcode_get(phba, mboxq),
6672                                 psli->sli_flag, flag);
6673                 /* Try to block the asynchronous mailbox posting */
6674                 rc = lpfc_sli4_async_mbox_block(phba);
6675                 if (!rc) {
6676                         /* Successfully blocked, now issue sync mbox cmd */
6677                         rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
6678                         if (rc != MBX_SUCCESS)
6679                                 lpfc_printf_log(phba, KERN_ERR,
6680                                                 LOG_MBOX | LOG_SLI,
6681                                                 "(%d):2597 Mailbox command "
6682                                                 "x%x (x%x) cannot issue "
6683                                                 "Data: x%x x%x\n",
6684                                                 mboxq->vport ?
6685                                                 mboxq->vport->vpi : 0,
6686                                                 mboxq->u.mb.mbxCommand,
6687                                                 lpfc_sli4_mbox_opcode_get(phba,
6688                                                                 mboxq),
6689                                                 psli->sli_flag, flag);
6690                         /* Unblock the async mailbox posting afterward */
6691                         lpfc_sli4_async_mbox_unblock(phba);
6692                 }
6693                 return rc;
6694         }
6695
6696         /* Now, interrupt mode asynchrous mailbox command */
6697         rc = lpfc_mbox_cmd_check(phba, mboxq);
6698         if (rc) {
6699                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6700                                 "(%d):2543 Mailbox command x%x (x%x) "
6701                                 "cannot issue Data: x%x x%x\n",
6702                                 mboxq->vport ? mboxq->vport->vpi : 0,
6703                                 mboxq->u.mb.mbxCommand,
6704                                 lpfc_sli4_mbox_opcode_get(phba, mboxq),
6705                                 psli->sli_flag, flag);
6706                 goto out_not_finished;
6707         }
6708
6709         /* Put the mailbox command to the driver internal FIFO */
6710         psli->slistat.mbox_busy++;
6711         spin_lock_irqsave(&phba->hbalock, iflags);
6712         lpfc_mbox_put(phba, mboxq);
6713         spin_unlock_irqrestore(&phba->hbalock, iflags);
6714         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6715                         "(%d):0354 Mbox cmd issue - Enqueue Data: "
6716                         "x%x (x%x) x%x x%x x%x\n",
6717                         mboxq->vport ? mboxq->vport->vpi : 0xffffff,
6718                         bf_get(lpfc_mqe_command, &mboxq->u.mqe),
6719                         lpfc_sli4_mbox_opcode_get(phba, mboxq),
6720                         phba->pport->port_state,
6721                         psli->sli_flag, MBX_NOWAIT);
6722         /* Wake up worker thread to transport mailbox command from head */
6723         lpfc_worker_wake_up(phba);
6724
6725         return MBX_BUSY;
6726
6727 out_not_finished:
6728         return MBX_NOT_FINISHED;
6729 }
6730
6731 /**
6732  * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
6733  * @phba: Pointer to HBA context object.
6734  *
6735  * This function is called by worker thread to send a mailbox command to
6736  * SLI4 HBA firmware.
6737  *
6738  **/
6739 int
6740 lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
6741 {
6742         struct lpfc_sli *psli = &phba->sli;
6743         LPFC_MBOXQ_t *mboxq;
6744         int rc = MBX_SUCCESS;
6745         unsigned long iflags;
6746         struct lpfc_mqe *mqe;
6747         uint32_t mbx_cmnd;
6748
6749         /* Check interrupt mode before post async mailbox command */
6750         if (unlikely(!phba->sli4_hba.intr_enable))
6751                 return MBX_NOT_FINISHED;
6752
6753         /* Check for mailbox command service token */
6754         spin_lock_irqsave(&phba->hbalock, iflags);
6755         if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
6756                 spin_unlock_irqrestore(&phba->hbalock, iflags);
6757                 return MBX_NOT_FINISHED;
6758         }
6759         if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
6760                 spin_unlock_irqrestore(&phba->hbalock, iflags);
6761                 return MBX_NOT_FINISHED;
6762         }
6763         if (unlikely(phba->sli.mbox_active)) {
6764                 spin_unlock_irqrestore(&phba->hbalock, iflags);
6765                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6766                                 "0384 There is pending active mailbox cmd\n");
6767                 return MBX_NOT_FINISHED;
6768         }
6769         /* Take the mailbox command service token */
6770         psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
6771
6772         /* Get the next mailbox command from head of queue */
6773         mboxq = lpfc_mbox_get(phba);
6774
6775         /* If no more mailbox command waiting for post, we're done */
6776         if (!mboxq) {
6777                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
6778                 spin_unlock_irqrestore(&phba->hbalock, iflags);
6779                 return MBX_SUCCESS;
6780         }
6781         phba->sli.mbox_active = mboxq;
6782         spin_unlock_irqrestore(&phba->hbalock, iflags);
6783
6784         /* Check device readiness for posting mailbox command */
6785         rc = lpfc_mbox_dev_check(phba);
6786         if (unlikely(rc))
6787                 /* Driver clean routine will clean up pending mailbox */
6788                 goto out_not_finished;
6789
6790         /* Prepare the mbox command to be posted */
6791         mqe = &mboxq->u.mqe;
6792         mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
6793
6794         /* Start timer for the mbox_tmo and log some mailbox post messages */
6795         mod_timer(&psli->mbox_tmo, (jiffies +
6796                   (HZ * lpfc_mbox_tmo_val(phba, mbx_cmnd))));
6797
6798         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6799                         "(%d):0355 Mailbox cmd x%x (x%x) issue Data: "
6800                         "x%x x%x\n",
6801                         mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
6802                         lpfc_sli4_mbox_opcode_get(phba, mboxq),
6803                         phba->pport->port_state, psli->sli_flag);
6804
6805         if (mbx_cmnd != MBX_HEARTBEAT) {
6806                 if (mboxq->vport) {
6807                         lpfc_debugfs_disc_trc(mboxq->vport,
6808                                 LPFC_DISC_TRC_MBOX_VPORT,
6809                                 "MBOX Send vport: cmd:x%x mb:x%x x%x",
6810                                 mbx_cmnd, mqe->un.mb_words[0],
6811                                 mqe->un.mb_words[1]);
6812                 } else {
6813                         lpfc_debugfs_disc_trc(phba->pport,
6814                                 LPFC_DISC_TRC_MBOX,
6815                                 "MBOX Send: cmd:x%x mb:x%x x%x",
6816                                 mbx_cmnd, mqe->un.mb_words[0],
6817                                 mqe->un.mb_words[1]);
6818                 }
6819         }
6820         psli->slistat.mbox_cmd++;
6821
6822         /* Post the mailbox command to the port */
6823         rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
6824         if (rc != MBX_SUCCESS) {
6825                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6826                                 "(%d):2533 Mailbox command x%x (x%x) "
6827                                 "cannot issue Data: x%x x%x\n",
6828                                 mboxq->vport ? mboxq->vport->vpi : 0,
6829                                 mboxq->u.mb.mbxCommand,
6830                                 lpfc_sli4_mbox_opcode_get(phba, mboxq),
6831                                 psli->sli_flag, MBX_NOWAIT);
6832                 goto out_not_finished;
6833         }
6834
6835         return rc;
6836
6837 out_not_finished:
6838         spin_lock_irqsave(&phba->hbalock, iflags);
6839         mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
6840         __lpfc_mbox_cmpl_put(phba, mboxq);
6841         /* Release the token */
6842         psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
6843         phba->sli.mbox_active = NULL;
6844         spin_unlock_irqrestore(&phba->hbalock, iflags);
6845
6846         return MBX_NOT_FINISHED;
6847 }
6848
6849 /**
6850  * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
6851  * @phba: Pointer to HBA context object.
6852  * @pmbox: Pointer to mailbox object.
6853  * @flag: Flag indicating how the mailbox need to be processed.
6854  *
6855  * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
6856  * the API jump table function pointer from the lpfc_hba struct.
6857  *
6858  * Return codes the caller owns the mailbox command after the return of the
6859  * function.
6860  **/
6861 int
6862 lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
6863 {
6864         return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
6865 }
6866
6867 /**
6868  * lpfc_mbox_api_table_setup - Set up mbox api function jump table
6869  * @phba: The hba struct for which this call is being executed.
6870  * @dev_grp: The HBA PCI-Device group number.
6871  *
6872  * This routine sets up the mbox interface API function jump table in @phba
6873  * struct.
6874  * Returns: 0 - success, -ENODEV - failure.
6875  **/
6876 int
6877 lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
6878 {
6879
6880         switch (dev_grp) {
6881         case LPFC_PCI_DEV_LP:
6882                 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
6883                 phba->lpfc_sli_handle_slow_ring_event =
6884                                 lpfc_sli_handle_slow_ring_event_s3;
6885                 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
6886                 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
6887                 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
6888                 break;
6889         case LPFC_PCI_DEV_OC:
6890                 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
6891                 phba->lpfc_sli_handle_slow_ring_event =
6892                                 lpfc_sli_handle_slow_ring_event_s4;
6893                 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
6894                 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
6895                 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
6896                 break;
6897         default:
6898                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6899                                 "1420 Invalid HBA PCI-device group: 0x%x\n",
6900                                 dev_grp);
6901                 return -ENODEV;
6902                 break;
6903         }
6904         return 0;
6905 }
6906
6907 /**
6908  * __lpfc_sli_ringtx_put - Add an iocb to the txq
6909  * @phba: Pointer to HBA context object.
6910  * @pring: Pointer to driver SLI ring object.
6911  * @piocb: Pointer to address of newly added command iocb.
6912  *
6913  * This function is called with hbalock held to add a command
6914  * iocb to the txq when SLI layer cannot submit the command iocb
6915  * to the ring.
6916  **/
6917 void
6918 __lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
6919                     struct lpfc_iocbq *piocb)
6920 {
6921         /* Insert the caller's iocb in the txq tail for later processing. */
6922         list_add_tail(&piocb->list, &pring->txq);
6923         pring->txq_cnt++;
6924 }
6925
6926 /**
6927  * lpfc_sli_next_iocb - Get the next iocb in the txq
6928  * @phba: Pointer to HBA context object.
6929  * @pring: Pointer to driver SLI ring object.
6930  * @piocb: Pointer to address of newly added command iocb.
6931  *
6932  * This function is called with hbalock held before a new
6933  * iocb is submitted to the firmware. This function checks
6934  * txq to flush the iocbs in txq to Firmware before
6935  * submitting new iocbs to the Firmware.
6936  * If there are iocbs in the txq which need to be submitted
6937  * to firmware, lpfc_sli_next_iocb returns the first element
6938  * of the txq after dequeuing it from txq.
6939  * If there is no iocb in the txq then the function will return
6940  * *piocb and *piocb is set to NULL. Caller needs to check
6941  * *piocb to find if there are more commands in the txq.
6942  **/
6943 static struct lpfc_iocbq *
6944 lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
6945                    struct lpfc_iocbq **piocb)
6946 {
6947         struct lpfc_iocbq * nextiocb;
6948
6949         nextiocb = lpfc_sli_ringtx_get(phba, pring);
6950         if (!nextiocb) {
6951                 nextiocb = *piocb;
6952                 *piocb = NULL;
6953         }
6954
6955         return nextiocb;
6956 }
6957
6958 /**
6959  * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
6960  * @phba: Pointer to HBA context object.
6961  * @ring_number: SLI ring number to issue iocb on.
6962  * @piocb: Pointer to command iocb.
6963  * @flag: Flag indicating if this command can be put into txq.
6964  *
6965  * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
6966  * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
6967  * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
6968  * flag is turned on, the function returns IOCB_ERROR. When the link is down,
6969  * this function allows only iocbs for posting buffers. This function finds
6970  * next available slot in the command ring and posts the command to the
6971  * available slot and writes the port attention register to request HBA start
6972  * processing new iocb. If there is no slot available in the ring and
6973  * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
6974  * the function returns IOCB_BUSY.
6975  *
6976  * This function is called with hbalock held. The function will return success
6977  * after it successfully submit the iocb to firmware or after adding to the
6978  * txq.
6979  **/
6980 static int
6981 __lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
6982                     struct lpfc_iocbq *piocb, uint32_t flag)
6983 {
6984         struct lpfc_iocbq *nextiocb;
6985         IOCB_t *iocb;
6986         struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
6987
6988         if (piocb->iocb_cmpl && (!piocb->vport) &&
6989            (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
6990            (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
6991                 lpfc_printf_log(phba, KERN_ERR,
6992                                 LOG_SLI | LOG_VPORT,
6993                                 "1807 IOCB x%x failed. No vport\n",
6994                                 piocb->iocb.ulpCommand);
6995                 dump_stack();
6996                 return IOCB_ERROR;
6997         }
6998
6999
7000         /* If the PCI channel is in offline state, do not post iocbs. */
7001         if (unlikely(pci_channel_offline(phba->pcidev)))
7002                 return IOCB_ERROR;
7003
7004         /* If HBA has a deferred error attention, fail the iocb. */
7005         if (unlikely(phba->hba_flag & DEFER_ERATT))
7006                 return IOCB_ERROR;
7007
7008         /*
7009          * We should never get an IOCB if we are in a < LINK_DOWN state
7010          */
7011         if (unlikely(phba->link_state < LPFC_LINK_DOWN))
7012                 return IOCB_ERROR;
7013
7014         /*
7015          * Check to see if we are blocking IOCB processing because of a
7016          * outstanding event.
7017          */
7018         if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
7019                 goto iocb_busy;
7020
7021         if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
7022                 /*
7023                  * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
7024                  * can be issued if the link is not up.
7025                  */
7026                 switch (piocb->iocb.ulpCommand) {
7027                 case CMD_GEN_REQUEST64_CR:
7028                 case CMD_GEN_REQUEST64_CX:
7029                         if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
7030                                 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
7031                                         FC_RCTL_DD_UNSOL_CMD) ||
7032                                 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
7033                                         MENLO_TRANSPORT_TYPE))
7034
7035                                 goto iocb_busy;
7036                         break;
7037                 case CMD_QUE_RING_BUF_CN:
7038                 case CMD_QUE_RING_BUF64_CN:
7039                         /*
7040                          * For IOCBs, like QUE_RING_BUF, that have no rsp ring
7041                          * completion, iocb_cmpl MUST be 0.
7042                          */
7043                         if (piocb->iocb_cmpl)
7044                                 piocb->iocb_cmpl = NULL;
7045                         /*FALLTHROUGH*/
7046                 case CMD_CREATE_XRI_CR:
7047                 case CMD_CLOSE_XRI_CN:
7048                 case CMD_CLOSE_XRI_CX:
7049                         break;
7050                 default:
7051                         goto iocb_busy;
7052                 }
7053
7054         /*
7055          * For FCP commands, we must be in a state where we can process link
7056          * attention events.
7057          */
7058         } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
7059                             !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
7060                 goto iocb_busy;
7061         }
7062
7063         while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
7064                (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
7065                 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
7066
7067         if (iocb)
7068                 lpfc_sli_update_ring(phba, pring);
7069         else
7070                 lpfc_sli_update_full_ring(phba, pring);
7071
7072         if (!piocb)
7073                 return IOCB_SUCCESS;
7074
7075         goto out_busy;
7076
7077  iocb_busy:
7078         pring->stats.iocb_cmd_delay++;
7079
7080  out_busy:
7081
7082         if (!(flag & SLI_IOCB_RET_IOCB)) {
7083                 __lpfc_sli_ringtx_put(phba, pring, piocb);
7084                 return IOCB_SUCCESS;
7085         }
7086
7087         return IOCB_BUSY;
7088 }
7089
7090 /**
7091  * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
7092  * @phba: Pointer to HBA context object.
7093  * @piocb: Pointer to command iocb.
7094  * @sglq: Pointer to the scatter gather queue object.
7095  *
7096  * This routine converts the bpl or bde that is in the IOCB
7097  * to a sgl list for the sli4 hardware. The physical address
7098  * of the bpl/bde is converted back to a virtual address.
7099  * If the IOCB contains a BPL then the list of BDE's is
7100  * converted to sli4_sge's. If the IOCB contains a single
7101  * BDE then it is converted to a single sli_sge.
7102  * The IOCB is still in cpu endianess so the contents of
7103  * the bpl can be used without byte swapping.
7104  *
7105  * Returns valid XRI = Success, NO_XRI = Failure.
7106 **/
7107 static uint16_t
7108 lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
7109                 struct lpfc_sglq *sglq)
7110 {
7111         uint16_t xritag = NO_XRI;
7112         struct ulp_bde64 *bpl = NULL;
7113         struct ulp_bde64 bde;
7114         struct sli4_sge *sgl  = NULL;
7115         IOCB_t *icmd;
7116         int numBdes = 0;
7117         int i = 0;
7118         uint32_t offset = 0; /* accumulated offset in the sg request list */
7119         int inbound = 0; /* number of sg reply entries inbound from firmware */
7120
7121         if (!piocbq || !sglq)
7122                 return xritag;
7123
7124         sgl  = (struct sli4_sge *)sglq->sgl;
7125         icmd = &piocbq->iocb;
7126         if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
7127                 numBdes = icmd->un.genreq64.bdl.bdeSize /
7128                                 sizeof(struct ulp_bde64);
7129                 /* The addrHigh and addrLow fields within the IOCB
7130                  * have not been byteswapped yet so there is no
7131                  * need to swap them back.
7132                  */
7133                 bpl  = (struct ulp_bde64 *)
7134                         ((struct lpfc_dmabuf *)piocbq->context3)->virt;
7135
7136                 if (!bpl)
7137                         return xritag;
7138
7139                 for (i = 0; i < numBdes; i++) {
7140                         /* Should already be byte swapped. */
7141                         sgl->addr_hi = bpl->addrHigh;
7142                         sgl->addr_lo = bpl->addrLow;
7143
7144                         sgl->word2 = le32_to_cpu(sgl->word2);
7145                         if ((i+1) == numBdes)
7146                                 bf_set(lpfc_sli4_sge_last, sgl, 1);
7147                         else
7148                                 bf_set(lpfc_sli4_sge_last, sgl, 0);
7149                         /* swap the size field back to the cpu so we
7150                          * can assign it to the sgl.
7151                          */
7152                         bde.tus.w = le32_to_cpu(bpl->tus.w);
7153                         sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
7154                         /* The offsets in the sgl need to be accumulated
7155                          * separately for the request and reply lists.
7156                          * The request is always first, the reply follows.
7157                          */
7158                         if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
7159                                 /* add up the reply sg entries */
7160                                 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
7161                                         inbound++;
7162                                 /* first inbound? reset the offset */
7163                                 if (inbound == 1)
7164                                         offset = 0;
7165                                 bf_set(lpfc_sli4_sge_offset, sgl, offset);
7166                                 offset += bde.tus.f.bdeSize;
7167                         }
7168                         sgl->word2 = cpu_to_le32(sgl->word2);
7169                         bpl++;
7170                         sgl++;
7171                 }
7172         } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
7173                         /* The addrHigh and addrLow fields of the BDE have not
7174                          * been byteswapped yet so they need to be swapped
7175                          * before putting them in the sgl.
7176                          */
7177                         sgl->addr_hi =
7178                                 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
7179                         sgl->addr_lo =
7180                                 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
7181                         sgl->word2 = le32_to_cpu(sgl->word2);
7182                         bf_set(lpfc_sli4_sge_last, sgl, 1);
7183                         sgl->word2 = cpu_to_le32(sgl->word2);
7184                         sgl->sge_len =
7185                                 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
7186         }
7187         return sglq->sli4_xritag;
7188 }
7189
7190 /**
7191  * lpfc_sli4_scmd_to_wqidx_distr - scsi command to SLI4 WQ index distribution
7192  * @phba: Pointer to HBA context object.
7193  *
7194  * This routine performs a roundrobin SCSI command to SLI4 FCP WQ index
7195  * distribution.  This is called by __lpfc_sli_issue_iocb_s4() with the hbalock
7196  * held.
7197  *
7198  * Return: index into SLI4 fast-path FCP queue index.
7199  **/
7200 static uint32_t
7201 lpfc_sli4_scmd_to_wqidx_distr(struct lpfc_hba *phba)
7202 {
7203         ++phba->fcp_qidx;
7204         if (phba->fcp_qidx >= phba->cfg_fcp_wq_count)
7205                 phba->fcp_qidx = 0;
7206
7207         return phba->fcp_qidx;
7208 }
7209
7210 /**
7211  * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
7212  * @phba: Pointer to HBA context object.
7213  * @piocb: Pointer to command iocb.
7214  * @wqe: Pointer to the work queue entry.
7215  *
7216  * This routine converts the iocb command to its Work Queue Entry
7217  * equivalent. The wqe pointer should not have any fields set when
7218  * this routine is called because it will memcpy over them.
7219  * This routine does not set the CQ_ID or the WQEC bits in the
7220  * wqe.
7221  *
7222  * Returns: 0 = Success, IOCB_ERROR = Failure.
7223  **/
7224 static int
7225 lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
7226                 union lpfc_wqe *wqe)
7227 {
7228         uint32_t xmit_len = 0, total_len = 0;
7229         uint8_t ct = 0;
7230         uint32_t fip;
7231         uint32_t abort_tag;
7232         uint8_t command_type = ELS_COMMAND_NON_FIP;
7233         uint8_t cmnd;
7234         uint16_t xritag;
7235         uint16_t abrt_iotag;
7236         struct lpfc_iocbq *abrtiocbq;
7237         struct ulp_bde64 *bpl = NULL;
7238         uint32_t els_id = LPFC_ELS_ID_DEFAULT;
7239         int numBdes, i;
7240         struct ulp_bde64 bde;
7241         struct lpfc_nodelist *ndlp;
7242
7243         fip = phba->hba_flag & HBA_FIP_SUPPORT;
7244         /* The fcp commands will set command type */
7245         if (iocbq->iocb_flag &  LPFC_IO_FCP)
7246                 command_type = FCP_COMMAND;
7247         else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
7248                 command_type = ELS_COMMAND_FIP;
7249         else
7250                 command_type = ELS_COMMAND_NON_FIP;
7251
7252         /* Some of the fields are in the right position already */
7253         memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
7254         abort_tag = (uint32_t) iocbq->iotag;
7255         xritag = iocbq->sli4_xritag;
7256         wqe->generic.wqe_com.word7 = 0; /* The ct field has moved so reset */
7257         /* words0-2 bpl convert bde */
7258         if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
7259                 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
7260                                 sizeof(struct ulp_bde64);
7261                 bpl  = (struct ulp_bde64 *)
7262                         ((struct lpfc_dmabuf *)iocbq->context3)->virt;
7263                 if (!bpl)
7264                         return IOCB_ERROR;
7265
7266                 /* Should already be byte swapped. */
7267                 wqe->generic.bde.addrHigh =  le32_to_cpu(bpl->addrHigh);
7268                 wqe->generic.bde.addrLow =  le32_to_cpu(bpl->addrLow);
7269                 /* swap the size field back to the cpu so we
7270                  * can assign it to the sgl.
7271                  */
7272                 wqe->generic.bde.tus.w  = le32_to_cpu(bpl->tus.w);
7273                 xmit_len = wqe->generic.bde.tus.f.bdeSize;
7274                 total_len = 0;
7275                 for (i = 0; i < numBdes; i++) {
7276                         bde.tus.w  = le32_to_cpu(bpl[i].tus.w);
7277                         total_len += bde.tus.f.bdeSize;
7278                 }
7279         } else
7280                 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
7281
7282         iocbq->iocb.ulpIoTag = iocbq->iotag;
7283         cmnd = iocbq->iocb.ulpCommand;
7284
7285         switch (iocbq->iocb.ulpCommand) {
7286         case CMD_ELS_REQUEST64_CR:
7287                 ndlp = (struct lpfc_nodelist *)iocbq->context1;
7288                 if (!iocbq->iocb.ulpLe) {
7289                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7290                                 "2007 Only Limited Edition cmd Format"
7291                                 " supported 0x%x\n",
7292                                 iocbq->iocb.ulpCommand);
7293                         return IOCB_ERROR;
7294                 }
7295                 wqe->els_req.payload_len = xmit_len;
7296                 /* Els_reguest64 has a TMO */
7297                 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
7298                         iocbq->iocb.ulpTimeout);
7299                 /* Need a VF for word 4 set the vf bit*/
7300                 bf_set(els_req64_vf, &wqe->els_req, 0);
7301                 /* And a VFID for word 12 */
7302                 bf_set(els_req64_vfid, &wqe->els_req, 0);
7303                 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
7304                 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
7305                        iocbq->iocb.ulpContext);
7306                 bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
7307                 bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
7308                 /* CCP CCPE PV PRI in word10 were set in the memcpy */
7309                 if (command_type == ELS_COMMAND_FIP) {
7310                         els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
7311                                         >> LPFC_FIP_ELS_ID_SHIFT);
7312                 }
7313                 bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
7314                        phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
7315                 bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
7316                 bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
7317                 bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
7318                 bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
7319                 bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
7320                 bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
7321         break;
7322         case CMD_XMIT_SEQUENCE64_CX:
7323                 bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
7324                        iocbq->iocb.un.ulpWord[3]);
7325                 bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
7326                        iocbq->iocb.ulpContext);
7327                 /* The entire sequence is transmitted for this IOCB */
7328                 xmit_len = total_len;
7329                 cmnd = CMD_XMIT_SEQUENCE64_CR;
7330         case CMD_XMIT_SEQUENCE64_CR:
7331                 /* word3 iocb=io_tag32 wqe=reserved */
7332                 wqe->xmit_sequence.rsvd3 = 0;
7333                 /* word4 relative_offset memcpy */
7334                 /* word5 r_ctl/df_ctl memcpy */
7335                 bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
7336                 bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
7337                 bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
7338                        LPFC_WQE_IOD_WRITE);
7339                 bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
7340                        LPFC_WQE_LENLOC_WORD12);
7341                 bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
7342                 wqe->xmit_sequence.xmit_len = xmit_len;
7343                 command_type = OTHER_COMMAND;
7344         break;
7345         case CMD_XMIT_BCAST64_CN:
7346                 /* word3 iocb=iotag32 wqe=seq_payload_len */
7347                 wqe->xmit_bcast64.seq_payload_len = xmit_len;
7348                 /* word4 iocb=rsvd wqe=rsvd */
7349                 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
7350                 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
7351                 bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
7352                         ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
7353                 bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
7354                 bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
7355                 bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
7356                        LPFC_WQE_LENLOC_WORD3);
7357                 bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
7358         break;
7359         case CMD_FCP_IWRITE64_CR:
7360                 command_type = FCP_COMMAND_DATA_OUT;
7361                 /* word3 iocb=iotag wqe=payload_offset_len */
7362                 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
7363                 wqe->fcp_iwrite.payload_offset_len =
7364                         xmit_len + sizeof(struct fcp_rsp);
7365                 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
7366                 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
7367                 bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
7368                        iocbq->iocb.ulpFCP2Rcvy);
7369                 bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
7370                 /* Always open the exchange */
7371                 bf_set(wqe_xc, &wqe->fcp_iwrite.wqe_com, 0);
7372                 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
7373                 bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
7374                 bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
7375                        LPFC_WQE_LENLOC_WORD4);
7376                 bf_set(wqe_ebde_cnt, &wqe->fcp_iwrite.wqe_com, 0);
7377                 bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
7378         break;
7379         case CMD_FCP_IREAD64_CR:
7380                 /* word3 iocb=iotag wqe=payload_offset_len */
7381                 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
7382                 wqe->fcp_iread.payload_offset_len =
7383                         xmit_len + sizeof(struct fcp_rsp);
7384                 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
7385                 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
7386                 bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
7387                        iocbq->iocb.ulpFCP2Rcvy);
7388                 bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
7389                 /* Always open the exchange */
7390                 bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
7391                 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
7392                 bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
7393                 bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
7394                        LPFC_WQE_LENLOC_WORD4);
7395                 bf_set(wqe_ebde_cnt, &wqe->fcp_iread.wqe_com, 0);
7396                 bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
7397         break;
7398         case CMD_FCP_ICMND64_CR:
7399                 /* word3 iocb=IO_TAG wqe=reserved */
7400                 wqe->fcp_icmd.rsrvd3 = 0;
7401                 bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
7402                 /* Always open the exchange */
7403                 bf_set(wqe_xc, &wqe->fcp_icmd.wqe_com, 0);
7404                 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
7405                 bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
7406                 bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
7407                 bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
7408                        LPFC_WQE_LENLOC_NONE);
7409                 bf_set(wqe_ebde_cnt, &wqe->fcp_icmd.wqe_com, 0);
7410         break;
7411         case CMD_GEN_REQUEST64_CR:
7412                 /* For this command calculate the xmit length of the
7413                  * request bde.
7414                  */
7415                 xmit_len = 0;
7416                 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
7417                         sizeof(struct ulp_bde64);
7418                 for (i = 0; i < numBdes; i++) {
7419                         bde.tus.w = le32_to_cpu(bpl[i].tus.w);
7420                         if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
7421                                 break;
7422                         xmit_len += bde.tus.f.bdeSize;
7423                 }
7424                 /* word3 iocb=IO_TAG wqe=request_payload_len */
7425                 wqe->gen_req.request_payload_len = xmit_len;
7426                 /* word4 iocb=parameter wqe=relative_offset memcpy */
7427                 /* word5 [rctl, type, df_ctl, la] copied in memcpy */
7428                 /* word6 context tag copied in memcpy */
7429                 if (iocbq->iocb.ulpCt_h  || iocbq->iocb.ulpCt_l) {
7430                         ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
7431                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7432                                 "2015 Invalid CT %x command 0x%x\n",
7433                                 ct, iocbq->iocb.ulpCommand);
7434                         return IOCB_ERROR;
7435                 }
7436                 bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
7437                 bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
7438                 bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
7439                 bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
7440                 bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
7441                 bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
7442                 bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
7443                 bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
7444                 command_type = OTHER_COMMAND;
7445         break;
7446         case CMD_XMIT_ELS_RSP64_CX:
7447                 ndlp = (struct lpfc_nodelist *)iocbq->context1;
7448                 /* words0-2 BDE memcpy */
7449                 /* word3 iocb=iotag32 wqe=response_payload_len */
7450                 wqe->xmit_els_rsp.response_payload_len = xmit_len;
7451                 /* word4 iocb=did wge=rsvd. */
7452                 wqe->xmit_els_rsp.rsvd4 = 0;
7453                 /* word5 iocb=rsvd wge=did */
7454                 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
7455                          iocbq->iocb.un.elsreq64.remoteID);
7456                 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
7457                        ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
7458                 bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
7459                 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
7460                        iocbq->iocb.ulpContext);
7461                 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
7462                         bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
7463                                phba->vpi_ids[iocbq->vport->vpi]);
7464                 bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
7465                 bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
7466                 bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
7467                 bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
7468                        LPFC_WQE_LENLOC_WORD3);
7469                 bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
7470                 bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
7471                        phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
7472                 command_type = OTHER_COMMAND;
7473         break;
7474         case CMD_CLOSE_XRI_CN:
7475         case CMD_ABORT_XRI_CN:
7476         case CMD_ABORT_XRI_CX:
7477                 /* words 0-2 memcpy should be 0 rserved */
7478                 /* port will send abts */
7479                 abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
7480                 if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
7481                         abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
7482                         fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
7483                 } else
7484                         fip = 0;
7485
7486                 if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
7487                         /*
7488                          * The link is down, or the command was ELS_FIP
7489                          * so the fw does not need to send abts
7490                          * on the wire.
7491                          */
7492                         bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
7493                 else
7494                         bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
7495                 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
7496                 /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
7497                 wqe->abort_cmd.rsrvd5 = 0;
7498                 bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
7499                         ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
7500                 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
7501                 /*
7502                  * The abort handler will send us CMD_ABORT_XRI_CN or
7503                  * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
7504                  */
7505                 bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
7506                 bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
7507                 bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
7508                        LPFC_WQE_LENLOC_NONE);
7509                 cmnd = CMD_ABORT_XRI_CX;
7510                 command_type = OTHER_COMMAND;
7511                 xritag = 0;
7512         break;
7513         case CMD_XMIT_BLS_RSP64_CX:
7514                 /* As BLS ABTS RSP WQE is very different from other WQEs,
7515                  * we re-construct this WQE here based on information in
7516                  * iocbq from scratch.
7517                  */
7518                 memset(wqe, 0, sizeof(union lpfc_wqe));
7519                 /* OX_ID is invariable to who sent ABTS to CT exchange */
7520                 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
7521                        bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
7522                 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
7523                     LPFC_ABTS_UNSOL_INT) {
7524                         /* ABTS sent by initiator to CT exchange, the
7525                          * RX_ID field will be filled with the newly
7526                          * allocated responder XRI.
7527                          */
7528                         bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
7529                                iocbq->sli4_xritag);
7530                 } else {
7531                         /* ABTS sent by responder to CT exchange, the
7532                          * RX_ID field will be filled with the responder
7533                          * RX_ID from ABTS.
7534                          */
7535                         bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
7536                                bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
7537                 }
7538                 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
7539                 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
7540                 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
7541                        iocbq->iocb.ulpContext);
7542                 bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
7543                 bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
7544                        LPFC_WQE_LENLOC_NONE);
7545                 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
7546                 command_type = OTHER_COMMAND;
7547                 if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
7548                         bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
7549                                bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
7550                         bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
7551                                bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
7552                         bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
7553                                bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
7554                 }
7555
7556         break;
7557         case CMD_XRI_ABORTED_CX:
7558         case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
7559         case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
7560         case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
7561         case CMD_FCP_TRSP64_CX: /* Target mode rcv */
7562         case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
7563         default:
7564                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7565                                 "2014 Invalid command 0x%x\n",
7566                                 iocbq->iocb.ulpCommand);
7567                 return IOCB_ERROR;
7568         break;
7569         }
7570
7571         bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
7572         bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
7573         wqe->generic.wqe_com.abort_tag = abort_tag;
7574         bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
7575         bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
7576         bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
7577         bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
7578         return 0;
7579 }
7580
7581 /**
7582  * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
7583  * @phba: Pointer to HBA context object.
7584  * @ring_number: SLI ring number to issue iocb on.
7585  * @piocb: Pointer to command iocb.
7586  * @flag: Flag indicating if this command can be put into txq.
7587  *
7588  * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
7589  * an iocb command to an HBA with SLI-4 interface spec.
7590  *
7591  * This function is called with hbalock held. The function will return success
7592  * after it successfully submit the iocb to firmware or after adding to the
7593  * txq.
7594  **/
7595 static int
7596 __lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
7597                          struct lpfc_iocbq *piocb, uint32_t flag)
7598 {
7599         struct lpfc_sglq *sglq;
7600         union lpfc_wqe wqe;
7601         struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
7602
7603         if (piocb->sli4_xritag == NO_XRI) {
7604                 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
7605                     piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN ||
7606                     piocb->iocb.ulpCommand == CMD_XMIT_BLS_RSP64_CX)
7607                         sglq = NULL;
7608                 else {
7609                         if (pring->txq_cnt) {
7610                                 if (!(flag & SLI_IOCB_RET_IOCB)) {
7611                                         __lpfc_sli_ringtx_put(phba,
7612                                                 pring, piocb);
7613                                         return IOCB_SUCCESS;
7614                                 } else {
7615                                         return IOCB_BUSY;
7616                                 }
7617                         } else {
7618                                 sglq = __lpfc_sli_get_sglq(phba, piocb);
7619                                 if (!sglq) {
7620                                         if (!(flag & SLI_IOCB_RET_IOCB)) {
7621                                                 __lpfc_sli_ringtx_put(phba,
7622                                                                 pring,
7623                                                                 piocb);
7624                                                 return IOCB_SUCCESS;
7625                                         } else
7626                                                 return IOCB_BUSY;
7627                                 }
7628                         }
7629                 }
7630         } else if (piocb->iocb_flag &  LPFC_IO_FCP) {
7631                 /* These IO's already have an XRI and a mapped sgl. */
7632                 sglq = NULL;
7633         } else {
7634                 /*
7635                  * This is a continuation of a commandi,(CX) so this
7636                  * sglq is on the active list
7637                  */
7638                 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_xritag);
7639                 if (!sglq)
7640                         return IOCB_ERROR;
7641         }
7642
7643         if (sglq) {
7644                 piocb->sli4_lxritag = sglq->sli4_lxritag;
7645                 piocb->sli4_xritag = sglq->sli4_xritag;
7646                 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
7647                         return IOCB_ERROR;
7648         }
7649
7650         if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
7651                 return IOCB_ERROR;
7652
7653         if ((piocb->iocb_flag & LPFC_IO_FCP) ||
7654                 (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
7655                 /*
7656                  * For FCP command IOCB, get a new WQ index to distribute
7657                  * WQE across the WQsr. On the other hand, for abort IOCB,
7658                  * it carries the same WQ index to the original command
7659                  * IOCB.
7660                  */
7661                 if (piocb->iocb_flag & LPFC_IO_FCP)
7662                         piocb->fcp_wqidx = lpfc_sli4_scmd_to_wqidx_distr(phba);
7663                 if (lpfc_sli4_wq_put(phba->sli4_hba.fcp_wq[piocb->fcp_wqidx],
7664                                      &wqe))
7665                         return IOCB_ERROR;
7666         } else {
7667                 if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
7668                         return IOCB_ERROR;
7669         }
7670         lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
7671
7672         return 0;
7673 }
7674
7675 /**
7676  * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
7677  *
7678  * This routine wraps the actual lockless version for issusing IOCB function
7679  * pointer from the lpfc_hba struct.
7680  *
7681  * Return codes:
7682  *      IOCB_ERROR - Error
7683  *      IOCB_SUCCESS - Success
7684  *      IOCB_BUSY - Busy
7685  **/
7686 int
7687 __lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
7688                 struct lpfc_iocbq *piocb, uint32_t flag)
7689 {
7690         return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
7691 }
7692
7693 /**
7694  * lpfc_sli_api_table_setup - Set up sli api function jump table
7695  * @phba: The hba struct for which this call is being executed.
7696  * @dev_grp: The HBA PCI-Device group number.
7697  *
7698  * This routine sets up the SLI interface API function jump table in @phba
7699  * struct.
7700  * Returns: 0 - success, -ENODEV - failure.
7701  **/
7702 int
7703 lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
7704 {
7705
7706         switch (dev_grp) {
7707         case LPFC_PCI_DEV_LP:
7708                 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
7709                 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
7710                 break;
7711         case LPFC_PCI_DEV_OC:
7712                 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
7713                 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
7714                 break;
7715         default:
7716                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7717                                 "1419 Invalid HBA PCI-device group: 0x%x\n",
7718                                 dev_grp);
7719                 return -ENODEV;
7720                 break;
7721         }
7722         phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
7723         return 0;
7724 }
7725
7726 /**
7727  * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
7728  * @phba: Pointer to HBA context object.
7729  * @pring: Pointer to driver SLI ring object.
7730  * @piocb: Pointer to command iocb.
7731  * @flag: Flag indicating if this command can be put into txq.
7732  *
7733  * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
7734  * function. This function gets the hbalock and calls
7735  * __lpfc_sli_issue_iocb function and will return the error returned
7736  * by __lpfc_sli_issue_iocb function. This wrapper is used by
7737  * functions which do not hold hbalock.
7738  **/
7739 int
7740 lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
7741                     struct lpfc_iocbq *piocb, uint32_t flag)
7742 {
7743         unsigned long iflags;
7744         int rc;
7745
7746         spin_lock_irqsave(&phba->hbalock, iflags);
7747         rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
7748         spin_unlock_irqrestore(&phba->hbalock, iflags);
7749
7750         return rc;
7751 }
7752
7753 /**
7754  * lpfc_extra_ring_setup - Extra ring setup function
7755  * @phba: Pointer to HBA context object.
7756  *
7757  * This function is called while driver attaches with the
7758  * HBA to setup the extra ring. The extra ring is used
7759  * only when driver needs to support target mode functionality
7760  * or IP over FC functionalities.
7761  *
7762  * This function is called with no lock held.
7763  **/
7764 static int
7765 lpfc_extra_ring_setup( struct lpfc_hba *phba)
7766 {
7767         struct lpfc_sli *psli;
7768         struct lpfc_sli_ring *pring;
7769
7770         psli = &phba->sli;
7771
7772         /* Adjust cmd/rsp ring iocb entries more evenly */
7773
7774         /* Take some away from the FCP ring */
7775         pring = &psli->ring[psli->fcp_ring];
7776         pring->numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
7777         pring->numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
7778         pring->numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
7779         pring->numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
7780
7781         /* and give them to the extra ring */
7782         pring = &psli->ring[psli->extra_ring];
7783
7784         pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
7785         pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
7786         pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
7787         pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
7788
7789         /* Setup default profile for this ring */
7790         pring->iotag_max = 4096;
7791         pring->num_mask = 1;
7792         pring->prt[0].profile = 0;      /* Mask 0 */
7793         pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
7794         pring->prt[0].type = phba->cfg_multi_ring_type;
7795         pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
7796         return 0;
7797 }
7798
7799 /**
7800  * lpfc_sli_async_event_handler - ASYNC iocb handler function
7801  * @phba: Pointer to HBA context object.
7802  * @pring: Pointer to driver SLI ring object.
7803  * @iocbq: Pointer to iocb object.
7804  *
7805  * This function is called by the slow ring event handler
7806  * function when there is an ASYNC event iocb in the ring.
7807  * This function is called with no lock held.
7808  * Currently this function handles only temperature related
7809  * ASYNC events. The function decodes the temperature sensor
7810  * event message and posts events for the management applications.
7811  **/
7812 static void
7813 lpfc_sli_async_event_handler(struct lpfc_hba * phba,
7814         struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
7815 {
7816         IOCB_t *icmd;
7817         uint16_t evt_code;
7818         uint16_t temp;
7819         struct temp_event temp_event_data;
7820         struct Scsi_Host *shost;
7821         uint32_t *iocb_w;
7822
7823         icmd = &iocbq->iocb;
7824         evt_code = icmd->un.asyncstat.evt_code;
7825         temp = icmd->ulpContext;
7826
7827         if ((evt_code != ASYNC_TEMP_WARN) &&
7828                 (evt_code != ASYNC_TEMP_SAFE)) {
7829                 iocb_w = (uint32_t *) icmd;
7830                 lpfc_printf_log(phba,
7831                         KERN_ERR,
7832                         LOG_SLI,
7833                         "0346 Ring %d handler: unexpected ASYNC_STATUS"
7834                         " evt_code 0x%x\n"
7835                         "W0  0x%08x W1  0x%08x W2  0x%08x W3  0x%08x\n"
7836                         "W4  0x%08x W5  0x%08x W6  0x%08x W7  0x%08x\n"
7837                         "W8  0x%08x W9  0x%08x W10 0x%08x W11 0x%08x\n"
7838                         "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
7839                         pring->ringno,
7840                         icmd->un.asyncstat.evt_code,
7841                         iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
7842                         iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
7843                         iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
7844                         iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
7845
7846                 return;
7847         }
7848         temp_event_data.data = (uint32_t)temp;
7849         temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
7850         if (evt_code == ASYNC_TEMP_WARN) {
7851                 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
7852                 lpfc_printf_log(phba,
7853                                 KERN_ERR,
7854                                 LOG_TEMP,
7855                                 "0347 Adapter is very hot, please take "
7856                                 "corrective action. temperature : %d Celsius\n",
7857                                 temp);
7858         }
7859         if (evt_code == ASYNC_TEMP_SAFE) {
7860                 temp_event_data.event_code = LPFC_NORMAL_TEMP;
7861                 lpfc_printf_log(phba,
7862                                 KERN_ERR,
7863                                 LOG_TEMP,
7864                                 "0340 Adapter temperature is OK now. "
7865                                 "temperature : %d Celsius\n",
7866                                 temp);
7867         }
7868
7869         /* Send temperature change event to applications */
7870         shost = lpfc_shost_from_vport(phba->pport);
7871         fc_host_post_vendor_event(shost, fc_get_event_number(),
7872                 sizeof(temp_event_data), (char *) &temp_event_data,
7873                 LPFC_NL_VENDOR_ID);
7874
7875 }
7876
7877
7878 /**
7879  * lpfc_sli_setup - SLI ring setup function
7880  * @phba: Pointer to HBA context object.
7881  *
7882  * lpfc_sli_setup sets up rings of the SLI interface with
7883  * number of iocbs per ring and iotags. This function is
7884  * called while driver attach to the HBA and before the
7885  * interrupts are enabled. So there is no need for locking.
7886  *
7887  * This function always returns 0.
7888  **/
7889 int
7890 lpfc_sli_setup(struct lpfc_hba *phba)
7891 {
7892         int i, totiocbsize = 0;
7893         struct lpfc_sli *psli = &phba->sli;
7894         struct lpfc_sli_ring *pring;
7895
7896         psli->num_rings = MAX_CONFIGURED_RINGS;
7897         psli->sli_flag = 0;
7898         psli->fcp_ring = LPFC_FCP_RING;
7899         psli->next_ring = LPFC_FCP_NEXT_RING;
7900         psli->extra_ring = LPFC_EXTRA_RING;
7901
7902         psli->iocbq_lookup = NULL;
7903         psli->iocbq_lookup_len = 0;
7904         psli->last_iotag = 0;
7905
7906         for (i = 0; i < psli->num_rings; i++) {
7907                 pring = &psli->ring[i];
7908                 switch (i) {
7909                 case LPFC_FCP_RING:     /* ring 0 - FCP */
7910                         /* numCiocb and numRiocb are used in config_port */
7911                         pring->numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
7912                         pring->numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
7913                         pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
7914                         pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
7915                         pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
7916                         pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
7917                         pring->sizeCiocb = (phba->sli_rev == 3) ?
7918                                                         SLI3_IOCB_CMD_SIZE :
7919                                                         SLI2_IOCB_CMD_SIZE;
7920                         pring->sizeRiocb = (phba->sli_rev == 3) ?
7921                                                         SLI3_IOCB_RSP_SIZE :
7922                                                         SLI2_IOCB_RSP_SIZE;
7923                         pring->iotag_ctr = 0;
7924                         pring->iotag_max =
7925                             (phba->cfg_hba_queue_depth * 2);
7926                         pring->fast_iotag = pring->iotag_max;
7927                         pring->num_mask = 0;
7928                         break;
7929                 case LPFC_EXTRA_RING:   /* ring 1 - EXTRA */
7930                         /* numCiocb and numRiocb are used in config_port */
7931                         pring->numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
7932                         pring->numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
7933                         pring->sizeCiocb = (phba->sli_rev == 3) ?
7934                                                         SLI3_IOCB_CMD_SIZE :
7935                                                         SLI2_IOCB_CMD_SIZE;
7936                         pring->sizeRiocb = (phba->sli_rev == 3) ?
7937                                                         SLI3_IOCB_RSP_SIZE :
7938                                                         SLI2_IOCB_RSP_SIZE;
7939                         pring->iotag_max = phba->cfg_hba_queue_depth;
7940                         pring->num_mask = 0;
7941                         break;
7942                 case LPFC_ELS_RING:     /* ring 2 - ELS / CT */
7943                         /* numCiocb and numRiocb are used in config_port */
7944                         pring->numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
7945                         pring->numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
7946                         pring->sizeCiocb = (phba->sli_rev == 3) ?
7947                                                         SLI3_IOCB_CMD_SIZE :
7948                                                         SLI2_IOCB_CMD_SIZE;
7949                         pring->sizeRiocb = (phba->sli_rev == 3) ?
7950                                                         SLI3_IOCB_RSP_SIZE :
7951                                                         SLI2_IOCB_RSP_SIZE;
7952                         pring->fast_iotag = 0;
7953                         pring->iotag_ctr = 0;
7954                         pring->iotag_max = 4096;
7955                         pring->lpfc_sli_rcv_async_status =
7956                                 lpfc_sli_async_event_handler;
7957                         pring->num_mask = LPFC_MAX_RING_MASK;
7958                         pring->prt[0].profile = 0;      /* Mask 0 */
7959                         pring->prt[0].rctl = FC_RCTL_ELS_REQ;
7960                         pring->prt[0].type = FC_TYPE_ELS;
7961                         pring->prt[0].lpfc_sli_rcv_unsol_event =
7962                             lpfc_els_unsol_event;
7963                         pring->prt[1].profile = 0;      /* Mask 1 */
7964                         pring->prt[1].rctl = FC_RCTL_ELS_REP;
7965                         pring->prt[1].type = FC_TYPE_ELS;
7966                         pring->prt[1].lpfc_sli_rcv_unsol_event =
7967                             lpfc_els_unsol_event;
7968                         pring->prt[2].profile = 0;      /* Mask 2 */
7969                         /* NameServer Inquiry */
7970                         pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
7971                         /* NameServer */
7972                         pring->prt[2].type = FC_TYPE_CT;
7973                         pring->prt[2].lpfc_sli_rcv_unsol_event =
7974                             lpfc_ct_unsol_event;
7975                         pring->prt[3].profile = 0;      /* Mask 3 */
7976                         /* NameServer response */
7977                         pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
7978                         /* NameServer */
7979                         pring->prt[3].type = FC_TYPE_CT;
7980                         pring->prt[3].lpfc_sli_rcv_unsol_event =
7981                             lpfc_ct_unsol_event;
7982                         /* abort unsolicited sequence */
7983                         pring->prt[4].profile = 0;      /* Mask 4 */
7984                         pring->prt[4].rctl = FC_RCTL_BA_ABTS;
7985                         pring->prt[4].type = FC_TYPE_BLS;
7986                         pring->prt[4].lpfc_sli_rcv_unsol_event =
7987                             lpfc_sli4_ct_abort_unsol_event;
7988                         break;
7989                 }
7990                 totiocbsize += (pring->numCiocb * pring->sizeCiocb) +
7991                                 (pring->numRiocb * pring->sizeRiocb);
7992         }
7993         if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
7994                 /* Too many cmd / rsp ring entries in SLI2 SLIM */
7995                 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
7996                        "SLI2 SLIM Data: x%x x%lx\n",
7997                        phba->brd_no, totiocbsize,
7998                        (unsigned long) MAX_SLIM_IOCB_SIZE);
7999         }
8000         if (phba->cfg_multi_ring_support == 2)
8001                 lpfc_extra_ring_setup(phba);
8002
8003         return 0;
8004 }
8005
8006 /**
8007  * lpfc_sli_queue_setup - Queue initialization function
8008  * @phba: Pointer to HBA context object.
8009  *
8010  * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
8011  * ring. This function also initializes ring indices of each ring.
8012  * This function is called during the initialization of the SLI
8013  * interface of an HBA.
8014  * This function is called with no lock held and always returns
8015  * 1.
8016  **/
8017 int
8018 lpfc_sli_queue_setup(struct lpfc_hba *phba)
8019 {
8020         struct lpfc_sli *psli;
8021         struct lpfc_sli_ring *pring;
8022         int i;
8023
8024         psli = &phba->sli;
8025         spin_lock_irq(&phba->hbalock);
8026         INIT_LIST_HEAD(&psli->mboxq);
8027         INIT_LIST_HEAD(&psli->mboxq_cmpl);
8028         /* Initialize list headers for txq and txcmplq as double linked lists */
8029         for (i = 0; i < psli->num_rings; i++) {
8030                 pring = &psli->ring[i];
8031                 pring->ringno = i;
8032                 pring->next_cmdidx  = 0;
8033                 pring->local_getidx = 0;
8034                 pring->cmdidx = 0;
8035                 INIT_LIST_HEAD(&pring->txq);
8036                 INIT_LIST_HEAD(&pring->txcmplq);
8037                 INIT_LIST_HEAD(&pring->iocb_continueq);
8038                 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
8039                 INIT_LIST_HEAD(&pring->postbufq);
8040         }
8041         spin_unlock_irq(&phba->hbalock);
8042         return 1;
8043 }
8044
8045 /**
8046  * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
8047  * @phba: Pointer to HBA context object.
8048  *
8049  * This routine flushes the mailbox command subsystem. It will unconditionally
8050  * flush all the mailbox commands in the three possible stages in the mailbox
8051  * command sub-system: pending mailbox command queue; the outstanding mailbox
8052  * command; and completed mailbox command queue. It is caller's responsibility
8053  * to make sure that the driver is in the proper state to flush the mailbox
8054  * command sub-system. Namely, the posting of mailbox commands into the
8055  * pending mailbox command queue from the various clients must be stopped;
8056  * either the HBA is in a state that it will never works on the outstanding
8057  * mailbox command (such as in EEH or ERATT conditions) or the outstanding
8058  * mailbox command has been completed.
8059  **/
8060 static void
8061 lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
8062 {
8063         LIST_HEAD(completions);
8064         struct lpfc_sli *psli = &phba->sli;
8065         LPFC_MBOXQ_t *pmb;
8066         unsigned long iflag;
8067
8068         /* Flush all the mailbox commands in the mbox system */
8069         spin_lock_irqsave(&phba->hbalock, iflag);
8070         /* The pending mailbox command queue */
8071         list_splice_init(&phba->sli.mboxq, &completions);
8072         /* The outstanding active mailbox command */
8073         if (psli->mbox_active) {
8074                 list_add_tail(&psli->mbox_active->list, &completions);
8075                 psli->mbox_active = NULL;
8076                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8077         }
8078         /* The completed mailbox command queue */
8079         list_splice_init(&phba->sli.mboxq_cmpl, &completions);
8080         spin_unlock_irqrestore(&phba->hbalock, iflag);
8081
8082         /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
8083         while (!list_empty(&completions)) {
8084                 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
8085                 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
8086                 if (pmb->mbox_cmpl)
8087                         pmb->mbox_cmpl(phba, pmb);
8088         }
8089 }
8090
8091 /**
8092  * lpfc_sli_host_down - Vport cleanup function
8093  * @vport: Pointer to virtual port object.
8094  *
8095  * lpfc_sli_host_down is called to clean up the resources
8096  * associated with a vport before destroying virtual
8097  * port data structures.
8098  * This function does following operations:
8099  * - Free discovery resources associated with this virtual
8100  *   port.
8101  * - Free iocbs associated with this virtual port in
8102  *   the txq.
8103  * - Send abort for all iocb commands associated with this
8104  *   vport in txcmplq.
8105  *
8106  * This function is called with no lock held and always returns 1.
8107  **/
8108 int
8109 lpfc_sli_host_down(struct lpfc_vport *vport)
8110 {
8111         LIST_HEAD(completions);
8112         struct lpfc_hba *phba = vport->phba;
8113         struct lpfc_sli *psli = &phba->sli;
8114         struct lpfc_sli_ring *pring;
8115         struct lpfc_iocbq *iocb, *next_iocb;
8116         int i;
8117         unsigned long flags = 0;
8118         uint16_t prev_pring_flag;
8119
8120         lpfc_cleanup_discovery_resources(vport);
8121
8122         spin_lock_irqsave(&phba->hbalock, flags);
8123         for (i = 0; i < psli->num_rings; i++) {
8124                 pring = &psli->ring[i];
8125                 prev_pring_flag = pring->flag;
8126                 /* Only slow rings */
8127                 if (pring->ringno == LPFC_ELS_RING) {
8128                         pring->flag |= LPFC_DEFERRED_RING_EVENT;
8129                         /* Set the lpfc data pending flag */
8130                         set_bit(LPFC_DATA_READY, &phba->data_flags);
8131                 }
8132                 /*
8133                  * Error everything on the txq since these iocbs have not been
8134                  * given to the FW yet.
8135                  */
8136                 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
8137                         if (iocb->vport != vport)
8138                                 continue;
8139                         list_move_tail(&iocb->list, &completions);
8140                         pring->txq_cnt--;
8141                 }
8142
8143                 /* Next issue ABTS for everything on the txcmplq */
8144                 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
8145                                                                         list) {
8146                         if (iocb->vport != vport)
8147                                 continue;
8148                         lpfc_sli_issue_abort_iotag(phba, pring, iocb);
8149                 }
8150
8151                 pring->flag = prev_pring_flag;
8152         }
8153
8154         spin_unlock_irqrestore(&phba->hbalock, flags);
8155
8156         /* Cancel all the IOCBs from the completions list */
8157         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
8158                               IOERR_SLI_DOWN);
8159         return 1;
8160 }
8161
8162 /**
8163  * lpfc_sli_hba_down - Resource cleanup function for the HBA
8164  * @phba: Pointer to HBA context object.
8165  *
8166  * This function cleans up all iocb, buffers, mailbox commands
8167  * while shutting down the HBA. This function is called with no
8168  * lock held and always returns 1.
8169  * This function does the following to cleanup driver resources:
8170  * - Free discovery resources for each virtual port
8171  * - Cleanup any pending fabric iocbs
8172  * - Iterate through the iocb txq and free each entry
8173  *   in the list.
8174  * - Free up any buffer posted to the HBA
8175  * - Free mailbox commands in the mailbox queue.
8176  **/
8177 int
8178 lpfc_sli_hba_down(struct lpfc_hba *phba)
8179 {
8180         LIST_HEAD(completions);
8181         struct lpfc_sli *psli = &phba->sli;
8182         struct lpfc_sli_ring *pring;
8183         struct lpfc_dmabuf *buf_ptr;
8184         unsigned long flags = 0;
8185         int i;
8186
8187         /* Shutdown the mailbox command sub-system */
8188         lpfc_sli_mbox_sys_shutdown(phba);
8189
8190         lpfc_hba_down_prep(phba);
8191
8192         lpfc_fabric_abort_hba(phba);
8193
8194         spin_lock_irqsave(&phba->hbalock, flags);
8195         for (i = 0; i < psli->num_rings; i++) {
8196                 pring = &psli->ring[i];
8197                 /* Only slow rings */
8198                 if (pring->ringno == LPFC_ELS_RING) {
8199                         pring->flag |= LPFC_DEFERRED_RING_EVENT;
8200                         /* Set the lpfc data pending flag */
8201                         set_bit(LPFC_DATA_READY, &phba->data_flags);
8202                 }
8203
8204                 /*
8205                  * Error everything on the txq since these iocbs have not been
8206                  * given to the FW yet.
8207                  */
8208                 list_splice_init(&pring->txq, &completions);
8209                 pring->txq_cnt = 0;
8210
8211         }
8212         spin_unlock_irqrestore(&phba->hbalock, flags);
8213
8214         /* Cancel all the IOCBs from the completions list */
8215         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
8216                               IOERR_SLI_DOWN);
8217
8218         spin_lock_irqsave(&phba->hbalock, flags);
8219         list_splice_init(&phba->elsbuf, &completions);
8220         phba->elsbuf_cnt = 0;
8221         phba->elsbuf_prev_cnt = 0;
8222         spin_unlock_irqrestore(&phba->hbalock, flags);
8223
8224         while (!list_empty(&completions)) {
8225                 list_remove_head(&completions, buf_ptr,
8226                         struct lpfc_dmabuf, list);
8227                 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
8228                 kfree(buf_ptr);
8229         }
8230
8231         /* Return any active mbox cmds */
8232         del_timer_sync(&psli->mbox_tmo);
8233
8234         spin_lock_irqsave(&phba->pport->work_port_lock, flags);
8235         phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
8236         spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
8237
8238         return 1;
8239 }
8240
8241 /**
8242  * lpfc_sli_pcimem_bcopy - SLI memory copy function
8243  * @srcp: Source memory pointer.
8244  * @destp: Destination memory pointer.
8245  * @cnt: Number of words required to be copied.
8246  *
8247  * This function is used for copying data between driver memory
8248  * and the SLI memory. This function also changes the endianness
8249  * of each word if native endianness is different from SLI
8250  * endianness. This function can be called with or without
8251  * lock.
8252  **/
8253 void
8254 lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
8255 {
8256         uint32_t *src = srcp;
8257         uint32_t *dest = destp;
8258         uint32_t ldata;
8259         int i;
8260
8261         for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
8262                 ldata = *src;
8263                 ldata = le32_to_cpu(ldata);
8264                 *dest = ldata;
8265                 src++;
8266                 dest++;
8267         }
8268 }
8269
8270
8271 /**
8272  * lpfc_sli_bemem_bcopy - SLI memory copy function
8273  * @srcp: Source memory pointer.
8274  * @destp: Destination memory pointer.
8275  * @cnt: Number of words required to be copied.
8276  *
8277  * This function is used for copying data between a data structure
8278  * with big endian representation to local endianness.
8279  * This function can be called with or without lock.
8280  **/
8281 void
8282 lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
8283 {
8284         uint32_t *src = srcp;
8285         uint32_t *dest = destp;
8286         uint32_t ldata;
8287         int i;
8288
8289         for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
8290                 ldata = *src;
8291                 ldata = be32_to_cpu(ldata);
8292                 *dest = ldata;
8293                 src++;
8294                 dest++;
8295         }
8296 }
8297
8298 /**
8299  * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
8300  * @phba: Pointer to HBA context object.
8301  * @pring: Pointer to driver SLI ring object.
8302  * @mp: Pointer to driver buffer object.
8303  *
8304  * This function is called with no lock held.
8305  * It always return zero after adding the buffer to the postbufq
8306  * buffer list.
8307  **/
8308 int
8309 lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
8310                          struct lpfc_dmabuf *mp)
8311 {
8312         /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
8313            later */
8314         spin_lock_irq(&phba->hbalock);
8315         list_add_tail(&mp->list, &pring->postbufq);
8316         pring->postbufq_cnt++;
8317         spin_unlock_irq(&phba->hbalock);
8318         return 0;
8319 }
8320
8321 /**
8322  * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
8323  * @phba: Pointer to HBA context object.
8324  *
8325  * When HBQ is enabled, buffers are searched based on tags. This function
8326  * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
8327  * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
8328  * does not conflict with tags of buffer posted for unsolicited events.
8329  * The function returns the allocated tag. The function is called with
8330  * no locks held.
8331  **/
8332 uint32_t
8333 lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
8334 {
8335         spin_lock_irq(&phba->hbalock);
8336         phba->buffer_tag_count++;
8337         /*
8338          * Always set the QUE_BUFTAG_BIT to distiguish between
8339          * a tag assigned by HBQ.
8340          */
8341         phba->buffer_tag_count |= QUE_BUFTAG_BIT;
8342         spin_unlock_irq(&phba->hbalock);
8343         return phba->buffer_tag_count;
8344 }
8345
8346 /**
8347  * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
8348  * @phba: Pointer to HBA context object.
8349  * @pring: Pointer to driver SLI ring object.
8350  * @tag: Buffer tag.
8351  *
8352  * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
8353  * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
8354  * iocb is posted to the response ring with the tag of the buffer.
8355  * This function searches the pring->postbufq list using the tag
8356  * to find buffer associated with CMD_IOCB_RET_XRI64_CX
8357  * iocb. If the buffer is found then lpfc_dmabuf object of the
8358  * buffer is returned to the caller else NULL is returned.
8359  * This function is called with no lock held.
8360  **/
8361 struct lpfc_dmabuf *
8362 lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
8363                         uint32_t tag)
8364 {
8365         struct lpfc_dmabuf *mp, *next_mp;
8366         struct list_head *slp = &pring->postbufq;
8367
8368         /* Search postbufq, from the beginning, looking for a match on tag */
8369         spin_lock_irq(&phba->hbalock);
8370         list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
8371                 if (mp->buffer_tag == tag) {
8372                         list_del_init(&mp->list);
8373                         pring->postbufq_cnt--;
8374                         spin_unlock_irq(&phba->hbalock);
8375                         return mp;
8376                 }
8377         }
8378
8379         spin_unlock_irq(&phba->hbalock);
8380         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8381                         "0402 Cannot find virtual addr for buffer tag on "
8382                         "ring %d Data x%lx x%p x%p x%x\n",
8383                         pring->ringno, (unsigned long) tag,
8384                         slp->next, slp->prev, pring->postbufq_cnt);
8385
8386         return NULL;
8387 }
8388
8389 /**
8390  * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
8391  * @phba: Pointer to HBA context object.
8392  * @pring: Pointer to driver SLI ring object.
8393  * @phys: DMA address of the buffer.
8394  *
8395  * This function searches the buffer list using the dma_address
8396  * of unsolicited event to find the driver's lpfc_dmabuf object
8397  * corresponding to the dma_address. The function returns the
8398  * lpfc_dmabuf object if a buffer is found else it returns NULL.
8399  * This function is called by the ct and els unsolicited event
8400  * handlers to get the buffer associated with the unsolicited
8401  * event.
8402  *
8403  * This function is called with no lock held.
8404  **/
8405 struct lpfc_dmabuf *
8406 lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
8407                          dma_addr_t phys)
8408 {
8409         struct lpfc_dmabuf *mp, *next_mp;
8410         struct list_head *slp = &pring->postbufq;
8411
8412         /* Search postbufq, from the beginning, looking for a match on phys */
8413         spin_lock_irq(&phba->hbalock);
8414         list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
8415                 if (mp->phys == phys) {
8416                         list_del_init(&mp->list);
8417                         pring->postbufq_cnt--;
8418                         spin_unlock_irq(&phba->hbalock);
8419                         return mp;
8420                 }
8421         }
8422
8423         spin_unlock_irq(&phba->hbalock);
8424         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8425                         "0410 Cannot find virtual addr for mapped buf on "
8426                         "ring %d Data x%llx x%p x%p x%x\n",
8427                         pring->ringno, (unsigned long long)phys,
8428                         slp->next, slp->prev, pring->postbufq_cnt);
8429         return NULL;
8430 }
8431
8432 /**
8433  * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
8434  * @phba: Pointer to HBA context object.
8435  * @cmdiocb: Pointer to driver command iocb object.
8436  * @rspiocb: Pointer to driver response iocb object.
8437  *
8438  * This function is the completion handler for the abort iocbs for
8439  * ELS commands. This function is called from the ELS ring event
8440  * handler with no lock held. This function frees memory resources
8441  * associated with the abort iocb.
8442  **/
8443 static void
8444 lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
8445                         struct lpfc_iocbq *rspiocb)
8446 {
8447         IOCB_t *irsp = &rspiocb->iocb;
8448         uint16_t abort_iotag, abort_context;
8449         struct lpfc_iocbq *abort_iocb;
8450         struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
8451
8452         abort_iocb = NULL;
8453
8454         if (irsp->ulpStatus) {
8455                 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
8456                 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
8457
8458                 spin_lock_irq(&phba->hbalock);
8459                 if (phba->sli_rev < LPFC_SLI_REV4) {
8460                         if (abort_iotag != 0 &&
8461                                 abort_iotag <= phba->sli.last_iotag)
8462                                 abort_iocb =
8463                                         phba->sli.iocbq_lookup[abort_iotag];
8464                 } else
8465                         /* For sli4 the abort_tag is the XRI,
8466                          * so the abort routine puts the iotag  of the iocb
8467                          * being aborted in the context field of the abort
8468                          * IOCB.
8469                          */
8470                         abort_iocb = phba->sli.iocbq_lookup[abort_context];
8471
8472                 /*
8473                  *  If the iocb is not found in Firmware queue the iocb
8474                  *  might have completed already. Do not free it again.
8475                  */
8476                 if (irsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
8477                         if (irsp->un.ulpWord[4] != IOERR_NO_XRI) {
8478                                 spin_unlock_irq(&phba->hbalock);
8479                                 lpfc_sli_release_iocbq(phba, cmdiocb);
8480                                 return;
8481                         }
8482                         /* For SLI4 the ulpContext field for abort IOCB
8483                          * holds the iotag of the IOCB being aborted so
8484                          * the local abort_context needs to be reset to
8485                          * match the aborted IOCBs ulpContext.
8486                          */
8487                         if (abort_iocb && phba->sli_rev == LPFC_SLI_REV4)
8488                                 abort_context = abort_iocb->iocb.ulpContext;
8489                 }
8490
8491                 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
8492                                 "0327 Cannot abort els iocb %p "
8493                                 "with tag %x context %x, abort status %x, "
8494                                 "abort code %x\n",
8495                                 abort_iocb, abort_iotag, abort_context,
8496                                 irsp->ulpStatus, irsp->un.ulpWord[4]);
8497                 /*
8498                  * make sure we have the right iocbq before taking it
8499                  * off the txcmplq and try to call completion routine.
8500                  */
8501                 if (!abort_iocb ||
8502                     abort_iocb->iocb.ulpContext != abort_context ||
8503                     (abort_iocb->iocb_flag & LPFC_DRIVER_ABORTED) == 0)
8504                         spin_unlock_irq(&phba->hbalock);
8505                 else if (phba->sli_rev < LPFC_SLI_REV4) {
8506                         /*
8507                          * leave the SLI4 aborted command on the txcmplq
8508                          * list and the command complete WCQE's XB bit
8509                          * will tell whether the SGL (XRI) can be released
8510                          * immediately or to the aborted SGL list for the
8511                          * following abort XRI from the HBA.
8512                          */
8513                         list_del_init(&abort_iocb->list);
8514                         if (abort_iocb->iocb_flag & LPFC_IO_ON_Q) {
8515                                 abort_iocb->iocb_flag &= ~LPFC_IO_ON_Q;
8516                                 pring->txcmplq_cnt--;
8517                         }
8518
8519                         /* Firmware could still be in progress of DMAing
8520                          * payload, so don't free data buffer till after
8521                          * a hbeat.
8522                          */
8523                         abort_iocb->iocb_flag |= LPFC_DELAY_MEM_FREE;
8524                         abort_iocb->iocb_flag &= ~LPFC_DRIVER_ABORTED;
8525                         spin_unlock_irq(&phba->hbalock);
8526
8527                         abort_iocb->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
8528                         abort_iocb->iocb.un.ulpWord[4] = IOERR_ABORT_REQUESTED;
8529                         (abort_iocb->iocb_cmpl)(phba, abort_iocb, abort_iocb);
8530                 } else
8531                         spin_unlock_irq(&phba->hbalock);
8532         }
8533
8534         lpfc_sli_release_iocbq(phba, cmdiocb);
8535         return;
8536 }
8537
8538 /**
8539  * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
8540  * @phba: Pointer to HBA context object.
8541  * @cmdiocb: Pointer to driver command iocb object.
8542  * @rspiocb: Pointer to driver response iocb object.
8543  *
8544  * The function is called from SLI ring event handler with no
8545  * lock held. This function is the completion handler for ELS commands
8546  * which are aborted. The function frees memory resources used for
8547  * the aborted ELS commands.
8548  **/
8549 static void
8550 lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
8551                      struct lpfc_iocbq *rspiocb)
8552 {
8553         IOCB_t *irsp = &rspiocb->iocb;
8554
8555         /* ELS cmd tag <ulpIoTag> completes */
8556         lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
8557                         "0139 Ignoring ELS cmd tag x%x completion Data: "
8558                         "x%x x%x x%x\n",
8559                         irsp->ulpIoTag, irsp->ulpStatus,
8560                         irsp->un.ulpWord[4], irsp->ulpTimeout);
8561         if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
8562                 lpfc_ct_free_iocb(phba, cmdiocb);
8563         else
8564                 lpfc_els_free_iocb(phba, cmdiocb);
8565         return;
8566 }
8567
8568 /**
8569  * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
8570  * @phba: Pointer to HBA context object.
8571  * @pring: Pointer to driver SLI ring object.
8572  * @cmdiocb: Pointer to driver command iocb object.
8573  *
8574  * This function issues an abort iocb for the provided command iocb down to
8575  * the port. Other than the case the outstanding command iocb is an abort
8576  * request, this function issues abort out unconditionally. This function is
8577  * called with hbalock held. The function returns 0 when it fails due to
8578  * memory allocation failure or when the command iocb is an abort request.
8579  **/
8580 static int
8581 lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
8582                            struct lpfc_iocbq *cmdiocb)
8583 {
8584         struct lpfc_vport *vport = cmdiocb->vport;
8585         struct lpfc_iocbq *abtsiocbp;
8586         IOCB_t *icmd = NULL;
8587         IOCB_t *iabt = NULL;
8588         int retval;
8589
8590         /*
8591          * There are certain command types we don't want to abort.  And we
8592          * don't want to abort commands that are already in the process of
8593          * being aborted.
8594          */
8595         icmd = &cmdiocb->iocb;
8596         if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
8597             icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
8598             (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
8599                 return 0;
8600
8601         /* issue ABTS for this IOCB based on iotag */
8602         abtsiocbp = __lpfc_sli_get_iocbq(phba);
8603         if (abtsiocbp == NULL)
8604                 return 0;
8605
8606         /* This signals the response to set the correct status
8607          * before calling the completion handler
8608          */
8609         cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
8610
8611         iabt = &abtsiocbp->iocb;
8612         iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
8613         iabt->un.acxri.abortContextTag = icmd->ulpContext;
8614         if (phba->sli_rev == LPFC_SLI_REV4) {
8615                 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
8616                 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
8617         }
8618         else
8619                 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
8620         iabt->ulpLe = 1;
8621         iabt->ulpClass = icmd->ulpClass;
8622
8623         /* ABTS WQE must go to the same WQ as the WQE to be aborted */
8624         abtsiocbp->fcp_wqidx = cmdiocb->fcp_wqidx;
8625         if (cmdiocb->iocb_flag & LPFC_IO_FCP)
8626                 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
8627
8628         if (phba->link_state >= LPFC_LINK_UP)
8629                 iabt->ulpCommand = CMD_ABORT_XRI_CN;
8630         else
8631                 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
8632
8633         abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
8634
8635         lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
8636                          "0339 Abort xri x%x, original iotag x%x, "
8637                          "abort cmd iotag x%x\n",
8638                          iabt->un.acxri.abortIoTag,
8639                          iabt->un.acxri.abortContextTag,
8640                          abtsiocbp->iotag);
8641         retval = __lpfc_sli_issue_iocb(phba, pring->ringno, abtsiocbp, 0);
8642
8643         if (retval)
8644                 __lpfc_sli_release_iocbq(phba, abtsiocbp);
8645
8646         /*
8647          * Caller to this routine should check for IOCB_ERROR
8648          * and handle it properly.  This routine no longer removes
8649          * iocb off txcmplq and call compl in case of IOCB_ERROR.
8650          */
8651         return retval;
8652 }
8653
8654 /**
8655  * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
8656  * @phba: Pointer to HBA context object.
8657  * @pring: Pointer to driver SLI ring object.
8658  * @cmdiocb: Pointer to driver command iocb object.
8659  *
8660  * This function issues an abort iocb for the provided command iocb. In case
8661  * of unloading, the abort iocb will not be issued to commands on the ELS
8662  * ring. Instead, the callback function shall be changed to those commands
8663  * so that nothing happens when them finishes. This function is called with
8664  * hbalock held. The function returns 0 when the command iocb is an abort
8665  * request.
8666  **/
8667 int
8668 lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
8669                            struct lpfc_iocbq *cmdiocb)
8670 {
8671         struct lpfc_vport *vport = cmdiocb->vport;
8672         int retval = IOCB_ERROR;
8673         IOCB_t *icmd = NULL;
8674
8675         /*
8676          * There are certain command types we don't want to abort.  And we
8677          * don't want to abort commands that are already in the process of
8678          * being aborted.
8679          */
8680         icmd = &cmdiocb->iocb;
8681         if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
8682             icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
8683             (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
8684                 return 0;
8685
8686         /*
8687          * If we're unloading, don't abort iocb on the ELS ring, but change
8688          * the callback so that nothing happens when it finishes.
8689          */
8690         if ((vport->load_flag & FC_UNLOADING) &&
8691             (pring->ringno == LPFC_ELS_RING)) {
8692                 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
8693                         cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
8694                 else
8695                         cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
8696                 goto abort_iotag_exit;
8697         }
8698
8699         /* Now, we try to issue the abort to the cmdiocb out */
8700         retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
8701
8702 abort_iotag_exit:
8703         /*
8704          * Caller to this routine should check for IOCB_ERROR
8705          * and handle it properly.  This routine no longer removes
8706          * iocb off txcmplq and call compl in case of IOCB_ERROR.
8707          */
8708         return retval;
8709 }
8710
8711 /**
8712  * lpfc_sli_iocb_ring_abort - Unconditionally abort all iocbs on an iocb ring
8713  * @phba: Pointer to HBA context object.
8714  * @pring: Pointer to driver SLI ring object.
8715  *
8716  * This function aborts all iocbs in the given ring and frees all the iocb
8717  * objects in txq. This function issues abort iocbs unconditionally for all
8718  * the iocb commands in txcmplq. The iocbs in the txcmplq is not guaranteed
8719  * to complete before the return of this function. The caller is not required
8720  * to hold any locks.
8721  **/
8722 static void
8723 lpfc_sli_iocb_ring_abort(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
8724 {
8725         LIST_HEAD(completions);
8726         struct lpfc_iocbq *iocb, *next_iocb;
8727
8728         if (pring->ringno == LPFC_ELS_RING)
8729                 lpfc_fabric_abort_hba(phba);
8730
8731         spin_lock_irq(&phba->hbalock);
8732
8733         /* Take off all the iocbs on txq for cancelling */
8734         list_splice_init(&pring->txq, &completions);
8735         pring->txq_cnt = 0;
8736
8737         /* Next issue ABTS for everything on the txcmplq */
8738         list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
8739                 lpfc_sli_abort_iotag_issue(phba, pring, iocb);
8740
8741         spin_unlock_irq(&phba->hbalock);
8742
8743         /* Cancel all the IOCBs from the completions list */
8744         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
8745                               IOERR_SLI_ABORTED);
8746 }
8747
8748 /**
8749  * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
8750  * @phba: pointer to lpfc HBA data structure.
8751  *
8752  * This routine will abort all pending and outstanding iocbs to an HBA.
8753  **/
8754 void
8755 lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
8756 {
8757         struct lpfc_sli *psli = &phba->sli;
8758         struct lpfc_sli_ring *pring;
8759         int i;
8760
8761         for (i = 0; i < psli->num_rings; i++) {
8762                 pring = &psli->ring[i];
8763                 lpfc_sli_iocb_ring_abort(phba, pring);
8764         }
8765 }
8766
8767 /**
8768  * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
8769  * @iocbq: Pointer to driver iocb object.
8770  * @vport: Pointer to driver virtual port object.
8771  * @tgt_id: SCSI ID of the target.
8772  * @lun_id: LUN ID of the scsi device.
8773  * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
8774  *
8775  * This function acts as an iocb filter for functions which abort or count
8776  * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
8777  * 0 if the filtering criteria is met for the given iocb and will return
8778  * 1 if the filtering criteria is not met.
8779  * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
8780  * given iocb is for the SCSI device specified by vport, tgt_id and
8781  * lun_id parameter.
8782  * If ctx_cmd == LPFC_CTX_TGT,  the function returns 0 only if the
8783  * given iocb is for the SCSI target specified by vport and tgt_id
8784  * parameters.
8785  * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
8786  * given iocb is for the SCSI host associated with the given vport.
8787  * This function is called with no locks held.
8788  **/
8789 static int
8790 lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
8791                            uint16_t tgt_id, uint64_t lun_id,
8792                            lpfc_ctx_cmd ctx_cmd)
8793 {
8794         struct lpfc_scsi_buf *lpfc_cmd;
8795         int rc = 1;
8796
8797         if (!(iocbq->iocb_flag &  LPFC_IO_FCP))
8798                 return rc;
8799
8800         if (iocbq->vport != vport)
8801                 return rc;
8802
8803         lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
8804
8805         if (lpfc_cmd->pCmd == NULL)
8806                 return rc;
8807
8808         switch (ctx_cmd) {
8809         case LPFC_CTX_LUN:
8810                 if ((lpfc_cmd->rdata->pnode) &&
8811                     (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
8812                     (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
8813                         rc = 0;
8814                 break;
8815         case LPFC_CTX_TGT:
8816                 if ((lpfc_cmd->rdata->pnode) &&
8817                     (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
8818                         rc = 0;
8819                 break;
8820         case LPFC_CTX_HOST:
8821                 rc = 0;
8822                 break;
8823         default:
8824                 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
8825                         __func__, ctx_cmd);
8826                 break;
8827         }
8828
8829         return rc;
8830 }
8831
8832 /**
8833  * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
8834  * @vport: Pointer to virtual port.
8835  * @tgt_id: SCSI ID of the target.
8836  * @lun_id: LUN ID of the scsi device.
8837  * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
8838  *
8839  * This function returns number of FCP commands pending for the vport.
8840  * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
8841  * commands pending on the vport associated with SCSI device specified
8842  * by tgt_id and lun_id parameters.
8843  * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
8844  * commands pending on the vport associated with SCSI target specified
8845  * by tgt_id parameter.
8846  * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
8847  * commands pending on the vport.
8848  * This function returns the number of iocbs which satisfy the filter.
8849  * This function is called without any lock held.
8850  **/
8851 int
8852 lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
8853                   lpfc_ctx_cmd ctx_cmd)
8854 {
8855         struct lpfc_hba *phba = vport->phba;
8856         struct lpfc_iocbq *iocbq;
8857         int sum, i;
8858
8859         for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
8860                 iocbq = phba->sli.iocbq_lookup[i];
8861
8862                 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
8863                                                 ctx_cmd) == 0)
8864                         sum++;
8865         }
8866
8867         return sum;
8868 }
8869
8870 /**
8871  * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
8872  * @phba: Pointer to HBA context object
8873  * @cmdiocb: Pointer to command iocb object.
8874  * @rspiocb: Pointer to response iocb object.
8875  *
8876  * This function is called when an aborted FCP iocb completes. This
8877  * function is called by the ring event handler with no lock held.
8878  * This function frees the iocb.
8879  **/
8880 void
8881 lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
8882                         struct lpfc_iocbq *rspiocb)
8883 {
8884         lpfc_sli_release_iocbq(phba, cmdiocb);
8885         return;
8886 }
8887
8888 /**
8889  * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
8890  * @vport: Pointer to virtual port.
8891  * @pring: Pointer to driver SLI ring object.
8892  * @tgt_id: SCSI ID of the target.
8893  * @lun_id: LUN ID of the scsi device.
8894  * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
8895  *
8896  * This function sends an abort command for every SCSI command
8897  * associated with the given virtual port pending on the ring
8898  * filtered by lpfc_sli_validate_fcp_iocb function.
8899  * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
8900  * FCP iocbs associated with lun specified by tgt_id and lun_id
8901  * parameters
8902  * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
8903  * FCP iocbs associated with SCSI target specified by tgt_id parameter.
8904  * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
8905  * FCP iocbs associated with virtual port.
8906  * This function returns number of iocbs it failed to abort.
8907  * This function is called with no locks held.
8908  **/
8909 int
8910 lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
8911                     uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
8912 {
8913         struct lpfc_hba *phba = vport->phba;
8914         struct lpfc_iocbq *iocbq;
8915         struct lpfc_iocbq *abtsiocb;
8916         IOCB_t *cmd = NULL;
8917         int errcnt = 0, ret_val = 0;
8918         int i;
8919
8920         for (i = 1; i <= phba->sli.last_iotag; i++) {
8921                 iocbq = phba->sli.iocbq_lookup[i];
8922
8923                 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
8924                                                abort_cmd) != 0)
8925                         continue;
8926
8927                 /* issue ABTS for this IOCB based on iotag */
8928                 abtsiocb = lpfc_sli_get_iocbq(phba);
8929                 if (abtsiocb == NULL) {
8930                         errcnt++;
8931                         continue;
8932                 }
8933
8934                 cmd = &iocbq->iocb;
8935                 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
8936                 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
8937                 if (phba->sli_rev == LPFC_SLI_REV4)
8938                         abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
8939                 else
8940                         abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
8941                 abtsiocb->iocb.ulpLe = 1;
8942                 abtsiocb->iocb.ulpClass = cmd->ulpClass;
8943                 abtsiocb->vport = phba->pport;
8944
8945                 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
8946                 abtsiocb->fcp_wqidx = iocbq->fcp_wqidx;
8947                 if (iocbq->iocb_flag & LPFC_IO_FCP)
8948                         abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
8949
8950                 if (lpfc_is_link_up(phba))
8951                         abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
8952                 else
8953                         abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
8954
8955                 /* Setup callback routine and issue the command. */
8956                 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
8957                 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
8958                                               abtsiocb, 0);
8959                 if (ret_val == IOCB_ERROR) {
8960                         lpfc_sli_release_iocbq(phba, abtsiocb);
8961                         errcnt++;
8962                         continue;
8963                 }
8964         }
8965
8966         return errcnt;
8967 }
8968
8969 /**
8970  * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
8971  * @phba: Pointer to HBA context object.
8972  * @cmdiocbq: Pointer to command iocb.
8973  * @rspiocbq: Pointer to response iocb.
8974  *
8975  * This function is the completion handler for iocbs issued using
8976  * lpfc_sli_issue_iocb_wait function. This function is called by the
8977  * ring event handler function without any lock held. This function
8978  * can be called from both worker thread context and interrupt
8979  * context. This function also can be called from other thread which
8980  * cleans up the SLI layer objects.
8981  * This function copy the contents of the response iocb to the
8982  * response iocb memory object provided by the caller of
8983  * lpfc_sli_issue_iocb_wait and then wakes up the thread which
8984  * sleeps for the iocb completion.
8985  **/
8986 static void
8987 lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
8988                         struct lpfc_iocbq *cmdiocbq,
8989                         struct lpfc_iocbq *rspiocbq)
8990 {
8991         wait_queue_head_t *pdone_q;
8992         unsigned long iflags;
8993         struct lpfc_scsi_buf *lpfc_cmd;
8994
8995         spin_lock_irqsave(&phba->hbalock, iflags);
8996         cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
8997         if (cmdiocbq->context2 && rspiocbq)
8998                 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
8999                        &rspiocbq->iocb, sizeof(IOCB_t));
9000
9001         /* Set the exchange busy flag for task management commands */
9002         if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
9003                 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
9004                 lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
9005                         cur_iocbq);
9006                 lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
9007         }
9008
9009         pdone_q = cmdiocbq->context_un.wait_queue;
9010         if (pdone_q)
9011                 wake_up(pdone_q);
9012         spin_unlock_irqrestore(&phba->hbalock, iflags);
9013         return;
9014 }
9015
9016 /**
9017  * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
9018  * @phba: Pointer to HBA context object..
9019  * @piocbq: Pointer to command iocb.
9020  * @flag: Flag to test.
9021  *
9022  * This routine grabs the hbalock and then test the iocb_flag to
9023  * see if the passed in flag is set.
9024  * Returns:
9025  * 1 if flag is set.
9026  * 0 if flag is not set.
9027  **/
9028 static int
9029 lpfc_chk_iocb_flg(struct lpfc_hba *phba,
9030                  struct lpfc_iocbq *piocbq, uint32_t flag)
9031 {
9032         unsigned long iflags;
9033         int ret;
9034
9035         spin_lock_irqsave(&phba->hbalock, iflags);
9036         ret = piocbq->iocb_flag & flag;
9037         spin_unlock_irqrestore(&phba->hbalock, iflags);
9038         return ret;
9039
9040 }
9041
9042 /**
9043  * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
9044  * @phba: Pointer to HBA context object..
9045  * @pring: Pointer to sli ring.
9046  * @piocb: Pointer to command iocb.
9047  * @prspiocbq: Pointer to response iocb.
9048  * @timeout: Timeout in number of seconds.
9049  *
9050  * This function issues the iocb to firmware and waits for the
9051  * iocb to complete. If the iocb command is not
9052  * completed within timeout seconds, it returns IOCB_TIMEDOUT.
9053  * Caller should not free the iocb resources if this function
9054  * returns IOCB_TIMEDOUT.
9055  * The function waits for the iocb completion using an
9056  * non-interruptible wait.
9057  * This function will sleep while waiting for iocb completion.
9058  * So, this function should not be called from any context which
9059  * does not allow sleeping. Due to the same reason, this function
9060  * cannot be called with interrupt disabled.
9061  * This function assumes that the iocb completions occur while
9062  * this function sleep. So, this function cannot be called from
9063  * the thread which process iocb completion for this ring.
9064  * This function clears the iocb_flag of the iocb object before
9065  * issuing the iocb and the iocb completion handler sets this
9066  * flag and wakes this thread when the iocb completes.
9067  * The contents of the response iocb will be copied to prspiocbq
9068  * by the completion handler when the command completes.
9069  * This function returns IOCB_SUCCESS when success.
9070  * This function is called with no lock held.
9071  **/
9072 int
9073 lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
9074                          uint32_t ring_number,
9075                          struct lpfc_iocbq *piocb,
9076                          struct lpfc_iocbq *prspiocbq,
9077                          uint32_t timeout)
9078 {
9079         DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
9080         long timeleft, timeout_req = 0;
9081         int retval = IOCB_SUCCESS;
9082         uint32_t creg_val;
9083         struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
9084         /*
9085          * If the caller has provided a response iocbq buffer, then context2
9086          * is NULL or its an error.
9087          */
9088         if (prspiocbq) {
9089                 if (piocb->context2)
9090                         return IOCB_ERROR;
9091                 piocb->context2 = prspiocbq;
9092         }
9093
9094         piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
9095         piocb->context_un.wait_queue = &done_q;
9096         piocb->iocb_flag &= ~LPFC_IO_WAKE;
9097
9098         if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
9099                 if (lpfc_readl(phba->HCregaddr, &creg_val))
9100                         return IOCB_ERROR;
9101                 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
9102                 writel(creg_val, phba->HCregaddr);
9103                 readl(phba->HCregaddr); /* flush */
9104         }
9105
9106         retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
9107                                      SLI_IOCB_RET_IOCB);
9108         if (retval == IOCB_SUCCESS) {
9109                 timeout_req = timeout * HZ;
9110                 timeleft = wait_event_timeout(done_q,
9111                                 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
9112                                 timeout_req);
9113
9114                 if (piocb->iocb_flag & LPFC_IO_WAKE) {
9115                         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9116                                         "0331 IOCB wake signaled\n");
9117                 } else if (timeleft == 0) {
9118                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9119                                         "0338 IOCB wait timeout error - no "
9120                                         "wake response Data x%x\n", timeout);
9121                         retval = IOCB_TIMEDOUT;
9122                 } else {
9123                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9124                                         "0330 IOCB wake NOT set, "
9125                                         "Data x%x x%lx\n",
9126                                         timeout, (timeleft / jiffies));
9127                         retval = IOCB_TIMEDOUT;
9128                 }
9129         } else if (retval == IOCB_BUSY) {
9130                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9131                         "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
9132                         phba->iocb_cnt, pring->txq_cnt, pring->txcmplq_cnt);
9133                 return retval;
9134         } else {
9135                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9136                                 "0332 IOCB wait issue failed, Data x%x\n",
9137                                 retval);
9138                 retval = IOCB_ERROR;
9139         }
9140
9141         if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
9142                 if (lpfc_readl(phba->HCregaddr, &creg_val))
9143                         return IOCB_ERROR;
9144                 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
9145                 writel(creg_val, phba->HCregaddr);
9146                 readl(phba->HCregaddr); /* flush */
9147         }
9148
9149         if (prspiocbq)
9150                 piocb->context2 = NULL;
9151
9152         piocb->context_un.wait_queue = NULL;
9153         piocb->iocb_cmpl = NULL;
9154         return retval;
9155 }
9156
9157 /**
9158  * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
9159  * @phba: Pointer to HBA context object.
9160  * @pmboxq: Pointer to driver mailbox object.
9161  * @timeout: Timeout in number of seconds.
9162  *
9163  * This function issues the mailbox to firmware and waits for the
9164  * mailbox command to complete. If the mailbox command is not
9165  * completed within timeout seconds, it returns MBX_TIMEOUT.
9166  * The function waits for the mailbox completion using an
9167  * interruptible wait. If the thread is woken up due to a
9168  * signal, MBX_TIMEOUT error is returned to the caller. Caller
9169  * should not free the mailbox resources, if this function returns
9170  * MBX_TIMEOUT.
9171  * This function will sleep while waiting for mailbox completion.
9172  * So, this function should not be called from any context which
9173  * does not allow sleeping. Due to the same reason, this function
9174  * cannot be called with interrupt disabled.
9175  * This function assumes that the mailbox completion occurs while
9176  * this function sleep. So, this function cannot be called from
9177  * the worker thread which processes mailbox completion.
9178  * This function is called in the context of HBA management
9179  * applications.
9180  * This function returns MBX_SUCCESS when successful.
9181  * This function is called with no lock held.
9182  **/
9183 int
9184 lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
9185                          uint32_t timeout)
9186 {
9187         DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
9188         int retval;
9189         unsigned long flag;
9190
9191         /* The caller must leave context1 empty. */
9192         if (pmboxq->context1)
9193                 return MBX_NOT_FINISHED;
9194
9195         pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
9196         /* setup wake call as IOCB callback */
9197         pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
9198         /* setup context field to pass wait_queue pointer to wake function  */
9199         pmboxq->context1 = &done_q;
9200
9201         /* now issue the command */
9202         retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
9203
9204         if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
9205                 wait_event_interruptible_timeout(done_q,
9206                                 pmboxq->mbox_flag & LPFC_MBX_WAKE,
9207                                 timeout * HZ);
9208
9209                 spin_lock_irqsave(&phba->hbalock, flag);
9210                 pmboxq->context1 = NULL;
9211                 /*
9212                  * if LPFC_MBX_WAKE flag is set the mailbox is completed
9213                  * else do not free the resources.
9214                  */
9215                 if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
9216                         retval = MBX_SUCCESS;
9217                         lpfc_sli4_swap_str(phba, pmboxq);
9218                 } else {
9219                         retval = MBX_TIMEOUT;
9220                         pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
9221                 }
9222                 spin_unlock_irqrestore(&phba->hbalock, flag);
9223         }
9224
9225         return retval;
9226 }
9227
9228 /**
9229  * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
9230  * @phba: Pointer to HBA context.
9231  *
9232  * This function is called to shutdown the driver's mailbox sub-system.
9233  * It first marks the mailbox sub-system is in a block state to prevent
9234  * the asynchronous mailbox command from issued off the pending mailbox
9235  * command queue. If the mailbox command sub-system shutdown is due to
9236  * HBA error conditions such as EEH or ERATT, this routine shall invoke
9237  * the mailbox sub-system flush routine to forcefully bring down the
9238  * mailbox sub-system. Otherwise, if it is due to normal condition (such
9239  * as with offline or HBA function reset), this routine will wait for the
9240  * outstanding mailbox command to complete before invoking the mailbox
9241  * sub-system flush routine to gracefully bring down mailbox sub-system.
9242  **/
9243 void
9244 lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba)
9245 {
9246         struct lpfc_sli *psli = &phba->sli;
9247         uint8_t actcmd = MBX_HEARTBEAT;
9248         unsigned long timeout;
9249
9250         spin_lock_irq(&phba->hbalock);
9251         psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
9252         spin_unlock_irq(&phba->hbalock);
9253
9254         if (psli->sli_flag & LPFC_SLI_ACTIVE) {
9255                 spin_lock_irq(&phba->hbalock);
9256                 if (phba->sli.mbox_active)
9257                         actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
9258                 spin_unlock_irq(&phba->hbalock);
9259                 /* Determine how long we might wait for the active mailbox
9260                  * command to be gracefully completed by firmware.
9261                  */
9262                 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, actcmd) *
9263                                            1000) + jiffies;
9264                 while (phba->sli.mbox_active) {
9265                         /* Check active mailbox complete status every 2ms */
9266                         msleep(2);
9267                         if (time_after(jiffies, timeout))
9268                                 /* Timeout, let the mailbox flush routine to
9269                                  * forcefully release active mailbox command
9270                                  */
9271                                 break;
9272                 }
9273         }
9274         lpfc_sli_mbox_sys_flush(phba);
9275 }
9276
9277 /**
9278  * lpfc_sli_eratt_read - read sli-3 error attention events
9279  * @phba: Pointer to HBA context.
9280  *
9281  * This function is called to read the SLI3 device error attention registers
9282  * for possible error attention events. The caller must hold the hostlock
9283  * with spin_lock_irq().
9284  *
9285  * This function returns 1 when there is Error Attention in the Host Attention
9286  * Register and returns 0 otherwise.
9287  **/
9288 static int
9289 lpfc_sli_eratt_read(struct lpfc_hba *phba)
9290 {
9291         uint32_t ha_copy;
9292
9293         /* Read chip Host Attention (HA) register */
9294         if (lpfc_readl(phba->HAregaddr, &ha_copy))
9295                 goto unplug_err;
9296
9297         if (ha_copy & HA_ERATT) {
9298                 /* Read host status register to retrieve error event */
9299                 if (lpfc_sli_read_hs(phba))
9300                         goto unplug_err;
9301
9302                 /* Check if there is a deferred error condition is active */
9303                 if ((HS_FFER1 & phba->work_hs) &&
9304                     ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
9305                       HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
9306                         phba->hba_flag |= DEFER_ERATT;
9307                         /* Clear all interrupt enable conditions */
9308                         writel(0, phba->HCregaddr);
9309                         readl(phba->HCregaddr);
9310                 }
9311
9312                 /* Set the driver HA work bitmap */
9313                 phba->work_ha |= HA_ERATT;
9314                 /* Indicate polling handles this ERATT */
9315                 phba->hba_flag |= HBA_ERATT_HANDLED;
9316                 return 1;
9317         }
9318         return 0;
9319
9320 unplug_err:
9321         /* Set the driver HS work bitmap */
9322         phba->work_hs |= UNPLUG_ERR;
9323         /* Set the driver HA work bitmap */
9324         phba->work_ha |= HA_ERATT;
9325         /* Indicate polling handles this ERATT */
9326         phba->hba_flag |= HBA_ERATT_HANDLED;
9327         return 1;
9328 }
9329
9330 /**
9331  * lpfc_sli4_eratt_read - read sli-4 error attention events
9332  * @phba: Pointer to HBA context.
9333  *
9334  * This function is called to read the SLI4 device error attention registers
9335  * for possible error attention events. The caller must hold the hostlock
9336  * with spin_lock_irq().
9337  *
9338  * This function returns 1 when there is Error Attention in the Host Attention
9339  * Register and returns 0 otherwise.
9340  **/
9341 static int
9342 lpfc_sli4_eratt_read(struct lpfc_hba *phba)
9343 {
9344         uint32_t uerr_sta_hi, uerr_sta_lo;
9345         uint32_t if_type, portsmphr;
9346         struct lpfc_register portstat_reg;
9347
9348         /*
9349          * For now, use the SLI4 device internal unrecoverable error
9350          * registers for error attention. This can be changed later.
9351          */
9352         if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
9353         switch (if_type) {
9354         case LPFC_SLI_INTF_IF_TYPE_0:
9355                 if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
9356                         &uerr_sta_lo) ||
9357                         lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
9358                         &uerr_sta_hi)) {
9359                         phba->work_hs |= UNPLUG_ERR;
9360                         phba->work_ha |= HA_ERATT;
9361                         phba->hba_flag |= HBA_ERATT_HANDLED;
9362                         return 1;
9363                 }
9364                 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
9365                     (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
9366                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9367                                         "1423 HBA Unrecoverable error: "
9368                                         "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
9369                                         "ue_mask_lo_reg=0x%x, "
9370                                         "ue_mask_hi_reg=0x%x\n",
9371                                         uerr_sta_lo, uerr_sta_hi,
9372                                         phba->sli4_hba.ue_mask_lo,
9373                                         phba->sli4_hba.ue_mask_hi);
9374                         phba->work_status[0] = uerr_sta_lo;
9375                         phba->work_status[1] = uerr_sta_hi;
9376                         phba->work_ha |= HA_ERATT;
9377                         phba->hba_flag |= HBA_ERATT_HANDLED;
9378                         return 1;
9379                 }
9380                 break;
9381         case LPFC_SLI_INTF_IF_TYPE_2:
9382                 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
9383                         &portstat_reg.word0) ||
9384                         lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
9385                         &portsmphr)){
9386                         phba->work_hs |= UNPLUG_ERR;
9387                         phba->work_ha |= HA_ERATT;
9388                         phba->hba_flag |= HBA_ERATT_HANDLED;
9389                         return 1;
9390                 }
9391                 if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
9392                         phba->work_status[0] =
9393                                 readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
9394                         phba->work_status[1] =
9395                                 readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
9396                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9397                                         "2885 Port Error Detected: "
9398                                         "port status reg 0x%x, "
9399                                         "port smphr reg 0x%x, "
9400                                         "error 1=0x%x, error 2=0x%x\n",
9401                                         portstat_reg.word0,
9402                                         portsmphr,
9403                                         phba->work_status[0],
9404                                         phba->work_status[1]);
9405                         phba->work_ha |= HA_ERATT;
9406                         phba->hba_flag |= HBA_ERATT_HANDLED;
9407                         return 1;
9408                 }
9409                 break;
9410         case LPFC_SLI_INTF_IF_TYPE_1:
9411         default:
9412                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9413                                 "2886 HBA Error Attention on unsupported "
9414                                 "if type %d.", if_type);
9415                 return 1;
9416         }
9417
9418         return 0;
9419 }
9420
9421 /**
9422  * lpfc_sli_check_eratt - check error attention events
9423  * @phba: Pointer to HBA context.
9424  *
9425  * This function is called from timer soft interrupt context to check HBA's
9426  * error attention register bit for error attention events.
9427  *
9428  * This function returns 1 when there is Error Attention in the Host Attention
9429  * Register and returns 0 otherwise.
9430  **/
9431 int
9432 lpfc_sli_check_eratt(struct lpfc_hba *phba)
9433 {
9434         uint32_t ha_copy;
9435
9436         /* If somebody is waiting to handle an eratt, don't process it
9437          * here. The brdkill function will do this.
9438          */
9439         if (phba->link_flag & LS_IGNORE_ERATT)
9440                 return 0;
9441
9442         /* Check if interrupt handler handles this ERATT */
9443         spin_lock_irq(&phba->hbalock);
9444         if (phba->hba_flag & HBA_ERATT_HANDLED) {
9445                 /* Interrupt handler has handled ERATT */
9446                 spin_unlock_irq(&phba->hbalock);
9447                 return 0;
9448         }
9449
9450         /*
9451          * If there is deferred error attention, do not check for error
9452          * attention
9453          */
9454         if (unlikely(phba->hba_flag & DEFER_ERATT)) {
9455                 spin_unlock_irq(&phba->hbalock);
9456                 return 0;
9457         }
9458
9459         /* If PCI channel is offline, don't process it */
9460         if (unlikely(pci_channel_offline(phba->pcidev))) {
9461                 spin_unlock_irq(&phba->hbalock);
9462                 return 0;
9463         }
9464
9465         switch (phba->sli_rev) {
9466         case LPFC_SLI_REV2:
9467         case LPFC_SLI_REV3:
9468                 /* Read chip Host Attention (HA) register */
9469                 ha_copy = lpfc_sli_eratt_read(phba);
9470                 break;
9471         case LPFC_SLI_REV4:
9472                 /* Read device Uncoverable Error (UERR) registers */
9473                 ha_copy = lpfc_sli4_eratt_read(phba);
9474                 break;
9475         default:
9476                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9477                                 "0299 Invalid SLI revision (%d)\n",
9478                                 phba->sli_rev);
9479                 ha_copy = 0;
9480                 break;
9481         }
9482         spin_unlock_irq(&phba->hbalock);
9483
9484         return ha_copy;
9485 }
9486
9487 /**
9488  * lpfc_intr_state_check - Check device state for interrupt handling
9489  * @phba: Pointer to HBA context.
9490  *
9491  * This inline routine checks whether a device or its PCI slot is in a state
9492  * that the interrupt should be handled.
9493  *
9494  * This function returns 0 if the device or the PCI slot is in a state that
9495  * interrupt should be handled, otherwise -EIO.
9496  */
9497 static inline int
9498 lpfc_intr_state_check(struct lpfc_hba *phba)
9499 {
9500         /* If the pci channel is offline, ignore all the interrupts */
9501         if (unlikely(pci_channel_offline(phba->pcidev)))
9502                 return -EIO;
9503
9504         /* Update device level interrupt statistics */
9505         phba->sli.slistat.sli_intr++;
9506
9507         /* Ignore all interrupts during initialization. */
9508         if (unlikely(phba->link_state < LPFC_LINK_DOWN))
9509                 return -EIO;
9510
9511         return 0;
9512 }
9513
9514 /**
9515  * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
9516  * @irq: Interrupt number.
9517  * @dev_id: The device context pointer.
9518  *
9519  * This function is directly called from the PCI layer as an interrupt
9520  * service routine when device with SLI-3 interface spec is enabled with
9521  * MSI-X multi-message interrupt mode and there are slow-path events in
9522  * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
9523  * interrupt mode, this function is called as part of the device-level
9524  * interrupt handler. When the PCI slot is in error recovery or the HBA
9525  * is undergoing initialization, the interrupt handler will not process
9526  * the interrupt. The link attention and ELS ring attention events are
9527  * handled by the worker thread. The interrupt handler signals the worker
9528  * thread and returns for these events. This function is called without
9529  * any lock held. It gets the hbalock to access and update SLI data
9530  * structures.
9531  *
9532  * This function returns IRQ_HANDLED when interrupt is handled else it
9533  * returns IRQ_NONE.
9534  **/
9535 irqreturn_t
9536 lpfc_sli_sp_intr_handler(int irq, void *dev_id)
9537 {
9538         struct lpfc_hba  *phba;
9539         uint32_t ha_copy, hc_copy;
9540         uint32_t work_ha_copy;
9541         unsigned long status;
9542         unsigned long iflag;
9543         uint32_t control;
9544
9545         MAILBOX_t *mbox, *pmbox;
9546         struct lpfc_vport *vport;
9547         struct lpfc_nodelist *ndlp;
9548         struct lpfc_dmabuf *mp;
9549         LPFC_MBOXQ_t *pmb;
9550         int rc;
9551
9552         /*
9553          * Get the driver's phba structure from the dev_id and
9554          * assume the HBA is not interrupting.
9555          */
9556         phba = (struct lpfc_hba *)dev_id;
9557
9558         if (unlikely(!phba))
9559                 return IRQ_NONE;
9560
9561         /*
9562          * Stuff needs to be attented to when this function is invoked as an
9563          * individual interrupt handler in MSI-X multi-message interrupt mode
9564          */
9565         if (phba->intr_type == MSIX) {
9566                 /* Check device state for handling interrupt */
9567                 if (lpfc_intr_state_check(phba))
9568                         return IRQ_NONE;
9569                 /* Need to read HA REG for slow-path events */
9570                 spin_lock_irqsave(&phba->hbalock, iflag);
9571                 if (lpfc_readl(phba->HAregaddr, &ha_copy))
9572                         goto unplug_error;
9573                 /* If somebody is waiting to handle an eratt don't process it
9574                  * here. The brdkill function will do this.
9575                  */
9576                 if (phba->link_flag & LS_IGNORE_ERATT)
9577                         ha_copy &= ~HA_ERATT;
9578                 /* Check the need for handling ERATT in interrupt handler */
9579                 if (ha_copy & HA_ERATT) {
9580                         if (phba->hba_flag & HBA_ERATT_HANDLED)
9581                                 /* ERATT polling has handled ERATT */
9582                                 ha_copy &= ~HA_ERATT;
9583                         else
9584                                 /* Indicate interrupt handler handles ERATT */
9585                                 phba->hba_flag |= HBA_ERATT_HANDLED;
9586                 }
9587
9588                 /*
9589                  * If there is deferred error attention, do not check for any
9590                  * interrupt.
9591                  */
9592                 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
9593                         spin_unlock_irqrestore(&phba->hbalock, iflag);
9594                         return IRQ_NONE;
9595                 }
9596
9597                 /* Clear up only attention source related to slow-path */
9598                 if (lpfc_readl(phba->HCregaddr, &hc_copy))
9599                         goto unplug_error;
9600
9601                 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
9602                         HC_LAINT_ENA | HC_ERINT_ENA),
9603                         phba->HCregaddr);
9604                 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
9605                         phba->HAregaddr);
9606                 writel(hc_copy, phba->HCregaddr);
9607                 readl(phba->HAregaddr); /* flush */
9608                 spin_unlock_irqrestore(&phba->hbalock, iflag);
9609         } else
9610                 ha_copy = phba->ha_copy;
9611
9612         work_ha_copy = ha_copy & phba->work_ha_mask;
9613
9614         if (work_ha_copy) {
9615                 if (work_ha_copy & HA_LATT) {
9616                         if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
9617                                 /*
9618                                  * Turn off Link Attention interrupts
9619                                  * until CLEAR_LA done
9620                                  */
9621                                 spin_lock_irqsave(&phba->hbalock, iflag);
9622                                 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
9623                                 if (lpfc_readl(phba->HCregaddr, &control))
9624                                         goto unplug_error;
9625                                 control &= ~HC_LAINT_ENA;
9626                                 writel(control, phba->HCregaddr);
9627                                 readl(phba->HCregaddr); /* flush */
9628                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
9629                         }
9630                         else
9631                                 work_ha_copy &= ~HA_LATT;
9632                 }
9633
9634                 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
9635                         /*
9636                          * Turn off Slow Rings interrupts, LPFC_ELS_RING is
9637                          * the only slow ring.
9638                          */
9639                         status = (work_ha_copy &
9640                                 (HA_RXMASK  << (4*LPFC_ELS_RING)));
9641                         status >>= (4*LPFC_ELS_RING);
9642                         if (status & HA_RXMASK) {
9643                                 spin_lock_irqsave(&phba->hbalock, iflag);
9644                                 if (lpfc_readl(phba->HCregaddr, &control))
9645                                         goto unplug_error;
9646
9647                                 lpfc_debugfs_slow_ring_trc(phba,
9648                                 "ISR slow ring:   ctl:x%x stat:x%x isrcnt:x%x",
9649                                 control, status,
9650                                 (uint32_t)phba->sli.slistat.sli_intr);
9651
9652                                 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
9653                                         lpfc_debugfs_slow_ring_trc(phba,
9654                                                 "ISR Disable ring:"
9655                                                 "pwork:x%x hawork:x%x wait:x%x",
9656                                                 phba->work_ha, work_ha_copy,
9657                                                 (uint32_t)((unsigned long)
9658                                                 &phba->work_waitq));
9659
9660                                         control &=
9661                                             ~(HC_R0INT_ENA << LPFC_ELS_RING);
9662                                         writel(control, phba->HCregaddr);
9663                                         readl(phba->HCregaddr); /* flush */
9664                                 }
9665                                 else {
9666                                         lpfc_debugfs_slow_ring_trc(phba,
9667                                                 "ISR slow ring:   pwork:"
9668                                                 "x%x hawork:x%x wait:x%x",
9669                                                 phba->work_ha, work_ha_copy,
9670                                                 (uint32_t)((unsigned long)
9671                                                 &phba->work_waitq));
9672                                 }
9673                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
9674                         }
9675                 }
9676                 spin_lock_irqsave(&phba->hbalock, iflag);
9677                 if (work_ha_copy & HA_ERATT) {
9678                         if (lpfc_sli_read_hs(phba))
9679                                 goto unplug_error;
9680                         /*
9681                          * Check if there is a deferred error condition
9682                          * is active
9683                          */
9684                         if ((HS_FFER1 & phba->work_hs) &&
9685                                 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
9686                                   HS_FFER6 | HS_FFER7 | HS_FFER8) &
9687                                   phba->work_hs)) {
9688                                 phba->hba_flag |= DEFER_ERATT;
9689                                 /* Clear all interrupt enable conditions */
9690                                 writel(0, phba->HCregaddr);
9691                                 readl(phba->HCregaddr);
9692                         }
9693                 }
9694
9695                 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
9696                         pmb = phba->sli.mbox_active;
9697                         pmbox = &pmb->u.mb;
9698                         mbox = phba->mbox;
9699                         vport = pmb->vport;
9700
9701                         /* First check out the status word */
9702                         lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
9703                         if (pmbox->mbxOwner != OWN_HOST) {
9704                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
9705                                 /*
9706                                  * Stray Mailbox Interrupt, mbxCommand <cmd>
9707                                  * mbxStatus <status>
9708                                  */
9709                                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
9710                                                 LOG_SLI,
9711                                                 "(%d):0304 Stray Mailbox "
9712                                                 "Interrupt mbxCommand x%x "
9713                                                 "mbxStatus x%x\n",
9714                                                 (vport ? vport->vpi : 0),
9715                                                 pmbox->mbxCommand,
9716                                                 pmbox->mbxStatus);
9717                                 /* clear mailbox attention bit */
9718                                 work_ha_copy &= ~HA_MBATT;
9719                         } else {
9720                                 phba->sli.mbox_active = NULL;
9721                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
9722                                 phba->last_completion_time = jiffies;
9723                                 del_timer(&phba->sli.mbox_tmo);
9724                                 if (pmb->mbox_cmpl) {
9725                                         lpfc_sli_pcimem_bcopy(mbox, pmbox,
9726                                                         MAILBOX_CMD_SIZE);
9727                                         if (pmb->out_ext_byte_len &&
9728                                                 pmb->context2)
9729                                                 lpfc_sli_pcimem_bcopy(
9730                                                 phba->mbox_ext,
9731                                                 pmb->context2,
9732                                                 pmb->out_ext_byte_len);
9733                                 }
9734                                 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
9735                                         pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
9736
9737                                         lpfc_debugfs_disc_trc(vport,
9738                                                 LPFC_DISC_TRC_MBOX_VPORT,
9739                                                 "MBOX dflt rpi: : "
9740                                                 "status:x%x rpi:x%x",
9741                                                 (uint32_t)pmbox->mbxStatus,
9742                                                 pmbox->un.varWords[0], 0);
9743
9744                                         if (!pmbox->mbxStatus) {
9745                                                 mp = (struct lpfc_dmabuf *)
9746                                                         (pmb->context1);
9747                                                 ndlp = (struct lpfc_nodelist *)
9748                                                         pmb->context2;
9749
9750                                                 /* Reg_LOGIN of dflt RPI was
9751                                                  * successful. new lets get
9752                                                  * rid of the RPI using the
9753                                                  * same mbox buffer.
9754                                                  */
9755                                                 lpfc_unreg_login(phba,
9756                                                         vport->vpi,
9757                                                         pmbox->un.varWords[0],
9758                                                         pmb);
9759                                                 pmb->mbox_cmpl =
9760                                                         lpfc_mbx_cmpl_dflt_rpi;
9761                                                 pmb->context1 = mp;
9762                                                 pmb->context2 = ndlp;
9763                                                 pmb->vport = vport;
9764                                                 rc = lpfc_sli_issue_mbox(phba,
9765                                                                 pmb,
9766                                                                 MBX_NOWAIT);
9767                                                 if (rc != MBX_BUSY)
9768                                                         lpfc_printf_log(phba,
9769                                                         KERN_ERR,
9770                                                         LOG_MBOX | LOG_SLI,
9771                                                         "0350 rc should have"
9772                                                         "been MBX_BUSY\n");
9773                                                 if (rc != MBX_NOT_FINISHED)
9774                                                         goto send_current_mbox;
9775                                         }
9776                                 }
9777                                 spin_lock_irqsave(
9778                                                 &phba->pport->work_port_lock,
9779                                                 iflag);
9780                                 phba->pport->work_port_events &=
9781                                         ~WORKER_MBOX_TMO;
9782                                 spin_unlock_irqrestore(
9783                                                 &phba->pport->work_port_lock,
9784                                                 iflag);
9785                                 lpfc_mbox_cmpl_put(phba, pmb);
9786                         }
9787                 } else
9788                         spin_unlock_irqrestore(&phba->hbalock, iflag);
9789
9790                 if ((work_ha_copy & HA_MBATT) &&
9791                     (phba->sli.mbox_active == NULL)) {
9792 send_current_mbox:
9793                         /* Process next mailbox command if there is one */
9794                         do {
9795                                 rc = lpfc_sli_issue_mbox(phba, NULL,
9796                                                          MBX_NOWAIT);
9797                         } while (rc == MBX_NOT_FINISHED);
9798                         if (rc != MBX_SUCCESS)
9799                                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
9800                                                 LOG_SLI, "0349 rc should be "
9801                                                 "MBX_SUCCESS\n");
9802                 }
9803
9804                 spin_lock_irqsave(&phba->hbalock, iflag);
9805                 phba->work_ha |= work_ha_copy;
9806                 spin_unlock_irqrestore(&phba->hbalock, iflag);
9807                 lpfc_worker_wake_up(phba);
9808         }
9809         return IRQ_HANDLED;
9810 unplug_error:
9811         spin_unlock_irqrestore(&phba->hbalock, iflag);
9812         return IRQ_HANDLED;
9813
9814 } /* lpfc_sli_sp_intr_handler */
9815
9816 /**
9817  * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
9818  * @irq: Interrupt number.
9819  * @dev_id: The device context pointer.
9820  *
9821  * This function is directly called from the PCI layer as an interrupt
9822  * service routine when device with SLI-3 interface spec is enabled with
9823  * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
9824  * ring event in the HBA. However, when the device is enabled with either
9825  * MSI or Pin-IRQ interrupt mode, this function is called as part of the
9826  * device-level interrupt handler. When the PCI slot is in error recovery
9827  * or the HBA is undergoing initialization, the interrupt handler will not
9828  * process the interrupt. The SCSI FCP fast-path ring event are handled in
9829  * the intrrupt context. This function is called without any lock held.
9830  * It gets the hbalock to access and update SLI data structures.
9831  *
9832  * This function returns IRQ_HANDLED when interrupt is handled else it
9833  * returns IRQ_NONE.
9834  **/
9835 irqreturn_t
9836 lpfc_sli_fp_intr_handler(int irq, void *dev_id)
9837 {
9838         struct lpfc_hba  *phba;
9839         uint32_t ha_copy;
9840         unsigned long status;
9841         unsigned long iflag;
9842
9843         /* Get the driver's phba structure from the dev_id and
9844          * assume the HBA is not interrupting.
9845          */
9846         phba = (struct lpfc_hba *) dev_id;
9847
9848         if (unlikely(!phba))
9849                 return IRQ_NONE;
9850
9851         /*
9852          * Stuff needs to be attented to when this function is invoked as an
9853          * individual interrupt handler in MSI-X multi-message interrupt mode
9854          */
9855         if (phba->intr_type == MSIX) {
9856                 /* Check device state for handling interrupt */
9857                 if (lpfc_intr_state_check(phba))
9858                         return IRQ_NONE;
9859                 /* Need to read HA REG for FCP ring and other ring events */
9860                 if (lpfc_readl(phba->HAregaddr, &ha_copy))
9861                         return IRQ_HANDLED;
9862                 /* Clear up only attention source related to fast-path */
9863                 spin_lock_irqsave(&phba->hbalock, iflag);
9864                 /*
9865                  * If there is deferred error attention, do not check for
9866                  * any interrupt.
9867                  */
9868                 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
9869                         spin_unlock_irqrestore(&phba->hbalock, iflag);
9870                         return IRQ_NONE;
9871                 }
9872                 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
9873                         phba->HAregaddr);
9874                 readl(phba->HAregaddr); /* flush */
9875                 spin_unlock_irqrestore(&phba->hbalock, iflag);
9876         } else
9877                 ha_copy = phba->ha_copy;
9878
9879         /*
9880          * Process all events on FCP ring. Take the optimized path for FCP IO.
9881          */
9882         ha_copy &= ~(phba->work_ha_mask);
9883
9884         status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
9885         status >>= (4*LPFC_FCP_RING);
9886         if (status & HA_RXMASK)
9887                 lpfc_sli_handle_fast_ring_event(phba,
9888                                                 &phba->sli.ring[LPFC_FCP_RING],
9889                                                 status);
9890
9891         if (phba->cfg_multi_ring_support == 2) {
9892                 /*
9893                  * Process all events on extra ring. Take the optimized path
9894                  * for extra ring IO.
9895                  */
9896                 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
9897                 status >>= (4*LPFC_EXTRA_RING);
9898                 if (status & HA_RXMASK) {
9899                         lpfc_sli_handle_fast_ring_event(phba,
9900                                         &phba->sli.ring[LPFC_EXTRA_RING],
9901                                         status);
9902                 }
9903         }
9904         return IRQ_HANDLED;
9905 }  /* lpfc_sli_fp_intr_handler */
9906
9907 /**
9908  * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
9909  * @irq: Interrupt number.
9910  * @dev_id: The device context pointer.
9911  *
9912  * This function is the HBA device-level interrupt handler to device with
9913  * SLI-3 interface spec, called from the PCI layer when either MSI or
9914  * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
9915  * requires driver attention. This function invokes the slow-path interrupt
9916  * attention handling function and fast-path interrupt attention handling
9917  * function in turn to process the relevant HBA attention events. This
9918  * function is called without any lock held. It gets the hbalock to access
9919  * and update SLI data structures.
9920  *
9921  * This function returns IRQ_HANDLED when interrupt is handled, else it
9922  * returns IRQ_NONE.
9923  **/
9924 irqreturn_t
9925 lpfc_sli_intr_handler(int irq, void *dev_id)
9926 {
9927         struct lpfc_hba  *phba;
9928         irqreturn_t sp_irq_rc, fp_irq_rc;
9929         unsigned long status1, status2;
9930         uint32_t hc_copy;
9931
9932         /*
9933          * Get the driver's phba structure from the dev_id and
9934          * assume the HBA is not interrupting.
9935          */
9936         phba = (struct lpfc_hba *) dev_id;
9937
9938         if (unlikely(!phba))
9939                 return IRQ_NONE;
9940
9941         /* Check device state for handling interrupt */
9942         if (lpfc_intr_state_check(phba))
9943                 return IRQ_NONE;
9944
9945         spin_lock(&phba->hbalock);
9946         if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
9947                 spin_unlock(&phba->hbalock);
9948                 return IRQ_HANDLED;
9949         }
9950
9951         if (unlikely(!phba->ha_copy)) {
9952                 spin_unlock(&phba->hbalock);
9953                 return IRQ_NONE;
9954         } else if (phba->ha_copy & HA_ERATT) {
9955                 if (phba->hba_flag & HBA_ERATT_HANDLED)
9956                         /* ERATT polling has handled ERATT */
9957                         phba->ha_copy &= ~HA_ERATT;
9958                 else
9959                         /* Indicate interrupt handler handles ERATT */
9960                         phba->hba_flag |= HBA_ERATT_HANDLED;
9961         }
9962
9963         /*
9964          * If there is deferred error attention, do not check for any interrupt.
9965          */
9966         if (unlikely(phba->hba_flag & DEFER_ERATT)) {
9967                 spin_unlock(&phba->hbalock);
9968                 return IRQ_NONE;
9969         }
9970
9971         /* Clear attention sources except link and error attentions */
9972         if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
9973                 spin_unlock(&phba->hbalock);
9974                 return IRQ_HANDLED;
9975         }
9976         writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
9977                 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
9978                 phba->HCregaddr);
9979         writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
9980         writel(hc_copy, phba->HCregaddr);
9981         readl(phba->HAregaddr); /* flush */
9982         spin_unlock(&phba->hbalock);
9983
9984         /*
9985          * Invokes slow-path host attention interrupt handling as appropriate.
9986          */
9987
9988         /* status of events with mailbox and link attention */
9989         status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
9990
9991         /* status of events with ELS ring */
9992         status2 = (phba->ha_copy & (HA_RXMASK  << (4*LPFC_ELS_RING)));
9993         status2 >>= (4*LPFC_ELS_RING);
9994
9995         if (status1 || (status2 & HA_RXMASK))
9996                 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
9997         else
9998                 sp_irq_rc = IRQ_NONE;
9999
10000         /*
10001          * Invoke fast-path host attention interrupt handling as appropriate.
10002          */
10003
10004         /* status of events with FCP ring */
10005         status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
10006         status1 >>= (4*LPFC_FCP_RING);
10007
10008         /* status of events with extra ring */
10009         if (phba->cfg_multi_ring_support == 2) {
10010                 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
10011                 status2 >>= (4*LPFC_EXTRA_RING);
10012         } else
10013                 status2 = 0;
10014
10015         if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
10016                 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
10017         else
10018                 fp_irq_rc = IRQ_NONE;
10019
10020         /* Return device-level interrupt handling status */
10021         return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
10022 }  /* lpfc_sli_intr_handler */
10023
10024 /**
10025  * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
10026  * @phba: pointer to lpfc hba data structure.
10027  *
10028  * This routine is invoked by the worker thread to process all the pending
10029  * SLI4 FCP abort XRI events.
10030  **/
10031 void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
10032 {
10033         struct lpfc_cq_event *cq_event;
10034
10035         /* First, declare the fcp xri abort event has been handled */
10036         spin_lock_irq(&phba->hbalock);
10037         phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
10038         spin_unlock_irq(&phba->hbalock);
10039         /* Now, handle all the fcp xri abort events */
10040         while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
10041                 /* Get the first event from the head of the event queue */
10042                 spin_lock_irq(&phba->hbalock);
10043                 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
10044                                  cq_event, struct lpfc_cq_event, list);
10045                 spin_unlock_irq(&phba->hbalock);
10046                 /* Notify aborted XRI for FCP work queue */
10047                 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
10048                 /* Free the event processed back to the free pool */
10049                 lpfc_sli4_cq_event_release(phba, cq_event);
10050         }
10051 }
10052
10053 /**
10054  * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
10055  * @phba: pointer to lpfc hba data structure.
10056  *
10057  * This routine is invoked by the worker thread to process all the pending
10058  * SLI4 els abort xri events.
10059  **/
10060 void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
10061 {
10062         struct lpfc_cq_event *cq_event;
10063
10064         /* First, declare the els xri abort event has been handled */
10065         spin_lock_irq(&phba->hbalock);
10066         phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
10067         spin_unlock_irq(&phba->hbalock);
10068         /* Now, handle all the els xri abort events */
10069         while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
10070                 /* Get the first event from the head of the event queue */
10071                 spin_lock_irq(&phba->hbalock);
10072                 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
10073                                  cq_event, struct lpfc_cq_event, list);
10074                 spin_unlock_irq(&phba->hbalock);
10075                 /* Notify aborted XRI for ELS work queue */
10076                 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
10077                 /* Free the event processed back to the free pool */
10078                 lpfc_sli4_cq_event_release(phba, cq_event);
10079         }
10080 }
10081
10082 /**
10083  * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
10084  * @phba: pointer to lpfc hba data structure
10085  * @pIocbIn: pointer to the rspiocbq
10086  * @pIocbOut: pointer to the cmdiocbq
10087  * @wcqe: pointer to the complete wcqe
10088  *
10089  * This routine transfers the fields of a command iocbq to a response iocbq
10090  * by copying all the IOCB fields from command iocbq and transferring the
10091  * completion status information from the complete wcqe.
10092  **/
10093 static void
10094 lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
10095                               struct lpfc_iocbq *pIocbIn,
10096                               struct lpfc_iocbq *pIocbOut,
10097                               struct lpfc_wcqe_complete *wcqe)
10098 {
10099         unsigned long iflags;
10100         size_t offset = offsetof(struct lpfc_iocbq, iocb);
10101
10102         memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
10103                sizeof(struct lpfc_iocbq) - offset);
10104         /* Map WCQE parameters into irspiocb parameters */
10105         pIocbIn->iocb.ulpStatus = bf_get(lpfc_wcqe_c_status, wcqe);
10106         if (pIocbOut->iocb_flag & LPFC_IO_FCP)
10107                 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
10108                         pIocbIn->iocb.un.fcpi.fcpi_parm =
10109                                         pIocbOut->iocb.un.fcpi.fcpi_parm -
10110                                         wcqe->total_data_placed;
10111                 else
10112                         pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
10113         else {
10114                 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
10115                 pIocbIn->iocb.un.genreq64.bdl.bdeSize = wcqe->total_data_placed;
10116         }
10117
10118         /* Pick up HBA exchange busy condition */
10119         if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
10120                 spin_lock_irqsave(&phba->hbalock, iflags);
10121                 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
10122                 spin_unlock_irqrestore(&phba->hbalock, iflags);
10123         }
10124 }
10125
10126 /**
10127  * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
10128  * @phba: Pointer to HBA context object.
10129  * @wcqe: Pointer to work-queue completion queue entry.
10130  *
10131  * This routine handles an ELS work-queue completion event and construct
10132  * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
10133  * discovery engine to handle.
10134  *
10135  * Return: Pointer to the receive IOCBQ, NULL otherwise.
10136  **/
10137 static struct lpfc_iocbq *
10138 lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
10139                                struct lpfc_iocbq *irspiocbq)
10140 {
10141         struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
10142         struct lpfc_iocbq *cmdiocbq;
10143         struct lpfc_wcqe_complete *wcqe;
10144         unsigned long iflags;
10145
10146         wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
10147         spin_lock_irqsave(&phba->hbalock, iflags);
10148         pring->stats.iocb_event++;
10149         /* Look up the ELS command IOCB and create pseudo response IOCB */
10150         cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
10151                                 bf_get(lpfc_wcqe_c_request_tag, wcqe));
10152         spin_unlock_irqrestore(&phba->hbalock, iflags);
10153
10154         if (unlikely(!cmdiocbq)) {
10155                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
10156                                 "0386 ELS complete with no corresponding "
10157                                 "cmdiocb: iotag (%d)\n",
10158                                 bf_get(lpfc_wcqe_c_request_tag, wcqe));
10159                 lpfc_sli_release_iocbq(phba, irspiocbq);
10160                 return NULL;
10161         }
10162
10163         /* Fake the irspiocbq and copy necessary response information */
10164         lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
10165
10166         return irspiocbq;
10167 }
10168
10169 /**
10170  * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
10171  * @phba: Pointer to HBA context object.
10172  * @cqe: Pointer to mailbox completion queue entry.
10173  *
10174  * This routine process a mailbox completion queue entry with asynchrous
10175  * event.
10176  *
10177  * Return: true if work posted to worker thread, otherwise false.
10178  **/
10179 static bool
10180 lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
10181 {
10182         struct lpfc_cq_event *cq_event;
10183         unsigned long iflags;
10184
10185         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
10186                         "0392 Async Event: word0:x%x, word1:x%x, "
10187                         "word2:x%x, word3:x%x\n", mcqe->word0,
10188                         mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
10189
10190         /* Allocate a new internal CQ_EVENT entry */
10191         cq_event = lpfc_sli4_cq_event_alloc(phba);
10192         if (!cq_event) {
10193                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10194                                 "0394 Failed to allocate CQ_EVENT entry\n");
10195                 return false;
10196         }
10197
10198         /* Move the CQE into an asynchronous event entry */
10199         memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
10200         spin_lock_irqsave(&phba->hbalock, iflags);
10201         list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
10202         /* Set the async event flag */
10203         phba->hba_flag |= ASYNC_EVENT;
10204         spin_unlock_irqrestore(&phba->hbalock, iflags);
10205
10206         return true;
10207 }
10208
10209 /**
10210  * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
10211  * @phba: Pointer to HBA context object.
10212  * @cqe: Pointer to mailbox completion queue entry.
10213  *
10214  * This routine process a mailbox completion queue entry with mailbox
10215  * completion event.
10216  *
10217  * Return: true if work posted to worker thread, otherwise false.
10218  **/
10219 static bool
10220 lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
10221 {
10222         uint32_t mcqe_status;
10223         MAILBOX_t *mbox, *pmbox;
10224         struct lpfc_mqe *mqe;
10225         struct lpfc_vport *vport;
10226         struct lpfc_nodelist *ndlp;
10227         struct lpfc_dmabuf *mp;
10228         unsigned long iflags;
10229         LPFC_MBOXQ_t *pmb;
10230         bool workposted = false;
10231         int rc;
10232
10233         /* If not a mailbox complete MCQE, out by checking mailbox consume */
10234         if (!bf_get(lpfc_trailer_completed, mcqe))
10235                 goto out_no_mqe_complete;
10236
10237         /* Get the reference to the active mbox command */
10238         spin_lock_irqsave(&phba->hbalock, iflags);
10239         pmb = phba->sli.mbox_active;
10240         if (unlikely(!pmb)) {
10241                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
10242                                 "1832 No pending MBOX command to handle\n");
10243                 spin_unlock_irqrestore(&phba->hbalock, iflags);
10244                 goto out_no_mqe_complete;
10245         }
10246         spin_unlock_irqrestore(&phba->hbalock, iflags);
10247         mqe = &pmb->u.mqe;
10248         pmbox = (MAILBOX_t *)&pmb->u.mqe;
10249         mbox = phba->mbox;
10250         vport = pmb->vport;
10251
10252         /* Reset heartbeat timer */
10253         phba->last_completion_time = jiffies;
10254         del_timer(&phba->sli.mbox_tmo);
10255
10256         /* Move mbox data to caller's mailbox region, do endian swapping */
10257         if (pmb->mbox_cmpl && mbox)
10258                 lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
10259         /* Set the mailbox status with SLI4 range 0x4000 */
10260         mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
10261         if (mcqe_status != MB_CQE_STATUS_SUCCESS)
10262                 bf_set(lpfc_mqe_status, mqe,
10263                        (LPFC_MBX_ERROR_RANGE | mcqe_status));
10264
10265         if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
10266                 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
10267                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
10268                                       "MBOX dflt rpi: status:x%x rpi:x%x",
10269                                       mcqe_status,
10270                                       pmbox->un.varWords[0], 0);
10271                 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
10272                         mp = (struct lpfc_dmabuf *)(pmb->context1);
10273                         ndlp = (struct lpfc_nodelist *)pmb->context2;
10274                         /* Reg_LOGIN of dflt RPI was successful. Now lets get
10275                          * RID of the PPI using the same mbox buffer.
10276                          */
10277                         lpfc_unreg_login(phba, vport->vpi,
10278                                          pmbox->un.varWords[0], pmb);
10279                         pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
10280                         pmb->context1 = mp;
10281                         pmb->context2 = ndlp;
10282                         pmb->vport = vport;
10283                         rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
10284                         if (rc != MBX_BUSY)
10285                                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
10286                                                 LOG_SLI, "0385 rc should "
10287                                                 "have been MBX_BUSY\n");
10288                         if (rc != MBX_NOT_FINISHED)
10289                                 goto send_current_mbox;
10290                 }
10291         }
10292         spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
10293         phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
10294         spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
10295
10296         /* There is mailbox completion work to do */
10297         spin_lock_irqsave(&phba->hbalock, iflags);
10298         __lpfc_mbox_cmpl_put(phba, pmb);
10299         phba->work_ha |= HA_MBATT;
10300         spin_unlock_irqrestore(&phba->hbalock, iflags);
10301         workposted = true;
10302
10303 send_current_mbox:
10304         spin_lock_irqsave(&phba->hbalock, iflags);
10305         /* Release the mailbox command posting token */
10306         phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
10307         /* Setting active mailbox pointer need to be in sync to flag clear */
10308         phba->sli.mbox_active = NULL;
10309         spin_unlock_irqrestore(&phba->hbalock, iflags);
10310         /* Wake up worker thread to post the next pending mailbox command */
10311         lpfc_worker_wake_up(phba);
10312 out_no_mqe_complete:
10313         if (bf_get(lpfc_trailer_consumed, mcqe))
10314                 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
10315         return workposted;
10316 }
10317
10318 /**
10319  * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
10320  * @phba: Pointer to HBA context object.
10321  * @cqe: Pointer to mailbox completion queue entry.
10322  *
10323  * This routine process a mailbox completion queue entry, it invokes the
10324  * proper mailbox complete handling or asynchrous event handling routine
10325  * according to the MCQE's async bit.
10326  *
10327  * Return: true if work posted to worker thread, otherwise false.
10328  **/
10329 static bool
10330 lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
10331 {
10332         struct lpfc_mcqe mcqe;
10333         bool workposted;
10334
10335         /* Copy the mailbox MCQE and convert endian order as needed */
10336         lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
10337
10338         /* Invoke the proper event handling routine */
10339         if (!bf_get(lpfc_trailer_async, &mcqe))
10340                 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
10341         else
10342                 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
10343         return workposted;
10344 }
10345
10346 /**
10347  * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
10348  * @phba: Pointer to HBA context object.
10349  * @wcqe: Pointer to work-queue completion queue entry.
10350  *
10351  * This routine handles an ELS work-queue completion event.
10352  *
10353  * Return: true if work posted to worker thread, otherwise false.
10354  **/
10355 static bool
10356 lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba,
10357                              struct lpfc_wcqe_complete *wcqe)
10358 {
10359         struct lpfc_iocbq *irspiocbq;
10360         unsigned long iflags;
10361         struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
10362
10363         /* Get an irspiocbq for later ELS response processing use */
10364         irspiocbq = lpfc_sli_get_iocbq(phba);
10365         if (!irspiocbq) {
10366                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10367                         "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
10368                         "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
10369                         pring->txq_cnt, phba->iocb_cnt,
10370                         phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt,
10371                         phba->sli.ring[LPFC_ELS_RING].txcmplq_cnt);
10372                 return false;
10373         }
10374
10375         /* Save off the slow-path queue event for work thread to process */
10376         memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
10377         spin_lock_irqsave(&phba->hbalock, iflags);
10378         list_add_tail(&irspiocbq->cq_event.list,
10379                       &phba->sli4_hba.sp_queue_event);
10380         phba->hba_flag |= HBA_SP_QUEUE_EVT;
10381         spin_unlock_irqrestore(&phba->hbalock, iflags);
10382
10383         return true;
10384 }
10385
10386 /**
10387  * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
10388  * @phba: Pointer to HBA context object.
10389  * @wcqe: Pointer to work-queue completion queue entry.
10390  *
10391  * This routine handles slow-path WQ entry comsumed event by invoking the
10392  * proper WQ release routine to the slow-path WQ.
10393  **/
10394 static void
10395 lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
10396                              struct lpfc_wcqe_release *wcqe)
10397 {
10398         /* Check for the slow-path ELS work queue */
10399         if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
10400                 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
10401                                      bf_get(lpfc_wcqe_r_wqe_index, wcqe));
10402         else
10403                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
10404                                 "2579 Slow-path wqe consume event carries "
10405                                 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
10406                                 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
10407                                 phba->sli4_hba.els_wq->queue_id);
10408 }
10409
10410 /**
10411  * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
10412  * @phba: Pointer to HBA context object.
10413  * @cq: Pointer to a WQ completion queue.
10414  * @wcqe: Pointer to work-queue completion queue entry.
10415  *
10416  * This routine handles an XRI abort event.
10417  *
10418  * Return: true if work posted to worker thread, otherwise false.
10419  **/
10420 static bool
10421 lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
10422                                    struct lpfc_queue *cq,
10423                                    struct sli4_wcqe_xri_aborted *wcqe)
10424 {
10425         bool workposted = false;
10426         struct lpfc_cq_event *cq_event;
10427         unsigned long iflags;
10428
10429         /* Allocate a new internal CQ_EVENT entry */
10430         cq_event = lpfc_sli4_cq_event_alloc(phba);
10431         if (!cq_event) {
10432                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10433                                 "0602 Failed to allocate CQ_EVENT entry\n");
10434                 return false;
10435         }
10436
10437         /* Move the CQE into the proper xri abort event list */
10438         memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
10439         switch (cq->subtype) {
10440         case LPFC_FCP:
10441                 spin_lock_irqsave(&phba->hbalock, iflags);
10442                 list_add_tail(&cq_event->list,
10443                               &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
10444                 /* Set the fcp xri abort event flag */
10445                 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
10446                 spin_unlock_irqrestore(&phba->hbalock, iflags);
10447                 workposted = true;
10448                 break;
10449         case LPFC_ELS:
10450                 spin_lock_irqsave(&phba->hbalock, iflags);
10451                 list_add_tail(&cq_event->list,
10452                               &phba->sli4_hba.sp_els_xri_aborted_work_queue);
10453                 /* Set the els xri abort event flag */
10454                 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
10455                 spin_unlock_irqrestore(&phba->hbalock, iflags);
10456                 workposted = true;
10457                 break;
10458         default:
10459                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10460                                 "0603 Invalid work queue CQE subtype (x%x)\n",
10461                                 cq->subtype);
10462                 workposted = false;
10463                 break;
10464         }
10465         return workposted;
10466 }
10467
10468 /**
10469  * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
10470  * @phba: Pointer to HBA context object.
10471  * @rcqe: Pointer to receive-queue completion queue entry.
10472  *
10473  * This routine process a receive-queue completion queue entry.
10474  *
10475  * Return: true if work posted to worker thread, otherwise false.
10476  **/
10477 static bool
10478 lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
10479 {
10480         bool workposted = false;
10481         struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
10482         struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
10483         struct hbq_dmabuf *dma_buf;
10484         uint32_t status;
10485         unsigned long iflags;
10486
10487         if (bf_get(lpfc_rcqe_rq_id, rcqe) != hrq->queue_id)
10488                 goto out;
10489
10490         status = bf_get(lpfc_rcqe_status, rcqe);
10491         switch (status) {
10492         case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
10493                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10494                                 "2537 Receive Frame Truncated!!\n");
10495         case FC_STATUS_RQ_SUCCESS:
10496                 lpfc_sli4_rq_release(hrq, drq);
10497                 spin_lock_irqsave(&phba->hbalock, iflags);
10498                 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
10499                 if (!dma_buf) {
10500                         spin_unlock_irqrestore(&phba->hbalock, iflags);
10501                         goto out;
10502                 }
10503                 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
10504                 /* save off the frame for the word thread to process */
10505                 list_add_tail(&dma_buf->cq_event.list,
10506                               &phba->sli4_hba.sp_queue_event);
10507                 /* Frame received */
10508                 phba->hba_flag |= HBA_SP_QUEUE_EVT;
10509                 spin_unlock_irqrestore(&phba->hbalock, iflags);
10510                 workposted = true;
10511                 break;
10512         case FC_STATUS_INSUFF_BUF_NEED_BUF:
10513         case FC_STATUS_INSUFF_BUF_FRM_DISC:
10514                 /* Post more buffers if possible */
10515                 spin_lock_irqsave(&phba->hbalock, iflags);
10516                 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
10517                 spin_unlock_irqrestore(&phba->hbalock, iflags);
10518                 workposted = true;
10519                 break;
10520         }
10521 out:
10522         return workposted;
10523 }
10524
10525 /**
10526  * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
10527  * @phba: Pointer to HBA context object.
10528  * @cq: Pointer to the completion queue.
10529  * @wcqe: Pointer to a completion queue entry.
10530  *
10531  * This routine process a slow-path work-queue or receive queue completion queue
10532  * entry.
10533  *
10534  * Return: true if work posted to worker thread, otherwise false.
10535  **/
10536 static bool
10537 lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
10538                          struct lpfc_cqe *cqe)
10539 {
10540         struct lpfc_cqe cqevt;
10541         bool workposted = false;
10542
10543         /* Copy the work queue CQE and convert endian order if needed */
10544         lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
10545
10546         /* Check and process for different type of WCQE and dispatch */
10547         switch (bf_get(lpfc_cqe_code, &cqevt)) {
10548         case CQE_CODE_COMPL_WQE:
10549                 /* Process the WQ/RQ complete event */
10550                 phba->last_completion_time = jiffies;
10551                 workposted = lpfc_sli4_sp_handle_els_wcqe(phba,
10552                                 (struct lpfc_wcqe_complete *)&cqevt);
10553                 break;
10554         case CQE_CODE_RELEASE_WQE:
10555                 /* Process the WQ release event */
10556                 lpfc_sli4_sp_handle_rel_wcqe(phba,
10557                                 (struct lpfc_wcqe_release *)&cqevt);
10558                 break;
10559         case CQE_CODE_XRI_ABORTED:
10560                 /* Process the WQ XRI abort event */
10561                 phba->last_completion_time = jiffies;
10562                 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
10563                                 (struct sli4_wcqe_xri_aborted *)&cqevt);
10564                 break;
10565         case CQE_CODE_RECEIVE:
10566                 /* Process the RQ event */
10567                 phba->last_completion_time = jiffies;
10568                 workposted = lpfc_sli4_sp_handle_rcqe(phba,
10569                                 (struct lpfc_rcqe *)&cqevt);
10570                 break;
10571         default:
10572                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10573                                 "0388 Not a valid WCQE code: x%x\n",
10574                                 bf_get(lpfc_cqe_code, &cqevt));
10575                 break;
10576         }
10577         return workposted;
10578 }
10579
10580 /**
10581  * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
10582  * @phba: Pointer to HBA context object.
10583  * @eqe: Pointer to fast-path event queue entry.
10584  *
10585  * This routine process a event queue entry from the slow-path event queue.
10586  * It will check the MajorCode and MinorCode to determine this is for a
10587  * completion event on a completion queue, if not, an error shall be logged
10588  * and just return. Otherwise, it will get to the corresponding completion
10589  * queue and process all the entries on that completion queue, rearm the
10590  * completion queue, and then return.
10591  *
10592  **/
10593 static void
10594 lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
10595 {
10596         struct lpfc_queue *cq = NULL, *childq, *speq;
10597         struct lpfc_cqe *cqe;
10598         bool workposted = false;
10599         int ecount = 0;
10600         uint16_t cqid;
10601
10602         if (bf_get_le32(lpfc_eqe_major_code, eqe) != 0) {
10603                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10604                                 "0359 Not a valid slow-path completion "
10605                                 "event: majorcode=x%x, minorcode=x%x\n",
10606                                 bf_get_le32(lpfc_eqe_major_code, eqe),
10607                                 bf_get_le32(lpfc_eqe_minor_code, eqe));
10608                 return;
10609         }
10610
10611         /* Get the reference to the corresponding CQ */
10612         cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
10613
10614         /* Search for completion queue pointer matching this cqid */
10615         speq = phba->sli4_hba.sp_eq;
10616         list_for_each_entry(childq, &speq->child_list, list) {
10617                 if (childq->queue_id == cqid) {
10618                         cq = childq;
10619                         break;
10620                 }
10621         }
10622         if (unlikely(!cq)) {
10623                 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
10624                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10625                                         "0365 Slow-path CQ identifier "
10626                                         "(%d) does not exist\n", cqid);
10627                 return;
10628         }
10629
10630         /* Process all the entries to the CQ */
10631         switch (cq->type) {
10632         case LPFC_MCQ:
10633                 while ((cqe = lpfc_sli4_cq_get(cq))) {
10634                         workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
10635                         if (!(++ecount % LPFC_GET_QE_REL_INT))
10636                                 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
10637                 }
10638                 break;
10639         case LPFC_WCQ:
10640                 while ((cqe = lpfc_sli4_cq_get(cq))) {
10641                         if (cq->subtype == LPFC_FCP)
10642                                 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq,
10643                                                                        cqe);
10644                         else
10645                                 workposted |= lpfc_sli4_sp_handle_cqe(phba, cq,
10646                                                                       cqe);
10647                         if (!(++ecount % LPFC_GET_QE_REL_INT))
10648                                 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
10649                 }
10650                 break;
10651         default:
10652                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10653                                 "0370 Invalid completion queue type (%d)\n",
10654                                 cq->type);
10655                 return;
10656         }
10657
10658         /* Catch the no cq entry condition, log an error */
10659         if (unlikely(ecount == 0))
10660                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10661                                 "0371 No entry from the CQ: identifier "
10662                                 "(x%x), type (%d)\n", cq->queue_id, cq->type);
10663
10664         /* In any case, flash and re-arm the RCQ */
10665         lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
10666
10667         /* wake up worker thread if there are works to be done */
10668         if (workposted)
10669                 lpfc_worker_wake_up(phba);
10670 }
10671
10672 /**
10673  * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
10674  * @eqe: Pointer to fast-path completion queue entry.
10675  *
10676  * This routine process a fast-path work queue completion entry from fast-path
10677  * event queue for FCP command response completion.
10678  **/
10679 static void
10680 lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba,
10681                              struct lpfc_wcqe_complete *wcqe)
10682 {
10683         struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
10684         struct lpfc_iocbq *cmdiocbq;
10685         struct lpfc_iocbq irspiocbq;
10686         unsigned long iflags;
10687
10688         spin_lock_irqsave(&phba->hbalock, iflags);
10689         pring->stats.iocb_event++;
10690         spin_unlock_irqrestore(&phba->hbalock, iflags);
10691
10692         /* Check for response status */
10693         if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
10694                 /* If resource errors reported from HBA, reduce queue
10695                  * depth of the SCSI device.
10696                  */
10697                 if ((bf_get(lpfc_wcqe_c_status, wcqe) ==
10698                      IOSTAT_LOCAL_REJECT) &&
10699                     (wcqe->parameter == IOERR_NO_RESOURCES)) {
10700                         phba->lpfc_rampdown_queue_depth(phba);
10701                 }
10702                 /* Log the error status */
10703                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
10704                                 "0373 FCP complete error: status=x%x, "
10705                                 "hw_status=x%x, total_data_specified=%d, "
10706                                 "parameter=x%x, word3=x%x\n",
10707                                 bf_get(lpfc_wcqe_c_status, wcqe),
10708                                 bf_get(lpfc_wcqe_c_hw_status, wcqe),
10709                                 wcqe->total_data_placed, wcqe->parameter,
10710                                 wcqe->word3);
10711         }
10712
10713         /* Look up the FCP command IOCB and create pseudo response IOCB */
10714         spin_lock_irqsave(&phba->hbalock, iflags);
10715         cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
10716                                 bf_get(lpfc_wcqe_c_request_tag, wcqe));
10717         spin_unlock_irqrestore(&phba->hbalock, iflags);
10718         if (unlikely(!cmdiocbq)) {
10719                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
10720                                 "0374 FCP complete with no corresponding "
10721                                 "cmdiocb: iotag (%d)\n",
10722                                 bf_get(lpfc_wcqe_c_request_tag, wcqe));
10723                 return;
10724         }
10725         if (unlikely(!cmdiocbq->iocb_cmpl)) {
10726                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
10727                                 "0375 FCP cmdiocb not callback function "
10728                                 "iotag: (%d)\n",
10729                                 bf_get(lpfc_wcqe_c_request_tag, wcqe));
10730                 return;
10731         }
10732
10733         /* Fake the irspiocb and copy necessary response information */
10734         lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
10735
10736         if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
10737                 spin_lock_irqsave(&phba->hbalock, iflags);
10738                 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
10739                 spin_unlock_irqrestore(&phba->hbalock, iflags);
10740         }
10741
10742         /* Pass the cmd_iocb and the rsp state to the upper layer */
10743         (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
10744 }
10745
10746 /**
10747  * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
10748  * @phba: Pointer to HBA context object.
10749  * @cq: Pointer to completion queue.
10750  * @wcqe: Pointer to work-queue completion queue entry.
10751  *
10752  * This routine handles an fast-path WQ entry comsumed event by invoking the
10753  * proper WQ release routine to the slow-path WQ.
10754  **/
10755 static void
10756 lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
10757                              struct lpfc_wcqe_release *wcqe)
10758 {
10759         struct lpfc_queue *childwq;
10760         bool wqid_matched = false;
10761         uint16_t fcp_wqid;
10762
10763         /* Check for fast-path FCP work queue release */
10764         fcp_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
10765         list_for_each_entry(childwq, &cq->child_list, list) {
10766                 if (childwq->queue_id == fcp_wqid) {
10767                         lpfc_sli4_wq_release(childwq,
10768                                         bf_get(lpfc_wcqe_r_wqe_index, wcqe));
10769                         wqid_matched = true;
10770                         break;
10771                 }
10772         }
10773         /* Report warning log message if no match found */
10774         if (wqid_matched != true)
10775                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
10776                                 "2580 Fast-path wqe consume event carries "
10777                                 "miss-matched qid: wcqe-qid=x%x\n", fcp_wqid);
10778 }
10779
10780 /**
10781  * lpfc_sli4_fp_handle_wcqe - Process fast-path work queue completion entry
10782  * @cq: Pointer to the completion queue.
10783  * @eqe: Pointer to fast-path completion queue entry.
10784  *
10785  * This routine process a fast-path work queue completion entry from fast-path
10786  * event queue for FCP command response completion.
10787  **/
10788 static int
10789 lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
10790                          struct lpfc_cqe *cqe)
10791 {
10792         struct lpfc_wcqe_release wcqe;
10793         bool workposted = false;
10794
10795         /* Copy the work queue CQE and convert endian order if needed */
10796         lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
10797
10798         /* Check and process for different type of WCQE and dispatch */
10799         switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
10800         case CQE_CODE_COMPL_WQE:
10801                 /* Process the WQ complete event */
10802                 phba->last_completion_time = jiffies;
10803                 lpfc_sli4_fp_handle_fcp_wcqe(phba,
10804                                 (struct lpfc_wcqe_complete *)&wcqe);
10805                 break;
10806         case CQE_CODE_RELEASE_WQE:
10807                 /* Process the WQ release event */
10808                 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
10809                                 (struct lpfc_wcqe_release *)&wcqe);
10810                 break;
10811         case CQE_CODE_XRI_ABORTED:
10812                 /* Process the WQ XRI abort event */
10813                 phba->last_completion_time = jiffies;
10814                 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
10815                                 (struct sli4_wcqe_xri_aborted *)&wcqe);
10816                 break;
10817         default:
10818                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10819                                 "0144 Not a valid WCQE code: x%x\n",
10820                                 bf_get(lpfc_wcqe_c_code, &wcqe));
10821                 break;
10822         }
10823         return workposted;
10824 }
10825
10826 /**
10827  * lpfc_sli4_fp_handle_eqe - Process a fast-path event queue entry
10828  * @phba: Pointer to HBA context object.
10829  * @eqe: Pointer to fast-path event queue entry.
10830  *
10831  * This routine process a event queue entry from the fast-path event queue.
10832  * It will check the MajorCode and MinorCode to determine this is for a
10833  * completion event on a completion queue, if not, an error shall be logged
10834  * and just return. Otherwise, it will get to the corresponding completion
10835  * queue and process all the entries on the completion queue, rearm the
10836  * completion queue, and then return.
10837  **/
10838 static void
10839 lpfc_sli4_fp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
10840                         uint32_t fcp_cqidx)
10841 {
10842         struct lpfc_queue *cq;
10843         struct lpfc_cqe *cqe;
10844         bool workposted = false;
10845         uint16_t cqid;
10846         int ecount = 0;
10847
10848         if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
10849                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10850                                 "0366 Not a valid fast-path completion "
10851                                 "event: majorcode=x%x, minorcode=x%x\n",
10852                                 bf_get_le32(lpfc_eqe_major_code, eqe),
10853                                 bf_get_le32(lpfc_eqe_minor_code, eqe));
10854                 return;
10855         }
10856
10857         cq = phba->sli4_hba.fcp_cq[fcp_cqidx];
10858         if (unlikely(!cq)) {
10859                 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
10860                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10861                                         "0367 Fast-path completion queue "
10862                                         "does not exist\n");
10863                 return;
10864         }
10865
10866         /* Get the reference to the corresponding CQ */
10867         cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
10868         if (unlikely(cqid != cq->queue_id)) {
10869                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10870                                 "0368 Miss-matched fast-path completion "
10871                                 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
10872                                 cqid, cq->queue_id);
10873                 return;
10874         }
10875
10876         /* Process all the entries to the CQ */
10877         while ((cqe = lpfc_sli4_cq_get(cq))) {
10878                 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
10879                 if (!(++ecount % LPFC_GET_QE_REL_INT))
10880                         lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
10881         }
10882
10883         /* Catch the no cq entry condition */
10884         if (unlikely(ecount == 0))
10885                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10886                                 "0369 No entry from fast-path completion "
10887                                 "queue fcpcqid=%d\n", cq->queue_id);
10888
10889         /* In any case, flash and re-arm the CQ */
10890         lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
10891
10892         /* wake up worker thread if there are works to be done */
10893         if (workposted)
10894                 lpfc_worker_wake_up(phba);
10895 }
10896
10897 static void
10898 lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
10899 {
10900         struct lpfc_eqe *eqe;
10901
10902         /* walk all the EQ entries and drop on the floor */
10903         while ((eqe = lpfc_sli4_eq_get(eq)))
10904                 ;
10905
10906         /* Clear and re-arm the EQ */
10907         lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
10908 }
10909
10910 /**
10911  * lpfc_sli4_sp_intr_handler - Slow-path interrupt handler to SLI-4 device
10912  * @irq: Interrupt number.
10913  * @dev_id: The device context pointer.
10914  *
10915  * This function is directly called from the PCI layer as an interrupt
10916  * service routine when device with SLI-4 interface spec is enabled with
10917  * MSI-X multi-message interrupt mode and there are slow-path events in
10918  * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
10919  * interrupt mode, this function is called as part of the device-level
10920  * interrupt handler. When the PCI slot is in error recovery or the HBA is
10921  * undergoing initialization, the interrupt handler will not process the
10922  * interrupt. The link attention and ELS ring attention events are handled
10923  * by the worker thread. The interrupt handler signals the worker thread
10924  * and returns for these events. This function is called without any lock
10925  * held. It gets the hbalock to access and update SLI data structures.
10926  *
10927  * This function returns IRQ_HANDLED when interrupt is handled else it
10928  * returns IRQ_NONE.
10929  **/
10930 irqreturn_t
10931 lpfc_sli4_sp_intr_handler(int irq, void *dev_id)
10932 {
10933         struct lpfc_hba *phba;
10934         struct lpfc_queue *speq;
10935         struct lpfc_eqe *eqe;
10936         unsigned long iflag;
10937         int ecount = 0;
10938
10939         /*
10940          * Get the driver's phba structure from the dev_id
10941          */
10942         phba = (struct lpfc_hba *)dev_id;
10943
10944         if (unlikely(!phba))
10945                 return IRQ_NONE;
10946
10947         /* Get to the EQ struct associated with this vector */
10948         speq = phba->sli4_hba.sp_eq;
10949
10950         /* Check device state for handling interrupt */
10951         if (unlikely(lpfc_intr_state_check(phba))) {
10952                 /* Check again for link_state with lock held */
10953                 spin_lock_irqsave(&phba->hbalock, iflag);
10954                 if (phba->link_state < LPFC_LINK_DOWN)
10955                         /* Flush, clear interrupt, and rearm the EQ */
10956                         lpfc_sli4_eq_flush(phba, speq);
10957                 spin_unlock_irqrestore(&phba->hbalock, iflag);
10958                 return IRQ_NONE;
10959         }
10960
10961         /*
10962          * Process all the event on FCP slow-path EQ
10963          */
10964         while ((eqe = lpfc_sli4_eq_get(speq))) {
10965                 lpfc_sli4_sp_handle_eqe(phba, eqe);
10966                 if (!(++ecount % LPFC_GET_QE_REL_INT))
10967                         lpfc_sli4_eq_release(speq, LPFC_QUEUE_NOARM);
10968         }
10969
10970         /* Always clear and re-arm the slow-path EQ */
10971         lpfc_sli4_eq_release(speq, LPFC_QUEUE_REARM);
10972
10973         /* Catch the no cq entry condition */
10974         if (unlikely(ecount == 0)) {
10975                 if (phba->intr_type == MSIX)
10976                         /* MSI-X treated interrupt served as no EQ share INT */
10977                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
10978                                         "0357 MSI-X interrupt with no EQE\n");
10979                 else
10980                         /* Non MSI-X treated on interrupt as EQ share INT */
10981                         return IRQ_NONE;
10982         }
10983
10984         return IRQ_HANDLED;
10985 } /* lpfc_sli4_sp_intr_handler */
10986
10987 /**
10988  * lpfc_sli4_fp_intr_handler - Fast-path interrupt handler to SLI-4 device
10989  * @irq: Interrupt number.
10990  * @dev_id: The device context pointer.
10991  *
10992  * This function is directly called from the PCI layer as an interrupt
10993  * service routine when device with SLI-4 interface spec is enabled with
10994  * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
10995  * ring event in the HBA. However, when the device is enabled with either
10996  * MSI or Pin-IRQ interrupt mode, this function is called as part of the
10997  * device-level interrupt handler. When the PCI slot is in error recovery
10998  * or the HBA is undergoing initialization, the interrupt handler will not
10999  * process the interrupt. The SCSI FCP fast-path ring event are handled in
11000  * the intrrupt context. This function is called without any lock held.
11001  * It gets the hbalock to access and update SLI data structures. Note that,
11002  * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
11003  * equal to that of FCP CQ index.
11004  *
11005  * This function returns IRQ_HANDLED when interrupt is handled else it
11006  * returns IRQ_NONE.
11007  **/
11008 irqreturn_t
11009 lpfc_sli4_fp_intr_handler(int irq, void *dev_id)
11010 {
11011         struct lpfc_hba *phba;
11012         struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
11013         struct lpfc_queue *fpeq;
11014         struct lpfc_eqe *eqe;
11015         unsigned long iflag;
11016         int ecount = 0;
11017         uint32_t fcp_eqidx;
11018
11019         /* Get the driver's phba structure from the dev_id */
11020         fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
11021         phba = fcp_eq_hdl->phba;
11022         fcp_eqidx = fcp_eq_hdl->idx;
11023
11024         if (unlikely(!phba))
11025                 return IRQ_NONE;
11026
11027         /* Get to the EQ struct associated with this vector */
11028         fpeq = phba->sli4_hba.fp_eq[fcp_eqidx];
11029
11030         /* Check device state for handling interrupt */
11031         if (unlikely(lpfc_intr_state_check(phba))) {
11032                 /* Check again for link_state with lock held */
11033                 spin_lock_irqsave(&phba->hbalock, iflag);
11034                 if (phba->link_state < LPFC_LINK_DOWN)
11035                         /* Flush, clear interrupt, and rearm the EQ */
11036                         lpfc_sli4_eq_flush(phba, fpeq);
11037                 spin_unlock_irqrestore(&phba->hbalock, iflag);
11038                 return IRQ_NONE;
11039         }
11040
11041         /*
11042          * Process all the event on FCP fast-path EQ
11043          */
11044         while ((eqe = lpfc_sli4_eq_get(fpeq))) {
11045                 lpfc_sli4_fp_handle_eqe(phba, eqe, fcp_eqidx);
11046                 if (!(++ecount % LPFC_GET_QE_REL_INT))
11047                         lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
11048         }
11049
11050         /* Always clear and re-arm the fast-path EQ */
11051         lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
11052
11053         if (unlikely(ecount == 0)) {
11054                 if (phba->intr_type == MSIX)
11055                         /* MSI-X treated interrupt served as no EQ share INT */
11056                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11057                                         "0358 MSI-X interrupt with no EQE\n");
11058                 else
11059                         /* Non MSI-X treated on interrupt as EQ share INT */
11060                         return IRQ_NONE;
11061         }
11062
11063         return IRQ_HANDLED;
11064 } /* lpfc_sli4_fp_intr_handler */
11065
11066 /**
11067  * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
11068  * @irq: Interrupt number.
11069  * @dev_id: The device context pointer.
11070  *
11071  * This function is the device-level interrupt handler to device with SLI-4
11072  * interface spec, called from the PCI layer when either MSI or Pin-IRQ
11073  * interrupt mode is enabled and there is an event in the HBA which requires
11074  * driver attention. This function invokes the slow-path interrupt attention
11075  * handling function and fast-path interrupt attention handling function in
11076  * turn to process the relevant HBA attention events. This function is called
11077  * without any lock held. It gets the hbalock to access and update SLI data
11078  * structures.
11079  *
11080  * This function returns IRQ_HANDLED when interrupt is handled, else it
11081  * returns IRQ_NONE.
11082  **/
11083 irqreturn_t
11084 lpfc_sli4_intr_handler(int irq, void *dev_id)
11085 {
11086         struct lpfc_hba  *phba;
11087         irqreturn_t sp_irq_rc, fp_irq_rc;
11088         bool fp_handled = false;
11089         uint32_t fcp_eqidx;
11090
11091         /* Get the driver's phba structure from the dev_id */
11092         phba = (struct lpfc_hba *)dev_id;
11093
11094         if (unlikely(!phba))
11095                 return IRQ_NONE;
11096
11097         /*
11098          * Invokes slow-path host attention interrupt handling as appropriate.
11099          */
11100         sp_irq_rc = lpfc_sli4_sp_intr_handler(irq, dev_id);
11101
11102         /*
11103          * Invoke fast-path host attention interrupt handling as appropriate.
11104          */
11105         for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
11106                 fp_irq_rc = lpfc_sli4_fp_intr_handler(irq,
11107                                         &phba->sli4_hba.fcp_eq_hdl[fcp_eqidx]);
11108                 if (fp_irq_rc == IRQ_HANDLED)
11109                         fp_handled |= true;
11110         }
11111
11112         return (fp_handled == true) ? IRQ_HANDLED : sp_irq_rc;
11113 } /* lpfc_sli4_intr_handler */
11114
11115 /**
11116  * lpfc_sli4_queue_free - free a queue structure and associated memory
11117  * @queue: The queue structure to free.
11118  *
11119  * This function frees a queue structure and the DMAable memory used for
11120  * the host resident queue. This function must be called after destroying the
11121  * queue on the HBA.
11122  **/
11123 void
11124 lpfc_sli4_queue_free(struct lpfc_queue *queue)
11125 {
11126         struct lpfc_dmabuf *dmabuf;
11127
11128         if (!queue)
11129                 return;
11130
11131         while (!list_empty(&queue->page_list)) {
11132                 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
11133                                  list);
11134                 dma_free_coherent(&queue->phba->pcidev->dev, SLI4_PAGE_SIZE,
11135                                   dmabuf->virt, dmabuf->phys);
11136                 kfree(dmabuf);
11137         }
11138         kfree(queue);
11139         return;
11140 }
11141
11142 /**
11143  * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
11144  * @phba: The HBA that this queue is being created on.
11145  * @entry_size: The size of each queue entry for this queue.
11146  * @entry count: The number of entries that this queue will handle.
11147  *
11148  * This function allocates a queue structure and the DMAable memory used for
11149  * the host resident queue. This function must be called before creating the
11150  * queue on the HBA.
11151  **/
11152 struct lpfc_queue *
11153 lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
11154                       uint32_t entry_count)
11155 {
11156         struct lpfc_queue *queue;
11157         struct lpfc_dmabuf *dmabuf;
11158         int x, total_qe_count;
11159         void *dma_pointer;
11160         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
11161
11162         if (!phba->sli4_hba.pc_sli4_params.supported)
11163                 hw_page_size = SLI4_PAGE_SIZE;
11164
11165         queue = kzalloc(sizeof(struct lpfc_queue) +
11166                         (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
11167         if (!queue)
11168                 return NULL;
11169         queue->page_count = (ALIGN(entry_size * entry_count,
11170                         hw_page_size))/hw_page_size;
11171         INIT_LIST_HEAD(&queue->list);
11172         INIT_LIST_HEAD(&queue->page_list);
11173         INIT_LIST_HEAD(&queue->child_list);
11174         for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
11175                 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
11176                 if (!dmabuf)
11177                         goto out_fail;
11178                 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
11179                                                   hw_page_size, &dmabuf->phys,
11180                                                   GFP_KERNEL);
11181                 if (!dmabuf->virt) {
11182                         kfree(dmabuf);
11183                         goto out_fail;
11184                 }
11185                 memset(dmabuf->virt, 0, hw_page_size);
11186                 dmabuf->buffer_tag = x;
11187                 list_add_tail(&dmabuf->list, &queue->page_list);
11188                 /* initialize queue's entry array */
11189                 dma_pointer = dmabuf->virt;
11190                 for (; total_qe_count < entry_count &&
11191                      dma_pointer < (hw_page_size + dmabuf->virt);
11192                      total_qe_count++, dma_pointer += entry_size) {
11193                         queue->qe[total_qe_count].address = dma_pointer;
11194                 }
11195         }
11196         queue->entry_size = entry_size;
11197         queue->entry_count = entry_count;
11198         queue->phba = phba;
11199
11200         return queue;
11201 out_fail:
11202         lpfc_sli4_queue_free(queue);
11203         return NULL;
11204 }
11205
11206 /**
11207  * lpfc_eq_create - Create an Event Queue on the HBA
11208  * @phba: HBA structure that indicates port to create a queue on.
11209  * @eq: The queue structure to use to create the event queue.
11210  * @imax: The maximum interrupt per second limit.
11211  *
11212  * This function creates an event queue, as detailed in @eq, on a port,
11213  * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
11214  *
11215  * The @phba struct is used to send mailbox command to HBA. The @eq struct
11216  * is used to get the entry count and entry size that are necessary to
11217  * determine the number of pages to allocate and use for this queue. This
11218  * function will send the EQ_CREATE mailbox command to the HBA to setup the
11219  * event queue. This function is asynchronous and will wait for the mailbox
11220  * command to finish before continuing.
11221  *
11222  * On success this function will return a zero. If unable to allocate enough
11223  * memory this function will return -ENOMEM. If the queue create mailbox command
11224  * fails this function will return -ENXIO.
11225  **/
11226 uint32_t
11227 lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint16_t imax)
11228 {
11229         struct lpfc_mbx_eq_create *eq_create;
11230         LPFC_MBOXQ_t *mbox;
11231         int rc, length, status = 0;
11232         struct lpfc_dmabuf *dmabuf;
11233         uint32_t shdr_status, shdr_add_status;
11234         union lpfc_sli4_cfg_shdr *shdr;
11235         uint16_t dmult;
11236         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
11237
11238         if (!phba->sli4_hba.pc_sli4_params.supported)
11239                 hw_page_size = SLI4_PAGE_SIZE;
11240
11241         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11242         if (!mbox)
11243                 return -ENOMEM;
11244         length = (sizeof(struct lpfc_mbx_eq_create) -
11245                   sizeof(struct lpfc_sli4_cfg_mhdr));
11246         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
11247                          LPFC_MBOX_OPCODE_EQ_CREATE,
11248                          length, LPFC_SLI4_MBX_EMBED);
11249         eq_create = &mbox->u.mqe.un.eq_create;
11250         bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
11251                eq->page_count);
11252         bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
11253                LPFC_EQE_SIZE);
11254         bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
11255         /* Calculate delay multiper from maximum interrupt per second */
11256         dmult = LPFC_DMULT_CONST/imax - 1;
11257         bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
11258                dmult);
11259         switch (eq->entry_count) {
11260         default:
11261                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11262                                 "0360 Unsupported EQ count. (%d)\n",
11263                                 eq->entry_count);
11264                 if (eq->entry_count < 256)
11265                         return -EINVAL;
11266                 /* otherwise default to smallest count (drop through) */
11267         case 256:
11268                 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
11269                        LPFC_EQ_CNT_256);
11270                 break;
11271         case 512:
11272                 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
11273                        LPFC_EQ_CNT_512);
11274                 break;
11275         case 1024:
11276                 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
11277                        LPFC_EQ_CNT_1024);
11278                 break;
11279         case 2048:
11280                 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
11281                        LPFC_EQ_CNT_2048);
11282                 break;
11283         case 4096:
11284                 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
11285                        LPFC_EQ_CNT_4096);
11286                 break;
11287         }
11288         list_for_each_entry(dmabuf, &eq->page_list, list) {
11289                 memset(dmabuf->virt, 0, hw_page_size);
11290                 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
11291                                         putPaddrLow(dmabuf->phys);
11292                 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
11293                                         putPaddrHigh(dmabuf->phys);
11294         }
11295         mbox->vport = phba->pport;
11296         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
11297         mbox->context1 = NULL;
11298         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
11299         shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
11300         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11301         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11302         if (shdr_status || shdr_add_status || rc) {
11303                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11304                                 "2500 EQ_CREATE mailbox failed with "
11305                                 "status x%x add_status x%x, mbx status x%x\n",
11306                                 shdr_status, shdr_add_status, rc);
11307                 status = -ENXIO;
11308         }
11309         eq->type = LPFC_EQ;
11310         eq->subtype = LPFC_NONE;
11311         eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
11312         if (eq->queue_id == 0xFFFF)
11313                 status = -ENXIO;
11314         eq->host_index = 0;
11315         eq->hba_index = 0;
11316
11317         mempool_free(mbox, phba->mbox_mem_pool);
11318         return status;
11319 }
11320
11321 /**
11322  * lpfc_cq_create - Create a Completion Queue on the HBA
11323  * @phba: HBA structure that indicates port to create a queue on.
11324  * @cq: The queue structure to use to create the completion queue.
11325  * @eq: The event queue to bind this completion queue to.
11326  *
11327  * This function creates a completion queue, as detailed in @wq, on a port,
11328  * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
11329  *
11330  * The @phba struct is used to send mailbox command to HBA. The @cq struct
11331  * is used to get the entry count and entry size that are necessary to
11332  * determine the number of pages to allocate and use for this queue. The @eq
11333  * is used to indicate which event queue to bind this completion queue to. This
11334  * function will send the CQ_CREATE mailbox command to the HBA to setup the
11335  * completion queue. This function is asynchronous and will wait for the mailbox
11336  * command to finish before continuing.
11337  *
11338  * On success this function will return a zero. If unable to allocate enough
11339  * memory this function will return -ENOMEM. If the queue create mailbox command
11340  * fails this function will return -ENXIO.
11341  **/
11342 uint32_t
11343 lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
11344                struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
11345 {
11346         struct lpfc_mbx_cq_create *cq_create;
11347         struct lpfc_dmabuf *dmabuf;
11348         LPFC_MBOXQ_t *mbox;
11349         int rc, length, status = 0;
11350         uint32_t shdr_status, shdr_add_status;
11351         union lpfc_sli4_cfg_shdr *shdr;
11352         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
11353
11354         if (!phba->sli4_hba.pc_sli4_params.supported)
11355                 hw_page_size = SLI4_PAGE_SIZE;
11356
11357         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11358         if (!mbox)
11359                 return -ENOMEM;
11360         length = (sizeof(struct lpfc_mbx_cq_create) -
11361                   sizeof(struct lpfc_sli4_cfg_mhdr));
11362         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
11363                          LPFC_MBOX_OPCODE_CQ_CREATE,
11364                          length, LPFC_SLI4_MBX_EMBED);
11365         cq_create = &mbox->u.mqe.un.cq_create;
11366         shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
11367         bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
11368                     cq->page_count);
11369         bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
11370         bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
11371         bf_set(lpfc_mbox_hdr_version, &shdr->request,
11372                phba->sli4_hba.pc_sli4_params.cqv);
11373         if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
11374                 /* FW only supports 1. Should be PAGE_SIZE/SLI4_PAGE_SIZE */
11375                 bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request, 1);
11376                 bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
11377                        eq->queue_id);
11378         } else {
11379                 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
11380                        eq->queue_id);
11381         }
11382         switch (cq->entry_count) {
11383         default:
11384                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11385                                 "0361 Unsupported CQ count. (%d)\n",
11386                                 cq->entry_count);
11387                 if (cq->entry_count < 256)
11388                         return -EINVAL;
11389                 /* otherwise default to smallest count (drop through) */
11390         case 256:
11391                 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
11392                        LPFC_CQ_CNT_256);
11393                 break;
11394         case 512:
11395                 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
11396                        LPFC_CQ_CNT_512);
11397                 break;
11398         case 1024:
11399                 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
11400                        LPFC_CQ_CNT_1024);
11401                 break;
11402         }
11403         list_for_each_entry(dmabuf, &cq->page_list, list) {
11404                 memset(dmabuf->virt, 0, hw_page_size);
11405                 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
11406                                         putPaddrLow(dmabuf->phys);
11407                 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
11408                                         putPaddrHigh(dmabuf->phys);
11409         }
11410         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
11411
11412         /* The IOCTL status is embedded in the mailbox subheader. */
11413         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11414         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11415         if (shdr_status || shdr_add_status || rc) {
11416                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11417                                 "2501 CQ_CREATE mailbox failed with "
11418                                 "status x%x add_status x%x, mbx status x%x\n",
11419                                 shdr_status, shdr_add_status, rc);
11420                 status = -ENXIO;
11421                 goto out;
11422         }
11423         cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
11424         if (cq->queue_id == 0xFFFF) {
11425                 status = -ENXIO;
11426                 goto out;
11427         }
11428         /* link the cq onto the parent eq child list */
11429         list_add_tail(&cq->list, &eq->child_list);
11430         /* Set up completion queue's type and subtype */
11431         cq->type = type;
11432         cq->subtype = subtype;
11433         cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
11434         cq->assoc_qid = eq->queue_id;
11435         cq->host_index = 0;
11436         cq->hba_index = 0;
11437
11438 out:
11439         mempool_free(mbox, phba->mbox_mem_pool);
11440         return status;
11441 }
11442
11443 /**
11444  * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
11445  * @phba: HBA structure that indicates port to create a queue on.
11446  * @mq: The queue structure to use to create the mailbox queue.
11447  * @mbox: An allocated pointer to type LPFC_MBOXQ_t
11448  * @cq: The completion queue to associate with this cq.
11449  *
11450  * This function provides failback (fb) functionality when the
11451  * mq_create_ext fails on older FW generations.  It's purpose is identical
11452  * to mq_create_ext otherwise.
11453  *
11454  * This routine cannot fail as all attributes were previously accessed and
11455  * initialized in mq_create_ext.
11456  **/
11457 static void
11458 lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
11459                        LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
11460 {
11461         struct lpfc_mbx_mq_create *mq_create;
11462         struct lpfc_dmabuf *dmabuf;
11463         int length;
11464
11465         length = (sizeof(struct lpfc_mbx_mq_create) -
11466                   sizeof(struct lpfc_sli4_cfg_mhdr));
11467         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
11468                          LPFC_MBOX_OPCODE_MQ_CREATE,
11469                          length, LPFC_SLI4_MBX_EMBED);
11470         mq_create = &mbox->u.mqe.un.mq_create;
11471         bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
11472                mq->page_count);
11473         bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
11474                cq->queue_id);
11475         bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
11476         switch (mq->entry_count) {
11477         case 16:
11478                 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
11479                        LPFC_MQ_RING_SIZE_16);
11480                 break;
11481         case 32:
11482                 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
11483                        LPFC_MQ_RING_SIZE_32);
11484                 break;
11485         case 64:
11486                 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
11487                        LPFC_MQ_RING_SIZE_64);
11488                 break;
11489         case 128:
11490                 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
11491                        LPFC_MQ_RING_SIZE_128);
11492                 break;
11493         }
11494         list_for_each_entry(dmabuf, &mq->page_list, list) {
11495                 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
11496                         putPaddrLow(dmabuf->phys);
11497                 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
11498                         putPaddrHigh(dmabuf->phys);
11499         }
11500 }
11501
11502 /**
11503  * lpfc_mq_create - Create a mailbox Queue on the HBA
11504  * @phba: HBA structure that indicates port to create a queue on.
11505  * @mq: The queue structure to use to create the mailbox queue.
11506  * @cq: The completion queue to associate with this cq.
11507  * @subtype: The queue's subtype.
11508  *
11509  * This function creates a mailbox queue, as detailed in @mq, on a port,
11510  * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
11511  *
11512  * The @phba struct is used to send mailbox command to HBA. The @cq struct
11513  * is used to get the entry count and entry size that are necessary to
11514  * determine the number of pages to allocate and use for this queue. This
11515  * function will send the MQ_CREATE mailbox command to the HBA to setup the
11516  * mailbox queue. This function is asynchronous and will wait for the mailbox
11517  * command to finish before continuing.
11518  *
11519  * On success this function will return a zero. If unable to allocate enough
11520  * memory this function will return -ENOMEM. If the queue create mailbox command
11521  * fails this function will return -ENXIO.
11522  **/
11523 int32_t
11524 lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
11525                struct lpfc_queue *cq, uint32_t subtype)
11526 {
11527         struct lpfc_mbx_mq_create *mq_create;
11528         struct lpfc_mbx_mq_create_ext *mq_create_ext;
11529         struct lpfc_dmabuf *dmabuf;
11530         LPFC_MBOXQ_t *mbox;
11531         int rc, length, status = 0;
11532         uint32_t shdr_status, shdr_add_status;
11533         union lpfc_sli4_cfg_shdr *shdr;
11534         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
11535
11536         if (!phba->sli4_hba.pc_sli4_params.supported)
11537                 hw_page_size = SLI4_PAGE_SIZE;
11538
11539         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11540         if (!mbox)
11541                 return -ENOMEM;
11542         length = (sizeof(struct lpfc_mbx_mq_create_ext) -
11543                   sizeof(struct lpfc_sli4_cfg_mhdr));
11544         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
11545                          LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
11546                          length, LPFC_SLI4_MBX_EMBED);
11547
11548         mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
11549         shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
11550         bf_set(lpfc_mbx_mq_create_ext_num_pages,
11551                &mq_create_ext->u.request, mq->page_count);
11552         bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
11553                &mq_create_ext->u.request, 1);
11554         bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
11555                &mq_create_ext->u.request, 1);
11556         bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
11557                &mq_create_ext->u.request, 1);
11558         bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
11559                &mq_create_ext->u.request, 1);
11560         bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
11561                &mq_create_ext->u.request, 1);
11562         bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
11563         bf_set(lpfc_mbox_hdr_version, &shdr->request,
11564                phba->sli4_hba.pc_sli4_params.mqv);
11565         if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
11566                 bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
11567                        cq->queue_id);
11568         else
11569                 bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
11570                        cq->queue_id);
11571         switch (mq->entry_count) {
11572         default:
11573                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11574                                 "0362 Unsupported MQ count. (%d)\n",
11575                                 mq->entry_count);
11576                 if (mq->entry_count < 16)
11577                         return -EINVAL;
11578                 /* otherwise default to smallest count (drop through) */
11579         case 16:
11580                 bf_set(lpfc_mq_context_ring_size,
11581                        &mq_create_ext->u.request.context,
11582                        LPFC_MQ_RING_SIZE_16);
11583                 break;
11584         case 32:
11585                 bf_set(lpfc_mq_context_ring_size,
11586                        &mq_create_ext->u.request.context,
11587                        LPFC_MQ_RING_SIZE_32);
11588                 break;
11589         case 64:
11590                 bf_set(lpfc_mq_context_ring_size,
11591                        &mq_create_ext->u.request.context,
11592                        LPFC_MQ_RING_SIZE_64);
11593                 break;
11594         case 128:
11595                 bf_set(lpfc_mq_context_ring_size,
11596                        &mq_create_ext->u.request.context,
11597                        LPFC_MQ_RING_SIZE_128);
11598                 break;
11599         }
11600         list_for_each_entry(dmabuf, &mq->page_list, list) {
11601                 memset(dmabuf->virt, 0, hw_page_size);
11602                 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
11603                                         putPaddrLow(dmabuf->phys);
11604                 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
11605                                         putPaddrHigh(dmabuf->phys);
11606         }
11607         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
11608         mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
11609                               &mq_create_ext->u.response);
11610         if (rc != MBX_SUCCESS) {
11611                 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
11612                                 "2795 MQ_CREATE_EXT failed with "
11613                                 "status x%x. Failback to MQ_CREATE.\n",
11614                                 rc);
11615                 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
11616                 mq_create = &mbox->u.mqe.un.mq_create;
11617                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
11618                 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
11619                 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
11620                                       &mq_create->u.response);
11621         }
11622
11623         /* The IOCTL status is embedded in the mailbox subheader. */
11624         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11625         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11626         if (shdr_status || shdr_add_status || rc) {
11627                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11628                                 "2502 MQ_CREATE mailbox failed with "
11629                                 "status x%x add_status x%x, mbx status x%x\n",
11630                                 shdr_status, shdr_add_status, rc);
11631                 status = -ENXIO;
11632                 goto out;
11633         }
11634         if (mq->queue_id == 0xFFFF) {
11635                 status = -ENXIO;
11636                 goto out;
11637         }
11638         mq->type = LPFC_MQ;
11639         mq->assoc_qid = cq->queue_id;
11640         mq->subtype = subtype;
11641         mq->host_index = 0;
11642         mq->hba_index = 0;
11643
11644         /* link the mq onto the parent cq child list */
11645         list_add_tail(&mq->list, &cq->child_list);
11646 out:
11647         mempool_free(mbox, phba->mbox_mem_pool);
11648         return status;
11649 }
11650
11651 /**
11652  * lpfc_wq_create - Create a Work Queue on the HBA
11653  * @phba: HBA structure that indicates port to create a queue on.
11654  * @wq: The queue structure to use to create the work queue.
11655  * @cq: The completion queue to bind this work queue to.
11656  * @subtype: The subtype of the work queue indicating its functionality.
11657  *
11658  * This function creates a work queue, as detailed in @wq, on a port, described
11659  * by @phba by sending a WQ_CREATE mailbox command to the HBA.
11660  *
11661  * The @phba struct is used to send mailbox command to HBA. The @wq struct
11662  * is used to get the entry count and entry size that are necessary to
11663  * determine the number of pages to allocate and use for this queue. The @cq
11664  * is used to indicate which completion queue to bind this work queue to. This
11665  * function will send the WQ_CREATE mailbox command to the HBA to setup the
11666  * work queue. This function is asynchronous and will wait for the mailbox
11667  * command to finish before continuing.
11668  *
11669  * On success this function will return a zero. If unable to allocate enough
11670  * memory this function will return -ENOMEM. If the queue create mailbox command
11671  * fails this function will return -ENXIO.
11672  **/
11673 uint32_t
11674 lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
11675                struct lpfc_queue *cq, uint32_t subtype)
11676 {
11677         struct lpfc_mbx_wq_create *wq_create;
11678         struct lpfc_dmabuf *dmabuf;
11679         LPFC_MBOXQ_t *mbox;
11680         int rc, length, status = 0;
11681         uint32_t shdr_status, shdr_add_status;
11682         union lpfc_sli4_cfg_shdr *shdr;
11683         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
11684         struct dma_address *page;
11685
11686         if (!phba->sli4_hba.pc_sli4_params.supported)
11687                 hw_page_size = SLI4_PAGE_SIZE;
11688
11689         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11690         if (!mbox)
11691                 return -ENOMEM;
11692         length = (sizeof(struct lpfc_mbx_wq_create) -
11693                   sizeof(struct lpfc_sli4_cfg_mhdr));
11694         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
11695                          LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
11696                          length, LPFC_SLI4_MBX_EMBED);
11697         wq_create = &mbox->u.mqe.un.wq_create;
11698         shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
11699         bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
11700                     wq->page_count);
11701         bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
11702                     cq->queue_id);
11703         bf_set(lpfc_mbox_hdr_version, &shdr->request,
11704                phba->sli4_hba.pc_sli4_params.wqv);
11705         if (phba->sli4_hba.pc_sli4_params.wqv == LPFC_Q_CREATE_VERSION_1) {
11706                 bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
11707                        wq->entry_count);
11708                 switch (wq->entry_size) {
11709                 default:
11710                 case 64:
11711                         bf_set(lpfc_mbx_wq_create_wqe_size,
11712                                &wq_create->u.request_1,
11713                                LPFC_WQ_WQE_SIZE_64);
11714                         break;
11715                 case 128:
11716                         bf_set(lpfc_mbx_wq_create_wqe_size,
11717                                &wq_create->u.request_1,
11718                                LPFC_WQ_WQE_SIZE_128);
11719                         break;
11720                 }
11721                 bf_set(lpfc_mbx_wq_create_page_size, &wq_create->u.request_1,
11722                        (PAGE_SIZE/SLI4_PAGE_SIZE));
11723                 page = wq_create->u.request_1.page;
11724         } else {
11725                 page = wq_create->u.request.page;
11726         }
11727         list_for_each_entry(dmabuf, &wq->page_list, list) {
11728                 memset(dmabuf->virt, 0, hw_page_size);
11729                 page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
11730                 page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
11731         }
11732         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
11733         /* The IOCTL status is embedded in the mailbox subheader. */
11734         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11735         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11736         if (shdr_status || shdr_add_status || rc) {
11737                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11738                                 "2503 WQ_CREATE mailbox failed with "
11739                                 "status x%x add_status x%x, mbx status x%x\n",
11740                                 shdr_status, shdr_add_status, rc);
11741                 status = -ENXIO;
11742                 goto out;
11743         }
11744         wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
11745         if (wq->queue_id == 0xFFFF) {
11746                 status = -ENXIO;
11747                 goto out;
11748         }
11749         wq->type = LPFC_WQ;
11750         wq->assoc_qid = cq->queue_id;
11751         wq->subtype = subtype;
11752         wq->host_index = 0;
11753         wq->hba_index = 0;
11754
11755         /* link the wq onto the parent cq child list */
11756         list_add_tail(&wq->list, &cq->child_list);
11757 out:
11758         mempool_free(mbox, phba->mbox_mem_pool);
11759         return status;
11760 }
11761
11762 /**
11763  * lpfc_rq_create - Create a Receive Queue on the HBA
11764  * @phba: HBA structure that indicates port to create a queue on.
11765  * @hrq: The queue structure to use to create the header receive queue.
11766  * @drq: The queue structure to use to create the data receive queue.
11767  * @cq: The completion queue to bind this work queue to.
11768  *
11769  * This function creates a receive buffer queue pair , as detailed in @hrq and
11770  * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
11771  * to the HBA.
11772  *
11773  * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
11774  * struct is used to get the entry count that is necessary to determine the
11775  * number of pages to use for this queue. The @cq is used to indicate which
11776  * completion queue to bind received buffers that are posted to these queues to.
11777  * This function will send the RQ_CREATE mailbox command to the HBA to setup the
11778  * receive queue pair. This function is asynchronous and will wait for the
11779  * mailbox command to finish before continuing.
11780  *
11781  * On success this function will return a zero. If unable to allocate enough
11782  * memory this function will return -ENOMEM. If the queue create mailbox command
11783  * fails this function will return -ENXIO.
11784  **/
11785 uint32_t
11786 lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
11787                struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
11788 {
11789         struct lpfc_mbx_rq_create *rq_create;
11790         struct lpfc_dmabuf *dmabuf;
11791         LPFC_MBOXQ_t *mbox;
11792         int rc, length, status = 0;
11793         uint32_t shdr_status, shdr_add_status;
11794         union lpfc_sli4_cfg_shdr *shdr;
11795         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
11796
11797         if (!phba->sli4_hba.pc_sli4_params.supported)
11798                 hw_page_size = SLI4_PAGE_SIZE;
11799
11800         if (hrq->entry_count != drq->entry_count)
11801                 return -EINVAL;
11802         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11803         if (!mbox)
11804                 return -ENOMEM;
11805         length = (sizeof(struct lpfc_mbx_rq_create) -
11806                   sizeof(struct lpfc_sli4_cfg_mhdr));
11807         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
11808                          LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
11809                          length, LPFC_SLI4_MBX_EMBED);
11810         rq_create = &mbox->u.mqe.un.rq_create;
11811         shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
11812         bf_set(lpfc_mbox_hdr_version, &shdr->request,
11813                phba->sli4_hba.pc_sli4_params.rqv);
11814         if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
11815                 bf_set(lpfc_rq_context_rqe_count_1,
11816                        &rq_create->u.request.context,
11817                        hrq->entry_count);
11818                 rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
11819                 bf_set(lpfc_rq_context_rqe_size,
11820                        &rq_create->u.request.context,
11821                        LPFC_RQE_SIZE_8);
11822                 bf_set(lpfc_rq_context_page_size,
11823                        &rq_create->u.request.context,
11824                        (PAGE_SIZE/SLI4_PAGE_SIZE));
11825         } else {
11826                 switch (hrq->entry_count) {
11827                 default:
11828                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11829                                         "2535 Unsupported RQ count. (%d)\n",
11830                                         hrq->entry_count);
11831                         if (hrq->entry_count < 512)
11832                                 return -EINVAL;
11833                         /* otherwise default to smallest count (drop through) */
11834                 case 512:
11835                         bf_set(lpfc_rq_context_rqe_count,
11836                                &rq_create->u.request.context,
11837                                LPFC_RQ_RING_SIZE_512);
11838                         break;
11839                 case 1024:
11840                         bf_set(lpfc_rq_context_rqe_count,
11841                                &rq_create->u.request.context,
11842                                LPFC_RQ_RING_SIZE_1024);
11843                         break;
11844                 case 2048:
11845                         bf_set(lpfc_rq_context_rqe_count,
11846                                &rq_create->u.request.context,
11847                                LPFC_RQ_RING_SIZE_2048);
11848                         break;
11849                 case 4096:
11850                         bf_set(lpfc_rq_context_rqe_count,
11851                                &rq_create->u.request.context,
11852                                LPFC_RQ_RING_SIZE_4096);
11853                         break;
11854                 }
11855                 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
11856                        LPFC_HDR_BUF_SIZE);
11857         }
11858         bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
11859                cq->queue_id);
11860         bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
11861                hrq->page_count);
11862         list_for_each_entry(dmabuf, &hrq->page_list, list) {
11863                 memset(dmabuf->virt, 0, hw_page_size);
11864                 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
11865                                         putPaddrLow(dmabuf->phys);
11866                 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
11867                                         putPaddrHigh(dmabuf->phys);
11868         }
11869         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
11870         /* The IOCTL status is embedded in the mailbox subheader. */
11871         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11872         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11873         if (shdr_status || shdr_add_status || rc) {
11874                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11875                                 "2504 RQ_CREATE mailbox failed with "
11876                                 "status x%x add_status x%x, mbx status x%x\n",
11877                                 shdr_status, shdr_add_status, rc);
11878                 status = -ENXIO;
11879                 goto out;
11880         }
11881         hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
11882         if (hrq->queue_id == 0xFFFF) {
11883                 status = -ENXIO;
11884                 goto out;
11885         }
11886         hrq->type = LPFC_HRQ;
11887         hrq->assoc_qid = cq->queue_id;
11888         hrq->subtype = subtype;
11889         hrq->host_index = 0;
11890         hrq->hba_index = 0;
11891
11892         /* now create the data queue */
11893         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
11894                          LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
11895                          length, LPFC_SLI4_MBX_EMBED);
11896         bf_set(lpfc_mbox_hdr_version, &shdr->request,
11897                phba->sli4_hba.pc_sli4_params.rqv);
11898         if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
11899                 bf_set(lpfc_rq_context_rqe_count_1,
11900                        &rq_create->u.request.context, hrq->entry_count);
11901                 rq_create->u.request.context.buffer_size = LPFC_DATA_BUF_SIZE;
11902                 bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
11903                        LPFC_RQE_SIZE_8);
11904                 bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
11905                        (PAGE_SIZE/SLI4_PAGE_SIZE));
11906         } else {
11907                 switch (drq->entry_count) {
11908                 default:
11909                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11910                                         "2536 Unsupported RQ count. (%d)\n",
11911                                         drq->entry_count);
11912                         if (drq->entry_count < 512)
11913                                 return -EINVAL;
11914                         /* otherwise default to smallest count (drop through) */
11915                 case 512:
11916                         bf_set(lpfc_rq_context_rqe_count,
11917                                &rq_create->u.request.context,
11918                                LPFC_RQ_RING_SIZE_512);
11919                         break;
11920                 case 1024:
11921                         bf_set(lpfc_rq_context_rqe_count,
11922                                &rq_create->u.request.context,
11923                                LPFC_RQ_RING_SIZE_1024);
11924                         break;
11925                 case 2048:
11926                         bf_set(lpfc_rq_context_rqe_count,
11927                                &rq_create->u.request.context,
11928                                LPFC_RQ_RING_SIZE_2048);
11929                         break;
11930                 case 4096:
11931                         bf_set(lpfc_rq_context_rqe_count,
11932                                &rq_create->u.request.context,
11933                                LPFC_RQ_RING_SIZE_4096);
11934                         break;
11935                 }
11936                 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
11937                        LPFC_DATA_BUF_SIZE);
11938         }
11939         bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
11940                cq->queue_id);
11941         bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
11942                drq->page_count);
11943         list_for_each_entry(dmabuf, &drq->page_list, list) {
11944                 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
11945                                         putPaddrLow(dmabuf->phys);
11946                 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
11947                                         putPaddrHigh(dmabuf->phys);
11948         }
11949         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
11950         /* The IOCTL status is embedded in the mailbox subheader. */
11951         shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
11952         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11953         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11954         if (shdr_status || shdr_add_status || rc) {
11955                 status = -ENXIO;
11956                 goto out;
11957         }
11958         drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
11959         if (drq->queue_id == 0xFFFF) {
11960                 status = -ENXIO;
11961                 goto out;
11962         }
11963         drq->type = LPFC_DRQ;
11964         drq->assoc_qid = cq->queue_id;
11965         drq->subtype = subtype;
11966         drq->host_index = 0;
11967         drq->hba_index = 0;
11968
11969         /* link the header and data RQs onto the parent cq child list */
11970         list_add_tail(&hrq->list, &cq->child_list);
11971         list_add_tail(&drq->list, &cq->child_list);
11972
11973 out:
11974         mempool_free(mbox, phba->mbox_mem_pool);
11975         return status;
11976 }
11977
11978 /**
11979  * lpfc_eq_destroy - Destroy an event Queue on the HBA
11980  * @eq: The queue structure associated with the queue to destroy.
11981  *
11982  * This function destroys a queue, as detailed in @eq by sending an mailbox
11983  * command, specific to the type of queue, to the HBA.
11984  *
11985  * The @eq struct is used to get the queue ID of the queue to destroy.
11986  *
11987  * On success this function will return a zero. If the queue destroy mailbox
11988  * command fails this function will return -ENXIO.
11989  **/
11990 uint32_t
11991 lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
11992 {
11993         LPFC_MBOXQ_t *mbox;
11994         int rc, length, status = 0;
11995         uint32_t shdr_status, shdr_add_status;
11996         union lpfc_sli4_cfg_shdr *shdr;
11997
11998         if (!eq)
11999                 return -ENODEV;
12000         mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
12001         if (!mbox)
12002                 return -ENOMEM;
12003         length = (sizeof(struct lpfc_mbx_eq_destroy) -
12004                   sizeof(struct lpfc_sli4_cfg_mhdr));
12005         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12006                          LPFC_MBOX_OPCODE_EQ_DESTROY,
12007                          length, LPFC_SLI4_MBX_EMBED);
12008         bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
12009                eq->queue_id);
12010         mbox->vport = eq->phba->pport;
12011         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12012
12013         rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
12014         /* The IOCTL status is embedded in the mailbox subheader. */
12015         shdr = (union lpfc_sli4_cfg_shdr *)
12016                 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
12017         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12018         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12019         if (shdr_status || shdr_add_status || rc) {
12020                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12021                                 "2505 EQ_DESTROY mailbox failed with "
12022                                 "status x%x add_status x%x, mbx status x%x\n",
12023                                 shdr_status, shdr_add_status, rc);
12024                 status = -ENXIO;
12025         }
12026
12027         /* Remove eq from any list */
12028         list_del_init(&eq->list);
12029         mempool_free(mbox, eq->phba->mbox_mem_pool);
12030         return status;
12031 }
12032
12033 /**
12034  * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
12035  * @cq: The queue structure associated with the queue to destroy.
12036  *
12037  * This function destroys a queue, as detailed in @cq by sending an mailbox
12038  * command, specific to the type of queue, to the HBA.
12039  *
12040  * The @cq struct is used to get the queue ID of the queue to destroy.
12041  *
12042  * On success this function will return a zero. If the queue destroy mailbox
12043  * command fails this function will return -ENXIO.
12044  **/
12045 uint32_t
12046 lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
12047 {
12048         LPFC_MBOXQ_t *mbox;
12049         int rc, length, status = 0;
12050         uint32_t shdr_status, shdr_add_status;
12051         union lpfc_sli4_cfg_shdr *shdr;
12052
12053         if (!cq)
12054                 return -ENODEV;
12055         mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
12056         if (!mbox)
12057                 return -ENOMEM;
12058         length = (sizeof(struct lpfc_mbx_cq_destroy) -
12059                   sizeof(struct lpfc_sli4_cfg_mhdr));
12060         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12061                          LPFC_MBOX_OPCODE_CQ_DESTROY,
12062                          length, LPFC_SLI4_MBX_EMBED);
12063         bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
12064                cq->queue_id);
12065         mbox->vport = cq->phba->pport;
12066         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12067         rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
12068         /* The IOCTL status is embedded in the mailbox subheader. */
12069         shdr = (union lpfc_sli4_cfg_shdr *)
12070                 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
12071         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12072         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12073         if (shdr_status || shdr_add_status || rc) {
12074                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12075                                 "2506 CQ_DESTROY mailbox failed with "
12076                                 "status x%x add_status x%x, mbx status x%x\n",
12077                                 shdr_status, shdr_add_status, rc);
12078                 status = -ENXIO;
12079         }
12080         /* Remove cq from any list */
12081         list_del_init(&cq->list);
12082         mempool_free(mbox, cq->phba->mbox_mem_pool);
12083         return status;
12084 }
12085
12086 /**
12087  * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
12088  * @qm: The queue structure associated with the queue to destroy.
12089  *
12090  * This function destroys a queue, as detailed in @mq by sending an mailbox
12091  * command, specific to the type of queue, to the HBA.
12092  *
12093  * The @mq struct is used to get the queue ID of the queue to destroy.
12094  *
12095  * On success this function will return a zero. If the queue destroy mailbox
12096  * command fails this function will return -ENXIO.
12097  **/
12098 uint32_t
12099 lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
12100 {
12101         LPFC_MBOXQ_t *mbox;
12102         int rc, length, status = 0;
12103         uint32_t shdr_status, shdr_add_status;
12104         union lpfc_sli4_cfg_shdr *shdr;
12105
12106         if (!mq)
12107                 return -ENODEV;
12108         mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
12109         if (!mbox)
12110                 return -ENOMEM;
12111         length = (sizeof(struct lpfc_mbx_mq_destroy) -
12112                   sizeof(struct lpfc_sli4_cfg_mhdr));
12113         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12114                          LPFC_MBOX_OPCODE_MQ_DESTROY,
12115                          length, LPFC_SLI4_MBX_EMBED);
12116         bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
12117                mq->queue_id);
12118         mbox->vport = mq->phba->pport;
12119         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12120         rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
12121         /* The IOCTL status is embedded in the mailbox subheader. */
12122         shdr = (union lpfc_sli4_cfg_shdr *)
12123                 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
12124         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12125         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12126         if (shdr_status || shdr_add_status || rc) {
12127                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12128                                 "2507 MQ_DESTROY mailbox failed with "
12129                                 "status x%x add_status x%x, mbx status x%x\n",
12130                                 shdr_status, shdr_add_status, rc);
12131                 status = -ENXIO;
12132         }
12133         /* Remove mq from any list */
12134         list_del_init(&mq->list);
12135         mempool_free(mbox, mq->phba->mbox_mem_pool);
12136         return status;
12137 }
12138
12139 /**
12140  * lpfc_wq_destroy - Destroy a Work Queue on the HBA
12141  * @wq: The queue structure associated with the queue to destroy.
12142  *
12143  * This function destroys a queue, as detailed in @wq by sending an mailbox
12144  * command, specific to the type of queue, to the HBA.
12145  *
12146  * The @wq struct is used to get the queue ID of the queue to destroy.
12147  *
12148  * On success this function will return a zero. If the queue destroy mailbox
12149  * command fails this function will return -ENXIO.
12150  **/
12151 uint32_t
12152 lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
12153 {
12154         LPFC_MBOXQ_t *mbox;
12155         int rc, length, status = 0;
12156         uint32_t shdr_status, shdr_add_status;
12157         union lpfc_sli4_cfg_shdr *shdr;
12158
12159         if (!wq)
12160                 return -ENODEV;
12161         mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
12162         if (!mbox)
12163                 return -ENOMEM;
12164         length = (sizeof(struct lpfc_mbx_wq_destroy) -
12165                   sizeof(struct lpfc_sli4_cfg_mhdr));
12166         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12167                          LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
12168                          length, LPFC_SLI4_MBX_EMBED);
12169         bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
12170                wq->queue_id);
12171         mbox->vport = wq->phba->pport;
12172         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12173         rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
12174         shdr = (union lpfc_sli4_cfg_shdr *)
12175                 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
12176         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12177         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12178         if (shdr_status || shdr_add_status || rc) {
12179                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12180                                 "2508 WQ_DESTROY mailbox failed with "
12181                                 "status x%x add_status x%x, mbx status x%x\n",
12182                                 shdr_status, shdr_add_status, rc);
12183                 status = -ENXIO;
12184         }
12185         /* Remove wq from any list */
12186         list_del_init(&wq->list);
12187         mempool_free(mbox, wq->phba->mbox_mem_pool);
12188         return status;
12189 }
12190
12191 /**
12192  * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
12193  * @rq: The queue structure associated with the queue to destroy.
12194  *
12195  * This function destroys a queue, as detailed in @rq by sending an mailbox
12196  * command, specific to the type of queue, to the HBA.
12197  *
12198  * The @rq struct is used to get the queue ID of the queue to destroy.
12199  *
12200  * On success this function will return a zero. If the queue destroy mailbox
12201  * command fails this function will return -ENXIO.
12202  **/
12203 uint32_t
12204 lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
12205                 struct lpfc_queue *drq)
12206 {
12207         LPFC_MBOXQ_t *mbox;
12208         int rc, length, status = 0;
12209         uint32_t shdr_status, shdr_add_status;
12210         union lpfc_sli4_cfg_shdr *shdr;
12211
12212         if (!hrq || !drq)
12213                 return -ENODEV;
12214         mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
12215         if (!mbox)
12216                 return -ENOMEM;
12217         length = (sizeof(struct lpfc_mbx_rq_destroy) -
12218                   sizeof(struct lpfc_sli4_cfg_mhdr));
12219         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12220                          LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
12221                          length, LPFC_SLI4_MBX_EMBED);
12222         bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
12223                hrq->queue_id);
12224         mbox->vport = hrq->phba->pport;
12225         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12226         rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
12227         /* The IOCTL status is embedded in the mailbox subheader. */
12228         shdr = (union lpfc_sli4_cfg_shdr *)
12229                 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
12230         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12231         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12232         if (shdr_status || shdr_add_status || rc) {
12233                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12234                                 "2509 RQ_DESTROY mailbox failed with "
12235                                 "status x%x add_status x%x, mbx status x%x\n",
12236                                 shdr_status, shdr_add_status, rc);
12237                 if (rc != MBX_TIMEOUT)
12238                         mempool_free(mbox, hrq->phba->mbox_mem_pool);
12239                 return -ENXIO;
12240         }
12241         bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
12242                drq->queue_id);
12243         rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
12244         shdr = (union lpfc_sli4_cfg_shdr *)
12245                 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
12246         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12247         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12248         if (shdr_status || shdr_add_status || rc) {
12249                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12250                                 "2510 RQ_DESTROY mailbox failed with "
12251                                 "status x%x add_status x%x, mbx status x%x\n",
12252                                 shdr_status, shdr_add_status, rc);
12253                 status = -ENXIO;
12254         }
12255         list_del_init(&hrq->list);
12256         list_del_init(&drq->list);
12257         mempool_free(mbox, hrq->phba->mbox_mem_pool);
12258         return status;
12259 }
12260
12261 /**
12262  * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
12263  * @phba: The virtual port for which this call being executed.
12264  * @pdma_phys_addr0: Physical address of the 1st SGL page.
12265  * @pdma_phys_addr1: Physical address of the 2nd SGL page.
12266  * @xritag: the xritag that ties this io to the SGL pages.
12267  *
12268  * This routine will post the sgl pages for the IO that has the xritag
12269  * that is in the iocbq structure. The xritag is assigned during iocbq
12270  * creation and persists for as long as the driver is loaded.
12271  * if the caller has fewer than 256 scatter gather segments to map then
12272  * pdma_phys_addr1 should be 0.
12273  * If the caller needs to map more than 256 scatter gather segment then
12274  * pdma_phys_addr1 should be a valid physical address.
12275  * physical address for SGLs must be 64 byte aligned.
12276  * If you are going to map 2 SGL's then the first one must have 256 entries
12277  * the second sgl can have between 1 and 256 entries.
12278  *
12279  * Return codes:
12280  *      0 - Success
12281  *      -ENXIO, -ENOMEM - Failure
12282  **/
12283 int
12284 lpfc_sli4_post_sgl(struct lpfc_hba *phba,
12285                 dma_addr_t pdma_phys_addr0,
12286                 dma_addr_t pdma_phys_addr1,
12287                 uint16_t xritag)
12288 {
12289         struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
12290         LPFC_MBOXQ_t *mbox;
12291         int rc;
12292         uint32_t shdr_status, shdr_add_status;
12293         uint32_t mbox_tmo;
12294         union lpfc_sli4_cfg_shdr *shdr;
12295
12296         if (xritag == NO_XRI) {
12297                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12298                                 "0364 Invalid param:\n");
12299                 return -EINVAL;
12300         }
12301
12302         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12303         if (!mbox)
12304                 return -ENOMEM;
12305
12306         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12307                         LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
12308                         sizeof(struct lpfc_mbx_post_sgl_pages) -
12309                         sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
12310
12311         post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
12312                                 &mbox->u.mqe.un.post_sgl_pages;
12313         bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
12314         bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
12315
12316         post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
12317                                 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
12318         post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
12319                                 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
12320
12321         post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
12322                                 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
12323         post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
12324                                 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
12325         if (!phba->sli4_hba.intr_enable)
12326                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12327         else {
12328                 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
12329                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
12330         }
12331         /* The IOCTL status is embedded in the mailbox subheader. */
12332         shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
12333         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12334         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12335         if (rc != MBX_TIMEOUT)
12336                 mempool_free(mbox, phba->mbox_mem_pool);
12337         if (shdr_status || shdr_add_status || rc) {
12338                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12339                                 "2511 POST_SGL mailbox failed with "
12340                                 "status x%x add_status x%x, mbx status x%x\n",
12341                                 shdr_status, shdr_add_status, rc);
12342                 rc = -ENXIO;
12343         }
12344         return 0;
12345 }
12346
12347 /**
12348  * lpfc_sli4_init_rpi_hdrs - Post the rpi header memory region to the port
12349  * @phba: pointer to lpfc hba data structure.
12350  *
12351  * This routine is invoked to post rpi header templates to the
12352  * port for those SLI4 ports that do not support extents.  This routine
12353  * posts a PAGE_SIZE memory region to the port to hold up to
12354  * PAGE_SIZE modulo 64 rpi context headers.  This is an initialization routine
12355  * and should be called only when interrupts are disabled.
12356  *
12357  * Return codes
12358  *      0 - successful
12359  *      -ERROR - otherwise.
12360  */
12361 uint16_t
12362 lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
12363 {
12364         unsigned long xri;
12365
12366         /*
12367          * Fetch the next logical xri.  Because this index is logical,
12368          * the driver starts at 0 each time.
12369          */
12370         spin_lock_irq(&phba->hbalock);
12371         xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
12372                                  phba->sli4_hba.max_cfg_param.max_xri, 0);
12373         if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
12374                 spin_unlock_irq(&phba->hbalock);
12375                 return NO_XRI;
12376         } else {
12377                 set_bit(xri, phba->sli4_hba.xri_bmask);
12378                 phba->sli4_hba.max_cfg_param.xri_used++;
12379                 phba->sli4_hba.xri_count++;
12380         }
12381
12382         spin_unlock_irq(&phba->hbalock);
12383         return xri;
12384 }
12385
12386 /**
12387  * lpfc_sli4_free_xri - Release an xri for reuse.
12388  * @phba: pointer to lpfc hba data structure.
12389  *
12390  * This routine is invoked to release an xri to the pool of
12391  * available rpis maintained by the driver.
12392  **/
12393 void
12394 __lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
12395 {
12396         if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
12397                 phba->sli4_hba.xri_count--;
12398                 phba->sli4_hba.max_cfg_param.xri_used--;
12399         }
12400 }
12401
12402 /**
12403  * lpfc_sli4_free_xri - Release an xri for reuse.
12404  * @phba: pointer to lpfc hba data structure.
12405  *
12406  * This routine is invoked to release an xri to the pool of
12407  * available rpis maintained by the driver.
12408  **/
12409 void
12410 lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
12411 {
12412         spin_lock_irq(&phba->hbalock);
12413         __lpfc_sli4_free_xri(phba, xri);
12414         spin_unlock_irq(&phba->hbalock);
12415 }
12416
12417 /**
12418  * lpfc_sli4_next_xritag - Get an xritag for the io
12419  * @phba: Pointer to HBA context object.
12420  *
12421  * This function gets an xritag for the iocb. If there is no unused xritag
12422  * it will return 0xffff.
12423  * The function returns the allocated xritag if successful, else returns zero.
12424  * Zero is not a valid xritag.
12425  * The caller is not required to hold any lock.
12426  **/
12427 uint16_t
12428 lpfc_sli4_next_xritag(struct lpfc_hba *phba)
12429 {
12430         uint16_t xri_index;
12431
12432         xri_index = lpfc_sli4_alloc_xri(phba);
12433         if (xri_index != NO_XRI)
12434                 return xri_index;
12435
12436         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12437                         "2004 Failed to allocate XRI.last XRITAG is %d"
12438                         " Max XRI is %d, Used XRI is %d\n",
12439                         xri_index,
12440                         phba->sli4_hba.max_cfg_param.max_xri,
12441                         phba->sli4_hba.max_cfg_param.xri_used);
12442         return NO_XRI;
12443 }
12444
12445 /**
12446  * lpfc_sli4_post_els_sgl_list - post a block of ELS sgls to the port.
12447  * @phba: pointer to lpfc hba data structure.
12448  *
12449  * This routine is invoked to post a block of driver's sgl pages to the
12450  * HBA using non-embedded mailbox command. No Lock is held. This routine
12451  * is only called when the driver is loading and after all IO has been
12452  * stopped.
12453  **/
12454 int
12455 lpfc_sli4_post_els_sgl_list(struct lpfc_hba *phba)
12456 {
12457         struct lpfc_sglq *sglq_entry;
12458         struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
12459         struct sgl_page_pairs *sgl_pg_pairs;
12460         void *viraddr;
12461         LPFC_MBOXQ_t *mbox;
12462         uint32_t reqlen, alloclen, pg_pairs;
12463         uint32_t mbox_tmo;
12464         uint16_t xritag_start = 0, lxri = 0;
12465         int els_xri_cnt, rc = 0;
12466         uint32_t shdr_status, shdr_add_status;
12467         union lpfc_sli4_cfg_shdr *shdr;
12468
12469         /* The number of sgls to be posted */
12470         els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
12471
12472         reqlen = els_xri_cnt * sizeof(struct sgl_page_pairs) +
12473                  sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
12474         if (reqlen > SLI4_PAGE_SIZE) {
12475                 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
12476                                 "2559 Block sgl registration required DMA "
12477                                 "size (%d) great than a page\n", reqlen);
12478                 return -ENOMEM;
12479         }
12480         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12481         if (!mbox)
12482                 return -ENOMEM;
12483
12484         /* Allocate DMA memory and set up the non-embedded mailbox command */
12485         alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12486                          LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
12487                          LPFC_SLI4_MBX_NEMBED);
12488
12489         if (alloclen < reqlen) {
12490                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12491                                 "0285 Allocated DMA memory size (%d) is "
12492                                 "less than the requested DMA memory "
12493                                 "size (%d)\n", alloclen, reqlen);
12494                 lpfc_sli4_mbox_cmd_free(phba, mbox);
12495                 return -ENOMEM;
12496         }
12497         /* Set up the SGL pages in the non-embedded DMA pages */
12498         viraddr = mbox->sge_array->addr[0];
12499         sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
12500         sgl_pg_pairs = &sgl->sgl_pg_pairs;
12501
12502         for (pg_pairs = 0; pg_pairs < els_xri_cnt; pg_pairs++) {
12503                 sglq_entry = phba->sli4_hba.lpfc_els_sgl_array[pg_pairs];
12504
12505                 /*
12506                  * Assign the sglq a physical xri only if the driver has not
12507                  * initialized those resources.  A port reset only needs
12508                  * the sglq's posted.
12509                  */
12510                 if (bf_get(lpfc_xri_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
12511                     LPFC_XRI_RSRC_RDY) {
12512                         lxri = lpfc_sli4_next_xritag(phba);
12513                         if (lxri == NO_XRI) {
12514                                 lpfc_sli4_mbox_cmd_free(phba, mbox);
12515                                 return -ENOMEM;
12516                         }
12517                         sglq_entry->sli4_lxritag = lxri;
12518                         sglq_entry->sli4_xritag = phba->sli4_hba.xri_ids[lxri];
12519                 }
12520
12521                 /* Set up the sge entry */
12522                 sgl_pg_pairs->sgl_pg0_addr_lo =
12523                                 cpu_to_le32(putPaddrLow(sglq_entry->phys));
12524                 sgl_pg_pairs->sgl_pg0_addr_hi =
12525                                 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
12526                 sgl_pg_pairs->sgl_pg1_addr_lo =
12527                                 cpu_to_le32(putPaddrLow(0));
12528                 sgl_pg_pairs->sgl_pg1_addr_hi =
12529                                 cpu_to_le32(putPaddrHigh(0));
12530
12531                 /* Keep the first xritag on the list */
12532                 if (pg_pairs == 0)
12533                         xritag_start = sglq_entry->sli4_xritag;
12534                 sgl_pg_pairs++;
12535         }
12536
12537         /* Complete initialization and perform endian conversion. */
12538         bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
12539         bf_set(lpfc_post_sgl_pages_xricnt, sgl, els_xri_cnt);
12540         sgl->word0 = cpu_to_le32(sgl->word0);
12541         if (!phba->sli4_hba.intr_enable)
12542                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12543         else {
12544                 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
12545                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
12546         }
12547         shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
12548         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12549         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12550         if (rc != MBX_TIMEOUT)
12551                 lpfc_sli4_mbox_cmd_free(phba, mbox);
12552         if (shdr_status || shdr_add_status || rc) {
12553                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12554                                 "2513 POST_SGL_BLOCK mailbox command failed "
12555                                 "status x%x add_status x%x mbx status x%x\n",
12556                                 shdr_status, shdr_add_status, rc);
12557                 rc = -ENXIO;
12558         }
12559
12560         if (rc == 0)
12561                 bf_set(lpfc_xri_rsrc_rdy, &phba->sli4_hba.sli4_flags,
12562                        LPFC_XRI_RSRC_RDY);
12563         return rc;
12564 }
12565
12566 /**
12567  * lpfc_sli4_post_els_sgl_list_ext - post a block of ELS sgls to the port.
12568  * @phba: pointer to lpfc hba data structure.
12569  *
12570  * This routine is invoked to post a block of driver's sgl pages to the
12571  * HBA using non-embedded mailbox command. No Lock is held. This routine
12572  * is only called when the driver is loading and after all IO has been
12573  * stopped.
12574  **/
12575 int
12576 lpfc_sli4_post_els_sgl_list_ext(struct lpfc_hba *phba)
12577 {
12578         struct lpfc_sglq *sglq_entry;
12579         struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
12580         struct sgl_page_pairs *sgl_pg_pairs;
12581         void *viraddr;
12582         LPFC_MBOXQ_t *mbox;
12583         uint32_t reqlen, alloclen, index;
12584         uint32_t mbox_tmo;
12585         uint16_t rsrc_start, rsrc_size, els_xri_cnt;
12586         uint16_t xritag_start = 0, lxri = 0;
12587         struct lpfc_rsrc_blks *rsrc_blk;
12588         int cnt, ttl_cnt, rc = 0;
12589         int loop_cnt;
12590         uint32_t shdr_status, shdr_add_status;
12591         union lpfc_sli4_cfg_shdr *shdr;
12592
12593         /* The number of sgls to be posted */
12594         els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
12595
12596         reqlen = els_xri_cnt * sizeof(struct sgl_page_pairs) +
12597                  sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
12598         if (reqlen > SLI4_PAGE_SIZE) {
12599                 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
12600                                 "2989 Block sgl registration required DMA "
12601                                 "size (%d) great than a page\n", reqlen);
12602                 return -ENOMEM;
12603         }
12604
12605         cnt = 0;
12606         ttl_cnt = 0;
12607         list_for_each_entry(rsrc_blk, &phba->sli4_hba.lpfc_xri_blk_list,
12608                             list) {
12609                 rsrc_start = rsrc_blk->rsrc_start;
12610                 rsrc_size = rsrc_blk->rsrc_size;
12611
12612                 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
12613                                 "3014 Working ELS Extent start %d, cnt %d\n",
12614                                 rsrc_start, rsrc_size);
12615
12616                 loop_cnt = min(els_xri_cnt, rsrc_size);
12617                 if (ttl_cnt + loop_cnt >= els_xri_cnt) {
12618                         loop_cnt = els_xri_cnt - ttl_cnt;
12619                         ttl_cnt = els_xri_cnt;
12620                 }
12621
12622                 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12623                 if (!mbox)
12624                         return -ENOMEM;
12625                 /*
12626                  * Allocate DMA memory and set up the non-embedded mailbox
12627                  * command.
12628                  */
12629                 alloclen = lpfc_sli4_config(phba, mbox,
12630                                         LPFC_MBOX_SUBSYSTEM_FCOE,
12631                                         LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
12632                                         reqlen, LPFC_SLI4_MBX_NEMBED);
12633                 if (alloclen < reqlen) {
12634                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12635                                         "2987 Allocated DMA memory size (%d) "
12636                                         "is less than the requested DMA memory "
12637                                         "size (%d)\n", alloclen, reqlen);
12638                         lpfc_sli4_mbox_cmd_free(phba, mbox);
12639                         return -ENOMEM;
12640                 }
12641
12642                 /* Set up the SGL pages in the non-embedded DMA pages */
12643                 viraddr = mbox->sge_array->addr[0];
12644                 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
12645                 sgl_pg_pairs = &sgl->sgl_pg_pairs;
12646
12647                 /*
12648                  * The starting resource may not begin at zero. Control
12649                  * the loop variants via the block resource parameters,
12650                  * but handle the sge pointers with a zero-based index
12651                  * that doesn't get reset per loop pass.
12652                  */
12653                 for (index = rsrc_start;
12654                      index < rsrc_start + loop_cnt;
12655                      index++) {
12656                         sglq_entry = phba->sli4_hba.lpfc_els_sgl_array[cnt];
12657
12658                         /*
12659                          * Assign the sglq a physical xri only if the driver
12660                          * has not initialized those resources.  A port reset
12661                          * only needs the sglq's posted.
12662                          */
12663                         if (bf_get(lpfc_xri_rsrc_rdy,
12664                                    &phba->sli4_hba.sli4_flags) !=
12665                                    LPFC_XRI_RSRC_RDY) {
12666                                 lxri = lpfc_sli4_next_xritag(phba);
12667                                 if (lxri == NO_XRI) {
12668                                         lpfc_sli4_mbox_cmd_free(phba, mbox);
12669                                         rc = -ENOMEM;
12670                                         goto err_exit;
12671                                 }
12672                                 sglq_entry->sli4_lxritag = lxri;
12673                                 sglq_entry->sli4_xritag =
12674                                                 phba->sli4_hba.xri_ids[lxri];
12675                         }
12676
12677                         /* Set up the sge entry */
12678                         sgl_pg_pairs->sgl_pg0_addr_lo =
12679                                 cpu_to_le32(putPaddrLow(sglq_entry->phys));
12680                         sgl_pg_pairs->sgl_pg0_addr_hi =
12681                                 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
12682                         sgl_pg_pairs->sgl_pg1_addr_lo =
12683                                 cpu_to_le32(putPaddrLow(0));
12684                         sgl_pg_pairs->sgl_pg1_addr_hi =
12685                                 cpu_to_le32(putPaddrHigh(0));
12686
12687                         /* Track the starting physical XRI for the mailbox. */
12688                         if (index == rsrc_start)
12689                                 xritag_start = sglq_entry->sli4_xritag;
12690                         sgl_pg_pairs++;
12691                         cnt++;
12692                 }
12693
12694                 /* Complete initialization and perform endian conversion. */
12695                 rsrc_blk->rsrc_used += loop_cnt;
12696                 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
12697                 bf_set(lpfc_post_sgl_pages_xricnt, sgl, loop_cnt);
12698                 sgl->word0 = cpu_to_le32(sgl->word0);
12699
12700                 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
12701                                 "3015 Post ELS Extent SGL, start %d, "
12702                                 "cnt %d, used %d\n",
12703                                 xritag_start, loop_cnt, rsrc_blk->rsrc_used);
12704                 if (!phba->sli4_hba.intr_enable)
12705                         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12706                 else {
12707                         mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
12708                         rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
12709                 }
12710                 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
12711                 shdr_status = bf_get(lpfc_mbox_hdr_status,
12712                                      &shdr->response);
12713                 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
12714                                          &shdr->response);
12715                 if (rc != MBX_TIMEOUT)
12716                         lpfc_sli4_mbox_cmd_free(phba, mbox);
12717                 if (shdr_status || shdr_add_status || rc) {
12718                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12719                                         "2988 POST_SGL_BLOCK mailbox "
12720                                         "command failed status x%x "
12721                                         "add_status x%x mbx status x%x\n",
12722                                         shdr_status, shdr_add_status, rc);
12723                         rc = -ENXIO;
12724                         goto err_exit;
12725                 }
12726                 if (ttl_cnt >= els_xri_cnt)
12727                         break;
12728         }
12729
12730  err_exit:
12731         if (rc == 0)
12732                 bf_set(lpfc_xri_rsrc_rdy, &phba->sli4_hba.sli4_flags,
12733                        LPFC_XRI_RSRC_RDY);
12734         return rc;
12735 }
12736
12737 /**
12738  * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
12739  * @phba: pointer to lpfc hba data structure.
12740  * @sblist: pointer to scsi buffer list.
12741  * @count: number of scsi buffers on the list.
12742  *
12743  * This routine is invoked to post a block of @count scsi sgl pages from a
12744  * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
12745  * No Lock is held.
12746  *
12747  **/
12748 int
12749 lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba, struct list_head *sblist,
12750                               int cnt)
12751 {
12752         struct lpfc_scsi_buf *psb;
12753         struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
12754         struct sgl_page_pairs *sgl_pg_pairs;
12755         void *viraddr;
12756         LPFC_MBOXQ_t *mbox;
12757         uint32_t reqlen, alloclen, pg_pairs;
12758         uint32_t mbox_tmo;
12759         uint16_t xritag_start = 0;
12760         int rc = 0;
12761         uint32_t shdr_status, shdr_add_status;
12762         dma_addr_t pdma_phys_bpl1;
12763         union lpfc_sli4_cfg_shdr *shdr;
12764
12765         /* Calculate the requested length of the dma memory */
12766         reqlen = cnt * sizeof(struct sgl_page_pairs) +
12767                  sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
12768         if (reqlen > SLI4_PAGE_SIZE) {
12769                 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
12770                                 "0217 Block sgl registration required DMA "
12771                                 "size (%d) great than a page\n", reqlen);
12772                 return -ENOMEM;
12773         }
12774         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12775         if (!mbox) {
12776                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12777                                 "0283 Failed to allocate mbox cmd memory\n");
12778                 return -ENOMEM;
12779         }
12780
12781         /* Allocate DMA memory and set up the non-embedded mailbox command */
12782         alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12783                                 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
12784                                 LPFC_SLI4_MBX_NEMBED);
12785
12786         if (alloclen < reqlen) {
12787                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12788                                 "2561 Allocated DMA memory size (%d) is "
12789                                 "less than the requested DMA memory "
12790                                 "size (%d)\n", alloclen, reqlen);
12791                 lpfc_sli4_mbox_cmd_free(phba, mbox);
12792                 return -ENOMEM;
12793         }
12794
12795         /* Get the first SGE entry from the non-embedded DMA memory */
12796         viraddr = mbox->sge_array->addr[0];
12797
12798         /* Set up the SGL pages in the non-embedded DMA pages */
12799         sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
12800         sgl_pg_pairs = &sgl->sgl_pg_pairs;
12801
12802         pg_pairs = 0;
12803         list_for_each_entry(psb, sblist, list) {
12804                 /* Set up the sge entry */
12805                 sgl_pg_pairs->sgl_pg0_addr_lo =
12806                         cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
12807                 sgl_pg_pairs->sgl_pg0_addr_hi =
12808                         cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
12809                 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
12810                         pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
12811                 else
12812                         pdma_phys_bpl1 = 0;
12813                 sgl_pg_pairs->sgl_pg1_addr_lo =
12814                         cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
12815                 sgl_pg_pairs->sgl_pg1_addr_hi =
12816                         cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
12817                 /* Keep the first xritag on the list */
12818                 if (pg_pairs == 0)
12819                         xritag_start = psb->cur_iocbq.sli4_xritag;
12820                 sgl_pg_pairs++;
12821                 pg_pairs++;
12822         }
12823         bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
12824         bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
12825         /* Perform endian conversion if necessary */
12826         sgl->word0 = cpu_to_le32(sgl->word0);
12827
12828         if (!phba->sli4_hba.intr_enable)
12829                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12830         else {
12831                 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
12832                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
12833         }
12834         shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
12835         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12836         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12837         if (rc != MBX_TIMEOUT)
12838                 lpfc_sli4_mbox_cmd_free(phba, mbox);
12839         if (shdr_status || shdr_add_status || rc) {
12840                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12841                                 "2564 POST_SGL_BLOCK mailbox command failed "
12842                                 "status x%x add_status x%x mbx status x%x\n",
12843                                 shdr_status, shdr_add_status, rc);
12844                 rc = -ENXIO;
12845         }
12846         return rc;
12847 }
12848
12849 /**
12850  * lpfc_sli4_post_scsi_sgl_blk_ext - post a block of scsi sgls to the port.
12851  * @phba: pointer to lpfc hba data structure.
12852  * @sblist: pointer to scsi buffer list.
12853  * @count: number of scsi buffers on the list.
12854  *
12855  * This routine is invoked to post a block of @count scsi sgl pages from a
12856  * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
12857  * No Lock is held.
12858  *
12859  **/
12860 int
12861 lpfc_sli4_post_scsi_sgl_blk_ext(struct lpfc_hba *phba, struct list_head *sblist,
12862                                 int cnt)
12863 {
12864         struct lpfc_scsi_buf *psb = NULL;
12865         struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
12866         struct sgl_page_pairs *sgl_pg_pairs;
12867         void *viraddr;
12868         LPFC_MBOXQ_t *mbox;
12869         uint32_t reqlen, alloclen, pg_pairs;
12870         uint32_t mbox_tmo;
12871         uint16_t xri_start = 0, scsi_xri_start;
12872         uint16_t rsrc_range;
12873         int rc = 0, avail_cnt;
12874         uint32_t shdr_status, shdr_add_status;
12875         dma_addr_t pdma_phys_bpl1;
12876         union lpfc_sli4_cfg_shdr *shdr;
12877         struct lpfc_rsrc_blks *rsrc_blk;
12878         uint32_t xri_cnt = 0;
12879
12880         /* Calculate the total requested length of the dma memory */
12881         reqlen = cnt * sizeof(struct sgl_page_pairs) +
12882                  sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
12883         if (reqlen > SLI4_PAGE_SIZE) {
12884                 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
12885                                 "2932 Block sgl registration required DMA "
12886                                 "size (%d) great than a page\n", reqlen);
12887                 return -ENOMEM;
12888         }
12889
12890         /*
12891          * The use of extents requires the driver to post the sgl headers
12892          * in multiple postings to meet the contiguous resource assignment.
12893          */
12894         psb = list_prepare_entry(psb, sblist, list);
12895         scsi_xri_start = phba->sli4_hba.scsi_xri_start;
12896         list_for_each_entry(rsrc_blk, &phba->sli4_hba.lpfc_xri_blk_list,
12897                             list) {
12898                 rsrc_range = rsrc_blk->rsrc_start + rsrc_blk->rsrc_size;
12899                 if (rsrc_range < scsi_xri_start)
12900                         continue;
12901                 else if (rsrc_blk->rsrc_used >= rsrc_blk->rsrc_size)
12902                         continue;
12903                 else
12904                         avail_cnt = rsrc_blk->rsrc_size - rsrc_blk->rsrc_used;
12905
12906                 reqlen = (avail_cnt * sizeof(struct sgl_page_pairs)) +
12907                         sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
12908                 /*
12909                  * Allocate DMA memory and set up the non-embedded mailbox
12910                  * command. The mbox is used to post an SGL page per loop
12911                  * but the DMA memory has a use-once semantic so the mailbox
12912                  * is used and freed per loop pass.
12913                  */
12914                 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12915                 if (!mbox) {
12916                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12917                                         "2933 Failed to allocate mbox cmd "
12918                                         "memory\n");
12919                         return -ENOMEM;
12920                 }
12921                 alloclen = lpfc_sli4_config(phba, mbox,
12922                                         LPFC_MBOX_SUBSYSTEM_FCOE,
12923                                         LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
12924                                         reqlen,
12925                                         LPFC_SLI4_MBX_NEMBED);
12926                 if (alloclen < reqlen) {
12927                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12928                                         "2934 Allocated DMA memory size (%d) "
12929                                         "is less than the requested DMA memory "
12930                                         "size (%d)\n", alloclen, reqlen);
12931                         lpfc_sli4_mbox_cmd_free(phba, mbox);
12932                         return -ENOMEM;
12933                 }
12934
12935                 /* Get the first SGE entry from the non-embedded DMA memory */
12936                 viraddr = mbox->sge_array->addr[0];
12937
12938                 /* Set up the SGL pages in the non-embedded DMA pages */
12939                 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
12940                 sgl_pg_pairs = &sgl->sgl_pg_pairs;
12941
12942                 /* pg_pairs tracks posted SGEs per loop iteration. */
12943                 pg_pairs = 0;
12944                 list_for_each_entry_continue(psb, sblist, list) {
12945                         /* Set up the sge entry */
12946                         sgl_pg_pairs->sgl_pg0_addr_lo =
12947                                 cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
12948                         sgl_pg_pairs->sgl_pg0_addr_hi =
12949                                 cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
12950                         if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
12951                                 pdma_phys_bpl1 = psb->dma_phys_bpl +
12952                                         SGL_PAGE_SIZE;
12953                         else
12954                                 pdma_phys_bpl1 = 0;
12955                         sgl_pg_pairs->sgl_pg1_addr_lo =
12956                                 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
12957                         sgl_pg_pairs->sgl_pg1_addr_hi =
12958                                 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
12959                         /* Keep the first xri for this extent. */
12960                         if (pg_pairs == 0)
12961                                 xri_start = psb->cur_iocbq.sli4_xritag;
12962                         sgl_pg_pairs++;
12963                         pg_pairs++;
12964                         xri_cnt++;
12965
12966                         /*
12967                          * Track two exit conditions - the loop has constructed
12968                          * all of the caller's SGE pairs or all available
12969                          * resource IDs in this extent are consumed.
12970                          */
12971                         if ((xri_cnt == cnt) || (pg_pairs >= avail_cnt))
12972                                 break;
12973                 }
12974                 rsrc_blk->rsrc_used += pg_pairs;
12975                 bf_set(lpfc_post_sgl_pages_xri, sgl, xri_start);
12976                 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
12977
12978                 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
12979                                 "3016 Post SCSI Extent SGL, start %d, cnt %d "
12980                                 "blk use %d\n",
12981                                 xri_start, pg_pairs, rsrc_blk->rsrc_used);
12982                 /* Perform endian conversion if necessary */
12983                 sgl->word0 = cpu_to_le32(sgl->word0);
12984                 if (!phba->sli4_hba.intr_enable)
12985                         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12986                 else {
12987                         mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
12988                         rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
12989                 }
12990                 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
12991                 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12992                 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
12993                                          &shdr->response);
12994                 if (rc != MBX_TIMEOUT)
12995                         lpfc_sli4_mbox_cmd_free(phba, mbox);
12996                 if (shdr_status || shdr_add_status || rc) {
12997                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12998                                         "2935 POST_SGL_BLOCK mailbox command "
12999                                         "failed status x%x add_status x%x "
13000                                         "mbx status x%x\n",
13001                                         shdr_status, shdr_add_status, rc);
13002                         return -ENXIO;
13003                 }
13004
13005                 /* Post only what is requested. */
13006                 if (xri_cnt >= cnt)
13007                         break;
13008         }
13009         return rc;
13010 }
13011
13012 /**
13013  * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
13014  * @phba: pointer to lpfc_hba struct that the frame was received on
13015  * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
13016  *
13017  * This function checks the fields in the @fc_hdr to see if the FC frame is a
13018  * valid type of frame that the LPFC driver will handle. This function will
13019  * return a zero if the frame is a valid frame or a non zero value when the
13020  * frame does not pass the check.
13021  **/
13022 static int
13023 lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
13024 {
13025         /*  make rctl_names static to save stack space */
13026         static char *rctl_names[] = FC_RCTL_NAMES_INIT;
13027         char *type_names[] = FC_TYPE_NAMES_INIT;
13028         struct fc_vft_header *fc_vft_hdr;
13029         uint32_t *header = (uint32_t *) fc_hdr;
13030
13031         switch (fc_hdr->fh_r_ctl) {
13032         case FC_RCTL_DD_UNCAT:          /* uncategorized information */
13033         case FC_RCTL_DD_SOL_DATA:       /* solicited data */
13034         case FC_RCTL_DD_UNSOL_CTL:      /* unsolicited control */
13035         case FC_RCTL_DD_SOL_CTL:        /* solicited control or reply */
13036         case FC_RCTL_DD_UNSOL_DATA:     /* unsolicited data */
13037         case FC_RCTL_DD_DATA_DESC:      /* data descriptor */
13038         case FC_RCTL_DD_UNSOL_CMD:      /* unsolicited command */
13039         case FC_RCTL_DD_CMD_STATUS:     /* command status */
13040         case FC_RCTL_ELS_REQ:   /* extended link services request */
13041         case FC_RCTL_ELS_REP:   /* extended link services reply */
13042         case FC_RCTL_ELS4_REQ:  /* FC-4 ELS request */
13043         case FC_RCTL_ELS4_REP:  /* FC-4 ELS reply */
13044         case FC_RCTL_BA_NOP:    /* basic link service NOP */
13045         case FC_RCTL_BA_ABTS:   /* basic link service abort */
13046         case FC_RCTL_BA_RMC:    /* remove connection */
13047         case FC_RCTL_BA_ACC:    /* basic accept */
13048         case FC_RCTL_BA_RJT:    /* basic reject */
13049         case FC_RCTL_BA_PRMT:
13050         case FC_RCTL_ACK_1:     /* acknowledge_1 */
13051         case FC_RCTL_ACK_0:     /* acknowledge_0 */
13052         case FC_RCTL_P_RJT:     /* port reject */
13053         case FC_RCTL_F_RJT:     /* fabric reject */
13054         case FC_RCTL_P_BSY:     /* port busy */
13055         case FC_RCTL_F_BSY:     /* fabric busy to data frame */
13056         case FC_RCTL_F_BSYL:    /* fabric busy to link control frame */
13057         case FC_RCTL_LCR:       /* link credit reset */
13058         case FC_RCTL_END:       /* end */
13059                 break;
13060         case FC_RCTL_VFTH:      /* Virtual Fabric tagging Header */
13061                 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
13062                 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
13063                 return lpfc_fc_frame_check(phba, fc_hdr);
13064         default:
13065                 goto drop;
13066         }
13067         switch (fc_hdr->fh_type) {
13068         case FC_TYPE_BLS:
13069         case FC_TYPE_ELS:
13070         case FC_TYPE_FCP:
13071         case FC_TYPE_CT:
13072                 break;
13073         case FC_TYPE_IP:
13074         case FC_TYPE_ILS:
13075         default:
13076                 goto drop;
13077         }
13078
13079         lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
13080                         "2538 Received frame rctl:%s type:%s "
13081                         "Frame Data:%08x %08x %08x %08x %08x %08x\n",
13082                         rctl_names[fc_hdr->fh_r_ctl],
13083                         type_names[fc_hdr->fh_type],
13084                         be32_to_cpu(header[0]), be32_to_cpu(header[1]),
13085                         be32_to_cpu(header[2]), be32_to_cpu(header[3]),
13086                         be32_to_cpu(header[4]), be32_to_cpu(header[5]));
13087         return 0;
13088 drop:
13089         lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
13090                         "2539 Dropped frame rctl:%s type:%s\n",
13091                         rctl_names[fc_hdr->fh_r_ctl],
13092                         type_names[fc_hdr->fh_type]);
13093         return 1;
13094 }
13095
13096 /**
13097  * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
13098  * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
13099  *
13100  * This function processes the FC header to retrieve the VFI from the VF
13101  * header, if one exists. This function will return the VFI if one exists
13102  * or 0 if no VSAN Header exists.
13103  **/
13104 static uint32_t
13105 lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
13106 {
13107         struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
13108
13109         if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
13110                 return 0;
13111         return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
13112 }
13113
13114 /**
13115  * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
13116  * @phba: Pointer to the HBA structure to search for the vport on
13117  * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
13118  * @fcfi: The FC Fabric ID that the frame came from
13119  *
13120  * This function searches the @phba for a vport that matches the content of the
13121  * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
13122  * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
13123  * returns the matching vport pointer or NULL if unable to match frame to a
13124  * vport.
13125  **/
13126 static struct lpfc_vport *
13127 lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
13128                        uint16_t fcfi)
13129 {
13130         struct lpfc_vport **vports;
13131         struct lpfc_vport *vport = NULL;
13132         int i;
13133         uint32_t did = (fc_hdr->fh_d_id[0] << 16 |
13134                         fc_hdr->fh_d_id[1] << 8 |
13135                         fc_hdr->fh_d_id[2]);
13136
13137         vports = lpfc_create_vport_work_array(phba);
13138         if (vports != NULL)
13139                 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
13140                         if (phba->fcf.fcfi == fcfi &&
13141                             vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
13142                             vports[i]->fc_myDID == did) {
13143                                 vport = vports[i];
13144                                 break;
13145                         }
13146                 }
13147         lpfc_destroy_vport_work_array(phba, vports);
13148         return vport;
13149 }
13150
13151 /**
13152  * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
13153  * @vport: The vport to work on.
13154  *
13155  * This function updates the receive sequence time stamp for this vport. The
13156  * receive sequence time stamp indicates the time that the last frame of the
13157  * the sequence that has been idle for the longest amount of time was received.
13158  * the driver uses this time stamp to indicate if any received sequences have
13159  * timed out.
13160  **/
13161 void
13162 lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
13163 {
13164         struct lpfc_dmabuf *h_buf;
13165         struct hbq_dmabuf *dmabuf = NULL;
13166
13167         /* get the oldest sequence on the rcv list */
13168         h_buf = list_get_first(&vport->rcv_buffer_list,
13169                                struct lpfc_dmabuf, list);
13170         if (!h_buf)
13171                 return;
13172         dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
13173         vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
13174 }
13175
13176 /**
13177  * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
13178  * @vport: The vport that the received sequences were sent to.
13179  *
13180  * This function cleans up all outstanding received sequences. This is called
13181  * by the driver when a link event or user action invalidates all the received
13182  * sequences.
13183  **/
13184 void
13185 lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
13186 {
13187         struct lpfc_dmabuf *h_buf, *hnext;
13188         struct lpfc_dmabuf *d_buf, *dnext;
13189         struct hbq_dmabuf *dmabuf = NULL;
13190
13191         /* start with the oldest sequence on the rcv list */
13192         list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
13193                 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
13194                 list_del_init(&dmabuf->hbuf.list);
13195                 list_for_each_entry_safe(d_buf, dnext,
13196                                          &dmabuf->dbuf.list, list) {
13197                         list_del_init(&d_buf->list);
13198                         lpfc_in_buf_free(vport->phba, d_buf);
13199                 }
13200                 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
13201         }
13202 }
13203
13204 /**
13205  * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
13206  * @vport: The vport that the received sequences were sent to.
13207  *
13208  * This function determines whether any received sequences have timed out by
13209  * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
13210  * indicates that there is at least one timed out sequence this routine will
13211  * go through the received sequences one at a time from most inactive to most
13212  * active to determine which ones need to be cleaned up. Once it has determined
13213  * that a sequence needs to be cleaned up it will simply free up the resources
13214  * without sending an abort.
13215  **/
13216 void
13217 lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
13218 {
13219         struct lpfc_dmabuf *h_buf, *hnext;
13220         struct lpfc_dmabuf *d_buf, *dnext;
13221         struct hbq_dmabuf *dmabuf = NULL;
13222         unsigned long timeout;
13223         int abort_count = 0;
13224
13225         timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
13226                    vport->rcv_buffer_time_stamp);
13227         if (list_empty(&vport->rcv_buffer_list) ||
13228             time_before(jiffies, timeout))
13229                 return;
13230         /* start with the oldest sequence on the rcv list */
13231         list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
13232                 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
13233                 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
13234                            dmabuf->time_stamp);
13235                 if (time_before(jiffies, timeout))
13236                         break;
13237                 abort_count++;
13238                 list_del_init(&dmabuf->hbuf.list);
13239                 list_for_each_entry_safe(d_buf, dnext,
13240                                          &dmabuf->dbuf.list, list) {
13241                         list_del_init(&d_buf->list);
13242                         lpfc_in_buf_free(vport->phba, d_buf);
13243                 }
13244                 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
13245         }
13246         if (abort_count)
13247                 lpfc_update_rcv_time_stamp(vport);
13248 }
13249
13250 /**
13251  * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
13252  * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
13253  *
13254  * This function searches through the existing incomplete sequences that have
13255  * been sent to this @vport. If the frame matches one of the incomplete
13256  * sequences then the dbuf in the @dmabuf is added to the list of frames that
13257  * make up that sequence. If no sequence is found that matches this frame then
13258  * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
13259  * This function returns a pointer to the first dmabuf in the sequence list that
13260  * the frame was linked to.
13261  **/
13262 static struct hbq_dmabuf *
13263 lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
13264 {
13265         struct fc_frame_header *new_hdr;
13266         struct fc_frame_header *temp_hdr;
13267         struct lpfc_dmabuf *d_buf;
13268         struct lpfc_dmabuf *h_buf;
13269         struct hbq_dmabuf *seq_dmabuf = NULL;
13270         struct hbq_dmabuf *temp_dmabuf = NULL;
13271
13272         INIT_LIST_HEAD(&dmabuf->dbuf.list);
13273         dmabuf->time_stamp = jiffies;
13274         new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
13275         /* Use the hdr_buf to find the sequence that this frame belongs to */
13276         list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
13277                 temp_hdr = (struct fc_frame_header *)h_buf->virt;
13278                 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
13279                     (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
13280                     (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
13281                         continue;
13282                 /* found a pending sequence that matches this frame */
13283                 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
13284                 break;
13285         }
13286         if (!seq_dmabuf) {
13287                 /*
13288                  * This indicates first frame received for this sequence.
13289                  * Queue the buffer on the vport's rcv_buffer_list.
13290                  */
13291                 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
13292                 lpfc_update_rcv_time_stamp(vport);
13293                 return dmabuf;
13294         }
13295         temp_hdr = seq_dmabuf->hbuf.virt;
13296         if (be16_to_cpu(new_hdr->fh_seq_cnt) <
13297                 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
13298                 list_del_init(&seq_dmabuf->hbuf.list);
13299                 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
13300                 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
13301                 lpfc_update_rcv_time_stamp(vport);
13302                 return dmabuf;
13303         }
13304         /* move this sequence to the tail to indicate a young sequence */
13305         list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
13306         seq_dmabuf->time_stamp = jiffies;
13307         lpfc_update_rcv_time_stamp(vport);
13308         if (list_empty(&seq_dmabuf->dbuf.list)) {
13309                 temp_hdr = dmabuf->hbuf.virt;
13310                 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
13311                 return seq_dmabuf;
13312         }
13313         /* find the correct place in the sequence to insert this frame */
13314         list_for_each_entry_reverse(d_buf, &seq_dmabuf->dbuf.list, list) {
13315                 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
13316                 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
13317                 /*
13318                  * If the frame's sequence count is greater than the frame on
13319                  * the list then insert the frame right after this frame
13320                  */
13321                 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
13322                         be16_to_cpu(temp_hdr->fh_seq_cnt)) {
13323                         list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
13324                         return seq_dmabuf;
13325                 }
13326         }
13327         return NULL;
13328 }
13329
13330 /**
13331  * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
13332  * @vport: pointer to a vitural port
13333  * @dmabuf: pointer to a dmabuf that describes the FC sequence
13334  *
13335  * This function tries to abort from the partially assembed sequence, described
13336  * by the information from basic abbort @dmabuf. It checks to see whether such
13337  * partially assembled sequence held by the driver. If so, it shall free up all
13338  * the frames from the partially assembled sequence.
13339  *
13340  * Return
13341  * true  -- if there is matching partially assembled sequence present and all
13342  *          the frames freed with the sequence;
13343  * false -- if there is no matching partially assembled sequence present so
13344  *          nothing got aborted in the lower layer driver
13345  **/
13346 static bool
13347 lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
13348                             struct hbq_dmabuf *dmabuf)
13349 {
13350         struct fc_frame_header *new_hdr;
13351         struct fc_frame_header *temp_hdr;
13352         struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
13353         struct hbq_dmabuf *seq_dmabuf = NULL;
13354
13355         /* Use the hdr_buf to find the sequence that matches this frame */
13356         INIT_LIST_HEAD(&dmabuf->dbuf.list);
13357         INIT_LIST_HEAD(&dmabuf->hbuf.list);
13358         new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
13359         list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
13360                 temp_hdr = (struct fc_frame_header *)h_buf->virt;
13361                 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
13362                     (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
13363                     (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
13364                         continue;
13365                 /* found a pending sequence that matches this frame */
13366                 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
13367                 break;
13368         }
13369
13370         /* Free up all the frames from the partially assembled sequence */
13371         if (seq_dmabuf) {
13372                 list_for_each_entry_safe(d_buf, n_buf,
13373                                          &seq_dmabuf->dbuf.list, list) {
13374                         list_del_init(&d_buf->list);
13375                         lpfc_in_buf_free(vport->phba, d_buf);
13376                 }
13377                 return true;
13378         }
13379         return false;
13380 }
13381
13382 /**
13383  * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
13384  * @phba: Pointer to HBA context object.
13385  * @cmd_iocbq: pointer to the command iocbq structure.
13386  * @rsp_iocbq: pointer to the response iocbq structure.
13387  *
13388  * This function handles the sequence abort response iocb command complete
13389  * event. It properly releases the memory allocated to the sequence abort
13390  * accept iocb.
13391  **/
13392 static void
13393 lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
13394                              struct lpfc_iocbq *cmd_iocbq,
13395                              struct lpfc_iocbq *rsp_iocbq)
13396 {
13397         if (cmd_iocbq)
13398                 lpfc_sli_release_iocbq(phba, cmd_iocbq);
13399 }
13400
13401 /**
13402  * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
13403  * @phba: Pointer to HBA context object.
13404  * @xri: xri id in transaction.
13405  *
13406  * This function validates the xri maps to the known range of XRIs allocated an
13407  * used by the driver.
13408  **/
13409 static uint16_t
13410 lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
13411                       uint16_t xri)
13412 {
13413         int i;
13414
13415         for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
13416                 if (xri == phba->sli4_hba.xri_ids[i])
13417                         return i;
13418         }
13419         return NO_XRI;
13420 }
13421
13422
13423 /**
13424  * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
13425  * @phba: Pointer to HBA context object.
13426  * @fc_hdr: pointer to a FC frame header.
13427  *
13428  * This function sends a basic response to a previous unsol sequence abort
13429  * event after aborting the sequence handling.
13430  **/
13431 static void
13432 lpfc_sli4_seq_abort_rsp(struct lpfc_hba *phba,
13433                         struct fc_frame_header *fc_hdr)
13434 {
13435         struct lpfc_iocbq *ctiocb = NULL;
13436         struct lpfc_nodelist *ndlp;
13437         uint16_t oxid, rxid;
13438         uint32_t sid, fctl;
13439         IOCB_t *icmd;
13440         int rc;
13441
13442         if (!lpfc_is_link_up(phba))
13443                 return;
13444
13445         sid = sli4_sid_from_fc_hdr(fc_hdr);
13446         oxid = be16_to_cpu(fc_hdr->fh_ox_id);
13447         rxid = be16_to_cpu(fc_hdr->fh_rx_id);
13448
13449         ndlp = lpfc_findnode_did(phba->pport, sid);
13450         if (!ndlp) {
13451                 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
13452                                 "1268 Find ndlp returned NULL for oxid:x%x "
13453                                 "SID:x%x\n", oxid, sid);
13454                 return;
13455         }
13456         if (lpfc_sli4_xri_inrange(phba, rxid))
13457                 lpfc_set_rrq_active(phba, ndlp, rxid, oxid, 0);
13458
13459         /* Allocate buffer for rsp iocb */
13460         ctiocb = lpfc_sli_get_iocbq(phba);
13461         if (!ctiocb)
13462                 return;
13463
13464         /* Extract the F_CTL field from FC_HDR */
13465         fctl = sli4_fctl_from_fc_hdr(fc_hdr);
13466
13467         icmd = &ctiocb->iocb;
13468         icmd->un.xseq64.bdl.bdeSize = 0;
13469         icmd->un.xseq64.bdl.ulpIoTag32 = 0;
13470         icmd->un.xseq64.w5.hcsw.Dfctl = 0;
13471         icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
13472         icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
13473
13474         /* Fill in the rest of iocb fields */
13475         icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
13476         icmd->ulpBdeCount = 0;
13477         icmd->ulpLe = 1;
13478         icmd->ulpClass = CLASS3;
13479         icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
13480         ctiocb->context1 = ndlp;
13481
13482         ctiocb->iocb_cmpl = NULL;
13483         ctiocb->vport = phba->pport;
13484         ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
13485         ctiocb->sli4_lxritag = NO_XRI;
13486         ctiocb->sli4_xritag = NO_XRI;
13487
13488         /* If the oxid maps to the FCP XRI range or if it is out of range,
13489          * send a BLS_RJT.  The driver no longer has that exchange.
13490          * Override the IOCB for a BA_RJT.
13491          */
13492         if (oxid > (phba->sli4_hba.max_cfg_param.max_xri +
13493                     phba->sli4_hba.max_cfg_param.xri_base) ||
13494             oxid > (lpfc_sli4_get_els_iocb_cnt(phba) +
13495                     phba->sli4_hba.max_cfg_param.xri_base)) {
13496                 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
13497                 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
13498                 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
13499                 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
13500         }
13501
13502         if (fctl & FC_FC_EX_CTX) {
13503                 /* ABTS sent by responder to CT exchange, construction
13504                  * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
13505                  * field and RX_ID from ABTS for RX_ID field.
13506                  */
13507                 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
13508                 bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
13509         } else {
13510                 /* ABTS sent by initiator to CT exchange, construction
13511                  * of BA_ACC will need to allocate a new XRI as for the
13512                  * XRI_TAG and RX_ID fields.
13513                  */
13514                 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
13515                 bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, NO_XRI);
13516         }
13517         bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
13518
13519         /* Xmit CT abts response on exchange <xid> */
13520         lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
13521                         "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
13522                         icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
13523
13524         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
13525         if (rc == IOCB_ERROR) {
13526                 lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
13527                                 "2925 Failed to issue CT ABTS RSP x%x on "
13528                                 "xri x%x, Data x%x\n",
13529                                 icmd->un.xseq64.w5.hcsw.Rctl, oxid,
13530                                 phba->link_state);
13531                 lpfc_sli_release_iocbq(phba, ctiocb);
13532         }
13533 }
13534
13535 /**
13536  * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
13537  * @vport: Pointer to the vport on which this sequence was received
13538  * @dmabuf: pointer to a dmabuf that describes the FC sequence
13539  *
13540  * This function handles an SLI-4 unsolicited abort event. If the unsolicited
13541  * receive sequence is only partially assembed by the driver, it shall abort
13542  * the partially assembled frames for the sequence. Otherwise, if the
13543  * unsolicited receive sequence has been completely assembled and passed to
13544  * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
13545  * unsolicited sequence has been aborted. After that, it will issue a basic
13546  * accept to accept the abort.
13547  **/
13548 void
13549 lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
13550                              struct hbq_dmabuf *dmabuf)
13551 {
13552         struct lpfc_hba *phba = vport->phba;
13553         struct fc_frame_header fc_hdr;
13554         uint32_t fctl;
13555         bool abts_par;
13556
13557         /* Make a copy of fc_hdr before the dmabuf being released */
13558         memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
13559         fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
13560
13561         if (fctl & FC_FC_EX_CTX) {
13562                 /*
13563                  * ABTS sent by responder to exchange, just free the buffer
13564                  */
13565                 lpfc_in_buf_free(phba, &dmabuf->dbuf);
13566         } else {
13567                 /*
13568                  * ABTS sent by initiator to exchange, need to do cleanup
13569                  */
13570                 /* Try to abort partially assembled seq */
13571                 abts_par = lpfc_sli4_abort_partial_seq(vport, dmabuf);
13572
13573                 /* Send abort to ULP if partially seq abort failed */
13574                 if (abts_par == false)
13575                         lpfc_sli4_send_seq_to_ulp(vport, dmabuf);
13576                 else
13577                         lpfc_in_buf_free(phba, &dmabuf->dbuf);
13578         }
13579         /* Send basic accept (BA_ACC) to the abort requester */
13580         lpfc_sli4_seq_abort_rsp(phba, &fc_hdr);
13581 }
13582
13583 /**
13584  * lpfc_seq_complete - Indicates if a sequence is complete
13585  * @dmabuf: pointer to a dmabuf that describes the FC sequence
13586  *
13587  * This function checks the sequence, starting with the frame described by
13588  * @dmabuf, to see if all the frames associated with this sequence are present.
13589  * the frames associated with this sequence are linked to the @dmabuf using the
13590  * dbuf list. This function looks for two major things. 1) That the first frame
13591  * has a sequence count of zero. 2) There is a frame with last frame of sequence
13592  * set. 3) That there are no holes in the sequence count. The function will
13593  * return 1 when the sequence is complete, otherwise it will return 0.
13594  **/
13595 static int
13596 lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
13597 {
13598         struct fc_frame_header *hdr;
13599         struct lpfc_dmabuf *d_buf;
13600         struct hbq_dmabuf *seq_dmabuf;
13601         uint32_t fctl;
13602         int seq_count = 0;
13603
13604         hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
13605         /* make sure first fame of sequence has a sequence count of zero */
13606         if (hdr->fh_seq_cnt != seq_count)
13607                 return 0;
13608         fctl = (hdr->fh_f_ctl[0] << 16 |
13609                 hdr->fh_f_ctl[1] << 8 |
13610                 hdr->fh_f_ctl[2]);
13611         /* If last frame of sequence we can return success. */
13612         if (fctl & FC_FC_END_SEQ)
13613                 return 1;
13614         list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
13615                 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
13616                 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
13617                 /* If there is a hole in the sequence count then fail. */
13618                 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
13619                         return 0;
13620                 fctl = (hdr->fh_f_ctl[0] << 16 |
13621                         hdr->fh_f_ctl[1] << 8 |
13622                         hdr->fh_f_ctl[2]);
13623                 /* If last frame of sequence we can return success. */
13624                 if (fctl & FC_FC_END_SEQ)
13625                         return 1;
13626         }
13627         return 0;
13628 }
13629
13630 /**
13631  * lpfc_prep_seq - Prep sequence for ULP processing
13632  * @vport: Pointer to the vport on which this sequence was received
13633  * @dmabuf: pointer to a dmabuf that describes the FC sequence
13634  *
13635  * This function takes a sequence, described by a list of frames, and creates
13636  * a list of iocbq structures to describe the sequence. This iocbq list will be
13637  * used to issue to the generic unsolicited sequence handler. This routine
13638  * returns a pointer to the first iocbq in the list. If the function is unable
13639  * to allocate an iocbq then it throw out the received frames that were not
13640  * able to be described and return a pointer to the first iocbq. If unable to
13641  * allocate any iocbqs (including the first) this function will return NULL.
13642  **/
13643 static struct lpfc_iocbq *
13644 lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
13645 {
13646         struct lpfc_dmabuf *d_buf, *n_buf;
13647         struct lpfc_iocbq *first_iocbq, *iocbq;
13648         struct fc_frame_header *fc_hdr;
13649         uint32_t sid;
13650         struct ulp_bde64 *pbde;
13651
13652         fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
13653         /* remove from receive buffer list */
13654         list_del_init(&seq_dmabuf->hbuf.list);
13655         lpfc_update_rcv_time_stamp(vport);
13656         /* get the Remote Port's SID */
13657         sid = sli4_sid_from_fc_hdr(fc_hdr);
13658         /* Get an iocbq struct to fill in. */
13659         first_iocbq = lpfc_sli_get_iocbq(vport->phba);
13660         if (first_iocbq) {
13661                 /* Initialize the first IOCB. */
13662                 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
13663                 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
13664                 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
13665                 first_iocbq->iocb.ulpContext = be16_to_cpu(fc_hdr->fh_ox_id);
13666                 /* iocbq is prepped for internal consumption.  Logical vpi. */
13667                 first_iocbq->iocb.unsli3.rcvsli3.vpi = vport->vpi;
13668                 /* put the first buffer into the first IOCBq */
13669                 first_iocbq->context2 = &seq_dmabuf->dbuf;
13670                 first_iocbq->context3 = NULL;
13671                 first_iocbq->iocb.ulpBdeCount = 1;
13672                 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
13673                                                         LPFC_DATA_BUF_SIZE;
13674                 first_iocbq->iocb.un.rcvels.remoteID = sid;
13675                 first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
13676                                 bf_get(lpfc_rcqe_length,
13677                                        &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
13678         }
13679         iocbq = first_iocbq;
13680         /*
13681          * Each IOCBq can have two Buffers assigned, so go through the list
13682          * of buffers for this sequence and save two buffers in each IOCBq
13683          */
13684         list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
13685                 if (!iocbq) {
13686                         lpfc_in_buf_free(vport->phba, d_buf);
13687                         continue;
13688                 }
13689                 if (!iocbq->context3) {
13690                         iocbq->context3 = d_buf;
13691                         iocbq->iocb.ulpBdeCount++;
13692                         pbde = (struct ulp_bde64 *)
13693                                         &iocbq->iocb.unsli3.sli3Words[4];
13694                         pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
13695                         first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
13696                                 bf_get(lpfc_rcqe_length,
13697                                        &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
13698                 } else {
13699                         iocbq = lpfc_sli_get_iocbq(vport->phba);
13700                         if (!iocbq) {
13701                                 if (first_iocbq) {
13702                                         first_iocbq->iocb.ulpStatus =
13703                                                         IOSTAT_FCP_RSP_ERROR;
13704                                         first_iocbq->iocb.un.ulpWord[4] =
13705                                                         IOERR_NO_RESOURCES;
13706                                 }
13707                                 lpfc_in_buf_free(vport->phba, d_buf);
13708                                 continue;
13709                         }
13710                         iocbq->context2 = d_buf;
13711                         iocbq->context3 = NULL;
13712                         iocbq->iocb.ulpBdeCount = 1;
13713                         iocbq->iocb.un.cont64[0].tus.f.bdeSize =
13714                                                         LPFC_DATA_BUF_SIZE;
13715                         first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
13716                                 bf_get(lpfc_rcqe_length,
13717                                        &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
13718                         iocbq->iocb.un.rcvels.remoteID = sid;
13719                         list_add_tail(&iocbq->list, &first_iocbq->list);
13720                 }
13721         }
13722         return first_iocbq;
13723 }
13724
13725 static void
13726 lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
13727                           struct hbq_dmabuf *seq_dmabuf)
13728 {
13729         struct fc_frame_header *fc_hdr;
13730         struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
13731         struct lpfc_hba *phba = vport->phba;
13732
13733         fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
13734         iocbq = lpfc_prep_seq(vport, seq_dmabuf);
13735         if (!iocbq) {
13736                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13737                                 "2707 Ring %d handler: Failed to allocate "
13738                                 "iocb Rctl x%x Type x%x received\n",
13739                                 LPFC_ELS_RING,
13740                                 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
13741                 return;
13742         }
13743         if (!lpfc_complete_unsol_iocb(phba,
13744                                       &phba->sli.ring[LPFC_ELS_RING],
13745                                       iocbq, fc_hdr->fh_r_ctl,
13746                                       fc_hdr->fh_type))
13747                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13748                                 "2540 Ring %d handler: unexpected Rctl "
13749                                 "x%x Type x%x received\n",
13750                                 LPFC_ELS_RING,
13751                                 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
13752
13753         /* Free iocb created in lpfc_prep_seq */
13754         list_for_each_entry_safe(curr_iocb, next_iocb,
13755                 &iocbq->list, list) {
13756                 list_del_init(&curr_iocb->list);
13757                 lpfc_sli_release_iocbq(phba, curr_iocb);
13758         }
13759         lpfc_sli_release_iocbq(phba, iocbq);
13760 }
13761
13762 /**
13763  * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
13764  * @phba: Pointer to HBA context object.
13765  *
13766  * This function is called with no lock held. This function processes all
13767  * the received buffers and gives it to upper layers when a received buffer
13768  * indicates that it is the final frame in the sequence. The interrupt
13769  * service routine processes received buffers at interrupt contexts and adds
13770  * received dma buffers to the rb_pend_list queue and signals the worker thread.
13771  * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
13772  * appropriate receive function when the final frame in a sequence is received.
13773  **/
13774 void
13775 lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
13776                                  struct hbq_dmabuf *dmabuf)
13777 {
13778         struct hbq_dmabuf *seq_dmabuf;
13779         struct fc_frame_header *fc_hdr;
13780         struct lpfc_vport *vport;
13781         uint32_t fcfi;
13782
13783         /* Process each received buffer */
13784         fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
13785         /* check to see if this a valid type of frame */
13786         if (lpfc_fc_frame_check(phba, fc_hdr)) {
13787                 lpfc_in_buf_free(phba, &dmabuf->dbuf);
13788                 return;
13789         }
13790         fcfi = bf_get(lpfc_rcqe_fcf_id, &dmabuf->cq_event.cqe.rcqe_cmpl);
13791         vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi);
13792         if (!vport || !(vport->vpi_state & LPFC_VPI_REGISTERED)) {
13793                 /* throw out the frame */
13794                 lpfc_in_buf_free(phba, &dmabuf->dbuf);
13795                 return;
13796         }
13797         /* Handle the basic abort sequence (BA_ABTS) event */
13798         if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
13799                 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
13800                 return;
13801         }
13802
13803         /* Link this frame */
13804         seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
13805         if (!seq_dmabuf) {
13806                 /* unable to add frame to vport - throw it out */
13807                 lpfc_in_buf_free(phba, &dmabuf->dbuf);
13808                 return;
13809         }
13810         /* If not last frame in sequence continue processing frames. */
13811         if (!lpfc_seq_complete(seq_dmabuf))
13812                 return;
13813
13814         /* Send the complete sequence to the upper layer protocol */
13815         lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
13816 }
13817
13818 /**
13819  * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
13820  * @phba: pointer to lpfc hba data structure.
13821  *
13822  * This routine is invoked to post rpi header templates to the
13823  * HBA consistent with the SLI-4 interface spec.  This routine
13824  * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
13825  * SLI4_PAGE_SIZE modulo 64 rpi context headers.
13826  *
13827  * This routine does not require any locks.  It's usage is expected
13828  * to be driver load or reset recovery when the driver is
13829  * sequential.
13830  *
13831  * Return codes
13832  *      0 - successful
13833  *      -EIO - The mailbox failed to complete successfully.
13834  *      When this error occurs, the driver is not guaranteed
13835  *      to have any rpi regions posted to the device and
13836  *      must either attempt to repost the regions or take a
13837  *      fatal error.
13838  **/
13839 int
13840 lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
13841 {
13842         struct lpfc_rpi_hdr *rpi_page;
13843         uint32_t rc = 0;
13844         uint16_t lrpi = 0;
13845
13846         /* SLI4 ports that support extents do not require RPI headers. */
13847         if (!phba->sli4_hba.rpi_hdrs_in_use)
13848                 goto exit;
13849         if (phba->sli4_hba.extents_in_use)
13850                 return -EIO;
13851
13852         list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
13853                 /*
13854                  * Assign the rpi headers a physical rpi only if the driver
13855                  * has not initialized those resources.  A port reset only
13856                  * needs the headers posted.
13857                  */
13858                 if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
13859                     LPFC_RPI_RSRC_RDY)
13860                         rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
13861
13862                 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
13863                 if (rc != MBX_SUCCESS) {
13864                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13865                                         "2008 Error %d posting all rpi "
13866                                         "headers\n", rc);
13867                         rc = -EIO;
13868                         break;
13869                 }
13870         }
13871
13872  exit:
13873         bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
13874                LPFC_RPI_RSRC_RDY);
13875         return rc;
13876 }
13877
13878 /**
13879  * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
13880  * @phba: pointer to lpfc hba data structure.
13881  * @rpi_page:  pointer to the rpi memory region.
13882  *
13883  * This routine is invoked to post a single rpi header to the
13884  * HBA consistent with the SLI-4 interface spec.  This memory region
13885  * maps up to 64 rpi context regions.
13886  *
13887  * Return codes
13888  *      0 - successful
13889  *      -ENOMEM - No available memory
13890  *      -EIO - The mailbox failed to complete successfully.
13891  **/
13892 int
13893 lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
13894 {
13895         LPFC_MBOXQ_t *mboxq;
13896         struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
13897         uint32_t rc = 0;
13898         uint32_t shdr_status, shdr_add_status;
13899         union lpfc_sli4_cfg_shdr *shdr;
13900
13901         /* SLI4 ports that support extents do not require RPI headers. */
13902         if (!phba->sli4_hba.rpi_hdrs_in_use)
13903                 return rc;
13904         if (phba->sli4_hba.extents_in_use)
13905                 return -EIO;
13906
13907         /* The port is notified of the header region via a mailbox command. */
13908         mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13909         if (!mboxq) {
13910                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13911                                 "2001 Unable to allocate memory for issuing "
13912                                 "SLI_CONFIG_SPECIAL mailbox command\n");
13913                 return -ENOMEM;
13914         }
13915
13916         /* Post all rpi memory regions to the port. */
13917         hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
13918         lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
13919                          LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
13920                          sizeof(struct lpfc_mbx_post_hdr_tmpl) -
13921                          sizeof(struct lpfc_sli4_cfg_mhdr),
13922                          LPFC_SLI4_MBX_EMBED);
13923
13924
13925         /* Post the physical rpi to the port for this rpi header. */
13926         bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
13927                rpi_page->start_rpi);
13928         bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
13929                hdr_tmpl, rpi_page->page_count);
13930
13931         hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
13932         hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
13933         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
13934         shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
13935         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13936         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13937         if (rc != MBX_TIMEOUT)
13938                 mempool_free(mboxq, phba->mbox_mem_pool);
13939         if (shdr_status || shdr_add_status || rc) {
13940                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13941                                 "2514 POST_RPI_HDR mailbox failed with "
13942                                 "status x%x add_status x%x, mbx status x%x\n",
13943                                 shdr_status, shdr_add_status, rc);
13944                 rc = -ENXIO;
13945         }
13946         return rc;
13947 }
13948
13949 /**
13950  * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
13951  * @phba: pointer to lpfc hba data structure.
13952  *
13953  * This routine is invoked to post rpi header templates to the
13954  * HBA consistent with the SLI-4 interface spec.  This routine
13955  * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
13956  * SLI4_PAGE_SIZE modulo 64 rpi context headers.
13957  *
13958  * Returns
13959  *      A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
13960  *      LPFC_RPI_ALLOC_ERROR if no rpis are available.
13961  **/
13962 int
13963 lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
13964 {
13965         unsigned long rpi;
13966         uint16_t max_rpi, rpi_limit;
13967         uint16_t rpi_remaining, lrpi = 0;
13968         struct lpfc_rpi_hdr *rpi_hdr;
13969
13970         max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
13971         rpi_limit = phba->sli4_hba.next_rpi;
13972
13973         /*
13974          * Fetch the next logical rpi.  Because this index is logical,
13975          * the  driver starts at 0 each time.
13976          */
13977         spin_lock_irq(&phba->hbalock);
13978         rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
13979         if (rpi >= rpi_limit)
13980                 rpi = LPFC_RPI_ALLOC_ERROR;
13981         else {
13982                 set_bit(rpi, phba->sli4_hba.rpi_bmask);
13983                 phba->sli4_hba.max_cfg_param.rpi_used++;
13984                 phba->sli4_hba.rpi_count++;
13985         }
13986
13987         /*
13988          * Don't try to allocate more rpi header regions if the device limit
13989          * has been exhausted.
13990          */
13991         if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
13992             (phba->sli4_hba.rpi_count >= max_rpi)) {
13993                 spin_unlock_irq(&phba->hbalock);
13994                 return rpi;
13995         }
13996
13997         /*
13998          * RPI header postings are not required for SLI4 ports capable of
13999          * extents.
14000          */
14001         if (!phba->sli4_hba.rpi_hdrs_in_use) {
14002                 spin_unlock_irq(&phba->hbalock);
14003                 return rpi;
14004         }
14005
14006         /*
14007          * If the driver is running low on rpi resources, allocate another
14008          * page now.  Note that the next_rpi value is used because
14009          * it represents how many are actually in use whereas max_rpi notes
14010          * how many are supported max by the device.
14011          */
14012         rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
14013         spin_unlock_irq(&phba->hbalock);
14014         if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
14015                 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
14016                 if (!rpi_hdr) {
14017                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14018                                         "2002 Error Could not grow rpi "
14019                                         "count\n");
14020                 } else {
14021                         lrpi = rpi_hdr->start_rpi;
14022                         rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
14023                         lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
14024                 }
14025         }
14026
14027         return rpi;
14028 }
14029
14030 /**
14031  * lpfc_sli4_free_rpi - Release an rpi for reuse.
14032  * @phba: pointer to lpfc hba data structure.
14033  *
14034  * This routine is invoked to release an rpi to the pool of
14035  * available rpis maintained by the driver.
14036  **/
14037 void
14038 __lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
14039 {
14040         if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
14041                 phba->sli4_hba.rpi_count--;
14042                 phba->sli4_hba.max_cfg_param.rpi_used--;
14043         }
14044 }
14045
14046 /**
14047  * lpfc_sli4_free_rpi - Release an rpi for reuse.
14048  * @phba: pointer to lpfc hba data structure.
14049  *
14050  * This routine is invoked to release an rpi to the pool of
14051  * available rpis maintained by the driver.
14052  **/
14053 void
14054 lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
14055 {
14056         spin_lock_irq(&phba->hbalock);
14057         __lpfc_sli4_free_rpi(phba, rpi);
14058         spin_unlock_irq(&phba->hbalock);
14059 }
14060
14061 /**
14062  * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
14063  * @phba: pointer to lpfc hba data structure.
14064  *
14065  * This routine is invoked to remove the memory region that
14066  * provided rpi via a bitmask.
14067  **/
14068 void
14069 lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
14070 {
14071         kfree(phba->sli4_hba.rpi_bmask);
14072         kfree(phba->sli4_hba.rpi_ids);
14073         bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
14074 }
14075
14076 /**
14077  * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
14078  * @phba: pointer to lpfc hba data structure.
14079  *
14080  * This routine is invoked to remove the memory region that
14081  * provided rpi via a bitmask.
14082  **/
14083 int
14084 lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp)
14085 {
14086         LPFC_MBOXQ_t *mboxq;
14087         struct lpfc_hba *phba = ndlp->phba;
14088         int rc;
14089
14090         /* The port is notified of the header region via a mailbox command. */
14091         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14092         if (!mboxq)
14093                 return -ENOMEM;
14094
14095         /* Post all rpi memory regions to the port. */
14096         lpfc_resume_rpi(mboxq, ndlp);
14097         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
14098         if (rc == MBX_NOT_FINISHED) {
14099                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14100                                 "2010 Resume RPI Mailbox failed "
14101                                 "status %d, mbxStatus x%x\n", rc,
14102                                 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
14103                 mempool_free(mboxq, phba->mbox_mem_pool);
14104                 return -EIO;
14105         }
14106         return 0;
14107 }
14108
14109 /**
14110  * lpfc_sli4_init_vpi - Initialize a vpi with the port
14111  * @vport: Pointer to the vport for which the vpi is being initialized
14112  *
14113  * This routine is invoked to activate a vpi with the port.
14114  *
14115  * Returns:
14116  *    0 success
14117  *    -Evalue otherwise
14118  **/
14119 int
14120 lpfc_sli4_init_vpi(struct lpfc_vport *vport)
14121 {
14122         LPFC_MBOXQ_t *mboxq;
14123         int rc = 0;
14124         int retval = MBX_SUCCESS;
14125         uint32_t mbox_tmo;
14126         struct lpfc_hba *phba = vport->phba;
14127         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14128         if (!mboxq)
14129                 return -ENOMEM;
14130         lpfc_init_vpi(phba, mboxq, vport->vpi);
14131         mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_INIT_VPI);
14132         rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
14133         if (rc != MBX_SUCCESS) {
14134                 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
14135                                 "2022 INIT VPI Mailbox failed "
14136                                 "status %d, mbxStatus x%x\n", rc,
14137                                 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
14138                 retval = -EIO;
14139         }
14140         if (rc != MBX_TIMEOUT)
14141                 mempool_free(mboxq, vport->phba->mbox_mem_pool);
14142
14143         return retval;
14144 }
14145
14146 /**
14147  * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
14148  * @phba: pointer to lpfc hba data structure.
14149  * @mboxq: Pointer to mailbox object.
14150  *
14151  * This routine is invoked to manually add a single FCF record. The caller
14152  * must pass a completely initialized FCF_Record.  This routine takes
14153  * care of the nonembedded mailbox operations.
14154  **/
14155 static void
14156 lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
14157 {
14158         void *virt_addr;
14159         union lpfc_sli4_cfg_shdr *shdr;
14160         uint32_t shdr_status, shdr_add_status;
14161
14162         virt_addr = mboxq->sge_array->addr[0];
14163         /* The IOCTL status is embedded in the mailbox subheader. */
14164         shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
14165         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14166         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14167
14168         if ((shdr_status || shdr_add_status) &&
14169                 (shdr_status != STATUS_FCF_IN_USE))
14170                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14171                         "2558 ADD_FCF_RECORD mailbox failed with "
14172                         "status x%x add_status x%x\n",
14173                         shdr_status, shdr_add_status);
14174
14175         lpfc_sli4_mbox_cmd_free(phba, mboxq);
14176 }
14177
14178 /**
14179  * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
14180  * @phba: pointer to lpfc hba data structure.
14181  * @fcf_record:  pointer to the initialized fcf record to add.
14182  *
14183  * This routine is invoked to manually add a single FCF record. The caller
14184  * must pass a completely initialized FCF_Record.  This routine takes
14185  * care of the nonembedded mailbox operations.
14186  **/
14187 int
14188 lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
14189 {
14190         int rc = 0;
14191         LPFC_MBOXQ_t *mboxq;
14192         uint8_t *bytep;
14193         void *virt_addr;
14194         dma_addr_t phys_addr;
14195         struct lpfc_mbx_sge sge;
14196         uint32_t alloc_len, req_len;
14197         uint32_t fcfindex;
14198
14199         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14200         if (!mboxq) {
14201                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14202                         "2009 Failed to allocate mbox for ADD_FCF cmd\n");
14203                 return -ENOMEM;
14204         }
14205
14206         req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
14207                   sizeof(uint32_t);
14208
14209         /* Allocate DMA memory and set up the non-embedded mailbox command */
14210         alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
14211                                      LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
14212                                      req_len, LPFC_SLI4_MBX_NEMBED);
14213         if (alloc_len < req_len) {
14214                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14215                         "2523 Allocated DMA memory size (x%x) is "
14216                         "less than the requested DMA memory "
14217                         "size (x%x)\n", alloc_len, req_len);
14218                 lpfc_sli4_mbox_cmd_free(phba, mboxq);
14219                 return -ENOMEM;
14220         }
14221
14222         /*
14223          * Get the first SGE entry from the non-embedded DMA memory.  This
14224          * routine only uses a single SGE.
14225          */
14226         lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
14227         phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
14228         virt_addr = mboxq->sge_array->addr[0];
14229         /*
14230          * Configure the FCF record for FCFI 0.  This is the driver's
14231          * hardcoded default and gets used in nonFIP mode.
14232          */
14233         fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
14234         bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
14235         lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
14236
14237         /*
14238          * Copy the fcf_index and the FCF Record Data. The data starts after
14239          * the FCoE header plus word10. The data copy needs to be endian
14240          * correct.
14241          */
14242         bytep += sizeof(uint32_t);
14243         lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
14244         mboxq->vport = phba->pport;
14245         mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
14246         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
14247         if (rc == MBX_NOT_FINISHED) {
14248                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14249                         "2515 ADD_FCF_RECORD mailbox failed with "
14250                         "status 0x%x\n", rc);
14251                 lpfc_sli4_mbox_cmd_free(phba, mboxq);
14252                 rc = -EIO;
14253         } else
14254                 rc = 0;
14255
14256         return rc;
14257 }
14258
14259 /**
14260  * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
14261  * @phba: pointer to lpfc hba data structure.
14262  * @fcf_record:  pointer to the fcf record to write the default data.
14263  * @fcf_index: FCF table entry index.
14264  *
14265  * This routine is invoked to build the driver's default FCF record.  The
14266  * values used are hardcoded.  This routine handles memory initialization.
14267  *
14268  **/
14269 void
14270 lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
14271                                 struct fcf_record *fcf_record,
14272                                 uint16_t fcf_index)
14273 {
14274         memset(fcf_record, 0, sizeof(struct fcf_record));
14275         fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
14276         fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
14277         fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
14278         bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
14279         bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
14280         bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
14281         bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
14282         bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
14283         bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
14284         bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
14285         bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
14286         bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
14287         bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
14288         bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
14289         bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
14290         bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
14291                 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
14292         /* Set the VLAN bit map */
14293         if (phba->valid_vlan) {
14294                 fcf_record->vlan_bitmap[phba->vlan_id / 8]
14295                         = 1 << (phba->vlan_id % 8);
14296         }
14297 }
14298
14299 /**
14300  * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
14301  * @phba: pointer to lpfc hba data structure.
14302  * @fcf_index: FCF table entry offset.
14303  *
14304  * This routine is invoked to scan the entire FCF table by reading FCF
14305  * record and processing it one at a time starting from the @fcf_index
14306  * for initial FCF discovery or fast FCF failover rediscovery.
14307  *
14308  * Return 0 if the mailbox command is submitted successfully, none 0
14309  * otherwise.
14310  **/
14311 int
14312 lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
14313 {
14314         int rc = 0, error;
14315         LPFC_MBOXQ_t *mboxq;
14316
14317         phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
14318         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14319         if (!mboxq) {
14320                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14321                                 "2000 Failed to allocate mbox for "
14322                                 "READ_FCF cmd\n");
14323                 error = -ENOMEM;
14324                 goto fail_fcf_scan;
14325         }
14326         /* Construct the read FCF record mailbox command */
14327         rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
14328         if (rc) {
14329                 error = -EINVAL;
14330                 goto fail_fcf_scan;
14331         }
14332         /* Issue the mailbox command asynchronously */
14333         mboxq->vport = phba->pport;
14334         mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
14335
14336         spin_lock_irq(&phba->hbalock);
14337         phba->hba_flag |= FCF_TS_INPROG;
14338         spin_unlock_irq(&phba->hbalock);
14339
14340         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
14341         if (rc == MBX_NOT_FINISHED)
14342                 error = -EIO;
14343         else {
14344                 /* Reset eligible FCF count for new scan */
14345                 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
14346                         phba->fcf.eligible_fcf_cnt = 0;
14347                 error = 0;
14348         }
14349 fail_fcf_scan:
14350         if (error) {
14351                 if (mboxq)
14352                         lpfc_sli4_mbox_cmd_free(phba, mboxq);
14353                 /* FCF scan failed, clear FCF_TS_INPROG flag */
14354                 spin_lock_irq(&phba->hbalock);
14355                 phba->hba_flag &= ~FCF_TS_INPROG;
14356                 spin_unlock_irq(&phba->hbalock);
14357         }
14358         return error;
14359 }
14360
14361 /**
14362  * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
14363  * @phba: pointer to lpfc hba data structure.
14364  * @fcf_index: FCF table entry offset.
14365  *
14366  * This routine is invoked to read an FCF record indicated by @fcf_index
14367  * and to use it for FLOGI roundrobin FCF failover.
14368  *
14369  * Return 0 if the mailbox command is submitted successfully, none 0
14370  * otherwise.
14371  **/
14372 int
14373 lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
14374 {
14375         int rc = 0, error;
14376         LPFC_MBOXQ_t *mboxq;
14377
14378         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14379         if (!mboxq) {
14380                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
14381                                 "2763 Failed to allocate mbox for "
14382                                 "READ_FCF cmd\n");
14383                 error = -ENOMEM;
14384                 goto fail_fcf_read;
14385         }
14386         /* Construct the read FCF record mailbox command */
14387         rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
14388         if (rc) {
14389                 error = -EINVAL;
14390                 goto fail_fcf_read;
14391         }
14392         /* Issue the mailbox command asynchronously */
14393         mboxq->vport = phba->pport;
14394         mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
14395         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
14396         if (rc == MBX_NOT_FINISHED)
14397                 error = -EIO;
14398         else
14399                 error = 0;
14400
14401 fail_fcf_read:
14402         if (error && mboxq)
14403                 lpfc_sli4_mbox_cmd_free(phba, mboxq);
14404         return error;
14405 }
14406
14407 /**
14408  * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
14409  * @phba: pointer to lpfc hba data structure.
14410  * @fcf_index: FCF table entry offset.
14411  *
14412  * This routine is invoked to read an FCF record indicated by @fcf_index to
14413  * determine whether it's eligible for FLOGI roundrobin failover list.
14414  *
14415  * Return 0 if the mailbox command is submitted successfully, none 0
14416  * otherwise.
14417  **/
14418 int
14419 lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
14420 {
14421         int rc = 0, error;
14422         LPFC_MBOXQ_t *mboxq;
14423
14424         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14425         if (!mboxq) {
14426                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
14427                                 "2758 Failed to allocate mbox for "
14428                                 "READ_FCF cmd\n");
14429                                 error = -ENOMEM;
14430                                 goto fail_fcf_read;
14431         }
14432         /* Construct the read FCF record mailbox command */
14433         rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
14434         if (rc) {
14435                 error = -EINVAL;
14436                 goto fail_fcf_read;
14437         }
14438         /* Issue the mailbox command asynchronously */
14439         mboxq->vport = phba->pport;
14440         mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
14441         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
14442         if (rc == MBX_NOT_FINISHED)
14443                 error = -EIO;
14444         else
14445                 error = 0;
14446
14447 fail_fcf_read:
14448         if (error && mboxq)
14449                 lpfc_sli4_mbox_cmd_free(phba, mboxq);
14450         return error;
14451 }
14452
14453 /**
14454  * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
14455  * @phba: pointer to lpfc hba data structure.
14456  *
14457  * This routine is to get the next eligible FCF record index in a round
14458  * robin fashion. If the next eligible FCF record index equals to the
14459  * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
14460  * shall be returned, otherwise, the next eligible FCF record's index
14461  * shall be returned.
14462  **/
14463 uint16_t
14464 lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
14465 {
14466         uint16_t next_fcf_index;
14467
14468         /* Search start from next bit of currently registered FCF index */
14469         next_fcf_index = (phba->fcf.current_rec.fcf_indx + 1) %
14470                                         LPFC_SLI4_FCF_TBL_INDX_MAX;
14471         next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
14472                                        LPFC_SLI4_FCF_TBL_INDX_MAX,
14473                                        next_fcf_index);
14474
14475         /* Wrap around condition on phba->fcf.fcf_rr_bmask */
14476         if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
14477                 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
14478                                                LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
14479
14480         /* Check roundrobin failover list empty condition */
14481         if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
14482                 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
14483                                 "2844 No roundrobin failover FCF available\n");
14484                 return LPFC_FCOE_FCF_NEXT_NONE;
14485         }
14486
14487         lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
14488                         "2845 Get next roundrobin failover FCF (x%x)\n",
14489                         next_fcf_index);
14490
14491         return next_fcf_index;
14492 }
14493
14494 /**
14495  * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
14496  * @phba: pointer to lpfc hba data structure.
14497  *
14498  * This routine sets the FCF record index in to the eligible bmask for
14499  * roundrobin failover search. It checks to make sure that the index
14500  * does not go beyond the range of the driver allocated bmask dimension
14501  * before setting the bit.
14502  *
14503  * Returns 0 if the index bit successfully set, otherwise, it returns
14504  * -EINVAL.
14505  **/
14506 int
14507 lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
14508 {
14509         if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
14510                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
14511                                 "2610 FCF (x%x) reached driver's book "
14512                                 "keeping dimension:x%x\n",
14513                                 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
14514                 return -EINVAL;
14515         }
14516         /* Set the eligible FCF record index bmask */
14517         set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
14518
14519         lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
14520                         "2790 Set FCF (x%x) to roundrobin FCF failover "
14521                         "bmask\n", fcf_index);
14522
14523         return 0;
14524 }
14525
14526 /**
14527  * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
14528  * @phba: pointer to lpfc hba data structure.
14529  *
14530  * This routine clears the FCF record index from the eligible bmask for
14531  * roundrobin failover search. It checks to make sure that the index
14532  * does not go beyond the range of the driver allocated bmask dimension
14533  * before clearing the bit.
14534  **/
14535 void
14536 lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
14537 {
14538         if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
14539                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
14540                                 "2762 FCF (x%x) reached driver's book "
14541                                 "keeping dimension:x%x\n",
14542                                 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
14543                 return;
14544         }
14545         /* Clear the eligible FCF record index bmask */
14546         clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
14547
14548         lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
14549                         "2791 Clear FCF (x%x) from roundrobin failover "
14550                         "bmask\n", fcf_index);
14551 }
14552
14553 /**
14554  * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
14555  * @phba: pointer to lpfc hba data structure.
14556  *
14557  * This routine is the completion routine for the rediscover FCF table mailbox
14558  * command. If the mailbox command returned failure, it will try to stop the
14559  * FCF rediscover wait timer.
14560  **/
14561 void
14562 lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
14563 {
14564         struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
14565         uint32_t shdr_status, shdr_add_status;
14566
14567         redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
14568
14569         shdr_status = bf_get(lpfc_mbox_hdr_status,
14570                              &redisc_fcf->header.cfg_shdr.response);
14571         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
14572                              &redisc_fcf->header.cfg_shdr.response);
14573         if (shdr_status || shdr_add_status) {
14574                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
14575                                 "2746 Requesting for FCF rediscovery failed "
14576                                 "status x%x add_status x%x\n",
14577                                 shdr_status, shdr_add_status);
14578                 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
14579                         spin_lock_irq(&phba->hbalock);
14580                         phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
14581                         spin_unlock_irq(&phba->hbalock);
14582                         /*
14583                          * CVL event triggered FCF rediscover request failed,
14584                          * last resort to re-try current registered FCF entry.
14585                          */
14586                         lpfc_retry_pport_discovery(phba);
14587                 } else {
14588                         spin_lock_irq(&phba->hbalock);
14589                         phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
14590                         spin_unlock_irq(&phba->hbalock);
14591                         /*
14592                          * DEAD FCF event triggered FCF rediscover request
14593                          * failed, last resort to fail over as a link down
14594                          * to FCF registration.
14595                          */
14596                         lpfc_sli4_fcf_dead_failthrough(phba);
14597                 }
14598         } else {
14599                 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
14600                                 "2775 Start FCF rediscover quiescent timer\n");
14601                 /*
14602                  * Start FCF rediscovery wait timer for pending FCF
14603                  * before rescan FCF record table.
14604                  */
14605                 lpfc_fcf_redisc_wait_start_timer(phba);
14606         }
14607
14608         mempool_free(mbox, phba->mbox_mem_pool);
14609 }
14610
14611 /**
14612  * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
14613  * @phba: pointer to lpfc hba data structure.
14614  *
14615  * This routine is invoked to request for rediscovery of the entire FCF table
14616  * by the port.
14617  **/
14618 int
14619 lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
14620 {
14621         LPFC_MBOXQ_t *mbox;
14622         struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
14623         int rc, length;
14624
14625         /* Cancel retry delay timers to all vports before FCF rediscover */
14626         lpfc_cancel_all_vport_retry_delay_timer(phba);
14627
14628         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14629         if (!mbox) {
14630                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14631                                 "2745 Failed to allocate mbox for "
14632                                 "requesting FCF rediscover.\n");
14633                 return -ENOMEM;
14634         }
14635
14636         length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
14637                   sizeof(struct lpfc_sli4_cfg_mhdr));
14638         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14639                          LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
14640                          length, LPFC_SLI4_MBX_EMBED);
14641
14642         redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
14643         /* Set count to 0 for invalidating the entire FCF database */
14644         bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
14645
14646         /* Issue the mailbox command asynchronously */
14647         mbox->vport = phba->pport;
14648         mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
14649         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
14650
14651         if (rc == MBX_NOT_FINISHED) {
14652                 mempool_free(mbox, phba->mbox_mem_pool);
14653                 return -EIO;
14654         }
14655         return 0;
14656 }
14657
14658 /**
14659  * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
14660  * @phba: pointer to lpfc hba data structure.
14661  *
14662  * This function is the failover routine as a last resort to the FCF DEAD
14663  * event when driver failed to perform fast FCF failover.
14664  **/
14665 void
14666 lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
14667 {
14668         uint32_t link_state;
14669
14670         /*
14671          * Last resort as FCF DEAD event failover will treat this as
14672          * a link down, but save the link state because we don't want
14673          * it to be changed to Link Down unless it is already down.
14674          */
14675         link_state = phba->link_state;
14676         lpfc_linkdown(phba);
14677         phba->link_state = link_state;
14678
14679         /* Unregister FCF if no devices connected to it */
14680         lpfc_unregister_unused_fcf(phba);
14681 }
14682
14683 /**
14684  * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
14685  * @phba: pointer to lpfc hba data structure.
14686  *
14687  * This function read region 23 and parse TLV for port status to
14688  * decide if the user disaled the port. If the TLV indicates the
14689  * port is disabled, the hba_flag is set accordingly.
14690  **/
14691 void
14692 lpfc_sli_read_link_ste(struct lpfc_hba *phba)
14693 {
14694         LPFC_MBOXQ_t *pmb = NULL;
14695         MAILBOX_t *mb;
14696         uint8_t *rgn23_data = NULL;
14697         uint32_t offset = 0, data_size, sub_tlv_len, tlv_offset;
14698         int rc;
14699
14700         pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14701         if (!pmb) {
14702                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14703                         "2600 lpfc_sli_read_serdes_param failed to"
14704                         " allocate mailbox memory\n");
14705                 goto out;
14706         }
14707         mb = &pmb->u.mb;
14708
14709         /* Get adapter Region 23 data */
14710         rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
14711         if (!rgn23_data)
14712                 goto out;
14713
14714         do {
14715                 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
14716                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
14717
14718                 if (rc != MBX_SUCCESS) {
14719                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
14720                                 "2601 lpfc_sli_read_link_ste failed to"
14721                                 " read config region 23 rc 0x%x Status 0x%x\n",
14722                                 rc, mb->mbxStatus);
14723                         mb->un.varDmp.word_cnt = 0;
14724                 }
14725                 /*
14726                  * dump mem may return a zero when finished or we got a
14727                  * mailbox error, either way we are done.
14728                  */
14729                 if (mb->un.varDmp.word_cnt == 0)
14730                         break;
14731                 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
14732                         mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
14733
14734                 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
14735                         rgn23_data + offset,
14736                         mb->un.varDmp.word_cnt);
14737                 offset += mb->un.varDmp.word_cnt;
14738         } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
14739
14740         data_size = offset;
14741         offset = 0;
14742
14743         if (!data_size)
14744                 goto out;
14745
14746         /* Check the region signature first */
14747         if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
14748                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14749                         "2619 Config region 23 has bad signature\n");
14750                         goto out;
14751         }
14752         offset += 4;
14753
14754         /* Check the data structure version */
14755         if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
14756                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14757                         "2620 Config region 23 has bad version\n");
14758                 goto out;
14759         }
14760         offset += 4;
14761
14762         /* Parse TLV entries in the region */
14763         while (offset < data_size) {
14764                 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
14765                         break;
14766                 /*
14767                  * If the TLV is not driver specific TLV or driver id is
14768                  * not linux driver id, skip the record.
14769                  */
14770                 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
14771                     (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
14772                     (rgn23_data[offset + 3] != 0)) {
14773                         offset += rgn23_data[offset + 1] * 4 + 4;
14774                         continue;
14775                 }
14776
14777                 /* Driver found a driver specific TLV in the config region */
14778                 sub_tlv_len = rgn23_data[offset + 1] * 4;
14779                 offset += 4;
14780                 tlv_offset = 0;
14781
14782                 /*
14783                  * Search for configured port state sub-TLV.
14784                  */
14785                 while ((offset < data_size) &&
14786                         (tlv_offset < sub_tlv_len)) {
14787                         if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
14788                                 offset += 4;
14789                                 tlv_offset += 4;
14790                                 break;
14791                         }
14792                         if (rgn23_data[offset] != PORT_STE_TYPE) {
14793                                 offset += rgn23_data[offset + 1] * 4 + 4;
14794                                 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
14795                                 continue;
14796                         }
14797
14798                         /* This HBA contains PORT_STE configured */
14799                         if (!rgn23_data[offset + 2])
14800                                 phba->hba_flag |= LINK_DISABLED;
14801
14802                         goto out;
14803                 }
14804         }
14805 out:
14806         if (pmb)
14807                 mempool_free(pmb, phba->mbox_mem_pool);
14808         kfree(rgn23_data);
14809         return;
14810 }
14811
14812 /**
14813  * lpfc_wr_object - write an object to the firmware
14814  * @phba: HBA structure that indicates port to create a queue on.
14815  * @dmabuf_list: list of dmabufs to write to the port.
14816  * @size: the total byte value of the objects to write to the port.
14817  * @offset: the current offset to be used to start the transfer.
14818  *
14819  * This routine will create a wr_object mailbox command to send to the port.
14820  * the mailbox command will be constructed using the dma buffers described in
14821  * @dmabuf_list to create a list of BDEs. This routine will fill in as many
14822  * BDEs that the imbedded mailbox can support. The @offset variable will be
14823  * used to indicate the starting offset of the transfer and will also return
14824  * the offset after the write object mailbox has completed. @size is used to
14825  * determine the end of the object and whether the eof bit should be set.
14826  *
14827  * Return 0 is successful and offset will contain the the new offset to use
14828  * for the next write.
14829  * Return negative value for error cases.
14830  **/
14831 int
14832 lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
14833                uint32_t size, uint32_t *offset)
14834 {
14835         struct lpfc_mbx_wr_object *wr_object;
14836         LPFC_MBOXQ_t *mbox;
14837         int rc = 0, i = 0;
14838         uint32_t shdr_status, shdr_add_status;
14839         uint32_t mbox_tmo;
14840         union lpfc_sli4_cfg_shdr *shdr;
14841         struct lpfc_dmabuf *dmabuf;
14842         uint32_t written = 0;
14843
14844         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14845         if (!mbox)
14846                 return -ENOMEM;
14847
14848         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14849                         LPFC_MBOX_OPCODE_WRITE_OBJECT,
14850                         sizeof(struct lpfc_mbx_wr_object) -
14851                         sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
14852
14853         wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
14854         wr_object->u.request.write_offset = *offset;
14855         sprintf((uint8_t *)wr_object->u.request.object_name, "/");
14856         wr_object->u.request.object_name[0] =
14857                 cpu_to_le32(wr_object->u.request.object_name[0]);
14858         bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
14859         list_for_each_entry(dmabuf, dmabuf_list, list) {
14860                 if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
14861                         break;
14862                 wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
14863                 wr_object->u.request.bde[i].addrHigh =
14864                         putPaddrHigh(dmabuf->phys);
14865                 if (written + SLI4_PAGE_SIZE >= size) {
14866                         wr_object->u.request.bde[i].tus.f.bdeSize =
14867                                 (size - written);
14868                         written += (size - written);
14869                         bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
14870                 } else {
14871                         wr_object->u.request.bde[i].tus.f.bdeSize =
14872                                 SLI4_PAGE_SIZE;
14873                         written += SLI4_PAGE_SIZE;
14874                 }
14875                 i++;
14876         }
14877         wr_object->u.request.bde_count = i;
14878         bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
14879         if (!phba->sli4_hba.intr_enable)
14880                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14881         else {
14882                 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
14883                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
14884         }
14885         /* The IOCTL status is embedded in the mailbox subheader. */
14886         shdr = (union lpfc_sli4_cfg_shdr *) &wr_object->header.cfg_shdr;
14887         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14888         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14889         if (rc != MBX_TIMEOUT)
14890                 mempool_free(mbox, phba->mbox_mem_pool);
14891         if (shdr_status || shdr_add_status || rc) {
14892                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14893                                 "3025 Write Object mailbox failed with "
14894                                 "status x%x add_status x%x, mbx status x%x\n",
14895                                 shdr_status, shdr_add_status, rc);
14896                 rc = -ENXIO;
14897         } else
14898                 *offset += wr_object->u.response.actual_write_length;
14899         return rc;
14900 }
14901
14902 /**
14903  * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
14904  * @vport: pointer to vport data structure.
14905  *
14906  * This function iterate through the mailboxq and clean up all REG_LOGIN
14907  * and REG_VPI mailbox commands associated with the vport. This function
14908  * is called when driver want to restart discovery of the vport due to
14909  * a Clear Virtual Link event.
14910  **/
14911 void
14912 lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
14913 {
14914         struct lpfc_hba *phba = vport->phba;
14915         LPFC_MBOXQ_t *mb, *nextmb;
14916         struct lpfc_dmabuf *mp;
14917         struct lpfc_nodelist *ndlp;
14918         struct lpfc_nodelist *act_mbx_ndlp = NULL;
14919         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
14920         LIST_HEAD(mbox_cmd_list);
14921         uint8_t restart_loop;
14922
14923         /* Clean up internally queued mailbox commands with the vport */
14924         spin_lock_irq(&phba->hbalock);
14925         list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
14926                 if (mb->vport != vport)
14927                         continue;
14928
14929                 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
14930                         (mb->u.mb.mbxCommand != MBX_REG_VPI))
14931                         continue;
14932
14933                 list_del(&mb->list);
14934                 list_add_tail(&mb->list, &mbox_cmd_list);
14935         }
14936         /* Clean up active mailbox command with the vport */
14937         mb = phba->sli.mbox_active;
14938         if (mb && (mb->vport == vport)) {
14939                 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
14940                         (mb->u.mb.mbxCommand == MBX_REG_VPI))
14941                         mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14942                 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
14943                         act_mbx_ndlp = (struct lpfc_nodelist *)mb->context2;
14944                         /* Put reference count for delayed processing */
14945                         act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
14946                         /* Unregister the RPI when mailbox complete */
14947                         mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
14948                 }
14949         }
14950         /* Cleanup any mailbox completions which are not yet processed */
14951         do {
14952                 restart_loop = 0;
14953                 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
14954                         /*
14955                          * If this mailox is already processed or it is
14956                          * for another vport ignore it.
14957                          */
14958                         if ((mb->vport != vport) ||
14959                                 (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
14960                                 continue;
14961
14962                         if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
14963                                 (mb->u.mb.mbxCommand != MBX_REG_VPI))
14964                                 continue;
14965
14966                         mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14967                         if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
14968                                 ndlp = (struct lpfc_nodelist *)mb->context2;
14969                                 /* Unregister the RPI when mailbox complete */
14970                                 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
14971                                 restart_loop = 1;
14972                                 spin_unlock_irq(&phba->hbalock);
14973                                 spin_lock(shost->host_lock);
14974                                 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
14975                                 spin_unlock(shost->host_lock);
14976                                 spin_lock_irq(&phba->hbalock);
14977                                 break;
14978                         }
14979                 }
14980         } while (restart_loop);
14981
14982         spin_unlock_irq(&phba->hbalock);
14983
14984         /* Release the cleaned-up mailbox commands */
14985         while (!list_empty(&mbox_cmd_list)) {
14986                 list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
14987                 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
14988                         mp = (struct lpfc_dmabuf *) (mb->context1);
14989                         if (mp) {
14990                                 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
14991                                 kfree(mp);
14992                         }
14993                         ndlp = (struct lpfc_nodelist *) mb->context2;
14994                         mb->context2 = NULL;
14995                         if (ndlp) {
14996                                 spin_lock(shost->host_lock);
14997                                 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
14998                                 spin_unlock(shost->host_lock);
14999                                 lpfc_nlp_put(ndlp);
15000                         }
15001                 }
15002                 mempool_free(mb, phba->mbox_mem_pool);
15003         }
15004
15005         /* Release the ndlp with the cleaned-up active mailbox command */
15006         if (act_mbx_ndlp) {
15007                 spin_lock(shost->host_lock);
15008                 act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
15009                 spin_unlock(shost->host_lock);
15010                 lpfc_nlp_put(act_mbx_ndlp);
15011         }
15012 }
15013
15014 /**
15015  * lpfc_drain_txq - Drain the txq
15016  * @phba: Pointer to HBA context object.
15017  *
15018  * This function attempt to submit IOCBs on the txq
15019  * to the adapter.  For SLI4 adapters, the txq contains
15020  * ELS IOCBs that have been deferred because the there
15021  * are no SGLs.  This congestion can occur with large
15022  * vport counts during node discovery.
15023  **/
15024
15025 uint32_t
15026 lpfc_drain_txq(struct lpfc_hba *phba)
15027 {
15028         LIST_HEAD(completions);
15029         struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
15030         struct lpfc_iocbq *piocbq = 0;
15031         unsigned long iflags = 0;
15032         char *fail_msg = NULL;
15033         struct lpfc_sglq *sglq;
15034         union lpfc_wqe wqe;
15035
15036         spin_lock_irqsave(&phba->hbalock, iflags);
15037         if (pring->txq_cnt > pring->txq_max)
15038                 pring->txq_max = pring->txq_cnt;
15039
15040         spin_unlock_irqrestore(&phba->hbalock, iflags);
15041
15042         while (pring->txq_cnt) {
15043                 spin_lock_irqsave(&phba->hbalock, iflags);
15044
15045                 piocbq = lpfc_sli_ringtx_get(phba, pring);
15046                 sglq = __lpfc_sli_get_sglq(phba, piocbq);
15047                 if (!sglq) {
15048                         __lpfc_sli_ringtx_put(phba, pring, piocbq);
15049                         spin_unlock_irqrestore(&phba->hbalock, iflags);
15050                         break;
15051                 } else {
15052                         if (!piocbq) {
15053                                 /* The txq_cnt out of sync. This should
15054                                  * never happen
15055                                  */
15056                                 sglq = __lpfc_clear_active_sglq(phba,
15057                                                  sglq->sli4_lxritag);
15058                                 spin_unlock_irqrestore(&phba->hbalock, iflags);
15059                                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15060                                         "2823 txq empty and txq_cnt is %d\n ",
15061                                         pring->txq_cnt);
15062                                 break;
15063                         }
15064                 }
15065
15066                 /* The xri and iocb resources secured,
15067                  * attempt to issue request
15068                  */
15069                 piocbq->sli4_lxritag = sglq->sli4_lxritag;
15070                 piocbq->sli4_xritag = sglq->sli4_xritag;
15071                 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
15072                         fail_msg = "to convert bpl to sgl";
15073                 else if (lpfc_sli4_iocb2wqe(phba, piocbq, &wqe))
15074                         fail_msg = "to convert iocb to wqe";
15075                 else if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
15076                         fail_msg = " - Wq is full";
15077                 else
15078                         lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
15079
15080                 if (fail_msg) {
15081                         /* Failed means we can't issue and need to cancel */
15082                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15083                                         "2822 IOCB failed %s iotag 0x%x "
15084                                         "xri 0x%x\n",
15085                                         fail_msg,
15086                                         piocbq->iotag, piocbq->sli4_xritag);
15087                         list_add_tail(&piocbq->list, &completions);
15088                 }
15089                 spin_unlock_irqrestore(&phba->hbalock, iflags);
15090         }
15091
15092         /* Cancel all the IOCBs that cannot be issued */
15093         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
15094                                 IOERR_SLI_ABORTED);
15095
15096         return pring->txq_cnt;
15097 }