Merge branch 'linux-next' of git://git.infradead.org/ubi-2.6
[pandora-kernel.git] / drivers / scsi / lpfc / lpfc_sli.c
1 /*******************************************************************
2  * This file is part of the Emulex Linux Device Driver for         *
3  * Fibre Channel Host Bus Adapters.                                *
4  * Copyright (C) 2004-2009 Emulex.  All rights reserved.           *
5  * EMULEX and SLI are trademarks of Emulex.                        *
6  * www.emulex.com                                                  *
7  * Portions Copyright (C) 2004-2005 Christoph Hellwig              *
8  *                                                                 *
9  * This program is free software; you can redistribute it and/or   *
10  * modify it under the terms of version 2 of the GNU General       *
11  * Public License as published by the Free Software Foundation.    *
12  * This program is distributed in the hope that it will be useful. *
13  * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND          *
14  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,  *
15  * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE      *
16  * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
17  * TO BE LEGALLY INVALID.  See the GNU General Public License for  *
18  * more details, a copy of which can be found in the file COPYING  *
19  * included with this package.                                     *
20  *******************************************************************/
21
22 #include <linux/blkdev.h>
23 #include <linux/pci.h>
24 #include <linux/interrupt.h>
25 #include <linux/delay.h>
26 #include <linux/slab.h>
27
28 #include <scsi/scsi.h>
29 #include <scsi/scsi_cmnd.h>
30 #include <scsi/scsi_device.h>
31 #include <scsi/scsi_host.h>
32 #include <scsi/scsi_transport_fc.h>
33 #include <scsi/fc/fc_fs.h>
34 #include <linux/aer.h>
35
36 #include "lpfc_hw4.h"
37 #include "lpfc_hw.h"
38 #include "lpfc_sli.h"
39 #include "lpfc_sli4.h"
40 #include "lpfc_nl.h"
41 #include "lpfc_disc.h"
42 #include "lpfc_scsi.h"
43 #include "lpfc.h"
44 #include "lpfc_crtn.h"
45 #include "lpfc_logmsg.h"
46 #include "lpfc_compat.h"
47 #include "lpfc_debugfs.h"
48 #include "lpfc_vport.h"
49
50 /* There are only four IOCB completion types. */
51 typedef enum _lpfc_iocb_type {
52         LPFC_UNKNOWN_IOCB,
53         LPFC_UNSOL_IOCB,
54         LPFC_SOL_IOCB,
55         LPFC_ABORT_IOCB
56 } lpfc_iocb_type;
57
58
59 /* Provide function prototypes local to this module. */
60 static int lpfc_sli_issue_mbox_s4(struct lpfc_hba *, LPFC_MBOXQ_t *,
61                                   uint32_t);
62 static int lpfc_sli4_read_rev(struct lpfc_hba *, LPFC_MBOXQ_t *,
63                               uint8_t *, uint32_t *);
64 static struct lpfc_iocbq *lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *,
65                                                          struct lpfc_iocbq *);
66 static void lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *,
67                                       struct hbq_dmabuf *);
68 static IOCB_t *
69 lpfc_get_iocb_from_iocbq(struct lpfc_iocbq *iocbq)
70 {
71         return &iocbq->iocb;
72 }
73
74 /**
75  * lpfc_sli4_wq_put - Put a Work Queue Entry on an Work Queue
76  * @q: The Work Queue to operate on.
77  * @wqe: The work Queue Entry to put on the Work queue.
78  *
79  * This routine will copy the contents of @wqe to the next available entry on
80  * the @q. This function will then ring the Work Queue Doorbell to signal the
81  * HBA to start processing the Work Queue Entry. This function returns 0 if
82  * successful. If no entries are available on @q then this function will return
83  * -ENOMEM.
84  * The caller is expected to hold the hbalock when calling this routine.
85  **/
86 static uint32_t
87 lpfc_sli4_wq_put(struct lpfc_queue *q, union lpfc_wqe *wqe)
88 {
89         union lpfc_wqe *temp_wqe = q->qe[q->host_index].wqe;
90         struct lpfc_register doorbell;
91         uint32_t host_index;
92
93         /* If the host has not yet processed the next entry then we are done */
94         if (((q->host_index + 1) % q->entry_count) == q->hba_index)
95                 return -ENOMEM;
96         /* set consumption flag every once in a while */
97         if (!((q->host_index + 1) % LPFC_RELEASE_NOTIFICATION_INTERVAL))
98                 bf_set(lpfc_wqe_gen_wqec, &wqe->generic, 1);
99
100         lpfc_sli_pcimem_bcopy(wqe, temp_wqe, q->entry_size);
101
102         /* Update the host index before invoking device */
103         host_index = q->host_index;
104         q->host_index = ((q->host_index + 1) % q->entry_count);
105
106         /* Ring Doorbell */
107         doorbell.word0 = 0;
108         bf_set(lpfc_wq_doorbell_num_posted, &doorbell, 1);
109         bf_set(lpfc_wq_doorbell_index, &doorbell, host_index);
110         bf_set(lpfc_wq_doorbell_id, &doorbell, q->queue_id);
111         writel(doorbell.word0, q->phba->sli4_hba.WQDBregaddr);
112         readl(q->phba->sli4_hba.WQDBregaddr); /* Flush */
113
114         return 0;
115 }
116
117 /**
118  * lpfc_sli4_wq_release - Updates internal hba index for WQ
119  * @q: The Work Queue to operate on.
120  * @index: The index to advance the hba index to.
121  *
122  * This routine will update the HBA index of a queue to reflect consumption of
123  * Work Queue Entries by the HBA. When the HBA indicates that it has consumed
124  * an entry the host calls this function to update the queue's internal
125  * pointers. This routine returns the number of entries that were consumed by
126  * the HBA.
127  **/
128 static uint32_t
129 lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)
130 {
131         uint32_t released = 0;
132
133         if (q->hba_index == index)
134                 return 0;
135         do {
136                 q->hba_index = ((q->hba_index + 1) % q->entry_count);
137                 released++;
138         } while (q->hba_index != index);
139         return released;
140 }
141
142 /**
143  * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue
144  * @q: The Mailbox Queue to operate on.
145  * @wqe: The Mailbox Queue Entry to put on the Work queue.
146  *
147  * This routine will copy the contents of @mqe to the next available entry on
148  * the @q. This function will then ring the Work Queue Doorbell to signal the
149  * HBA to start processing the Work Queue Entry. This function returns 0 if
150  * successful. If no entries are available on @q then this function will return
151  * -ENOMEM.
152  * The caller is expected to hold the hbalock when calling this routine.
153  **/
154 static uint32_t
155 lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)
156 {
157         struct lpfc_mqe *temp_mqe = q->qe[q->host_index].mqe;
158         struct lpfc_register doorbell;
159         uint32_t host_index;
160
161         /* If the host has not yet processed the next entry then we are done */
162         if (((q->host_index + 1) % q->entry_count) == q->hba_index)
163                 return -ENOMEM;
164         lpfc_sli_pcimem_bcopy(mqe, temp_mqe, q->entry_size);
165         /* Save off the mailbox pointer for completion */
166         q->phba->mbox = (MAILBOX_t *)temp_mqe;
167
168         /* Update the host index before invoking device */
169         host_index = q->host_index;
170         q->host_index = ((q->host_index + 1) % q->entry_count);
171
172         /* Ring Doorbell */
173         doorbell.word0 = 0;
174         bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);
175         bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);
176         writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);
177         readl(q->phba->sli4_hba.MQDBregaddr); /* Flush */
178         return 0;
179 }
180
181 /**
182  * lpfc_sli4_mq_release - Updates internal hba index for MQ
183  * @q: The Mailbox Queue to operate on.
184  *
185  * This routine will update the HBA index of a queue to reflect consumption of
186  * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed
187  * an entry the host calls this function to update the queue's internal
188  * pointers. This routine returns the number of entries that were consumed by
189  * the HBA.
190  **/
191 static uint32_t
192 lpfc_sli4_mq_release(struct lpfc_queue *q)
193 {
194         /* Clear the mailbox pointer for completion */
195         q->phba->mbox = NULL;
196         q->hba_index = ((q->hba_index + 1) % q->entry_count);
197         return 1;
198 }
199
200 /**
201  * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ
202  * @q: The Event Queue to get the first valid EQE from
203  *
204  * This routine will get the first valid Event Queue Entry from @q, update
205  * the queue's internal hba index, and return the EQE. If no valid EQEs are in
206  * the Queue (no more work to do), or the Queue is full of EQEs that have been
207  * processed, but not popped back to the HBA then this routine will return NULL.
208  **/
209 static struct lpfc_eqe *
210 lpfc_sli4_eq_get(struct lpfc_queue *q)
211 {
212         struct lpfc_eqe *eqe = q->qe[q->hba_index].eqe;
213
214         /* If the next EQE is not valid then we are done */
215         if (!bf_get_le32(lpfc_eqe_valid, eqe))
216                 return NULL;
217         /* If the host has not yet processed the next entry then we are done */
218         if (((q->hba_index + 1) % q->entry_count) == q->host_index)
219                 return NULL;
220
221         q->hba_index = ((q->hba_index + 1) % q->entry_count);
222         return eqe;
223 }
224
225 /**
226  * lpfc_sli4_eq_release - Indicates the host has finished processing an EQ
227  * @q: The Event Queue that the host has completed processing for.
228  * @arm: Indicates whether the host wants to arms this CQ.
229  *
230  * This routine will mark all Event Queue Entries on @q, from the last
231  * known completed entry to the last entry that was processed, as completed
232  * by clearing the valid bit for each completion queue entry. Then it will
233  * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
234  * The internal host index in the @q will be updated by this routine to indicate
235  * that the host has finished processing the entries. The @arm parameter
236  * indicates that the queue should be rearmed when ringing the doorbell.
237  *
238  * This function will return the number of EQEs that were popped.
239  **/
240 uint32_t
241 lpfc_sli4_eq_release(struct lpfc_queue *q, bool arm)
242 {
243         uint32_t released = 0;
244         struct lpfc_eqe *temp_eqe;
245         struct lpfc_register doorbell;
246
247         /* while there are valid entries */
248         while (q->hba_index != q->host_index) {
249                 temp_eqe = q->qe[q->host_index].eqe;
250                 bf_set_le32(lpfc_eqe_valid, temp_eqe, 0);
251                 released++;
252                 q->host_index = ((q->host_index + 1) % q->entry_count);
253         }
254         if (unlikely(released == 0 && !arm))
255                 return 0;
256
257         /* ring doorbell for number popped */
258         doorbell.word0 = 0;
259         if (arm) {
260                 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
261                 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
262         }
263         bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
264         bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
265         bf_set(lpfc_eqcq_doorbell_eqid, &doorbell, q->queue_id);
266         writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
267         /* PCI read to flush PCI pipeline on re-arming for INTx mode */
268         if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
269                 readl(q->phba->sli4_hba.EQCQDBregaddr);
270         return released;
271 }
272
273 /**
274  * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
275  * @q: The Completion Queue to get the first valid CQE from
276  *
277  * This routine will get the first valid Completion Queue Entry from @q, update
278  * the queue's internal hba index, and return the CQE. If no valid CQEs are in
279  * the Queue (no more work to do), or the Queue is full of CQEs that have been
280  * processed, but not popped back to the HBA then this routine will return NULL.
281  **/
282 static struct lpfc_cqe *
283 lpfc_sli4_cq_get(struct lpfc_queue *q)
284 {
285         struct lpfc_cqe *cqe;
286
287         /* If the next CQE is not valid then we are done */
288         if (!bf_get_le32(lpfc_cqe_valid, q->qe[q->hba_index].cqe))
289                 return NULL;
290         /* If the host has not yet processed the next entry then we are done */
291         if (((q->hba_index + 1) % q->entry_count) == q->host_index)
292                 return NULL;
293
294         cqe = q->qe[q->hba_index].cqe;
295         q->hba_index = ((q->hba_index + 1) % q->entry_count);
296         return cqe;
297 }
298
299 /**
300  * lpfc_sli4_cq_release - Indicates the host has finished processing a CQ
301  * @q: The Completion Queue that the host has completed processing for.
302  * @arm: Indicates whether the host wants to arms this CQ.
303  *
304  * This routine will mark all Completion queue entries on @q, from the last
305  * known completed entry to the last entry that was processed, as completed
306  * by clearing the valid bit for each completion queue entry. Then it will
307  * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
308  * The internal host index in the @q will be updated by this routine to indicate
309  * that the host has finished processing the entries. The @arm parameter
310  * indicates that the queue should be rearmed when ringing the doorbell.
311  *
312  * This function will return the number of CQEs that were released.
313  **/
314 uint32_t
315 lpfc_sli4_cq_release(struct lpfc_queue *q, bool arm)
316 {
317         uint32_t released = 0;
318         struct lpfc_cqe *temp_qe;
319         struct lpfc_register doorbell;
320
321         /* while there are valid entries */
322         while (q->hba_index != q->host_index) {
323                 temp_qe = q->qe[q->host_index].cqe;
324                 bf_set_le32(lpfc_cqe_valid, temp_qe, 0);
325                 released++;
326                 q->host_index = ((q->host_index + 1) % q->entry_count);
327         }
328         if (unlikely(released == 0 && !arm))
329                 return 0;
330
331         /* ring doorbell for number popped */
332         doorbell.word0 = 0;
333         if (arm)
334                 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
335         bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
336         bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
337         bf_set(lpfc_eqcq_doorbell_cqid, &doorbell, q->queue_id);
338         writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
339         return released;
340 }
341
342 /**
343  * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
344  * @q: The Header Receive Queue to operate on.
345  * @wqe: The Receive Queue Entry to put on the Receive queue.
346  *
347  * This routine will copy the contents of @wqe to the next available entry on
348  * the @q. This function will then ring the Receive Queue Doorbell to signal the
349  * HBA to start processing the Receive Queue Entry. This function returns the
350  * index that the rqe was copied to if successful. If no entries are available
351  * on @q then this function will return -ENOMEM.
352  * The caller is expected to hold the hbalock when calling this routine.
353  **/
354 static int
355 lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
356                  struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
357 {
358         struct lpfc_rqe *temp_hrqe = hq->qe[hq->host_index].rqe;
359         struct lpfc_rqe *temp_drqe = dq->qe[dq->host_index].rqe;
360         struct lpfc_register doorbell;
361         int put_index = hq->host_index;
362
363         if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
364                 return -EINVAL;
365         if (hq->host_index != dq->host_index)
366                 return -EINVAL;
367         /* If the host has not yet processed the next entry then we are done */
368         if (((hq->host_index + 1) % hq->entry_count) == hq->hba_index)
369                 return -EBUSY;
370         lpfc_sli_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
371         lpfc_sli_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
372
373         /* Update the host index to point to the next slot */
374         hq->host_index = ((hq->host_index + 1) % hq->entry_count);
375         dq->host_index = ((dq->host_index + 1) % dq->entry_count);
376
377         /* Ring The Header Receive Queue Doorbell */
378         if (!(hq->host_index % LPFC_RQ_POST_BATCH)) {
379                 doorbell.word0 = 0;
380                 bf_set(lpfc_rq_doorbell_num_posted, &doorbell,
381                        LPFC_RQ_POST_BATCH);
382                 bf_set(lpfc_rq_doorbell_id, &doorbell, hq->queue_id);
383                 writel(doorbell.word0, hq->phba->sli4_hba.RQDBregaddr);
384         }
385         return put_index;
386 }
387
388 /**
389  * lpfc_sli4_rq_release - Updates internal hba index for RQ
390  * @q: The Header Receive Queue to operate on.
391  *
392  * This routine will update the HBA index of a queue to reflect consumption of
393  * one Receive Queue Entry by the HBA. When the HBA indicates that it has
394  * consumed an entry the host calls this function to update the queue's
395  * internal pointers. This routine returns the number of entries that were
396  * consumed by the HBA.
397  **/
398 static uint32_t
399 lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
400 {
401         if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
402                 return 0;
403         hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
404         dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
405         return 1;
406 }
407
408 /**
409  * lpfc_cmd_iocb - Get next command iocb entry in the ring
410  * @phba: Pointer to HBA context object.
411  * @pring: Pointer to driver SLI ring object.
412  *
413  * This function returns pointer to next command iocb entry
414  * in the command ring. The caller must hold hbalock to prevent
415  * other threads consume the next command iocb.
416  * SLI-2/SLI-3 provide different sized iocbs.
417  **/
418 static inline IOCB_t *
419 lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
420 {
421         return (IOCB_t *) (((char *) pring->cmdringaddr) +
422                            pring->cmdidx * phba->iocb_cmd_size);
423 }
424
425 /**
426  * lpfc_resp_iocb - Get next response iocb entry in the ring
427  * @phba: Pointer to HBA context object.
428  * @pring: Pointer to driver SLI ring object.
429  *
430  * This function returns pointer to next response iocb entry
431  * in the response ring. The caller must hold hbalock to make sure
432  * that no other thread consume the next response iocb.
433  * SLI-2/SLI-3 provide different sized iocbs.
434  **/
435 static inline IOCB_t *
436 lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
437 {
438         return (IOCB_t *) (((char *) pring->rspringaddr) +
439                            pring->rspidx * phba->iocb_rsp_size);
440 }
441
442 /**
443  * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
444  * @phba: Pointer to HBA context object.
445  *
446  * This function is called with hbalock held. This function
447  * allocates a new driver iocb object from the iocb pool. If the
448  * allocation is successful, it returns pointer to the newly
449  * allocated iocb object else it returns NULL.
450  **/
451 static struct lpfc_iocbq *
452 __lpfc_sli_get_iocbq(struct lpfc_hba *phba)
453 {
454         struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
455         struct lpfc_iocbq * iocbq = NULL;
456
457         list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
458         return iocbq;
459 }
460
461 /**
462  * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
463  * @phba: Pointer to HBA context object.
464  * @xritag: XRI value.
465  *
466  * This function clears the sglq pointer from the array of acive
467  * sglq's. The xritag that is passed in is used to index into the
468  * array. Before the xritag can be used it needs to be adjusted
469  * by subtracting the xribase.
470  *
471  * Returns sglq ponter = success, NULL = Failure.
472  **/
473 static struct lpfc_sglq *
474 __lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
475 {
476         uint16_t adj_xri;
477         struct lpfc_sglq *sglq;
478         adj_xri = xritag - phba->sli4_hba.max_cfg_param.xri_base;
479         if (adj_xri > phba->sli4_hba.max_cfg_param.max_xri)
480                 return NULL;
481         sglq = phba->sli4_hba.lpfc_sglq_active_list[adj_xri];
482         phba->sli4_hba.lpfc_sglq_active_list[adj_xri] = NULL;
483         return sglq;
484 }
485
486 /**
487  * __lpfc_get_active_sglq - Get the active sglq for this XRI.
488  * @phba: Pointer to HBA context object.
489  * @xritag: XRI value.
490  *
491  * This function returns the sglq pointer from the array of acive
492  * sglq's. The xritag that is passed in is used to index into the
493  * array. Before the xritag can be used it needs to be adjusted
494  * by subtracting the xribase.
495  *
496  * Returns sglq ponter = success, NULL = Failure.
497  **/
498 struct lpfc_sglq *
499 __lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
500 {
501         uint16_t adj_xri;
502         struct lpfc_sglq *sglq;
503         adj_xri = xritag - phba->sli4_hba.max_cfg_param.xri_base;
504         if (adj_xri > phba->sli4_hba.max_cfg_param.max_xri)
505                 return NULL;
506         sglq =  phba->sli4_hba.lpfc_sglq_active_list[adj_xri];
507         return sglq;
508 }
509
510 /**
511  * __lpfc_sli_get_sglq - Allocates an iocb object from sgl pool
512  * @phba: Pointer to HBA context object.
513  *
514  * This function is called with hbalock held. This function
515  * Gets a new driver sglq object from the sglq list. If the
516  * list is not empty then it is successful, it returns pointer to the newly
517  * allocated sglq object else it returns NULL.
518  **/
519 static struct lpfc_sglq *
520 __lpfc_sli_get_sglq(struct lpfc_hba *phba)
521 {
522         struct list_head *lpfc_sgl_list = &phba->sli4_hba.lpfc_sgl_list;
523         struct lpfc_sglq *sglq = NULL;
524         uint16_t adj_xri;
525         list_remove_head(lpfc_sgl_list, sglq, struct lpfc_sglq, list);
526         if (!sglq)
527                 return NULL;
528         adj_xri = sglq->sli4_xritag - phba->sli4_hba.max_cfg_param.xri_base;
529         phba->sli4_hba.lpfc_sglq_active_list[adj_xri] = sglq;
530         sglq->state = SGL_ALLOCATED;
531         return sglq;
532 }
533
534 /**
535  * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
536  * @phba: Pointer to HBA context object.
537  *
538  * This function is called with no lock held. This function
539  * allocates a new driver iocb object from the iocb pool. If the
540  * allocation is successful, it returns pointer to the newly
541  * allocated iocb object else it returns NULL.
542  **/
543 struct lpfc_iocbq *
544 lpfc_sli_get_iocbq(struct lpfc_hba *phba)
545 {
546         struct lpfc_iocbq * iocbq = NULL;
547         unsigned long iflags;
548
549         spin_lock_irqsave(&phba->hbalock, iflags);
550         iocbq = __lpfc_sli_get_iocbq(phba);
551         spin_unlock_irqrestore(&phba->hbalock, iflags);
552         return iocbq;
553 }
554
555 /**
556  * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
557  * @phba: Pointer to HBA context object.
558  * @iocbq: Pointer to driver iocb object.
559  *
560  * This function is called with hbalock held to release driver
561  * iocb object to the iocb pool. The iotag in the iocb object
562  * does not change for each use of the iocb object. This function
563  * clears all other fields of the iocb object when it is freed.
564  * The sqlq structure that holds the xritag and phys and virtual
565  * mappings for the scatter gather list is retrieved from the
566  * active array of sglq. The get of the sglq pointer also clears
567  * the entry in the array. If the status of the IO indiactes that
568  * this IO was aborted then the sglq entry it put on the
569  * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
570  * IO has good status or fails for any other reason then the sglq
571  * entry is added to the free list (lpfc_sgl_list).
572  **/
573 static void
574 __lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
575 {
576         struct lpfc_sglq *sglq;
577         size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
578         unsigned long iflag;
579
580         if (iocbq->sli4_xritag == NO_XRI)
581                 sglq = NULL;
582         else
583                 sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_xritag);
584         if (sglq)  {
585                 if ((iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) &&
586                         (sglq->state != SGL_XRI_ABORTED)) {
587                         spin_lock_irqsave(&phba->sli4_hba.abts_sgl_list_lock,
588                                         iflag);
589                         list_add(&sglq->list,
590                                 &phba->sli4_hba.lpfc_abts_els_sgl_list);
591                         spin_unlock_irqrestore(
592                                 &phba->sli4_hba.abts_sgl_list_lock, iflag);
593                 } else {
594                         sglq->state = SGL_FREED;
595                         list_add(&sglq->list, &phba->sli4_hba.lpfc_sgl_list);
596                 }
597         }
598
599
600         /*
601          * Clean all volatile data fields, preserve iotag and node struct.
602          */
603         memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
604         iocbq->sli4_xritag = NO_XRI;
605         list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
606 }
607
608 /**
609  * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
610  * @phba: Pointer to HBA context object.
611  * @iocbq: Pointer to driver iocb object.
612  *
613  * This function is called with hbalock held to release driver
614  * iocb object to the iocb pool. The iotag in the iocb object
615  * does not change for each use of the iocb object. This function
616  * clears all other fields of the iocb object when it is freed.
617  **/
618 static void
619 __lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
620 {
621         size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
622
623         /*
624          * Clean all volatile data fields, preserve iotag and node struct.
625          */
626         memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
627         iocbq->sli4_xritag = NO_XRI;
628         list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
629 }
630
631 /**
632  * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
633  * @phba: Pointer to HBA context object.
634  * @iocbq: Pointer to driver iocb object.
635  *
636  * This function is called with hbalock held to release driver
637  * iocb object to the iocb pool. The iotag in the iocb object
638  * does not change for each use of the iocb object. This function
639  * clears all other fields of the iocb object when it is freed.
640  **/
641 static void
642 __lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
643 {
644         phba->__lpfc_sli_release_iocbq(phba, iocbq);
645 }
646
647 /**
648  * lpfc_sli_release_iocbq - Release iocb to the iocb pool
649  * @phba: Pointer to HBA context object.
650  * @iocbq: Pointer to driver iocb object.
651  *
652  * This function is called with no lock held to release the iocb to
653  * iocb pool.
654  **/
655 void
656 lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
657 {
658         unsigned long iflags;
659
660         /*
661          * Clean all volatile data fields, preserve iotag and node struct.
662          */
663         spin_lock_irqsave(&phba->hbalock, iflags);
664         __lpfc_sli_release_iocbq(phba, iocbq);
665         spin_unlock_irqrestore(&phba->hbalock, iflags);
666 }
667
668 /**
669  * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
670  * @phba: Pointer to HBA context object.
671  * @iocblist: List of IOCBs.
672  * @ulpstatus: ULP status in IOCB command field.
673  * @ulpWord4: ULP word-4 in IOCB command field.
674  *
675  * This function is called with a list of IOCBs to cancel. It cancels the IOCB
676  * on the list by invoking the complete callback function associated with the
677  * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
678  * fields.
679  **/
680 void
681 lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
682                       uint32_t ulpstatus, uint32_t ulpWord4)
683 {
684         struct lpfc_iocbq *piocb;
685
686         while (!list_empty(iocblist)) {
687                 list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
688
689                 if (!piocb->iocb_cmpl)
690                         lpfc_sli_release_iocbq(phba, piocb);
691                 else {
692                         piocb->iocb.ulpStatus = ulpstatus;
693                         piocb->iocb.un.ulpWord[4] = ulpWord4;
694                         (piocb->iocb_cmpl) (phba, piocb, piocb);
695                 }
696         }
697         return;
698 }
699
700 /**
701  * lpfc_sli_iocb_cmd_type - Get the iocb type
702  * @iocb_cmnd: iocb command code.
703  *
704  * This function is called by ring event handler function to get the iocb type.
705  * This function translates the iocb command to an iocb command type used to
706  * decide the final disposition of each completed IOCB.
707  * The function returns
708  * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
709  * LPFC_SOL_IOCB     if it is a solicited iocb completion
710  * LPFC_ABORT_IOCB   if it is an abort iocb
711  * LPFC_UNSOL_IOCB   if it is an unsolicited iocb
712  *
713  * The caller is not required to hold any lock.
714  **/
715 static lpfc_iocb_type
716 lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
717 {
718         lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
719
720         if (iocb_cmnd > CMD_MAX_IOCB_CMD)
721                 return 0;
722
723         switch (iocb_cmnd) {
724         case CMD_XMIT_SEQUENCE_CR:
725         case CMD_XMIT_SEQUENCE_CX:
726         case CMD_XMIT_BCAST_CN:
727         case CMD_XMIT_BCAST_CX:
728         case CMD_ELS_REQUEST_CR:
729         case CMD_ELS_REQUEST_CX:
730         case CMD_CREATE_XRI_CR:
731         case CMD_CREATE_XRI_CX:
732         case CMD_GET_RPI_CN:
733         case CMD_XMIT_ELS_RSP_CX:
734         case CMD_GET_RPI_CR:
735         case CMD_FCP_IWRITE_CR:
736         case CMD_FCP_IWRITE_CX:
737         case CMD_FCP_IREAD_CR:
738         case CMD_FCP_IREAD_CX:
739         case CMD_FCP_ICMND_CR:
740         case CMD_FCP_ICMND_CX:
741         case CMD_FCP_TSEND_CX:
742         case CMD_FCP_TRSP_CX:
743         case CMD_FCP_TRECEIVE_CX:
744         case CMD_FCP_AUTO_TRSP_CX:
745         case CMD_ADAPTER_MSG:
746         case CMD_ADAPTER_DUMP:
747         case CMD_XMIT_SEQUENCE64_CR:
748         case CMD_XMIT_SEQUENCE64_CX:
749         case CMD_XMIT_BCAST64_CN:
750         case CMD_XMIT_BCAST64_CX:
751         case CMD_ELS_REQUEST64_CR:
752         case CMD_ELS_REQUEST64_CX:
753         case CMD_FCP_IWRITE64_CR:
754         case CMD_FCP_IWRITE64_CX:
755         case CMD_FCP_IREAD64_CR:
756         case CMD_FCP_IREAD64_CX:
757         case CMD_FCP_ICMND64_CR:
758         case CMD_FCP_ICMND64_CX:
759         case CMD_FCP_TSEND64_CX:
760         case CMD_FCP_TRSP64_CX:
761         case CMD_FCP_TRECEIVE64_CX:
762         case CMD_GEN_REQUEST64_CR:
763         case CMD_GEN_REQUEST64_CX:
764         case CMD_XMIT_ELS_RSP64_CX:
765         case DSSCMD_IWRITE64_CR:
766         case DSSCMD_IWRITE64_CX:
767         case DSSCMD_IREAD64_CR:
768         case DSSCMD_IREAD64_CX:
769                 type = LPFC_SOL_IOCB;
770                 break;
771         case CMD_ABORT_XRI_CN:
772         case CMD_ABORT_XRI_CX:
773         case CMD_CLOSE_XRI_CN:
774         case CMD_CLOSE_XRI_CX:
775         case CMD_XRI_ABORTED_CX:
776         case CMD_ABORT_MXRI64_CN:
777         case CMD_XMIT_BLS_RSP64_CX:
778                 type = LPFC_ABORT_IOCB;
779                 break;
780         case CMD_RCV_SEQUENCE_CX:
781         case CMD_RCV_ELS_REQ_CX:
782         case CMD_RCV_SEQUENCE64_CX:
783         case CMD_RCV_ELS_REQ64_CX:
784         case CMD_ASYNC_STATUS:
785         case CMD_IOCB_RCV_SEQ64_CX:
786         case CMD_IOCB_RCV_ELS64_CX:
787         case CMD_IOCB_RCV_CONT64_CX:
788         case CMD_IOCB_RET_XRI64_CX:
789                 type = LPFC_UNSOL_IOCB;
790                 break;
791         case CMD_IOCB_XMIT_MSEQ64_CR:
792         case CMD_IOCB_XMIT_MSEQ64_CX:
793         case CMD_IOCB_RCV_SEQ_LIST64_CX:
794         case CMD_IOCB_RCV_ELS_LIST64_CX:
795         case CMD_IOCB_CLOSE_EXTENDED_CN:
796         case CMD_IOCB_ABORT_EXTENDED_CN:
797         case CMD_IOCB_RET_HBQE64_CN:
798         case CMD_IOCB_FCP_IBIDIR64_CR:
799         case CMD_IOCB_FCP_IBIDIR64_CX:
800         case CMD_IOCB_FCP_ITASKMGT64_CX:
801         case CMD_IOCB_LOGENTRY_CN:
802         case CMD_IOCB_LOGENTRY_ASYNC_CN:
803                 printk("%s - Unhandled SLI-3 Command x%x\n",
804                                 __func__, iocb_cmnd);
805                 type = LPFC_UNKNOWN_IOCB;
806                 break;
807         default:
808                 type = LPFC_UNKNOWN_IOCB;
809                 break;
810         }
811
812         return type;
813 }
814
815 /**
816  * lpfc_sli_ring_map - Issue config_ring mbox for all rings
817  * @phba: Pointer to HBA context object.
818  *
819  * This function is called from SLI initialization code
820  * to configure every ring of the HBA's SLI interface. The
821  * caller is not required to hold any lock. This function issues
822  * a config_ring mailbox command for each ring.
823  * This function returns zero if successful else returns a negative
824  * error code.
825  **/
826 static int
827 lpfc_sli_ring_map(struct lpfc_hba *phba)
828 {
829         struct lpfc_sli *psli = &phba->sli;
830         LPFC_MBOXQ_t *pmb;
831         MAILBOX_t *pmbox;
832         int i, rc, ret = 0;
833
834         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
835         if (!pmb)
836                 return -ENOMEM;
837         pmbox = &pmb->u.mb;
838         phba->link_state = LPFC_INIT_MBX_CMDS;
839         for (i = 0; i < psli->num_rings; i++) {
840                 lpfc_config_ring(phba, i, pmb);
841                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
842                 if (rc != MBX_SUCCESS) {
843                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
844                                         "0446 Adapter failed to init (%d), "
845                                         "mbxCmd x%x CFG_RING, mbxStatus x%x, "
846                                         "ring %d\n",
847                                         rc, pmbox->mbxCommand,
848                                         pmbox->mbxStatus, i);
849                         phba->link_state = LPFC_HBA_ERROR;
850                         ret = -ENXIO;
851                         break;
852                 }
853         }
854         mempool_free(pmb, phba->mbox_mem_pool);
855         return ret;
856 }
857
858 /**
859  * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
860  * @phba: Pointer to HBA context object.
861  * @pring: Pointer to driver SLI ring object.
862  * @piocb: Pointer to the driver iocb object.
863  *
864  * This function is called with hbalock held. The function adds the
865  * new iocb to txcmplq of the given ring. This function always returns
866  * 0. If this function is called for ELS ring, this function checks if
867  * there is a vport associated with the ELS command. This function also
868  * starts els_tmofunc timer if this is an ELS command.
869  **/
870 static int
871 lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
872                         struct lpfc_iocbq *piocb)
873 {
874         list_add_tail(&piocb->list, &pring->txcmplq);
875         pring->txcmplq_cnt++;
876         if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
877            (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
878            (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
879                 if (!piocb->vport)
880                         BUG();
881                 else
882                         mod_timer(&piocb->vport->els_tmofunc,
883                                   jiffies + HZ * (phba->fc_ratov << 1));
884         }
885
886
887         return 0;
888 }
889
890 /**
891  * lpfc_sli_ringtx_get - Get first element of the txq
892  * @phba: Pointer to HBA context object.
893  * @pring: Pointer to driver SLI ring object.
894  *
895  * This function is called with hbalock held to get next
896  * iocb in txq of the given ring. If there is any iocb in
897  * the txq, the function returns first iocb in the list after
898  * removing the iocb from the list, else it returns NULL.
899  **/
900 static struct lpfc_iocbq *
901 lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
902 {
903         struct lpfc_iocbq *cmd_iocb;
904
905         list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
906         if (cmd_iocb != NULL)
907                 pring->txq_cnt--;
908         return cmd_iocb;
909 }
910
911 /**
912  * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
913  * @phba: Pointer to HBA context object.
914  * @pring: Pointer to driver SLI ring object.
915  *
916  * This function is called with hbalock held and the caller must post the
917  * iocb without releasing the lock. If the caller releases the lock,
918  * iocb slot returned by the function is not guaranteed to be available.
919  * The function returns pointer to the next available iocb slot if there
920  * is available slot in the ring, else it returns NULL.
921  * If the get index of the ring is ahead of the put index, the function
922  * will post an error attention event to the worker thread to take the
923  * HBA to offline state.
924  **/
925 static IOCB_t *
926 lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
927 {
928         struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
929         uint32_t  max_cmd_idx = pring->numCiocb;
930         if ((pring->next_cmdidx == pring->cmdidx) &&
931            (++pring->next_cmdidx >= max_cmd_idx))
932                 pring->next_cmdidx = 0;
933
934         if (unlikely(pring->local_getidx == pring->next_cmdidx)) {
935
936                 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
937
938                 if (unlikely(pring->local_getidx >= max_cmd_idx)) {
939                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
940                                         "0315 Ring %d issue: portCmdGet %d "
941                                         "is bigger than cmd ring %d\n",
942                                         pring->ringno,
943                                         pring->local_getidx, max_cmd_idx);
944
945                         phba->link_state = LPFC_HBA_ERROR;
946                         /*
947                          * All error attention handlers are posted to
948                          * worker thread
949                          */
950                         phba->work_ha |= HA_ERATT;
951                         phba->work_hs = HS_FFER3;
952
953                         lpfc_worker_wake_up(phba);
954
955                         return NULL;
956                 }
957
958                 if (pring->local_getidx == pring->next_cmdidx)
959                         return NULL;
960         }
961
962         return lpfc_cmd_iocb(phba, pring);
963 }
964
965 /**
966  * lpfc_sli_next_iotag - Get an iotag for the iocb
967  * @phba: Pointer to HBA context object.
968  * @iocbq: Pointer to driver iocb object.
969  *
970  * This function gets an iotag for the iocb. If there is no unused iotag and
971  * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
972  * array and assigns a new iotag.
973  * The function returns the allocated iotag if successful, else returns zero.
974  * Zero is not a valid iotag.
975  * The caller is not required to hold any lock.
976  **/
977 uint16_t
978 lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
979 {
980         struct lpfc_iocbq **new_arr;
981         struct lpfc_iocbq **old_arr;
982         size_t new_len;
983         struct lpfc_sli *psli = &phba->sli;
984         uint16_t iotag;
985
986         spin_lock_irq(&phba->hbalock);
987         iotag = psli->last_iotag;
988         if(++iotag < psli->iocbq_lookup_len) {
989                 psli->last_iotag = iotag;
990                 psli->iocbq_lookup[iotag] = iocbq;
991                 spin_unlock_irq(&phba->hbalock);
992                 iocbq->iotag = iotag;
993                 return iotag;
994         } else if (psli->iocbq_lookup_len < (0xffff
995                                            - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
996                 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
997                 spin_unlock_irq(&phba->hbalock);
998                 new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
999                                   GFP_KERNEL);
1000                 if (new_arr) {
1001                         spin_lock_irq(&phba->hbalock);
1002                         old_arr = psli->iocbq_lookup;
1003                         if (new_len <= psli->iocbq_lookup_len) {
1004                                 /* highly unprobable case */
1005                                 kfree(new_arr);
1006                                 iotag = psli->last_iotag;
1007                                 if(++iotag < psli->iocbq_lookup_len) {
1008                                         psli->last_iotag = iotag;
1009                                         psli->iocbq_lookup[iotag] = iocbq;
1010                                         spin_unlock_irq(&phba->hbalock);
1011                                         iocbq->iotag = iotag;
1012                                         return iotag;
1013                                 }
1014                                 spin_unlock_irq(&phba->hbalock);
1015                                 return 0;
1016                         }
1017                         if (psli->iocbq_lookup)
1018                                 memcpy(new_arr, old_arr,
1019                                        ((psli->last_iotag  + 1) *
1020                                         sizeof (struct lpfc_iocbq *)));
1021                         psli->iocbq_lookup = new_arr;
1022                         psli->iocbq_lookup_len = new_len;
1023                         psli->last_iotag = iotag;
1024                         psli->iocbq_lookup[iotag] = iocbq;
1025                         spin_unlock_irq(&phba->hbalock);
1026                         iocbq->iotag = iotag;
1027                         kfree(old_arr);
1028                         return iotag;
1029                 }
1030         } else
1031                 spin_unlock_irq(&phba->hbalock);
1032
1033         lpfc_printf_log(phba, KERN_ERR,LOG_SLI,
1034                         "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
1035                         psli->last_iotag);
1036
1037         return 0;
1038 }
1039
1040 /**
1041  * lpfc_sli_submit_iocb - Submit an iocb to the firmware
1042  * @phba: Pointer to HBA context object.
1043  * @pring: Pointer to driver SLI ring object.
1044  * @iocb: Pointer to iocb slot in the ring.
1045  * @nextiocb: Pointer to driver iocb object which need to be
1046  *            posted to firmware.
1047  *
1048  * This function is called with hbalock held to post a new iocb to
1049  * the firmware. This function copies the new iocb to ring iocb slot and
1050  * updates the ring pointers. It adds the new iocb to txcmplq if there is
1051  * a completion call back for this iocb else the function will free the
1052  * iocb object.
1053  **/
1054 static void
1055 lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1056                 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
1057 {
1058         /*
1059          * Set up an iotag
1060          */
1061         nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
1062
1063
1064         if (pring->ringno == LPFC_ELS_RING) {
1065                 lpfc_debugfs_slow_ring_trc(phba,
1066                         "IOCB cmd ring:   wd4:x%08x wd6:x%08x wd7:x%08x",
1067                         *(((uint32_t *) &nextiocb->iocb) + 4),
1068                         *(((uint32_t *) &nextiocb->iocb) + 6),
1069                         *(((uint32_t *) &nextiocb->iocb) + 7));
1070         }
1071
1072         /*
1073          * Issue iocb command to adapter
1074          */
1075         lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
1076         wmb();
1077         pring->stats.iocb_cmd++;
1078
1079         /*
1080          * If there is no completion routine to call, we can release the
1081          * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
1082          * that have no rsp ring completion, iocb_cmpl MUST be NULL.
1083          */
1084         if (nextiocb->iocb_cmpl)
1085                 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
1086         else
1087                 __lpfc_sli_release_iocbq(phba, nextiocb);
1088
1089         /*
1090          * Let the HBA know what IOCB slot will be the next one the
1091          * driver will put a command into.
1092          */
1093         pring->cmdidx = pring->next_cmdidx;
1094         writel(pring->cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
1095 }
1096
1097 /**
1098  * lpfc_sli_update_full_ring - Update the chip attention register
1099  * @phba: Pointer to HBA context object.
1100  * @pring: Pointer to driver SLI ring object.
1101  *
1102  * The caller is not required to hold any lock for calling this function.
1103  * This function updates the chip attention bits for the ring to inform firmware
1104  * that there are pending work to be done for this ring and requests an
1105  * interrupt when there is space available in the ring. This function is
1106  * called when the driver is unable to post more iocbs to the ring due
1107  * to unavailability of space in the ring.
1108  **/
1109 static void
1110 lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1111 {
1112         int ringno = pring->ringno;
1113
1114         pring->flag |= LPFC_CALL_RING_AVAILABLE;
1115
1116         wmb();
1117
1118         /*
1119          * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
1120          * The HBA will tell us when an IOCB entry is available.
1121          */
1122         writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
1123         readl(phba->CAregaddr); /* flush */
1124
1125         pring->stats.iocb_cmd_full++;
1126 }
1127
1128 /**
1129  * lpfc_sli_update_ring - Update chip attention register
1130  * @phba: Pointer to HBA context object.
1131  * @pring: Pointer to driver SLI ring object.
1132  *
1133  * This function updates the chip attention register bit for the
1134  * given ring to inform HBA that there is more work to be done
1135  * in this ring. The caller is not required to hold any lock.
1136  **/
1137 static void
1138 lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1139 {
1140         int ringno = pring->ringno;
1141
1142         /*
1143          * Tell the HBA that there is work to do in this ring.
1144          */
1145         if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
1146                 wmb();
1147                 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
1148                 readl(phba->CAregaddr); /* flush */
1149         }
1150 }
1151
1152 /**
1153  * lpfc_sli_resume_iocb - Process iocbs in the txq
1154  * @phba: Pointer to HBA context object.
1155  * @pring: Pointer to driver SLI ring object.
1156  *
1157  * This function is called with hbalock held to post pending iocbs
1158  * in the txq to the firmware. This function is called when driver
1159  * detects space available in the ring.
1160  **/
1161 static void
1162 lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1163 {
1164         IOCB_t *iocb;
1165         struct lpfc_iocbq *nextiocb;
1166
1167         /*
1168          * Check to see if:
1169          *  (a) there is anything on the txq to send
1170          *  (b) link is up
1171          *  (c) link attention events can be processed (fcp ring only)
1172          *  (d) IOCB processing is not blocked by the outstanding mbox command.
1173          */
1174         if (pring->txq_cnt &&
1175             lpfc_is_link_up(phba) &&
1176             (pring->ringno != phba->sli.fcp_ring ||
1177              phba->sli.sli_flag & LPFC_PROCESS_LA)) {
1178
1179                 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
1180                        (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
1181                         lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
1182
1183                 if (iocb)
1184                         lpfc_sli_update_ring(phba, pring);
1185                 else
1186                         lpfc_sli_update_full_ring(phba, pring);
1187         }
1188
1189         return;
1190 }
1191
1192 /**
1193  * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
1194  * @phba: Pointer to HBA context object.
1195  * @hbqno: HBQ number.
1196  *
1197  * This function is called with hbalock held to get the next
1198  * available slot for the given HBQ. If there is free slot
1199  * available for the HBQ it will return pointer to the next available
1200  * HBQ entry else it will return NULL.
1201  **/
1202 static struct lpfc_hbq_entry *
1203 lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
1204 {
1205         struct hbq_s *hbqp = &phba->hbqs[hbqno];
1206
1207         if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
1208             ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
1209                 hbqp->next_hbqPutIdx = 0;
1210
1211         if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
1212                 uint32_t raw_index = phba->hbq_get[hbqno];
1213                 uint32_t getidx = le32_to_cpu(raw_index);
1214
1215                 hbqp->local_hbqGetIdx = getidx;
1216
1217                 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
1218                         lpfc_printf_log(phba, KERN_ERR,
1219                                         LOG_SLI | LOG_VPORT,
1220                                         "1802 HBQ %d: local_hbqGetIdx "
1221                                         "%u is > than hbqp->entry_count %u\n",
1222                                         hbqno, hbqp->local_hbqGetIdx,
1223                                         hbqp->entry_count);
1224
1225                         phba->link_state = LPFC_HBA_ERROR;
1226                         return NULL;
1227                 }
1228
1229                 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
1230                         return NULL;
1231         }
1232
1233         return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
1234                         hbqp->hbqPutIdx;
1235 }
1236
1237 /**
1238  * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
1239  * @phba: Pointer to HBA context object.
1240  *
1241  * This function is called with no lock held to free all the
1242  * hbq buffers while uninitializing the SLI interface. It also
1243  * frees the HBQ buffers returned by the firmware but not yet
1244  * processed by the upper layers.
1245  **/
1246 void
1247 lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
1248 {
1249         struct lpfc_dmabuf *dmabuf, *next_dmabuf;
1250         struct hbq_dmabuf *hbq_buf;
1251         unsigned long flags;
1252         int i, hbq_count;
1253         uint32_t hbqno;
1254
1255         hbq_count = lpfc_sli_hbq_count();
1256         /* Return all memory used by all HBQs */
1257         spin_lock_irqsave(&phba->hbalock, flags);
1258         for (i = 0; i < hbq_count; ++i) {
1259                 list_for_each_entry_safe(dmabuf, next_dmabuf,
1260                                 &phba->hbqs[i].hbq_buffer_list, list) {
1261                         hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1262                         list_del(&hbq_buf->dbuf.list);
1263                         (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
1264                 }
1265                 phba->hbqs[i].buffer_count = 0;
1266         }
1267         /* Return all HBQ buffer that are in-fly */
1268         list_for_each_entry_safe(dmabuf, next_dmabuf, &phba->rb_pend_list,
1269                                  list) {
1270                 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1271                 list_del(&hbq_buf->dbuf.list);
1272                 if (hbq_buf->tag == -1) {
1273                         (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1274                                 (phba, hbq_buf);
1275                 } else {
1276                         hbqno = hbq_buf->tag >> 16;
1277                         if (hbqno >= LPFC_MAX_HBQS)
1278                                 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1279                                         (phba, hbq_buf);
1280                         else
1281                                 (phba->hbqs[hbqno].hbq_free_buffer)(phba,
1282                                         hbq_buf);
1283                 }
1284         }
1285
1286         /* Mark the HBQs not in use */
1287         phba->hbq_in_use = 0;
1288         spin_unlock_irqrestore(&phba->hbalock, flags);
1289 }
1290
1291 /**
1292  * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
1293  * @phba: Pointer to HBA context object.
1294  * @hbqno: HBQ number.
1295  * @hbq_buf: Pointer to HBQ buffer.
1296  *
1297  * This function is called with the hbalock held to post a
1298  * hbq buffer to the firmware. If the function finds an empty
1299  * slot in the HBQ, it will post the buffer. The function will return
1300  * pointer to the hbq entry if it successfully post the buffer
1301  * else it will return NULL.
1302  **/
1303 static int
1304 lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
1305                          struct hbq_dmabuf *hbq_buf)
1306 {
1307         return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
1308 }
1309
1310 /**
1311  * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
1312  * @phba: Pointer to HBA context object.
1313  * @hbqno: HBQ number.
1314  * @hbq_buf: Pointer to HBQ buffer.
1315  *
1316  * This function is called with the hbalock held to post a hbq buffer to the
1317  * firmware. If the function finds an empty slot in the HBQ, it will post the
1318  * buffer and place it on the hbq_buffer_list. The function will return zero if
1319  * it successfully post the buffer else it will return an error.
1320  **/
1321 static int
1322 lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
1323                             struct hbq_dmabuf *hbq_buf)
1324 {
1325         struct lpfc_hbq_entry *hbqe;
1326         dma_addr_t physaddr = hbq_buf->dbuf.phys;
1327
1328         /* Get next HBQ entry slot to use */
1329         hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
1330         if (hbqe) {
1331                 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1332
1333                 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1334                 hbqe->bde.addrLow  = le32_to_cpu(putPaddrLow(physaddr));
1335                 hbqe->bde.tus.f.bdeSize = hbq_buf->size;
1336                 hbqe->bde.tus.f.bdeFlags = 0;
1337                 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
1338                 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
1339                                 /* Sync SLIM */
1340                 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
1341                 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
1342                                 /* flush */
1343                 readl(phba->hbq_put + hbqno);
1344                 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
1345                 return 0;
1346         } else
1347                 return -ENOMEM;
1348 }
1349
1350 /**
1351  * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
1352  * @phba: Pointer to HBA context object.
1353  * @hbqno: HBQ number.
1354  * @hbq_buf: Pointer to HBQ buffer.
1355  *
1356  * This function is called with the hbalock held to post an RQE to the SLI4
1357  * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
1358  * the hbq_buffer_list and return zero, otherwise it will return an error.
1359  **/
1360 static int
1361 lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
1362                             struct hbq_dmabuf *hbq_buf)
1363 {
1364         int rc;
1365         struct lpfc_rqe hrqe;
1366         struct lpfc_rqe drqe;
1367
1368         hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
1369         hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
1370         drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
1371         drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
1372         rc = lpfc_sli4_rq_put(phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
1373                               &hrqe, &drqe);
1374         if (rc < 0)
1375                 return rc;
1376         hbq_buf->tag = rc;
1377         list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
1378         return 0;
1379 }
1380
1381 /* HBQ for ELS and CT traffic. */
1382 static struct lpfc_hbq_init lpfc_els_hbq = {
1383         .rn = 1,
1384         .entry_count = 256,
1385         .mask_count = 0,
1386         .profile = 0,
1387         .ring_mask = (1 << LPFC_ELS_RING),
1388         .buffer_count = 0,
1389         .init_count = 40,
1390         .add_count = 40,
1391 };
1392
1393 /* HBQ for the extra ring if needed */
1394 static struct lpfc_hbq_init lpfc_extra_hbq = {
1395         .rn = 1,
1396         .entry_count = 200,
1397         .mask_count = 0,
1398         .profile = 0,
1399         .ring_mask = (1 << LPFC_EXTRA_RING),
1400         .buffer_count = 0,
1401         .init_count = 0,
1402         .add_count = 5,
1403 };
1404
1405 /* Array of HBQs */
1406 struct lpfc_hbq_init *lpfc_hbq_defs[] = {
1407         &lpfc_els_hbq,
1408         &lpfc_extra_hbq,
1409 };
1410
1411 /**
1412  * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
1413  * @phba: Pointer to HBA context object.
1414  * @hbqno: HBQ number.
1415  * @count: Number of HBQ buffers to be posted.
1416  *
1417  * This function is called with no lock held to post more hbq buffers to the
1418  * given HBQ. The function returns the number of HBQ buffers successfully
1419  * posted.
1420  **/
1421 static int
1422 lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
1423 {
1424         uint32_t i, posted = 0;
1425         unsigned long flags;
1426         struct hbq_dmabuf *hbq_buffer;
1427         LIST_HEAD(hbq_buf_list);
1428         if (!phba->hbqs[hbqno].hbq_alloc_buffer)
1429                 return 0;
1430
1431         if ((phba->hbqs[hbqno].buffer_count + count) >
1432             lpfc_hbq_defs[hbqno]->entry_count)
1433                 count = lpfc_hbq_defs[hbqno]->entry_count -
1434                                         phba->hbqs[hbqno].buffer_count;
1435         if (!count)
1436                 return 0;
1437         /* Allocate HBQ entries */
1438         for (i = 0; i < count; i++) {
1439                 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
1440                 if (!hbq_buffer)
1441                         break;
1442                 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
1443         }
1444         /* Check whether HBQ is still in use */
1445         spin_lock_irqsave(&phba->hbalock, flags);
1446         if (!phba->hbq_in_use)
1447                 goto err;
1448         while (!list_empty(&hbq_buf_list)) {
1449                 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1450                                  dbuf.list);
1451                 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
1452                                       (hbqno << 16));
1453                 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
1454                         phba->hbqs[hbqno].buffer_count++;
1455                         posted++;
1456                 } else
1457                         (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1458         }
1459         spin_unlock_irqrestore(&phba->hbalock, flags);
1460         return posted;
1461 err:
1462         spin_unlock_irqrestore(&phba->hbalock, flags);
1463         while (!list_empty(&hbq_buf_list)) {
1464                 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1465                                  dbuf.list);
1466                 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1467         }
1468         return 0;
1469 }
1470
1471 /**
1472  * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
1473  * @phba: Pointer to HBA context object.
1474  * @qno: HBQ number.
1475  *
1476  * This function posts more buffers to the HBQ. This function
1477  * is called with no lock held. The function returns the number of HBQ entries
1478  * successfully allocated.
1479  **/
1480 int
1481 lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
1482 {
1483         if (phba->sli_rev == LPFC_SLI_REV4)
1484                 return 0;
1485         else
1486                 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1487                                          lpfc_hbq_defs[qno]->add_count);
1488 }
1489
1490 /**
1491  * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
1492  * @phba: Pointer to HBA context object.
1493  * @qno:  HBQ queue number.
1494  *
1495  * This function is called from SLI initialization code path with
1496  * no lock held to post initial HBQ buffers to firmware. The
1497  * function returns the number of HBQ entries successfully allocated.
1498  **/
1499 static int
1500 lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
1501 {
1502         if (phba->sli_rev == LPFC_SLI_REV4)
1503                 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1504                                          lpfc_hbq_defs[qno]->entry_count);
1505         else
1506                 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1507                                          lpfc_hbq_defs[qno]->init_count);
1508 }
1509
1510 /**
1511  * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
1512  * @phba: Pointer to HBA context object.
1513  * @hbqno: HBQ number.
1514  *
1515  * This function removes the first hbq buffer on an hbq list and returns a
1516  * pointer to that buffer. If it finds no buffers on the list it returns NULL.
1517  **/
1518 static struct hbq_dmabuf *
1519 lpfc_sli_hbqbuf_get(struct list_head *rb_list)
1520 {
1521         struct lpfc_dmabuf *d_buf;
1522
1523         list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
1524         if (!d_buf)
1525                 return NULL;
1526         return container_of(d_buf, struct hbq_dmabuf, dbuf);
1527 }
1528
1529 /**
1530  * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
1531  * @phba: Pointer to HBA context object.
1532  * @tag: Tag of the hbq buffer.
1533  *
1534  * This function is called with hbalock held. This function searches
1535  * for the hbq buffer associated with the given tag in the hbq buffer
1536  * list. If it finds the hbq buffer, it returns the hbq_buffer other wise
1537  * it returns NULL.
1538  **/
1539 static struct hbq_dmabuf *
1540 lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
1541 {
1542         struct lpfc_dmabuf *d_buf;
1543         struct hbq_dmabuf *hbq_buf;
1544         uint32_t hbqno;
1545
1546         hbqno = tag >> 16;
1547         if (hbqno >= LPFC_MAX_HBQS)
1548                 return NULL;
1549
1550         spin_lock_irq(&phba->hbalock);
1551         list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
1552                 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
1553                 if (hbq_buf->tag == tag) {
1554                         spin_unlock_irq(&phba->hbalock);
1555                         return hbq_buf;
1556                 }
1557         }
1558         spin_unlock_irq(&phba->hbalock);
1559         lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
1560                         "1803 Bad hbq tag. Data: x%x x%x\n",
1561                         tag, phba->hbqs[tag >> 16].buffer_count);
1562         return NULL;
1563 }
1564
1565 /**
1566  * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
1567  * @phba: Pointer to HBA context object.
1568  * @hbq_buffer: Pointer to HBQ buffer.
1569  *
1570  * This function is called with hbalock. This function gives back
1571  * the hbq buffer to firmware. If the HBQ does not have space to
1572  * post the buffer, it will free the buffer.
1573  **/
1574 void
1575 lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
1576 {
1577         uint32_t hbqno;
1578
1579         if (hbq_buffer) {
1580                 hbqno = hbq_buffer->tag >> 16;
1581                 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
1582                         (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1583         }
1584 }
1585
1586 /**
1587  * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
1588  * @mbxCommand: mailbox command code.
1589  *
1590  * This function is called by the mailbox event handler function to verify
1591  * that the completed mailbox command is a legitimate mailbox command. If the
1592  * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
1593  * and the mailbox event handler will take the HBA offline.
1594  **/
1595 static int
1596 lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
1597 {
1598         uint8_t ret;
1599
1600         switch (mbxCommand) {
1601         case MBX_LOAD_SM:
1602         case MBX_READ_NV:
1603         case MBX_WRITE_NV:
1604         case MBX_WRITE_VPARMS:
1605         case MBX_RUN_BIU_DIAG:
1606         case MBX_INIT_LINK:
1607         case MBX_DOWN_LINK:
1608         case MBX_CONFIG_LINK:
1609         case MBX_CONFIG_RING:
1610         case MBX_RESET_RING:
1611         case MBX_READ_CONFIG:
1612         case MBX_READ_RCONFIG:
1613         case MBX_READ_SPARM:
1614         case MBX_READ_STATUS:
1615         case MBX_READ_RPI:
1616         case MBX_READ_XRI:
1617         case MBX_READ_REV:
1618         case MBX_READ_LNK_STAT:
1619         case MBX_REG_LOGIN:
1620         case MBX_UNREG_LOGIN:
1621         case MBX_READ_LA:
1622         case MBX_CLEAR_LA:
1623         case MBX_DUMP_MEMORY:
1624         case MBX_DUMP_CONTEXT:
1625         case MBX_RUN_DIAGS:
1626         case MBX_RESTART:
1627         case MBX_UPDATE_CFG:
1628         case MBX_DOWN_LOAD:
1629         case MBX_DEL_LD_ENTRY:
1630         case MBX_RUN_PROGRAM:
1631         case MBX_SET_MASK:
1632         case MBX_SET_VARIABLE:
1633         case MBX_UNREG_D_ID:
1634         case MBX_KILL_BOARD:
1635         case MBX_CONFIG_FARP:
1636         case MBX_BEACON:
1637         case MBX_LOAD_AREA:
1638         case MBX_RUN_BIU_DIAG64:
1639         case MBX_CONFIG_PORT:
1640         case MBX_READ_SPARM64:
1641         case MBX_READ_RPI64:
1642         case MBX_REG_LOGIN64:
1643         case MBX_READ_LA64:
1644         case MBX_WRITE_WWN:
1645         case MBX_SET_DEBUG:
1646         case MBX_LOAD_EXP_ROM:
1647         case MBX_ASYNCEVT_ENABLE:
1648         case MBX_REG_VPI:
1649         case MBX_UNREG_VPI:
1650         case MBX_HEARTBEAT:
1651         case MBX_PORT_CAPABILITIES:
1652         case MBX_PORT_IOV_CONTROL:
1653         case MBX_SLI4_CONFIG:
1654         case MBX_SLI4_REQ_FTRS:
1655         case MBX_REG_FCFI:
1656         case MBX_UNREG_FCFI:
1657         case MBX_REG_VFI:
1658         case MBX_UNREG_VFI:
1659         case MBX_INIT_VPI:
1660         case MBX_INIT_VFI:
1661         case MBX_RESUME_RPI:
1662         case MBX_READ_EVENT_LOG_STATUS:
1663         case MBX_READ_EVENT_LOG:
1664                 ret = mbxCommand;
1665                 break;
1666         default:
1667                 ret = MBX_SHUTDOWN;
1668                 break;
1669         }
1670         return ret;
1671 }
1672
1673 /**
1674  * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
1675  * @phba: Pointer to HBA context object.
1676  * @pmboxq: Pointer to mailbox command.
1677  *
1678  * This is completion handler function for mailbox commands issued from
1679  * lpfc_sli_issue_mbox_wait function. This function is called by the
1680  * mailbox event handler function with no lock held. This function
1681  * will wake up thread waiting on the wait queue pointed by context1
1682  * of the mailbox.
1683  **/
1684 void
1685 lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
1686 {
1687         wait_queue_head_t *pdone_q;
1688         unsigned long drvr_flag;
1689
1690         /*
1691          * If pdone_q is empty, the driver thread gave up waiting and
1692          * continued running.
1693          */
1694         pmboxq->mbox_flag |= LPFC_MBX_WAKE;
1695         spin_lock_irqsave(&phba->hbalock, drvr_flag);
1696         pdone_q = (wait_queue_head_t *) pmboxq->context1;
1697         if (pdone_q)
1698                 wake_up_interruptible(pdone_q);
1699         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
1700         return;
1701 }
1702
1703
1704 /**
1705  * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
1706  * @phba: Pointer to HBA context object.
1707  * @pmb: Pointer to mailbox object.
1708  *
1709  * This function is the default mailbox completion handler. It
1710  * frees the memory resources associated with the completed mailbox
1711  * command. If the completed command is a REG_LOGIN mailbox command,
1712  * this function will issue a UREG_LOGIN to re-claim the RPI.
1713  **/
1714 void
1715 lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
1716 {
1717         struct lpfc_dmabuf *mp;
1718         uint16_t rpi, vpi;
1719         int rc;
1720         struct lpfc_vport  *vport = pmb->vport;
1721
1722         mp = (struct lpfc_dmabuf *) (pmb->context1);
1723
1724         if (mp) {
1725                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1726                 kfree(mp);
1727         }
1728
1729         if ((pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) &&
1730             (phba->sli_rev == LPFC_SLI_REV4))
1731                 lpfc_sli4_free_rpi(phba, pmb->u.mb.un.varUnregLogin.rpi);
1732
1733         /*
1734          * If a REG_LOGIN succeeded  after node is destroyed or node
1735          * is in re-discovery driver need to cleanup the RPI.
1736          */
1737         if (!(phba->pport->load_flag & FC_UNLOADING) &&
1738             pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
1739             !pmb->u.mb.mbxStatus) {
1740                 rpi = pmb->u.mb.un.varWords[0];
1741                 vpi = pmb->u.mb.un.varRegLogin.vpi - phba->vpi_base;
1742                 lpfc_unreg_login(phba, vpi, rpi, pmb);
1743                 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
1744                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
1745                 if (rc != MBX_NOT_FINISHED)
1746                         return;
1747         }
1748
1749         /* Unreg VPI, if the REG_VPI succeed after VLink failure */
1750         if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
1751                 !(phba->pport->load_flag & FC_UNLOADING) &&
1752                 !pmb->u.mb.mbxStatus) {
1753                 lpfc_unreg_vpi(phba, pmb->u.mb.un.varRegVpi.vpi, pmb);
1754                 pmb->vport = vport;
1755                 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
1756                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
1757                 if (rc != MBX_NOT_FINISHED)
1758                         return;
1759         }
1760
1761         if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
1762                 lpfc_sli4_mbox_cmd_free(phba, pmb);
1763         else
1764                 mempool_free(pmb, phba->mbox_mem_pool);
1765 }
1766
1767 /**
1768  * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
1769  * @phba: Pointer to HBA context object.
1770  *
1771  * This function is called with no lock held. This function processes all
1772  * the completed mailbox commands and gives it to upper layers. The interrupt
1773  * service routine processes mailbox completion interrupt and adds completed
1774  * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
1775  * Worker thread call lpfc_sli_handle_mb_event, which will return the
1776  * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
1777  * function returns the mailbox commands to the upper layer by calling the
1778  * completion handler function of each mailbox.
1779  **/
1780 int
1781 lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
1782 {
1783         MAILBOX_t *pmbox;
1784         LPFC_MBOXQ_t *pmb;
1785         int rc;
1786         LIST_HEAD(cmplq);
1787
1788         phba->sli.slistat.mbox_event++;
1789
1790         /* Get all completed mailboxe buffers into the cmplq */
1791         spin_lock_irq(&phba->hbalock);
1792         list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
1793         spin_unlock_irq(&phba->hbalock);
1794
1795         /* Get a Mailbox buffer to setup mailbox commands for callback */
1796         do {
1797                 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
1798                 if (pmb == NULL)
1799                         break;
1800
1801                 pmbox = &pmb->u.mb;
1802
1803                 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
1804                         if (pmb->vport) {
1805                                 lpfc_debugfs_disc_trc(pmb->vport,
1806                                         LPFC_DISC_TRC_MBOX_VPORT,
1807                                         "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
1808                                         (uint32_t)pmbox->mbxCommand,
1809                                         pmbox->un.varWords[0],
1810                                         pmbox->un.varWords[1]);
1811                         }
1812                         else {
1813                                 lpfc_debugfs_disc_trc(phba->pport,
1814                                         LPFC_DISC_TRC_MBOX,
1815                                         "MBOX cmpl:       cmd:x%x mb:x%x x%x",
1816                                         (uint32_t)pmbox->mbxCommand,
1817                                         pmbox->un.varWords[0],
1818                                         pmbox->un.varWords[1]);
1819                         }
1820                 }
1821
1822                 /*
1823                  * It is a fatal error if unknown mbox command completion.
1824                  */
1825                 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
1826                     MBX_SHUTDOWN) {
1827                         /* Unknown mailbox command compl */
1828                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
1829                                         "(%d):0323 Unknown Mailbox command "
1830                                         "x%x (x%x) Cmpl\n",
1831                                         pmb->vport ? pmb->vport->vpi : 0,
1832                                         pmbox->mbxCommand,
1833                                         lpfc_sli4_mbox_opcode_get(phba, pmb));
1834                         phba->link_state = LPFC_HBA_ERROR;
1835                         phba->work_hs = HS_FFER3;
1836                         lpfc_handle_eratt(phba);
1837                         continue;
1838                 }
1839
1840                 if (pmbox->mbxStatus) {
1841                         phba->sli.slistat.mbox_stat_err++;
1842                         if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
1843                                 /* Mbox cmd cmpl error - RETRYing */
1844                                 lpfc_printf_log(phba, KERN_INFO,
1845                                                 LOG_MBOX | LOG_SLI,
1846                                                 "(%d):0305 Mbox cmd cmpl "
1847                                                 "error - RETRYing Data: x%x "
1848                                                 "(x%x) x%x x%x x%x\n",
1849                                                 pmb->vport ? pmb->vport->vpi :0,
1850                                                 pmbox->mbxCommand,
1851                                                 lpfc_sli4_mbox_opcode_get(phba,
1852                                                                           pmb),
1853                                                 pmbox->mbxStatus,
1854                                                 pmbox->un.varWords[0],
1855                                                 pmb->vport->port_state);
1856                                 pmbox->mbxStatus = 0;
1857                                 pmbox->mbxOwner = OWN_HOST;
1858                                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
1859                                 if (rc != MBX_NOT_FINISHED)
1860                                         continue;
1861                         }
1862                 }
1863
1864                 /* Mailbox cmd <cmd> Cmpl <cmpl> */
1865                 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
1866                                 "(%d):0307 Mailbox cmd x%x (x%x) Cmpl x%p "
1867                                 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x\n",
1868                                 pmb->vport ? pmb->vport->vpi : 0,
1869                                 pmbox->mbxCommand,
1870                                 lpfc_sli4_mbox_opcode_get(phba, pmb),
1871                                 pmb->mbox_cmpl,
1872                                 *((uint32_t *) pmbox),
1873                                 pmbox->un.varWords[0],
1874                                 pmbox->un.varWords[1],
1875                                 pmbox->un.varWords[2],
1876                                 pmbox->un.varWords[3],
1877                                 pmbox->un.varWords[4],
1878                                 pmbox->un.varWords[5],
1879                                 pmbox->un.varWords[6],
1880                                 pmbox->un.varWords[7]);
1881
1882                 if (pmb->mbox_cmpl)
1883                         pmb->mbox_cmpl(phba,pmb);
1884         } while (1);
1885         return 0;
1886 }
1887
1888 /**
1889  * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
1890  * @phba: Pointer to HBA context object.
1891  * @pring: Pointer to driver SLI ring object.
1892  * @tag: buffer tag.
1893  *
1894  * This function is called with no lock held. When QUE_BUFTAG_BIT bit
1895  * is set in the tag the buffer is posted for a particular exchange,
1896  * the function will return the buffer without replacing the buffer.
1897  * If the buffer is for unsolicited ELS or CT traffic, this function
1898  * returns the buffer and also posts another buffer to the firmware.
1899  **/
1900 static struct lpfc_dmabuf *
1901 lpfc_sli_get_buff(struct lpfc_hba *phba,
1902                   struct lpfc_sli_ring *pring,
1903                   uint32_t tag)
1904 {
1905         struct hbq_dmabuf *hbq_entry;
1906
1907         if (tag & QUE_BUFTAG_BIT)
1908                 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
1909         hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
1910         if (!hbq_entry)
1911                 return NULL;
1912         return &hbq_entry->dbuf;
1913 }
1914
1915 /**
1916  * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
1917  * @phba: Pointer to HBA context object.
1918  * @pring: Pointer to driver SLI ring object.
1919  * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
1920  * @fch_r_ctl: the r_ctl for the first frame of the sequence.
1921  * @fch_type: the type for the first frame of the sequence.
1922  *
1923  * This function is called with no lock held. This function uses the r_ctl and
1924  * type of the received sequence to find the correct callback function to call
1925  * to process the sequence.
1926  **/
1927 static int
1928 lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1929                          struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
1930                          uint32_t fch_type)
1931 {
1932         int i;
1933
1934         /* unSolicited Responses */
1935         if (pring->prt[0].profile) {
1936                 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
1937                         (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
1938                                                                         saveq);
1939                 return 1;
1940         }
1941         /* We must search, based on rctl / type
1942            for the right routine */
1943         for (i = 0; i < pring->num_mask; i++) {
1944                 if ((pring->prt[i].rctl == fch_r_ctl) &&
1945                     (pring->prt[i].type == fch_type)) {
1946                         if (pring->prt[i].lpfc_sli_rcv_unsol_event)
1947                                 (pring->prt[i].lpfc_sli_rcv_unsol_event)
1948                                                 (phba, pring, saveq);
1949                         return 1;
1950                 }
1951         }
1952         return 0;
1953 }
1954
1955 /**
1956  * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
1957  * @phba: Pointer to HBA context object.
1958  * @pring: Pointer to driver SLI ring object.
1959  * @saveq: Pointer to the unsolicited iocb.
1960  *
1961  * This function is called with no lock held by the ring event handler
1962  * when there is an unsolicited iocb posted to the response ring by the
1963  * firmware. This function gets the buffer associated with the iocbs
1964  * and calls the event handler for the ring. This function handles both
1965  * qring buffers and hbq buffers.
1966  * When the function returns 1 the caller can free the iocb object otherwise
1967  * upper layer functions will free the iocb objects.
1968  **/
1969 static int
1970 lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1971                             struct lpfc_iocbq *saveq)
1972 {
1973         IOCB_t           * irsp;
1974         WORD5            * w5p;
1975         uint32_t           Rctl, Type;
1976         uint32_t           match;
1977         struct lpfc_iocbq *iocbq;
1978         struct lpfc_dmabuf *dmzbuf;
1979
1980         match = 0;
1981         irsp = &(saveq->iocb);
1982
1983         if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
1984                 if (pring->lpfc_sli_rcv_async_status)
1985                         pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
1986                 else
1987                         lpfc_printf_log(phba,
1988                                         KERN_WARNING,
1989                                         LOG_SLI,
1990                                         "0316 Ring %d handler: unexpected "
1991                                         "ASYNC_STATUS iocb received evt_code "
1992                                         "0x%x\n",
1993                                         pring->ringno,
1994                                         irsp->un.asyncstat.evt_code);
1995                 return 1;
1996         }
1997
1998         if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
1999                 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
2000                 if (irsp->ulpBdeCount > 0) {
2001                         dmzbuf = lpfc_sli_get_buff(phba, pring,
2002                                         irsp->un.ulpWord[3]);
2003                         lpfc_in_buf_free(phba, dmzbuf);
2004                 }
2005
2006                 if (irsp->ulpBdeCount > 1) {
2007                         dmzbuf = lpfc_sli_get_buff(phba, pring,
2008                                         irsp->unsli3.sli3Words[3]);
2009                         lpfc_in_buf_free(phba, dmzbuf);
2010                 }
2011
2012                 if (irsp->ulpBdeCount > 2) {
2013                         dmzbuf = lpfc_sli_get_buff(phba, pring,
2014                                 irsp->unsli3.sli3Words[7]);
2015                         lpfc_in_buf_free(phba, dmzbuf);
2016                 }
2017
2018                 return 1;
2019         }
2020
2021         if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
2022                 if (irsp->ulpBdeCount != 0) {
2023                         saveq->context2 = lpfc_sli_get_buff(phba, pring,
2024                                                 irsp->un.ulpWord[3]);
2025                         if (!saveq->context2)
2026                                 lpfc_printf_log(phba,
2027                                         KERN_ERR,
2028                                         LOG_SLI,
2029                                         "0341 Ring %d Cannot find buffer for "
2030                                         "an unsolicited iocb. tag 0x%x\n",
2031                                         pring->ringno,
2032                                         irsp->un.ulpWord[3]);
2033                 }
2034                 if (irsp->ulpBdeCount == 2) {
2035                         saveq->context3 = lpfc_sli_get_buff(phba, pring,
2036                                                 irsp->unsli3.sli3Words[7]);
2037                         if (!saveq->context3)
2038                                 lpfc_printf_log(phba,
2039                                         KERN_ERR,
2040                                         LOG_SLI,
2041                                         "0342 Ring %d Cannot find buffer for an"
2042                                         " unsolicited iocb. tag 0x%x\n",
2043                                         pring->ringno,
2044                                         irsp->unsli3.sli3Words[7]);
2045                 }
2046                 list_for_each_entry(iocbq, &saveq->list, list) {
2047                         irsp = &(iocbq->iocb);
2048                         if (irsp->ulpBdeCount != 0) {
2049                                 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
2050                                                         irsp->un.ulpWord[3]);
2051                                 if (!iocbq->context2)
2052                                         lpfc_printf_log(phba,
2053                                                 KERN_ERR,
2054                                                 LOG_SLI,
2055                                                 "0343 Ring %d Cannot find "
2056                                                 "buffer for an unsolicited iocb"
2057                                                 ". tag 0x%x\n", pring->ringno,
2058                                                 irsp->un.ulpWord[3]);
2059                         }
2060                         if (irsp->ulpBdeCount == 2) {
2061                                 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
2062                                                 irsp->unsli3.sli3Words[7]);
2063                                 if (!iocbq->context3)
2064                                         lpfc_printf_log(phba,
2065                                                 KERN_ERR,
2066                                                 LOG_SLI,
2067                                                 "0344 Ring %d Cannot find "
2068                                                 "buffer for an unsolicited "
2069                                                 "iocb. tag 0x%x\n",
2070                                                 pring->ringno,
2071                                                 irsp->unsli3.sli3Words[7]);
2072                         }
2073                 }
2074         }
2075         if (irsp->ulpBdeCount != 0 &&
2076             (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
2077              irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
2078                 int found = 0;
2079
2080                 /* search continue save q for same XRI */
2081                 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
2082                         if (iocbq->iocb.ulpContext == saveq->iocb.ulpContext) {
2083                                 list_add_tail(&saveq->list, &iocbq->list);
2084                                 found = 1;
2085                                 break;
2086                         }
2087                 }
2088                 if (!found)
2089                         list_add_tail(&saveq->clist,
2090                                       &pring->iocb_continue_saveq);
2091                 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
2092                         list_del_init(&iocbq->clist);
2093                         saveq = iocbq;
2094                         irsp = &(saveq->iocb);
2095                 } else
2096                         return 0;
2097         }
2098         if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
2099             (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
2100             (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
2101                 Rctl = FC_RCTL_ELS_REQ;
2102                 Type = FC_TYPE_ELS;
2103         } else {
2104                 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
2105                 Rctl = w5p->hcsw.Rctl;
2106                 Type = w5p->hcsw.Type;
2107
2108                 /* Firmware Workaround */
2109                 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
2110                         (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
2111                          irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
2112                         Rctl = FC_RCTL_ELS_REQ;
2113                         Type = FC_TYPE_ELS;
2114                         w5p->hcsw.Rctl = Rctl;
2115                         w5p->hcsw.Type = Type;
2116                 }
2117         }
2118
2119         if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
2120                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
2121                                 "0313 Ring %d handler: unexpected Rctl x%x "
2122                                 "Type x%x received\n",
2123                                 pring->ringno, Rctl, Type);
2124
2125         return 1;
2126 }
2127
2128 /**
2129  * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
2130  * @phba: Pointer to HBA context object.
2131  * @pring: Pointer to driver SLI ring object.
2132  * @prspiocb: Pointer to response iocb object.
2133  *
2134  * This function looks up the iocb_lookup table to get the command iocb
2135  * corresponding to the given response iocb using the iotag of the
2136  * response iocb. This function is called with the hbalock held.
2137  * This function returns the command iocb object if it finds the command
2138  * iocb else returns NULL.
2139  **/
2140 static struct lpfc_iocbq *
2141 lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
2142                       struct lpfc_sli_ring *pring,
2143                       struct lpfc_iocbq *prspiocb)
2144 {
2145         struct lpfc_iocbq *cmd_iocb = NULL;
2146         uint16_t iotag;
2147
2148         iotag = prspiocb->iocb.ulpIoTag;
2149
2150         if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2151                 cmd_iocb = phba->sli.iocbq_lookup[iotag];
2152                 list_del_init(&cmd_iocb->list);
2153                 pring->txcmplq_cnt--;
2154                 return cmd_iocb;
2155         }
2156
2157         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2158                         "0317 iotag x%x is out off "
2159                         "range: max iotag x%x wd0 x%x\n",
2160                         iotag, phba->sli.last_iotag,
2161                         *(((uint32_t *) &prspiocb->iocb) + 7));
2162         return NULL;
2163 }
2164
2165 /**
2166  * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
2167  * @phba: Pointer to HBA context object.
2168  * @pring: Pointer to driver SLI ring object.
2169  * @iotag: IOCB tag.
2170  *
2171  * This function looks up the iocb_lookup table to get the command iocb
2172  * corresponding to the given iotag. This function is called with the
2173  * hbalock held.
2174  * This function returns the command iocb object if it finds the command
2175  * iocb else returns NULL.
2176  **/
2177 static struct lpfc_iocbq *
2178 lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
2179                              struct lpfc_sli_ring *pring, uint16_t iotag)
2180 {
2181         struct lpfc_iocbq *cmd_iocb;
2182
2183         if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2184                 cmd_iocb = phba->sli.iocbq_lookup[iotag];
2185                 list_del_init(&cmd_iocb->list);
2186                 pring->txcmplq_cnt--;
2187                 return cmd_iocb;
2188         }
2189
2190         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2191                         "0372 iotag x%x is out off range: max iotag (x%x)\n",
2192                         iotag, phba->sli.last_iotag);
2193         return NULL;
2194 }
2195
2196 /**
2197  * lpfc_sli_process_sol_iocb - process solicited iocb completion
2198  * @phba: Pointer to HBA context object.
2199  * @pring: Pointer to driver SLI ring object.
2200  * @saveq: Pointer to the response iocb to be processed.
2201  *
2202  * This function is called by the ring event handler for non-fcp
2203  * rings when there is a new response iocb in the response ring.
2204  * The caller is not required to hold any locks. This function
2205  * gets the command iocb associated with the response iocb and
2206  * calls the completion handler for the command iocb. If there
2207  * is no completion handler, the function will free the resources
2208  * associated with command iocb. If the response iocb is for
2209  * an already aborted command iocb, the status of the completion
2210  * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
2211  * This function always returns 1.
2212  **/
2213 static int
2214 lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2215                           struct lpfc_iocbq *saveq)
2216 {
2217         struct lpfc_iocbq *cmdiocbp;
2218         int rc = 1;
2219         unsigned long iflag;
2220
2221         /* Based on the iotag field, get the cmd IOCB from the txcmplq */
2222         spin_lock_irqsave(&phba->hbalock, iflag);
2223         cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
2224         spin_unlock_irqrestore(&phba->hbalock, iflag);
2225
2226         if (cmdiocbp) {
2227                 if (cmdiocbp->iocb_cmpl) {
2228                         /*
2229                          * If an ELS command failed send an event to mgmt
2230                          * application.
2231                          */
2232                         if (saveq->iocb.ulpStatus &&
2233                              (pring->ringno == LPFC_ELS_RING) &&
2234                              (cmdiocbp->iocb.ulpCommand ==
2235                                 CMD_ELS_REQUEST64_CR))
2236                                 lpfc_send_els_failure_event(phba,
2237                                         cmdiocbp, saveq);
2238
2239                         /*
2240                          * Post all ELS completions to the worker thread.
2241                          * All other are passed to the completion callback.
2242                          */
2243                         if (pring->ringno == LPFC_ELS_RING) {
2244                                 if ((phba->sli_rev < LPFC_SLI_REV4) &&
2245                                     (cmdiocbp->iocb_flag &
2246                                                         LPFC_DRIVER_ABORTED)) {
2247                                         spin_lock_irqsave(&phba->hbalock,
2248                                                           iflag);
2249                                         cmdiocbp->iocb_flag &=
2250                                                 ~LPFC_DRIVER_ABORTED;
2251                                         spin_unlock_irqrestore(&phba->hbalock,
2252                                                                iflag);
2253                                         saveq->iocb.ulpStatus =
2254                                                 IOSTAT_LOCAL_REJECT;
2255                                         saveq->iocb.un.ulpWord[4] =
2256                                                 IOERR_SLI_ABORTED;
2257
2258                                         /* Firmware could still be in progress
2259                                          * of DMAing payload, so don't free data
2260                                          * buffer till after a hbeat.
2261                                          */
2262                                         spin_lock_irqsave(&phba->hbalock,
2263                                                           iflag);
2264                                         saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
2265                                         spin_unlock_irqrestore(&phba->hbalock,
2266                                                                iflag);
2267                                 }
2268                                 if (phba->sli_rev == LPFC_SLI_REV4) {
2269                                         if (saveq->iocb_flag &
2270                                             LPFC_EXCHANGE_BUSY) {
2271                                                 /* Set cmdiocb flag for the
2272                                                  * exchange busy so sgl (xri)
2273                                                  * will not be released until
2274                                                  * the abort xri is received
2275                                                  * from hba.
2276                                                  */
2277                                                 spin_lock_irqsave(
2278                                                         &phba->hbalock, iflag);
2279                                                 cmdiocbp->iocb_flag |=
2280                                                         LPFC_EXCHANGE_BUSY;
2281                                                 spin_unlock_irqrestore(
2282                                                         &phba->hbalock, iflag);
2283                                         }
2284                                         if (cmdiocbp->iocb_flag &
2285                                             LPFC_DRIVER_ABORTED) {
2286                                                 /*
2287                                                  * Clear LPFC_DRIVER_ABORTED
2288                                                  * bit in case it was driver
2289                                                  * initiated abort.
2290                                                  */
2291                                                 spin_lock_irqsave(
2292                                                         &phba->hbalock, iflag);
2293                                                 cmdiocbp->iocb_flag &=
2294                                                         ~LPFC_DRIVER_ABORTED;
2295                                                 spin_unlock_irqrestore(
2296                                                         &phba->hbalock, iflag);
2297                                                 cmdiocbp->iocb.ulpStatus =
2298                                                         IOSTAT_LOCAL_REJECT;
2299                                                 cmdiocbp->iocb.un.ulpWord[4] =
2300                                                         IOERR_ABORT_REQUESTED;
2301                                                 /*
2302                                                  * For SLI4, irsiocb contains
2303                                                  * NO_XRI in sli_xritag, it
2304                                                  * shall not affect releasing
2305                                                  * sgl (xri) process.
2306                                                  */
2307                                                 saveq->iocb.ulpStatus =
2308                                                         IOSTAT_LOCAL_REJECT;
2309                                                 saveq->iocb.un.ulpWord[4] =
2310                                                         IOERR_SLI_ABORTED;
2311                                                 spin_lock_irqsave(
2312                                                         &phba->hbalock, iflag);
2313                                                 saveq->iocb_flag |=
2314                                                         LPFC_DELAY_MEM_FREE;
2315                                                 spin_unlock_irqrestore(
2316                                                         &phba->hbalock, iflag);
2317                                         }
2318                                 }
2319                         }
2320                         (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
2321                 } else
2322                         lpfc_sli_release_iocbq(phba, cmdiocbp);
2323         } else {
2324                 /*
2325                  * Unknown initiating command based on the response iotag.
2326                  * This could be the case on the ELS ring because of
2327                  * lpfc_els_abort().
2328                  */
2329                 if (pring->ringno != LPFC_ELS_RING) {
2330                         /*
2331                          * Ring <ringno> handler: unexpected completion IoTag
2332                          * <IoTag>
2333                          */
2334                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
2335                                          "0322 Ring %d handler: "
2336                                          "unexpected completion IoTag x%x "
2337                                          "Data: x%x x%x x%x x%x\n",
2338                                          pring->ringno,
2339                                          saveq->iocb.ulpIoTag,
2340                                          saveq->iocb.ulpStatus,
2341                                          saveq->iocb.un.ulpWord[4],
2342                                          saveq->iocb.ulpCommand,
2343                                          saveq->iocb.ulpContext);
2344                 }
2345         }
2346
2347         return rc;
2348 }
2349
2350 /**
2351  * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
2352  * @phba: Pointer to HBA context object.
2353  * @pring: Pointer to driver SLI ring object.
2354  *
2355  * This function is called from the iocb ring event handlers when
2356  * put pointer is ahead of the get pointer for a ring. This function signal
2357  * an error attention condition to the worker thread and the worker
2358  * thread will transition the HBA to offline state.
2359  **/
2360 static void
2361 lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
2362 {
2363         struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
2364         /*
2365          * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
2366          * rsp ring <portRspMax>
2367          */
2368         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2369                         "0312 Ring %d handler: portRspPut %d "
2370                         "is bigger than rsp ring %d\n",
2371                         pring->ringno, le32_to_cpu(pgp->rspPutInx),
2372                         pring->numRiocb);
2373
2374         phba->link_state = LPFC_HBA_ERROR;
2375
2376         /*
2377          * All error attention handlers are posted to
2378          * worker thread
2379          */
2380         phba->work_ha |= HA_ERATT;
2381         phba->work_hs = HS_FFER3;
2382
2383         lpfc_worker_wake_up(phba);
2384
2385         return;
2386 }
2387
2388 /**
2389  * lpfc_poll_eratt - Error attention polling timer timeout handler
2390  * @ptr: Pointer to address of HBA context object.
2391  *
2392  * This function is invoked by the Error Attention polling timer when the
2393  * timer times out. It will check the SLI Error Attention register for
2394  * possible attention events. If so, it will post an Error Attention event
2395  * and wake up worker thread to process it. Otherwise, it will set up the
2396  * Error Attention polling timer for the next poll.
2397  **/
2398 void lpfc_poll_eratt(unsigned long ptr)
2399 {
2400         struct lpfc_hba *phba;
2401         uint32_t eratt = 0;
2402
2403         phba = (struct lpfc_hba *)ptr;
2404
2405         /* Check chip HA register for error event */
2406         eratt = lpfc_sli_check_eratt(phba);
2407
2408         if (eratt)
2409                 /* Tell the worker thread there is work to do */
2410                 lpfc_worker_wake_up(phba);
2411         else
2412                 /* Restart the timer for next eratt poll */
2413                 mod_timer(&phba->eratt_poll, jiffies +
2414                                         HZ * LPFC_ERATT_POLL_INTERVAL);
2415         return;
2416 }
2417
2418
2419 /**
2420  * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
2421  * @phba: Pointer to HBA context object.
2422  * @pring: Pointer to driver SLI ring object.
2423  * @mask: Host attention register mask for this ring.
2424  *
2425  * This function is called from the interrupt context when there is a ring
2426  * event for the fcp ring. The caller does not hold any lock.
2427  * The function processes each response iocb in the response ring until it
2428  * finds an iocb with LE bit set and chains all the iocbs upto the iocb with
2429  * LE bit set. The function will call the completion handler of the command iocb
2430  * if the response iocb indicates a completion for a command iocb or it is
2431  * an abort completion. The function will call lpfc_sli_process_unsol_iocb
2432  * function if this is an unsolicited iocb.
2433  * This routine presumes LPFC_FCP_RING handling and doesn't bother
2434  * to check it explicitly.
2435  */
2436 int
2437 lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
2438                                 struct lpfc_sli_ring *pring, uint32_t mask)
2439 {
2440         struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
2441         IOCB_t *irsp = NULL;
2442         IOCB_t *entry = NULL;
2443         struct lpfc_iocbq *cmdiocbq = NULL;
2444         struct lpfc_iocbq rspiocbq;
2445         uint32_t status;
2446         uint32_t portRspPut, portRspMax;
2447         int rc = 1;
2448         lpfc_iocb_type type;
2449         unsigned long iflag;
2450         uint32_t rsp_cmpl = 0;
2451
2452         spin_lock_irqsave(&phba->hbalock, iflag);
2453         pring->stats.iocb_event++;
2454
2455         /*
2456          * The next available response entry should never exceed the maximum
2457          * entries.  If it does, treat it as an adapter hardware error.
2458          */
2459         portRspMax = pring->numRiocb;
2460         portRspPut = le32_to_cpu(pgp->rspPutInx);
2461         if (unlikely(portRspPut >= portRspMax)) {
2462                 lpfc_sli_rsp_pointers_error(phba, pring);
2463                 spin_unlock_irqrestore(&phba->hbalock, iflag);
2464                 return 1;
2465         }
2466         if (phba->fcp_ring_in_use) {
2467                 spin_unlock_irqrestore(&phba->hbalock, iflag);
2468                 return 1;
2469         } else
2470                 phba->fcp_ring_in_use = 1;
2471
2472         rmb();
2473         while (pring->rspidx != portRspPut) {
2474                 /*
2475                  * Fetch an entry off the ring and copy it into a local data
2476                  * structure.  The copy involves a byte-swap since the
2477                  * network byte order and pci byte orders are different.
2478                  */
2479                 entry = lpfc_resp_iocb(phba, pring);
2480                 phba->last_completion_time = jiffies;
2481
2482                 if (++pring->rspidx >= portRspMax)
2483                         pring->rspidx = 0;
2484
2485                 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
2486                                       (uint32_t *) &rspiocbq.iocb,
2487                                       phba->iocb_rsp_size);
2488                 INIT_LIST_HEAD(&(rspiocbq.list));
2489                 irsp = &rspiocbq.iocb;
2490
2491                 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
2492                 pring->stats.iocb_rsp++;
2493                 rsp_cmpl++;
2494
2495                 if (unlikely(irsp->ulpStatus)) {
2496                         /*
2497                          * If resource errors reported from HBA, reduce
2498                          * queuedepths of the SCSI device.
2499                          */
2500                         if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
2501                                 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
2502                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
2503                                 phba->lpfc_rampdown_queue_depth(phba);
2504                                 spin_lock_irqsave(&phba->hbalock, iflag);
2505                         }
2506
2507                         /* Rsp ring <ringno> error: IOCB */
2508                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
2509                                         "0336 Rsp Ring %d error: IOCB Data: "
2510                                         "x%x x%x x%x x%x x%x x%x x%x x%x\n",
2511                                         pring->ringno,
2512                                         irsp->un.ulpWord[0],
2513                                         irsp->un.ulpWord[1],
2514                                         irsp->un.ulpWord[2],
2515                                         irsp->un.ulpWord[3],
2516                                         irsp->un.ulpWord[4],
2517                                         irsp->un.ulpWord[5],
2518                                         *(uint32_t *)&irsp->un1,
2519                                         *((uint32_t *)&irsp->un1 + 1));
2520                 }
2521
2522                 switch (type) {
2523                 case LPFC_ABORT_IOCB:
2524                 case LPFC_SOL_IOCB:
2525                         /*
2526                          * Idle exchange closed via ABTS from port.  No iocb
2527                          * resources need to be recovered.
2528                          */
2529                         if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
2530                                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
2531                                                 "0333 IOCB cmd 0x%x"
2532                                                 " processed. Skipping"
2533                                                 " completion\n",
2534                                                 irsp->ulpCommand);
2535                                 break;
2536                         }
2537
2538                         cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
2539                                                          &rspiocbq);
2540                         if (unlikely(!cmdiocbq))
2541                                 break;
2542                         if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
2543                                 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
2544                         if (cmdiocbq->iocb_cmpl) {
2545                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
2546                                 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
2547                                                       &rspiocbq);
2548                                 spin_lock_irqsave(&phba->hbalock, iflag);
2549                         }
2550                         break;
2551                 case LPFC_UNSOL_IOCB:
2552                         spin_unlock_irqrestore(&phba->hbalock, iflag);
2553                         lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
2554                         spin_lock_irqsave(&phba->hbalock, iflag);
2555                         break;
2556                 default:
2557                         if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
2558                                 char adaptermsg[LPFC_MAX_ADPTMSG];
2559                                 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
2560                                 memcpy(&adaptermsg[0], (uint8_t *) irsp,
2561                                        MAX_MSG_DATA);
2562                                 dev_warn(&((phba->pcidev)->dev),
2563                                          "lpfc%d: %s\n",
2564                                          phba->brd_no, adaptermsg);
2565                         } else {
2566                                 /* Unknown IOCB command */
2567                                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2568                                                 "0334 Unknown IOCB command "
2569                                                 "Data: x%x, x%x x%x x%x x%x\n",
2570                                                 type, irsp->ulpCommand,
2571                                                 irsp->ulpStatus,
2572                                                 irsp->ulpIoTag,
2573                                                 irsp->ulpContext);
2574                         }
2575                         break;
2576                 }
2577
2578                 /*
2579                  * The response IOCB has been processed.  Update the ring
2580                  * pointer in SLIM.  If the port response put pointer has not
2581                  * been updated, sync the pgp->rspPutInx and fetch the new port
2582                  * response put pointer.
2583                  */
2584                 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
2585
2586                 if (pring->rspidx == portRspPut)
2587                         portRspPut = le32_to_cpu(pgp->rspPutInx);
2588         }
2589
2590         if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
2591                 pring->stats.iocb_rsp_full++;
2592                 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
2593                 writel(status, phba->CAregaddr);
2594                 readl(phba->CAregaddr);
2595         }
2596         if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
2597                 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
2598                 pring->stats.iocb_cmd_empty++;
2599
2600                 /* Force update of the local copy of cmdGetInx */
2601                 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
2602                 lpfc_sli_resume_iocb(phba, pring);
2603
2604                 if ((pring->lpfc_sli_cmd_available))
2605                         (pring->lpfc_sli_cmd_available) (phba, pring);
2606
2607         }
2608
2609         phba->fcp_ring_in_use = 0;
2610         spin_unlock_irqrestore(&phba->hbalock, iflag);
2611         return rc;
2612 }
2613
2614 /**
2615  * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
2616  * @phba: Pointer to HBA context object.
2617  * @pring: Pointer to driver SLI ring object.
2618  * @rspiocbp: Pointer to driver response IOCB object.
2619  *
2620  * This function is called from the worker thread when there is a slow-path
2621  * response IOCB to process. This function chains all the response iocbs until
2622  * seeing the iocb with the LE bit set. The function will call
2623  * lpfc_sli_process_sol_iocb function if the response iocb indicates a
2624  * completion of a command iocb. The function will call the
2625  * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
2626  * The function frees the resources or calls the completion handler if this
2627  * iocb is an abort completion. The function returns NULL when the response
2628  * iocb has the LE bit set and all the chained iocbs are processed, otherwise
2629  * this function shall chain the iocb on to the iocb_continueq and return the
2630  * response iocb passed in.
2631  **/
2632 static struct lpfc_iocbq *
2633 lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2634                         struct lpfc_iocbq *rspiocbp)
2635 {
2636         struct lpfc_iocbq *saveq;
2637         struct lpfc_iocbq *cmdiocbp;
2638         struct lpfc_iocbq *next_iocb;
2639         IOCB_t *irsp = NULL;
2640         uint32_t free_saveq;
2641         uint8_t iocb_cmd_type;
2642         lpfc_iocb_type type;
2643         unsigned long iflag;
2644         int rc;
2645
2646         spin_lock_irqsave(&phba->hbalock, iflag);
2647         /* First add the response iocb to the countinueq list */
2648         list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
2649         pring->iocb_continueq_cnt++;
2650
2651         /* Now, determine whetehr the list is completed for processing */
2652         irsp = &rspiocbp->iocb;
2653         if (irsp->ulpLe) {
2654                 /*
2655                  * By default, the driver expects to free all resources
2656                  * associated with this iocb completion.
2657                  */
2658                 free_saveq = 1;
2659                 saveq = list_get_first(&pring->iocb_continueq,
2660                                        struct lpfc_iocbq, list);
2661                 irsp = &(saveq->iocb);
2662                 list_del_init(&pring->iocb_continueq);
2663                 pring->iocb_continueq_cnt = 0;
2664
2665                 pring->stats.iocb_rsp++;
2666
2667                 /*
2668                  * If resource errors reported from HBA, reduce
2669                  * queuedepths of the SCSI device.
2670                  */
2671                 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
2672                     (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
2673                         spin_unlock_irqrestore(&phba->hbalock, iflag);
2674                         phba->lpfc_rampdown_queue_depth(phba);
2675                         spin_lock_irqsave(&phba->hbalock, iflag);
2676                 }
2677
2678                 if (irsp->ulpStatus) {
2679                         /* Rsp ring <ringno> error: IOCB */
2680                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
2681                                         "0328 Rsp Ring %d error: "
2682                                         "IOCB Data: "
2683                                         "x%x x%x x%x x%x "
2684                                         "x%x x%x x%x x%x "
2685                                         "x%x x%x x%x x%x "
2686                                         "x%x x%x x%x x%x\n",
2687                                         pring->ringno,
2688                                         irsp->un.ulpWord[0],
2689                                         irsp->un.ulpWord[1],
2690                                         irsp->un.ulpWord[2],
2691                                         irsp->un.ulpWord[3],
2692                                         irsp->un.ulpWord[4],
2693                                         irsp->un.ulpWord[5],
2694                                         *(((uint32_t *) irsp) + 6),
2695                                         *(((uint32_t *) irsp) + 7),
2696                                         *(((uint32_t *) irsp) + 8),
2697                                         *(((uint32_t *) irsp) + 9),
2698                                         *(((uint32_t *) irsp) + 10),
2699                                         *(((uint32_t *) irsp) + 11),
2700                                         *(((uint32_t *) irsp) + 12),
2701                                         *(((uint32_t *) irsp) + 13),
2702                                         *(((uint32_t *) irsp) + 14),
2703                                         *(((uint32_t *) irsp) + 15));
2704                 }
2705
2706                 /*
2707                  * Fetch the IOCB command type and call the correct completion
2708                  * routine. Solicited and Unsolicited IOCBs on the ELS ring
2709                  * get freed back to the lpfc_iocb_list by the discovery
2710                  * kernel thread.
2711                  */
2712                 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
2713                 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
2714                 switch (type) {
2715                 case LPFC_SOL_IOCB:
2716                         spin_unlock_irqrestore(&phba->hbalock, iflag);
2717                         rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
2718                         spin_lock_irqsave(&phba->hbalock, iflag);
2719                         break;
2720
2721                 case LPFC_UNSOL_IOCB:
2722                         spin_unlock_irqrestore(&phba->hbalock, iflag);
2723                         rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
2724                         spin_lock_irqsave(&phba->hbalock, iflag);
2725                         if (!rc)
2726                                 free_saveq = 0;
2727                         break;
2728
2729                 case LPFC_ABORT_IOCB:
2730                         cmdiocbp = NULL;
2731                         if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
2732                                 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
2733                                                                  saveq);
2734                         if (cmdiocbp) {
2735                                 /* Call the specified completion routine */
2736                                 if (cmdiocbp->iocb_cmpl) {
2737                                         spin_unlock_irqrestore(&phba->hbalock,
2738                                                                iflag);
2739                                         (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
2740                                                               saveq);
2741                                         spin_lock_irqsave(&phba->hbalock,
2742                                                           iflag);
2743                                 } else
2744                                         __lpfc_sli_release_iocbq(phba,
2745                                                                  cmdiocbp);
2746                         }
2747                         break;
2748
2749                 case LPFC_UNKNOWN_IOCB:
2750                         if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
2751                                 char adaptermsg[LPFC_MAX_ADPTMSG];
2752                                 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
2753                                 memcpy(&adaptermsg[0], (uint8_t *)irsp,
2754                                        MAX_MSG_DATA);
2755                                 dev_warn(&((phba->pcidev)->dev),
2756                                          "lpfc%d: %s\n",
2757                                          phba->brd_no, adaptermsg);
2758                         } else {
2759                                 /* Unknown IOCB command */
2760                                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2761                                                 "0335 Unknown IOCB "
2762                                                 "command Data: x%x "
2763                                                 "x%x x%x x%x\n",
2764                                                 irsp->ulpCommand,
2765                                                 irsp->ulpStatus,
2766                                                 irsp->ulpIoTag,
2767                                                 irsp->ulpContext);
2768                         }
2769                         break;
2770                 }
2771
2772                 if (free_saveq) {
2773                         list_for_each_entry_safe(rspiocbp, next_iocb,
2774                                                  &saveq->list, list) {
2775                                 list_del(&rspiocbp->list);
2776                                 __lpfc_sli_release_iocbq(phba, rspiocbp);
2777                         }
2778                         __lpfc_sli_release_iocbq(phba, saveq);
2779                 }
2780                 rspiocbp = NULL;
2781         }
2782         spin_unlock_irqrestore(&phba->hbalock, iflag);
2783         return rspiocbp;
2784 }
2785
2786 /**
2787  * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
2788  * @phba: Pointer to HBA context object.
2789  * @pring: Pointer to driver SLI ring object.
2790  * @mask: Host attention register mask for this ring.
2791  *
2792  * This routine wraps the actual slow_ring event process routine from the
2793  * API jump table function pointer from the lpfc_hba struct.
2794  **/
2795 void
2796 lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
2797                                 struct lpfc_sli_ring *pring, uint32_t mask)
2798 {
2799         phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
2800 }
2801
2802 /**
2803  * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
2804  * @phba: Pointer to HBA context object.
2805  * @pring: Pointer to driver SLI ring object.
2806  * @mask: Host attention register mask for this ring.
2807  *
2808  * This function is called from the worker thread when there is a ring event
2809  * for non-fcp rings. The caller does not hold any lock. The function will
2810  * remove each response iocb in the response ring and calls the handle
2811  * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
2812  **/
2813 static void
2814 lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
2815                                    struct lpfc_sli_ring *pring, uint32_t mask)
2816 {
2817         struct lpfc_pgp *pgp;
2818         IOCB_t *entry;
2819         IOCB_t *irsp = NULL;
2820         struct lpfc_iocbq *rspiocbp = NULL;
2821         uint32_t portRspPut, portRspMax;
2822         unsigned long iflag;
2823         uint32_t status;
2824
2825         pgp = &phba->port_gp[pring->ringno];
2826         spin_lock_irqsave(&phba->hbalock, iflag);
2827         pring->stats.iocb_event++;
2828
2829         /*
2830          * The next available response entry should never exceed the maximum
2831          * entries.  If it does, treat it as an adapter hardware error.
2832          */
2833         portRspMax = pring->numRiocb;
2834         portRspPut = le32_to_cpu(pgp->rspPutInx);
2835         if (portRspPut >= portRspMax) {
2836                 /*
2837                  * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
2838                  * rsp ring <portRspMax>
2839                  */
2840                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2841                                 "0303 Ring %d handler: portRspPut %d "
2842                                 "is bigger than rsp ring %d\n",
2843                                 pring->ringno, portRspPut, portRspMax);
2844
2845                 phba->link_state = LPFC_HBA_ERROR;
2846                 spin_unlock_irqrestore(&phba->hbalock, iflag);
2847
2848                 phba->work_hs = HS_FFER3;
2849                 lpfc_handle_eratt(phba);
2850
2851                 return;
2852         }
2853
2854         rmb();
2855         while (pring->rspidx != portRspPut) {
2856                 /*
2857                  * Build a completion list and call the appropriate handler.
2858                  * The process is to get the next available response iocb, get
2859                  * a free iocb from the list, copy the response data into the
2860                  * free iocb, insert to the continuation list, and update the
2861                  * next response index to slim.  This process makes response
2862                  * iocb's in the ring available to DMA as fast as possible but
2863                  * pays a penalty for a copy operation.  Since the iocb is
2864                  * only 32 bytes, this penalty is considered small relative to
2865                  * the PCI reads for register values and a slim write.  When
2866                  * the ulpLe field is set, the entire Command has been
2867                  * received.
2868                  */
2869                 entry = lpfc_resp_iocb(phba, pring);
2870
2871                 phba->last_completion_time = jiffies;
2872                 rspiocbp = __lpfc_sli_get_iocbq(phba);
2873                 if (rspiocbp == NULL) {
2874                         printk(KERN_ERR "%s: out of buffers! Failing "
2875                                "completion.\n", __func__);
2876                         break;
2877                 }
2878
2879                 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
2880                                       phba->iocb_rsp_size);
2881                 irsp = &rspiocbp->iocb;
2882
2883                 if (++pring->rspidx >= portRspMax)
2884                         pring->rspidx = 0;
2885
2886                 if (pring->ringno == LPFC_ELS_RING) {
2887                         lpfc_debugfs_slow_ring_trc(phba,
2888                         "IOCB rsp ring:   wd4:x%08x wd6:x%08x wd7:x%08x",
2889                                 *(((uint32_t *) irsp) + 4),
2890                                 *(((uint32_t *) irsp) + 6),
2891                                 *(((uint32_t *) irsp) + 7));
2892                 }
2893
2894                 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
2895
2896                 spin_unlock_irqrestore(&phba->hbalock, iflag);
2897                 /* Handle the response IOCB */
2898                 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
2899                 spin_lock_irqsave(&phba->hbalock, iflag);
2900
2901                 /*
2902                  * If the port response put pointer has not been updated, sync
2903                  * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
2904                  * response put pointer.
2905                  */
2906                 if (pring->rspidx == portRspPut) {
2907                         portRspPut = le32_to_cpu(pgp->rspPutInx);
2908                 }
2909         } /* while (pring->rspidx != portRspPut) */
2910
2911         if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
2912                 /* At least one response entry has been freed */
2913                 pring->stats.iocb_rsp_full++;
2914                 /* SET RxRE_RSP in Chip Att register */
2915                 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
2916                 writel(status, phba->CAregaddr);
2917                 readl(phba->CAregaddr); /* flush */
2918         }
2919         if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
2920                 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
2921                 pring->stats.iocb_cmd_empty++;
2922
2923                 /* Force update of the local copy of cmdGetInx */
2924                 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
2925                 lpfc_sli_resume_iocb(phba, pring);
2926
2927                 if ((pring->lpfc_sli_cmd_available))
2928                         (pring->lpfc_sli_cmd_available) (phba, pring);
2929
2930         }
2931
2932         spin_unlock_irqrestore(&phba->hbalock, iflag);
2933         return;
2934 }
2935
2936 /**
2937  * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
2938  * @phba: Pointer to HBA context object.
2939  * @pring: Pointer to driver SLI ring object.
2940  * @mask: Host attention register mask for this ring.
2941  *
2942  * This function is called from the worker thread when there is a pending
2943  * ELS response iocb on the driver internal slow-path response iocb worker
2944  * queue. The caller does not hold any lock. The function will remove each
2945  * response iocb from the response worker queue and calls the handle
2946  * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
2947  **/
2948 static void
2949 lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
2950                                    struct lpfc_sli_ring *pring, uint32_t mask)
2951 {
2952         struct lpfc_iocbq *irspiocbq;
2953         struct hbq_dmabuf *dmabuf;
2954         struct lpfc_cq_event *cq_event;
2955         unsigned long iflag;
2956
2957         spin_lock_irqsave(&phba->hbalock, iflag);
2958         phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
2959         spin_unlock_irqrestore(&phba->hbalock, iflag);
2960         while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
2961                 /* Get the response iocb from the head of work queue */
2962                 spin_lock_irqsave(&phba->hbalock, iflag);
2963                 list_remove_head(&phba->sli4_hba.sp_queue_event,
2964                                  cq_event, struct lpfc_cq_event, list);
2965                 spin_unlock_irqrestore(&phba->hbalock, iflag);
2966
2967                 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
2968                 case CQE_CODE_COMPL_WQE:
2969                         irspiocbq = container_of(cq_event, struct lpfc_iocbq,
2970                                                  cq_event);
2971                         /* Translate ELS WCQE to response IOCBQ */
2972                         irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
2973                                                                    irspiocbq);
2974                         if (irspiocbq)
2975                                 lpfc_sli_sp_handle_rspiocb(phba, pring,
2976                                                            irspiocbq);
2977                         break;
2978                 case CQE_CODE_RECEIVE:
2979                         dmabuf = container_of(cq_event, struct hbq_dmabuf,
2980                                               cq_event);
2981                         lpfc_sli4_handle_received_buffer(phba, dmabuf);
2982                         break;
2983                 default:
2984                         break;
2985                 }
2986         }
2987 }
2988
2989 /**
2990  * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
2991  * @phba: Pointer to HBA context object.
2992  * @pring: Pointer to driver SLI ring object.
2993  *
2994  * This function aborts all iocbs in the given ring and frees all the iocb
2995  * objects in txq. This function issues an abort iocb for all the iocb commands
2996  * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
2997  * the return of this function. The caller is not required to hold any locks.
2998  **/
2999 void
3000 lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3001 {
3002         LIST_HEAD(completions);
3003         struct lpfc_iocbq *iocb, *next_iocb;
3004
3005         if (pring->ringno == LPFC_ELS_RING) {
3006                 lpfc_fabric_abort_hba(phba);
3007         }
3008
3009         /* Error everything on txq and txcmplq
3010          * First do the txq.
3011          */
3012         spin_lock_irq(&phba->hbalock);
3013         list_splice_init(&pring->txq, &completions);
3014         pring->txq_cnt = 0;
3015
3016         /* Next issue ABTS for everything on the txcmplq */
3017         list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3018                 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3019
3020         spin_unlock_irq(&phba->hbalock);
3021
3022         /* Cancel all the IOCBs from the completions list */
3023         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3024                               IOERR_SLI_ABORTED);
3025 }
3026
3027 /**
3028  * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
3029  * @phba: Pointer to HBA context object.
3030  *
3031  * This function flushes all iocbs in the fcp ring and frees all the iocb
3032  * objects in txq and txcmplq. This function will not issue abort iocbs
3033  * for all the iocb commands in txcmplq, they will just be returned with
3034  * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3035  * slot has been permanently disabled.
3036  **/
3037 void
3038 lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
3039 {
3040         LIST_HEAD(txq);
3041         LIST_HEAD(txcmplq);
3042         struct lpfc_sli *psli = &phba->sli;
3043         struct lpfc_sli_ring  *pring;
3044
3045         /* Currently, only one fcp ring */
3046         pring = &psli->ring[psli->fcp_ring];
3047
3048         spin_lock_irq(&phba->hbalock);
3049         /* Retrieve everything on txq */
3050         list_splice_init(&pring->txq, &txq);
3051         pring->txq_cnt = 0;
3052
3053         /* Retrieve everything on the txcmplq */
3054         list_splice_init(&pring->txcmplq, &txcmplq);
3055         pring->txcmplq_cnt = 0;
3056         spin_unlock_irq(&phba->hbalock);
3057
3058         /* Flush the txq */
3059         lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
3060                               IOERR_SLI_DOWN);
3061
3062         /* Flush the txcmpq */
3063         lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
3064                               IOERR_SLI_DOWN);
3065 }
3066
3067 /**
3068  * lpfc_sli_brdready_s3 - Check for sli3 host ready status
3069  * @phba: Pointer to HBA context object.
3070  * @mask: Bit mask to be checked.
3071  *
3072  * This function reads the host status register and compares
3073  * with the provided bit mask to check if HBA completed
3074  * the restart. This function will wait in a loop for the
3075  * HBA to complete restart. If the HBA does not restart within
3076  * 15 iterations, the function will reset the HBA again. The
3077  * function returns 1 when HBA fail to restart otherwise returns
3078  * zero.
3079  **/
3080 static int
3081 lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
3082 {
3083         uint32_t status;
3084         int i = 0;
3085         int retval = 0;
3086
3087         /* Read the HBA Host Status Register */
3088         status = readl(phba->HSregaddr);
3089
3090         /*
3091          * Check status register every 100ms for 5 retries, then every
3092          * 500ms for 5, then every 2.5 sec for 5, then reset board and
3093          * every 2.5 sec for 4.
3094          * Break our of the loop if errors occurred during init.
3095          */
3096         while (((status & mask) != mask) &&
3097                !(status & HS_FFERM) &&
3098                i++ < 20) {
3099
3100                 if (i <= 5)
3101                         msleep(10);
3102                 else if (i <= 10)
3103                         msleep(500);
3104                 else
3105                         msleep(2500);
3106
3107                 if (i == 15) {
3108                                 /* Do post */
3109                         phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3110                         lpfc_sli_brdrestart(phba);
3111                 }
3112                 /* Read the HBA Host Status Register */
3113                 status = readl(phba->HSregaddr);
3114         }
3115
3116         /* Check to see if any errors occurred during init */
3117         if ((status & HS_FFERM) || (i >= 20)) {
3118                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3119                                 "2751 Adapter failed to restart, "
3120                                 "status reg x%x, FW Data: A8 x%x AC x%x\n",
3121                                 status,
3122                                 readl(phba->MBslimaddr + 0xa8),
3123                                 readl(phba->MBslimaddr + 0xac));
3124                 phba->link_state = LPFC_HBA_ERROR;
3125                 retval = 1;
3126         }
3127
3128         return retval;
3129 }
3130
3131 /**
3132  * lpfc_sli_brdready_s4 - Check for sli4 host ready status
3133  * @phba: Pointer to HBA context object.
3134  * @mask: Bit mask to be checked.
3135  *
3136  * This function checks the host status register to check if HBA is
3137  * ready. This function will wait in a loop for the HBA to be ready
3138  * If the HBA is not ready , the function will will reset the HBA PCI
3139  * function again. The function returns 1 when HBA fail to be ready
3140  * otherwise returns zero.
3141  **/
3142 static int
3143 lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
3144 {
3145         uint32_t status;
3146         int retval = 0;
3147
3148         /* Read the HBA Host Status Register */
3149         status = lpfc_sli4_post_status_check(phba);
3150
3151         if (status) {
3152                 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3153                 lpfc_sli_brdrestart(phba);
3154                 status = lpfc_sli4_post_status_check(phba);
3155         }
3156
3157         /* Check to see if any errors occurred during init */
3158         if (status) {
3159                 phba->link_state = LPFC_HBA_ERROR;
3160                 retval = 1;
3161         } else
3162                 phba->sli4_hba.intr_enable = 0;
3163
3164         return retval;
3165 }
3166
3167 /**
3168  * lpfc_sli_brdready - Wrapper func for checking the hba readyness
3169  * @phba: Pointer to HBA context object.
3170  * @mask: Bit mask to be checked.
3171  *
3172  * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
3173  * from the API jump table function pointer from the lpfc_hba struct.
3174  **/
3175 int
3176 lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
3177 {
3178         return phba->lpfc_sli_brdready(phba, mask);
3179 }
3180
3181 #define BARRIER_TEST_PATTERN (0xdeadbeef)
3182
3183 /**
3184  * lpfc_reset_barrier - Make HBA ready for HBA reset
3185  * @phba: Pointer to HBA context object.
3186  *
3187  * This function is called before resetting an HBA. This
3188  * function requests HBA to quiesce DMAs before a reset.
3189  **/
3190 void lpfc_reset_barrier(struct lpfc_hba *phba)
3191 {
3192         uint32_t __iomem *resp_buf;
3193         uint32_t __iomem *mbox_buf;
3194         volatile uint32_t mbox;
3195         uint32_t hc_copy;
3196         int  i;
3197         uint8_t hdrtype;
3198
3199         pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
3200         if (hdrtype != 0x80 ||
3201             (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
3202              FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
3203                 return;
3204
3205         /*
3206          * Tell the other part of the chip to suspend temporarily all
3207          * its DMA activity.
3208          */
3209         resp_buf = phba->MBslimaddr;
3210
3211         /* Disable the error attention */
3212         hc_copy = readl(phba->HCregaddr);
3213         writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
3214         readl(phba->HCregaddr); /* flush */
3215         phba->link_flag |= LS_IGNORE_ERATT;
3216
3217         if (readl(phba->HAregaddr) & HA_ERATT) {
3218                 /* Clear Chip error bit */
3219                 writel(HA_ERATT, phba->HAregaddr);
3220                 phba->pport->stopped = 1;
3221         }
3222
3223         mbox = 0;
3224         ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
3225         ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
3226
3227         writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
3228         mbox_buf = phba->MBslimaddr;
3229         writel(mbox, mbox_buf);
3230
3231         for (i = 0;
3232              readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN) && i < 50; i++)
3233                 mdelay(1);
3234
3235         if (readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN)) {
3236                 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
3237                     phba->pport->stopped)
3238                         goto restore_hc;
3239                 else
3240                         goto clear_errat;
3241         }
3242
3243         ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
3244         for (i = 0; readl(resp_buf) != mbox &&  i < 500; i++)
3245                 mdelay(1);
3246
3247 clear_errat:
3248
3249         while (!(readl(phba->HAregaddr) & HA_ERATT) && ++i < 500)
3250                 mdelay(1);
3251
3252         if (readl(phba->HAregaddr) & HA_ERATT) {
3253                 writel(HA_ERATT, phba->HAregaddr);
3254                 phba->pport->stopped = 1;
3255         }
3256
3257 restore_hc:
3258         phba->link_flag &= ~LS_IGNORE_ERATT;
3259         writel(hc_copy, phba->HCregaddr);
3260         readl(phba->HCregaddr); /* flush */
3261 }
3262
3263 /**
3264  * lpfc_sli_brdkill - Issue a kill_board mailbox command
3265  * @phba: Pointer to HBA context object.
3266  *
3267  * This function issues a kill_board mailbox command and waits for
3268  * the error attention interrupt. This function is called for stopping
3269  * the firmware processing. The caller is not required to hold any
3270  * locks. This function calls lpfc_hba_down_post function to free
3271  * any pending commands after the kill. The function will return 1 when it
3272  * fails to kill the board else will return 0.
3273  **/
3274 int
3275 lpfc_sli_brdkill(struct lpfc_hba *phba)
3276 {
3277         struct lpfc_sli *psli;
3278         LPFC_MBOXQ_t *pmb;
3279         uint32_t status;
3280         uint32_t ha_copy;
3281         int retval;
3282         int i = 0;
3283
3284         psli = &phba->sli;
3285
3286         /* Kill HBA */
3287         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3288                         "0329 Kill HBA Data: x%x x%x\n",
3289                         phba->pport->port_state, psli->sli_flag);
3290
3291         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3292         if (!pmb)
3293                 return 1;
3294
3295         /* Disable the error attention */
3296         spin_lock_irq(&phba->hbalock);
3297         status = readl(phba->HCregaddr);
3298         status &= ~HC_ERINT_ENA;
3299         writel(status, phba->HCregaddr);
3300         readl(phba->HCregaddr); /* flush */
3301         phba->link_flag |= LS_IGNORE_ERATT;
3302         spin_unlock_irq(&phba->hbalock);
3303
3304         lpfc_kill_board(phba, pmb);
3305         pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3306         retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3307
3308         if (retval != MBX_SUCCESS) {
3309                 if (retval != MBX_BUSY)
3310                         mempool_free(pmb, phba->mbox_mem_pool);
3311                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3312                                 "2752 KILL_BOARD command failed retval %d\n",
3313                                 retval);
3314                 spin_lock_irq(&phba->hbalock);
3315                 phba->link_flag &= ~LS_IGNORE_ERATT;
3316                 spin_unlock_irq(&phba->hbalock);
3317                 return 1;
3318         }
3319
3320         spin_lock_irq(&phba->hbalock);
3321         psli->sli_flag &= ~LPFC_SLI_ACTIVE;
3322         spin_unlock_irq(&phba->hbalock);
3323
3324         mempool_free(pmb, phba->mbox_mem_pool);
3325
3326         /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
3327          * attention every 100ms for 3 seconds. If we don't get ERATT after
3328          * 3 seconds we still set HBA_ERROR state because the status of the
3329          * board is now undefined.
3330          */
3331         ha_copy = readl(phba->HAregaddr);
3332
3333         while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
3334                 mdelay(100);
3335                 ha_copy = readl(phba->HAregaddr);
3336         }
3337
3338         del_timer_sync(&psli->mbox_tmo);
3339         if (ha_copy & HA_ERATT) {
3340                 writel(HA_ERATT, phba->HAregaddr);
3341                 phba->pport->stopped = 1;
3342         }
3343         spin_lock_irq(&phba->hbalock);
3344         psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
3345         psli->mbox_active = NULL;
3346         phba->link_flag &= ~LS_IGNORE_ERATT;
3347         spin_unlock_irq(&phba->hbalock);
3348
3349         lpfc_hba_down_post(phba);
3350         phba->link_state = LPFC_HBA_ERROR;
3351
3352         return ha_copy & HA_ERATT ? 0 : 1;
3353 }
3354
3355 /**
3356  * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
3357  * @phba: Pointer to HBA context object.
3358  *
3359  * This function resets the HBA by writing HC_INITFF to the control
3360  * register. After the HBA resets, this function resets all the iocb ring
3361  * indices. This function disables PCI layer parity checking during
3362  * the reset.
3363  * This function returns 0 always.
3364  * The caller is not required to hold any locks.
3365  **/
3366 int
3367 lpfc_sli_brdreset(struct lpfc_hba *phba)
3368 {
3369         struct lpfc_sli *psli;
3370         struct lpfc_sli_ring *pring;
3371         uint16_t cfg_value;
3372         int i;
3373
3374         psli = &phba->sli;
3375
3376         /* Reset HBA */
3377         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3378                         "0325 Reset HBA Data: x%x x%x\n",
3379                         phba->pport->port_state, psli->sli_flag);
3380
3381         /* perform board reset */
3382         phba->fc_eventTag = 0;
3383         phba->link_events = 0;
3384         phba->pport->fc_myDID = 0;
3385         phba->pport->fc_prevDID = 0;
3386
3387         /* Turn off parity checking and serr during the physical reset */
3388         pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3389         pci_write_config_word(phba->pcidev, PCI_COMMAND,
3390                               (cfg_value &
3391                                ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3392
3393         psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
3394
3395         /* Now toggle INITFF bit in the Host Control Register */
3396         writel(HC_INITFF, phba->HCregaddr);
3397         mdelay(1);
3398         readl(phba->HCregaddr); /* flush */
3399         writel(0, phba->HCregaddr);
3400         readl(phba->HCregaddr); /* flush */
3401
3402         /* Restore PCI cmd register */
3403         pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
3404
3405         /* Initialize relevant SLI info */
3406         for (i = 0; i < psli->num_rings; i++) {
3407                 pring = &psli->ring[i];
3408                 pring->flag = 0;
3409                 pring->rspidx = 0;
3410                 pring->next_cmdidx  = 0;
3411                 pring->local_getidx = 0;
3412                 pring->cmdidx = 0;
3413                 pring->missbufcnt = 0;
3414         }
3415
3416         phba->link_state = LPFC_WARM_START;
3417         return 0;
3418 }
3419
3420 /**
3421  * lpfc_sli4_brdreset - Reset a sli-4 HBA
3422  * @phba: Pointer to HBA context object.
3423  *
3424  * This function resets a SLI4 HBA. This function disables PCI layer parity
3425  * checking during resets the device. The caller is not required to hold
3426  * any locks.
3427  *
3428  * This function returns 0 always.
3429  **/
3430 int
3431 lpfc_sli4_brdreset(struct lpfc_hba *phba)
3432 {
3433         struct lpfc_sli *psli = &phba->sli;
3434         uint16_t cfg_value;
3435         uint8_t qindx;
3436
3437         /* Reset HBA */
3438         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3439                         "0295 Reset HBA Data: x%x x%x\n",
3440                         phba->pport->port_state, psli->sli_flag);
3441
3442         /* perform board reset */
3443         phba->fc_eventTag = 0;
3444         phba->link_events = 0;
3445         phba->pport->fc_myDID = 0;
3446         phba->pport->fc_prevDID = 0;
3447
3448         /* Turn off parity checking and serr during the physical reset */
3449         pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3450         pci_write_config_word(phba->pcidev, PCI_COMMAND,
3451                               (cfg_value &
3452                               ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3453
3454         spin_lock_irq(&phba->hbalock);
3455         psli->sli_flag &= ~(LPFC_PROCESS_LA);
3456         phba->fcf.fcf_flag = 0;
3457         /* Clean up the child queue list for the CQs */
3458         list_del_init(&phba->sli4_hba.mbx_wq->list);
3459         list_del_init(&phba->sli4_hba.els_wq->list);
3460         list_del_init(&phba->sli4_hba.hdr_rq->list);
3461         list_del_init(&phba->sli4_hba.dat_rq->list);
3462         list_del_init(&phba->sli4_hba.mbx_cq->list);
3463         list_del_init(&phba->sli4_hba.els_cq->list);
3464         for (qindx = 0; qindx < phba->cfg_fcp_wq_count; qindx++)
3465                 list_del_init(&phba->sli4_hba.fcp_wq[qindx]->list);
3466         for (qindx = 0; qindx < phba->cfg_fcp_eq_count; qindx++)
3467                 list_del_init(&phba->sli4_hba.fcp_cq[qindx]->list);
3468         spin_unlock_irq(&phba->hbalock);
3469
3470         /* Now physically reset the device */
3471         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3472                         "0389 Performing PCI function reset!\n");
3473         /* Perform FCoE PCI function reset */
3474         lpfc_pci_function_reset(phba);
3475
3476         return 0;
3477 }
3478
3479 /**
3480  * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
3481  * @phba: Pointer to HBA context object.
3482  *
3483  * This function is called in the SLI initialization code path to
3484  * restart the HBA. The caller is not required to hold any lock.
3485  * This function writes MBX_RESTART mailbox command to the SLIM and
3486  * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
3487  * function to free any pending commands. The function enables
3488  * POST only during the first initialization. The function returns zero.
3489  * The function does not guarantee completion of MBX_RESTART mailbox
3490  * command before the return of this function.
3491  **/
3492 static int
3493 lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
3494 {
3495         MAILBOX_t *mb;
3496         struct lpfc_sli *psli;
3497         volatile uint32_t word0;
3498         void __iomem *to_slim;
3499         uint32_t hba_aer_enabled;
3500
3501         spin_lock_irq(&phba->hbalock);
3502
3503         /* Take PCIe device Advanced Error Reporting (AER) state */
3504         hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
3505
3506         psli = &phba->sli;
3507
3508         /* Restart HBA */
3509         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3510                         "0337 Restart HBA Data: x%x x%x\n",
3511                         phba->pport->port_state, psli->sli_flag);
3512
3513         word0 = 0;
3514         mb = (MAILBOX_t *) &word0;
3515         mb->mbxCommand = MBX_RESTART;
3516         mb->mbxHc = 1;
3517
3518         lpfc_reset_barrier(phba);
3519
3520         to_slim = phba->MBslimaddr;
3521         writel(*(uint32_t *) mb, to_slim);
3522         readl(to_slim); /* flush */
3523
3524         /* Only skip post after fc_ffinit is completed */
3525         if (phba->pport->port_state)
3526                 word0 = 1;      /* This is really setting up word1 */
3527         else
3528                 word0 = 0;      /* This is really setting up word1 */
3529         to_slim = phba->MBslimaddr + sizeof (uint32_t);
3530         writel(*(uint32_t *) mb, to_slim);
3531         readl(to_slim); /* flush */
3532
3533         lpfc_sli_brdreset(phba);
3534         phba->pport->stopped = 0;
3535         phba->link_state = LPFC_INIT_START;
3536         phba->hba_flag = 0;
3537         spin_unlock_irq(&phba->hbalock);
3538
3539         memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
3540         psli->stats_start = get_seconds();
3541
3542         /* Give the INITFF and Post time to settle. */
3543         mdelay(100);
3544
3545         /* Reset HBA AER if it was enabled, note hba_flag was reset above */
3546         if (hba_aer_enabled)
3547                 pci_disable_pcie_error_reporting(phba->pcidev);
3548
3549         lpfc_hba_down_post(phba);
3550
3551         return 0;
3552 }
3553
3554 /**
3555  * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
3556  * @phba: Pointer to HBA context object.
3557  *
3558  * This function is called in the SLI initialization code path to restart
3559  * a SLI4 HBA. The caller is not required to hold any lock.
3560  * At the end of the function, it calls lpfc_hba_down_post function to
3561  * free any pending commands.
3562  **/
3563 static int
3564 lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
3565 {
3566         struct lpfc_sli *psli = &phba->sli;
3567
3568
3569         /* Restart HBA */
3570         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3571                         "0296 Restart HBA Data: x%x x%x\n",
3572                         phba->pport->port_state, psli->sli_flag);
3573
3574         lpfc_sli4_brdreset(phba);
3575
3576         spin_lock_irq(&phba->hbalock);
3577         phba->pport->stopped = 0;
3578         phba->link_state = LPFC_INIT_START;
3579         phba->hba_flag = 0;
3580         spin_unlock_irq(&phba->hbalock);
3581
3582         memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
3583         psli->stats_start = get_seconds();
3584
3585         lpfc_hba_down_post(phba);
3586
3587         return 0;
3588 }
3589
3590 /**
3591  * lpfc_sli_brdrestart - Wrapper func for restarting hba
3592  * @phba: Pointer to HBA context object.
3593  *
3594  * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
3595  * API jump table function pointer from the lpfc_hba struct.
3596 **/
3597 int
3598 lpfc_sli_brdrestart(struct lpfc_hba *phba)
3599 {
3600         return phba->lpfc_sli_brdrestart(phba);
3601 }
3602
3603 /**
3604  * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
3605  * @phba: Pointer to HBA context object.
3606  *
3607  * This function is called after a HBA restart to wait for successful
3608  * restart of the HBA. Successful restart of the HBA is indicated by
3609  * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
3610  * iteration, the function will restart the HBA again. The function returns
3611  * zero if HBA successfully restarted else returns negative error code.
3612  **/
3613 static int
3614 lpfc_sli_chipset_init(struct lpfc_hba *phba)
3615 {
3616         uint32_t status, i = 0;
3617
3618         /* Read the HBA Host Status Register */
3619         status = readl(phba->HSregaddr);
3620
3621         /* Check status register to see what current state is */
3622         i = 0;
3623         while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
3624
3625                 /* Check every 100ms for 5 retries, then every 500ms for 5, then
3626                  * every 2.5 sec for 5, then reset board and every 2.5 sec for
3627                  * 4.
3628                  */
3629                 if (i++ >= 20) {
3630                         /* Adapter failed to init, timeout, status reg
3631                            <status> */
3632                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3633                                         "0436 Adapter failed to init, "
3634                                         "timeout, status reg x%x, "
3635                                         "FW Data: A8 x%x AC x%x\n", status,
3636                                         readl(phba->MBslimaddr + 0xa8),
3637                                         readl(phba->MBslimaddr + 0xac));
3638                         phba->link_state = LPFC_HBA_ERROR;
3639                         return -ETIMEDOUT;
3640                 }
3641
3642                 /* Check to see if any errors occurred during init */
3643                 if (status & HS_FFERM) {
3644                         /* ERROR: During chipset initialization */
3645                         /* Adapter failed to init, chipset, status reg
3646                            <status> */
3647                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3648                                         "0437 Adapter failed to init, "
3649                                         "chipset, status reg x%x, "
3650                                         "FW Data: A8 x%x AC x%x\n", status,
3651                                         readl(phba->MBslimaddr + 0xa8),
3652                                         readl(phba->MBslimaddr + 0xac));
3653                         phba->link_state = LPFC_HBA_ERROR;
3654                         return -EIO;
3655                 }
3656
3657                 if (i <= 5) {
3658                         msleep(10);
3659                 } else if (i <= 10) {
3660                         msleep(500);
3661                 } else {
3662                         msleep(2500);
3663                 }
3664
3665                 if (i == 15) {
3666                                 /* Do post */
3667                         phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3668                         lpfc_sli_brdrestart(phba);
3669                 }
3670                 /* Read the HBA Host Status Register */
3671                 status = readl(phba->HSregaddr);
3672         }
3673
3674         /* Check to see if any errors occurred during init */
3675         if (status & HS_FFERM) {
3676                 /* ERROR: During chipset initialization */
3677                 /* Adapter failed to init, chipset, status reg <status> */
3678                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3679                                 "0438 Adapter failed to init, chipset, "
3680                                 "status reg x%x, "
3681                                 "FW Data: A8 x%x AC x%x\n", status,
3682                                 readl(phba->MBslimaddr + 0xa8),
3683                                 readl(phba->MBslimaddr + 0xac));
3684                 phba->link_state = LPFC_HBA_ERROR;
3685                 return -EIO;
3686         }
3687
3688         /* Clear all interrupt enable conditions */
3689         writel(0, phba->HCregaddr);
3690         readl(phba->HCregaddr); /* flush */
3691
3692         /* setup host attn register */
3693         writel(0xffffffff, phba->HAregaddr);
3694         readl(phba->HAregaddr); /* flush */
3695         return 0;
3696 }
3697
3698 /**
3699  * lpfc_sli_hbq_count - Get the number of HBQs to be configured
3700  *
3701  * This function calculates and returns the number of HBQs required to be
3702  * configured.
3703  **/
3704 int
3705 lpfc_sli_hbq_count(void)
3706 {
3707         return ARRAY_SIZE(lpfc_hbq_defs);
3708 }
3709
3710 /**
3711  * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
3712  *
3713  * This function adds the number of hbq entries in every HBQ to get
3714  * the total number of hbq entries required for the HBA and returns
3715  * the total count.
3716  **/
3717 static int
3718 lpfc_sli_hbq_entry_count(void)
3719 {
3720         int  hbq_count = lpfc_sli_hbq_count();
3721         int  count = 0;
3722         int  i;
3723
3724         for (i = 0; i < hbq_count; ++i)
3725                 count += lpfc_hbq_defs[i]->entry_count;
3726         return count;
3727 }
3728
3729 /**
3730  * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
3731  *
3732  * This function calculates amount of memory required for all hbq entries
3733  * to be configured and returns the total memory required.
3734  **/
3735 int
3736 lpfc_sli_hbq_size(void)
3737 {
3738         return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
3739 }
3740
3741 /**
3742  * lpfc_sli_hbq_setup - configure and initialize HBQs
3743  * @phba: Pointer to HBA context object.
3744  *
3745  * This function is called during the SLI initialization to configure
3746  * all the HBQs and post buffers to the HBQ. The caller is not
3747  * required to hold any locks. This function will return zero if successful
3748  * else it will return negative error code.
3749  **/
3750 static int
3751 lpfc_sli_hbq_setup(struct lpfc_hba *phba)
3752 {
3753         int  hbq_count = lpfc_sli_hbq_count();
3754         LPFC_MBOXQ_t *pmb;
3755         MAILBOX_t *pmbox;
3756         uint32_t hbqno;
3757         uint32_t hbq_entry_index;
3758
3759                                 /* Get a Mailbox buffer to setup mailbox
3760                                  * commands for HBA initialization
3761                                  */
3762         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3763
3764         if (!pmb)
3765                 return -ENOMEM;
3766
3767         pmbox = &pmb->u.mb;
3768
3769         /* Initialize the struct lpfc_sli_hbq structure for each hbq */
3770         phba->link_state = LPFC_INIT_MBX_CMDS;
3771         phba->hbq_in_use = 1;
3772
3773         hbq_entry_index = 0;
3774         for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
3775                 phba->hbqs[hbqno].next_hbqPutIdx = 0;
3776                 phba->hbqs[hbqno].hbqPutIdx      = 0;
3777                 phba->hbqs[hbqno].local_hbqGetIdx   = 0;
3778                 phba->hbqs[hbqno].entry_count =
3779                         lpfc_hbq_defs[hbqno]->entry_count;
3780                 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
3781                         hbq_entry_index, pmb);
3782                 hbq_entry_index += phba->hbqs[hbqno].entry_count;
3783
3784                 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
3785                         /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
3786                            mbxStatus <status>, ring <num> */
3787
3788                         lpfc_printf_log(phba, KERN_ERR,
3789                                         LOG_SLI | LOG_VPORT,
3790                                         "1805 Adapter failed to init. "
3791                                         "Data: x%x x%x x%x\n",
3792                                         pmbox->mbxCommand,
3793                                         pmbox->mbxStatus, hbqno);
3794
3795                         phba->link_state = LPFC_HBA_ERROR;
3796                         mempool_free(pmb, phba->mbox_mem_pool);
3797                         return ENXIO;
3798                 }
3799         }
3800         phba->hbq_count = hbq_count;
3801
3802         mempool_free(pmb, phba->mbox_mem_pool);
3803
3804         /* Initially populate or replenish the HBQs */
3805         for (hbqno = 0; hbqno < hbq_count; ++hbqno)
3806                 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
3807         return 0;
3808 }
3809
3810 /**
3811  * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
3812  * @phba: Pointer to HBA context object.
3813  *
3814  * This function is called during the SLI initialization to configure
3815  * all the HBQs and post buffers to the HBQ. The caller is not
3816  * required to hold any locks. This function will return zero if successful
3817  * else it will return negative error code.
3818  **/
3819 static int
3820 lpfc_sli4_rb_setup(struct lpfc_hba *phba)
3821 {
3822         phba->hbq_in_use = 1;
3823         phba->hbqs[0].entry_count = lpfc_hbq_defs[0]->entry_count;
3824         phba->hbq_count = 1;
3825         /* Initially populate or replenish the HBQs */
3826         lpfc_sli_hbqbuf_init_hbqs(phba, 0);
3827         return 0;
3828 }
3829
3830 /**
3831  * lpfc_sli_config_port - Issue config port mailbox command
3832  * @phba: Pointer to HBA context object.
3833  * @sli_mode: sli mode - 2/3
3834  *
3835  * This function is called by the sli intialization code path
3836  * to issue config_port mailbox command. This function restarts the
3837  * HBA firmware and issues a config_port mailbox command to configure
3838  * the SLI interface in the sli mode specified by sli_mode
3839  * variable. The caller is not required to hold any locks.
3840  * The function returns 0 if successful, else returns negative error
3841  * code.
3842  **/
3843 int
3844 lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
3845 {
3846         LPFC_MBOXQ_t *pmb;
3847         uint32_t resetcount = 0, rc = 0, done = 0;
3848
3849         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3850         if (!pmb) {
3851                 phba->link_state = LPFC_HBA_ERROR;
3852                 return -ENOMEM;
3853         }
3854
3855         phba->sli_rev = sli_mode;
3856         while (resetcount < 2 && !done) {
3857                 spin_lock_irq(&phba->hbalock);
3858                 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
3859                 spin_unlock_irq(&phba->hbalock);
3860                 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3861                 lpfc_sli_brdrestart(phba);
3862                 rc = lpfc_sli_chipset_init(phba);
3863                 if (rc)
3864                         break;
3865
3866                 spin_lock_irq(&phba->hbalock);
3867                 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
3868                 spin_unlock_irq(&phba->hbalock);
3869                 resetcount++;
3870
3871                 /* Call pre CONFIG_PORT mailbox command initialization.  A
3872                  * value of 0 means the call was successful.  Any other
3873                  * nonzero value is a failure, but if ERESTART is returned,
3874                  * the driver may reset the HBA and try again.
3875                  */
3876                 rc = lpfc_config_port_prep(phba);
3877                 if (rc == -ERESTART) {
3878                         phba->link_state = LPFC_LINK_UNKNOWN;
3879                         continue;
3880                 } else if (rc)
3881                         break;
3882                 phba->link_state = LPFC_INIT_MBX_CMDS;
3883                 lpfc_config_port(phba, pmb);
3884                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
3885                 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
3886                                         LPFC_SLI3_HBQ_ENABLED |
3887                                         LPFC_SLI3_CRP_ENABLED |
3888                                         LPFC_SLI3_INB_ENABLED |
3889                                         LPFC_SLI3_BG_ENABLED);
3890                 if (rc != MBX_SUCCESS) {
3891                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3892                                 "0442 Adapter failed to init, mbxCmd x%x "
3893                                 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
3894                                 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
3895                         spin_lock_irq(&phba->hbalock);
3896                         phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
3897                         spin_unlock_irq(&phba->hbalock);
3898                         rc = -ENXIO;
3899                 } else {
3900                         /* Allow asynchronous mailbox command to go through */
3901                         spin_lock_irq(&phba->hbalock);
3902                         phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
3903                         spin_unlock_irq(&phba->hbalock);
3904                         done = 1;
3905                 }
3906         }
3907         if (!done) {
3908                 rc = -EINVAL;
3909                 goto do_prep_failed;
3910         }
3911         if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
3912                 if (!pmb->u.mb.un.varCfgPort.cMA) {
3913                         rc = -ENXIO;
3914                         goto do_prep_failed;
3915                 }
3916                 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
3917                         phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
3918                         phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
3919                         phba->max_vports = (phba->max_vpi > phba->max_vports) ?
3920                                 phba->max_vpi : phba->max_vports;
3921
3922                 } else
3923                         phba->max_vpi = 0;
3924                 if (pmb->u.mb.un.varCfgPort.gdss)
3925                         phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
3926                 if (pmb->u.mb.un.varCfgPort.gerbm)
3927                         phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
3928                 if (pmb->u.mb.un.varCfgPort.gcrp)
3929                         phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
3930                 if (pmb->u.mb.un.varCfgPort.ginb) {
3931                         phba->sli3_options |= LPFC_SLI3_INB_ENABLED;
3932                         phba->hbq_get = phba->mbox->us.s3_inb_pgp.hbq_get;
3933                         phba->port_gp = phba->mbox->us.s3_inb_pgp.port;
3934                         phba->inb_ha_copy = &phba->mbox->us.s3_inb_pgp.ha_copy;
3935                         phba->inb_counter = &phba->mbox->us.s3_inb_pgp.counter;
3936                         phba->inb_last_counter =
3937                                         phba->mbox->us.s3_inb_pgp.counter;
3938                 } else {
3939                         phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
3940                         phba->port_gp = phba->mbox->us.s3_pgp.port;
3941                         phba->inb_ha_copy = NULL;
3942                         phba->inb_counter = NULL;
3943                 }
3944
3945                 if (phba->cfg_enable_bg) {
3946                         if (pmb->u.mb.un.varCfgPort.gbg)
3947                                 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
3948                         else
3949                                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3950                                                 "0443 Adapter did not grant "
3951                                                 "BlockGuard\n");
3952                 }
3953         } else {
3954                 phba->hbq_get = NULL;
3955                 phba->port_gp = phba->mbox->us.s2.port;
3956                 phba->inb_ha_copy = NULL;
3957                 phba->inb_counter = NULL;
3958                 phba->max_vpi = 0;
3959         }
3960 do_prep_failed:
3961         mempool_free(pmb, phba->mbox_mem_pool);
3962         return rc;
3963 }
3964
3965
3966 /**
3967  * lpfc_sli_hba_setup - SLI intialization function
3968  * @phba: Pointer to HBA context object.
3969  *
3970  * This function is the main SLI intialization function. This function
3971  * is called by the HBA intialization code, HBA reset code and HBA
3972  * error attention handler code. Caller is not required to hold any
3973  * locks. This function issues config_port mailbox command to configure
3974  * the SLI, setup iocb rings and HBQ rings. In the end the function
3975  * calls the config_port_post function to issue init_link mailbox
3976  * command and to start the discovery. The function will return zero
3977  * if successful, else it will return negative error code.
3978  **/
3979 int
3980 lpfc_sli_hba_setup(struct lpfc_hba *phba)
3981 {
3982         uint32_t rc;
3983         int  mode = 3;
3984
3985         switch (lpfc_sli_mode) {
3986         case 2:
3987                 if (phba->cfg_enable_npiv) {
3988                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
3989                                 "1824 NPIV enabled: Override lpfc_sli_mode "
3990                                 "parameter (%d) to auto (0).\n",
3991                                 lpfc_sli_mode);
3992                         break;
3993                 }
3994                 mode = 2;
3995                 break;
3996         case 0:
3997         case 3:
3998                 break;
3999         default:
4000                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
4001                                 "1819 Unrecognized lpfc_sli_mode "
4002                                 "parameter: %d.\n", lpfc_sli_mode);
4003
4004                 break;
4005         }
4006
4007         rc = lpfc_sli_config_port(phba, mode);
4008
4009         if (rc && lpfc_sli_mode == 3)
4010                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
4011                                 "1820 Unable to select SLI-3.  "
4012                                 "Not supported by adapter.\n");
4013         if (rc && mode != 2)
4014                 rc = lpfc_sli_config_port(phba, 2);
4015         if (rc)
4016                 goto lpfc_sli_hba_setup_error;
4017
4018         /* Enable PCIe device Advanced Error Reporting (AER) if configured */
4019         if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
4020                 rc = pci_enable_pcie_error_reporting(phba->pcidev);
4021                 if (!rc) {
4022                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4023                                         "2709 This device supports "
4024                                         "Advanced Error Reporting (AER)\n");
4025                         spin_lock_irq(&phba->hbalock);
4026                         phba->hba_flag |= HBA_AER_ENABLED;
4027                         spin_unlock_irq(&phba->hbalock);
4028                 } else {
4029                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4030                                         "2708 This device does not support "
4031                                         "Advanced Error Reporting (AER)\n");
4032                         phba->cfg_aer_support = 0;
4033                 }
4034         }
4035
4036         if (phba->sli_rev == 3) {
4037                 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
4038                 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
4039         } else {
4040                 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
4041                 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
4042                 phba->sli3_options = 0;
4043         }
4044
4045         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4046                         "0444 Firmware in SLI %x mode. Max_vpi %d\n",
4047                         phba->sli_rev, phba->max_vpi);
4048         rc = lpfc_sli_ring_map(phba);
4049
4050         if (rc)
4051                 goto lpfc_sli_hba_setup_error;
4052
4053         /* Init HBQs */
4054         if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
4055                 rc = lpfc_sli_hbq_setup(phba);
4056                 if (rc)
4057                         goto lpfc_sli_hba_setup_error;
4058         }
4059         spin_lock_irq(&phba->hbalock);
4060         phba->sli.sli_flag |= LPFC_PROCESS_LA;
4061         spin_unlock_irq(&phba->hbalock);
4062
4063         rc = lpfc_config_port_post(phba);
4064         if (rc)
4065                 goto lpfc_sli_hba_setup_error;
4066
4067         return rc;
4068
4069 lpfc_sli_hba_setup_error:
4070         phba->link_state = LPFC_HBA_ERROR;
4071         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4072                         "0445 Firmware initialization failed\n");
4073         return rc;
4074 }
4075
4076 /**
4077  * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
4078  * @phba: Pointer to HBA context object.
4079  * @mboxq: mailbox pointer.
4080  * This function issue a dump mailbox command to read config region
4081  * 23 and parse the records in the region and populate driver
4082  * data structure.
4083  **/
4084 static int
4085 lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba,
4086                 LPFC_MBOXQ_t *mboxq)
4087 {
4088         struct lpfc_dmabuf *mp;
4089         struct lpfc_mqe *mqe;
4090         uint32_t data_length;
4091         int rc;
4092
4093         /* Program the default value of vlan_id and fc_map */
4094         phba->valid_vlan = 0;
4095         phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4096         phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4097         phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
4098
4099         mqe = &mboxq->u.mqe;
4100         if (lpfc_dump_fcoe_param(phba, mboxq))
4101                 return -ENOMEM;
4102
4103         mp = (struct lpfc_dmabuf *) mboxq->context1;
4104         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4105
4106         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4107                         "(%d):2571 Mailbox cmd x%x Status x%x "
4108                         "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4109                         "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4110                         "CQ: x%x x%x x%x x%x\n",
4111                         mboxq->vport ? mboxq->vport->vpi : 0,
4112                         bf_get(lpfc_mqe_command, mqe),
4113                         bf_get(lpfc_mqe_status, mqe),
4114                         mqe->un.mb_words[0], mqe->un.mb_words[1],
4115                         mqe->un.mb_words[2], mqe->un.mb_words[3],
4116                         mqe->un.mb_words[4], mqe->un.mb_words[5],
4117                         mqe->un.mb_words[6], mqe->un.mb_words[7],
4118                         mqe->un.mb_words[8], mqe->un.mb_words[9],
4119                         mqe->un.mb_words[10], mqe->un.mb_words[11],
4120                         mqe->un.mb_words[12], mqe->un.mb_words[13],
4121                         mqe->un.mb_words[14], mqe->un.mb_words[15],
4122                         mqe->un.mb_words[16], mqe->un.mb_words[50],
4123                         mboxq->mcqe.word0,
4124                         mboxq->mcqe.mcqe_tag0,  mboxq->mcqe.mcqe_tag1,
4125                         mboxq->mcqe.trailer);
4126
4127         if (rc) {
4128                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4129                 kfree(mp);
4130                 return -EIO;
4131         }
4132         data_length = mqe->un.mb_words[5];
4133         if (data_length > DMP_RGN23_SIZE) {
4134                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4135                 kfree(mp);
4136                 return -EIO;
4137         }
4138
4139         lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
4140         lpfc_mbuf_free(phba, mp->virt, mp->phys);
4141         kfree(mp);
4142         return 0;
4143 }
4144
4145 /**
4146  * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
4147  * @phba: pointer to lpfc hba data structure.
4148  * @mboxq: pointer to the LPFC_MBOXQ_t structure.
4149  * @vpd: pointer to the memory to hold resulting port vpd data.
4150  * @vpd_size: On input, the number of bytes allocated to @vpd.
4151  *            On output, the number of data bytes in @vpd.
4152  *
4153  * This routine executes a READ_REV SLI4 mailbox command.  In
4154  * addition, this routine gets the port vpd data.
4155  *
4156  * Return codes
4157  *      0 - successful
4158  *      ENOMEM - could not allocated memory.
4159  **/
4160 static int
4161 lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
4162                     uint8_t *vpd, uint32_t *vpd_size)
4163 {
4164         int rc = 0;
4165         uint32_t dma_size;
4166         struct lpfc_dmabuf *dmabuf;
4167         struct lpfc_mqe *mqe;
4168
4169         dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4170         if (!dmabuf)
4171                 return -ENOMEM;
4172
4173         /*
4174          * Get a DMA buffer for the vpd data resulting from the READ_REV
4175          * mailbox command.
4176          */
4177         dma_size = *vpd_size;
4178         dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
4179                                           dma_size,
4180                                           &dmabuf->phys,
4181                                           GFP_KERNEL);
4182         if (!dmabuf->virt) {
4183                 kfree(dmabuf);
4184                 return -ENOMEM;
4185         }
4186         memset(dmabuf->virt, 0, dma_size);
4187
4188         /*
4189          * The SLI4 implementation of READ_REV conflicts at word1,
4190          * bits 31:16 and SLI4 adds vpd functionality not present
4191          * in SLI3.  This code corrects the conflicts.
4192          */
4193         lpfc_read_rev(phba, mboxq);
4194         mqe = &mboxq->u.mqe;
4195         mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
4196         mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
4197         mqe->un.read_rev.word1 &= 0x0000FFFF;
4198         bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
4199         bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
4200
4201         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4202         if (rc) {
4203                 dma_free_coherent(&phba->pcidev->dev, dma_size,
4204                                   dmabuf->virt, dmabuf->phys);
4205                 kfree(dmabuf);
4206                 return -EIO;
4207         }
4208
4209         /*
4210          * The available vpd length cannot be bigger than the
4211          * DMA buffer passed to the port.  Catch the less than
4212          * case and update the caller's size.
4213          */
4214         if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
4215                 *vpd_size = mqe->un.read_rev.avail_vpd_len;
4216
4217         lpfc_sli_pcimem_bcopy(dmabuf->virt, vpd, *vpd_size);
4218         dma_free_coherent(&phba->pcidev->dev, dma_size,
4219                           dmabuf->virt, dmabuf->phys);
4220         kfree(dmabuf);
4221         return 0;
4222 }
4223
4224 /**
4225  * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
4226  * @phba: pointer to lpfc hba data structure.
4227  *
4228  * This routine is called to explicitly arm the SLI4 device's completion and
4229  * event queues
4230  **/
4231 static void
4232 lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
4233 {
4234         uint8_t fcp_eqidx;
4235
4236         lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
4237         lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
4238         for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++)
4239                 lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[fcp_eqidx],
4240                                      LPFC_QUEUE_REARM);
4241         lpfc_sli4_eq_release(phba->sli4_hba.sp_eq, LPFC_QUEUE_REARM);
4242         for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++)
4243                 lpfc_sli4_eq_release(phba->sli4_hba.fp_eq[fcp_eqidx],
4244                                      LPFC_QUEUE_REARM);
4245 }
4246
4247 /**
4248  * lpfc_sli4_hba_setup - SLI4 device intialization PCI function
4249  * @phba: Pointer to HBA context object.
4250  *
4251  * This function is the main SLI4 device intialization PCI function. This
4252  * function is called by the HBA intialization code, HBA reset code and
4253  * HBA error attention handler code. Caller is not required to hold any
4254  * locks.
4255  **/
4256 int
4257 lpfc_sli4_hba_setup(struct lpfc_hba *phba)
4258 {
4259         int rc;
4260         LPFC_MBOXQ_t *mboxq;
4261         struct lpfc_mqe *mqe;
4262         uint8_t *vpd;
4263         uint32_t vpd_size;
4264         uint32_t ftr_rsp = 0;
4265         struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
4266         struct lpfc_vport *vport = phba->pport;
4267         struct lpfc_dmabuf *mp;
4268
4269         /* Perform a PCI function reset to start from clean */
4270         rc = lpfc_pci_function_reset(phba);
4271         if (unlikely(rc))
4272                 return -ENODEV;
4273
4274         /* Check the HBA Host Status Register for readyness */
4275         rc = lpfc_sli4_post_status_check(phba);
4276         if (unlikely(rc))
4277                 return -ENODEV;
4278         else {
4279                 spin_lock_irq(&phba->hbalock);
4280                 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
4281                 spin_unlock_irq(&phba->hbalock);
4282         }
4283
4284         /*
4285          * Allocate a single mailbox container for initializing the
4286          * port.
4287          */
4288         mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4289         if (!mboxq)
4290                 return -ENOMEM;
4291
4292         /*
4293          * Continue initialization with default values even if driver failed
4294          * to read FCoE param config regions
4295          */
4296         if (lpfc_sli4_read_fcoe_params(phba, mboxq))
4297                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
4298                         "2570 Failed to read FCoE parameters\n");
4299
4300         /* Issue READ_REV to collect vpd and FW information. */
4301         vpd_size = SLI4_PAGE_SIZE;
4302         vpd = kzalloc(vpd_size, GFP_KERNEL);
4303         if (!vpd) {
4304                 rc = -ENOMEM;
4305                 goto out_free_mbox;
4306         }
4307
4308         rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
4309         if (unlikely(rc))
4310                 goto out_free_vpd;
4311
4312         mqe = &mboxq->u.mqe;
4313         phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
4314         if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev))
4315                 phba->hba_flag |= HBA_FCOE_SUPPORT;
4316
4317         if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
4318                 LPFC_DCBX_CEE_MODE)
4319                 phba->hba_flag |= HBA_FIP_SUPPORT;
4320         else
4321                 phba->hba_flag &= ~HBA_FIP_SUPPORT;
4322
4323         if (phba->sli_rev != LPFC_SLI_REV4 ||
4324             !(phba->hba_flag & HBA_FCOE_SUPPORT)) {
4325                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4326                         "0376 READ_REV Error. SLI Level %d "
4327                         "FCoE enabled %d\n",
4328                         phba->sli_rev, phba->hba_flag & HBA_FCOE_SUPPORT);
4329                 rc = -EIO;
4330                 goto out_free_vpd;
4331         }
4332         /*
4333          * Evaluate the read rev and vpd data. Populate the driver
4334          * state with the results. If this routine fails, the failure
4335          * is not fatal as the driver will use generic values.
4336          */
4337         rc = lpfc_parse_vpd(phba, vpd, vpd_size);
4338         if (unlikely(!rc)) {
4339                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4340                                 "0377 Error %d parsing vpd. "
4341                                 "Using defaults.\n", rc);
4342                 rc = 0;
4343         }
4344
4345         /* Save information as VPD data */
4346         phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
4347         phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
4348         phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
4349         phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
4350                                          &mqe->un.read_rev);
4351         phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
4352                                        &mqe->un.read_rev);
4353         phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
4354                                             &mqe->un.read_rev);
4355         phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
4356                                            &mqe->un.read_rev);
4357         phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
4358         memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
4359         phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
4360         memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
4361         phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
4362         memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
4363         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4364                         "(%d):0380 READ_REV Status x%x "
4365                         "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
4366                         mboxq->vport ? mboxq->vport->vpi : 0,
4367                         bf_get(lpfc_mqe_status, mqe),
4368                         phba->vpd.rev.opFwName,
4369                         phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
4370                         phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
4371
4372         /*
4373          * Discover the port's supported feature set and match it against the
4374          * hosts requests.
4375          */
4376         lpfc_request_features(phba, mboxq);
4377         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4378         if (unlikely(rc)) {
4379                 rc = -EIO;
4380                 goto out_free_vpd;
4381         }
4382
4383         /*
4384          * The port must support FCP initiator mode as this is the
4385          * only mode running in the host.
4386          */
4387         if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
4388                 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
4389                                 "0378 No support for fcpi mode.\n");
4390                 ftr_rsp++;
4391         }
4392
4393         /*
4394          * If the port cannot support the host's requested features
4395          * then turn off the global config parameters to disable the
4396          * feature in the driver.  This is not a fatal error.
4397          */
4398         if ((phba->cfg_enable_bg) &&
4399             !(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
4400                 ftr_rsp++;
4401
4402         if (phba->max_vpi && phba->cfg_enable_npiv &&
4403             !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
4404                 ftr_rsp++;
4405
4406         if (ftr_rsp) {
4407                 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
4408                                 "0379 Feature Mismatch Data: x%08x %08x "
4409                                 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
4410                                 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
4411                                 phba->cfg_enable_npiv, phba->max_vpi);
4412                 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
4413                         phba->cfg_enable_bg = 0;
4414                 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
4415                         phba->cfg_enable_npiv = 0;
4416         }
4417
4418         /* These SLI3 features are assumed in SLI4 */
4419         spin_lock_irq(&phba->hbalock);
4420         phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
4421         spin_unlock_irq(&phba->hbalock);
4422
4423         /* Read the port's service parameters. */
4424         rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
4425         if (rc) {
4426                 phba->link_state = LPFC_HBA_ERROR;
4427                 rc = -ENOMEM;
4428                 goto out_free_vpd;
4429         }
4430
4431         mboxq->vport = vport;
4432         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4433         mp = (struct lpfc_dmabuf *) mboxq->context1;
4434         if (rc == MBX_SUCCESS) {
4435                 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
4436                 rc = 0;
4437         }
4438
4439         /*
4440          * This memory was allocated by the lpfc_read_sparam routine. Release
4441          * it to the mbuf pool.
4442          */
4443         lpfc_mbuf_free(phba, mp->virt, mp->phys);
4444         kfree(mp);
4445         mboxq->context1 = NULL;
4446         if (unlikely(rc)) {
4447                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4448                                 "0382 READ_SPARAM command failed "
4449                                 "status %d, mbxStatus x%x\n",
4450                                 rc, bf_get(lpfc_mqe_status, mqe));
4451                 phba->link_state = LPFC_HBA_ERROR;
4452                 rc = -EIO;
4453                 goto out_free_vpd;
4454         }
4455
4456         if (phba->cfg_soft_wwnn)
4457                 u64_to_wwn(phba->cfg_soft_wwnn,
4458                            vport->fc_sparam.nodeName.u.wwn);
4459         if (phba->cfg_soft_wwpn)
4460                 u64_to_wwn(phba->cfg_soft_wwpn,
4461                            vport->fc_sparam.portName.u.wwn);
4462         memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
4463                sizeof(struct lpfc_name));
4464         memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
4465                sizeof(struct lpfc_name));
4466
4467         /* Update the fc_host data structures with new wwn. */
4468         fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
4469         fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
4470
4471         /* Register SGL pool to the device using non-embedded mailbox command */
4472         rc = lpfc_sli4_post_sgl_list(phba);
4473         if (unlikely(rc)) {
4474                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4475                                 "0582 Error %d during sgl post operation\n",
4476                                         rc);
4477                 rc = -ENODEV;
4478                 goto out_free_vpd;
4479         }
4480
4481         /* Register SCSI SGL pool to the device */
4482         rc = lpfc_sli4_repost_scsi_sgl_list(phba);
4483         if (unlikely(rc)) {
4484                 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
4485                                 "0383 Error %d during scsi sgl post "
4486                                 "operation\n", rc);
4487                 /* Some Scsi buffers were moved to the abort scsi list */
4488                 /* A pci function reset will repost them */
4489                 rc = -ENODEV;
4490                 goto out_free_vpd;
4491         }
4492
4493         /* Post the rpi header region to the device. */
4494         rc = lpfc_sli4_post_all_rpi_hdrs(phba);
4495         if (unlikely(rc)) {
4496                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4497                                 "0393 Error %d during rpi post operation\n",
4498                                 rc);
4499                 rc = -ENODEV;
4500                 goto out_free_vpd;
4501         }
4502
4503         /* Set up all the queues to the device */
4504         rc = lpfc_sli4_queue_setup(phba);
4505         if (unlikely(rc)) {
4506                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4507                                 "0381 Error %d during queue setup.\n ", rc);
4508                 goto out_stop_timers;
4509         }
4510
4511         /* Arm the CQs and then EQs on device */
4512         lpfc_sli4_arm_cqeq_intr(phba);
4513
4514         /* Indicate device interrupt mode */
4515         phba->sli4_hba.intr_enable = 1;
4516
4517         /* Allow asynchronous mailbox command to go through */
4518         spin_lock_irq(&phba->hbalock);
4519         phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4520         spin_unlock_irq(&phba->hbalock);
4521
4522         /* Post receive buffers to the device */
4523         lpfc_sli4_rb_setup(phba);
4524
4525         /* Reset HBA FCF states after HBA reset */
4526         phba->fcf.fcf_flag = 0;
4527         phba->fcf.current_rec.flag = 0;
4528
4529         /* Start the ELS watchdog timer */
4530         mod_timer(&vport->els_tmofunc,
4531                   jiffies + HZ * (phba->fc_ratov * 2));
4532
4533         /* Start heart beat timer */
4534         mod_timer(&phba->hb_tmofunc,
4535                   jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
4536         phba->hb_outstanding = 0;
4537         phba->last_completion_time = jiffies;
4538
4539         /* Start error attention (ERATT) polling timer */
4540         mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
4541
4542         /*
4543          * The port is ready, set the host's link state to LINK_DOWN
4544          * in preparation for link interrupts.
4545          */
4546         lpfc_init_link(phba, mboxq, phba->cfg_topology, phba->cfg_link_speed);
4547         mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4548         lpfc_set_loopback_flag(phba);
4549         /* Change driver state to LPFC_LINK_DOWN right before init link */
4550         spin_lock_irq(&phba->hbalock);
4551         phba->link_state = LPFC_LINK_DOWN;
4552         spin_unlock_irq(&phba->hbalock);
4553         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
4554         if (unlikely(rc != MBX_NOT_FINISHED)) {
4555                 kfree(vpd);
4556                 return 0;
4557         } else
4558                 rc = -EIO;
4559
4560         /* Unset all the queues set up in this routine when error out */
4561         if (rc)
4562                 lpfc_sli4_queue_unset(phba);
4563
4564 out_stop_timers:
4565         if (rc)
4566                 lpfc_stop_hba_timers(phba);
4567 out_free_vpd:
4568         kfree(vpd);
4569 out_free_mbox:
4570         mempool_free(mboxq, phba->mbox_mem_pool);
4571         return rc;
4572 }
4573
4574 /**
4575  * lpfc_mbox_timeout - Timeout call back function for mbox timer
4576  * @ptr: context object - pointer to hba structure.
4577  *
4578  * This is the callback function for mailbox timer. The mailbox
4579  * timer is armed when a new mailbox command is issued and the timer
4580  * is deleted when the mailbox complete. The function is called by
4581  * the kernel timer code when a mailbox does not complete within
4582  * expected time. This function wakes up the worker thread to
4583  * process the mailbox timeout and returns. All the processing is
4584  * done by the worker thread function lpfc_mbox_timeout_handler.
4585  **/
4586 void
4587 lpfc_mbox_timeout(unsigned long ptr)
4588 {
4589         struct lpfc_hba  *phba = (struct lpfc_hba *) ptr;
4590         unsigned long iflag;
4591         uint32_t tmo_posted;
4592
4593         spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
4594         tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
4595         if (!tmo_posted)
4596                 phba->pport->work_port_events |= WORKER_MBOX_TMO;
4597         spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
4598
4599         if (!tmo_posted)
4600                 lpfc_worker_wake_up(phba);
4601         return;
4602 }
4603
4604
4605 /**
4606  * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
4607  * @phba: Pointer to HBA context object.
4608  *
4609  * This function is called from worker thread when a mailbox command times out.
4610  * The caller is not required to hold any locks. This function will reset the
4611  * HBA and recover all the pending commands.
4612  **/
4613 void
4614 lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
4615 {
4616         LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
4617         MAILBOX_t *mb = &pmbox->u.mb;
4618         struct lpfc_sli *psli = &phba->sli;
4619         struct lpfc_sli_ring *pring;
4620
4621         /* Check the pmbox pointer first.  There is a race condition
4622          * between the mbox timeout handler getting executed in the
4623          * worklist and the mailbox actually completing. When this
4624          * race condition occurs, the mbox_active will be NULL.
4625          */
4626         spin_lock_irq(&phba->hbalock);
4627         if (pmbox == NULL) {
4628                 lpfc_printf_log(phba, KERN_WARNING,
4629                                 LOG_MBOX | LOG_SLI,
4630                                 "0353 Active Mailbox cleared - mailbox timeout "
4631                                 "exiting\n");
4632                 spin_unlock_irq(&phba->hbalock);
4633                 return;
4634         }
4635
4636         /* Mbox cmd <mbxCommand> timeout */
4637         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4638                         "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
4639                         mb->mbxCommand,
4640                         phba->pport->port_state,
4641                         phba->sli.sli_flag,
4642                         phba->sli.mbox_active);
4643         spin_unlock_irq(&phba->hbalock);
4644
4645         /* Setting state unknown so lpfc_sli_abort_iocb_ring
4646          * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
4647          * it to fail all oustanding SCSI IO.
4648          */
4649         spin_lock_irq(&phba->pport->work_port_lock);
4650         phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
4651         spin_unlock_irq(&phba->pport->work_port_lock);
4652         spin_lock_irq(&phba->hbalock);
4653         phba->link_state = LPFC_LINK_UNKNOWN;
4654         psli->sli_flag &= ~LPFC_SLI_ACTIVE;
4655         spin_unlock_irq(&phba->hbalock);
4656
4657         pring = &psli->ring[psli->fcp_ring];
4658         lpfc_sli_abort_iocb_ring(phba, pring);
4659
4660         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4661                         "0345 Resetting board due to mailbox timeout\n");
4662
4663         /* Reset the HBA device */
4664         lpfc_reset_hba(phba);
4665 }
4666
4667 /**
4668  * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
4669  * @phba: Pointer to HBA context object.
4670  * @pmbox: Pointer to mailbox object.
4671  * @flag: Flag indicating how the mailbox need to be processed.
4672  *
4673  * This function is called by discovery code and HBA management code
4674  * to submit a mailbox command to firmware with SLI-3 interface spec. This
4675  * function gets the hbalock to protect the data structures.
4676  * The mailbox command can be submitted in polling mode, in which case
4677  * this function will wait in a polling loop for the completion of the
4678  * mailbox.
4679  * If the mailbox is submitted in no_wait mode (not polling) the
4680  * function will submit the command and returns immediately without waiting
4681  * for the mailbox completion. The no_wait is supported only when HBA
4682  * is in SLI2/SLI3 mode - interrupts are enabled.
4683  * The SLI interface allows only one mailbox pending at a time. If the
4684  * mailbox is issued in polling mode and there is already a mailbox
4685  * pending, then the function will return an error. If the mailbox is issued
4686  * in NO_WAIT mode and there is a mailbox pending already, the function
4687  * will return MBX_BUSY after queuing the mailbox into mailbox queue.
4688  * The sli layer owns the mailbox object until the completion of mailbox
4689  * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
4690  * return codes the caller owns the mailbox command after the return of
4691  * the function.
4692  **/
4693 static int
4694 lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
4695                        uint32_t flag)
4696 {
4697         MAILBOX_t *mb;
4698         struct lpfc_sli *psli = &phba->sli;
4699         uint32_t status, evtctr;
4700         uint32_t ha_copy;
4701         int i;
4702         unsigned long timeout;
4703         unsigned long drvr_flag = 0;
4704         uint32_t word0, ldata;
4705         void __iomem *to_slim;
4706         int processing_queue = 0;
4707
4708         spin_lock_irqsave(&phba->hbalock, drvr_flag);
4709         if (!pmbox) {
4710                 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
4711                 /* processing mbox queue from intr_handler */
4712                 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
4713                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4714                         return MBX_SUCCESS;
4715                 }
4716                 processing_queue = 1;
4717                 pmbox = lpfc_mbox_get(phba);
4718                 if (!pmbox) {
4719                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4720                         return MBX_SUCCESS;
4721                 }
4722         }
4723
4724         if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
4725                 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
4726                 if(!pmbox->vport) {
4727                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4728                         lpfc_printf_log(phba, KERN_ERR,
4729                                         LOG_MBOX | LOG_VPORT,
4730                                         "1806 Mbox x%x failed. No vport\n",
4731                                         pmbox->u.mb.mbxCommand);
4732                         dump_stack();
4733                         goto out_not_finished;
4734                 }
4735         }
4736
4737         /* If the PCI channel is in offline state, do not post mbox. */
4738         if (unlikely(pci_channel_offline(phba->pcidev))) {
4739                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4740                 goto out_not_finished;
4741         }
4742
4743         /* If HBA has a deferred error attention, fail the iocb. */
4744         if (unlikely(phba->hba_flag & DEFER_ERATT)) {
4745                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4746                 goto out_not_finished;
4747         }
4748
4749         psli = &phba->sli;
4750
4751         mb = &pmbox->u.mb;
4752         status = MBX_SUCCESS;
4753
4754         if (phba->link_state == LPFC_HBA_ERROR) {
4755                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4756
4757                 /* Mbox command <mbxCommand> cannot issue */
4758                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4759                                 "(%d):0311 Mailbox command x%x cannot "
4760                                 "issue Data: x%x x%x\n",
4761                                 pmbox->vport ? pmbox->vport->vpi : 0,
4762                                 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
4763                 goto out_not_finished;
4764         }
4765
4766         if (mb->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT &&
4767             !(readl(phba->HCregaddr) & HC_MBINT_ENA)) {
4768                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4769                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4770                                 "(%d):2528 Mailbox command x%x cannot "
4771                                 "issue Data: x%x x%x\n",
4772                                 pmbox->vport ? pmbox->vport->vpi : 0,
4773                                 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
4774                 goto out_not_finished;
4775         }
4776
4777         if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
4778                 /* Polling for a mbox command when another one is already active
4779                  * is not allowed in SLI. Also, the driver must have established
4780                  * SLI2 mode to queue and process multiple mbox commands.
4781                  */
4782
4783                 if (flag & MBX_POLL) {
4784                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4785
4786                         /* Mbox command <mbxCommand> cannot issue */
4787                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4788                                         "(%d):2529 Mailbox command x%x "
4789                                         "cannot issue Data: x%x x%x\n",
4790                                         pmbox->vport ? pmbox->vport->vpi : 0,
4791                                         pmbox->u.mb.mbxCommand,
4792                                         psli->sli_flag, flag);
4793                         goto out_not_finished;
4794                 }
4795
4796                 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
4797                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4798                         /* Mbox command <mbxCommand> cannot issue */
4799                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4800                                         "(%d):2530 Mailbox command x%x "
4801                                         "cannot issue Data: x%x x%x\n",
4802                                         pmbox->vport ? pmbox->vport->vpi : 0,
4803                                         pmbox->u.mb.mbxCommand,
4804                                         psli->sli_flag, flag);
4805                         goto out_not_finished;
4806                 }
4807
4808                 /* Another mailbox command is still being processed, queue this
4809                  * command to be processed later.
4810                  */
4811                 lpfc_mbox_put(phba, pmbox);
4812
4813                 /* Mbox cmd issue - BUSY */
4814                 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4815                                 "(%d):0308 Mbox cmd issue - BUSY Data: "
4816                                 "x%x x%x x%x x%x\n",
4817                                 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
4818                                 mb->mbxCommand, phba->pport->port_state,
4819                                 psli->sli_flag, flag);
4820
4821                 psli->slistat.mbox_busy++;
4822                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4823
4824                 if (pmbox->vport) {
4825                         lpfc_debugfs_disc_trc(pmbox->vport,
4826                                 LPFC_DISC_TRC_MBOX_VPORT,
4827                                 "MBOX Bsy vport:  cmd:x%x mb:x%x x%x",
4828                                 (uint32_t)mb->mbxCommand,
4829                                 mb->un.varWords[0], mb->un.varWords[1]);
4830                 }
4831                 else {
4832                         lpfc_debugfs_disc_trc(phba->pport,
4833                                 LPFC_DISC_TRC_MBOX,
4834                                 "MBOX Bsy:        cmd:x%x mb:x%x x%x",
4835                                 (uint32_t)mb->mbxCommand,
4836                                 mb->un.varWords[0], mb->un.varWords[1]);
4837                 }
4838
4839                 return MBX_BUSY;
4840         }
4841
4842         psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
4843
4844         /* If we are not polling, we MUST be in SLI2 mode */
4845         if (flag != MBX_POLL) {
4846                 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
4847                     (mb->mbxCommand != MBX_KILL_BOARD)) {
4848                         psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
4849                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4850                         /* Mbox command <mbxCommand> cannot issue */
4851                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4852                                         "(%d):2531 Mailbox command x%x "
4853                                         "cannot issue Data: x%x x%x\n",
4854                                         pmbox->vport ? pmbox->vport->vpi : 0,
4855                                         pmbox->u.mb.mbxCommand,
4856                                         psli->sli_flag, flag);
4857                         goto out_not_finished;
4858                 }
4859                 /* timeout active mbox command */
4860                 mod_timer(&psli->mbox_tmo, (jiffies +
4861                                (HZ * lpfc_mbox_tmo_val(phba, mb->mbxCommand))));
4862         }
4863
4864         /* Mailbox cmd <cmd> issue */
4865         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4866                         "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
4867                         "x%x\n",
4868                         pmbox->vport ? pmbox->vport->vpi : 0,
4869                         mb->mbxCommand, phba->pport->port_state,
4870                         psli->sli_flag, flag);
4871
4872         if (mb->mbxCommand != MBX_HEARTBEAT) {
4873                 if (pmbox->vport) {
4874                         lpfc_debugfs_disc_trc(pmbox->vport,
4875                                 LPFC_DISC_TRC_MBOX_VPORT,
4876                                 "MBOX Send vport: cmd:x%x mb:x%x x%x",
4877                                 (uint32_t)mb->mbxCommand,
4878                                 mb->un.varWords[0], mb->un.varWords[1]);
4879                 }
4880                 else {
4881                         lpfc_debugfs_disc_trc(phba->pport,
4882                                 LPFC_DISC_TRC_MBOX,
4883                                 "MBOX Send:       cmd:x%x mb:x%x x%x",
4884                                 (uint32_t)mb->mbxCommand,
4885                                 mb->un.varWords[0], mb->un.varWords[1]);
4886                 }
4887         }
4888
4889         psli->slistat.mbox_cmd++;
4890         evtctr = psli->slistat.mbox_event;
4891
4892         /* next set own bit for the adapter and copy over command word */
4893         mb->mbxOwner = OWN_CHIP;
4894
4895         if (psli->sli_flag & LPFC_SLI_ACTIVE) {
4896                 /* Populate mbox extension offset word. */
4897                 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
4898                         *(((uint32_t *)mb) + pmbox->mbox_offset_word)
4899                                 = (uint8_t *)phba->mbox_ext
4900                                   - (uint8_t *)phba->mbox;
4901                 }
4902
4903                 /* Copy the mailbox extension data */
4904                 if (pmbox->in_ext_byte_len && pmbox->context2) {
4905                         lpfc_sli_pcimem_bcopy(pmbox->context2,
4906                                 (uint8_t *)phba->mbox_ext,
4907                                 pmbox->in_ext_byte_len);
4908                 }
4909                 /* Copy command data to host SLIM area */
4910                 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
4911         } else {
4912                 /* Populate mbox extension offset word. */
4913                 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
4914                         *(((uint32_t *)mb) + pmbox->mbox_offset_word)
4915                                 = MAILBOX_HBA_EXT_OFFSET;
4916
4917                 /* Copy the mailbox extension data */
4918                 if (pmbox->in_ext_byte_len && pmbox->context2) {
4919                         lpfc_memcpy_to_slim(phba->MBslimaddr +
4920                                 MAILBOX_HBA_EXT_OFFSET,
4921                                 pmbox->context2, pmbox->in_ext_byte_len);
4922
4923                 }
4924                 if (mb->mbxCommand == MBX_CONFIG_PORT) {
4925                         /* copy command data into host mbox for cmpl */
4926                         lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
4927                 }
4928
4929                 /* First copy mbox command data to HBA SLIM, skip past first
4930                    word */
4931                 to_slim = phba->MBslimaddr + sizeof (uint32_t);
4932                 lpfc_memcpy_to_slim(to_slim, &mb->un.varWords[0],
4933                             MAILBOX_CMD_SIZE - sizeof (uint32_t));
4934
4935                 /* Next copy over first word, with mbxOwner set */
4936                 ldata = *((uint32_t *)mb);
4937                 to_slim = phba->MBslimaddr;
4938                 writel(ldata, to_slim);
4939                 readl(to_slim); /* flush */
4940
4941                 if (mb->mbxCommand == MBX_CONFIG_PORT) {
4942                         /* switch over to host mailbox */
4943                         psli->sli_flag |= LPFC_SLI_ACTIVE;
4944                 }
4945         }
4946
4947         wmb();
4948
4949         switch (flag) {
4950         case MBX_NOWAIT:
4951                 /* Set up reference to mailbox command */
4952                 psli->mbox_active = pmbox;
4953                 /* Interrupt board to do it */
4954                 writel(CA_MBATT, phba->CAregaddr);
4955                 readl(phba->CAregaddr); /* flush */
4956                 /* Don't wait for it to finish, just return */
4957                 break;
4958
4959         case MBX_POLL:
4960                 /* Set up null reference to mailbox command */
4961                 psli->mbox_active = NULL;
4962                 /* Interrupt board to do it */
4963                 writel(CA_MBATT, phba->CAregaddr);
4964                 readl(phba->CAregaddr); /* flush */
4965
4966                 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
4967                         /* First read mbox status word */
4968                         word0 = *((uint32_t *)phba->mbox);
4969                         word0 = le32_to_cpu(word0);
4970                 } else {
4971                         /* First read mbox status word */
4972                         word0 = readl(phba->MBslimaddr);
4973                 }
4974
4975                 /* Read the HBA Host Attention Register */
4976                 ha_copy = readl(phba->HAregaddr);
4977                 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
4978                                                              mb->mbxCommand) *
4979                                            1000) + jiffies;
4980                 i = 0;
4981                 /* Wait for command to complete */
4982                 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
4983                        (!(ha_copy & HA_MBATT) &&
4984                         (phba->link_state > LPFC_WARM_START))) {
4985                         if (time_after(jiffies, timeout)) {
4986                                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
4987                                 spin_unlock_irqrestore(&phba->hbalock,
4988                                                        drvr_flag);
4989                                 goto out_not_finished;
4990                         }
4991
4992                         /* Check if we took a mbox interrupt while we were
4993                            polling */
4994                         if (((word0 & OWN_CHIP) != OWN_CHIP)
4995                             && (evtctr != psli->slistat.mbox_event))
4996                                 break;
4997
4998                         if (i++ > 10) {
4999                                 spin_unlock_irqrestore(&phba->hbalock,
5000                                                        drvr_flag);
5001                                 msleep(1);
5002                                 spin_lock_irqsave(&phba->hbalock, drvr_flag);
5003                         }
5004
5005                         if (psli->sli_flag & LPFC_SLI_ACTIVE) {
5006                                 /* First copy command data */
5007                                 word0 = *((uint32_t *)phba->mbox);
5008                                 word0 = le32_to_cpu(word0);
5009                                 if (mb->mbxCommand == MBX_CONFIG_PORT) {
5010                                         MAILBOX_t *slimmb;
5011                                         uint32_t slimword0;
5012                                         /* Check real SLIM for any errors */
5013                                         slimword0 = readl(phba->MBslimaddr);
5014                                         slimmb = (MAILBOX_t *) & slimword0;
5015                                         if (((slimword0 & OWN_CHIP) != OWN_CHIP)
5016                                             && slimmb->mbxStatus) {
5017                                                 psli->sli_flag &=
5018                                                     ~LPFC_SLI_ACTIVE;
5019                                                 word0 = slimword0;
5020                                         }
5021                                 }
5022                         } else {
5023                                 /* First copy command data */
5024                                 word0 = readl(phba->MBslimaddr);
5025                         }
5026                         /* Read the HBA Host Attention Register */
5027                         ha_copy = readl(phba->HAregaddr);
5028                 }
5029
5030                 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
5031                         /* copy results back to user */
5032                         lpfc_sli_pcimem_bcopy(phba->mbox, mb, MAILBOX_CMD_SIZE);
5033                         /* Copy the mailbox extension data */
5034                         if (pmbox->out_ext_byte_len && pmbox->context2) {
5035                                 lpfc_sli_pcimem_bcopy(phba->mbox_ext,
5036                                                       pmbox->context2,
5037                                                       pmbox->out_ext_byte_len);
5038                         }
5039                 } else {
5040                         /* First copy command data */
5041                         lpfc_memcpy_from_slim(mb, phba->MBslimaddr,
5042                                                         MAILBOX_CMD_SIZE);
5043                         /* Copy the mailbox extension data */
5044                         if (pmbox->out_ext_byte_len && pmbox->context2) {
5045                                 lpfc_memcpy_from_slim(pmbox->context2,
5046                                         phba->MBslimaddr +
5047                                         MAILBOX_HBA_EXT_OFFSET,
5048                                         pmbox->out_ext_byte_len);
5049                         }
5050                 }
5051
5052                 writel(HA_MBATT, phba->HAregaddr);
5053                 readl(phba->HAregaddr); /* flush */
5054
5055                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5056                 status = mb->mbxStatus;
5057         }
5058
5059         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
5060         return status;
5061
5062 out_not_finished:
5063         if (processing_queue) {
5064                 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
5065                 lpfc_mbox_cmpl_put(phba, pmbox);
5066         }
5067         return MBX_NOT_FINISHED;
5068 }
5069
5070 /**
5071  * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
5072  * @phba: Pointer to HBA context object.
5073  *
5074  * The function blocks the posting of SLI4 asynchronous mailbox commands from
5075  * the driver internal pending mailbox queue. It will then try to wait out the
5076  * possible outstanding mailbox command before return.
5077  *
5078  * Returns:
5079  *      0 - the outstanding mailbox command completed; otherwise, the wait for
5080  *      the outstanding mailbox command timed out.
5081  **/
5082 static int
5083 lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
5084 {
5085         struct lpfc_sli *psli = &phba->sli;
5086         uint8_t actcmd = MBX_HEARTBEAT;
5087         int rc = 0;
5088         unsigned long timeout;
5089
5090         /* Mark the asynchronous mailbox command posting as blocked */
5091         spin_lock_irq(&phba->hbalock);
5092         psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
5093         if (phba->sli.mbox_active)
5094                 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
5095         spin_unlock_irq(&phba->hbalock);
5096         /* Determine how long we might wait for the active mailbox
5097          * command to be gracefully completed by firmware.
5098          */
5099         timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, actcmd) * 1000) +
5100                                    jiffies;
5101         /* Wait for the outstnading mailbox command to complete */
5102         while (phba->sli.mbox_active) {
5103                 /* Check active mailbox complete status every 2ms */
5104                 msleep(2);
5105                 if (time_after(jiffies, timeout)) {
5106                         /* Timeout, marked the outstanding cmd not complete */
5107                         rc = 1;
5108                         break;
5109                 }
5110         }
5111
5112         /* Can not cleanly block async mailbox command, fails it */
5113         if (rc) {
5114                 spin_lock_irq(&phba->hbalock);
5115                 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
5116                 spin_unlock_irq(&phba->hbalock);
5117         }
5118         return rc;
5119 }
5120
5121 /**
5122  * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
5123  * @phba: Pointer to HBA context object.
5124  *
5125  * The function unblocks and resume posting of SLI4 asynchronous mailbox
5126  * commands from the driver internal pending mailbox queue. It makes sure
5127  * that there is no outstanding mailbox command before resuming posting
5128  * asynchronous mailbox commands. If, for any reason, there is outstanding
5129  * mailbox command, it will try to wait it out before resuming asynchronous
5130  * mailbox command posting.
5131  **/
5132 static void
5133 lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
5134 {
5135         struct lpfc_sli *psli = &phba->sli;
5136
5137         spin_lock_irq(&phba->hbalock);
5138         if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
5139                 /* Asynchronous mailbox posting is not blocked, do nothing */
5140                 spin_unlock_irq(&phba->hbalock);
5141                 return;
5142         }
5143
5144         /* Outstanding synchronous mailbox command is guaranteed to be done,
5145          * successful or timeout, after timing-out the outstanding mailbox
5146          * command shall always be removed, so just unblock posting async
5147          * mailbox command and resume
5148          */
5149         psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
5150         spin_unlock_irq(&phba->hbalock);
5151
5152         /* wake up worker thread to post asynchronlous mailbox command */
5153         lpfc_worker_wake_up(phba);
5154 }
5155
5156 /**
5157  * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
5158  * @phba: Pointer to HBA context object.
5159  * @mboxq: Pointer to mailbox object.
5160  *
5161  * The function posts a mailbox to the port.  The mailbox is expected
5162  * to be comletely filled in and ready for the port to operate on it.
5163  * This routine executes a synchronous completion operation on the
5164  * mailbox by polling for its completion.
5165  *
5166  * The caller must not be holding any locks when calling this routine.
5167  *
5168  * Returns:
5169  *      MBX_SUCCESS - mailbox posted successfully
5170  *      Any of the MBX error values.
5171  **/
5172 static int
5173 lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
5174 {
5175         int rc = MBX_SUCCESS;
5176         unsigned long iflag;
5177         uint32_t db_ready;
5178         uint32_t mcqe_status;
5179         uint32_t mbx_cmnd;
5180         unsigned long timeout;
5181         struct lpfc_sli *psli = &phba->sli;
5182         struct lpfc_mqe *mb = &mboxq->u.mqe;
5183         struct lpfc_bmbx_create *mbox_rgn;
5184         struct dma_address *dma_address;
5185         struct lpfc_register bmbx_reg;
5186
5187         /*
5188          * Only one mailbox can be active to the bootstrap mailbox region
5189          * at a time and there is no queueing provided.
5190          */
5191         spin_lock_irqsave(&phba->hbalock, iflag);
5192         if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
5193                 spin_unlock_irqrestore(&phba->hbalock, iflag);
5194                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5195                                 "(%d):2532 Mailbox command x%x (x%x) "
5196                                 "cannot issue Data: x%x x%x\n",
5197                                 mboxq->vport ? mboxq->vport->vpi : 0,
5198                                 mboxq->u.mb.mbxCommand,
5199                                 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5200                                 psli->sli_flag, MBX_POLL);
5201                 return MBXERR_ERROR;
5202         }
5203         /* The server grabs the token and owns it until release */
5204         psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
5205         phba->sli.mbox_active = mboxq;
5206         spin_unlock_irqrestore(&phba->hbalock, iflag);
5207
5208         /*
5209          * Initialize the bootstrap memory region to avoid stale data areas
5210          * in the mailbox post.  Then copy the caller's mailbox contents to
5211          * the bmbx mailbox region.
5212          */
5213         mbx_cmnd = bf_get(lpfc_mqe_command, mb);
5214         memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
5215         lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
5216                               sizeof(struct lpfc_mqe));
5217
5218         /* Post the high mailbox dma address to the port and wait for ready. */
5219         dma_address = &phba->sli4_hba.bmbx.dma_address;
5220         writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
5221
5222         timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mbx_cmnd)
5223                                    * 1000) + jiffies;
5224         do {
5225                 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
5226                 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
5227                 if (!db_ready)
5228                         msleep(2);
5229
5230                 if (time_after(jiffies, timeout)) {
5231                         rc = MBXERR_ERROR;
5232                         goto exit;
5233                 }
5234         } while (!db_ready);
5235
5236         /* Post the low mailbox dma address to the port. */
5237         writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
5238         timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mbx_cmnd)
5239                                    * 1000) + jiffies;
5240         do {
5241                 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
5242                 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
5243                 if (!db_ready)
5244                         msleep(2);
5245
5246                 if (time_after(jiffies, timeout)) {
5247                         rc = MBXERR_ERROR;
5248                         goto exit;
5249                 }
5250         } while (!db_ready);
5251
5252         /*
5253          * Read the CQ to ensure the mailbox has completed.
5254          * If so, update the mailbox status so that the upper layers
5255          * can complete the request normally.
5256          */
5257         lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
5258                               sizeof(struct lpfc_mqe));
5259         mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
5260         lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
5261                               sizeof(struct lpfc_mcqe));
5262         mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
5263
5264         /* Prefix the mailbox status with range x4000 to note SLI4 status. */
5265         if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
5266                 bf_set(lpfc_mqe_status, mb, LPFC_MBX_ERROR_RANGE | mcqe_status);
5267                 rc = MBXERR_ERROR;
5268         }
5269
5270         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5271                         "(%d):0356 Mailbox cmd x%x (x%x) Status x%x "
5272                         "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
5273                         " x%x x%x CQ: x%x x%x x%x x%x\n",
5274                         mboxq->vport ? mboxq->vport->vpi : 0,
5275                         mbx_cmnd, lpfc_sli4_mbox_opcode_get(phba, mboxq),
5276                         bf_get(lpfc_mqe_status, mb),
5277                         mb->un.mb_words[0], mb->un.mb_words[1],
5278                         mb->un.mb_words[2], mb->un.mb_words[3],
5279                         mb->un.mb_words[4], mb->un.mb_words[5],
5280                         mb->un.mb_words[6], mb->un.mb_words[7],
5281                         mb->un.mb_words[8], mb->un.mb_words[9],
5282                         mb->un.mb_words[10], mb->un.mb_words[11],
5283                         mb->un.mb_words[12], mboxq->mcqe.word0,
5284                         mboxq->mcqe.mcqe_tag0,  mboxq->mcqe.mcqe_tag1,
5285                         mboxq->mcqe.trailer);
5286 exit:
5287         /* We are holding the token, no needed for lock when release */
5288         spin_lock_irqsave(&phba->hbalock, iflag);
5289         psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5290         phba->sli.mbox_active = NULL;
5291         spin_unlock_irqrestore(&phba->hbalock, iflag);
5292         return rc;
5293 }
5294
5295 /**
5296  * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
5297  * @phba: Pointer to HBA context object.
5298  * @pmbox: Pointer to mailbox object.
5299  * @flag: Flag indicating how the mailbox need to be processed.
5300  *
5301  * This function is called by discovery code and HBA management code to submit
5302  * a mailbox command to firmware with SLI-4 interface spec.
5303  *
5304  * Return codes the caller owns the mailbox command after the return of the
5305  * function.
5306  **/
5307 static int
5308 lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
5309                        uint32_t flag)
5310 {
5311         struct lpfc_sli *psli = &phba->sli;
5312         unsigned long iflags;
5313         int rc;
5314
5315         rc = lpfc_mbox_dev_check(phba);
5316         if (unlikely(rc)) {
5317                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5318                                 "(%d):2544 Mailbox command x%x (x%x) "
5319                                 "cannot issue Data: x%x x%x\n",
5320                                 mboxq->vport ? mboxq->vport->vpi : 0,
5321                                 mboxq->u.mb.mbxCommand,
5322                                 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5323                                 psli->sli_flag, flag);
5324                 goto out_not_finished;
5325         }
5326
5327         /* Detect polling mode and jump to a handler */
5328         if (!phba->sli4_hba.intr_enable) {
5329                 if (flag == MBX_POLL)
5330                         rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
5331                 else
5332                         rc = -EIO;
5333                 if (rc != MBX_SUCCESS)
5334                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5335                                         "(%d):2541 Mailbox command x%x "
5336                                         "(x%x) cannot issue Data: x%x x%x\n",
5337                                         mboxq->vport ? mboxq->vport->vpi : 0,
5338                                         mboxq->u.mb.mbxCommand,
5339                                         lpfc_sli4_mbox_opcode_get(phba, mboxq),
5340                                         psli->sli_flag, flag);
5341                 return rc;
5342         } else if (flag == MBX_POLL) {
5343                 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
5344                                 "(%d):2542 Try to issue mailbox command "
5345                                 "x%x (x%x) synchronously ahead of async"
5346                                 "mailbox command queue: x%x x%x\n",
5347                                 mboxq->vport ? mboxq->vport->vpi : 0,
5348                                 mboxq->u.mb.mbxCommand,
5349                                 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5350                                 psli->sli_flag, flag);
5351                 /* Try to block the asynchronous mailbox posting */
5352                 rc = lpfc_sli4_async_mbox_block(phba);
5353                 if (!rc) {
5354                         /* Successfully blocked, now issue sync mbox cmd */
5355                         rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
5356                         if (rc != MBX_SUCCESS)
5357                                 lpfc_printf_log(phba, KERN_ERR,
5358                                                 LOG_MBOX | LOG_SLI,
5359                                                 "(%d):2597 Mailbox command "
5360                                                 "x%x (x%x) cannot issue "
5361                                                 "Data: x%x x%x\n",
5362                                                 mboxq->vport ?
5363                                                 mboxq->vport->vpi : 0,
5364                                                 mboxq->u.mb.mbxCommand,
5365                                                 lpfc_sli4_mbox_opcode_get(phba,
5366                                                                 mboxq),
5367                                                 psli->sli_flag, flag);
5368                         /* Unblock the async mailbox posting afterward */
5369                         lpfc_sli4_async_mbox_unblock(phba);
5370                 }
5371                 return rc;
5372         }
5373
5374         /* Now, interrupt mode asynchrous mailbox command */
5375         rc = lpfc_mbox_cmd_check(phba, mboxq);
5376         if (rc) {
5377                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5378                                 "(%d):2543 Mailbox command x%x (x%x) "
5379                                 "cannot issue Data: x%x x%x\n",
5380                                 mboxq->vport ? mboxq->vport->vpi : 0,
5381                                 mboxq->u.mb.mbxCommand,
5382                                 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5383                                 psli->sli_flag, flag);
5384                 goto out_not_finished;
5385         }
5386
5387         /* Put the mailbox command to the driver internal FIFO */
5388         psli->slistat.mbox_busy++;
5389         spin_lock_irqsave(&phba->hbalock, iflags);
5390         lpfc_mbox_put(phba, mboxq);
5391         spin_unlock_irqrestore(&phba->hbalock, iflags);
5392         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5393                         "(%d):0354 Mbox cmd issue - Enqueue Data: "
5394                         "x%x (x%x) x%x x%x x%x\n",
5395                         mboxq->vport ? mboxq->vport->vpi : 0xffffff,
5396                         bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5397                         lpfc_sli4_mbox_opcode_get(phba, mboxq),
5398                         phba->pport->port_state,
5399                         psli->sli_flag, MBX_NOWAIT);
5400         /* Wake up worker thread to transport mailbox command from head */
5401         lpfc_worker_wake_up(phba);
5402
5403         return MBX_BUSY;
5404
5405 out_not_finished:
5406         return MBX_NOT_FINISHED;
5407 }
5408
5409 /**
5410  * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
5411  * @phba: Pointer to HBA context object.
5412  *
5413  * This function is called by worker thread to send a mailbox command to
5414  * SLI4 HBA firmware.
5415  *
5416  **/
5417 int
5418 lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
5419 {
5420         struct lpfc_sli *psli = &phba->sli;
5421         LPFC_MBOXQ_t *mboxq;
5422         int rc = MBX_SUCCESS;
5423         unsigned long iflags;
5424         struct lpfc_mqe *mqe;
5425         uint32_t mbx_cmnd;
5426
5427         /* Check interrupt mode before post async mailbox command */
5428         if (unlikely(!phba->sli4_hba.intr_enable))
5429                 return MBX_NOT_FINISHED;
5430
5431         /* Check for mailbox command service token */
5432         spin_lock_irqsave(&phba->hbalock, iflags);
5433         if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
5434                 spin_unlock_irqrestore(&phba->hbalock, iflags);
5435                 return MBX_NOT_FINISHED;
5436         }
5437         if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
5438                 spin_unlock_irqrestore(&phba->hbalock, iflags);
5439                 return MBX_NOT_FINISHED;
5440         }
5441         if (unlikely(phba->sli.mbox_active)) {
5442                 spin_unlock_irqrestore(&phba->hbalock, iflags);
5443                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5444                                 "0384 There is pending active mailbox cmd\n");
5445                 return MBX_NOT_FINISHED;
5446         }
5447         /* Take the mailbox command service token */
5448         psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
5449
5450         /* Get the next mailbox command from head of queue */
5451         mboxq = lpfc_mbox_get(phba);
5452
5453         /* If no more mailbox command waiting for post, we're done */
5454         if (!mboxq) {
5455                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5456                 spin_unlock_irqrestore(&phba->hbalock, iflags);
5457                 return MBX_SUCCESS;
5458         }
5459         phba->sli.mbox_active = mboxq;
5460         spin_unlock_irqrestore(&phba->hbalock, iflags);
5461
5462         /* Check device readiness for posting mailbox command */
5463         rc = lpfc_mbox_dev_check(phba);
5464         if (unlikely(rc))
5465                 /* Driver clean routine will clean up pending mailbox */
5466                 goto out_not_finished;
5467
5468         /* Prepare the mbox command to be posted */
5469         mqe = &mboxq->u.mqe;
5470         mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
5471
5472         /* Start timer for the mbox_tmo and log some mailbox post messages */
5473         mod_timer(&psli->mbox_tmo, (jiffies +
5474                   (HZ * lpfc_mbox_tmo_val(phba, mbx_cmnd))));
5475
5476         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5477                         "(%d):0355 Mailbox cmd x%x (x%x) issue Data: "
5478                         "x%x x%x\n",
5479                         mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
5480                         lpfc_sli4_mbox_opcode_get(phba, mboxq),
5481                         phba->pport->port_state, psli->sli_flag);
5482
5483         if (mbx_cmnd != MBX_HEARTBEAT) {
5484                 if (mboxq->vport) {
5485                         lpfc_debugfs_disc_trc(mboxq->vport,
5486                                 LPFC_DISC_TRC_MBOX_VPORT,
5487                                 "MBOX Send vport: cmd:x%x mb:x%x x%x",
5488                                 mbx_cmnd, mqe->un.mb_words[0],
5489                                 mqe->un.mb_words[1]);
5490                 } else {
5491                         lpfc_debugfs_disc_trc(phba->pport,
5492                                 LPFC_DISC_TRC_MBOX,
5493                                 "MBOX Send: cmd:x%x mb:x%x x%x",
5494                                 mbx_cmnd, mqe->un.mb_words[0],
5495                                 mqe->un.mb_words[1]);
5496                 }
5497         }
5498         psli->slistat.mbox_cmd++;
5499
5500         /* Post the mailbox command to the port */
5501         rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
5502         if (rc != MBX_SUCCESS) {
5503                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5504                                 "(%d):2533 Mailbox command x%x (x%x) "
5505                                 "cannot issue Data: x%x x%x\n",
5506                                 mboxq->vport ? mboxq->vport->vpi : 0,
5507                                 mboxq->u.mb.mbxCommand,
5508                                 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5509                                 psli->sli_flag, MBX_NOWAIT);
5510                 goto out_not_finished;
5511         }
5512
5513         return rc;
5514
5515 out_not_finished:
5516         spin_lock_irqsave(&phba->hbalock, iflags);
5517         mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
5518         __lpfc_mbox_cmpl_put(phba, mboxq);
5519         /* Release the token */
5520         psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5521         phba->sli.mbox_active = NULL;
5522         spin_unlock_irqrestore(&phba->hbalock, iflags);
5523
5524         return MBX_NOT_FINISHED;
5525 }
5526
5527 /**
5528  * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
5529  * @phba: Pointer to HBA context object.
5530  * @pmbox: Pointer to mailbox object.
5531  * @flag: Flag indicating how the mailbox need to be processed.
5532  *
5533  * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
5534  * the API jump table function pointer from the lpfc_hba struct.
5535  *
5536  * Return codes the caller owns the mailbox command after the return of the
5537  * function.
5538  **/
5539 int
5540 lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
5541 {
5542         return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
5543 }
5544
5545 /**
5546  * lpfc_mbox_api_table_setup - Set up mbox api fucntion jump table
5547  * @phba: The hba struct for which this call is being executed.
5548  * @dev_grp: The HBA PCI-Device group number.
5549  *
5550  * This routine sets up the mbox interface API function jump table in @phba
5551  * struct.
5552  * Returns: 0 - success, -ENODEV - failure.
5553  **/
5554 int
5555 lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
5556 {
5557
5558         switch (dev_grp) {
5559         case LPFC_PCI_DEV_LP:
5560                 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
5561                 phba->lpfc_sli_handle_slow_ring_event =
5562                                 lpfc_sli_handle_slow_ring_event_s3;
5563                 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
5564                 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
5565                 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
5566                 break;
5567         case LPFC_PCI_DEV_OC:
5568                 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
5569                 phba->lpfc_sli_handle_slow_ring_event =
5570                                 lpfc_sli_handle_slow_ring_event_s4;
5571                 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
5572                 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
5573                 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
5574                 break;
5575         default:
5576                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5577                                 "1420 Invalid HBA PCI-device group: 0x%x\n",
5578                                 dev_grp);
5579                 return -ENODEV;
5580                 break;
5581         }
5582         return 0;
5583 }
5584
5585 /**
5586  * __lpfc_sli_ringtx_put - Add an iocb to the txq
5587  * @phba: Pointer to HBA context object.
5588  * @pring: Pointer to driver SLI ring object.
5589  * @piocb: Pointer to address of newly added command iocb.
5590  *
5591  * This function is called with hbalock held to add a command
5592  * iocb to the txq when SLI layer cannot submit the command iocb
5593  * to the ring.
5594  **/
5595 static void
5596 __lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
5597                     struct lpfc_iocbq *piocb)
5598 {
5599         /* Insert the caller's iocb in the txq tail for later processing. */
5600         list_add_tail(&piocb->list, &pring->txq);
5601         pring->txq_cnt++;
5602 }
5603
5604 /**
5605  * lpfc_sli_next_iocb - Get the next iocb in the txq
5606  * @phba: Pointer to HBA context object.
5607  * @pring: Pointer to driver SLI ring object.
5608  * @piocb: Pointer to address of newly added command iocb.
5609  *
5610  * This function is called with hbalock held before a new
5611  * iocb is submitted to the firmware. This function checks
5612  * txq to flush the iocbs in txq to Firmware before
5613  * submitting new iocbs to the Firmware.
5614  * If there are iocbs in the txq which need to be submitted
5615  * to firmware, lpfc_sli_next_iocb returns the first element
5616  * of the txq after dequeuing it from txq.
5617  * If there is no iocb in the txq then the function will return
5618  * *piocb and *piocb is set to NULL. Caller needs to check
5619  * *piocb to find if there are more commands in the txq.
5620  **/
5621 static struct lpfc_iocbq *
5622 lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
5623                    struct lpfc_iocbq **piocb)
5624 {
5625         struct lpfc_iocbq * nextiocb;
5626
5627         nextiocb = lpfc_sli_ringtx_get(phba, pring);
5628         if (!nextiocb) {
5629                 nextiocb = *piocb;
5630                 *piocb = NULL;
5631         }
5632
5633         return nextiocb;
5634 }
5635
5636 /**
5637  * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
5638  * @phba: Pointer to HBA context object.
5639  * @ring_number: SLI ring number to issue iocb on.
5640  * @piocb: Pointer to command iocb.
5641  * @flag: Flag indicating if this command can be put into txq.
5642  *
5643  * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
5644  * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
5645  * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
5646  * flag is turned on, the function returns IOCB_ERROR. When the link is down,
5647  * this function allows only iocbs for posting buffers. This function finds
5648  * next available slot in the command ring and posts the command to the
5649  * available slot and writes the port attention register to request HBA start
5650  * processing new iocb. If there is no slot available in the ring and
5651  * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
5652  * the function returns IOCB_BUSY.
5653  *
5654  * This function is called with hbalock held. The function will return success
5655  * after it successfully submit the iocb to firmware or after adding to the
5656  * txq.
5657  **/
5658 static int
5659 __lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
5660                     struct lpfc_iocbq *piocb, uint32_t flag)
5661 {
5662         struct lpfc_iocbq *nextiocb;
5663         IOCB_t *iocb;
5664         struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
5665
5666         if (piocb->iocb_cmpl && (!piocb->vport) &&
5667            (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
5668            (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
5669                 lpfc_printf_log(phba, KERN_ERR,
5670                                 LOG_SLI | LOG_VPORT,
5671                                 "1807 IOCB x%x failed. No vport\n",
5672                                 piocb->iocb.ulpCommand);
5673                 dump_stack();
5674                 return IOCB_ERROR;
5675         }
5676
5677
5678         /* If the PCI channel is in offline state, do not post iocbs. */
5679         if (unlikely(pci_channel_offline(phba->pcidev)))
5680                 return IOCB_ERROR;
5681
5682         /* If HBA has a deferred error attention, fail the iocb. */
5683         if (unlikely(phba->hba_flag & DEFER_ERATT))
5684                 return IOCB_ERROR;
5685
5686         /*
5687          * We should never get an IOCB if we are in a < LINK_DOWN state
5688          */
5689         if (unlikely(phba->link_state < LPFC_LINK_DOWN))
5690                 return IOCB_ERROR;
5691
5692         /*
5693          * Check to see if we are blocking IOCB processing because of a
5694          * outstanding event.
5695          */
5696         if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
5697                 goto iocb_busy;
5698
5699         if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
5700                 /*
5701                  * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
5702                  * can be issued if the link is not up.
5703                  */
5704                 switch (piocb->iocb.ulpCommand) {
5705                 case CMD_GEN_REQUEST64_CR:
5706                 case CMD_GEN_REQUEST64_CX:
5707                         if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
5708                                 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
5709                                         FC_RCTL_DD_UNSOL_CMD) ||
5710                                 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
5711                                         MENLO_TRANSPORT_TYPE))
5712
5713                                 goto iocb_busy;
5714                         break;
5715                 case CMD_QUE_RING_BUF_CN:
5716                 case CMD_QUE_RING_BUF64_CN:
5717                         /*
5718                          * For IOCBs, like QUE_RING_BUF, that have no rsp ring
5719                          * completion, iocb_cmpl MUST be 0.
5720                          */
5721                         if (piocb->iocb_cmpl)
5722                                 piocb->iocb_cmpl = NULL;
5723                         /*FALLTHROUGH*/
5724                 case CMD_CREATE_XRI_CR:
5725                 case CMD_CLOSE_XRI_CN:
5726                 case CMD_CLOSE_XRI_CX:
5727                         break;
5728                 default:
5729                         goto iocb_busy;
5730                 }
5731
5732         /*
5733          * For FCP commands, we must be in a state where we can process link
5734          * attention events.
5735          */
5736         } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
5737                             !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
5738                 goto iocb_busy;
5739         }
5740
5741         while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
5742                (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
5743                 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
5744
5745         if (iocb)
5746                 lpfc_sli_update_ring(phba, pring);
5747         else
5748                 lpfc_sli_update_full_ring(phba, pring);
5749
5750         if (!piocb)
5751                 return IOCB_SUCCESS;
5752
5753         goto out_busy;
5754
5755  iocb_busy:
5756         pring->stats.iocb_cmd_delay++;
5757
5758  out_busy:
5759
5760         if (!(flag & SLI_IOCB_RET_IOCB)) {
5761                 __lpfc_sli_ringtx_put(phba, pring, piocb);
5762                 return IOCB_SUCCESS;
5763         }
5764
5765         return IOCB_BUSY;
5766 }
5767
5768 /**
5769  * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
5770  * @phba: Pointer to HBA context object.
5771  * @piocb: Pointer to command iocb.
5772  * @sglq: Pointer to the scatter gather queue object.
5773  *
5774  * This routine converts the bpl or bde that is in the IOCB
5775  * to a sgl list for the sli4 hardware. The physical address
5776  * of the bpl/bde is converted back to a virtual address.
5777  * If the IOCB contains a BPL then the list of BDE's is
5778  * converted to sli4_sge's. If the IOCB contains a single
5779  * BDE then it is converted to a single sli_sge.
5780  * The IOCB is still in cpu endianess so the contents of
5781  * the bpl can be used without byte swapping.
5782  *
5783  * Returns valid XRI = Success, NO_XRI = Failure.
5784 **/
5785 static uint16_t
5786 lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
5787                 struct lpfc_sglq *sglq)
5788 {
5789         uint16_t xritag = NO_XRI;
5790         struct ulp_bde64 *bpl = NULL;
5791         struct ulp_bde64 bde;
5792         struct sli4_sge *sgl  = NULL;
5793         IOCB_t *icmd;
5794         int numBdes = 0;
5795         int i = 0;
5796
5797         if (!piocbq || !sglq)
5798                 return xritag;
5799
5800         sgl  = (struct sli4_sge *)sglq->sgl;
5801         icmd = &piocbq->iocb;
5802         if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
5803                 numBdes = icmd->un.genreq64.bdl.bdeSize /
5804                                 sizeof(struct ulp_bde64);
5805                 /* The addrHigh and addrLow fields within the IOCB
5806                  * have not been byteswapped yet so there is no
5807                  * need to swap them back.
5808                  */
5809                 bpl  = (struct ulp_bde64 *)
5810                         ((struct lpfc_dmabuf *)piocbq->context3)->virt;
5811
5812                 if (!bpl)
5813                         return xritag;
5814
5815                 for (i = 0; i < numBdes; i++) {
5816                         /* Should already be byte swapped. */
5817                         sgl->addr_hi = bpl->addrHigh;
5818                         sgl->addr_lo = bpl->addrLow;
5819
5820                         if ((i+1) == numBdes)
5821                                 bf_set(lpfc_sli4_sge_last, sgl, 1);
5822                         else
5823                                 bf_set(lpfc_sli4_sge_last, sgl, 0);
5824                         sgl->word2 = cpu_to_le32(sgl->word2);
5825                         /* swap the size field back to the cpu so we
5826                          * can assign it to the sgl.
5827                          */
5828                         bde.tus.w = le32_to_cpu(bpl->tus.w);
5829                         sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
5830                         bpl++;
5831                         sgl++;
5832                 }
5833         } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
5834                         /* The addrHigh and addrLow fields of the BDE have not
5835                          * been byteswapped yet so they need to be swapped
5836                          * before putting them in the sgl.
5837                          */
5838                         sgl->addr_hi =
5839                                 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
5840                         sgl->addr_lo =
5841                                 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
5842                         bf_set(lpfc_sli4_sge_last, sgl, 1);
5843                         sgl->word2 = cpu_to_le32(sgl->word2);
5844                         sgl->sge_len =
5845                                 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
5846         }
5847         return sglq->sli4_xritag;
5848 }
5849
5850 /**
5851  * lpfc_sli4_scmd_to_wqidx_distr - scsi command to SLI4 WQ index distribution
5852  * @phba: Pointer to HBA context object.
5853  *
5854  * This routine performs a round robin SCSI command to SLI4 FCP WQ index
5855  * distribution.  This is called by __lpfc_sli_issue_iocb_s4() with the hbalock
5856  * held.
5857  *
5858  * Return: index into SLI4 fast-path FCP queue index.
5859  **/
5860 static uint32_t
5861 lpfc_sli4_scmd_to_wqidx_distr(struct lpfc_hba *phba)
5862 {
5863         ++phba->fcp_qidx;
5864         if (phba->fcp_qidx >= phba->cfg_fcp_wq_count)
5865                 phba->fcp_qidx = 0;
5866
5867         return phba->fcp_qidx;
5868 }
5869
5870 /**
5871  * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
5872  * @phba: Pointer to HBA context object.
5873  * @piocb: Pointer to command iocb.
5874  * @wqe: Pointer to the work queue entry.
5875  *
5876  * This routine converts the iocb command to its Work Queue Entry
5877  * equivalent. The wqe pointer should not have any fields set when
5878  * this routine is called because it will memcpy over them.
5879  * This routine does not set the CQ_ID or the WQEC bits in the
5880  * wqe.
5881  *
5882  * Returns: 0 = Success, IOCB_ERROR = Failure.
5883  **/
5884 static int
5885 lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
5886                 union lpfc_wqe *wqe)
5887 {
5888         uint32_t xmit_len = 0, total_len = 0;
5889         uint8_t ct = 0;
5890         uint32_t fip;
5891         uint32_t abort_tag;
5892         uint8_t command_type = ELS_COMMAND_NON_FIP;
5893         uint8_t cmnd;
5894         uint16_t xritag;
5895         struct ulp_bde64 *bpl = NULL;
5896         uint32_t els_id = ELS_ID_DEFAULT;
5897         int numBdes, i;
5898         struct ulp_bde64 bde;
5899
5900         fip = phba->hba_flag & HBA_FIP_SUPPORT;
5901         /* The fcp commands will set command type */
5902         if (iocbq->iocb_flag &  LPFC_IO_FCP)
5903                 command_type = FCP_COMMAND;
5904         else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
5905                 command_type = ELS_COMMAND_FIP;
5906         else
5907                 command_type = ELS_COMMAND_NON_FIP;
5908
5909         /* Some of the fields are in the right position already */
5910         memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
5911         abort_tag = (uint32_t) iocbq->iotag;
5912         xritag = iocbq->sli4_xritag;
5913         wqe->words[7] = 0; /* The ct field has moved so reset */
5914         /* words0-2 bpl convert bde */
5915         if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
5916                 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
5917                                 sizeof(struct ulp_bde64);
5918                 bpl  = (struct ulp_bde64 *)
5919                         ((struct lpfc_dmabuf *)iocbq->context3)->virt;
5920                 if (!bpl)
5921                         return IOCB_ERROR;
5922
5923                 /* Should already be byte swapped. */
5924                 wqe->generic.bde.addrHigh =  le32_to_cpu(bpl->addrHigh);
5925                 wqe->generic.bde.addrLow =  le32_to_cpu(bpl->addrLow);
5926                 /* swap the size field back to the cpu so we
5927                  * can assign it to the sgl.
5928                  */
5929                 wqe->generic.bde.tus.w  = le32_to_cpu(bpl->tus.w);
5930                 xmit_len = wqe->generic.bde.tus.f.bdeSize;
5931                 total_len = 0;
5932                 for (i = 0; i < numBdes; i++) {
5933                         bde.tus.w  = le32_to_cpu(bpl[i].tus.w);
5934                         total_len += bde.tus.f.bdeSize;
5935                 }
5936         } else
5937                 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
5938
5939         iocbq->iocb.ulpIoTag = iocbq->iotag;
5940         cmnd = iocbq->iocb.ulpCommand;
5941
5942         switch (iocbq->iocb.ulpCommand) {
5943         case CMD_ELS_REQUEST64_CR:
5944                 if (!iocbq->iocb.ulpLe) {
5945                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5946                                 "2007 Only Limited Edition cmd Format"
5947                                 " supported 0x%x\n",
5948                                 iocbq->iocb.ulpCommand);
5949                         return IOCB_ERROR;
5950                 }
5951                 wqe->els_req.payload_len = xmit_len;
5952                 /* Els_reguest64 has a TMO */
5953                 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
5954                         iocbq->iocb.ulpTimeout);
5955                 /* Need a VF for word 4 set the vf bit*/
5956                 bf_set(els_req64_vf, &wqe->els_req, 0);
5957                 /* And a VFID for word 12 */
5958                 bf_set(els_req64_vfid, &wqe->els_req, 0);
5959                 /*
5960                  * Set ct field to 3, indicates that the context_tag field
5961                  * contains the FCFI and remote N_Port_ID is
5962                  * in word 5.
5963                  */
5964
5965                 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
5966                 bf_set(lpfc_wqe_gen_context, &wqe->generic,
5967                                 iocbq->iocb.ulpContext);
5968
5969                 bf_set(lpfc_wqe_gen_ct, &wqe->generic, ct);
5970                 bf_set(lpfc_wqe_gen_pu, &wqe->generic, 0);
5971                 /* CCP CCPE PV PRI in word10 were set in the memcpy */
5972
5973                 if (command_type == ELS_COMMAND_FIP) {
5974                         els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
5975                                         >> LPFC_FIP_ELS_ID_SHIFT);
5976                 }
5977                 bf_set(lpfc_wqe_gen_els_id, &wqe->generic, els_id);
5978
5979         break;
5980         case CMD_XMIT_SEQUENCE64_CX:
5981                 bf_set(lpfc_wqe_gen_context, &wqe->generic,
5982                                         iocbq->iocb.un.ulpWord[3]);
5983                 wqe->generic.word3 = 0;
5984                 bf_set(wqe_rcvoxid, &wqe->generic, iocbq->iocb.ulpContext);
5985                 /* The entire sequence is transmitted for this IOCB */
5986                 xmit_len = total_len;
5987                 cmnd = CMD_XMIT_SEQUENCE64_CR;
5988         case CMD_XMIT_SEQUENCE64_CR:
5989                 /* word3 iocb=io_tag32 wqe=payload_offset */
5990                 /* payload offset used for multilpe outstanding
5991                  * sequences on the same exchange
5992                  */
5993                 wqe->words[3] = 0;
5994                 /* word4 relative_offset memcpy */
5995                 /* word5 r_ctl/df_ctl memcpy */
5996                 bf_set(lpfc_wqe_gen_pu, &wqe->generic, 0);
5997                 wqe->xmit_sequence.xmit_len = xmit_len;
5998                 command_type = OTHER_COMMAND;
5999         break;
6000         case CMD_XMIT_BCAST64_CN:
6001                 /* word3 iocb=iotag32 wqe=payload_len */
6002                 wqe->words[3] = 0; /* no definition for this in wqe */
6003                 /* word4 iocb=rsvd wqe=rsvd */
6004                 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
6005                 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
6006                 bf_set(lpfc_wqe_gen_ct, &wqe->generic,
6007                         ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
6008         break;
6009         case CMD_FCP_IWRITE64_CR:
6010                 command_type = FCP_COMMAND_DATA_OUT;
6011                 /* The struct for wqe fcp_iwrite has 3 fields that are somewhat
6012                  * confusing.
6013                  * word3 is payload_len: byte offset to the sgl entry for the
6014                  * fcp_command.
6015                  * word4 is total xfer len, same as the IOCB->ulpParameter.
6016                  * word5 is initial xfer len 0 = wait for xfer-ready
6017                  */
6018
6019                 /* Always wait for xfer-ready before sending data */
6020                 wqe->fcp_iwrite.initial_xfer_len = 0;
6021                 /* word 4 (xfer length) should have been set on the memcpy */
6022
6023         /* allow write to fall through to read */
6024         case CMD_FCP_IREAD64_CR:
6025                 /* FCP_CMD is always the 1st sgl entry */
6026                 wqe->fcp_iread.payload_len =
6027                         xmit_len + sizeof(struct fcp_rsp);
6028
6029                 /* word 4 (xfer length) should have been set on the memcpy */
6030
6031                 bf_set(lpfc_wqe_gen_erp, &wqe->generic,
6032                         iocbq->iocb.ulpFCP2Rcvy);
6033                 bf_set(lpfc_wqe_gen_lnk, &wqe->generic, iocbq->iocb.ulpXS);
6034                 /* The XC bit and the XS bit are similar. The driver never
6035                  * tracked whether or not the exchange was previouslly open.
6036                  * XC = Exchange create, 0 is create. 1 is already open.
6037                  * XS = link cmd: 1 do not close the exchange after command.
6038                  * XS = 0 close exchange when command completes.
6039                  * The only time we would not set the XC bit is when the XS bit
6040                  * is set and we are sending our 2nd or greater command on
6041                  * this exchange.
6042                  */
6043                 /* Always open the exchange */
6044                 bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
6045
6046                 wqe->words[10] &= 0xffff0000; /* zero out ebde count */
6047                 bf_set(lpfc_wqe_gen_pu, &wqe->generic, iocbq->iocb.ulpPU);
6048                 break;
6049         case CMD_FCP_ICMND64_CR:
6050                 /* Always open the exchange */
6051                 bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
6052
6053                 wqe->words[4] = 0;
6054                 wqe->words[10] &= 0xffff0000; /* zero out ebde count */
6055                 bf_set(lpfc_wqe_gen_pu, &wqe->generic, 0);
6056         break;
6057         case CMD_GEN_REQUEST64_CR:
6058                 /* word3 command length is described as byte offset to the
6059                  * rsp_data. Would always be 16, sizeof(struct sli4_sge)
6060                  * sgl[0] = cmnd
6061                  * sgl[1] = rsp.
6062                  *
6063                  */
6064                 wqe->gen_req.command_len = xmit_len;
6065                 /* Word4 parameter  copied in the memcpy */
6066                 /* Word5 [rctl, type, df_ctl, la] copied in memcpy */
6067                 /* word6 context tag copied in memcpy */
6068                 if (iocbq->iocb.ulpCt_h  || iocbq->iocb.ulpCt_l) {
6069                         ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
6070                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6071                                 "2015 Invalid CT %x command 0x%x\n",
6072                                 ct, iocbq->iocb.ulpCommand);
6073                         return IOCB_ERROR;
6074                 }
6075                 bf_set(lpfc_wqe_gen_ct, &wqe->generic, 0);
6076                 bf_set(wqe_tmo, &wqe->gen_req.wqe_com,
6077                         iocbq->iocb.ulpTimeout);
6078
6079                 bf_set(lpfc_wqe_gen_pu, &wqe->generic, iocbq->iocb.ulpPU);
6080                 command_type = OTHER_COMMAND;
6081         break;
6082         case CMD_XMIT_ELS_RSP64_CX:
6083                 /* words0-2 BDE memcpy */
6084                 /* word3 iocb=iotag32 wqe=rsvd */
6085                 wqe->words[3] = 0;
6086                 /* word4 iocb=did wge=rsvd. */
6087                 wqe->words[4] = 0;
6088                 /* word5 iocb=rsvd wge=did */
6089                 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
6090                          iocbq->iocb.un.elsreq64.remoteID);
6091
6092                 bf_set(lpfc_wqe_gen_ct, &wqe->generic,
6093                         ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
6094
6095                 bf_set(lpfc_wqe_gen_pu, &wqe->generic, iocbq->iocb.ulpPU);
6096                 bf_set(wqe_rcvoxid, &wqe->generic, iocbq->iocb.ulpContext);
6097                 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
6098                         bf_set(lpfc_wqe_gen_context, &wqe->generic,
6099                                iocbq->vport->vpi + phba->vpi_base);
6100                 command_type = OTHER_COMMAND;
6101         break;
6102         case CMD_CLOSE_XRI_CN:
6103         case CMD_ABORT_XRI_CN:
6104         case CMD_ABORT_XRI_CX:
6105                 /* words 0-2 memcpy should be 0 rserved */
6106                 /* port will send abts */
6107                 if (iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
6108                         /*
6109                          * The link is down so the fw does not need to send abts
6110                          * on the wire.
6111                          */
6112                         bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
6113                 else
6114                         bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
6115                 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
6116                 wqe->words[5] = 0;
6117                 bf_set(lpfc_wqe_gen_ct, &wqe->generic,
6118                         ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
6119                 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
6120                 /*
6121                  * The abort handler will send us CMD_ABORT_XRI_CN or
6122                  * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
6123                  */
6124                 bf_set(lpfc_wqe_gen_command, &wqe->generic, CMD_ABORT_XRI_CX);
6125                 cmnd = CMD_ABORT_XRI_CX;
6126                 command_type = OTHER_COMMAND;
6127                 xritag = 0;
6128         break;
6129         case CMD_XMIT_BLS_RSP64_CX:
6130                 /* As BLS ABTS-ACC WQE is very different from other WQEs,
6131                  * we re-construct this WQE here based on information in
6132                  * iocbq from scratch.
6133                  */
6134                 memset(wqe, 0, sizeof(union lpfc_wqe));
6135                 /* OX_ID is invariable to who sent ABTS to CT exchange */
6136                 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
6137                        bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_acc));
6138                 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_acc) ==
6139                     LPFC_ABTS_UNSOL_INT) {
6140                         /* ABTS sent by initiator to CT exchange, the
6141                          * RX_ID field will be filled with the newly
6142                          * allocated responder XRI.
6143                          */
6144                         bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
6145                                iocbq->sli4_xritag);
6146                 } else {
6147                         /* ABTS sent by responder to CT exchange, the
6148                          * RX_ID field will be filled with the responder
6149                          * RX_ID from ABTS.
6150                          */
6151                         bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
6152                                bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_acc));
6153                 }
6154                 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
6155                 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
6156                 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
6157                        iocbq->iocb.ulpContext);
6158                 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
6159                 command_type = OTHER_COMMAND;
6160         break;
6161         case CMD_XRI_ABORTED_CX:
6162         case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
6163                 /* words0-2 are all 0's no bde */
6164                 /* word3 and word4 are rsvrd */
6165                 wqe->words[3] = 0;
6166                 wqe->words[4] = 0;
6167                 /* word5 iocb=rsvd wge=did */
6168                 /* There is no remote port id in the IOCB? */
6169                 /* Let this fall through and fail */
6170         case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
6171         case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
6172         case CMD_FCP_TRSP64_CX: /* Target mode rcv */
6173         case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
6174         default:
6175                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6176                                 "2014 Invalid command 0x%x\n",
6177                                 iocbq->iocb.ulpCommand);
6178                 return IOCB_ERROR;
6179         break;
6180
6181         }
6182         bf_set(lpfc_wqe_gen_xri, &wqe->generic, xritag);
6183         bf_set(lpfc_wqe_gen_request_tag, &wqe->generic, iocbq->iotag);
6184         wqe->generic.abort_tag = abort_tag;
6185         bf_set(lpfc_wqe_gen_cmd_type, &wqe->generic, command_type);
6186         bf_set(lpfc_wqe_gen_command, &wqe->generic, cmnd);
6187         bf_set(lpfc_wqe_gen_class, &wqe->generic, iocbq->iocb.ulpClass);
6188         bf_set(lpfc_wqe_gen_cq_id, &wqe->generic, LPFC_WQE_CQ_ID_DEFAULT);
6189
6190         return 0;
6191 }
6192
6193 /**
6194  * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
6195  * @phba: Pointer to HBA context object.
6196  * @ring_number: SLI ring number to issue iocb on.
6197  * @piocb: Pointer to command iocb.
6198  * @flag: Flag indicating if this command can be put into txq.
6199  *
6200  * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
6201  * an iocb command to an HBA with SLI-4 interface spec.
6202  *
6203  * This function is called with hbalock held. The function will return success
6204  * after it successfully submit the iocb to firmware or after adding to the
6205  * txq.
6206  **/
6207 static int
6208 __lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
6209                          struct lpfc_iocbq *piocb, uint32_t flag)
6210 {
6211         struct lpfc_sglq *sglq;
6212         uint16_t xritag;
6213         union lpfc_wqe wqe;
6214         struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
6215
6216         if (piocb->sli4_xritag == NO_XRI) {
6217                 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
6218                     piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
6219                         sglq = NULL;
6220                 else {
6221                         sglq = __lpfc_sli_get_sglq(phba);
6222                         if (!sglq)
6223                                 return IOCB_ERROR;
6224                         piocb->sli4_xritag = sglq->sli4_xritag;
6225                 }
6226         } else if (piocb->iocb_flag &  LPFC_IO_FCP) {
6227                 sglq = NULL; /* These IO's already have an XRI and
6228                               * a mapped sgl.
6229                               */
6230         } else {
6231                 /* This is a continuation of a commandi,(CX) so this
6232                  * sglq is on the active list
6233                  */
6234                 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_xritag);
6235                 if (!sglq)
6236                         return IOCB_ERROR;
6237         }
6238
6239         if (sglq) {
6240                 xritag = lpfc_sli4_bpl2sgl(phba, piocb, sglq);
6241                 if (xritag != sglq->sli4_xritag)
6242                         return IOCB_ERROR;
6243         }
6244
6245         if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
6246                 return IOCB_ERROR;
6247
6248         if ((piocb->iocb_flag & LPFC_IO_FCP) ||
6249                 (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
6250                 /*
6251                  * For FCP command IOCB, get a new WQ index to distribute
6252                  * WQE across the WQsr. On the other hand, for abort IOCB,
6253                  * it carries the same WQ index to the original command
6254                  * IOCB.
6255                  */
6256                 if (piocb->iocb_flag & LPFC_IO_FCP)
6257                         piocb->fcp_wqidx = lpfc_sli4_scmd_to_wqidx_distr(phba);
6258                 if (lpfc_sli4_wq_put(phba->sli4_hba.fcp_wq[piocb->fcp_wqidx],
6259                                      &wqe))
6260                         return IOCB_ERROR;
6261         } else {
6262                 if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
6263                         return IOCB_ERROR;
6264         }
6265         lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
6266
6267         return 0;
6268 }
6269
6270 /**
6271  * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
6272  *
6273  * This routine wraps the actual lockless version for issusing IOCB function
6274  * pointer from the lpfc_hba struct.
6275  *
6276  * Return codes:
6277  *      IOCB_ERROR - Error
6278  *      IOCB_SUCCESS - Success
6279  *      IOCB_BUSY - Busy
6280  **/
6281 static inline int
6282 __lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
6283                 struct lpfc_iocbq *piocb, uint32_t flag)
6284 {
6285         return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
6286 }
6287
6288 /**
6289  * lpfc_sli_api_table_setup - Set up sli api fucntion jump table
6290  * @phba: The hba struct for which this call is being executed.
6291  * @dev_grp: The HBA PCI-Device group number.
6292  *
6293  * This routine sets up the SLI interface API function jump table in @phba
6294  * struct.
6295  * Returns: 0 - success, -ENODEV - failure.
6296  **/
6297 int
6298 lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
6299 {
6300
6301         switch (dev_grp) {
6302         case LPFC_PCI_DEV_LP:
6303                 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
6304                 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
6305                 break;
6306         case LPFC_PCI_DEV_OC:
6307                 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
6308                 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
6309                 break;
6310         default:
6311                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6312                                 "1419 Invalid HBA PCI-device group: 0x%x\n",
6313                                 dev_grp);
6314                 return -ENODEV;
6315                 break;
6316         }
6317         phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
6318         return 0;
6319 }
6320
6321 /**
6322  * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
6323  * @phba: Pointer to HBA context object.
6324  * @pring: Pointer to driver SLI ring object.
6325  * @piocb: Pointer to command iocb.
6326  * @flag: Flag indicating if this command can be put into txq.
6327  *
6328  * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
6329  * function. This function gets the hbalock and calls
6330  * __lpfc_sli_issue_iocb function and will return the error returned
6331  * by __lpfc_sli_issue_iocb function. This wrapper is used by
6332  * functions which do not hold hbalock.
6333  **/
6334 int
6335 lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
6336                     struct lpfc_iocbq *piocb, uint32_t flag)
6337 {
6338         unsigned long iflags;
6339         int rc;
6340
6341         spin_lock_irqsave(&phba->hbalock, iflags);
6342         rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
6343         spin_unlock_irqrestore(&phba->hbalock, iflags);
6344
6345         return rc;
6346 }
6347
6348 /**
6349  * lpfc_extra_ring_setup - Extra ring setup function
6350  * @phba: Pointer to HBA context object.
6351  *
6352  * This function is called while driver attaches with the
6353  * HBA to setup the extra ring. The extra ring is used
6354  * only when driver needs to support target mode functionality
6355  * or IP over FC functionalities.
6356  *
6357  * This function is called with no lock held.
6358  **/
6359 static int
6360 lpfc_extra_ring_setup( struct lpfc_hba *phba)
6361 {
6362         struct lpfc_sli *psli;
6363         struct lpfc_sli_ring *pring;
6364
6365         psli = &phba->sli;
6366
6367         /* Adjust cmd/rsp ring iocb entries more evenly */
6368
6369         /* Take some away from the FCP ring */
6370         pring = &psli->ring[psli->fcp_ring];
6371         pring->numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
6372         pring->numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
6373         pring->numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
6374         pring->numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
6375
6376         /* and give them to the extra ring */
6377         pring = &psli->ring[psli->extra_ring];
6378
6379         pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
6380         pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
6381         pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
6382         pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
6383
6384         /* Setup default profile for this ring */
6385         pring->iotag_max = 4096;
6386         pring->num_mask = 1;
6387         pring->prt[0].profile = 0;      /* Mask 0 */
6388         pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
6389         pring->prt[0].type = phba->cfg_multi_ring_type;
6390         pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
6391         return 0;
6392 }
6393
6394 /**
6395  * lpfc_sli_async_event_handler - ASYNC iocb handler function
6396  * @phba: Pointer to HBA context object.
6397  * @pring: Pointer to driver SLI ring object.
6398  * @iocbq: Pointer to iocb object.
6399  *
6400  * This function is called by the slow ring event handler
6401  * function when there is an ASYNC event iocb in the ring.
6402  * This function is called with no lock held.
6403  * Currently this function handles only temperature related
6404  * ASYNC events. The function decodes the temperature sensor
6405  * event message and posts events for the management applications.
6406  **/
6407 static void
6408 lpfc_sli_async_event_handler(struct lpfc_hba * phba,
6409         struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
6410 {
6411         IOCB_t *icmd;
6412         uint16_t evt_code;
6413         uint16_t temp;
6414         struct temp_event temp_event_data;
6415         struct Scsi_Host *shost;
6416         uint32_t *iocb_w;
6417
6418         icmd = &iocbq->iocb;
6419         evt_code = icmd->un.asyncstat.evt_code;
6420         temp = icmd->ulpContext;
6421
6422         if ((evt_code != ASYNC_TEMP_WARN) &&
6423                 (evt_code != ASYNC_TEMP_SAFE)) {
6424                 iocb_w = (uint32_t *) icmd;
6425                 lpfc_printf_log(phba,
6426                         KERN_ERR,
6427                         LOG_SLI,
6428                         "0346 Ring %d handler: unexpected ASYNC_STATUS"
6429                         " evt_code 0x%x\n"
6430                         "W0  0x%08x W1  0x%08x W2  0x%08x W3  0x%08x\n"
6431                         "W4  0x%08x W5  0x%08x W6  0x%08x W7  0x%08x\n"
6432                         "W8  0x%08x W9  0x%08x W10 0x%08x W11 0x%08x\n"
6433                         "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
6434                         pring->ringno,
6435                         icmd->un.asyncstat.evt_code,
6436                         iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
6437                         iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
6438                         iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
6439                         iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
6440
6441                 return;
6442         }
6443         temp_event_data.data = (uint32_t)temp;
6444         temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
6445         if (evt_code == ASYNC_TEMP_WARN) {
6446                 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
6447                 lpfc_printf_log(phba,
6448                                 KERN_ERR,
6449                                 LOG_TEMP,
6450                                 "0347 Adapter is very hot, please take "
6451                                 "corrective action. temperature : %d Celsius\n",
6452                                 temp);
6453         }
6454         if (evt_code == ASYNC_TEMP_SAFE) {
6455                 temp_event_data.event_code = LPFC_NORMAL_TEMP;
6456                 lpfc_printf_log(phba,
6457                                 KERN_ERR,
6458                                 LOG_TEMP,
6459                                 "0340 Adapter temperature is OK now. "
6460                                 "temperature : %d Celsius\n",
6461                                 temp);
6462         }
6463
6464         /* Send temperature change event to applications */
6465         shost = lpfc_shost_from_vport(phba->pport);
6466         fc_host_post_vendor_event(shost, fc_get_event_number(),
6467                 sizeof(temp_event_data), (char *) &temp_event_data,
6468                 LPFC_NL_VENDOR_ID);
6469
6470 }
6471
6472
6473 /**
6474  * lpfc_sli_setup - SLI ring setup function
6475  * @phba: Pointer to HBA context object.
6476  *
6477  * lpfc_sli_setup sets up rings of the SLI interface with
6478  * number of iocbs per ring and iotags. This function is
6479  * called while driver attach to the HBA and before the
6480  * interrupts are enabled. So there is no need for locking.
6481  *
6482  * This function always returns 0.
6483  **/
6484 int
6485 lpfc_sli_setup(struct lpfc_hba *phba)
6486 {
6487         int i, totiocbsize = 0;
6488         struct lpfc_sli *psli = &phba->sli;
6489         struct lpfc_sli_ring *pring;
6490
6491         psli->num_rings = MAX_CONFIGURED_RINGS;
6492         psli->sli_flag = 0;
6493         psli->fcp_ring = LPFC_FCP_RING;
6494         psli->next_ring = LPFC_FCP_NEXT_RING;
6495         psli->extra_ring = LPFC_EXTRA_RING;
6496
6497         psli->iocbq_lookup = NULL;
6498         psli->iocbq_lookup_len = 0;
6499         psli->last_iotag = 0;
6500
6501         for (i = 0; i < psli->num_rings; i++) {
6502                 pring = &psli->ring[i];
6503                 switch (i) {
6504                 case LPFC_FCP_RING:     /* ring 0 - FCP */
6505                         /* numCiocb and numRiocb are used in config_port */
6506                         pring->numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
6507                         pring->numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
6508                         pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
6509                         pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
6510                         pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
6511                         pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
6512                         pring->sizeCiocb = (phba->sli_rev == 3) ?
6513                                                         SLI3_IOCB_CMD_SIZE :
6514                                                         SLI2_IOCB_CMD_SIZE;
6515                         pring->sizeRiocb = (phba->sli_rev == 3) ?
6516                                                         SLI3_IOCB_RSP_SIZE :
6517                                                         SLI2_IOCB_RSP_SIZE;
6518                         pring->iotag_ctr = 0;
6519                         pring->iotag_max =
6520                             (phba->cfg_hba_queue_depth * 2);
6521                         pring->fast_iotag = pring->iotag_max;
6522                         pring->num_mask = 0;
6523                         break;
6524                 case LPFC_EXTRA_RING:   /* ring 1 - EXTRA */
6525                         /* numCiocb and numRiocb are used in config_port */
6526                         pring->numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
6527                         pring->numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
6528                         pring->sizeCiocb = (phba->sli_rev == 3) ?
6529                                                         SLI3_IOCB_CMD_SIZE :
6530                                                         SLI2_IOCB_CMD_SIZE;
6531                         pring->sizeRiocb = (phba->sli_rev == 3) ?
6532                                                         SLI3_IOCB_RSP_SIZE :
6533                                                         SLI2_IOCB_RSP_SIZE;
6534                         pring->iotag_max = phba->cfg_hba_queue_depth;
6535                         pring->num_mask = 0;
6536                         break;
6537                 case LPFC_ELS_RING:     /* ring 2 - ELS / CT */
6538                         /* numCiocb and numRiocb are used in config_port */
6539                         pring->numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
6540                         pring->numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
6541                         pring->sizeCiocb = (phba->sli_rev == 3) ?
6542                                                         SLI3_IOCB_CMD_SIZE :
6543                                                         SLI2_IOCB_CMD_SIZE;
6544                         pring->sizeRiocb = (phba->sli_rev == 3) ?
6545                                                         SLI3_IOCB_RSP_SIZE :
6546                                                         SLI2_IOCB_RSP_SIZE;
6547                         pring->fast_iotag = 0;
6548                         pring->iotag_ctr = 0;
6549                         pring->iotag_max = 4096;
6550                         pring->lpfc_sli_rcv_async_status =
6551                                 lpfc_sli_async_event_handler;
6552                         pring->num_mask = LPFC_MAX_RING_MASK;
6553                         pring->prt[0].profile = 0;      /* Mask 0 */
6554                         pring->prt[0].rctl = FC_RCTL_ELS_REQ;
6555                         pring->prt[0].type = FC_TYPE_ELS;
6556                         pring->prt[0].lpfc_sli_rcv_unsol_event =
6557                             lpfc_els_unsol_event;
6558                         pring->prt[1].profile = 0;      /* Mask 1 */
6559                         pring->prt[1].rctl = FC_RCTL_ELS_REP;
6560                         pring->prt[1].type = FC_TYPE_ELS;
6561                         pring->prt[1].lpfc_sli_rcv_unsol_event =
6562                             lpfc_els_unsol_event;
6563                         pring->prt[2].profile = 0;      /* Mask 2 */
6564                         /* NameServer Inquiry */
6565                         pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
6566                         /* NameServer */
6567                         pring->prt[2].type = FC_TYPE_CT;
6568                         pring->prt[2].lpfc_sli_rcv_unsol_event =
6569                             lpfc_ct_unsol_event;
6570                         pring->prt[3].profile = 0;      /* Mask 3 */
6571                         /* NameServer response */
6572                         pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
6573                         /* NameServer */
6574                         pring->prt[3].type = FC_TYPE_CT;
6575                         pring->prt[3].lpfc_sli_rcv_unsol_event =
6576                             lpfc_ct_unsol_event;
6577                         /* abort unsolicited sequence */
6578                         pring->prt[4].profile = 0;      /* Mask 4 */
6579                         pring->prt[4].rctl = FC_RCTL_BA_ABTS;
6580                         pring->prt[4].type = FC_TYPE_BLS;
6581                         pring->prt[4].lpfc_sli_rcv_unsol_event =
6582                             lpfc_sli4_ct_abort_unsol_event;
6583                         break;
6584                 }
6585                 totiocbsize += (pring->numCiocb * pring->sizeCiocb) +
6586                                 (pring->numRiocb * pring->sizeRiocb);
6587         }
6588         if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
6589                 /* Too many cmd / rsp ring entries in SLI2 SLIM */
6590                 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
6591                        "SLI2 SLIM Data: x%x x%lx\n",
6592                        phba->brd_no, totiocbsize,
6593                        (unsigned long) MAX_SLIM_IOCB_SIZE);
6594         }
6595         if (phba->cfg_multi_ring_support == 2)
6596                 lpfc_extra_ring_setup(phba);
6597
6598         return 0;
6599 }
6600
6601 /**
6602  * lpfc_sli_queue_setup - Queue initialization function
6603  * @phba: Pointer to HBA context object.
6604  *
6605  * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
6606  * ring. This function also initializes ring indices of each ring.
6607  * This function is called during the initialization of the SLI
6608  * interface of an HBA.
6609  * This function is called with no lock held and always returns
6610  * 1.
6611  **/
6612 int
6613 lpfc_sli_queue_setup(struct lpfc_hba *phba)
6614 {
6615         struct lpfc_sli *psli;
6616         struct lpfc_sli_ring *pring;
6617         int i;
6618
6619         psli = &phba->sli;
6620         spin_lock_irq(&phba->hbalock);
6621         INIT_LIST_HEAD(&psli->mboxq);
6622         INIT_LIST_HEAD(&psli->mboxq_cmpl);
6623         /* Initialize list headers for txq and txcmplq as double linked lists */
6624         for (i = 0; i < psli->num_rings; i++) {
6625                 pring = &psli->ring[i];
6626                 pring->ringno = i;
6627                 pring->next_cmdidx  = 0;
6628                 pring->local_getidx = 0;
6629                 pring->cmdidx = 0;
6630                 INIT_LIST_HEAD(&pring->txq);
6631                 INIT_LIST_HEAD(&pring->txcmplq);
6632                 INIT_LIST_HEAD(&pring->iocb_continueq);
6633                 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
6634                 INIT_LIST_HEAD(&pring->postbufq);
6635         }
6636         spin_unlock_irq(&phba->hbalock);
6637         return 1;
6638 }
6639
6640 /**
6641  * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
6642  * @phba: Pointer to HBA context object.
6643  *
6644  * This routine flushes the mailbox command subsystem. It will unconditionally
6645  * flush all the mailbox commands in the three possible stages in the mailbox
6646  * command sub-system: pending mailbox command queue; the outstanding mailbox
6647  * command; and completed mailbox command queue. It is caller's responsibility
6648  * to make sure that the driver is in the proper state to flush the mailbox
6649  * command sub-system. Namely, the posting of mailbox commands into the
6650  * pending mailbox command queue from the various clients must be stopped;
6651  * either the HBA is in a state that it will never works on the outstanding
6652  * mailbox command (such as in EEH or ERATT conditions) or the outstanding
6653  * mailbox command has been completed.
6654  **/
6655 static void
6656 lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
6657 {
6658         LIST_HEAD(completions);
6659         struct lpfc_sli *psli = &phba->sli;
6660         LPFC_MBOXQ_t *pmb;
6661         unsigned long iflag;
6662
6663         /* Flush all the mailbox commands in the mbox system */
6664         spin_lock_irqsave(&phba->hbalock, iflag);
6665         /* The pending mailbox command queue */
6666         list_splice_init(&phba->sli.mboxq, &completions);
6667         /* The outstanding active mailbox command */
6668         if (psli->mbox_active) {
6669                 list_add_tail(&psli->mbox_active->list, &completions);
6670                 psli->mbox_active = NULL;
6671                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
6672         }
6673         /* The completed mailbox command queue */
6674         list_splice_init(&phba->sli.mboxq_cmpl, &completions);
6675         spin_unlock_irqrestore(&phba->hbalock, iflag);
6676
6677         /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
6678         while (!list_empty(&completions)) {
6679                 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
6680                 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
6681                 if (pmb->mbox_cmpl)
6682                         pmb->mbox_cmpl(phba, pmb);
6683         }
6684 }
6685
6686 /**
6687  * lpfc_sli_host_down - Vport cleanup function
6688  * @vport: Pointer to virtual port object.
6689  *
6690  * lpfc_sli_host_down is called to clean up the resources
6691  * associated with a vport before destroying virtual
6692  * port data structures.
6693  * This function does following operations:
6694  * - Free discovery resources associated with this virtual
6695  *   port.
6696  * - Free iocbs associated with this virtual port in
6697  *   the txq.
6698  * - Send abort for all iocb commands associated with this
6699  *   vport in txcmplq.
6700  *
6701  * This function is called with no lock held and always returns 1.
6702  **/
6703 int
6704 lpfc_sli_host_down(struct lpfc_vport *vport)
6705 {
6706         LIST_HEAD(completions);
6707         struct lpfc_hba *phba = vport->phba;
6708         struct lpfc_sli *psli = &phba->sli;
6709         struct lpfc_sli_ring *pring;
6710         struct lpfc_iocbq *iocb, *next_iocb;
6711         int i;
6712         unsigned long flags = 0;
6713         uint16_t prev_pring_flag;
6714
6715         lpfc_cleanup_discovery_resources(vport);
6716
6717         spin_lock_irqsave(&phba->hbalock, flags);
6718         for (i = 0; i < psli->num_rings; i++) {
6719                 pring = &psli->ring[i];
6720                 prev_pring_flag = pring->flag;
6721                 /* Only slow rings */
6722                 if (pring->ringno == LPFC_ELS_RING) {
6723                         pring->flag |= LPFC_DEFERRED_RING_EVENT;
6724                         /* Set the lpfc data pending flag */
6725                         set_bit(LPFC_DATA_READY, &phba->data_flags);
6726                 }
6727                 /*
6728                  * Error everything on the txq since these iocbs have not been
6729                  * given to the FW yet.
6730                  */
6731                 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
6732                         if (iocb->vport != vport)
6733                                 continue;
6734                         list_move_tail(&iocb->list, &completions);
6735                         pring->txq_cnt--;
6736                 }
6737
6738                 /* Next issue ABTS for everything on the txcmplq */
6739                 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
6740                                                                         list) {
6741                         if (iocb->vport != vport)
6742                                 continue;
6743                         lpfc_sli_issue_abort_iotag(phba, pring, iocb);
6744                 }
6745
6746                 pring->flag = prev_pring_flag;
6747         }
6748
6749         spin_unlock_irqrestore(&phba->hbalock, flags);
6750
6751         /* Cancel all the IOCBs from the completions list */
6752         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
6753                               IOERR_SLI_DOWN);
6754         return 1;
6755 }
6756
6757 /**
6758  * lpfc_sli_hba_down - Resource cleanup function for the HBA
6759  * @phba: Pointer to HBA context object.
6760  *
6761  * This function cleans up all iocb, buffers, mailbox commands
6762  * while shutting down the HBA. This function is called with no
6763  * lock held and always returns 1.
6764  * This function does the following to cleanup driver resources:
6765  * - Free discovery resources for each virtual port
6766  * - Cleanup any pending fabric iocbs
6767  * - Iterate through the iocb txq and free each entry
6768  *   in the list.
6769  * - Free up any buffer posted to the HBA
6770  * - Free mailbox commands in the mailbox queue.
6771  **/
6772 int
6773 lpfc_sli_hba_down(struct lpfc_hba *phba)
6774 {
6775         LIST_HEAD(completions);
6776         struct lpfc_sli *psli = &phba->sli;
6777         struct lpfc_sli_ring *pring;
6778         struct lpfc_dmabuf *buf_ptr;
6779         unsigned long flags = 0;
6780         int i;
6781
6782         /* Shutdown the mailbox command sub-system */
6783         lpfc_sli_mbox_sys_shutdown(phba);
6784
6785         lpfc_hba_down_prep(phba);
6786
6787         lpfc_fabric_abort_hba(phba);
6788
6789         spin_lock_irqsave(&phba->hbalock, flags);
6790         for (i = 0; i < psli->num_rings; i++) {
6791                 pring = &psli->ring[i];
6792                 /* Only slow rings */
6793                 if (pring->ringno == LPFC_ELS_RING) {
6794                         pring->flag |= LPFC_DEFERRED_RING_EVENT;
6795                         /* Set the lpfc data pending flag */
6796                         set_bit(LPFC_DATA_READY, &phba->data_flags);
6797                 }
6798
6799                 /*
6800                  * Error everything on the txq since these iocbs have not been
6801                  * given to the FW yet.
6802                  */
6803                 list_splice_init(&pring->txq, &completions);
6804                 pring->txq_cnt = 0;
6805
6806         }
6807         spin_unlock_irqrestore(&phba->hbalock, flags);
6808
6809         /* Cancel all the IOCBs from the completions list */
6810         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
6811                               IOERR_SLI_DOWN);
6812
6813         spin_lock_irqsave(&phba->hbalock, flags);
6814         list_splice_init(&phba->elsbuf, &completions);
6815         phba->elsbuf_cnt = 0;
6816         phba->elsbuf_prev_cnt = 0;
6817         spin_unlock_irqrestore(&phba->hbalock, flags);
6818
6819         while (!list_empty(&completions)) {
6820                 list_remove_head(&completions, buf_ptr,
6821                         struct lpfc_dmabuf, list);
6822                 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
6823                 kfree(buf_ptr);
6824         }
6825
6826         /* Return any active mbox cmds */
6827         del_timer_sync(&psli->mbox_tmo);
6828
6829         spin_lock_irqsave(&phba->pport->work_port_lock, flags);
6830         phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
6831         spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
6832
6833         return 1;
6834 }
6835
6836 /**
6837  * lpfc_sli4_hba_down - PCI function resource cleanup for the SLI4 HBA
6838  * @phba: Pointer to HBA context object.
6839  *
6840  * This function cleans up all queues, iocb, buffers, mailbox commands while
6841  * shutting down the SLI4 HBA FCoE function. This function is called with no
6842  * lock held and always returns 1.
6843  *
6844  * This function does the following to cleanup driver FCoE function resources:
6845  * - Free discovery resources for each virtual port
6846  * - Cleanup any pending fabric iocbs
6847  * - Iterate through the iocb txq and free each entry in the list.
6848  * - Free up any buffer posted to the HBA.
6849  * - Clean up all the queue entries: WQ, RQ, MQ, EQ, CQ, etc.
6850  * - Free mailbox commands in the mailbox queue.
6851  **/
6852 int
6853 lpfc_sli4_hba_down(struct lpfc_hba *phba)
6854 {
6855         /* Stop the SLI4 device port */
6856         lpfc_stop_port(phba);
6857
6858         /* Tear down the queues in the HBA */
6859         lpfc_sli4_queue_unset(phba);
6860
6861         /* unregister default FCFI from the HBA */
6862         lpfc_sli4_fcfi_unreg(phba, phba->fcf.fcfi);
6863
6864         return 1;
6865 }
6866
6867 /**
6868  * lpfc_sli_pcimem_bcopy - SLI memory copy function
6869  * @srcp: Source memory pointer.
6870  * @destp: Destination memory pointer.
6871  * @cnt: Number of words required to be copied.
6872  *
6873  * This function is used for copying data between driver memory
6874  * and the SLI memory. This function also changes the endianness
6875  * of each word if native endianness is different from SLI
6876  * endianness. This function can be called with or without
6877  * lock.
6878  **/
6879 void
6880 lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
6881 {
6882         uint32_t *src = srcp;
6883         uint32_t *dest = destp;
6884         uint32_t ldata;
6885         int i;
6886
6887         for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
6888                 ldata = *src;
6889                 ldata = le32_to_cpu(ldata);
6890                 *dest = ldata;
6891                 src++;
6892                 dest++;
6893         }
6894 }
6895
6896
6897 /**
6898  * lpfc_sli_bemem_bcopy - SLI memory copy function
6899  * @srcp: Source memory pointer.
6900  * @destp: Destination memory pointer.
6901  * @cnt: Number of words required to be copied.
6902  *
6903  * This function is used for copying data between a data structure
6904  * with big endian representation to local endianness.
6905  * This function can be called with or without lock.
6906  **/
6907 void
6908 lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
6909 {
6910         uint32_t *src = srcp;
6911         uint32_t *dest = destp;
6912         uint32_t ldata;
6913         int i;
6914
6915         for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
6916                 ldata = *src;
6917                 ldata = be32_to_cpu(ldata);
6918                 *dest = ldata;
6919                 src++;
6920                 dest++;
6921         }
6922 }
6923
6924 /**
6925  * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
6926  * @phba: Pointer to HBA context object.
6927  * @pring: Pointer to driver SLI ring object.
6928  * @mp: Pointer to driver buffer object.
6929  *
6930  * This function is called with no lock held.
6931  * It always return zero after adding the buffer to the postbufq
6932  * buffer list.
6933  **/
6934 int
6935 lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
6936                          struct lpfc_dmabuf *mp)
6937 {
6938         /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
6939            later */
6940         spin_lock_irq(&phba->hbalock);
6941         list_add_tail(&mp->list, &pring->postbufq);
6942         pring->postbufq_cnt++;
6943         spin_unlock_irq(&phba->hbalock);
6944         return 0;
6945 }
6946
6947 /**
6948  * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
6949  * @phba: Pointer to HBA context object.
6950  *
6951  * When HBQ is enabled, buffers are searched based on tags. This function
6952  * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
6953  * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
6954  * does not conflict with tags of buffer posted for unsolicited events.
6955  * The function returns the allocated tag. The function is called with
6956  * no locks held.
6957  **/
6958 uint32_t
6959 lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
6960 {
6961         spin_lock_irq(&phba->hbalock);
6962         phba->buffer_tag_count++;
6963         /*
6964          * Always set the QUE_BUFTAG_BIT to distiguish between
6965          * a tag assigned by HBQ.
6966          */
6967         phba->buffer_tag_count |= QUE_BUFTAG_BIT;
6968         spin_unlock_irq(&phba->hbalock);
6969         return phba->buffer_tag_count;
6970 }
6971
6972 /**
6973  * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
6974  * @phba: Pointer to HBA context object.
6975  * @pring: Pointer to driver SLI ring object.
6976  * @tag: Buffer tag.
6977  *
6978  * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
6979  * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
6980  * iocb is posted to the response ring with the tag of the buffer.
6981  * This function searches the pring->postbufq list using the tag
6982  * to find buffer associated with CMD_IOCB_RET_XRI64_CX
6983  * iocb. If the buffer is found then lpfc_dmabuf object of the
6984  * buffer is returned to the caller else NULL is returned.
6985  * This function is called with no lock held.
6986  **/
6987 struct lpfc_dmabuf *
6988 lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
6989                         uint32_t tag)
6990 {
6991         struct lpfc_dmabuf *mp, *next_mp;
6992         struct list_head *slp = &pring->postbufq;
6993
6994         /* Search postbufq, from the begining, looking for a match on tag */
6995         spin_lock_irq(&phba->hbalock);
6996         list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
6997                 if (mp->buffer_tag == tag) {
6998                         list_del_init(&mp->list);
6999                         pring->postbufq_cnt--;
7000                         spin_unlock_irq(&phba->hbalock);
7001                         return mp;
7002                 }
7003         }
7004
7005         spin_unlock_irq(&phba->hbalock);
7006         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7007                         "0402 Cannot find virtual addr for buffer tag on "
7008                         "ring %d Data x%lx x%p x%p x%x\n",
7009                         pring->ringno, (unsigned long) tag,
7010                         slp->next, slp->prev, pring->postbufq_cnt);
7011
7012         return NULL;
7013 }
7014
7015 /**
7016  * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
7017  * @phba: Pointer to HBA context object.
7018  * @pring: Pointer to driver SLI ring object.
7019  * @phys: DMA address of the buffer.
7020  *
7021  * This function searches the buffer list using the dma_address
7022  * of unsolicited event to find the driver's lpfc_dmabuf object
7023  * corresponding to the dma_address. The function returns the
7024  * lpfc_dmabuf object if a buffer is found else it returns NULL.
7025  * This function is called by the ct and els unsolicited event
7026  * handlers to get the buffer associated with the unsolicited
7027  * event.
7028  *
7029  * This function is called with no lock held.
7030  **/
7031 struct lpfc_dmabuf *
7032 lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7033                          dma_addr_t phys)
7034 {
7035         struct lpfc_dmabuf *mp, *next_mp;
7036         struct list_head *slp = &pring->postbufq;
7037
7038         /* Search postbufq, from the begining, looking for a match on phys */
7039         spin_lock_irq(&phba->hbalock);
7040         list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
7041                 if (mp->phys == phys) {
7042                         list_del_init(&mp->list);
7043                         pring->postbufq_cnt--;
7044                         spin_unlock_irq(&phba->hbalock);
7045                         return mp;
7046                 }
7047         }
7048
7049         spin_unlock_irq(&phba->hbalock);
7050         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7051                         "0410 Cannot find virtual addr for mapped buf on "
7052                         "ring %d Data x%llx x%p x%p x%x\n",
7053                         pring->ringno, (unsigned long long)phys,
7054                         slp->next, slp->prev, pring->postbufq_cnt);
7055         return NULL;
7056 }
7057
7058 /**
7059  * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
7060  * @phba: Pointer to HBA context object.
7061  * @cmdiocb: Pointer to driver command iocb object.
7062  * @rspiocb: Pointer to driver response iocb object.
7063  *
7064  * This function is the completion handler for the abort iocbs for
7065  * ELS commands. This function is called from the ELS ring event
7066  * handler with no lock held. This function frees memory resources
7067  * associated with the abort iocb.
7068  **/
7069 static void
7070 lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7071                         struct lpfc_iocbq *rspiocb)
7072 {
7073         IOCB_t *irsp = &rspiocb->iocb;
7074         uint16_t abort_iotag, abort_context;
7075         struct lpfc_iocbq *abort_iocb;
7076         struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
7077
7078         abort_iocb = NULL;
7079
7080         if (irsp->ulpStatus) {
7081                 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
7082                 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
7083
7084                 spin_lock_irq(&phba->hbalock);
7085                 if (phba->sli_rev < LPFC_SLI_REV4) {
7086                         if (abort_iotag != 0 &&
7087                                 abort_iotag <= phba->sli.last_iotag)
7088                                 abort_iocb =
7089                                         phba->sli.iocbq_lookup[abort_iotag];
7090                 } else
7091                         /* For sli4 the abort_tag is the XRI,
7092                          * so the abort routine puts the iotag  of the iocb
7093                          * being aborted in the context field of the abort
7094                          * IOCB.
7095                          */
7096                         abort_iocb = phba->sli.iocbq_lookup[abort_context];
7097
7098                 lpfc_printf_log(phba, KERN_INFO, LOG_ELS | LOG_SLI,
7099                                 "0327 Cannot abort els iocb %p "
7100                                 "with tag %x context %x, abort status %x, "
7101                                 "abort code %x\n",
7102                                 abort_iocb, abort_iotag, abort_context,
7103                                 irsp->ulpStatus, irsp->un.ulpWord[4]);
7104
7105                 /*
7106                  *  If the iocb is not found in Firmware queue the iocb
7107                  *  might have completed already. Do not free it again.
7108                  */
7109                 if (irsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
7110                         if (irsp->un.ulpWord[4] != IOERR_NO_XRI) {
7111                                 spin_unlock_irq(&phba->hbalock);
7112                                 lpfc_sli_release_iocbq(phba, cmdiocb);
7113                                 return;
7114                         }
7115                         /* For SLI4 the ulpContext field for abort IOCB
7116                          * holds the iotag of the IOCB being aborted so
7117                          * the local abort_context needs to be reset to
7118                          * match the aborted IOCBs ulpContext.
7119                          */
7120                         if (abort_iocb && phba->sli_rev == LPFC_SLI_REV4)
7121                                 abort_context = abort_iocb->iocb.ulpContext;
7122                 }
7123                 /*
7124                  * make sure we have the right iocbq before taking it
7125                  * off the txcmplq and try to call completion routine.
7126                  */
7127                 if (!abort_iocb ||
7128                     abort_iocb->iocb.ulpContext != abort_context ||
7129                     (abort_iocb->iocb_flag & LPFC_DRIVER_ABORTED) == 0)
7130                         spin_unlock_irq(&phba->hbalock);
7131                 else if (phba->sli_rev < LPFC_SLI_REV4) {
7132                         /*
7133                          * leave the SLI4 aborted command on the txcmplq
7134                          * list and the command complete WCQE's XB bit
7135                          * will tell whether the SGL (XRI) can be released
7136                          * immediately or to the aborted SGL list for the
7137                          * following abort XRI from the HBA.
7138                          */
7139                         list_del_init(&abort_iocb->list);
7140                         pring->txcmplq_cnt--;
7141
7142                         /* Firmware could still be in progress of DMAing
7143                          * payload, so don't free data buffer till after
7144                          * a hbeat.
7145                          */
7146                         abort_iocb->iocb_flag |= LPFC_DELAY_MEM_FREE;
7147                         abort_iocb->iocb_flag &= ~LPFC_DRIVER_ABORTED;
7148                         spin_unlock_irq(&phba->hbalock);
7149
7150                         abort_iocb->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
7151                         abort_iocb->iocb.un.ulpWord[4] = IOERR_ABORT_REQUESTED;
7152                         (abort_iocb->iocb_cmpl)(phba, abort_iocb, abort_iocb);
7153                 } else
7154                         spin_unlock_irq(&phba->hbalock);
7155         }
7156
7157         lpfc_sli_release_iocbq(phba, cmdiocb);
7158         return;
7159 }
7160
7161 /**
7162  * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
7163  * @phba: Pointer to HBA context object.
7164  * @cmdiocb: Pointer to driver command iocb object.
7165  * @rspiocb: Pointer to driver response iocb object.
7166  *
7167  * The function is called from SLI ring event handler with no
7168  * lock held. This function is the completion handler for ELS commands
7169  * which are aborted. The function frees memory resources used for
7170  * the aborted ELS commands.
7171  **/
7172 static void
7173 lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7174                      struct lpfc_iocbq *rspiocb)
7175 {
7176         IOCB_t *irsp = &rspiocb->iocb;
7177
7178         /* ELS cmd tag <ulpIoTag> completes */
7179         lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
7180                         "0139 Ignoring ELS cmd tag x%x completion Data: "
7181                         "x%x x%x x%x\n",
7182                         irsp->ulpIoTag, irsp->ulpStatus,
7183                         irsp->un.ulpWord[4], irsp->ulpTimeout);
7184         if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
7185                 lpfc_ct_free_iocb(phba, cmdiocb);
7186         else
7187                 lpfc_els_free_iocb(phba, cmdiocb);
7188         return;
7189 }
7190
7191 /**
7192  * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
7193  * @phba: Pointer to HBA context object.
7194  * @pring: Pointer to driver SLI ring object.
7195  * @cmdiocb: Pointer to driver command iocb object.
7196  *
7197  * This function issues an abort iocb for the provided command
7198  * iocb. This function is called with hbalock held.
7199  * The function returns 0 when it fails due to memory allocation
7200  * failure or when the command iocb is an abort request.
7201  **/
7202 int
7203 lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7204                            struct lpfc_iocbq *cmdiocb)
7205 {
7206         struct lpfc_vport *vport = cmdiocb->vport;
7207         struct lpfc_iocbq *abtsiocbp;
7208         IOCB_t *icmd = NULL;
7209         IOCB_t *iabt = NULL;
7210         int retval = IOCB_ERROR;
7211
7212         /*
7213          * There are certain command types we don't want to abort.  And we
7214          * don't want to abort commands that are already in the process of
7215          * being aborted.
7216          */
7217         icmd = &cmdiocb->iocb;
7218         if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
7219             icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
7220             (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
7221                 return 0;
7222
7223         /* If we're unloading, don't abort iocb on the ELS ring, but change the
7224          * callback so that nothing happens when it finishes.
7225          */
7226         if ((vport->load_flag & FC_UNLOADING) &&
7227             (pring->ringno == LPFC_ELS_RING)) {
7228                 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
7229                         cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
7230                 else
7231                         cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
7232                 goto abort_iotag_exit;
7233         }
7234
7235         /* issue ABTS for this IOCB based on iotag */
7236         abtsiocbp = __lpfc_sli_get_iocbq(phba);
7237         if (abtsiocbp == NULL)
7238                 return 0;
7239
7240         /* This signals the response to set the correct status
7241          * before calling the completion handler
7242          */
7243         cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
7244
7245         iabt = &abtsiocbp->iocb;
7246         iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
7247         iabt->un.acxri.abortContextTag = icmd->ulpContext;
7248         if (phba->sli_rev == LPFC_SLI_REV4) {
7249                 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
7250                 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
7251         }
7252         else
7253                 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
7254         iabt->ulpLe = 1;
7255         iabt->ulpClass = icmd->ulpClass;
7256
7257         /* ABTS WQE must go to the same WQ as the WQE to be aborted */
7258         abtsiocbp->fcp_wqidx = cmdiocb->fcp_wqidx;
7259         if (cmdiocb->iocb_flag & LPFC_IO_FCP)
7260                 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
7261
7262         if (phba->link_state >= LPFC_LINK_UP)
7263                 iabt->ulpCommand = CMD_ABORT_XRI_CN;
7264         else
7265                 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
7266
7267         abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
7268
7269         lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
7270                          "0339 Abort xri x%x, original iotag x%x, "
7271                          "abort cmd iotag x%x\n",
7272                          iabt->un.acxri.abortContextTag,
7273                          iabt->un.acxri.abortIoTag, abtsiocbp->iotag);
7274         retval = __lpfc_sli_issue_iocb(phba, pring->ringno, abtsiocbp, 0);
7275
7276         if (retval)
7277                 __lpfc_sli_release_iocbq(phba, abtsiocbp);
7278 abort_iotag_exit:
7279         /*
7280          * Caller to this routine should check for IOCB_ERROR
7281          * and handle it properly.  This routine no longer removes
7282          * iocb off txcmplq and call compl in case of IOCB_ERROR.
7283          */
7284         return retval;
7285 }
7286
7287 /**
7288  * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
7289  * @iocbq: Pointer to driver iocb object.
7290  * @vport: Pointer to driver virtual port object.
7291  * @tgt_id: SCSI ID of the target.
7292  * @lun_id: LUN ID of the scsi device.
7293  * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
7294  *
7295  * This function acts as an iocb filter for functions which abort or count
7296  * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
7297  * 0 if the filtering criteria is met for the given iocb and will return
7298  * 1 if the filtering criteria is not met.
7299  * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
7300  * given iocb is for the SCSI device specified by vport, tgt_id and
7301  * lun_id parameter.
7302  * If ctx_cmd == LPFC_CTX_TGT,  the function returns 0 only if the
7303  * given iocb is for the SCSI target specified by vport and tgt_id
7304  * parameters.
7305  * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
7306  * given iocb is for the SCSI host associated with the given vport.
7307  * This function is called with no locks held.
7308  **/
7309 static int
7310 lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
7311                            uint16_t tgt_id, uint64_t lun_id,
7312                            lpfc_ctx_cmd ctx_cmd)
7313 {
7314         struct lpfc_scsi_buf *lpfc_cmd;
7315         int rc = 1;
7316
7317         if (!(iocbq->iocb_flag &  LPFC_IO_FCP))
7318                 return rc;
7319
7320         if (iocbq->vport != vport)
7321                 return rc;
7322
7323         lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
7324
7325         if (lpfc_cmd->pCmd == NULL)
7326                 return rc;
7327
7328         switch (ctx_cmd) {
7329         case LPFC_CTX_LUN:
7330                 if ((lpfc_cmd->rdata->pnode) &&
7331                     (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
7332                     (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
7333                         rc = 0;
7334                 break;
7335         case LPFC_CTX_TGT:
7336                 if ((lpfc_cmd->rdata->pnode) &&
7337                     (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
7338                         rc = 0;
7339                 break;
7340         case LPFC_CTX_HOST:
7341                 rc = 0;
7342                 break;
7343         default:
7344                 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
7345                         __func__, ctx_cmd);
7346                 break;
7347         }
7348
7349         return rc;
7350 }
7351
7352 /**
7353  * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
7354  * @vport: Pointer to virtual port.
7355  * @tgt_id: SCSI ID of the target.
7356  * @lun_id: LUN ID of the scsi device.
7357  * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
7358  *
7359  * This function returns number of FCP commands pending for the vport.
7360  * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
7361  * commands pending on the vport associated with SCSI device specified
7362  * by tgt_id and lun_id parameters.
7363  * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
7364  * commands pending on the vport associated with SCSI target specified
7365  * by tgt_id parameter.
7366  * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
7367  * commands pending on the vport.
7368  * This function returns the number of iocbs which satisfy the filter.
7369  * This function is called without any lock held.
7370  **/
7371 int
7372 lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
7373                   lpfc_ctx_cmd ctx_cmd)
7374 {
7375         struct lpfc_hba *phba = vport->phba;
7376         struct lpfc_iocbq *iocbq;
7377         int sum, i;
7378
7379         for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
7380                 iocbq = phba->sli.iocbq_lookup[i];
7381
7382                 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
7383                                                 ctx_cmd) == 0)
7384                         sum++;
7385         }
7386
7387         return sum;
7388 }
7389
7390 /**
7391  * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
7392  * @phba: Pointer to HBA context object
7393  * @cmdiocb: Pointer to command iocb object.
7394  * @rspiocb: Pointer to response iocb object.
7395  *
7396  * This function is called when an aborted FCP iocb completes. This
7397  * function is called by the ring event handler with no lock held.
7398  * This function frees the iocb.
7399  **/
7400 void
7401 lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7402                         struct lpfc_iocbq *rspiocb)
7403 {
7404         lpfc_sli_release_iocbq(phba, cmdiocb);
7405         return;
7406 }
7407
7408 /**
7409  * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
7410  * @vport: Pointer to virtual port.
7411  * @pring: Pointer to driver SLI ring object.
7412  * @tgt_id: SCSI ID of the target.
7413  * @lun_id: LUN ID of the scsi device.
7414  * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
7415  *
7416  * This function sends an abort command for every SCSI command
7417  * associated with the given virtual port pending on the ring
7418  * filtered by lpfc_sli_validate_fcp_iocb function.
7419  * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
7420  * FCP iocbs associated with lun specified by tgt_id and lun_id
7421  * parameters
7422  * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
7423  * FCP iocbs associated with SCSI target specified by tgt_id parameter.
7424  * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
7425  * FCP iocbs associated with virtual port.
7426  * This function returns number of iocbs it failed to abort.
7427  * This function is called with no locks held.
7428  **/
7429 int
7430 lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
7431                     uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
7432 {
7433         struct lpfc_hba *phba = vport->phba;
7434         struct lpfc_iocbq *iocbq;
7435         struct lpfc_iocbq *abtsiocb;
7436         IOCB_t *cmd = NULL;
7437         int errcnt = 0, ret_val = 0;
7438         int i;
7439
7440         for (i = 1; i <= phba->sli.last_iotag; i++) {
7441                 iocbq = phba->sli.iocbq_lookup[i];
7442
7443                 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
7444                                                abort_cmd) != 0)
7445                         continue;
7446
7447                 /* issue ABTS for this IOCB based on iotag */
7448                 abtsiocb = lpfc_sli_get_iocbq(phba);
7449                 if (abtsiocb == NULL) {
7450                         errcnt++;
7451                         continue;
7452                 }
7453
7454                 cmd = &iocbq->iocb;
7455                 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
7456                 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
7457                 if (phba->sli_rev == LPFC_SLI_REV4)
7458                         abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
7459                 else
7460                         abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
7461                 abtsiocb->iocb.ulpLe = 1;
7462                 abtsiocb->iocb.ulpClass = cmd->ulpClass;
7463                 abtsiocb->vport = phba->pport;
7464
7465                 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
7466                 abtsiocb->fcp_wqidx = iocbq->fcp_wqidx;
7467                 if (iocbq->iocb_flag & LPFC_IO_FCP)
7468                         abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
7469
7470                 if (lpfc_is_link_up(phba))
7471                         abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
7472                 else
7473                         abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
7474
7475                 /* Setup callback routine and issue the command. */
7476                 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
7477                 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
7478                                               abtsiocb, 0);
7479                 if (ret_val == IOCB_ERROR) {
7480                         lpfc_sli_release_iocbq(phba, abtsiocb);
7481                         errcnt++;
7482                         continue;
7483                 }
7484         }
7485
7486         return errcnt;
7487 }
7488
7489 /**
7490  * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
7491  * @phba: Pointer to HBA context object.
7492  * @cmdiocbq: Pointer to command iocb.
7493  * @rspiocbq: Pointer to response iocb.
7494  *
7495  * This function is the completion handler for iocbs issued using
7496  * lpfc_sli_issue_iocb_wait function. This function is called by the
7497  * ring event handler function without any lock held. This function
7498  * can be called from both worker thread context and interrupt
7499  * context. This function also can be called from other thread which
7500  * cleans up the SLI layer objects.
7501  * This function copy the contents of the response iocb to the
7502  * response iocb memory object provided by the caller of
7503  * lpfc_sli_issue_iocb_wait and then wakes up the thread which
7504  * sleeps for the iocb completion.
7505  **/
7506 static void
7507 lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
7508                         struct lpfc_iocbq *cmdiocbq,
7509                         struct lpfc_iocbq *rspiocbq)
7510 {
7511         wait_queue_head_t *pdone_q;
7512         unsigned long iflags;
7513         struct lpfc_scsi_buf *lpfc_cmd;
7514
7515         spin_lock_irqsave(&phba->hbalock, iflags);
7516         cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
7517         if (cmdiocbq->context2 && rspiocbq)
7518                 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
7519                        &rspiocbq->iocb, sizeof(IOCB_t));
7520
7521         /* Set the exchange busy flag for task management commands */
7522         if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
7523                 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
7524                 lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
7525                         cur_iocbq);
7526                 lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
7527         }
7528
7529         pdone_q = cmdiocbq->context_un.wait_queue;
7530         if (pdone_q)
7531                 wake_up(pdone_q);
7532         spin_unlock_irqrestore(&phba->hbalock, iflags);
7533         return;
7534 }
7535
7536 /**
7537  * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
7538  * @phba: Pointer to HBA context object..
7539  * @piocbq: Pointer to command iocb.
7540  * @flag: Flag to test.
7541  *
7542  * This routine grabs the hbalock and then test the iocb_flag to
7543  * see if the passed in flag is set.
7544  * Returns:
7545  * 1 if flag is set.
7546  * 0 if flag is not set.
7547  **/
7548 static int
7549 lpfc_chk_iocb_flg(struct lpfc_hba *phba,
7550                  struct lpfc_iocbq *piocbq, uint32_t flag)
7551 {
7552         unsigned long iflags;
7553         int ret;
7554
7555         spin_lock_irqsave(&phba->hbalock, iflags);
7556         ret = piocbq->iocb_flag & flag;
7557         spin_unlock_irqrestore(&phba->hbalock, iflags);
7558         return ret;
7559
7560 }
7561
7562 /**
7563  * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
7564  * @phba: Pointer to HBA context object..
7565  * @pring: Pointer to sli ring.
7566  * @piocb: Pointer to command iocb.
7567  * @prspiocbq: Pointer to response iocb.
7568  * @timeout: Timeout in number of seconds.
7569  *
7570  * This function issues the iocb to firmware and waits for the
7571  * iocb to complete. If the iocb command is not
7572  * completed within timeout seconds, it returns IOCB_TIMEDOUT.
7573  * Caller should not free the iocb resources if this function
7574  * returns IOCB_TIMEDOUT.
7575  * The function waits for the iocb completion using an
7576  * non-interruptible wait.
7577  * This function will sleep while waiting for iocb completion.
7578  * So, this function should not be called from any context which
7579  * does not allow sleeping. Due to the same reason, this function
7580  * cannot be called with interrupt disabled.
7581  * This function assumes that the iocb completions occur while
7582  * this function sleep. So, this function cannot be called from
7583  * the thread which process iocb completion for this ring.
7584  * This function clears the iocb_flag of the iocb object before
7585  * issuing the iocb and the iocb completion handler sets this
7586  * flag and wakes this thread when the iocb completes.
7587  * The contents of the response iocb will be copied to prspiocbq
7588  * by the completion handler when the command completes.
7589  * This function returns IOCB_SUCCESS when success.
7590  * This function is called with no lock held.
7591  **/
7592 int
7593 lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
7594                          uint32_t ring_number,
7595                          struct lpfc_iocbq *piocb,
7596                          struct lpfc_iocbq *prspiocbq,
7597                          uint32_t timeout)
7598 {
7599         DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
7600         long timeleft, timeout_req = 0;
7601         int retval = IOCB_SUCCESS;
7602         uint32_t creg_val;
7603
7604         /*
7605          * If the caller has provided a response iocbq buffer, then context2
7606          * is NULL or its an error.
7607          */
7608         if (prspiocbq) {
7609                 if (piocb->context2)
7610                         return IOCB_ERROR;
7611                 piocb->context2 = prspiocbq;
7612         }
7613
7614         piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
7615         piocb->context_un.wait_queue = &done_q;
7616         piocb->iocb_flag &= ~LPFC_IO_WAKE;
7617
7618         if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
7619                 creg_val = readl(phba->HCregaddr);
7620                 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
7621                 writel(creg_val, phba->HCregaddr);
7622                 readl(phba->HCregaddr); /* flush */
7623         }
7624
7625         retval = lpfc_sli_issue_iocb(phba, ring_number, piocb, 0);
7626         if (retval == IOCB_SUCCESS) {
7627                 timeout_req = timeout * HZ;
7628                 timeleft = wait_event_timeout(done_q,
7629                                 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
7630                                 timeout_req);
7631
7632                 if (piocb->iocb_flag & LPFC_IO_WAKE) {
7633                         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
7634                                         "0331 IOCB wake signaled\n");
7635                 } else if (timeleft == 0) {
7636                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7637                                         "0338 IOCB wait timeout error - no "
7638                                         "wake response Data x%x\n", timeout);
7639                         retval = IOCB_TIMEDOUT;
7640                 } else {
7641                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7642                                         "0330 IOCB wake NOT set, "
7643                                         "Data x%x x%lx\n",
7644                                         timeout, (timeleft / jiffies));
7645                         retval = IOCB_TIMEDOUT;
7646                 }
7647         } else {
7648                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
7649                                 "0332 IOCB wait issue failed, Data x%x\n",
7650                                 retval);
7651                 retval = IOCB_ERROR;
7652         }
7653
7654         if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
7655                 creg_val = readl(phba->HCregaddr);
7656                 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
7657                 writel(creg_val, phba->HCregaddr);
7658                 readl(phba->HCregaddr); /* flush */
7659         }
7660
7661         if (prspiocbq)
7662                 piocb->context2 = NULL;
7663
7664         piocb->context_un.wait_queue = NULL;
7665         piocb->iocb_cmpl = NULL;
7666         return retval;
7667 }
7668
7669 /**
7670  * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
7671  * @phba: Pointer to HBA context object.
7672  * @pmboxq: Pointer to driver mailbox object.
7673  * @timeout: Timeout in number of seconds.
7674  *
7675  * This function issues the mailbox to firmware and waits for the
7676  * mailbox command to complete. If the mailbox command is not
7677  * completed within timeout seconds, it returns MBX_TIMEOUT.
7678  * The function waits for the mailbox completion using an
7679  * interruptible wait. If the thread is woken up due to a
7680  * signal, MBX_TIMEOUT error is returned to the caller. Caller
7681  * should not free the mailbox resources, if this function returns
7682  * MBX_TIMEOUT.
7683  * This function will sleep while waiting for mailbox completion.
7684  * So, this function should not be called from any context which
7685  * does not allow sleeping. Due to the same reason, this function
7686  * cannot be called with interrupt disabled.
7687  * This function assumes that the mailbox completion occurs while
7688  * this function sleep. So, this function cannot be called from
7689  * the worker thread which processes mailbox completion.
7690  * This function is called in the context of HBA management
7691  * applications.
7692  * This function returns MBX_SUCCESS when successful.
7693  * This function is called with no lock held.
7694  **/
7695 int
7696 lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
7697                          uint32_t timeout)
7698 {
7699         DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
7700         int retval;
7701         unsigned long flag;
7702
7703         /* The caller must leave context1 empty. */
7704         if (pmboxq->context1)
7705                 return MBX_NOT_FINISHED;
7706
7707         pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
7708         /* setup wake call as IOCB callback */
7709         pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
7710         /* setup context field to pass wait_queue pointer to wake function  */
7711         pmboxq->context1 = &done_q;
7712
7713         /* now issue the command */
7714         retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
7715
7716         if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
7717                 wait_event_interruptible_timeout(done_q,
7718                                 pmboxq->mbox_flag & LPFC_MBX_WAKE,
7719                                 timeout * HZ);
7720
7721                 spin_lock_irqsave(&phba->hbalock, flag);
7722                 pmboxq->context1 = NULL;
7723                 /*
7724                  * if LPFC_MBX_WAKE flag is set the mailbox is completed
7725                  * else do not free the resources.
7726                  */
7727                 if (pmboxq->mbox_flag & LPFC_MBX_WAKE)
7728                         retval = MBX_SUCCESS;
7729                 else {
7730                         retval = MBX_TIMEOUT;
7731                         pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
7732                 }
7733                 spin_unlock_irqrestore(&phba->hbalock, flag);
7734         }
7735
7736         return retval;
7737 }
7738
7739 /**
7740  * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
7741  * @phba: Pointer to HBA context.
7742  *
7743  * This function is called to shutdown the driver's mailbox sub-system.
7744  * It first marks the mailbox sub-system is in a block state to prevent
7745  * the asynchronous mailbox command from issued off the pending mailbox
7746  * command queue. If the mailbox command sub-system shutdown is due to
7747  * HBA error conditions such as EEH or ERATT, this routine shall invoke
7748  * the mailbox sub-system flush routine to forcefully bring down the
7749  * mailbox sub-system. Otherwise, if it is due to normal condition (such
7750  * as with offline or HBA function reset), this routine will wait for the
7751  * outstanding mailbox command to complete before invoking the mailbox
7752  * sub-system flush routine to gracefully bring down mailbox sub-system.
7753  **/
7754 void
7755 lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba)
7756 {
7757         struct lpfc_sli *psli = &phba->sli;
7758         uint8_t actcmd = MBX_HEARTBEAT;
7759         unsigned long timeout;
7760
7761         spin_lock_irq(&phba->hbalock);
7762         psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
7763         spin_unlock_irq(&phba->hbalock);
7764
7765         if (psli->sli_flag & LPFC_SLI_ACTIVE) {
7766                 spin_lock_irq(&phba->hbalock);
7767                 if (phba->sli.mbox_active)
7768                         actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
7769                 spin_unlock_irq(&phba->hbalock);
7770                 /* Determine how long we might wait for the active mailbox
7771                  * command to be gracefully completed by firmware.
7772                  */
7773                 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, actcmd) *
7774                                            1000) + jiffies;
7775                 while (phba->sli.mbox_active) {
7776                         /* Check active mailbox complete status every 2ms */
7777                         msleep(2);
7778                         if (time_after(jiffies, timeout))
7779                                 /* Timeout, let the mailbox flush routine to
7780                                  * forcefully release active mailbox command
7781                                  */
7782                                 break;
7783                 }
7784         }
7785         lpfc_sli_mbox_sys_flush(phba);
7786 }
7787
7788 /**
7789  * lpfc_sli_eratt_read - read sli-3 error attention events
7790  * @phba: Pointer to HBA context.
7791  *
7792  * This function is called to read the SLI3 device error attention registers
7793  * for possible error attention events. The caller must hold the hostlock
7794  * with spin_lock_irq().
7795  *
7796  * This fucntion returns 1 when there is Error Attention in the Host Attention
7797  * Register and returns 0 otherwise.
7798  **/
7799 static int
7800 lpfc_sli_eratt_read(struct lpfc_hba *phba)
7801 {
7802         uint32_t ha_copy;
7803
7804         /* Read chip Host Attention (HA) register */
7805         ha_copy = readl(phba->HAregaddr);
7806         if (ha_copy & HA_ERATT) {
7807                 /* Read host status register to retrieve error event */
7808                 lpfc_sli_read_hs(phba);
7809
7810                 /* Check if there is a deferred error condition is active */
7811                 if ((HS_FFER1 & phba->work_hs) &&
7812                     ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
7813                      HS_FFER6 | HS_FFER7) & phba->work_hs)) {
7814                         phba->hba_flag |= DEFER_ERATT;
7815                         /* Clear all interrupt enable conditions */
7816                         writel(0, phba->HCregaddr);
7817                         readl(phba->HCregaddr);
7818                 }
7819
7820                 /* Set the driver HA work bitmap */
7821                 phba->work_ha |= HA_ERATT;
7822                 /* Indicate polling handles this ERATT */
7823                 phba->hba_flag |= HBA_ERATT_HANDLED;
7824                 return 1;
7825         }
7826         return 0;
7827 }
7828
7829 /**
7830  * lpfc_sli4_eratt_read - read sli-4 error attention events
7831  * @phba: Pointer to HBA context.
7832  *
7833  * This function is called to read the SLI4 device error attention registers
7834  * for possible error attention events. The caller must hold the hostlock
7835  * with spin_lock_irq().
7836  *
7837  * This fucntion returns 1 when there is Error Attention in the Host Attention
7838  * Register and returns 0 otherwise.
7839  **/
7840 static int
7841 lpfc_sli4_eratt_read(struct lpfc_hba *phba)
7842 {
7843         uint32_t uerr_sta_hi, uerr_sta_lo;
7844
7845         /* For now, use the SLI4 device internal unrecoverable error
7846          * registers for error attention. This can be changed later.
7847          */
7848         uerr_sta_lo = readl(phba->sli4_hba.UERRLOregaddr);
7849         uerr_sta_hi = readl(phba->sli4_hba.UERRHIregaddr);
7850         if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
7851             (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
7852                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7853                                 "1423 HBA Unrecoverable error: "
7854                                 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
7855                                 "ue_mask_lo_reg=0x%x, ue_mask_hi_reg=0x%x\n",
7856                                 uerr_sta_lo, uerr_sta_hi,
7857                                 phba->sli4_hba.ue_mask_lo,
7858                                 phba->sli4_hba.ue_mask_hi);
7859                 phba->work_status[0] = uerr_sta_lo;
7860                 phba->work_status[1] = uerr_sta_hi;
7861                 /* Set the driver HA work bitmap */
7862                 phba->work_ha |= HA_ERATT;
7863                 /* Indicate polling handles this ERATT */
7864                 phba->hba_flag |= HBA_ERATT_HANDLED;
7865                 return 1;
7866         }
7867         return 0;
7868 }
7869
7870 /**
7871  * lpfc_sli_check_eratt - check error attention events
7872  * @phba: Pointer to HBA context.
7873  *
7874  * This function is called from timer soft interrupt context to check HBA's
7875  * error attention register bit for error attention events.
7876  *
7877  * This fucntion returns 1 when there is Error Attention in the Host Attention
7878  * Register and returns 0 otherwise.
7879  **/
7880 int
7881 lpfc_sli_check_eratt(struct lpfc_hba *phba)
7882 {
7883         uint32_t ha_copy;
7884
7885         /* If somebody is waiting to handle an eratt, don't process it
7886          * here. The brdkill function will do this.
7887          */
7888         if (phba->link_flag & LS_IGNORE_ERATT)
7889                 return 0;
7890
7891         /* Check if interrupt handler handles this ERATT */
7892         spin_lock_irq(&phba->hbalock);
7893         if (phba->hba_flag & HBA_ERATT_HANDLED) {
7894                 /* Interrupt handler has handled ERATT */
7895                 spin_unlock_irq(&phba->hbalock);
7896                 return 0;
7897         }
7898
7899         /*
7900          * If there is deferred error attention, do not check for error
7901          * attention
7902          */
7903         if (unlikely(phba->hba_flag & DEFER_ERATT)) {
7904                 spin_unlock_irq(&phba->hbalock);
7905                 return 0;
7906         }
7907
7908         /* If PCI channel is offline, don't process it */
7909         if (unlikely(pci_channel_offline(phba->pcidev))) {
7910                 spin_unlock_irq(&phba->hbalock);
7911                 return 0;
7912         }
7913
7914         switch (phba->sli_rev) {
7915         case LPFC_SLI_REV2:
7916         case LPFC_SLI_REV3:
7917                 /* Read chip Host Attention (HA) register */
7918                 ha_copy = lpfc_sli_eratt_read(phba);
7919                 break;
7920         case LPFC_SLI_REV4:
7921                 /* Read devcie Uncoverable Error (UERR) registers */
7922                 ha_copy = lpfc_sli4_eratt_read(phba);
7923                 break;
7924         default:
7925                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7926                                 "0299 Invalid SLI revision (%d)\n",
7927                                 phba->sli_rev);
7928                 ha_copy = 0;
7929                 break;
7930         }
7931         spin_unlock_irq(&phba->hbalock);
7932
7933         return ha_copy;
7934 }
7935
7936 /**
7937  * lpfc_intr_state_check - Check device state for interrupt handling
7938  * @phba: Pointer to HBA context.
7939  *
7940  * This inline routine checks whether a device or its PCI slot is in a state
7941  * that the interrupt should be handled.
7942  *
7943  * This function returns 0 if the device or the PCI slot is in a state that
7944  * interrupt should be handled, otherwise -EIO.
7945  */
7946 static inline int
7947 lpfc_intr_state_check(struct lpfc_hba *phba)
7948 {
7949         /* If the pci channel is offline, ignore all the interrupts */
7950         if (unlikely(pci_channel_offline(phba->pcidev)))
7951                 return -EIO;
7952
7953         /* Update device level interrupt statistics */
7954         phba->sli.slistat.sli_intr++;
7955
7956         /* Ignore all interrupts during initialization. */
7957         if (unlikely(phba->link_state < LPFC_LINK_DOWN))
7958                 return -EIO;
7959
7960         return 0;
7961 }
7962
7963 /**
7964  * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
7965  * @irq: Interrupt number.
7966  * @dev_id: The device context pointer.
7967  *
7968  * This function is directly called from the PCI layer as an interrupt
7969  * service routine when device with SLI-3 interface spec is enabled with
7970  * MSI-X multi-message interrupt mode and there are slow-path events in
7971  * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
7972  * interrupt mode, this function is called as part of the device-level
7973  * interrupt handler. When the PCI slot is in error recovery or the HBA
7974  * is undergoing initialization, the interrupt handler will not process
7975  * the interrupt. The link attention and ELS ring attention events are
7976  * handled by the worker thread. The interrupt handler signals the worker
7977  * thread and returns for these events. This function is called without
7978  * any lock held. It gets the hbalock to access and update SLI data
7979  * structures.
7980  *
7981  * This function returns IRQ_HANDLED when interrupt is handled else it
7982  * returns IRQ_NONE.
7983  **/
7984 irqreturn_t
7985 lpfc_sli_sp_intr_handler(int irq, void *dev_id)
7986 {
7987         struct lpfc_hba  *phba;
7988         uint32_t ha_copy, hc_copy;
7989         uint32_t work_ha_copy;
7990         unsigned long status;
7991         unsigned long iflag;
7992         uint32_t control;
7993
7994         MAILBOX_t *mbox, *pmbox;
7995         struct lpfc_vport *vport;
7996         struct lpfc_nodelist *ndlp;
7997         struct lpfc_dmabuf *mp;
7998         LPFC_MBOXQ_t *pmb;
7999         int rc;
8000
8001         /*
8002          * Get the driver's phba structure from the dev_id and
8003          * assume the HBA is not interrupting.
8004          */
8005         phba = (struct lpfc_hba *)dev_id;
8006
8007         if (unlikely(!phba))
8008                 return IRQ_NONE;
8009
8010         /*
8011          * Stuff needs to be attented to when this function is invoked as an
8012          * individual interrupt handler in MSI-X multi-message interrupt mode
8013          */
8014         if (phba->intr_type == MSIX) {
8015                 /* Check device state for handling interrupt */
8016                 if (lpfc_intr_state_check(phba))
8017                         return IRQ_NONE;
8018                 /* Need to read HA REG for slow-path events */
8019                 spin_lock_irqsave(&phba->hbalock, iflag);
8020                 ha_copy = readl(phba->HAregaddr);
8021                 /* If somebody is waiting to handle an eratt don't process it
8022                  * here. The brdkill function will do this.
8023                  */
8024                 if (phba->link_flag & LS_IGNORE_ERATT)
8025                         ha_copy &= ~HA_ERATT;
8026                 /* Check the need for handling ERATT in interrupt handler */
8027                 if (ha_copy & HA_ERATT) {
8028                         if (phba->hba_flag & HBA_ERATT_HANDLED)
8029                                 /* ERATT polling has handled ERATT */
8030                                 ha_copy &= ~HA_ERATT;
8031                         else
8032                                 /* Indicate interrupt handler handles ERATT */
8033                                 phba->hba_flag |= HBA_ERATT_HANDLED;
8034                 }
8035
8036                 /*
8037                  * If there is deferred error attention, do not check for any
8038                  * interrupt.
8039                  */
8040                 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
8041                         spin_unlock_irqrestore(&phba->hbalock, iflag);
8042                         return IRQ_NONE;
8043                 }
8044
8045                 /* Clear up only attention source related to slow-path */
8046                 hc_copy = readl(phba->HCregaddr);
8047                 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
8048                         HC_LAINT_ENA | HC_ERINT_ENA),
8049                         phba->HCregaddr);
8050                 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
8051                         phba->HAregaddr);
8052                 writel(hc_copy, phba->HCregaddr);
8053                 readl(phba->HAregaddr); /* flush */
8054                 spin_unlock_irqrestore(&phba->hbalock, iflag);
8055         } else
8056                 ha_copy = phba->ha_copy;
8057
8058         work_ha_copy = ha_copy & phba->work_ha_mask;
8059
8060         if (work_ha_copy) {
8061                 if (work_ha_copy & HA_LATT) {
8062                         if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
8063                                 /*
8064                                  * Turn off Link Attention interrupts
8065                                  * until CLEAR_LA done
8066                                  */
8067                                 spin_lock_irqsave(&phba->hbalock, iflag);
8068                                 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
8069                                 control = readl(phba->HCregaddr);
8070                                 control &= ~HC_LAINT_ENA;
8071                                 writel(control, phba->HCregaddr);
8072                                 readl(phba->HCregaddr); /* flush */
8073                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
8074                         }
8075                         else
8076                                 work_ha_copy &= ~HA_LATT;
8077                 }
8078
8079                 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
8080                         /*
8081                          * Turn off Slow Rings interrupts, LPFC_ELS_RING is
8082                          * the only slow ring.
8083                          */
8084                         status = (work_ha_copy &
8085                                 (HA_RXMASK  << (4*LPFC_ELS_RING)));
8086                         status >>= (4*LPFC_ELS_RING);
8087                         if (status & HA_RXMASK) {
8088                                 spin_lock_irqsave(&phba->hbalock, iflag);
8089                                 control = readl(phba->HCregaddr);
8090
8091                                 lpfc_debugfs_slow_ring_trc(phba,
8092                                 "ISR slow ring:   ctl:x%x stat:x%x isrcnt:x%x",
8093                                 control, status,
8094                                 (uint32_t)phba->sli.slistat.sli_intr);
8095
8096                                 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
8097                                         lpfc_debugfs_slow_ring_trc(phba,
8098                                                 "ISR Disable ring:"
8099                                                 "pwork:x%x hawork:x%x wait:x%x",
8100                                                 phba->work_ha, work_ha_copy,
8101                                                 (uint32_t)((unsigned long)
8102                                                 &phba->work_waitq));
8103
8104                                         control &=
8105                                             ~(HC_R0INT_ENA << LPFC_ELS_RING);
8106                                         writel(control, phba->HCregaddr);
8107                                         readl(phba->HCregaddr); /* flush */
8108                                 }
8109                                 else {
8110                                         lpfc_debugfs_slow_ring_trc(phba,
8111                                                 "ISR slow ring:   pwork:"
8112                                                 "x%x hawork:x%x wait:x%x",
8113                                                 phba->work_ha, work_ha_copy,
8114                                                 (uint32_t)((unsigned long)
8115                                                 &phba->work_waitq));
8116                                 }
8117                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
8118                         }
8119                 }
8120                 spin_lock_irqsave(&phba->hbalock, iflag);
8121                 if (work_ha_copy & HA_ERATT) {
8122                         lpfc_sli_read_hs(phba);
8123                         /*
8124                          * Check if there is a deferred error condition
8125                          * is active
8126                          */
8127                         if ((HS_FFER1 & phba->work_hs) &&
8128                                 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
8129                                 HS_FFER6 | HS_FFER7) & phba->work_hs)) {
8130                                 phba->hba_flag |= DEFER_ERATT;
8131                                 /* Clear all interrupt enable conditions */
8132                                 writel(0, phba->HCregaddr);
8133                                 readl(phba->HCregaddr);
8134                         }
8135                 }
8136
8137                 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
8138                         pmb = phba->sli.mbox_active;
8139                         pmbox = &pmb->u.mb;
8140                         mbox = phba->mbox;
8141                         vport = pmb->vport;
8142
8143                         /* First check out the status word */
8144                         lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
8145                         if (pmbox->mbxOwner != OWN_HOST) {
8146                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
8147                                 /*
8148                                  * Stray Mailbox Interrupt, mbxCommand <cmd>
8149                                  * mbxStatus <status>
8150                                  */
8151                                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
8152                                                 LOG_SLI,
8153                                                 "(%d):0304 Stray Mailbox "
8154                                                 "Interrupt mbxCommand x%x "
8155                                                 "mbxStatus x%x\n",
8156                                                 (vport ? vport->vpi : 0),
8157                                                 pmbox->mbxCommand,
8158                                                 pmbox->mbxStatus);
8159                                 /* clear mailbox attention bit */
8160                                 work_ha_copy &= ~HA_MBATT;
8161                         } else {
8162                                 phba->sli.mbox_active = NULL;
8163                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
8164                                 phba->last_completion_time = jiffies;
8165                                 del_timer(&phba->sli.mbox_tmo);
8166                                 if (pmb->mbox_cmpl) {
8167                                         lpfc_sli_pcimem_bcopy(mbox, pmbox,
8168                                                         MAILBOX_CMD_SIZE);
8169                                         if (pmb->out_ext_byte_len &&
8170                                                 pmb->context2)
8171                                                 lpfc_sli_pcimem_bcopy(
8172                                                 phba->mbox_ext,
8173                                                 pmb->context2,
8174                                                 pmb->out_ext_byte_len);
8175                                 }
8176                                 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
8177                                         pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
8178
8179                                         lpfc_debugfs_disc_trc(vport,
8180                                                 LPFC_DISC_TRC_MBOX_VPORT,
8181                                                 "MBOX dflt rpi: : "
8182                                                 "status:x%x rpi:x%x",
8183                                                 (uint32_t)pmbox->mbxStatus,
8184                                                 pmbox->un.varWords[0], 0);
8185
8186                                         if (!pmbox->mbxStatus) {
8187                                                 mp = (struct lpfc_dmabuf *)
8188                                                         (pmb->context1);
8189                                                 ndlp = (struct lpfc_nodelist *)
8190                                                         pmb->context2;
8191
8192                                                 /* Reg_LOGIN of dflt RPI was
8193                                                  * successful. new lets get
8194                                                  * rid of the RPI using the
8195                                                  * same mbox buffer.
8196                                                  */
8197                                                 lpfc_unreg_login(phba,
8198                                                         vport->vpi,
8199                                                         pmbox->un.varWords[0],
8200                                                         pmb);
8201                                                 pmb->mbox_cmpl =
8202                                                         lpfc_mbx_cmpl_dflt_rpi;
8203                                                 pmb->context1 = mp;
8204                                                 pmb->context2 = ndlp;
8205                                                 pmb->vport = vport;
8206                                                 rc = lpfc_sli_issue_mbox(phba,
8207                                                                 pmb,
8208                                                                 MBX_NOWAIT);
8209                                                 if (rc != MBX_BUSY)
8210                                                         lpfc_printf_log(phba,
8211                                                         KERN_ERR,
8212                                                         LOG_MBOX | LOG_SLI,
8213                                                         "0350 rc should have"
8214                                                         "been MBX_BUSY\n");
8215                                                 if (rc != MBX_NOT_FINISHED)
8216                                                         goto send_current_mbox;
8217                                         }
8218                                 }
8219                                 spin_lock_irqsave(
8220                                                 &phba->pport->work_port_lock,
8221                                                 iflag);
8222                                 phba->pport->work_port_events &=
8223                                         ~WORKER_MBOX_TMO;
8224                                 spin_unlock_irqrestore(
8225                                                 &phba->pport->work_port_lock,
8226                                                 iflag);
8227                                 lpfc_mbox_cmpl_put(phba, pmb);
8228                         }
8229                 } else
8230                         spin_unlock_irqrestore(&phba->hbalock, iflag);
8231
8232                 if ((work_ha_copy & HA_MBATT) &&
8233                     (phba->sli.mbox_active == NULL)) {
8234 send_current_mbox:
8235                         /* Process next mailbox command if there is one */
8236                         do {
8237                                 rc = lpfc_sli_issue_mbox(phba, NULL,
8238                                                          MBX_NOWAIT);
8239                         } while (rc == MBX_NOT_FINISHED);
8240                         if (rc != MBX_SUCCESS)
8241                                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
8242                                                 LOG_SLI, "0349 rc should be "
8243                                                 "MBX_SUCCESS\n");
8244                 }
8245
8246                 spin_lock_irqsave(&phba->hbalock, iflag);
8247                 phba->work_ha |= work_ha_copy;
8248                 spin_unlock_irqrestore(&phba->hbalock, iflag);
8249                 lpfc_worker_wake_up(phba);
8250         }
8251         return IRQ_HANDLED;
8252
8253 } /* lpfc_sli_sp_intr_handler */
8254
8255 /**
8256  * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
8257  * @irq: Interrupt number.
8258  * @dev_id: The device context pointer.
8259  *
8260  * This function is directly called from the PCI layer as an interrupt
8261  * service routine when device with SLI-3 interface spec is enabled with
8262  * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
8263  * ring event in the HBA. However, when the device is enabled with either
8264  * MSI or Pin-IRQ interrupt mode, this function is called as part of the
8265  * device-level interrupt handler. When the PCI slot is in error recovery
8266  * or the HBA is undergoing initialization, the interrupt handler will not
8267  * process the interrupt. The SCSI FCP fast-path ring event are handled in
8268  * the intrrupt context. This function is called without any lock held.
8269  * It gets the hbalock to access and update SLI data structures.
8270  *
8271  * This function returns IRQ_HANDLED when interrupt is handled else it
8272  * returns IRQ_NONE.
8273  **/
8274 irqreturn_t
8275 lpfc_sli_fp_intr_handler(int irq, void *dev_id)
8276 {
8277         struct lpfc_hba  *phba;
8278         uint32_t ha_copy;
8279         unsigned long status;
8280         unsigned long iflag;
8281
8282         /* Get the driver's phba structure from the dev_id and
8283          * assume the HBA is not interrupting.
8284          */
8285         phba = (struct lpfc_hba *) dev_id;
8286
8287         if (unlikely(!phba))
8288                 return IRQ_NONE;
8289
8290         /*
8291          * Stuff needs to be attented to when this function is invoked as an
8292          * individual interrupt handler in MSI-X multi-message interrupt mode
8293          */
8294         if (phba->intr_type == MSIX) {
8295                 /* Check device state for handling interrupt */
8296                 if (lpfc_intr_state_check(phba))
8297                         return IRQ_NONE;
8298                 /* Need to read HA REG for FCP ring and other ring events */
8299                 ha_copy = readl(phba->HAregaddr);
8300                 /* Clear up only attention source related to fast-path */
8301                 spin_lock_irqsave(&phba->hbalock, iflag);
8302                 /*
8303                  * If there is deferred error attention, do not check for
8304                  * any interrupt.
8305                  */
8306                 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
8307                         spin_unlock_irqrestore(&phba->hbalock, iflag);
8308                         return IRQ_NONE;
8309                 }
8310                 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
8311                         phba->HAregaddr);
8312                 readl(phba->HAregaddr); /* flush */
8313                 spin_unlock_irqrestore(&phba->hbalock, iflag);
8314         } else
8315                 ha_copy = phba->ha_copy;
8316
8317         /*
8318          * Process all events on FCP ring. Take the optimized path for FCP IO.
8319          */
8320         ha_copy &= ~(phba->work_ha_mask);
8321
8322         status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
8323         status >>= (4*LPFC_FCP_RING);
8324         if (status & HA_RXMASK)
8325                 lpfc_sli_handle_fast_ring_event(phba,
8326                                                 &phba->sli.ring[LPFC_FCP_RING],
8327                                                 status);
8328
8329         if (phba->cfg_multi_ring_support == 2) {
8330                 /*
8331                  * Process all events on extra ring. Take the optimized path
8332                  * for extra ring IO.
8333                  */
8334                 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
8335                 status >>= (4*LPFC_EXTRA_RING);
8336                 if (status & HA_RXMASK) {
8337                         lpfc_sli_handle_fast_ring_event(phba,
8338                                         &phba->sli.ring[LPFC_EXTRA_RING],
8339                                         status);
8340                 }
8341         }
8342         return IRQ_HANDLED;
8343 }  /* lpfc_sli_fp_intr_handler */
8344
8345 /**
8346  * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
8347  * @irq: Interrupt number.
8348  * @dev_id: The device context pointer.
8349  *
8350  * This function is the HBA device-level interrupt handler to device with
8351  * SLI-3 interface spec, called from the PCI layer when either MSI or
8352  * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
8353  * requires driver attention. This function invokes the slow-path interrupt
8354  * attention handling function and fast-path interrupt attention handling
8355  * function in turn to process the relevant HBA attention events. This
8356  * function is called without any lock held. It gets the hbalock to access
8357  * and update SLI data structures.
8358  *
8359  * This function returns IRQ_HANDLED when interrupt is handled, else it
8360  * returns IRQ_NONE.
8361  **/
8362 irqreturn_t
8363 lpfc_sli_intr_handler(int irq, void *dev_id)
8364 {
8365         struct lpfc_hba  *phba;
8366         irqreturn_t sp_irq_rc, fp_irq_rc;
8367         unsigned long status1, status2;
8368         uint32_t hc_copy;
8369
8370         /*
8371          * Get the driver's phba structure from the dev_id and
8372          * assume the HBA is not interrupting.
8373          */
8374         phba = (struct lpfc_hba *) dev_id;
8375
8376         if (unlikely(!phba))
8377                 return IRQ_NONE;
8378
8379         /* Check device state for handling interrupt */
8380         if (lpfc_intr_state_check(phba))
8381                 return IRQ_NONE;
8382
8383         spin_lock(&phba->hbalock);
8384         phba->ha_copy = readl(phba->HAregaddr);
8385         if (unlikely(!phba->ha_copy)) {
8386                 spin_unlock(&phba->hbalock);
8387                 return IRQ_NONE;
8388         } else if (phba->ha_copy & HA_ERATT) {
8389                 if (phba->hba_flag & HBA_ERATT_HANDLED)
8390                         /* ERATT polling has handled ERATT */
8391                         phba->ha_copy &= ~HA_ERATT;
8392                 else
8393                         /* Indicate interrupt handler handles ERATT */
8394                         phba->hba_flag |= HBA_ERATT_HANDLED;
8395         }
8396
8397         /*
8398          * If there is deferred error attention, do not check for any interrupt.
8399          */
8400         if (unlikely(phba->hba_flag & DEFER_ERATT)) {
8401                 spin_unlock_irq(&phba->hbalock);
8402                 return IRQ_NONE;
8403         }
8404
8405         /* Clear attention sources except link and error attentions */
8406         hc_copy = readl(phba->HCregaddr);
8407         writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
8408                 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
8409                 phba->HCregaddr);
8410         writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
8411         writel(hc_copy, phba->HCregaddr);
8412         readl(phba->HAregaddr); /* flush */
8413         spin_unlock(&phba->hbalock);
8414
8415         /*
8416          * Invokes slow-path host attention interrupt handling as appropriate.
8417          */
8418
8419         /* status of events with mailbox and link attention */
8420         status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
8421
8422         /* status of events with ELS ring */
8423         status2 = (phba->ha_copy & (HA_RXMASK  << (4*LPFC_ELS_RING)));
8424         status2 >>= (4*LPFC_ELS_RING);
8425
8426         if (status1 || (status2 & HA_RXMASK))
8427                 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
8428         else
8429                 sp_irq_rc = IRQ_NONE;
8430
8431         /*
8432          * Invoke fast-path host attention interrupt handling as appropriate.
8433          */
8434
8435         /* status of events with FCP ring */
8436         status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
8437         status1 >>= (4*LPFC_FCP_RING);
8438
8439         /* status of events with extra ring */
8440         if (phba->cfg_multi_ring_support == 2) {
8441                 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
8442                 status2 >>= (4*LPFC_EXTRA_RING);
8443         } else
8444                 status2 = 0;
8445
8446         if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
8447                 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
8448         else
8449                 fp_irq_rc = IRQ_NONE;
8450
8451         /* Return device-level interrupt handling status */
8452         return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
8453 }  /* lpfc_sli_intr_handler */
8454
8455 /**
8456  * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
8457  * @phba: pointer to lpfc hba data structure.
8458  *
8459  * This routine is invoked by the worker thread to process all the pending
8460  * SLI4 FCP abort XRI events.
8461  **/
8462 void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
8463 {
8464         struct lpfc_cq_event *cq_event;
8465
8466         /* First, declare the fcp xri abort event has been handled */
8467         spin_lock_irq(&phba->hbalock);
8468         phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
8469         spin_unlock_irq(&phba->hbalock);
8470         /* Now, handle all the fcp xri abort events */
8471         while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
8472                 /* Get the first event from the head of the event queue */
8473                 spin_lock_irq(&phba->hbalock);
8474                 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
8475                                  cq_event, struct lpfc_cq_event, list);
8476                 spin_unlock_irq(&phba->hbalock);
8477                 /* Notify aborted XRI for FCP work queue */
8478                 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
8479                 /* Free the event processed back to the free pool */
8480                 lpfc_sli4_cq_event_release(phba, cq_event);
8481         }
8482 }
8483
8484 /**
8485  * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
8486  * @phba: pointer to lpfc hba data structure.
8487  *
8488  * This routine is invoked by the worker thread to process all the pending
8489  * SLI4 els abort xri events.
8490  **/
8491 void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
8492 {
8493         struct lpfc_cq_event *cq_event;
8494
8495         /* First, declare the els xri abort event has been handled */
8496         spin_lock_irq(&phba->hbalock);
8497         phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
8498         spin_unlock_irq(&phba->hbalock);
8499         /* Now, handle all the els xri abort events */
8500         while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
8501                 /* Get the first event from the head of the event queue */
8502                 spin_lock_irq(&phba->hbalock);
8503                 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
8504                                  cq_event, struct lpfc_cq_event, list);
8505                 spin_unlock_irq(&phba->hbalock);
8506                 /* Notify aborted XRI for ELS work queue */
8507                 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
8508                 /* Free the event processed back to the free pool */
8509                 lpfc_sli4_cq_event_release(phba, cq_event);
8510         }
8511 }
8512
8513 /**
8514  * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
8515  * @phba: pointer to lpfc hba data structure
8516  * @pIocbIn: pointer to the rspiocbq
8517  * @pIocbOut: pointer to the cmdiocbq
8518  * @wcqe: pointer to the complete wcqe
8519  *
8520  * This routine transfers the fields of a command iocbq to a response iocbq
8521  * by copying all the IOCB fields from command iocbq and transferring the
8522  * completion status information from the complete wcqe.
8523  **/
8524 static void
8525 lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
8526                               struct lpfc_iocbq *pIocbIn,
8527                               struct lpfc_iocbq *pIocbOut,
8528                               struct lpfc_wcqe_complete *wcqe)
8529 {
8530         unsigned long iflags;
8531         size_t offset = offsetof(struct lpfc_iocbq, iocb);
8532
8533         memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
8534                sizeof(struct lpfc_iocbq) - offset);
8535         /* Map WCQE parameters into irspiocb parameters */
8536         pIocbIn->iocb.ulpStatus = bf_get(lpfc_wcqe_c_status, wcqe);
8537         if (pIocbOut->iocb_flag & LPFC_IO_FCP)
8538                 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
8539                         pIocbIn->iocb.un.fcpi.fcpi_parm =
8540                                         pIocbOut->iocb.un.fcpi.fcpi_parm -
8541                                         wcqe->total_data_placed;
8542                 else
8543                         pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
8544         else {
8545                 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
8546                 pIocbIn->iocb.un.genreq64.bdl.bdeSize = wcqe->total_data_placed;
8547         }
8548
8549         /* Pick up HBA exchange busy condition */
8550         if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
8551                 spin_lock_irqsave(&phba->hbalock, iflags);
8552                 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
8553                 spin_unlock_irqrestore(&phba->hbalock, iflags);
8554         }
8555 }
8556
8557 /**
8558  * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
8559  * @phba: Pointer to HBA context object.
8560  * @wcqe: Pointer to work-queue completion queue entry.
8561  *
8562  * This routine handles an ELS work-queue completion event and construct
8563  * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
8564  * discovery engine to handle.
8565  *
8566  * Return: Pointer to the receive IOCBQ, NULL otherwise.
8567  **/
8568 static struct lpfc_iocbq *
8569 lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
8570                                struct lpfc_iocbq *irspiocbq)
8571 {
8572         struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
8573         struct lpfc_iocbq *cmdiocbq;
8574         struct lpfc_wcqe_complete *wcqe;
8575         unsigned long iflags;
8576
8577         wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
8578         spin_lock_irqsave(&phba->hbalock, iflags);
8579         pring->stats.iocb_event++;
8580         /* Look up the ELS command IOCB and create pseudo response IOCB */
8581         cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
8582                                 bf_get(lpfc_wcqe_c_request_tag, wcqe));
8583         spin_unlock_irqrestore(&phba->hbalock, iflags);
8584
8585         if (unlikely(!cmdiocbq)) {
8586                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8587                                 "0386 ELS complete with no corresponding "
8588                                 "cmdiocb: iotag (%d)\n",
8589                                 bf_get(lpfc_wcqe_c_request_tag, wcqe));
8590                 lpfc_sli_release_iocbq(phba, irspiocbq);
8591                 return NULL;
8592         }
8593
8594         /* Fake the irspiocbq and copy necessary response information */
8595         lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
8596
8597         return irspiocbq;
8598 }
8599
8600 /**
8601  * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
8602  * @phba: Pointer to HBA context object.
8603  * @cqe: Pointer to mailbox completion queue entry.
8604  *
8605  * This routine process a mailbox completion queue entry with asynchrous
8606  * event.
8607  *
8608  * Return: true if work posted to worker thread, otherwise false.
8609  **/
8610 static bool
8611 lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
8612 {
8613         struct lpfc_cq_event *cq_event;
8614         unsigned long iflags;
8615
8616         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8617                         "0392 Async Event: word0:x%x, word1:x%x, "
8618                         "word2:x%x, word3:x%x\n", mcqe->word0,
8619                         mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
8620
8621         /* Allocate a new internal CQ_EVENT entry */
8622         cq_event = lpfc_sli4_cq_event_alloc(phba);
8623         if (!cq_event) {
8624                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8625                                 "0394 Failed to allocate CQ_EVENT entry\n");
8626                 return false;
8627         }
8628
8629         /* Move the CQE into an asynchronous event entry */
8630         memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
8631         spin_lock_irqsave(&phba->hbalock, iflags);
8632         list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
8633         /* Set the async event flag */
8634         phba->hba_flag |= ASYNC_EVENT;
8635         spin_unlock_irqrestore(&phba->hbalock, iflags);
8636
8637         return true;
8638 }
8639
8640 /**
8641  * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
8642  * @phba: Pointer to HBA context object.
8643  * @cqe: Pointer to mailbox completion queue entry.
8644  *
8645  * This routine process a mailbox completion queue entry with mailbox
8646  * completion event.
8647  *
8648  * Return: true if work posted to worker thread, otherwise false.
8649  **/
8650 static bool
8651 lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
8652 {
8653         uint32_t mcqe_status;
8654         MAILBOX_t *mbox, *pmbox;
8655         struct lpfc_mqe *mqe;
8656         struct lpfc_vport *vport;
8657         struct lpfc_nodelist *ndlp;
8658         struct lpfc_dmabuf *mp;
8659         unsigned long iflags;
8660         LPFC_MBOXQ_t *pmb;
8661         bool workposted = false;
8662         int rc;
8663
8664         /* If not a mailbox complete MCQE, out by checking mailbox consume */
8665         if (!bf_get(lpfc_trailer_completed, mcqe))
8666                 goto out_no_mqe_complete;
8667
8668         /* Get the reference to the active mbox command */
8669         spin_lock_irqsave(&phba->hbalock, iflags);
8670         pmb = phba->sli.mbox_active;
8671         if (unlikely(!pmb)) {
8672                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
8673                                 "1832 No pending MBOX command to handle\n");
8674                 spin_unlock_irqrestore(&phba->hbalock, iflags);
8675                 goto out_no_mqe_complete;
8676         }
8677         spin_unlock_irqrestore(&phba->hbalock, iflags);
8678         mqe = &pmb->u.mqe;
8679         pmbox = (MAILBOX_t *)&pmb->u.mqe;
8680         mbox = phba->mbox;
8681         vport = pmb->vport;
8682
8683         /* Reset heartbeat timer */
8684         phba->last_completion_time = jiffies;
8685         del_timer(&phba->sli.mbox_tmo);
8686
8687         /* Move mbox data to caller's mailbox region, do endian swapping */
8688         if (pmb->mbox_cmpl && mbox)
8689                 lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
8690         /* Set the mailbox status with SLI4 range 0x4000 */
8691         mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
8692         if (mcqe_status != MB_CQE_STATUS_SUCCESS)
8693                 bf_set(lpfc_mqe_status, mqe,
8694                        (LPFC_MBX_ERROR_RANGE | mcqe_status));
8695
8696         if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
8697                 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
8698                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
8699                                       "MBOX dflt rpi: status:x%x rpi:x%x",
8700                                       mcqe_status,
8701                                       pmbox->un.varWords[0], 0);
8702                 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
8703                         mp = (struct lpfc_dmabuf *)(pmb->context1);
8704                         ndlp = (struct lpfc_nodelist *)pmb->context2;
8705                         /* Reg_LOGIN of dflt RPI was successful. Now lets get
8706                          * RID of the PPI using the same mbox buffer.
8707                          */
8708                         lpfc_unreg_login(phba, vport->vpi,
8709                                          pmbox->un.varWords[0], pmb);
8710                         pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
8711                         pmb->context1 = mp;
8712                         pmb->context2 = ndlp;
8713                         pmb->vport = vport;
8714                         rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
8715                         if (rc != MBX_BUSY)
8716                                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
8717                                                 LOG_SLI, "0385 rc should "
8718                                                 "have been MBX_BUSY\n");
8719                         if (rc != MBX_NOT_FINISHED)
8720                                 goto send_current_mbox;
8721                 }
8722         }
8723         spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
8724         phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
8725         spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
8726
8727         /* There is mailbox completion work to do */
8728         spin_lock_irqsave(&phba->hbalock, iflags);
8729         __lpfc_mbox_cmpl_put(phba, pmb);
8730         phba->work_ha |= HA_MBATT;
8731         spin_unlock_irqrestore(&phba->hbalock, iflags);
8732         workposted = true;
8733
8734 send_current_mbox:
8735         spin_lock_irqsave(&phba->hbalock, iflags);
8736         /* Release the mailbox command posting token */
8737         phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8738         /* Setting active mailbox pointer need to be in sync to flag clear */
8739         phba->sli.mbox_active = NULL;
8740         spin_unlock_irqrestore(&phba->hbalock, iflags);
8741         /* Wake up worker thread to post the next pending mailbox command */
8742         lpfc_worker_wake_up(phba);
8743 out_no_mqe_complete:
8744         if (bf_get(lpfc_trailer_consumed, mcqe))
8745                 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
8746         return workposted;
8747 }
8748
8749 /**
8750  * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
8751  * @phba: Pointer to HBA context object.
8752  * @cqe: Pointer to mailbox completion queue entry.
8753  *
8754  * This routine process a mailbox completion queue entry, it invokes the
8755  * proper mailbox complete handling or asynchrous event handling routine
8756  * according to the MCQE's async bit.
8757  *
8758  * Return: true if work posted to worker thread, otherwise false.
8759  **/
8760 static bool
8761 lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
8762 {
8763         struct lpfc_mcqe mcqe;
8764         bool workposted;
8765
8766         /* Copy the mailbox MCQE and convert endian order as needed */
8767         lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
8768
8769         /* Invoke the proper event handling routine */
8770         if (!bf_get(lpfc_trailer_async, &mcqe))
8771                 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
8772         else
8773                 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
8774         return workposted;
8775 }
8776
8777 /**
8778  * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
8779  * @phba: Pointer to HBA context object.
8780  * @wcqe: Pointer to work-queue completion queue entry.
8781  *
8782  * This routine handles an ELS work-queue completion event.
8783  *
8784  * Return: true if work posted to worker thread, otherwise false.
8785  **/
8786 static bool
8787 lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba,
8788                              struct lpfc_wcqe_complete *wcqe)
8789 {
8790         struct lpfc_iocbq *irspiocbq;
8791         unsigned long iflags;
8792
8793         /* Get an irspiocbq for later ELS response processing use */
8794         irspiocbq = lpfc_sli_get_iocbq(phba);
8795         if (!irspiocbq) {
8796                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8797                                 "0387 Failed to allocate an iocbq\n");
8798                 return false;
8799         }
8800
8801         /* Save off the slow-path queue event for work thread to process */
8802         memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
8803         spin_lock_irqsave(&phba->hbalock, iflags);
8804         list_add_tail(&irspiocbq->cq_event.list,
8805                       &phba->sli4_hba.sp_queue_event);
8806         phba->hba_flag |= HBA_SP_QUEUE_EVT;
8807         spin_unlock_irqrestore(&phba->hbalock, iflags);
8808
8809         return true;
8810 }
8811
8812 /**
8813  * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
8814  * @phba: Pointer to HBA context object.
8815  * @wcqe: Pointer to work-queue completion queue entry.
8816  *
8817  * This routine handles slow-path WQ entry comsumed event by invoking the
8818  * proper WQ release routine to the slow-path WQ.
8819  **/
8820 static void
8821 lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
8822                              struct lpfc_wcqe_release *wcqe)
8823 {
8824         /* Check for the slow-path ELS work queue */
8825         if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
8826                 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
8827                                      bf_get(lpfc_wcqe_r_wqe_index, wcqe));
8828         else
8829                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8830                                 "2579 Slow-path wqe consume event carries "
8831                                 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
8832                                 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
8833                                 phba->sli4_hba.els_wq->queue_id);
8834 }
8835
8836 /**
8837  * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
8838  * @phba: Pointer to HBA context object.
8839  * @cq: Pointer to a WQ completion queue.
8840  * @wcqe: Pointer to work-queue completion queue entry.
8841  *
8842  * This routine handles an XRI abort event.
8843  *
8844  * Return: true if work posted to worker thread, otherwise false.
8845  **/
8846 static bool
8847 lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
8848                                    struct lpfc_queue *cq,
8849                                    struct sli4_wcqe_xri_aborted *wcqe)
8850 {
8851         bool workposted = false;
8852         struct lpfc_cq_event *cq_event;
8853         unsigned long iflags;
8854
8855         /* Allocate a new internal CQ_EVENT entry */
8856         cq_event = lpfc_sli4_cq_event_alloc(phba);
8857         if (!cq_event) {
8858                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8859                                 "0602 Failed to allocate CQ_EVENT entry\n");
8860                 return false;
8861         }
8862
8863         /* Move the CQE into the proper xri abort event list */
8864         memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
8865         switch (cq->subtype) {
8866         case LPFC_FCP:
8867                 spin_lock_irqsave(&phba->hbalock, iflags);
8868                 list_add_tail(&cq_event->list,
8869                               &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
8870                 /* Set the fcp xri abort event flag */
8871                 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
8872                 spin_unlock_irqrestore(&phba->hbalock, iflags);
8873                 workposted = true;
8874                 break;
8875         case LPFC_ELS:
8876                 spin_lock_irqsave(&phba->hbalock, iflags);
8877                 list_add_tail(&cq_event->list,
8878                               &phba->sli4_hba.sp_els_xri_aborted_work_queue);
8879                 /* Set the els xri abort event flag */
8880                 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
8881                 spin_unlock_irqrestore(&phba->hbalock, iflags);
8882                 workposted = true;
8883                 break;
8884         default:
8885                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8886                                 "0603 Invalid work queue CQE subtype (x%x)\n",
8887                                 cq->subtype);
8888                 workposted = false;
8889                 break;
8890         }
8891         return workposted;
8892 }
8893
8894 /**
8895  * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
8896  * @phba: Pointer to HBA context object.
8897  * @rcqe: Pointer to receive-queue completion queue entry.
8898  *
8899  * This routine process a receive-queue completion queue entry.
8900  *
8901  * Return: true if work posted to worker thread, otherwise false.
8902  **/
8903 static bool
8904 lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
8905 {
8906         bool workposted = false;
8907         struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
8908         struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
8909         struct hbq_dmabuf *dma_buf;
8910         uint32_t status;
8911         unsigned long iflags;
8912
8913         if (bf_get(lpfc_rcqe_rq_id, rcqe) != hrq->queue_id)
8914                 goto out;
8915
8916         status = bf_get(lpfc_rcqe_status, rcqe);
8917         switch (status) {
8918         case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
8919                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8920                                 "2537 Receive Frame Truncated!!\n");
8921         case FC_STATUS_RQ_SUCCESS:
8922                 lpfc_sli4_rq_release(hrq, drq);
8923                 spin_lock_irqsave(&phba->hbalock, iflags);
8924                 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
8925                 if (!dma_buf) {
8926                         spin_unlock_irqrestore(&phba->hbalock, iflags);
8927                         goto out;
8928                 }
8929                 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
8930                 /* save off the frame for the word thread to process */
8931                 list_add_tail(&dma_buf->cq_event.list,
8932                               &phba->sli4_hba.sp_queue_event);
8933                 /* Frame received */
8934                 phba->hba_flag |= HBA_SP_QUEUE_EVT;
8935                 spin_unlock_irqrestore(&phba->hbalock, iflags);
8936                 workposted = true;
8937                 break;
8938         case FC_STATUS_INSUFF_BUF_NEED_BUF:
8939         case FC_STATUS_INSUFF_BUF_FRM_DISC:
8940                 /* Post more buffers if possible */
8941                 spin_lock_irqsave(&phba->hbalock, iflags);
8942                 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
8943                 spin_unlock_irqrestore(&phba->hbalock, iflags);
8944                 workposted = true;
8945                 break;
8946         }
8947 out:
8948         return workposted;
8949 }
8950
8951 /**
8952  * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
8953  * @phba: Pointer to HBA context object.
8954  * @cq: Pointer to the completion queue.
8955  * @wcqe: Pointer to a completion queue entry.
8956  *
8957  * This routine process a slow-path work-queue or recieve queue completion queue
8958  * entry.
8959  *
8960  * Return: true if work posted to worker thread, otherwise false.
8961  **/
8962 static bool
8963 lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
8964                          struct lpfc_cqe *cqe)
8965 {
8966         struct lpfc_cqe cqevt;
8967         bool workposted = false;
8968
8969         /* Copy the work queue CQE and convert endian order if needed */
8970         lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
8971
8972         /* Check and process for different type of WCQE and dispatch */
8973         switch (bf_get(lpfc_cqe_code, &cqevt)) {
8974         case CQE_CODE_COMPL_WQE:
8975                 /* Process the WQ/RQ complete event */
8976                 workposted = lpfc_sli4_sp_handle_els_wcqe(phba,
8977                                 (struct lpfc_wcqe_complete *)&cqevt);
8978                 break;
8979         case CQE_CODE_RELEASE_WQE:
8980                 /* Process the WQ release event */
8981                 lpfc_sli4_sp_handle_rel_wcqe(phba,
8982                                 (struct lpfc_wcqe_release *)&cqevt);
8983                 break;
8984         case CQE_CODE_XRI_ABORTED:
8985                 /* Process the WQ XRI abort event */
8986                 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
8987                                 (struct sli4_wcqe_xri_aborted *)&cqevt);
8988                 break;
8989         case CQE_CODE_RECEIVE:
8990                 /* Process the RQ event */
8991                 workposted = lpfc_sli4_sp_handle_rcqe(phba,
8992                                 (struct lpfc_rcqe *)&cqevt);
8993                 break;
8994         default:
8995                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8996                                 "0388 Not a valid WCQE code: x%x\n",
8997                                 bf_get(lpfc_cqe_code, &cqevt));
8998                 break;
8999         }
9000         return workposted;
9001 }
9002
9003 /**
9004  * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
9005  * @phba: Pointer to HBA context object.
9006  * @eqe: Pointer to fast-path event queue entry.
9007  *
9008  * This routine process a event queue entry from the slow-path event queue.
9009  * It will check the MajorCode and MinorCode to determine this is for a
9010  * completion event on a completion queue, if not, an error shall be logged
9011  * and just return. Otherwise, it will get to the corresponding completion
9012  * queue and process all the entries on that completion queue, rearm the
9013  * completion queue, and then return.
9014  *
9015  **/
9016 static void
9017 lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
9018 {
9019         struct lpfc_queue *cq = NULL, *childq, *speq;
9020         struct lpfc_cqe *cqe;
9021         bool workposted = false;
9022         int ecount = 0;
9023         uint16_t cqid;
9024
9025         if (bf_get_le32(lpfc_eqe_major_code, eqe) != 0) {
9026                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9027                                 "0359 Not a valid slow-path completion "
9028                                 "event: majorcode=x%x, minorcode=x%x\n",
9029                                 bf_get_le32(lpfc_eqe_major_code, eqe),
9030                                 bf_get_le32(lpfc_eqe_minor_code, eqe));
9031                 return;
9032         }
9033
9034         /* Get the reference to the corresponding CQ */
9035         cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
9036
9037         /* Search for completion queue pointer matching this cqid */
9038         speq = phba->sli4_hba.sp_eq;
9039         list_for_each_entry(childq, &speq->child_list, list) {
9040                 if (childq->queue_id == cqid) {
9041                         cq = childq;
9042                         break;
9043                 }
9044         }
9045         if (unlikely(!cq)) {
9046                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9047                                 "0365 Slow-path CQ identifier (%d) does "
9048                                 "not exist\n", cqid);
9049                 return;
9050         }
9051
9052         /* Process all the entries to the CQ */
9053         switch (cq->type) {
9054         case LPFC_MCQ:
9055                 while ((cqe = lpfc_sli4_cq_get(cq))) {
9056                         workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
9057                         if (!(++ecount % LPFC_GET_QE_REL_INT))
9058                                 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
9059                 }
9060                 break;
9061         case LPFC_WCQ:
9062                 while ((cqe = lpfc_sli4_cq_get(cq))) {
9063                         workposted |= lpfc_sli4_sp_handle_cqe(phba, cq, cqe);
9064                         if (!(++ecount % LPFC_GET_QE_REL_INT))
9065                                 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
9066                 }
9067                 break;
9068         default:
9069                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9070                                 "0370 Invalid completion queue type (%d)\n",
9071                                 cq->type);
9072                 return;
9073         }
9074
9075         /* Catch the no cq entry condition, log an error */
9076         if (unlikely(ecount == 0))
9077                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9078                                 "0371 No entry from the CQ: identifier "
9079                                 "(x%x), type (%d)\n", cq->queue_id, cq->type);
9080
9081         /* In any case, flash and re-arm the RCQ */
9082         lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
9083
9084         /* wake up worker thread if there are works to be done */
9085         if (workposted)
9086                 lpfc_worker_wake_up(phba);
9087 }
9088
9089 /**
9090  * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
9091  * @eqe: Pointer to fast-path completion queue entry.
9092  *
9093  * This routine process a fast-path work queue completion entry from fast-path
9094  * event queue for FCP command response completion.
9095  **/
9096 static void
9097 lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba,
9098                              struct lpfc_wcqe_complete *wcqe)
9099 {
9100         struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
9101         struct lpfc_iocbq *cmdiocbq;
9102         struct lpfc_iocbq irspiocbq;
9103         unsigned long iflags;
9104
9105         spin_lock_irqsave(&phba->hbalock, iflags);
9106         pring->stats.iocb_event++;
9107         spin_unlock_irqrestore(&phba->hbalock, iflags);
9108
9109         /* Check for response status */
9110         if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
9111                 /* If resource errors reported from HBA, reduce queue
9112                  * depth of the SCSI device.
9113                  */
9114                 if ((bf_get(lpfc_wcqe_c_status, wcqe) ==
9115                      IOSTAT_LOCAL_REJECT) &&
9116                     (wcqe->parameter == IOERR_NO_RESOURCES)) {
9117                         phba->lpfc_rampdown_queue_depth(phba);
9118                 }
9119                 /* Log the error status */
9120                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9121                                 "0373 FCP complete error: status=x%x, "
9122                                 "hw_status=x%x, total_data_specified=%d, "
9123                                 "parameter=x%x, word3=x%x\n",
9124                                 bf_get(lpfc_wcqe_c_status, wcqe),
9125                                 bf_get(lpfc_wcqe_c_hw_status, wcqe),
9126                                 wcqe->total_data_placed, wcqe->parameter,
9127                                 wcqe->word3);
9128         }
9129
9130         /* Look up the FCP command IOCB and create pseudo response IOCB */
9131         spin_lock_irqsave(&phba->hbalock, iflags);
9132         cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
9133                                 bf_get(lpfc_wcqe_c_request_tag, wcqe));
9134         spin_unlock_irqrestore(&phba->hbalock, iflags);
9135         if (unlikely(!cmdiocbq)) {
9136                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9137                                 "0374 FCP complete with no corresponding "
9138                                 "cmdiocb: iotag (%d)\n",
9139                                 bf_get(lpfc_wcqe_c_request_tag, wcqe));
9140                 return;
9141         }
9142         if (unlikely(!cmdiocbq->iocb_cmpl)) {
9143                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9144                                 "0375 FCP cmdiocb not callback function "
9145                                 "iotag: (%d)\n",
9146                                 bf_get(lpfc_wcqe_c_request_tag, wcqe));
9147                 return;
9148         }
9149
9150         /* Fake the irspiocb and copy necessary response information */
9151         lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
9152
9153         if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
9154                 spin_lock_irqsave(&phba->hbalock, iflags);
9155                 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
9156                 spin_unlock_irqrestore(&phba->hbalock, iflags);
9157         }
9158
9159         /* Pass the cmd_iocb and the rsp state to the upper layer */
9160         (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
9161 }
9162
9163 /**
9164  * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
9165  * @phba: Pointer to HBA context object.
9166  * @cq: Pointer to completion queue.
9167  * @wcqe: Pointer to work-queue completion queue entry.
9168  *
9169  * This routine handles an fast-path WQ entry comsumed event by invoking the
9170  * proper WQ release routine to the slow-path WQ.
9171  **/
9172 static void
9173 lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
9174                              struct lpfc_wcqe_release *wcqe)
9175 {
9176         struct lpfc_queue *childwq;
9177         bool wqid_matched = false;
9178         uint16_t fcp_wqid;
9179
9180         /* Check for fast-path FCP work queue release */
9181         fcp_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
9182         list_for_each_entry(childwq, &cq->child_list, list) {
9183                 if (childwq->queue_id == fcp_wqid) {
9184                         lpfc_sli4_wq_release(childwq,
9185                                         bf_get(lpfc_wcqe_r_wqe_index, wcqe));
9186                         wqid_matched = true;
9187                         break;
9188                 }
9189         }
9190         /* Report warning log message if no match found */
9191         if (wqid_matched != true)
9192                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9193                                 "2580 Fast-path wqe consume event carries "
9194                                 "miss-matched qid: wcqe-qid=x%x\n", fcp_wqid);
9195 }
9196
9197 /**
9198  * lpfc_sli4_fp_handle_wcqe - Process fast-path work queue completion entry
9199  * @cq: Pointer to the completion queue.
9200  * @eqe: Pointer to fast-path completion queue entry.
9201  *
9202  * This routine process a fast-path work queue completion entry from fast-path
9203  * event queue for FCP command response completion.
9204  **/
9205 static int
9206 lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
9207                          struct lpfc_cqe *cqe)
9208 {
9209         struct lpfc_wcqe_release wcqe;
9210         bool workposted = false;
9211
9212         /* Copy the work queue CQE and convert endian order if needed */
9213         lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
9214
9215         /* Check and process for different type of WCQE and dispatch */
9216         switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
9217         case CQE_CODE_COMPL_WQE:
9218                 /* Process the WQ complete event */
9219                 lpfc_sli4_fp_handle_fcp_wcqe(phba,
9220                                 (struct lpfc_wcqe_complete *)&wcqe);
9221                 break;
9222         case CQE_CODE_RELEASE_WQE:
9223                 /* Process the WQ release event */
9224                 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
9225                                 (struct lpfc_wcqe_release *)&wcqe);
9226                 break;
9227         case CQE_CODE_XRI_ABORTED:
9228                 /* Process the WQ XRI abort event */
9229                 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
9230                                 (struct sli4_wcqe_xri_aborted *)&wcqe);
9231                 break;
9232         default:
9233                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9234                                 "0144 Not a valid WCQE code: x%x\n",
9235                                 bf_get(lpfc_wcqe_c_code, &wcqe));
9236                 break;
9237         }
9238         return workposted;
9239 }
9240
9241 /**
9242  * lpfc_sli4_fp_handle_eqe - Process a fast-path event queue entry
9243  * @phba: Pointer to HBA context object.
9244  * @eqe: Pointer to fast-path event queue entry.
9245  *
9246  * This routine process a event queue entry from the fast-path event queue.
9247  * It will check the MajorCode and MinorCode to determine this is for a
9248  * completion event on a completion queue, if not, an error shall be logged
9249  * and just return. Otherwise, it will get to the corresponding completion
9250  * queue and process all the entries on the completion queue, rearm the
9251  * completion queue, and then return.
9252  **/
9253 static void
9254 lpfc_sli4_fp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
9255                         uint32_t fcp_cqidx)
9256 {
9257         struct lpfc_queue *cq;
9258         struct lpfc_cqe *cqe;
9259         bool workposted = false;
9260         uint16_t cqid;
9261         int ecount = 0;
9262
9263         if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
9264                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9265                                 "0366 Not a valid fast-path completion "
9266                                 "event: majorcode=x%x, minorcode=x%x\n",
9267                                 bf_get_le32(lpfc_eqe_major_code, eqe),
9268                                 bf_get_le32(lpfc_eqe_minor_code, eqe));
9269                 return;
9270         }
9271
9272         cq = phba->sli4_hba.fcp_cq[fcp_cqidx];
9273         if (unlikely(!cq)) {
9274                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9275                                 "0367 Fast-path completion queue does not "
9276                                 "exist\n");
9277                 return;
9278         }
9279
9280         /* Get the reference to the corresponding CQ */
9281         cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
9282         if (unlikely(cqid != cq->queue_id)) {
9283                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9284                                 "0368 Miss-matched fast-path completion "
9285                                 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
9286                                 cqid, cq->queue_id);
9287                 return;
9288         }
9289
9290         /* Process all the entries to the CQ */
9291         while ((cqe = lpfc_sli4_cq_get(cq))) {
9292                 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
9293                 if (!(++ecount % LPFC_GET_QE_REL_INT))
9294                         lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
9295         }
9296
9297         /* Catch the no cq entry condition */
9298         if (unlikely(ecount == 0))
9299                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9300                                 "0369 No entry from fast-path completion "
9301                                 "queue fcpcqid=%d\n", cq->queue_id);
9302
9303         /* In any case, flash and re-arm the CQ */
9304         lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
9305
9306         /* wake up worker thread if there are works to be done */
9307         if (workposted)
9308                 lpfc_worker_wake_up(phba);
9309 }
9310
9311 static void
9312 lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
9313 {
9314         struct lpfc_eqe *eqe;
9315
9316         /* walk all the EQ entries and drop on the floor */
9317         while ((eqe = lpfc_sli4_eq_get(eq)))
9318                 ;
9319
9320         /* Clear and re-arm the EQ */
9321         lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
9322 }
9323
9324 /**
9325  * lpfc_sli4_sp_intr_handler - Slow-path interrupt handler to SLI-4 device
9326  * @irq: Interrupt number.
9327  * @dev_id: The device context pointer.
9328  *
9329  * This function is directly called from the PCI layer as an interrupt
9330  * service routine when device with SLI-4 interface spec is enabled with
9331  * MSI-X multi-message interrupt mode and there are slow-path events in
9332  * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
9333  * interrupt mode, this function is called as part of the device-level
9334  * interrupt handler. When the PCI slot is in error recovery or the HBA is
9335  * undergoing initialization, the interrupt handler will not process the
9336  * interrupt. The link attention and ELS ring attention events are handled
9337  * by the worker thread. The interrupt handler signals the worker thread
9338  * and returns for these events. This function is called without any lock
9339  * held. It gets the hbalock to access and update SLI data structures.
9340  *
9341  * This function returns IRQ_HANDLED when interrupt is handled else it
9342  * returns IRQ_NONE.
9343  **/
9344 irqreturn_t
9345 lpfc_sli4_sp_intr_handler(int irq, void *dev_id)
9346 {
9347         struct lpfc_hba *phba;
9348         struct lpfc_queue *speq;
9349         struct lpfc_eqe *eqe;
9350         unsigned long iflag;
9351         int ecount = 0;
9352
9353         /*
9354          * Get the driver's phba structure from the dev_id
9355          */
9356         phba = (struct lpfc_hba *)dev_id;
9357
9358         if (unlikely(!phba))
9359                 return IRQ_NONE;
9360
9361         /* Get to the EQ struct associated with this vector */
9362         speq = phba->sli4_hba.sp_eq;
9363
9364         /* Check device state for handling interrupt */
9365         if (unlikely(lpfc_intr_state_check(phba))) {
9366                 /* Check again for link_state with lock held */
9367                 spin_lock_irqsave(&phba->hbalock, iflag);
9368                 if (phba->link_state < LPFC_LINK_DOWN)
9369                         /* Flush, clear interrupt, and rearm the EQ */
9370                         lpfc_sli4_eq_flush(phba, speq);
9371                 spin_unlock_irqrestore(&phba->hbalock, iflag);
9372                 return IRQ_NONE;
9373         }
9374
9375         /*
9376          * Process all the event on FCP slow-path EQ
9377          */
9378         while ((eqe = lpfc_sli4_eq_get(speq))) {
9379                 lpfc_sli4_sp_handle_eqe(phba, eqe);
9380                 if (!(++ecount % LPFC_GET_QE_REL_INT))
9381                         lpfc_sli4_eq_release(speq, LPFC_QUEUE_NOARM);
9382         }
9383
9384         /* Always clear and re-arm the slow-path EQ */
9385         lpfc_sli4_eq_release(speq, LPFC_QUEUE_REARM);
9386
9387         /* Catch the no cq entry condition */
9388         if (unlikely(ecount == 0)) {
9389                 if (phba->intr_type == MSIX)
9390                         /* MSI-X treated interrupt served as no EQ share INT */
9391                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9392                                         "0357 MSI-X interrupt with no EQE\n");
9393                 else
9394                         /* Non MSI-X treated on interrupt as EQ share INT */
9395                         return IRQ_NONE;
9396         }
9397
9398         return IRQ_HANDLED;
9399 } /* lpfc_sli4_sp_intr_handler */
9400
9401 /**
9402  * lpfc_sli4_fp_intr_handler - Fast-path interrupt handler to SLI-4 device
9403  * @irq: Interrupt number.
9404  * @dev_id: The device context pointer.
9405  *
9406  * This function is directly called from the PCI layer as an interrupt
9407  * service routine when device with SLI-4 interface spec is enabled with
9408  * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
9409  * ring event in the HBA. However, when the device is enabled with either
9410  * MSI or Pin-IRQ interrupt mode, this function is called as part of the
9411  * device-level interrupt handler. When the PCI slot is in error recovery
9412  * or the HBA is undergoing initialization, the interrupt handler will not
9413  * process the interrupt. The SCSI FCP fast-path ring event are handled in
9414  * the intrrupt context. This function is called without any lock held.
9415  * It gets the hbalock to access and update SLI data structures. Note that,
9416  * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
9417  * equal to that of FCP CQ index.
9418  *
9419  * This function returns IRQ_HANDLED when interrupt is handled else it
9420  * returns IRQ_NONE.
9421  **/
9422 irqreturn_t
9423 lpfc_sli4_fp_intr_handler(int irq, void *dev_id)
9424 {
9425         struct lpfc_hba *phba;
9426         struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
9427         struct lpfc_queue *fpeq;
9428         struct lpfc_eqe *eqe;
9429         unsigned long iflag;
9430         int ecount = 0;
9431         uint32_t fcp_eqidx;
9432
9433         /* Get the driver's phba structure from the dev_id */
9434         fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
9435         phba = fcp_eq_hdl->phba;
9436         fcp_eqidx = fcp_eq_hdl->idx;
9437
9438         if (unlikely(!phba))
9439                 return IRQ_NONE;
9440
9441         /* Get to the EQ struct associated with this vector */
9442         fpeq = phba->sli4_hba.fp_eq[fcp_eqidx];
9443
9444         /* Check device state for handling interrupt */
9445         if (unlikely(lpfc_intr_state_check(phba))) {
9446                 /* Check again for link_state with lock held */
9447                 spin_lock_irqsave(&phba->hbalock, iflag);
9448                 if (phba->link_state < LPFC_LINK_DOWN)
9449                         /* Flush, clear interrupt, and rearm the EQ */
9450                         lpfc_sli4_eq_flush(phba, fpeq);
9451                 spin_unlock_irqrestore(&phba->hbalock, iflag);
9452                 return IRQ_NONE;
9453         }
9454
9455         /*
9456          * Process all the event on FCP fast-path EQ
9457          */
9458         while ((eqe = lpfc_sli4_eq_get(fpeq))) {
9459                 lpfc_sli4_fp_handle_eqe(phba, eqe, fcp_eqidx);
9460                 if (!(++ecount % LPFC_GET_QE_REL_INT))
9461                         lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
9462         }
9463
9464         /* Always clear and re-arm the fast-path EQ */
9465         lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
9466
9467         if (unlikely(ecount == 0)) {
9468                 if (phba->intr_type == MSIX)
9469                         /* MSI-X treated interrupt served as no EQ share INT */
9470                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9471                                         "0358 MSI-X interrupt with no EQE\n");
9472                 else
9473                         /* Non MSI-X treated on interrupt as EQ share INT */
9474                         return IRQ_NONE;
9475         }
9476
9477         return IRQ_HANDLED;
9478 } /* lpfc_sli4_fp_intr_handler */
9479
9480 /**
9481  * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
9482  * @irq: Interrupt number.
9483  * @dev_id: The device context pointer.
9484  *
9485  * This function is the device-level interrupt handler to device with SLI-4
9486  * interface spec, called from the PCI layer when either MSI or Pin-IRQ
9487  * interrupt mode is enabled and there is an event in the HBA which requires
9488  * driver attention. This function invokes the slow-path interrupt attention
9489  * handling function and fast-path interrupt attention handling function in
9490  * turn to process the relevant HBA attention events. This function is called
9491  * without any lock held. It gets the hbalock to access and update SLI data
9492  * structures.
9493  *
9494  * This function returns IRQ_HANDLED when interrupt is handled, else it
9495  * returns IRQ_NONE.
9496  **/
9497 irqreturn_t
9498 lpfc_sli4_intr_handler(int irq, void *dev_id)
9499 {
9500         struct lpfc_hba  *phba;
9501         irqreturn_t sp_irq_rc, fp_irq_rc;
9502         bool fp_handled = false;
9503         uint32_t fcp_eqidx;
9504
9505         /* Get the driver's phba structure from the dev_id */
9506         phba = (struct lpfc_hba *)dev_id;
9507
9508         if (unlikely(!phba))
9509                 return IRQ_NONE;
9510
9511         /*
9512          * Invokes slow-path host attention interrupt handling as appropriate.
9513          */
9514         sp_irq_rc = lpfc_sli4_sp_intr_handler(irq, dev_id);
9515
9516         /*
9517          * Invoke fast-path host attention interrupt handling as appropriate.
9518          */
9519         for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
9520                 fp_irq_rc = lpfc_sli4_fp_intr_handler(irq,
9521                                         &phba->sli4_hba.fcp_eq_hdl[fcp_eqidx]);
9522                 if (fp_irq_rc == IRQ_HANDLED)
9523                         fp_handled |= true;
9524         }
9525
9526         return (fp_handled == true) ? IRQ_HANDLED : sp_irq_rc;
9527 } /* lpfc_sli4_intr_handler */
9528
9529 /**
9530  * lpfc_sli4_queue_free - free a queue structure and associated memory
9531  * @queue: The queue structure to free.
9532  *
9533  * This function frees a queue structure and the DMAable memeory used for
9534  * the host resident queue. This function must be called after destroying the
9535  * queue on the HBA.
9536  **/
9537 void
9538 lpfc_sli4_queue_free(struct lpfc_queue *queue)
9539 {
9540         struct lpfc_dmabuf *dmabuf;
9541
9542         if (!queue)
9543                 return;
9544
9545         while (!list_empty(&queue->page_list)) {
9546                 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
9547                                  list);
9548                 dma_free_coherent(&queue->phba->pcidev->dev, SLI4_PAGE_SIZE,
9549                                   dmabuf->virt, dmabuf->phys);
9550                 kfree(dmabuf);
9551         }
9552         kfree(queue);
9553         return;
9554 }
9555
9556 /**
9557  * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
9558  * @phba: The HBA that this queue is being created on.
9559  * @entry_size: The size of each queue entry for this queue.
9560  * @entry count: The number of entries that this queue will handle.
9561  *
9562  * This function allocates a queue structure and the DMAable memory used for
9563  * the host resident queue. This function must be called before creating the
9564  * queue on the HBA.
9565  **/
9566 struct lpfc_queue *
9567 lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
9568                       uint32_t entry_count)
9569 {
9570         struct lpfc_queue *queue;
9571         struct lpfc_dmabuf *dmabuf;
9572         int x, total_qe_count;
9573         void *dma_pointer;
9574         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
9575
9576         if (!phba->sli4_hba.pc_sli4_params.supported)
9577                 hw_page_size = SLI4_PAGE_SIZE;
9578
9579         queue = kzalloc(sizeof(struct lpfc_queue) +
9580                         (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
9581         if (!queue)
9582                 return NULL;
9583         queue->page_count = (ALIGN(entry_size * entry_count,
9584                         hw_page_size))/hw_page_size;
9585         INIT_LIST_HEAD(&queue->list);
9586         INIT_LIST_HEAD(&queue->page_list);
9587         INIT_LIST_HEAD(&queue->child_list);
9588         for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
9589                 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
9590                 if (!dmabuf)
9591                         goto out_fail;
9592                 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
9593                                                   hw_page_size, &dmabuf->phys,
9594                                                   GFP_KERNEL);
9595                 if (!dmabuf->virt) {
9596                         kfree(dmabuf);
9597                         goto out_fail;
9598                 }
9599                 memset(dmabuf->virt, 0, hw_page_size);
9600                 dmabuf->buffer_tag = x;
9601                 list_add_tail(&dmabuf->list, &queue->page_list);
9602                 /* initialize queue's entry array */
9603                 dma_pointer = dmabuf->virt;
9604                 for (; total_qe_count < entry_count &&
9605                      dma_pointer < (hw_page_size + dmabuf->virt);
9606                      total_qe_count++, dma_pointer += entry_size) {
9607                         queue->qe[total_qe_count].address = dma_pointer;
9608                 }
9609         }
9610         queue->entry_size = entry_size;
9611         queue->entry_count = entry_count;
9612         queue->phba = phba;
9613
9614         return queue;
9615 out_fail:
9616         lpfc_sli4_queue_free(queue);
9617         return NULL;
9618 }
9619
9620 /**
9621  * lpfc_eq_create - Create an Event Queue on the HBA
9622  * @phba: HBA structure that indicates port to create a queue on.
9623  * @eq: The queue structure to use to create the event queue.
9624  * @imax: The maximum interrupt per second limit.
9625  *
9626  * This function creates an event queue, as detailed in @eq, on a port,
9627  * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
9628  *
9629  * The @phba struct is used to send mailbox command to HBA. The @eq struct
9630  * is used to get the entry count and entry size that are necessary to
9631  * determine the number of pages to allocate and use for this queue. This
9632  * function will send the EQ_CREATE mailbox command to the HBA to setup the
9633  * event queue. This function is asynchronous and will wait for the mailbox
9634  * command to finish before continuing.
9635  *
9636  * On success this function will return a zero. If unable to allocate enough
9637  * memory this function will return ENOMEM. If the queue create mailbox command
9638  * fails this function will return ENXIO.
9639  **/
9640 uint32_t
9641 lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint16_t imax)
9642 {
9643         struct lpfc_mbx_eq_create *eq_create;
9644         LPFC_MBOXQ_t *mbox;
9645         int rc, length, status = 0;
9646         struct lpfc_dmabuf *dmabuf;
9647         uint32_t shdr_status, shdr_add_status;
9648         union lpfc_sli4_cfg_shdr *shdr;
9649         uint16_t dmult;
9650         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
9651
9652         if (!phba->sli4_hba.pc_sli4_params.supported)
9653                 hw_page_size = SLI4_PAGE_SIZE;
9654
9655         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9656         if (!mbox)
9657                 return -ENOMEM;
9658         length = (sizeof(struct lpfc_mbx_eq_create) -
9659                   sizeof(struct lpfc_sli4_cfg_mhdr));
9660         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
9661                          LPFC_MBOX_OPCODE_EQ_CREATE,
9662                          length, LPFC_SLI4_MBX_EMBED);
9663         eq_create = &mbox->u.mqe.un.eq_create;
9664         bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
9665                eq->page_count);
9666         bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
9667                LPFC_EQE_SIZE);
9668         bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
9669         /* Calculate delay multiper from maximum interrupt per second */
9670         dmult = LPFC_DMULT_CONST/imax - 1;
9671         bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
9672                dmult);
9673         switch (eq->entry_count) {
9674         default:
9675                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9676                                 "0360 Unsupported EQ count. (%d)\n",
9677                                 eq->entry_count);
9678                 if (eq->entry_count < 256)
9679                         return -EINVAL;
9680                 /* otherwise default to smallest count (drop through) */
9681         case 256:
9682                 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9683                        LPFC_EQ_CNT_256);
9684                 break;
9685         case 512:
9686                 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9687                        LPFC_EQ_CNT_512);
9688                 break;
9689         case 1024:
9690                 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9691                        LPFC_EQ_CNT_1024);
9692                 break;
9693         case 2048:
9694                 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9695                        LPFC_EQ_CNT_2048);
9696                 break;
9697         case 4096:
9698                 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9699                        LPFC_EQ_CNT_4096);
9700                 break;
9701         }
9702         list_for_each_entry(dmabuf, &eq->page_list, list) {
9703                 memset(dmabuf->virt, 0, hw_page_size);
9704                 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9705                                         putPaddrLow(dmabuf->phys);
9706                 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9707                                         putPaddrHigh(dmabuf->phys);
9708         }
9709         mbox->vport = phba->pport;
9710         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
9711         mbox->context1 = NULL;
9712         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9713         shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
9714         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9715         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9716         if (shdr_status || shdr_add_status || rc) {
9717                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9718                                 "2500 EQ_CREATE mailbox failed with "
9719                                 "status x%x add_status x%x, mbx status x%x\n",
9720                                 shdr_status, shdr_add_status, rc);
9721                 status = -ENXIO;
9722         }
9723         eq->type = LPFC_EQ;
9724         eq->subtype = LPFC_NONE;
9725         eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
9726         if (eq->queue_id == 0xFFFF)
9727                 status = -ENXIO;
9728         eq->host_index = 0;
9729         eq->hba_index = 0;
9730
9731         mempool_free(mbox, phba->mbox_mem_pool);
9732         return status;
9733 }
9734
9735 /**
9736  * lpfc_cq_create - Create a Completion Queue on the HBA
9737  * @phba: HBA structure that indicates port to create a queue on.
9738  * @cq: The queue structure to use to create the completion queue.
9739  * @eq: The event queue to bind this completion queue to.
9740  *
9741  * This function creates a completion queue, as detailed in @wq, on a port,
9742  * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
9743  *
9744  * The @phba struct is used to send mailbox command to HBA. The @cq struct
9745  * is used to get the entry count and entry size that are necessary to
9746  * determine the number of pages to allocate and use for this queue. The @eq
9747  * is used to indicate which event queue to bind this completion queue to. This
9748  * function will send the CQ_CREATE mailbox command to the HBA to setup the
9749  * completion queue. This function is asynchronous and will wait for the mailbox
9750  * command to finish before continuing.
9751  *
9752  * On success this function will return a zero. If unable to allocate enough
9753  * memory this function will return ENOMEM. If the queue create mailbox command
9754  * fails this function will return ENXIO.
9755  **/
9756 uint32_t
9757 lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
9758                struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
9759 {
9760         struct lpfc_mbx_cq_create *cq_create;
9761         struct lpfc_dmabuf *dmabuf;
9762         LPFC_MBOXQ_t *mbox;
9763         int rc, length, status = 0;
9764         uint32_t shdr_status, shdr_add_status;
9765         union lpfc_sli4_cfg_shdr *shdr;
9766         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
9767
9768         if (!phba->sli4_hba.pc_sli4_params.supported)
9769                 hw_page_size = SLI4_PAGE_SIZE;
9770
9771
9772         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9773         if (!mbox)
9774                 return -ENOMEM;
9775         length = (sizeof(struct lpfc_mbx_cq_create) -
9776                   sizeof(struct lpfc_sli4_cfg_mhdr));
9777         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
9778                          LPFC_MBOX_OPCODE_CQ_CREATE,
9779                          length, LPFC_SLI4_MBX_EMBED);
9780         cq_create = &mbox->u.mqe.un.cq_create;
9781         bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
9782                     cq->page_count);
9783         bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
9784         bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
9785         bf_set(lpfc_cq_eq_id, &cq_create->u.request.context, eq->queue_id);
9786         switch (cq->entry_count) {
9787         default:
9788                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9789                                 "0361 Unsupported CQ count. (%d)\n",
9790                                 cq->entry_count);
9791                 if (cq->entry_count < 256)
9792                         return -EINVAL;
9793                 /* otherwise default to smallest count (drop through) */
9794         case 256:
9795                 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
9796                        LPFC_CQ_CNT_256);
9797                 break;
9798         case 512:
9799                 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
9800                        LPFC_CQ_CNT_512);
9801                 break;
9802         case 1024:
9803                 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
9804                        LPFC_CQ_CNT_1024);
9805                 break;
9806         }
9807         list_for_each_entry(dmabuf, &cq->page_list, list) {
9808                 memset(dmabuf->virt, 0, hw_page_size);
9809                 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9810                                         putPaddrLow(dmabuf->phys);
9811                 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9812                                         putPaddrHigh(dmabuf->phys);
9813         }
9814         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9815
9816         /* The IOCTL status is embedded in the mailbox subheader. */
9817         shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
9818         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9819         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9820         if (shdr_status || shdr_add_status || rc) {
9821                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9822                                 "2501 CQ_CREATE mailbox failed with "
9823                                 "status x%x add_status x%x, mbx status x%x\n",
9824                                 shdr_status, shdr_add_status, rc);
9825                 status = -ENXIO;
9826                 goto out;
9827         }
9828         cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
9829         if (cq->queue_id == 0xFFFF) {
9830                 status = -ENXIO;
9831                 goto out;
9832         }
9833         /* link the cq onto the parent eq child list */
9834         list_add_tail(&cq->list, &eq->child_list);
9835         /* Set up completion queue's type and subtype */
9836         cq->type = type;
9837         cq->subtype = subtype;
9838         cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
9839         cq->host_index = 0;
9840         cq->hba_index = 0;
9841
9842 out:
9843         mempool_free(mbox, phba->mbox_mem_pool);
9844         return status;
9845 }
9846
9847 /**
9848  * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
9849  * @phba: HBA structure that indicates port to create a queue on.
9850  * @mq: The queue structure to use to create the mailbox queue.
9851  * @mbox: An allocated pointer to type LPFC_MBOXQ_t
9852  * @cq: The completion queue to associate with this cq.
9853  *
9854  * This function provides failback (fb) functionality when the
9855  * mq_create_ext fails on older FW generations.  It's purpose is identical
9856  * to mq_create_ext otherwise.
9857  *
9858  * This routine cannot fail as all attributes were previously accessed and
9859  * initialized in mq_create_ext.
9860  **/
9861 static void
9862 lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
9863                        LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
9864 {
9865         struct lpfc_mbx_mq_create *mq_create;
9866         struct lpfc_dmabuf *dmabuf;
9867         int length;
9868
9869         length = (sizeof(struct lpfc_mbx_mq_create) -
9870                   sizeof(struct lpfc_sli4_cfg_mhdr));
9871         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
9872                          LPFC_MBOX_OPCODE_MQ_CREATE,
9873                          length, LPFC_SLI4_MBX_EMBED);
9874         mq_create = &mbox->u.mqe.un.mq_create;
9875         bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
9876                mq->page_count);
9877         bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
9878                cq->queue_id);
9879         bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
9880         switch (mq->entry_count) {
9881         case 16:
9882                 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
9883                        LPFC_MQ_CNT_16);
9884                 break;
9885         case 32:
9886                 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
9887                        LPFC_MQ_CNT_32);
9888                 break;
9889         case 64:
9890                 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
9891                        LPFC_MQ_CNT_64);
9892                 break;
9893         case 128:
9894                 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
9895                        LPFC_MQ_CNT_128);
9896                 break;
9897         }
9898         list_for_each_entry(dmabuf, &mq->page_list, list) {
9899                 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9900                         putPaddrLow(dmabuf->phys);
9901                 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9902                         putPaddrHigh(dmabuf->phys);
9903         }
9904 }
9905
9906 /**
9907  * lpfc_mq_create - Create a mailbox Queue on the HBA
9908  * @phba: HBA structure that indicates port to create a queue on.
9909  * @mq: The queue structure to use to create the mailbox queue.
9910  * @cq: The completion queue to associate with this cq.
9911  * @subtype: The queue's subtype.
9912  *
9913  * This function creates a mailbox queue, as detailed in @mq, on a port,
9914  * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
9915  *
9916  * The @phba struct is used to send mailbox command to HBA. The @cq struct
9917  * is used to get the entry count and entry size that are necessary to
9918  * determine the number of pages to allocate and use for this queue. This
9919  * function will send the MQ_CREATE mailbox command to the HBA to setup the
9920  * mailbox queue. This function is asynchronous and will wait for the mailbox
9921  * command to finish before continuing.
9922  *
9923  * On success this function will return a zero. If unable to allocate enough
9924  * memory this function will return ENOMEM. If the queue create mailbox command
9925  * fails this function will return ENXIO.
9926  **/
9927 int32_t
9928 lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
9929                struct lpfc_queue *cq, uint32_t subtype)
9930 {
9931         struct lpfc_mbx_mq_create *mq_create;
9932         struct lpfc_mbx_mq_create_ext *mq_create_ext;
9933         struct lpfc_dmabuf *dmabuf;
9934         LPFC_MBOXQ_t *mbox;
9935         int rc, length, status = 0;
9936         uint32_t shdr_status, shdr_add_status;
9937         union lpfc_sli4_cfg_shdr *shdr;
9938         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
9939
9940         if (!phba->sli4_hba.pc_sli4_params.supported)
9941                 hw_page_size = SLI4_PAGE_SIZE;
9942
9943         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9944         if (!mbox)
9945                 return -ENOMEM;
9946         length = (sizeof(struct lpfc_mbx_mq_create_ext) -
9947                   sizeof(struct lpfc_sli4_cfg_mhdr));
9948         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
9949                          LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
9950                          length, LPFC_SLI4_MBX_EMBED);
9951
9952         mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
9953         bf_set(lpfc_mbx_mq_create_ext_num_pages, &mq_create_ext->u.request,
9954                     mq->page_count);
9955         bf_set(lpfc_mbx_mq_create_ext_async_evt_link, &mq_create_ext->u.request,
9956                1);
9957         bf_set(lpfc_mbx_mq_create_ext_async_evt_fcfste,
9958                &mq_create_ext->u.request, 1);
9959         bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
9960                &mq_create_ext->u.request, 1);
9961         bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
9962                cq->queue_id);
9963         bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
9964         switch (mq->entry_count) {
9965         default:
9966                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9967                                 "0362 Unsupported MQ count. (%d)\n",
9968                                 mq->entry_count);
9969                 if (mq->entry_count < 16)
9970                         return -EINVAL;
9971                 /* otherwise default to smallest count (drop through) */
9972         case 16:
9973                 bf_set(lpfc_mq_context_count, &mq_create_ext->u.request.context,
9974                        LPFC_MQ_CNT_16);
9975                 break;
9976         case 32:
9977                 bf_set(lpfc_mq_context_count, &mq_create_ext->u.request.context,
9978                        LPFC_MQ_CNT_32);
9979                 break;
9980         case 64:
9981                 bf_set(lpfc_mq_context_count, &mq_create_ext->u.request.context,
9982                        LPFC_MQ_CNT_64);
9983                 break;
9984         case 128:
9985                 bf_set(lpfc_mq_context_count, &mq_create_ext->u.request.context,
9986                        LPFC_MQ_CNT_128);
9987                 break;
9988         }
9989         list_for_each_entry(dmabuf, &mq->page_list, list) {
9990                 memset(dmabuf->virt, 0, hw_page_size);
9991                 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
9992                                         putPaddrLow(dmabuf->phys);
9993                 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
9994                                         putPaddrHigh(dmabuf->phys);
9995         }
9996         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9997         shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
9998         mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
9999                               &mq_create_ext->u.response);
10000         if (rc != MBX_SUCCESS) {
10001                 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10002                                 "2795 MQ_CREATE_EXT failed with "
10003                                 "status x%x. Failback to MQ_CREATE.\n",
10004                                 rc);
10005                 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
10006                 mq_create = &mbox->u.mqe.un.mq_create;
10007                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10008                 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
10009                 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
10010                                       &mq_create->u.response);
10011         }
10012
10013         /* The IOCTL status is embedded in the mailbox subheader. */
10014         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10015         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10016         if (shdr_status || shdr_add_status || rc) {
10017                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10018                                 "2502 MQ_CREATE mailbox failed with "
10019                                 "status x%x add_status x%x, mbx status x%x\n",
10020                                 shdr_status, shdr_add_status, rc);
10021                 status = -ENXIO;
10022                 goto out;
10023         }
10024         if (mq->queue_id == 0xFFFF) {
10025                 status = -ENXIO;
10026                 goto out;
10027         }
10028         mq->type = LPFC_MQ;
10029         mq->subtype = subtype;
10030         mq->host_index = 0;
10031         mq->hba_index = 0;
10032
10033         /* link the mq onto the parent cq child list */
10034         list_add_tail(&mq->list, &cq->child_list);
10035 out:
10036         mempool_free(mbox, phba->mbox_mem_pool);
10037         return status;
10038 }
10039
10040 /**
10041  * lpfc_wq_create - Create a Work Queue on the HBA
10042  * @phba: HBA structure that indicates port to create a queue on.
10043  * @wq: The queue structure to use to create the work queue.
10044  * @cq: The completion queue to bind this work queue to.
10045  * @subtype: The subtype of the work queue indicating its functionality.
10046  *
10047  * This function creates a work queue, as detailed in @wq, on a port, described
10048  * by @phba by sending a WQ_CREATE mailbox command to the HBA.
10049  *
10050  * The @phba struct is used to send mailbox command to HBA. The @wq struct
10051  * is used to get the entry count and entry size that are necessary to
10052  * determine the number of pages to allocate and use for this queue. The @cq
10053  * is used to indicate which completion queue to bind this work queue to. This
10054  * function will send the WQ_CREATE mailbox command to the HBA to setup the
10055  * work queue. This function is asynchronous and will wait for the mailbox
10056  * command to finish before continuing.
10057  *
10058  * On success this function will return a zero. If unable to allocate enough
10059  * memory this function will return ENOMEM. If the queue create mailbox command
10060  * fails this function will return ENXIO.
10061  **/
10062 uint32_t
10063 lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
10064                struct lpfc_queue *cq, uint32_t subtype)
10065 {
10066         struct lpfc_mbx_wq_create *wq_create;
10067         struct lpfc_dmabuf *dmabuf;
10068         LPFC_MBOXQ_t *mbox;
10069         int rc, length, status = 0;
10070         uint32_t shdr_status, shdr_add_status;
10071         union lpfc_sli4_cfg_shdr *shdr;
10072         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
10073
10074         if (!phba->sli4_hba.pc_sli4_params.supported)
10075                 hw_page_size = SLI4_PAGE_SIZE;
10076
10077         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10078         if (!mbox)
10079                 return -ENOMEM;
10080         length = (sizeof(struct lpfc_mbx_wq_create) -
10081                   sizeof(struct lpfc_sli4_cfg_mhdr));
10082         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10083                          LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
10084                          length, LPFC_SLI4_MBX_EMBED);
10085         wq_create = &mbox->u.mqe.un.wq_create;
10086         bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
10087                     wq->page_count);
10088         bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
10089                     cq->queue_id);
10090         list_for_each_entry(dmabuf, &wq->page_list, list) {
10091                 memset(dmabuf->virt, 0, hw_page_size);
10092                 wq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
10093                                         putPaddrLow(dmabuf->phys);
10094                 wq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
10095                                         putPaddrHigh(dmabuf->phys);
10096         }
10097         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10098         /* The IOCTL status is embedded in the mailbox subheader. */
10099         shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
10100         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10101         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10102         if (shdr_status || shdr_add_status || rc) {
10103                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10104                                 "2503 WQ_CREATE mailbox failed with "
10105                                 "status x%x add_status x%x, mbx status x%x\n",
10106                                 shdr_status, shdr_add_status, rc);
10107                 status = -ENXIO;
10108                 goto out;
10109         }
10110         wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
10111         if (wq->queue_id == 0xFFFF) {
10112                 status = -ENXIO;
10113                 goto out;
10114         }
10115         wq->type = LPFC_WQ;
10116         wq->subtype = subtype;
10117         wq->host_index = 0;
10118         wq->hba_index = 0;
10119
10120         /* link the wq onto the parent cq child list */
10121         list_add_tail(&wq->list, &cq->child_list);
10122 out:
10123         mempool_free(mbox, phba->mbox_mem_pool);
10124         return status;
10125 }
10126
10127 /**
10128  * lpfc_rq_create - Create a Receive Queue on the HBA
10129  * @phba: HBA structure that indicates port to create a queue on.
10130  * @hrq: The queue structure to use to create the header receive queue.
10131  * @drq: The queue structure to use to create the data receive queue.
10132  * @cq: The completion queue to bind this work queue to.
10133  *
10134  * This function creates a receive buffer queue pair , as detailed in @hrq and
10135  * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
10136  * to the HBA.
10137  *
10138  * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
10139  * struct is used to get the entry count that is necessary to determine the
10140  * number of pages to use for this queue. The @cq is used to indicate which
10141  * completion queue to bind received buffers that are posted to these queues to.
10142  * This function will send the RQ_CREATE mailbox command to the HBA to setup the
10143  * receive queue pair. This function is asynchronous and will wait for the
10144  * mailbox command to finish before continuing.
10145  *
10146  * On success this function will return a zero. If unable to allocate enough
10147  * memory this function will return ENOMEM. If the queue create mailbox command
10148  * fails this function will return ENXIO.
10149  **/
10150 uint32_t
10151 lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
10152                struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
10153 {
10154         struct lpfc_mbx_rq_create *rq_create;
10155         struct lpfc_dmabuf *dmabuf;
10156         LPFC_MBOXQ_t *mbox;
10157         int rc, length, status = 0;
10158         uint32_t shdr_status, shdr_add_status;
10159         union lpfc_sli4_cfg_shdr *shdr;
10160         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
10161
10162         if (!phba->sli4_hba.pc_sli4_params.supported)
10163                 hw_page_size = SLI4_PAGE_SIZE;
10164
10165         if (hrq->entry_count != drq->entry_count)
10166                 return -EINVAL;
10167         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10168         if (!mbox)
10169                 return -ENOMEM;
10170         length = (sizeof(struct lpfc_mbx_rq_create) -
10171                   sizeof(struct lpfc_sli4_cfg_mhdr));
10172         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10173                          LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
10174                          length, LPFC_SLI4_MBX_EMBED);
10175         rq_create = &mbox->u.mqe.un.rq_create;
10176         switch (hrq->entry_count) {
10177         default:
10178                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10179                                 "2535 Unsupported RQ count. (%d)\n",
10180                                 hrq->entry_count);
10181                 if (hrq->entry_count < 512)
10182                         return -EINVAL;
10183                 /* otherwise default to smallest count (drop through) */
10184         case 512:
10185                 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10186                        LPFC_RQ_RING_SIZE_512);
10187                 break;
10188         case 1024:
10189                 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10190                        LPFC_RQ_RING_SIZE_1024);
10191                 break;
10192         case 2048:
10193                 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10194                        LPFC_RQ_RING_SIZE_2048);
10195                 break;
10196         case 4096:
10197                 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10198                        LPFC_RQ_RING_SIZE_4096);
10199                 break;
10200         }
10201         bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
10202                cq->queue_id);
10203         bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
10204                hrq->page_count);
10205         bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
10206                LPFC_HDR_BUF_SIZE);
10207         list_for_each_entry(dmabuf, &hrq->page_list, list) {
10208                 memset(dmabuf->virt, 0, hw_page_size);
10209                 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
10210                                         putPaddrLow(dmabuf->phys);
10211                 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
10212                                         putPaddrHigh(dmabuf->phys);
10213         }
10214         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10215         /* The IOCTL status is embedded in the mailbox subheader. */
10216         shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
10217         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10218         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10219         if (shdr_status || shdr_add_status || rc) {
10220                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10221                                 "2504 RQ_CREATE mailbox failed with "
10222                                 "status x%x add_status x%x, mbx status x%x\n",
10223                                 shdr_status, shdr_add_status, rc);
10224                 status = -ENXIO;
10225                 goto out;
10226         }
10227         hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
10228         if (hrq->queue_id == 0xFFFF) {
10229                 status = -ENXIO;
10230                 goto out;
10231         }
10232         hrq->type = LPFC_HRQ;
10233         hrq->subtype = subtype;
10234         hrq->host_index = 0;
10235         hrq->hba_index = 0;
10236
10237         /* now create the data queue */
10238         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10239                          LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
10240                          length, LPFC_SLI4_MBX_EMBED);
10241         switch (drq->entry_count) {
10242         default:
10243                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10244                                 "2536 Unsupported RQ count. (%d)\n",
10245                                 drq->entry_count);
10246                 if (drq->entry_count < 512)
10247                         return -EINVAL;
10248                 /* otherwise default to smallest count (drop through) */
10249         case 512:
10250                 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10251                        LPFC_RQ_RING_SIZE_512);
10252                 break;
10253         case 1024:
10254                 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10255                        LPFC_RQ_RING_SIZE_1024);
10256                 break;
10257         case 2048:
10258                 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10259                        LPFC_RQ_RING_SIZE_2048);
10260                 break;
10261         case 4096:
10262                 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10263                        LPFC_RQ_RING_SIZE_4096);
10264                 break;
10265         }
10266         bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
10267                cq->queue_id);
10268         bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
10269                drq->page_count);
10270         bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
10271                LPFC_DATA_BUF_SIZE);
10272         list_for_each_entry(dmabuf, &drq->page_list, list) {
10273                 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
10274                                         putPaddrLow(dmabuf->phys);
10275                 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
10276                                         putPaddrHigh(dmabuf->phys);
10277         }
10278         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10279         /* The IOCTL status is embedded in the mailbox subheader. */
10280         shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
10281         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10282         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10283         if (shdr_status || shdr_add_status || rc) {
10284                 status = -ENXIO;
10285                 goto out;
10286         }
10287         drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
10288         if (drq->queue_id == 0xFFFF) {
10289                 status = -ENXIO;
10290                 goto out;
10291         }
10292         drq->type = LPFC_DRQ;
10293         drq->subtype = subtype;
10294         drq->host_index = 0;
10295         drq->hba_index = 0;
10296
10297         /* link the header and data RQs onto the parent cq child list */
10298         list_add_tail(&hrq->list, &cq->child_list);
10299         list_add_tail(&drq->list, &cq->child_list);
10300
10301 out:
10302         mempool_free(mbox, phba->mbox_mem_pool);
10303         return status;
10304 }
10305
10306 /**
10307  * lpfc_eq_destroy - Destroy an event Queue on the HBA
10308  * @eq: The queue structure associated with the queue to destroy.
10309  *
10310  * This function destroys a queue, as detailed in @eq by sending an mailbox
10311  * command, specific to the type of queue, to the HBA.
10312  *
10313  * The @eq struct is used to get the queue ID of the queue to destroy.
10314  *
10315  * On success this function will return a zero. If the queue destroy mailbox
10316  * command fails this function will return ENXIO.
10317  **/
10318 uint32_t
10319 lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
10320 {
10321         LPFC_MBOXQ_t *mbox;
10322         int rc, length, status = 0;
10323         uint32_t shdr_status, shdr_add_status;
10324         union lpfc_sli4_cfg_shdr *shdr;
10325
10326         if (!eq)
10327                 return -ENODEV;
10328         mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
10329         if (!mbox)
10330                 return -ENOMEM;
10331         length = (sizeof(struct lpfc_mbx_eq_destroy) -
10332                   sizeof(struct lpfc_sli4_cfg_mhdr));
10333         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10334                          LPFC_MBOX_OPCODE_EQ_DESTROY,
10335                          length, LPFC_SLI4_MBX_EMBED);
10336         bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
10337                eq->queue_id);
10338         mbox->vport = eq->phba->pport;
10339         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10340
10341         rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
10342         /* The IOCTL status is embedded in the mailbox subheader. */
10343         shdr = (union lpfc_sli4_cfg_shdr *)
10344                 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
10345         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10346         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10347         if (shdr_status || shdr_add_status || rc) {
10348                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10349                                 "2505 EQ_DESTROY mailbox failed with "
10350                                 "status x%x add_status x%x, mbx status x%x\n",
10351                                 shdr_status, shdr_add_status, rc);
10352                 status = -ENXIO;
10353         }
10354
10355         /* Remove eq from any list */
10356         list_del_init(&eq->list);
10357         mempool_free(mbox, eq->phba->mbox_mem_pool);
10358         return status;
10359 }
10360
10361 /**
10362  * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
10363  * @cq: The queue structure associated with the queue to destroy.
10364  *
10365  * This function destroys a queue, as detailed in @cq by sending an mailbox
10366  * command, specific to the type of queue, to the HBA.
10367  *
10368  * The @cq struct is used to get the queue ID of the queue to destroy.
10369  *
10370  * On success this function will return a zero. If the queue destroy mailbox
10371  * command fails this function will return ENXIO.
10372  **/
10373 uint32_t
10374 lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
10375 {
10376         LPFC_MBOXQ_t *mbox;
10377         int rc, length, status = 0;
10378         uint32_t shdr_status, shdr_add_status;
10379         union lpfc_sli4_cfg_shdr *shdr;
10380
10381         if (!cq)
10382                 return -ENODEV;
10383         mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
10384         if (!mbox)
10385                 return -ENOMEM;
10386         length = (sizeof(struct lpfc_mbx_cq_destroy) -
10387                   sizeof(struct lpfc_sli4_cfg_mhdr));
10388         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10389                          LPFC_MBOX_OPCODE_CQ_DESTROY,
10390                          length, LPFC_SLI4_MBX_EMBED);
10391         bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
10392                cq->queue_id);
10393         mbox->vport = cq->phba->pport;
10394         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10395         rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
10396         /* The IOCTL status is embedded in the mailbox subheader. */
10397         shdr = (union lpfc_sli4_cfg_shdr *)
10398                 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
10399         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10400         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10401         if (shdr_status || shdr_add_status || rc) {
10402                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10403                                 "2506 CQ_DESTROY mailbox failed with "
10404                                 "status x%x add_status x%x, mbx status x%x\n",
10405                                 shdr_status, shdr_add_status, rc);
10406                 status = -ENXIO;
10407         }
10408         /* Remove cq from any list */
10409         list_del_init(&cq->list);
10410         mempool_free(mbox, cq->phba->mbox_mem_pool);
10411         return status;
10412 }
10413
10414 /**
10415  * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
10416  * @qm: The queue structure associated with the queue to destroy.
10417  *
10418  * This function destroys a queue, as detailed in @mq by sending an mailbox
10419  * command, specific to the type of queue, to the HBA.
10420  *
10421  * The @mq struct is used to get the queue ID of the queue to destroy.
10422  *
10423  * On success this function will return a zero. If the queue destroy mailbox
10424  * command fails this function will return ENXIO.
10425  **/
10426 uint32_t
10427 lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
10428 {
10429         LPFC_MBOXQ_t *mbox;
10430         int rc, length, status = 0;
10431         uint32_t shdr_status, shdr_add_status;
10432         union lpfc_sli4_cfg_shdr *shdr;
10433
10434         if (!mq)
10435                 return -ENODEV;
10436         mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
10437         if (!mbox)
10438                 return -ENOMEM;
10439         length = (sizeof(struct lpfc_mbx_mq_destroy) -
10440                   sizeof(struct lpfc_sli4_cfg_mhdr));
10441         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10442                          LPFC_MBOX_OPCODE_MQ_DESTROY,
10443                          length, LPFC_SLI4_MBX_EMBED);
10444         bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
10445                mq->queue_id);
10446         mbox->vport = mq->phba->pport;
10447         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10448         rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
10449         /* The IOCTL status is embedded in the mailbox subheader. */
10450         shdr = (union lpfc_sli4_cfg_shdr *)
10451                 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
10452         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10453         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10454         if (shdr_status || shdr_add_status || rc) {
10455                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10456                                 "2507 MQ_DESTROY mailbox failed with "
10457                                 "status x%x add_status x%x, mbx status x%x\n",
10458                                 shdr_status, shdr_add_status, rc);
10459                 status = -ENXIO;
10460         }
10461         /* Remove mq from any list */
10462         list_del_init(&mq->list);
10463         mempool_free(mbox, mq->phba->mbox_mem_pool);
10464         return status;
10465 }
10466
10467 /**
10468  * lpfc_wq_destroy - Destroy a Work Queue on the HBA
10469  * @wq: The queue structure associated with the queue to destroy.
10470  *
10471  * This function destroys a queue, as detailed in @wq by sending an mailbox
10472  * command, specific to the type of queue, to the HBA.
10473  *
10474  * The @wq struct is used to get the queue ID of the queue to destroy.
10475  *
10476  * On success this function will return a zero. If the queue destroy mailbox
10477  * command fails this function will return ENXIO.
10478  **/
10479 uint32_t
10480 lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
10481 {
10482         LPFC_MBOXQ_t *mbox;
10483         int rc, length, status = 0;
10484         uint32_t shdr_status, shdr_add_status;
10485         union lpfc_sli4_cfg_shdr *shdr;
10486
10487         if (!wq)
10488                 return -ENODEV;
10489         mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
10490         if (!mbox)
10491                 return -ENOMEM;
10492         length = (sizeof(struct lpfc_mbx_wq_destroy) -
10493                   sizeof(struct lpfc_sli4_cfg_mhdr));
10494         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10495                          LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
10496                          length, LPFC_SLI4_MBX_EMBED);
10497         bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
10498                wq->queue_id);
10499         mbox->vport = wq->phba->pport;
10500         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10501         rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
10502         shdr = (union lpfc_sli4_cfg_shdr *)
10503                 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
10504         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10505         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10506         if (shdr_status || shdr_add_status || rc) {
10507                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10508                                 "2508 WQ_DESTROY mailbox failed with "
10509                                 "status x%x add_status x%x, mbx status x%x\n",
10510                                 shdr_status, shdr_add_status, rc);
10511                 status = -ENXIO;
10512         }
10513         /* Remove wq from any list */
10514         list_del_init(&wq->list);
10515         mempool_free(mbox, wq->phba->mbox_mem_pool);
10516         return status;
10517 }
10518
10519 /**
10520  * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
10521  * @rq: The queue structure associated with the queue to destroy.
10522  *
10523  * This function destroys a queue, as detailed in @rq by sending an mailbox
10524  * command, specific to the type of queue, to the HBA.
10525  *
10526  * The @rq struct is used to get the queue ID of the queue to destroy.
10527  *
10528  * On success this function will return a zero. If the queue destroy mailbox
10529  * command fails this function will return ENXIO.
10530  **/
10531 uint32_t
10532 lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
10533                 struct lpfc_queue *drq)
10534 {
10535         LPFC_MBOXQ_t *mbox;
10536         int rc, length, status = 0;
10537         uint32_t shdr_status, shdr_add_status;
10538         union lpfc_sli4_cfg_shdr *shdr;
10539
10540         if (!hrq || !drq)
10541                 return -ENODEV;
10542         mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
10543         if (!mbox)
10544                 return -ENOMEM;
10545         length = (sizeof(struct lpfc_mbx_rq_destroy) -
10546                   sizeof(struct mbox_header));
10547         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10548                          LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
10549                          length, LPFC_SLI4_MBX_EMBED);
10550         bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
10551                hrq->queue_id);
10552         mbox->vport = hrq->phba->pport;
10553         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10554         rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
10555         /* The IOCTL status is embedded in the mailbox subheader. */
10556         shdr = (union lpfc_sli4_cfg_shdr *)
10557                 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
10558         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10559         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10560         if (shdr_status || shdr_add_status || rc) {
10561                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10562                                 "2509 RQ_DESTROY mailbox failed with "
10563                                 "status x%x add_status x%x, mbx status x%x\n",
10564                                 shdr_status, shdr_add_status, rc);
10565                 if (rc != MBX_TIMEOUT)
10566                         mempool_free(mbox, hrq->phba->mbox_mem_pool);
10567                 return -ENXIO;
10568         }
10569         bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
10570                drq->queue_id);
10571         rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
10572         shdr = (union lpfc_sli4_cfg_shdr *)
10573                 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
10574         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10575         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10576         if (shdr_status || shdr_add_status || rc) {
10577                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10578                                 "2510 RQ_DESTROY mailbox failed with "
10579                                 "status x%x add_status x%x, mbx status x%x\n",
10580                                 shdr_status, shdr_add_status, rc);
10581                 status = -ENXIO;
10582         }
10583         list_del_init(&hrq->list);
10584         list_del_init(&drq->list);
10585         mempool_free(mbox, hrq->phba->mbox_mem_pool);
10586         return status;
10587 }
10588
10589 /**
10590  * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
10591  * @phba: The virtual port for which this call being executed.
10592  * @pdma_phys_addr0: Physical address of the 1st SGL page.
10593  * @pdma_phys_addr1: Physical address of the 2nd SGL page.
10594  * @xritag: the xritag that ties this io to the SGL pages.
10595  *
10596  * This routine will post the sgl pages for the IO that has the xritag
10597  * that is in the iocbq structure. The xritag is assigned during iocbq
10598  * creation and persists for as long as the driver is loaded.
10599  * if the caller has fewer than 256 scatter gather segments to map then
10600  * pdma_phys_addr1 should be 0.
10601  * If the caller needs to map more than 256 scatter gather segment then
10602  * pdma_phys_addr1 should be a valid physical address.
10603  * physical address for SGLs must be 64 byte aligned.
10604  * If you are going to map 2 SGL's then the first one must have 256 entries
10605  * the second sgl can have between 1 and 256 entries.
10606  *
10607  * Return codes:
10608  *      0 - Success
10609  *      -ENXIO, -ENOMEM - Failure
10610  **/
10611 int
10612 lpfc_sli4_post_sgl(struct lpfc_hba *phba,
10613                 dma_addr_t pdma_phys_addr0,
10614                 dma_addr_t pdma_phys_addr1,
10615                 uint16_t xritag)
10616 {
10617         struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
10618         LPFC_MBOXQ_t *mbox;
10619         int rc;
10620         uint32_t shdr_status, shdr_add_status;
10621         union lpfc_sli4_cfg_shdr *shdr;
10622
10623         if (xritag == NO_XRI) {
10624                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10625                                 "0364 Invalid param:\n");
10626                 return -EINVAL;
10627         }
10628
10629         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10630         if (!mbox)
10631                 return -ENOMEM;
10632
10633         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10634                         LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
10635                         sizeof(struct lpfc_mbx_post_sgl_pages) -
10636                         sizeof(struct mbox_header), LPFC_SLI4_MBX_EMBED);
10637
10638         post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
10639                                 &mbox->u.mqe.un.post_sgl_pages;
10640         bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
10641         bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
10642
10643         post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
10644                                 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
10645         post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
10646                                 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
10647
10648         post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
10649                                 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
10650         post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
10651                                 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
10652         if (!phba->sli4_hba.intr_enable)
10653                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10654         else
10655                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
10656         /* The IOCTL status is embedded in the mailbox subheader. */
10657         shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
10658         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10659         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10660         if (rc != MBX_TIMEOUT)
10661                 mempool_free(mbox, phba->mbox_mem_pool);
10662         if (shdr_status || shdr_add_status || rc) {
10663                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10664                                 "2511 POST_SGL mailbox failed with "
10665                                 "status x%x add_status x%x, mbx status x%x\n",
10666                                 shdr_status, shdr_add_status, rc);
10667                 rc = -ENXIO;
10668         }
10669         return 0;
10670 }
10671 /**
10672  * lpfc_sli4_remove_all_sgl_pages - Post scatter gather list for an XRI to HBA
10673  * @phba: The virtual port for which this call being executed.
10674  *
10675  * This routine will remove all of the sgl pages registered with the hba.
10676  *
10677  * Return codes:
10678  *      0 - Success
10679  *      -ENXIO, -ENOMEM - Failure
10680  **/
10681 int
10682 lpfc_sli4_remove_all_sgl_pages(struct lpfc_hba *phba)
10683 {
10684         LPFC_MBOXQ_t *mbox;
10685         int rc;
10686         uint32_t shdr_status, shdr_add_status;
10687         union lpfc_sli4_cfg_shdr *shdr;
10688
10689         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10690         if (!mbox)
10691                 return -ENOMEM;
10692
10693         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10694                         LPFC_MBOX_OPCODE_FCOE_REMOVE_SGL_PAGES, 0,
10695                         LPFC_SLI4_MBX_EMBED);
10696         if (!phba->sli4_hba.intr_enable)
10697                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10698         else
10699                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
10700         /* The IOCTL status is embedded in the mailbox subheader. */
10701         shdr = (union lpfc_sli4_cfg_shdr *)
10702                 &mbox->u.mqe.un.sli4_config.header.cfg_shdr;
10703         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10704         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10705         if (rc != MBX_TIMEOUT)
10706                 mempool_free(mbox, phba->mbox_mem_pool);
10707         if (shdr_status || shdr_add_status || rc) {
10708                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10709                                 "2512 REMOVE_ALL_SGL_PAGES mailbox failed with "
10710                                 "status x%x add_status x%x, mbx status x%x\n",
10711                                 shdr_status, shdr_add_status, rc);
10712                 rc = -ENXIO;
10713         }
10714         return rc;
10715 }
10716
10717 /**
10718  * lpfc_sli4_next_xritag - Get an xritag for the io
10719  * @phba: Pointer to HBA context object.
10720  *
10721  * This function gets an xritag for the iocb. If there is no unused xritag
10722  * it will return 0xffff.
10723  * The function returns the allocated xritag if successful, else returns zero.
10724  * Zero is not a valid xritag.
10725  * The caller is not required to hold any lock.
10726  **/
10727 uint16_t
10728 lpfc_sli4_next_xritag(struct lpfc_hba *phba)
10729 {
10730         uint16_t xritag;
10731
10732         spin_lock_irq(&phba->hbalock);
10733         xritag = phba->sli4_hba.next_xri;
10734         if ((xritag != (uint16_t) -1) && xritag <
10735                 (phba->sli4_hba.max_cfg_param.max_xri
10736                         + phba->sli4_hba.max_cfg_param.xri_base)) {
10737                 phba->sli4_hba.next_xri++;
10738                 phba->sli4_hba.max_cfg_param.xri_used++;
10739                 spin_unlock_irq(&phba->hbalock);
10740                 return xritag;
10741         }
10742         spin_unlock_irq(&phba->hbalock);
10743         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
10744                         "2004 Failed to allocate XRI.last XRITAG is %d"
10745                         " Max XRI is %d, Used XRI is %d\n",
10746                         phba->sli4_hba.next_xri,
10747                         phba->sli4_hba.max_cfg_param.max_xri,
10748                         phba->sli4_hba.max_cfg_param.xri_used);
10749         return -1;
10750 }
10751
10752 /**
10753  * lpfc_sli4_post_sgl_list - post a block of sgl list to the firmware.
10754  * @phba: pointer to lpfc hba data structure.
10755  *
10756  * This routine is invoked to post a block of driver's sgl pages to the
10757  * HBA using non-embedded mailbox command. No Lock is held. This routine
10758  * is only called when the driver is loading and after all IO has been
10759  * stopped.
10760  **/
10761 int
10762 lpfc_sli4_post_sgl_list(struct lpfc_hba *phba)
10763 {
10764         struct lpfc_sglq *sglq_entry;
10765         struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
10766         struct sgl_page_pairs *sgl_pg_pairs;
10767         void *viraddr;
10768         LPFC_MBOXQ_t *mbox;
10769         uint32_t reqlen, alloclen, pg_pairs;
10770         uint32_t mbox_tmo;
10771         uint16_t xritag_start = 0;
10772         int els_xri_cnt, rc = 0;
10773         uint32_t shdr_status, shdr_add_status;
10774         union lpfc_sli4_cfg_shdr *shdr;
10775
10776         /* The number of sgls to be posted */
10777         els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
10778
10779         reqlen = els_xri_cnt * sizeof(struct sgl_page_pairs) +
10780                  sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
10781         if (reqlen > SLI4_PAGE_SIZE) {
10782                 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
10783                                 "2559 Block sgl registration required DMA "
10784                                 "size (%d) great than a page\n", reqlen);
10785                 return -ENOMEM;
10786         }
10787         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10788         if (!mbox) {
10789                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10790                                 "2560 Failed to allocate mbox cmd memory\n");
10791                 return -ENOMEM;
10792         }
10793
10794         /* Allocate DMA memory and set up the non-embedded mailbox command */
10795         alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10796                          LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
10797                          LPFC_SLI4_MBX_NEMBED);
10798
10799         if (alloclen < reqlen) {
10800                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10801                                 "0285 Allocated DMA memory size (%d) is "
10802                                 "less than the requested DMA memory "
10803                                 "size (%d)\n", alloclen, reqlen);
10804                 lpfc_sli4_mbox_cmd_free(phba, mbox);
10805                 return -ENOMEM;
10806         }
10807         /* Get the first SGE entry from the non-embedded DMA memory */
10808         viraddr = mbox->sge_array->addr[0];
10809
10810         /* Set up the SGL pages in the non-embedded DMA pages */
10811         sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
10812         sgl_pg_pairs = &sgl->sgl_pg_pairs;
10813
10814         for (pg_pairs = 0; pg_pairs < els_xri_cnt; pg_pairs++) {
10815                 sglq_entry = phba->sli4_hba.lpfc_els_sgl_array[pg_pairs];
10816                 /* Set up the sge entry */
10817                 sgl_pg_pairs->sgl_pg0_addr_lo =
10818                                 cpu_to_le32(putPaddrLow(sglq_entry->phys));
10819                 sgl_pg_pairs->sgl_pg0_addr_hi =
10820                                 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
10821                 sgl_pg_pairs->sgl_pg1_addr_lo =
10822                                 cpu_to_le32(putPaddrLow(0));
10823                 sgl_pg_pairs->sgl_pg1_addr_hi =
10824                                 cpu_to_le32(putPaddrHigh(0));
10825                 /* Keep the first xritag on the list */
10826                 if (pg_pairs == 0)
10827                         xritag_start = sglq_entry->sli4_xritag;
10828                 sgl_pg_pairs++;
10829         }
10830         bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
10831         bf_set(lpfc_post_sgl_pages_xricnt, sgl, els_xri_cnt);
10832         /* Perform endian conversion if necessary */
10833         sgl->word0 = cpu_to_le32(sgl->word0);
10834
10835         if (!phba->sli4_hba.intr_enable)
10836                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10837         else {
10838                 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
10839                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
10840         }
10841         shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
10842         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10843         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10844         if (rc != MBX_TIMEOUT)
10845                 lpfc_sli4_mbox_cmd_free(phba, mbox);
10846         if (shdr_status || shdr_add_status || rc) {
10847                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10848                                 "2513 POST_SGL_BLOCK mailbox command failed "
10849                                 "status x%x add_status x%x mbx status x%x\n",
10850                                 shdr_status, shdr_add_status, rc);
10851                 rc = -ENXIO;
10852         }
10853         return rc;
10854 }
10855
10856 /**
10857  * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
10858  * @phba: pointer to lpfc hba data structure.
10859  * @sblist: pointer to scsi buffer list.
10860  * @count: number of scsi buffers on the list.
10861  *
10862  * This routine is invoked to post a block of @count scsi sgl pages from a
10863  * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
10864  * No Lock is held.
10865  *
10866  **/
10867 int
10868 lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba, struct list_head *sblist,
10869                               int cnt)
10870 {
10871         struct lpfc_scsi_buf *psb;
10872         struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
10873         struct sgl_page_pairs *sgl_pg_pairs;
10874         void *viraddr;
10875         LPFC_MBOXQ_t *mbox;
10876         uint32_t reqlen, alloclen, pg_pairs;
10877         uint32_t mbox_tmo;
10878         uint16_t xritag_start = 0;
10879         int rc = 0;
10880         uint32_t shdr_status, shdr_add_status;
10881         dma_addr_t pdma_phys_bpl1;
10882         union lpfc_sli4_cfg_shdr *shdr;
10883
10884         /* Calculate the requested length of the dma memory */
10885         reqlen = cnt * sizeof(struct sgl_page_pairs) +
10886                  sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
10887         if (reqlen > SLI4_PAGE_SIZE) {
10888                 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
10889                                 "0217 Block sgl registration required DMA "
10890                                 "size (%d) great than a page\n", reqlen);
10891                 return -ENOMEM;
10892         }
10893         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10894         if (!mbox) {
10895                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10896                                 "0283 Failed to allocate mbox cmd memory\n");
10897                 return -ENOMEM;
10898         }
10899
10900         /* Allocate DMA memory and set up the non-embedded mailbox command */
10901         alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10902                                 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
10903                                 LPFC_SLI4_MBX_NEMBED);
10904
10905         if (alloclen < reqlen) {
10906                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10907                                 "2561 Allocated DMA memory size (%d) is "
10908                                 "less than the requested DMA memory "
10909                                 "size (%d)\n", alloclen, reqlen);
10910                 lpfc_sli4_mbox_cmd_free(phba, mbox);
10911                 return -ENOMEM;
10912         }
10913         /* Get the first SGE entry from the non-embedded DMA memory */
10914         viraddr = mbox->sge_array->addr[0];
10915
10916         /* Set up the SGL pages in the non-embedded DMA pages */
10917         sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
10918         sgl_pg_pairs = &sgl->sgl_pg_pairs;
10919
10920         pg_pairs = 0;
10921         list_for_each_entry(psb, sblist, list) {
10922                 /* Set up the sge entry */
10923                 sgl_pg_pairs->sgl_pg0_addr_lo =
10924                         cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
10925                 sgl_pg_pairs->sgl_pg0_addr_hi =
10926                         cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
10927                 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
10928                         pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
10929                 else
10930                         pdma_phys_bpl1 = 0;
10931                 sgl_pg_pairs->sgl_pg1_addr_lo =
10932                         cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
10933                 sgl_pg_pairs->sgl_pg1_addr_hi =
10934                         cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
10935                 /* Keep the first xritag on the list */
10936                 if (pg_pairs == 0)
10937                         xritag_start = psb->cur_iocbq.sli4_xritag;
10938                 sgl_pg_pairs++;
10939                 pg_pairs++;
10940         }
10941         bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
10942         bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
10943         /* Perform endian conversion if necessary */
10944         sgl->word0 = cpu_to_le32(sgl->word0);
10945
10946         if (!phba->sli4_hba.intr_enable)
10947                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10948         else {
10949                 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
10950                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
10951         }
10952         shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
10953         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10954         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10955         if (rc != MBX_TIMEOUT)
10956                 lpfc_sli4_mbox_cmd_free(phba, mbox);
10957         if (shdr_status || shdr_add_status || rc) {
10958                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10959                                 "2564 POST_SGL_BLOCK mailbox command failed "
10960                                 "status x%x add_status x%x mbx status x%x\n",
10961                                 shdr_status, shdr_add_status, rc);
10962                 rc = -ENXIO;
10963         }
10964         return rc;
10965 }
10966
10967 /**
10968  * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
10969  * @phba: pointer to lpfc_hba struct that the frame was received on
10970  * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
10971  *
10972  * This function checks the fields in the @fc_hdr to see if the FC frame is a
10973  * valid type of frame that the LPFC driver will handle. This function will
10974  * return a zero if the frame is a valid frame or a non zero value when the
10975  * frame does not pass the check.
10976  **/
10977 static int
10978 lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
10979 {
10980         char *rctl_names[] = FC_RCTL_NAMES_INIT;
10981         char *type_names[] = FC_TYPE_NAMES_INIT;
10982         struct fc_vft_header *fc_vft_hdr;
10983
10984         switch (fc_hdr->fh_r_ctl) {
10985         case FC_RCTL_DD_UNCAT:          /* uncategorized information */
10986         case FC_RCTL_DD_SOL_DATA:       /* solicited data */
10987         case FC_RCTL_DD_UNSOL_CTL:      /* unsolicited control */
10988         case FC_RCTL_DD_SOL_CTL:        /* solicited control or reply */
10989         case FC_RCTL_DD_UNSOL_DATA:     /* unsolicited data */
10990         case FC_RCTL_DD_DATA_DESC:      /* data descriptor */
10991         case FC_RCTL_DD_UNSOL_CMD:      /* unsolicited command */
10992         case FC_RCTL_DD_CMD_STATUS:     /* command status */
10993         case FC_RCTL_ELS_REQ:   /* extended link services request */
10994         case FC_RCTL_ELS_REP:   /* extended link services reply */
10995         case FC_RCTL_ELS4_REQ:  /* FC-4 ELS request */
10996         case FC_RCTL_ELS4_REP:  /* FC-4 ELS reply */
10997         case FC_RCTL_BA_NOP:    /* basic link service NOP */
10998         case FC_RCTL_BA_ABTS:   /* basic link service abort */
10999         case FC_RCTL_BA_RMC:    /* remove connection */
11000         case FC_RCTL_BA_ACC:    /* basic accept */
11001         case FC_RCTL_BA_RJT:    /* basic reject */
11002         case FC_RCTL_BA_PRMT:
11003         case FC_RCTL_ACK_1:     /* acknowledge_1 */
11004         case FC_RCTL_ACK_0:     /* acknowledge_0 */
11005         case FC_RCTL_P_RJT:     /* port reject */
11006         case FC_RCTL_F_RJT:     /* fabric reject */
11007         case FC_RCTL_P_BSY:     /* port busy */
11008         case FC_RCTL_F_BSY:     /* fabric busy to data frame */
11009         case FC_RCTL_F_BSYL:    /* fabric busy to link control frame */
11010         case FC_RCTL_LCR:       /* link credit reset */
11011         case FC_RCTL_END:       /* end */
11012                 break;
11013         case FC_RCTL_VFTH:      /* Virtual Fabric tagging Header */
11014                 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
11015                 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
11016                 return lpfc_fc_frame_check(phba, fc_hdr);
11017         default:
11018                 goto drop;
11019         }
11020         switch (fc_hdr->fh_type) {
11021         case FC_TYPE_BLS:
11022         case FC_TYPE_ELS:
11023         case FC_TYPE_FCP:
11024         case FC_TYPE_CT:
11025                 break;
11026         case FC_TYPE_IP:
11027         case FC_TYPE_ILS:
11028         default:
11029                 goto drop;
11030         }
11031         lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
11032                         "2538 Received frame rctl:%s type:%s\n",
11033                         rctl_names[fc_hdr->fh_r_ctl],
11034                         type_names[fc_hdr->fh_type]);
11035         return 0;
11036 drop:
11037         lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
11038                         "2539 Dropped frame rctl:%s type:%s\n",
11039                         rctl_names[fc_hdr->fh_r_ctl],
11040                         type_names[fc_hdr->fh_type]);
11041         return 1;
11042 }
11043
11044 /**
11045  * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
11046  * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
11047  *
11048  * This function processes the FC header to retrieve the VFI from the VF
11049  * header, if one exists. This function will return the VFI if one exists
11050  * or 0 if no VSAN Header exists.
11051  **/
11052 static uint32_t
11053 lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
11054 {
11055         struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
11056
11057         if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
11058                 return 0;
11059         return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
11060 }
11061
11062 /**
11063  * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
11064  * @phba: Pointer to the HBA structure to search for the vport on
11065  * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
11066  * @fcfi: The FC Fabric ID that the frame came from
11067  *
11068  * This function searches the @phba for a vport that matches the content of the
11069  * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
11070  * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
11071  * returns the matching vport pointer or NULL if unable to match frame to a
11072  * vport.
11073  **/
11074 static struct lpfc_vport *
11075 lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
11076                        uint16_t fcfi)
11077 {
11078         struct lpfc_vport **vports;
11079         struct lpfc_vport *vport = NULL;
11080         int i;
11081         uint32_t did = (fc_hdr->fh_d_id[0] << 16 |
11082                         fc_hdr->fh_d_id[1] << 8 |
11083                         fc_hdr->fh_d_id[2]);
11084
11085         vports = lpfc_create_vport_work_array(phba);
11086         if (vports != NULL)
11087                 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
11088                         if (phba->fcf.fcfi == fcfi &&
11089                             vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
11090                             vports[i]->fc_myDID == did) {
11091                                 vport = vports[i];
11092                                 break;
11093                         }
11094                 }
11095         lpfc_destroy_vport_work_array(phba, vports);
11096         return vport;
11097 }
11098
11099 /**
11100  * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
11101  * @vport: The vport to work on.
11102  *
11103  * This function updates the receive sequence time stamp for this vport. The
11104  * receive sequence time stamp indicates the time that the last frame of the
11105  * the sequence that has been idle for the longest amount of time was received.
11106  * the driver uses this time stamp to indicate if any received sequences have
11107  * timed out.
11108  **/
11109 void
11110 lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
11111 {
11112         struct lpfc_dmabuf *h_buf;
11113         struct hbq_dmabuf *dmabuf = NULL;
11114
11115         /* get the oldest sequence on the rcv list */
11116         h_buf = list_get_first(&vport->rcv_buffer_list,
11117                                struct lpfc_dmabuf, list);
11118         if (!h_buf)
11119                 return;
11120         dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11121         vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
11122 }
11123
11124 /**
11125  * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
11126  * @vport: The vport that the received sequences were sent to.
11127  *
11128  * This function cleans up all outstanding received sequences. This is called
11129  * by the driver when a link event or user action invalidates all the received
11130  * sequences.
11131  **/
11132 void
11133 lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
11134 {
11135         struct lpfc_dmabuf *h_buf, *hnext;
11136         struct lpfc_dmabuf *d_buf, *dnext;
11137         struct hbq_dmabuf *dmabuf = NULL;
11138
11139         /* start with the oldest sequence on the rcv list */
11140         list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
11141                 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11142                 list_del_init(&dmabuf->hbuf.list);
11143                 list_for_each_entry_safe(d_buf, dnext,
11144                                          &dmabuf->dbuf.list, list) {
11145                         list_del_init(&d_buf->list);
11146                         lpfc_in_buf_free(vport->phba, d_buf);
11147                 }
11148                 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
11149         }
11150 }
11151
11152 /**
11153  * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
11154  * @vport: The vport that the received sequences were sent to.
11155  *
11156  * This function determines whether any received sequences have timed out by
11157  * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
11158  * indicates that there is at least one timed out sequence this routine will
11159  * go through the received sequences one at a time from most inactive to most
11160  * active to determine which ones need to be cleaned up. Once it has determined
11161  * that a sequence needs to be cleaned up it will simply free up the resources
11162  * without sending an abort.
11163  **/
11164 void
11165 lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
11166 {
11167         struct lpfc_dmabuf *h_buf, *hnext;
11168         struct lpfc_dmabuf *d_buf, *dnext;
11169         struct hbq_dmabuf *dmabuf = NULL;
11170         unsigned long timeout;
11171         int abort_count = 0;
11172
11173         timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
11174                    vport->rcv_buffer_time_stamp);
11175         if (list_empty(&vport->rcv_buffer_list) ||
11176             time_before(jiffies, timeout))
11177                 return;
11178         /* start with the oldest sequence on the rcv list */
11179         list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
11180                 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11181                 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
11182                            dmabuf->time_stamp);
11183                 if (time_before(jiffies, timeout))
11184                         break;
11185                 abort_count++;
11186                 list_del_init(&dmabuf->hbuf.list);
11187                 list_for_each_entry_safe(d_buf, dnext,
11188                                          &dmabuf->dbuf.list, list) {
11189                         list_del_init(&d_buf->list);
11190                         lpfc_in_buf_free(vport->phba, d_buf);
11191                 }
11192                 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
11193         }
11194         if (abort_count)
11195                 lpfc_update_rcv_time_stamp(vport);
11196 }
11197
11198 /**
11199  * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
11200  * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
11201  *
11202  * This function searches through the existing incomplete sequences that have
11203  * been sent to this @vport. If the frame matches one of the incomplete
11204  * sequences then the dbuf in the @dmabuf is added to the list of frames that
11205  * make up that sequence. If no sequence is found that matches this frame then
11206  * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
11207  * This function returns a pointer to the first dmabuf in the sequence list that
11208  * the frame was linked to.
11209  **/
11210 static struct hbq_dmabuf *
11211 lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
11212 {
11213         struct fc_frame_header *new_hdr;
11214         struct fc_frame_header *temp_hdr;
11215         struct lpfc_dmabuf *d_buf;
11216         struct lpfc_dmabuf *h_buf;
11217         struct hbq_dmabuf *seq_dmabuf = NULL;
11218         struct hbq_dmabuf *temp_dmabuf = NULL;
11219
11220         INIT_LIST_HEAD(&dmabuf->dbuf.list);
11221         dmabuf->time_stamp = jiffies;
11222         new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11223         /* Use the hdr_buf to find the sequence that this frame belongs to */
11224         list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
11225                 temp_hdr = (struct fc_frame_header *)h_buf->virt;
11226                 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
11227                     (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
11228                     (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
11229                         continue;
11230                 /* found a pending sequence that matches this frame */
11231                 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11232                 break;
11233         }
11234         if (!seq_dmabuf) {
11235                 /*
11236                  * This indicates first frame received for this sequence.
11237                  * Queue the buffer on the vport's rcv_buffer_list.
11238                  */
11239                 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
11240                 lpfc_update_rcv_time_stamp(vport);
11241                 return dmabuf;
11242         }
11243         temp_hdr = seq_dmabuf->hbuf.virt;
11244         if (be16_to_cpu(new_hdr->fh_seq_cnt) <
11245                 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
11246                 list_del_init(&seq_dmabuf->hbuf.list);
11247                 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
11248                 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
11249                 lpfc_update_rcv_time_stamp(vport);
11250                 return dmabuf;
11251         }
11252         /* move this sequence to the tail to indicate a young sequence */
11253         list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
11254         seq_dmabuf->time_stamp = jiffies;
11255         lpfc_update_rcv_time_stamp(vport);
11256         if (list_empty(&seq_dmabuf->dbuf.list)) {
11257                 temp_hdr = dmabuf->hbuf.virt;
11258                 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
11259                 return seq_dmabuf;
11260         }
11261         /* find the correct place in the sequence to insert this frame */
11262         list_for_each_entry_reverse(d_buf, &seq_dmabuf->dbuf.list, list) {
11263                 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
11264                 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
11265                 /*
11266                  * If the frame's sequence count is greater than the frame on
11267                  * the list then insert the frame right after this frame
11268                  */
11269                 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
11270                         be16_to_cpu(temp_hdr->fh_seq_cnt)) {
11271                         list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
11272                         return seq_dmabuf;
11273                 }
11274         }
11275         return NULL;
11276 }
11277
11278 /**
11279  * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
11280  * @vport: pointer to a vitural port
11281  * @dmabuf: pointer to a dmabuf that describes the FC sequence
11282  *
11283  * This function tries to abort from the partially assembed sequence, described
11284  * by the information from basic abbort @dmabuf. It checks to see whether such
11285  * partially assembled sequence held by the driver. If so, it shall free up all
11286  * the frames from the partially assembled sequence.
11287  *
11288  * Return
11289  * true  -- if there is matching partially assembled sequence present and all
11290  *          the frames freed with the sequence;
11291  * false -- if there is no matching partially assembled sequence present so
11292  *          nothing got aborted in the lower layer driver
11293  **/
11294 static bool
11295 lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
11296                             struct hbq_dmabuf *dmabuf)
11297 {
11298         struct fc_frame_header *new_hdr;
11299         struct fc_frame_header *temp_hdr;
11300         struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
11301         struct hbq_dmabuf *seq_dmabuf = NULL;
11302
11303         /* Use the hdr_buf to find the sequence that matches this frame */
11304         INIT_LIST_HEAD(&dmabuf->dbuf.list);
11305         INIT_LIST_HEAD(&dmabuf->hbuf.list);
11306         new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11307         list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
11308                 temp_hdr = (struct fc_frame_header *)h_buf->virt;
11309                 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
11310                     (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
11311                     (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
11312                         continue;
11313                 /* found a pending sequence that matches this frame */
11314                 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11315                 break;
11316         }
11317
11318         /* Free up all the frames from the partially assembled sequence */
11319         if (seq_dmabuf) {
11320                 list_for_each_entry_safe(d_buf, n_buf,
11321                                          &seq_dmabuf->dbuf.list, list) {
11322                         list_del_init(&d_buf->list);
11323                         lpfc_in_buf_free(vport->phba, d_buf);
11324                 }
11325                 return true;
11326         }
11327         return false;
11328 }
11329
11330 /**
11331  * lpfc_sli4_seq_abort_acc_cmpl - Accept seq abort iocb complete handler
11332  * @phba: Pointer to HBA context object.
11333  * @cmd_iocbq: pointer to the command iocbq structure.
11334  * @rsp_iocbq: pointer to the response iocbq structure.
11335  *
11336  * This function handles the sequence abort accept iocb command complete
11337  * event. It properly releases the memory allocated to the sequence abort
11338  * accept iocb.
11339  **/
11340 static void
11341 lpfc_sli4_seq_abort_acc_cmpl(struct lpfc_hba *phba,
11342                              struct lpfc_iocbq *cmd_iocbq,
11343                              struct lpfc_iocbq *rsp_iocbq)
11344 {
11345         if (cmd_iocbq)
11346                 lpfc_sli_release_iocbq(phba, cmd_iocbq);
11347 }
11348
11349 /**
11350  * lpfc_sli4_seq_abort_acc - Accept sequence abort
11351  * @phba: Pointer to HBA context object.
11352  * @fc_hdr: pointer to a FC frame header.
11353  *
11354  * This function sends a basic accept to a previous unsol sequence abort
11355  * event after aborting the sequence handling.
11356  **/
11357 static void
11358 lpfc_sli4_seq_abort_acc(struct lpfc_hba *phba,
11359                         struct fc_frame_header *fc_hdr)
11360 {
11361         struct lpfc_iocbq *ctiocb = NULL;
11362         struct lpfc_nodelist *ndlp;
11363         uint16_t oxid, rxid;
11364         uint32_t sid, fctl;
11365         IOCB_t *icmd;
11366
11367         if (!lpfc_is_link_up(phba))
11368                 return;
11369
11370         sid = sli4_sid_from_fc_hdr(fc_hdr);
11371         oxid = be16_to_cpu(fc_hdr->fh_ox_id);
11372         rxid = be16_to_cpu(fc_hdr->fh_rx_id);
11373
11374         ndlp = lpfc_findnode_did(phba->pport, sid);
11375         if (!ndlp) {
11376                 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
11377                                 "1268 Find ndlp returned NULL for oxid:x%x "
11378                                 "SID:x%x\n", oxid, sid);
11379                 return;
11380         }
11381
11382         /* Allocate buffer for acc iocb */
11383         ctiocb = lpfc_sli_get_iocbq(phba);
11384         if (!ctiocb)
11385                 return;
11386
11387         /* Extract the F_CTL field from FC_HDR */
11388         fctl = sli4_fctl_from_fc_hdr(fc_hdr);
11389
11390         icmd = &ctiocb->iocb;
11391         icmd->un.xseq64.bdl.bdeSize = 0;
11392         icmd->un.xseq64.bdl.ulpIoTag32 = 0;
11393         icmd->un.xseq64.w5.hcsw.Dfctl = 0;
11394         icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
11395         icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
11396
11397         /* Fill in the rest of iocb fields */
11398         icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
11399         icmd->ulpBdeCount = 0;
11400         icmd->ulpLe = 1;
11401         icmd->ulpClass = CLASS3;
11402         icmd->ulpContext = ndlp->nlp_rpi;
11403
11404         ctiocb->iocb_cmpl = NULL;
11405         ctiocb->vport = phba->pport;
11406         ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_acc_cmpl;
11407
11408         if (fctl & FC_FC_EX_CTX) {
11409                 /* ABTS sent by responder to CT exchange, construction
11410                  * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
11411                  * field and RX_ID from ABTS for RX_ID field.
11412                  */
11413                 bf_set(lpfc_abts_orig, &icmd->un.bls_acc, LPFC_ABTS_UNSOL_RSP);
11414                 bf_set(lpfc_abts_rxid, &icmd->un.bls_acc, rxid);
11415                 ctiocb->sli4_xritag = oxid;
11416         } else {
11417                 /* ABTS sent by initiator to CT exchange, construction
11418                  * of BA_ACC will need to allocate a new XRI as for the
11419                  * XRI_TAG and RX_ID fields.
11420                  */
11421                 bf_set(lpfc_abts_orig, &icmd->un.bls_acc, LPFC_ABTS_UNSOL_INT);
11422                 bf_set(lpfc_abts_rxid, &icmd->un.bls_acc, NO_XRI);
11423                 ctiocb->sli4_xritag = NO_XRI;
11424         }
11425         bf_set(lpfc_abts_oxid, &icmd->un.bls_acc, oxid);
11426
11427         /* Xmit CT abts accept on exchange <xid> */
11428         lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
11429                         "1200 Xmit CT ABTS ACC on exchange x%x Data: x%x\n",
11430                         CMD_XMIT_BLS_RSP64_CX, phba->link_state);
11431         lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
11432 }
11433
11434 /**
11435  * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
11436  * @vport: Pointer to the vport on which this sequence was received
11437  * @dmabuf: pointer to a dmabuf that describes the FC sequence
11438  *
11439  * This function handles an SLI-4 unsolicited abort event. If the unsolicited
11440  * receive sequence is only partially assembed by the driver, it shall abort
11441  * the partially assembled frames for the sequence. Otherwise, if the
11442  * unsolicited receive sequence has been completely assembled and passed to
11443  * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
11444  * unsolicited sequence has been aborted. After that, it will issue a basic
11445  * accept to accept the abort.
11446  **/
11447 void
11448 lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
11449                              struct hbq_dmabuf *dmabuf)
11450 {
11451         struct lpfc_hba *phba = vport->phba;
11452         struct fc_frame_header fc_hdr;
11453         uint32_t fctl;
11454         bool abts_par;
11455
11456         /* Make a copy of fc_hdr before the dmabuf being released */
11457         memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
11458         fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
11459
11460         if (fctl & FC_FC_EX_CTX) {
11461                 /*
11462                  * ABTS sent by responder to exchange, just free the buffer
11463                  */
11464                 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11465         } else {
11466                 /*
11467                  * ABTS sent by initiator to exchange, need to do cleanup
11468                  */
11469                 /* Try to abort partially assembled seq */
11470                 abts_par = lpfc_sli4_abort_partial_seq(vport, dmabuf);
11471
11472                 /* Send abort to ULP if partially seq abort failed */
11473                 if (abts_par == false)
11474                         lpfc_sli4_send_seq_to_ulp(vport, dmabuf);
11475                 else
11476                         lpfc_in_buf_free(phba, &dmabuf->dbuf);
11477         }
11478         /* Send basic accept (BA_ACC) to the abort requester */
11479         lpfc_sli4_seq_abort_acc(phba, &fc_hdr);
11480 }
11481
11482 /**
11483  * lpfc_seq_complete - Indicates if a sequence is complete
11484  * @dmabuf: pointer to a dmabuf that describes the FC sequence
11485  *
11486  * This function checks the sequence, starting with the frame described by
11487  * @dmabuf, to see if all the frames associated with this sequence are present.
11488  * the frames associated with this sequence are linked to the @dmabuf using the
11489  * dbuf list. This function looks for two major things. 1) That the first frame
11490  * has a sequence count of zero. 2) There is a frame with last frame of sequence
11491  * set. 3) That there are no holes in the sequence count. The function will
11492  * return 1 when the sequence is complete, otherwise it will return 0.
11493  **/
11494 static int
11495 lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
11496 {
11497         struct fc_frame_header *hdr;
11498         struct lpfc_dmabuf *d_buf;
11499         struct hbq_dmabuf *seq_dmabuf;
11500         uint32_t fctl;
11501         int seq_count = 0;
11502
11503         hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11504         /* make sure first fame of sequence has a sequence count of zero */
11505         if (hdr->fh_seq_cnt != seq_count)
11506                 return 0;
11507         fctl = (hdr->fh_f_ctl[0] << 16 |
11508                 hdr->fh_f_ctl[1] << 8 |
11509                 hdr->fh_f_ctl[2]);
11510         /* If last frame of sequence we can return success. */
11511         if (fctl & FC_FC_END_SEQ)
11512                 return 1;
11513         list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
11514                 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
11515                 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
11516                 /* If there is a hole in the sequence count then fail. */
11517                 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
11518                         return 0;
11519                 fctl = (hdr->fh_f_ctl[0] << 16 |
11520                         hdr->fh_f_ctl[1] << 8 |
11521                         hdr->fh_f_ctl[2]);
11522                 /* If last frame of sequence we can return success. */
11523                 if (fctl & FC_FC_END_SEQ)
11524                         return 1;
11525         }
11526         return 0;
11527 }
11528
11529 /**
11530  * lpfc_prep_seq - Prep sequence for ULP processing
11531  * @vport: Pointer to the vport on which this sequence was received
11532  * @dmabuf: pointer to a dmabuf that describes the FC sequence
11533  *
11534  * This function takes a sequence, described by a list of frames, and creates
11535  * a list of iocbq structures to describe the sequence. This iocbq list will be
11536  * used to issue to the generic unsolicited sequence handler. This routine
11537  * returns a pointer to the first iocbq in the list. If the function is unable
11538  * to allocate an iocbq then it throw out the received frames that were not
11539  * able to be described and return a pointer to the first iocbq. If unable to
11540  * allocate any iocbqs (including the first) this function will return NULL.
11541  **/
11542 static struct lpfc_iocbq *
11543 lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
11544 {
11545         struct lpfc_dmabuf *d_buf, *n_buf;
11546         struct lpfc_iocbq *first_iocbq, *iocbq;
11547         struct fc_frame_header *fc_hdr;
11548         uint32_t sid;
11549         struct ulp_bde64 *pbde;
11550
11551         fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
11552         /* remove from receive buffer list */
11553         list_del_init(&seq_dmabuf->hbuf.list);
11554         lpfc_update_rcv_time_stamp(vport);
11555         /* get the Remote Port's SID */
11556         sid = sli4_sid_from_fc_hdr(fc_hdr);
11557         /* Get an iocbq struct to fill in. */
11558         first_iocbq = lpfc_sli_get_iocbq(vport->phba);
11559         if (first_iocbq) {
11560                 /* Initialize the first IOCB. */
11561                 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
11562                 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
11563                 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
11564                 first_iocbq->iocb.ulpContext = be16_to_cpu(fc_hdr->fh_ox_id);
11565                 first_iocbq->iocb.unsli3.rcvsli3.vpi =
11566                                         vport->vpi + vport->phba->vpi_base;
11567                 /* put the first buffer into the first IOCBq */
11568                 first_iocbq->context2 = &seq_dmabuf->dbuf;
11569                 first_iocbq->context3 = NULL;
11570                 first_iocbq->iocb.ulpBdeCount = 1;
11571                 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
11572                                                         LPFC_DATA_BUF_SIZE;
11573                 first_iocbq->iocb.un.rcvels.remoteID = sid;
11574                 first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
11575                                 bf_get(lpfc_rcqe_length,
11576                                        &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
11577         }
11578         iocbq = first_iocbq;
11579         /*
11580          * Each IOCBq can have two Buffers assigned, so go through the list
11581          * of buffers for this sequence and save two buffers in each IOCBq
11582          */
11583         list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
11584                 if (!iocbq) {
11585                         lpfc_in_buf_free(vport->phba, d_buf);
11586                         continue;
11587                 }
11588                 if (!iocbq->context3) {
11589                         iocbq->context3 = d_buf;
11590                         iocbq->iocb.ulpBdeCount++;
11591                         pbde = (struct ulp_bde64 *)
11592                                         &iocbq->iocb.unsli3.sli3Words[4];
11593                         pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
11594                         first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
11595                                 bf_get(lpfc_rcqe_length,
11596                                        &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
11597                 } else {
11598                         iocbq = lpfc_sli_get_iocbq(vport->phba);
11599                         if (!iocbq) {
11600                                 if (first_iocbq) {
11601                                         first_iocbq->iocb.ulpStatus =
11602                                                         IOSTAT_FCP_RSP_ERROR;
11603                                         first_iocbq->iocb.un.ulpWord[4] =
11604                                                         IOERR_NO_RESOURCES;
11605                                 }
11606                                 lpfc_in_buf_free(vport->phba, d_buf);
11607                                 continue;
11608                         }
11609                         iocbq->context2 = d_buf;
11610                         iocbq->context3 = NULL;
11611                         iocbq->iocb.ulpBdeCount = 1;
11612                         iocbq->iocb.un.cont64[0].tus.f.bdeSize =
11613                                                         LPFC_DATA_BUF_SIZE;
11614                         first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
11615                                 bf_get(lpfc_rcqe_length,
11616                                        &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
11617                         iocbq->iocb.un.rcvels.remoteID = sid;
11618                         list_add_tail(&iocbq->list, &first_iocbq->list);
11619                 }
11620         }
11621         return first_iocbq;
11622 }
11623
11624 static void
11625 lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
11626                           struct hbq_dmabuf *seq_dmabuf)
11627 {
11628         struct fc_frame_header *fc_hdr;
11629         struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
11630         struct lpfc_hba *phba = vport->phba;
11631
11632         fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
11633         iocbq = lpfc_prep_seq(vport, seq_dmabuf);
11634         if (!iocbq) {
11635                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11636                                 "2707 Ring %d handler: Failed to allocate "
11637                                 "iocb Rctl x%x Type x%x received\n",
11638                                 LPFC_ELS_RING,
11639                                 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
11640                 return;
11641         }
11642         if (!lpfc_complete_unsol_iocb(phba,
11643                                       &phba->sli.ring[LPFC_ELS_RING],
11644                                       iocbq, fc_hdr->fh_r_ctl,
11645                                       fc_hdr->fh_type))
11646                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11647                                 "2540 Ring %d handler: unexpected Rctl "
11648                                 "x%x Type x%x received\n",
11649                                 LPFC_ELS_RING,
11650                                 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
11651
11652         /* Free iocb created in lpfc_prep_seq */
11653         list_for_each_entry_safe(curr_iocb, next_iocb,
11654                 &iocbq->list, list) {
11655                 list_del_init(&curr_iocb->list);
11656                 lpfc_sli_release_iocbq(phba, curr_iocb);
11657         }
11658         lpfc_sli_release_iocbq(phba, iocbq);
11659 }
11660
11661 /**
11662  * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
11663  * @phba: Pointer to HBA context object.
11664  *
11665  * This function is called with no lock held. This function processes all
11666  * the received buffers and gives it to upper layers when a received buffer
11667  * indicates that it is the final frame in the sequence. The interrupt
11668  * service routine processes received buffers at interrupt contexts and adds
11669  * received dma buffers to the rb_pend_list queue and signals the worker thread.
11670  * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
11671  * appropriate receive function when the final frame in a sequence is received.
11672  **/
11673 void
11674 lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
11675                                  struct hbq_dmabuf *dmabuf)
11676 {
11677         struct hbq_dmabuf *seq_dmabuf;
11678         struct fc_frame_header *fc_hdr;
11679         struct lpfc_vport *vport;
11680         uint32_t fcfi;
11681
11682         /* Process each received buffer */
11683         fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11684         /* check to see if this a valid type of frame */
11685         if (lpfc_fc_frame_check(phba, fc_hdr)) {
11686                 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11687                 return;
11688         }
11689         fcfi = bf_get(lpfc_rcqe_fcf_id, &dmabuf->cq_event.cqe.rcqe_cmpl);
11690         vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi);
11691         if (!vport || !(vport->vpi_state & LPFC_VPI_REGISTERED)) {
11692                 /* throw out the frame */
11693                 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11694                 return;
11695         }
11696         /* Handle the basic abort sequence (BA_ABTS) event */
11697         if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
11698                 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
11699                 return;
11700         }
11701
11702         /* Link this frame */
11703         seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
11704         if (!seq_dmabuf) {
11705                 /* unable to add frame to vport - throw it out */
11706                 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11707                 return;
11708         }
11709         /* If not last frame in sequence continue processing frames. */
11710         if (!lpfc_seq_complete(seq_dmabuf))
11711                 return;
11712
11713         /* Send the complete sequence to the upper layer protocol */
11714         lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
11715 }
11716
11717 /**
11718  * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
11719  * @phba: pointer to lpfc hba data structure.
11720  *
11721  * This routine is invoked to post rpi header templates to the
11722  * HBA consistent with the SLI-4 interface spec.  This routine
11723  * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
11724  * SLI4_PAGE_SIZE modulo 64 rpi context headers.
11725  *
11726  * This routine does not require any locks.  It's usage is expected
11727  * to be driver load or reset recovery when the driver is
11728  * sequential.
11729  *
11730  * Return codes
11731  *      0 - successful
11732  *      EIO - The mailbox failed to complete successfully.
11733  *      When this error occurs, the driver is not guaranteed
11734  *      to have any rpi regions posted to the device and
11735  *      must either attempt to repost the regions or take a
11736  *      fatal error.
11737  **/
11738 int
11739 lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
11740 {
11741         struct lpfc_rpi_hdr *rpi_page;
11742         uint32_t rc = 0;
11743
11744         /* Post all rpi memory regions to the port. */
11745         list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
11746                 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
11747                 if (rc != MBX_SUCCESS) {
11748                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11749                                         "2008 Error %d posting all rpi "
11750                                         "headers\n", rc);
11751                         rc = -EIO;
11752                         break;
11753                 }
11754         }
11755
11756         return rc;
11757 }
11758
11759 /**
11760  * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
11761  * @phba: pointer to lpfc hba data structure.
11762  * @rpi_page:  pointer to the rpi memory region.
11763  *
11764  * This routine is invoked to post a single rpi header to the
11765  * HBA consistent with the SLI-4 interface spec.  This memory region
11766  * maps up to 64 rpi context regions.
11767  *
11768  * Return codes
11769  *      0 - successful
11770  *      ENOMEM - No available memory
11771  *      EIO - The mailbox failed to complete successfully.
11772  **/
11773 int
11774 lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
11775 {
11776         LPFC_MBOXQ_t *mboxq;
11777         struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
11778         uint32_t rc = 0;
11779         uint32_t mbox_tmo;
11780         uint32_t shdr_status, shdr_add_status;
11781         union lpfc_sli4_cfg_shdr *shdr;
11782
11783         /* The port is notified of the header region via a mailbox command. */
11784         mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11785         if (!mboxq) {
11786                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11787                                 "2001 Unable to allocate memory for issuing "
11788                                 "SLI_CONFIG_SPECIAL mailbox command\n");
11789                 return -ENOMEM;
11790         }
11791
11792         /* Post all rpi memory regions to the port. */
11793         hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
11794         mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
11795         lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
11796                          LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
11797                          sizeof(struct lpfc_mbx_post_hdr_tmpl) -
11798                          sizeof(struct mbox_header), LPFC_SLI4_MBX_EMBED);
11799         bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
11800                hdr_tmpl, rpi_page->page_count);
11801         bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
11802                rpi_page->start_rpi);
11803         hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
11804         hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
11805         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
11806         shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
11807         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11808         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11809         if (rc != MBX_TIMEOUT)
11810                 mempool_free(mboxq, phba->mbox_mem_pool);
11811         if (shdr_status || shdr_add_status || rc) {
11812                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11813                                 "2514 POST_RPI_HDR mailbox failed with "
11814                                 "status x%x add_status x%x, mbx status x%x\n",
11815                                 shdr_status, shdr_add_status, rc);
11816                 rc = -ENXIO;
11817         }
11818         return rc;
11819 }
11820
11821 /**
11822  * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
11823  * @phba: pointer to lpfc hba data structure.
11824  *
11825  * This routine is invoked to post rpi header templates to the
11826  * HBA consistent with the SLI-4 interface spec.  This routine
11827  * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
11828  * SLI4_PAGE_SIZE modulo 64 rpi context headers.
11829  *
11830  * Returns
11831  *      A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
11832  *      LPFC_RPI_ALLOC_ERROR if no rpis are available.
11833  **/
11834 int
11835 lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
11836 {
11837         int rpi;
11838         uint16_t max_rpi, rpi_base, rpi_limit;
11839         uint16_t rpi_remaining;
11840         struct lpfc_rpi_hdr *rpi_hdr;
11841
11842         max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
11843         rpi_base = phba->sli4_hba.max_cfg_param.rpi_base;
11844         rpi_limit = phba->sli4_hba.next_rpi;
11845
11846         /*
11847          * The valid rpi range is not guaranteed to be zero-based.  Start
11848          * the search at the rpi_base as reported by the port.
11849          */
11850         spin_lock_irq(&phba->hbalock);
11851         rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, rpi_base);
11852         if (rpi >= rpi_limit || rpi < rpi_base)
11853                 rpi = LPFC_RPI_ALLOC_ERROR;
11854         else {
11855                 set_bit(rpi, phba->sli4_hba.rpi_bmask);
11856                 phba->sli4_hba.max_cfg_param.rpi_used++;
11857                 phba->sli4_hba.rpi_count++;
11858         }
11859
11860         /*
11861          * Don't try to allocate more rpi header regions if the device limit
11862          * on available rpis max has been exhausted.
11863          */
11864         if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
11865             (phba->sli4_hba.rpi_count >= max_rpi)) {
11866                 spin_unlock_irq(&phba->hbalock);
11867                 return rpi;
11868         }
11869
11870         /*
11871          * If the driver is running low on rpi resources, allocate another
11872          * page now.  Note that the next_rpi value is used because
11873          * it represents how many are actually in use whereas max_rpi notes
11874          * how many are supported max by the device.
11875          */
11876         rpi_remaining = phba->sli4_hba.next_rpi - rpi_base -
11877                 phba->sli4_hba.rpi_count;
11878         spin_unlock_irq(&phba->hbalock);
11879         if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
11880                 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
11881                 if (!rpi_hdr) {
11882                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11883                                         "2002 Error Could not grow rpi "
11884                                         "count\n");
11885                 } else {
11886                         lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
11887                 }
11888         }
11889
11890         return rpi;
11891 }
11892
11893 /**
11894  * lpfc_sli4_free_rpi - Release an rpi for reuse.
11895  * @phba: pointer to lpfc hba data structure.
11896  *
11897  * This routine is invoked to release an rpi to the pool of
11898  * available rpis maintained by the driver.
11899  **/
11900 void
11901 lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
11902 {
11903         spin_lock_irq(&phba->hbalock);
11904         clear_bit(rpi, phba->sli4_hba.rpi_bmask);
11905         phba->sli4_hba.rpi_count--;
11906         phba->sli4_hba.max_cfg_param.rpi_used--;
11907         spin_unlock_irq(&phba->hbalock);
11908 }
11909
11910 /**
11911  * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
11912  * @phba: pointer to lpfc hba data structure.
11913  *
11914  * This routine is invoked to remove the memory region that
11915  * provided rpi via a bitmask.
11916  **/
11917 void
11918 lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
11919 {
11920         kfree(phba->sli4_hba.rpi_bmask);
11921 }
11922
11923 /**
11924  * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
11925  * @phba: pointer to lpfc hba data structure.
11926  *
11927  * This routine is invoked to remove the memory region that
11928  * provided rpi via a bitmask.
11929  **/
11930 int
11931 lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp)
11932 {
11933         LPFC_MBOXQ_t *mboxq;
11934         struct lpfc_hba *phba = ndlp->phba;
11935         int rc;
11936
11937         /* The port is notified of the header region via a mailbox command. */
11938         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11939         if (!mboxq)
11940                 return -ENOMEM;
11941
11942         /* Post all rpi memory regions to the port. */
11943         lpfc_resume_rpi(mboxq, ndlp);
11944         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
11945         if (rc == MBX_NOT_FINISHED) {
11946                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11947                                 "2010 Resume RPI Mailbox failed "
11948                                 "status %d, mbxStatus x%x\n", rc,
11949                                 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
11950                 mempool_free(mboxq, phba->mbox_mem_pool);
11951                 return -EIO;
11952         }
11953         return 0;
11954 }
11955
11956 /**
11957  * lpfc_sli4_init_vpi - Initialize a vpi with the port
11958  * @phba: pointer to lpfc hba data structure.
11959  * @vpi: vpi value to activate with the port.
11960  *
11961  * This routine is invoked to activate a vpi with the
11962  * port when the host intends to use vports with a
11963  * nonzero vpi.
11964  *
11965  * Returns:
11966  *    0 success
11967  *    -Evalue otherwise
11968  **/
11969 int
11970 lpfc_sli4_init_vpi(struct lpfc_hba *phba, uint16_t vpi)
11971 {
11972         LPFC_MBOXQ_t *mboxq;
11973         int rc = 0;
11974         int retval = MBX_SUCCESS;
11975         uint32_t mbox_tmo;
11976
11977         if (vpi == 0)
11978                 return -EINVAL;
11979         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11980         if (!mboxq)
11981                 return -ENOMEM;
11982         lpfc_init_vpi(phba, mboxq, vpi);
11983         mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_INIT_VPI);
11984         rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
11985         if (rc != MBX_SUCCESS) {
11986                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11987                                 "2022 INIT VPI Mailbox failed "
11988                                 "status %d, mbxStatus x%x\n", rc,
11989                                 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
11990                 retval = -EIO;
11991         }
11992         if (rc != MBX_TIMEOUT)
11993                 mempool_free(mboxq, phba->mbox_mem_pool);
11994
11995         return retval;
11996 }
11997
11998 /**
11999  * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
12000  * @phba: pointer to lpfc hba data structure.
12001  * @mboxq: Pointer to mailbox object.
12002  *
12003  * This routine is invoked to manually add a single FCF record. The caller
12004  * must pass a completely initialized FCF_Record.  This routine takes
12005  * care of the nonembedded mailbox operations.
12006  **/
12007 static void
12008 lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
12009 {
12010         void *virt_addr;
12011         union lpfc_sli4_cfg_shdr *shdr;
12012         uint32_t shdr_status, shdr_add_status;
12013
12014         virt_addr = mboxq->sge_array->addr[0];
12015         /* The IOCTL status is embedded in the mailbox subheader. */
12016         shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
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
12020         if ((shdr_status || shdr_add_status) &&
12021                 (shdr_status != STATUS_FCF_IN_USE))
12022                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12023                         "2558 ADD_FCF_RECORD mailbox failed with "
12024                         "status x%x add_status x%x\n",
12025                         shdr_status, shdr_add_status);
12026
12027         lpfc_sli4_mbox_cmd_free(phba, mboxq);
12028 }
12029
12030 /**
12031  * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
12032  * @phba: pointer to lpfc hba data structure.
12033  * @fcf_record:  pointer to the initialized fcf record to add.
12034  *
12035  * This routine is invoked to manually add a single FCF record. The caller
12036  * must pass a completely initialized FCF_Record.  This routine takes
12037  * care of the nonembedded mailbox operations.
12038  **/
12039 int
12040 lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
12041 {
12042         int rc = 0;
12043         LPFC_MBOXQ_t *mboxq;
12044         uint8_t *bytep;
12045         void *virt_addr;
12046         dma_addr_t phys_addr;
12047         struct lpfc_mbx_sge sge;
12048         uint32_t alloc_len, req_len;
12049         uint32_t fcfindex;
12050
12051         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12052         if (!mboxq) {
12053                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12054                         "2009 Failed to allocate mbox for ADD_FCF cmd\n");
12055                 return -ENOMEM;
12056         }
12057
12058         req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
12059                   sizeof(uint32_t);
12060
12061         /* Allocate DMA memory and set up the non-embedded mailbox command */
12062         alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
12063                                      LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
12064                                      req_len, LPFC_SLI4_MBX_NEMBED);
12065         if (alloc_len < req_len) {
12066                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12067                         "2523 Allocated DMA memory size (x%x) is "
12068                         "less than the requested DMA memory "
12069                         "size (x%x)\n", alloc_len, req_len);
12070                 lpfc_sli4_mbox_cmd_free(phba, mboxq);
12071                 return -ENOMEM;
12072         }
12073
12074         /*
12075          * Get the first SGE entry from the non-embedded DMA memory.  This
12076          * routine only uses a single SGE.
12077          */
12078         lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
12079         phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
12080         virt_addr = mboxq->sge_array->addr[0];
12081         /*
12082          * Configure the FCF record for FCFI 0.  This is the driver's
12083          * hardcoded default and gets used in nonFIP mode.
12084          */
12085         fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
12086         bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
12087         lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
12088
12089         /*
12090          * Copy the fcf_index and the FCF Record Data. The data starts after
12091          * the FCoE header plus word10. The data copy needs to be endian
12092          * correct.
12093          */
12094         bytep += sizeof(uint32_t);
12095         lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
12096         mboxq->vport = phba->pport;
12097         mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
12098         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
12099         if (rc == MBX_NOT_FINISHED) {
12100                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12101                         "2515 ADD_FCF_RECORD mailbox failed with "
12102                         "status 0x%x\n", rc);
12103                 lpfc_sli4_mbox_cmd_free(phba, mboxq);
12104                 rc = -EIO;
12105         } else
12106                 rc = 0;
12107
12108         return rc;
12109 }
12110
12111 /**
12112  * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
12113  * @phba: pointer to lpfc hba data structure.
12114  * @fcf_record:  pointer to the fcf record to write the default data.
12115  * @fcf_index: FCF table entry index.
12116  *
12117  * This routine is invoked to build the driver's default FCF record.  The
12118  * values used are hardcoded.  This routine handles memory initialization.
12119  *
12120  **/
12121 void
12122 lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
12123                                 struct fcf_record *fcf_record,
12124                                 uint16_t fcf_index)
12125 {
12126         memset(fcf_record, 0, sizeof(struct fcf_record));
12127         fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
12128         fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
12129         fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
12130         bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
12131         bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
12132         bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
12133         bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
12134         bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
12135         bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
12136         bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
12137         bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
12138         bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
12139         bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
12140         bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
12141         bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
12142         bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
12143                 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
12144         /* Set the VLAN bit map */
12145         if (phba->valid_vlan) {
12146                 fcf_record->vlan_bitmap[phba->vlan_id / 8]
12147                         = 1 << (phba->vlan_id % 8);
12148         }
12149 }
12150
12151 /**
12152  * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
12153  * @phba: pointer to lpfc hba data structure.
12154  * @fcf_index: FCF table entry offset.
12155  *
12156  * This routine is invoked to scan the entire FCF table by reading FCF
12157  * record and processing it one at a time starting from the @fcf_index
12158  * for initial FCF discovery or fast FCF failover rediscovery.
12159  *
12160  * Return 0 if the mailbox command is submitted sucessfully, none 0
12161  * otherwise.
12162  **/
12163 int
12164 lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
12165 {
12166         int rc = 0, error;
12167         LPFC_MBOXQ_t *mboxq;
12168
12169         phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
12170         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12171         if (!mboxq) {
12172                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12173                                 "2000 Failed to allocate mbox for "
12174                                 "READ_FCF cmd\n");
12175                 error = -ENOMEM;
12176                 goto fail_fcf_scan;
12177         }
12178         /* Construct the read FCF record mailbox command */
12179         rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
12180         if (rc) {
12181                 error = -EINVAL;
12182                 goto fail_fcf_scan;
12183         }
12184         /* Issue the mailbox command asynchronously */
12185         mboxq->vport = phba->pport;
12186         mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
12187         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
12188         if (rc == MBX_NOT_FINISHED)
12189                 error = -EIO;
12190         else {
12191                 spin_lock_irq(&phba->hbalock);
12192                 phba->hba_flag |= FCF_DISC_INPROGRESS;
12193                 spin_unlock_irq(&phba->hbalock);
12194                 /* Reset FCF round robin index bmask for new scan */
12195                 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST) {
12196                         memset(phba->fcf.fcf_rr_bmask, 0,
12197                                sizeof(*phba->fcf.fcf_rr_bmask));
12198                         phba->fcf.eligible_fcf_cnt = 0;
12199                 }
12200                 error = 0;
12201         }
12202 fail_fcf_scan:
12203         if (error) {
12204                 if (mboxq)
12205                         lpfc_sli4_mbox_cmd_free(phba, mboxq);
12206                 /* FCF scan failed, clear FCF_DISC_INPROGRESS flag */
12207                 spin_lock_irq(&phba->hbalock);
12208                 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
12209                 spin_unlock_irq(&phba->hbalock);
12210         }
12211         return error;
12212 }
12213
12214 /**
12215  * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for round robin fcf.
12216  * @phba: pointer to lpfc hba data structure.
12217  * @fcf_index: FCF table entry offset.
12218  *
12219  * This routine is invoked to read an FCF record indicated by @fcf_index
12220  * and to use it for FLOGI round robin FCF failover.
12221  *
12222  * Return 0 if the mailbox command is submitted sucessfully, none 0
12223  * otherwise.
12224  **/
12225 int
12226 lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
12227 {
12228         int rc = 0, error;
12229         LPFC_MBOXQ_t *mboxq;
12230
12231         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12232         if (!mboxq) {
12233                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
12234                                 "2763 Failed to allocate mbox for "
12235                                 "READ_FCF cmd\n");
12236                 error = -ENOMEM;
12237                 goto fail_fcf_read;
12238         }
12239         /* Construct the read FCF record mailbox command */
12240         rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
12241         if (rc) {
12242                 error = -EINVAL;
12243                 goto fail_fcf_read;
12244         }
12245         /* Issue the mailbox command asynchronously */
12246         mboxq->vport = phba->pport;
12247         mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
12248         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
12249         if (rc == MBX_NOT_FINISHED)
12250                 error = -EIO;
12251         else
12252                 error = 0;
12253
12254 fail_fcf_read:
12255         if (error && mboxq)
12256                 lpfc_sli4_mbox_cmd_free(phba, mboxq);
12257         return error;
12258 }
12259
12260 /**
12261  * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
12262  * @phba: pointer to lpfc hba data structure.
12263  * @fcf_index: FCF table entry offset.
12264  *
12265  * This routine is invoked to read an FCF record indicated by @fcf_index to
12266  * determine whether it's eligible for FLOGI round robin failover list.
12267  *
12268  * Return 0 if the mailbox command is submitted sucessfully, none 0
12269  * otherwise.
12270  **/
12271 int
12272 lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
12273 {
12274         int rc = 0, error;
12275         LPFC_MBOXQ_t *mboxq;
12276
12277         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12278         if (!mboxq) {
12279                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
12280                                 "2758 Failed to allocate mbox for "
12281                                 "READ_FCF cmd\n");
12282                                 error = -ENOMEM;
12283                                 goto fail_fcf_read;
12284         }
12285         /* Construct the read FCF record mailbox command */
12286         rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
12287         if (rc) {
12288                 error = -EINVAL;
12289                 goto fail_fcf_read;
12290         }
12291         /* Issue the mailbox command asynchronously */
12292         mboxq->vport = phba->pport;
12293         mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
12294         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
12295         if (rc == MBX_NOT_FINISHED)
12296                 error = -EIO;
12297         else
12298                 error = 0;
12299
12300 fail_fcf_read:
12301         if (error && mboxq)
12302                 lpfc_sli4_mbox_cmd_free(phba, mboxq);
12303         return error;
12304 }
12305
12306 /**
12307  * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
12308  * @phba: pointer to lpfc hba data structure.
12309  *
12310  * This routine is to get the next eligible FCF record index in a round
12311  * robin fashion. If the next eligible FCF record index equals to the
12312  * initial round robin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
12313  * shall be returned, otherwise, the next eligible FCF record's index
12314  * shall be returned.
12315  **/
12316 uint16_t
12317 lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
12318 {
12319         uint16_t next_fcf_index;
12320
12321         /* Search from the currently registered FCF index */
12322         next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
12323                                        LPFC_SLI4_FCF_TBL_INDX_MAX,
12324                                        phba->fcf.current_rec.fcf_indx);
12325         /* Wrap around condition on phba->fcf.fcf_rr_bmask */
12326         if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
12327                 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
12328                                                LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
12329         /* Round robin failover stop condition */
12330         if (next_fcf_index == phba->fcf.fcf_rr_init_indx)
12331                 return LPFC_FCOE_FCF_NEXT_NONE;
12332
12333         return next_fcf_index;
12334 }
12335
12336 /**
12337  * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
12338  * @phba: pointer to lpfc hba data structure.
12339  *
12340  * This routine sets the FCF record index in to the eligible bmask for
12341  * round robin failover search. It checks to make sure that the index
12342  * does not go beyond the range of the driver allocated bmask dimension
12343  * before setting the bit.
12344  *
12345  * Returns 0 if the index bit successfully set, otherwise, it returns
12346  * -EINVAL.
12347  **/
12348 int
12349 lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
12350 {
12351         if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
12352                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
12353                                 "2610 HBA FCF index reached driver's "
12354                                 "book keeping dimension: fcf_index:%d, "
12355                                 "driver_bmask_max:%d\n",
12356                                 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
12357                 return -EINVAL;
12358         }
12359         /* Set the eligible FCF record index bmask */
12360         set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
12361
12362         return 0;
12363 }
12364
12365 /**
12366  * lpfc_sli4_fcf_rr_index_set - Clear bmask from eligible fcf record index
12367  * @phba: pointer to lpfc hba data structure.
12368  *
12369  * This routine clears the FCF record index from the eligible bmask for
12370  * round robin failover search. It checks to make sure that the index
12371  * does not go beyond the range of the driver allocated bmask dimension
12372  * before clearing the bit.
12373  **/
12374 void
12375 lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
12376 {
12377         if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
12378                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
12379                                 "2762 HBA FCF index goes beyond driver's "
12380                                 "book keeping dimension: fcf_index:%d, "
12381                                 "driver_bmask_max:%d\n",
12382                                 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
12383                 return;
12384         }
12385         /* Clear the eligible FCF record index bmask */
12386         clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
12387 }
12388
12389 /**
12390  * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
12391  * @phba: pointer to lpfc hba data structure.
12392  *
12393  * This routine is the completion routine for the rediscover FCF table mailbox
12394  * command. If the mailbox command returned failure, it will try to stop the
12395  * FCF rediscover wait timer.
12396  **/
12397 void
12398 lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
12399 {
12400         struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
12401         uint32_t shdr_status, shdr_add_status;
12402
12403         redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
12404
12405         shdr_status = bf_get(lpfc_mbox_hdr_status,
12406                              &redisc_fcf->header.cfg_shdr.response);
12407         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
12408                              &redisc_fcf->header.cfg_shdr.response);
12409         if (shdr_status || shdr_add_status) {
12410                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
12411                                 "2746 Requesting for FCF rediscovery failed "
12412                                 "status x%x add_status x%x\n",
12413                                 shdr_status, shdr_add_status);
12414                 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
12415                         spin_lock_irq(&phba->hbalock);
12416                         phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
12417                         spin_unlock_irq(&phba->hbalock);
12418                         /*
12419                          * CVL event triggered FCF rediscover request failed,
12420                          * last resort to re-try current registered FCF entry.
12421                          */
12422                         lpfc_retry_pport_discovery(phba);
12423                 } else {
12424                         spin_lock_irq(&phba->hbalock);
12425                         phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
12426                         spin_unlock_irq(&phba->hbalock);
12427                         /*
12428                          * DEAD FCF event triggered FCF rediscover request
12429                          * failed, last resort to fail over as a link down
12430                          * to FCF registration.
12431                          */
12432                         lpfc_sli4_fcf_dead_failthrough(phba);
12433                 }
12434         } else {
12435                 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
12436                                 "2775 Start FCF rediscovery quiescent period "
12437                                 "wait timer before scaning FCF table\n");
12438                 /*
12439                  * Start FCF rediscovery wait timer for pending FCF
12440                  * before rescan FCF record table.
12441                  */
12442                 lpfc_fcf_redisc_wait_start_timer(phba);
12443         }
12444
12445         mempool_free(mbox, phba->mbox_mem_pool);
12446 }
12447
12448 /**
12449  * lpfc_sli4_redisc_all_fcf - Request to rediscover entire FCF table by port.
12450  * @phba: pointer to lpfc hba data structure.
12451  *
12452  * This routine is invoked to request for rediscovery of the entire FCF table
12453  * by the port.
12454  **/
12455 int
12456 lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
12457 {
12458         LPFC_MBOXQ_t *mbox;
12459         struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
12460         int rc, length;
12461
12462         /* Cancel retry delay timers to all vports before FCF rediscover */
12463         lpfc_cancel_all_vport_retry_delay_timer(phba);
12464
12465         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12466         if (!mbox) {
12467                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12468                                 "2745 Failed to allocate mbox for "
12469                                 "requesting FCF rediscover.\n");
12470                 return -ENOMEM;
12471         }
12472
12473         length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
12474                   sizeof(struct lpfc_sli4_cfg_mhdr));
12475         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12476                          LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
12477                          length, LPFC_SLI4_MBX_EMBED);
12478
12479         redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
12480         /* Set count to 0 for invalidating the entire FCF database */
12481         bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
12482
12483         /* Issue the mailbox command asynchronously */
12484         mbox->vport = phba->pport;
12485         mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
12486         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
12487
12488         if (rc == MBX_NOT_FINISHED) {
12489                 mempool_free(mbox, phba->mbox_mem_pool);
12490                 return -EIO;
12491         }
12492         return 0;
12493 }
12494
12495 /**
12496  * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
12497  * @phba: pointer to lpfc hba data structure.
12498  *
12499  * This function is the failover routine as a last resort to the FCF DEAD
12500  * event when driver failed to perform fast FCF failover.
12501  **/
12502 void
12503 lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
12504 {
12505         uint32_t link_state;
12506
12507         /*
12508          * Last resort as FCF DEAD event failover will treat this as
12509          * a link down, but save the link state because we don't want
12510          * it to be changed to Link Down unless it is already down.
12511          */
12512         link_state = phba->link_state;
12513         lpfc_linkdown(phba);
12514         phba->link_state = link_state;
12515
12516         /* Unregister FCF if no devices connected to it */
12517         lpfc_unregister_unused_fcf(phba);
12518 }
12519
12520 /**
12521  * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
12522  * @phba: pointer to lpfc hba data structure.
12523  *
12524  * This function read region 23 and parse TLV for port status to
12525  * decide if the user disaled the port. If the TLV indicates the
12526  * port is disabled, the hba_flag is set accordingly.
12527  **/
12528 void
12529 lpfc_sli_read_link_ste(struct lpfc_hba *phba)
12530 {
12531         LPFC_MBOXQ_t *pmb = NULL;
12532         MAILBOX_t *mb;
12533         uint8_t *rgn23_data = NULL;
12534         uint32_t offset = 0, data_size, sub_tlv_len, tlv_offset;
12535         int rc;
12536
12537         pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12538         if (!pmb) {
12539                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12540                         "2600 lpfc_sli_read_serdes_param failed to"
12541                         " allocate mailbox memory\n");
12542                 goto out;
12543         }
12544         mb = &pmb->u.mb;
12545
12546         /* Get adapter Region 23 data */
12547         rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
12548         if (!rgn23_data)
12549                 goto out;
12550
12551         do {
12552                 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
12553                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
12554
12555                 if (rc != MBX_SUCCESS) {
12556                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
12557                                 "2601 lpfc_sli_read_link_ste failed to"
12558                                 " read config region 23 rc 0x%x Status 0x%x\n",
12559                                 rc, mb->mbxStatus);
12560                         mb->un.varDmp.word_cnt = 0;
12561                 }
12562                 /*
12563                  * dump mem may return a zero when finished or we got a
12564                  * mailbox error, either way we are done.
12565                  */
12566                 if (mb->un.varDmp.word_cnt == 0)
12567                         break;
12568                 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
12569                         mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
12570
12571                 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
12572                         rgn23_data + offset,
12573                         mb->un.varDmp.word_cnt);
12574                 offset += mb->un.varDmp.word_cnt;
12575         } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
12576
12577         data_size = offset;
12578         offset = 0;
12579
12580         if (!data_size)
12581                 goto out;
12582
12583         /* Check the region signature first */
12584         if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
12585                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12586                         "2619 Config region 23 has bad signature\n");
12587                         goto out;
12588         }
12589         offset += 4;
12590
12591         /* Check the data structure version */
12592         if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
12593                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12594                         "2620 Config region 23 has bad version\n");
12595                 goto out;
12596         }
12597         offset += 4;
12598
12599         /* Parse TLV entries in the region */
12600         while (offset < data_size) {
12601                 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
12602                         break;
12603                 /*
12604                  * If the TLV is not driver specific TLV or driver id is
12605                  * not linux driver id, skip the record.
12606                  */
12607                 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
12608                     (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
12609                     (rgn23_data[offset + 3] != 0)) {
12610                         offset += rgn23_data[offset + 1] * 4 + 4;
12611                         continue;
12612                 }
12613
12614                 /* Driver found a driver specific TLV in the config region */
12615                 sub_tlv_len = rgn23_data[offset + 1] * 4;
12616                 offset += 4;
12617                 tlv_offset = 0;
12618
12619                 /*
12620                  * Search for configured port state sub-TLV.
12621                  */
12622                 while ((offset < data_size) &&
12623                         (tlv_offset < sub_tlv_len)) {
12624                         if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
12625                                 offset += 4;
12626                                 tlv_offset += 4;
12627                                 break;
12628                         }
12629                         if (rgn23_data[offset] != PORT_STE_TYPE) {
12630                                 offset += rgn23_data[offset + 1] * 4 + 4;
12631                                 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
12632                                 continue;
12633                         }
12634
12635                         /* This HBA contains PORT_STE configured */
12636                         if (!rgn23_data[offset + 2])
12637                                 phba->hba_flag |= LINK_DISABLED;
12638
12639                         goto out;
12640                 }
12641         }
12642 out:
12643         if (pmb)
12644                 mempool_free(pmb, phba->mbox_mem_pool);
12645         kfree(rgn23_data);
12646         return;
12647 }
12648
12649 /**
12650  * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
12651  * @vport: pointer to vport data structure.
12652  *
12653  * This function iterate through the mailboxq and clean up all REG_LOGIN
12654  * and REG_VPI mailbox commands associated with the vport. This function
12655  * is called when driver want to restart discovery of the vport due to
12656  * a Clear Virtual Link event.
12657  **/
12658 void
12659 lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
12660 {
12661         struct lpfc_hba *phba = vport->phba;
12662         LPFC_MBOXQ_t *mb, *nextmb;
12663         struct lpfc_dmabuf *mp;
12664         struct lpfc_nodelist *ndlp;
12665
12666         spin_lock_irq(&phba->hbalock);
12667         list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
12668                 if (mb->vport != vport)
12669                         continue;
12670
12671                 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
12672                         (mb->u.mb.mbxCommand != MBX_REG_VPI))
12673                         continue;
12674
12675                 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
12676                         mp = (struct lpfc_dmabuf *) (mb->context1);
12677                         if (mp) {
12678                                 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
12679                                 kfree(mp);
12680                         }
12681                         ndlp = (struct lpfc_nodelist *) mb->context2;
12682                         if (ndlp) {
12683                                 lpfc_nlp_put(ndlp);
12684                                 mb->context2 = NULL;
12685                         }
12686                 }
12687                 list_del(&mb->list);
12688                 mempool_free(mb, phba->mbox_mem_pool);
12689         }
12690         mb = phba->sli.mbox_active;
12691         if (mb && (mb->vport == vport)) {
12692                 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
12693                         (mb->u.mb.mbxCommand == MBX_REG_VPI))
12694                         mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12695                 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
12696                         ndlp = (struct lpfc_nodelist *) mb->context2;
12697                         if (ndlp) {
12698                                 lpfc_nlp_put(ndlp);
12699                                 mb->context2 = NULL;
12700                         }
12701                         /* Unregister the RPI when mailbox complete */
12702                         mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
12703                 }
12704         }
12705         spin_unlock_irq(&phba->hbalock);
12706 }
12707