d6f3b83f32194ec33e339f9a5ff70478674fc5ec
[pandora-kernel.git] / drivers / scsi / lpfc / lpfc_sli.c
1 /*******************************************************************
2  * This file is part of the Emulex Linux Device Driver for         *
3  * Fibre Channel Host Bus Adapters.                                *
4  * Copyright (C) 2004-2009 Emulex.  All rights reserved.           *
5  * EMULEX and SLI are trademarks of Emulex.                        *
6  * www.emulex.com                                                  *
7  * Portions Copyright (C) 2004-2005 Christoph Hellwig              *
8  *                                                                 *
9  * This program is free software; you can redistribute it and/or   *
10  * modify it under the terms of version 2 of the GNU General       *
11  * Public License as published by the Free Software Foundation.    *
12  * This program is distributed in the hope that it will be useful. *
13  * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND          *
14  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,  *
15  * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE      *
16  * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
17  * TO BE LEGALLY INVALID.  See the GNU General Public License for  *
18  * more details, a copy of which can be found in the file COPYING  *
19  * included with this package.                                     *
20  *******************************************************************/
21
22 #include <linux/blkdev.h>
23 #include <linux/pci.h>
24 #include <linux/interrupt.h>
25 #include <linux/delay.h>
26 #include <linux/slab.h>
27
28 #include <scsi/scsi.h>
29 #include <scsi/scsi_cmnd.h>
30 #include <scsi/scsi_device.h>
31 #include <scsi/scsi_host.h>
32 #include <scsi/scsi_transport_fc.h>
33 #include <scsi/fc/fc_fs.h>
34 #include <linux/aer.h>
35
36 #include "lpfc_hw4.h"
37 #include "lpfc_hw.h"
38 #include "lpfc_sli.h"
39 #include "lpfc_sli4.h"
40 #include "lpfc_nl.h"
41 #include "lpfc_disc.h"
42 #include "lpfc_scsi.h"
43 #include "lpfc.h"
44 #include "lpfc_crtn.h"
45 #include "lpfc_logmsg.h"
46 #include "lpfc_compat.h"
47 #include "lpfc_debugfs.h"
48 #include "lpfc_vport.h"
49
50 /* There are only four IOCB completion types. */
51 typedef enum _lpfc_iocb_type {
52         LPFC_UNKNOWN_IOCB,
53         LPFC_UNSOL_IOCB,
54         LPFC_SOL_IOCB,
55         LPFC_ABORT_IOCB
56 } lpfc_iocb_type;
57
58
59 /* Provide function prototypes local to this module. */
60 static int lpfc_sli_issue_mbox_s4(struct lpfc_hba *, LPFC_MBOXQ_t *,
61                                   uint32_t);
62 static int lpfc_sli4_read_rev(struct lpfc_hba *, LPFC_MBOXQ_t *,
63                               uint8_t *, uint32_t *);
64 static struct lpfc_iocbq *lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *,
65                                                          struct lpfc_iocbq *);
66 static void lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *,
67                                       struct hbq_dmabuf *);
68 static IOCB_t *
69 lpfc_get_iocb_from_iocbq(struct lpfc_iocbq *iocbq)
70 {
71         return &iocbq->iocb;
72 }
73
74 /**
75  * lpfc_sli4_wq_put - Put a Work Queue Entry on an Work Queue
76  * @q: The Work Queue to operate on.
77  * @wqe: The work Queue Entry to put on the Work queue.
78  *
79  * This routine will copy the contents of @wqe to the next available entry on
80  * the @q. This function will then ring the Work Queue Doorbell to signal the
81  * HBA to start processing the Work Queue Entry. This function returns 0 if
82  * successful. If no entries are available on @q then this function will return
83  * -ENOMEM.
84  * The caller is expected to hold the hbalock when calling this routine.
85  **/
86 static uint32_t
87 lpfc_sli4_wq_put(struct lpfc_queue *q, union lpfc_wqe *wqe)
88 {
89         union lpfc_wqe *temp_wqe = q->qe[q->host_index].wqe;
90         struct lpfc_register doorbell;
91         uint32_t host_index;
92
93         /* If the host has not yet processed the next entry then we are done */
94         if (((q->host_index + 1) % q->entry_count) == q->hba_index)
95                 return -ENOMEM;
96         /* set consumption flag every once in a while */
97         if (!((q->host_index + 1) % LPFC_RELEASE_NOTIFICATION_INTERVAL))
98                 bf_set(wqe_wqec, &wqe->generic.wqe_com, 1);
99
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
459         if (iocbq)
460                 phba->iocb_cnt++;
461         if (phba->iocb_cnt > phba->iocb_max)
462                 phba->iocb_max = phba->iocb_cnt;
463         return iocbq;
464 }
465
466 /**
467  * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
468  * @phba: Pointer to HBA context object.
469  * @xritag: XRI value.
470  *
471  * This function clears the sglq pointer from the array of acive
472  * sglq's. The xritag that is passed in is used to index into the
473  * array. Before the xritag can be used it needs to be adjusted
474  * by subtracting the xribase.
475  *
476  * Returns sglq ponter = success, NULL = Failure.
477  **/
478 static struct lpfc_sglq *
479 __lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
480 {
481         uint16_t adj_xri;
482         struct lpfc_sglq *sglq;
483         adj_xri = xritag - phba->sli4_hba.max_cfg_param.xri_base;
484         if (adj_xri > phba->sli4_hba.max_cfg_param.max_xri)
485                 return NULL;
486         sglq = phba->sli4_hba.lpfc_sglq_active_list[adj_xri];
487         phba->sli4_hba.lpfc_sglq_active_list[adj_xri] = NULL;
488         return sglq;
489 }
490
491 /**
492  * __lpfc_get_active_sglq - Get the active sglq for this XRI.
493  * @phba: Pointer to HBA context object.
494  * @xritag: XRI value.
495  *
496  * This function returns the sglq pointer from the array of acive
497  * sglq's. The xritag that is passed in is used to index into the
498  * array. Before the xritag can be used it needs to be adjusted
499  * by subtracting the xribase.
500  *
501  * Returns sglq ponter = success, NULL = Failure.
502  **/
503 struct lpfc_sglq *
504 __lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
505 {
506         uint16_t adj_xri;
507         struct lpfc_sglq *sglq;
508         adj_xri = xritag - phba->sli4_hba.max_cfg_param.xri_base;
509         if (adj_xri > phba->sli4_hba.max_cfg_param.max_xri)
510                 return NULL;
511         sglq =  phba->sli4_hba.lpfc_sglq_active_list[adj_xri];
512         return sglq;
513 }
514
515 /**
516  * __lpfc_set_rrq_active - set RRQ active bit in the ndlp's xri_bitmap.
517  * @phba: Pointer to HBA context object.
518  * @ndlp: nodelist pointer for this target.
519  * @xritag: xri used in this exchange.
520  * @rxid: Remote Exchange ID.
521  * @send_rrq: Flag used to determine if we should send rrq els cmd.
522  *
523  * This function is called with hbalock held.
524  * The active bit is set in the ndlp's active rrq xri_bitmap. Allocates an
525  * rrq struct and adds it to the active_rrq_list.
526  *
527  * returns  0 for rrq slot for this xri
528  *         < 0  Were not able to get rrq mem or invalid parameter.
529  **/
530 static int
531 __lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
532                 uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
533 {
534         uint16_t adj_xri;
535         struct lpfc_node_rrq *rrq;
536         int empty;
537
538         /*
539          * set the active bit even if there is no mem available.
540          */
541         adj_xri = xritag - phba->sli4_hba.max_cfg_param.xri_base;
542         if (!ndlp)
543                 return -EINVAL;
544         if (test_and_set_bit(adj_xri, ndlp->active_rrqs.xri_bitmap))
545                 return -EINVAL;
546         rrq = mempool_alloc(phba->rrq_pool, GFP_KERNEL);
547         if (rrq) {
548                 rrq->send_rrq = send_rrq;
549                 rrq->xritag = xritag;
550                 rrq->rrq_stop_time = jiffies + HZ * (phba->fc_ratov + 1);
551                 rrq->ndlp = ndlp;
552                 rrq->nlp_DID = ndlp->nlp_DID;
553                 rrq->vport = ndlp->vport;
554                 rrq->rxid = rxid;
555                 empty = list_empty(&phba->active_rrq_list);
556                 if (phba->cfg_enable_rrq && send_rrq)
557                         /*
558                          * We need the xri before we can add this to the
559                          * phba active rrq list.
560                          */
561                         rrq->send_rrq = send_rrq;
562                 else
563                         rrq->send_rrq = 0;
564                 list_add_tail(&rrq->list, &phba->active_rrq_list);
565                 if (!(phba->hba_flag & HBA_RRQ_ACTIVE)) {
566                         phba->hba_flag |= HBA_RRQ_ACTIVE;
567                         if (empty)
568                                 lpfc_worker_wake_up(phba);
569                 }
570                 return 0;
571         }
572         return -ENOMEM;
573 }
574
575 /**
576  * __lpfc_clr_rrq_active - Clears RRQ active bit in xri_bitmap.
577  * @phba: Pointer to HBA context object.
578  * @xritag: xri used in this exchange.
579  * @rrq: The RRQ to be cleared.
580  *
581  * This function is called with hbalock held. This function
582  **/
583 static void
584 __lpfc_clr_rrq_active(struct lpfc_hba *phba,
585                         uint16_t xritag,
586                         struct lpfc_node_rrq *rrq)
587 {
588         uint16_t adj_xri;
589         struct lpfc_nodelist *ndlp;
590
591         ndlp = lpfc_findnode_did(rrq->vport, rrq->nlp_DID);
592
593         /* The target DID could have been swapped (cable swap)
594          * we should use the ndlp from the findnode if it is
595          * available.
596          */
597         if (!ndlp)
598                 ndlp = rrq->ndlp;
599
600         adj_xri = xritag - phba->sli4_hba.max_cfg_param.xri_base;
601         if (test_and_clear_bit(adj_xri, ndlp->active_rrqs.xri_bitmap)) {
602                 rrq->send_rrq = 0;
603                 rrq->xritag = 0;
604                 rrq->rrq_stop_time = 0;
605         }
606         mempool_free(rrq, phba->rrq_pool);
607 }
608
609 /**
610  * lpfc_handle_rrq_active - Checks if RRQ has waithed RATOV.
611  * @phba: Pointer to HBA context object.
612  *
613  * This function is called with hbalock held. This function
614  * Checks if stop_time (ratov from setting rrq active) has
615  * been reached, if it has and the send_rrq flag is set then
616  * it will call lpfc_send_rrq. If the send_rrq flag is not set
617  * then it will just call the routine to clear the rrq and
618  * free the rrq resource.
619  * The timer is set to the next rrq that is going to expire before
620  * leaving the routine.
621  *
622  **/
623 void
624 lpfc_handle_rrq_active(struct lpfc_hba *phba)
625 {
626         struct lpfc_node_rrq *rrq;
627         struct lpfc_node_rrq *nextrrq;
628         unsigned long next_time;
629         unsigned long iflags;
630
631         spin_lock_irqsave(&phba->hbalock, iflags);
632         phba->hba_flag &= ~HBA_RRQ_ACTIVE;
633         next_time = jiffies + HZ * (phba->fc_ratov + 1);
634         list_for_each_entry_safe(rrq, nextrrq,
635                         &phba->active_rrq_list, list) {
636                 if (time_after(jiffies, rrq->rrq_stop_time)) {
637                         list_del(&rrq->list);
638                         if (!rrq->send_rrq)
639                                 /* this call will free the rrq */
640                                 __lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
641                         else {
642                         /* if we send the rrq then the completion handler
643                          *  will clear the bit in the xribitmap.
644                          */
645                                 spin_unlock_irqrestore(&phba->hbalock, iflags);
646                                 if (lpfc_send_rrq(phba, rrq)) {
647                                         lpfc_clr_rrq_active(phba, rrq->xritag,
648                                                                  rrq);
649                                 }
650                                 spin_lock_irqsave(&phba->hbalock, iflags);
651                         }
652                 } else if  (time_before(rrq->rrq_stop_time, next_time))
653                         next_time = rrq->rrq_stop_time;
654         }
655         spin_unlock_irqrestore(&phba->hbalock, iflags);
656         if (!list_empty(&phba->active_rrq_list))
657                 mod_timer(&phba->rrq_tmr, next_time);
658 }
659
660 /**
661  * lpfc_get_active_rrq - Get the active RRQ for this exchange.
662  * @vport: Pointer to vport context object.
663  * @xri: The xri used in the exchange.
664  * @did: The targets DID for this exchange.
665  *
666  * returns NULL = rrq not found in the phba->active_rrq_list.
667  *         rrq = rrq for this xri and target.
668  **/
669 struct lpfc_node_rrq *
670 lpfc_get_active_rrq(struct lpfc_vport *vport, uint16_t xri, uint32_t did)
671 {
672         struct lpfc_hba *phba = vport->phba;
673         struct lpfc_node_rrq *rrq;
674         struct lpfc_node_rrq *nextrrq;
675         unsigned long iflags;
676
677         if (phba->sli_rev != LPFC_SLI_REV4)
678                 return NULL;
679         spin_lock_irqsave(&phba->hbalock, iflags);
680         list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {
681                 if (rrq->vport == vport && rrq->xritag == xri &&
682                                 rrq->nlp_DID == did){
683                         list_del(&rrq->list);
684                         spin_unlock_irqrestore(&phba->hbalock, iflags);
685                         return rrq;
686                 }
687         }
688         spin_unlock_irqrestore(&phba->hbalock, iflags);
689         return NULL;
690 }
691
692 /**
693  * lpfc_cleanup_vports_rrqs - Remove and clear the active RRQ for this vport.
694  * @vport: Pointer to vport context object.
695  *
696  * Remove all active RRQs for this vport from the phba->active_rrq_list and
697  * clear the rrq.
698  **/
699 void
700 lpfc_cleanup_vports_rrqs(struct lpfc_vport *vport)
701
702 {
703         struct lpfc_hba *phba = vport->phba;
704         struct lpfc_node_rrq *rrq;
705         struct lpfc_node_rrq *nextrrq;
706         unsigned long iflags;
707
708         if (phba->sli_rev != LPFC_SLI_REV4)
709                 return;
710         spin_lock_irqsave(&phba->hbalock, iflags);
711         list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {
712                 if (rrq->vport == vport) {
713                         list_del(&rrq->list);
714                         __lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
715                 }
716         }
717         spin_unlock_irqrestore(&phba->hbalock, iflags);
718 }
719
720 /**
721  * lpfc_cleanup_wt_rrqs - Remove all rrq's from the active list.
722  * @phba: Pointer to HBA context object.
723  *
724  * Remove all rrqs from the phba->active_rrq_list and free them by
725  * calling __lpfc_clr_active_rrq
726  *
727  **/
728 void
729 lpfc_cleanup_wt_rrqs(struct lpfc_hba *phba)
730 {
731         struct lpfc_node_rrq *rrq;
732         struct lpfc_node_rrq *nextrrq;
733         unsigned long next_time;
734         unsigned long iflags;
735
736         if (phba->sli_rev != LPFC_SLI_REV4)
737                 return;
738         spin_lock_irqsave(&phba->hbalock, iflags);
739         phba->hba_flag &= ~HBA_RRQ_ACTIVE;
740         next_time = jiffies + HZ * (phba->fc_ratov * 2);
741         list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {
742                 list_del(&rrq->list);
743                 __lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
744         }
745         spin_unlock_irqrestore(&phba->hbalock, iflags);
746         if (!list_empty(&phba->active_rrq_list))
747                 mod_timer(&phba->rrq_tmr, next_time);
748 }
749
750
751 /**
752  * __lpfc_test_rrq_active - Test RRQ bit in xri_bitmap.
753  * @phba: Pointer to HBA context object.
754  * @ndlp: Targets nodelist pointer for this exchange.
755  * @xritag the xri in the bitmap to test.
756  *
757  * This function is called with hbalock held. This function
758  * returns 0 = rrq not active for this xri
759  *         1 = rrq is valid for this xri.
760  **/
761 static int
762 __lpfc_test_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
763                         uint16_t  xritag)
764 {
765         uint16_t adj_xri;
766
767         adj_xri = xritag - phba->sli4_hba.max_cfg_param.xri_base;
768         if (!ndlp)
769                 return 0;
770         if (test_bit(adj_xri, ndlp->active_rrqs.xri_bitmap))
771                         return 1;
772         else
773                 return 0;
774 }
775
776 /**
777  * lpfc_set_rrq_active - set RRQ active bit in xri_bitmap.
778  * @phba: Pointer to HBA context object.
779  * @ndlp: nodelist pointer for this target.
780  * @xritag: xri used in this exchange.
781  * @rxid: Remote Exchange ID.
782  * @send_rrq: Flag used to determine if we should send rrq els cmd.
783  *
784  * This function takes the hbalock.
785  * The active bit is always set in the active rrq xri_bitmap even
786  * if there is no slot avaiable for the other rrq information.
787  *
788  * returns 0 rrq actived for this xri
789  *         < 0 No memory or invalid ndlp.
790  **/
791 int
792 lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
793                         uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
794 {
795         int ret;
796         unsigned long iflags;
797
798         spin_lock_irqsave(&phba->hbalock, iflags);
799         ret = __lpfc_set_rrq_active(phba, ndlp, xritag, rxid, send_rrq);
800         spin_unlock_irqrestore(&phba->hbalock, iflags);
801         return ret;
802 }
803
804 /**
805  * lpfc_clr_rrq_active - Clears RRQ active bit in xri_bitmap.
806  * @phba: Pointer to HBA context object.
807  * @xritag: xri used in this exchange.
808  * @rrq: The RRQ to be cleared.
809  *
810  * This function is takes the hbalock.
811  **/
812 void
813 lpfc_clr_rrq_active(struct lpfc_hba *phba,
814                         uint16_t xritag,
815                         struct lpfc_node_rrq *rrq)
816 {
817         unsigned long iflags;
818
819         spin_lock_irqsave(&phba->hbalock, iflags);
820         __lpfc_clr_rrq_active(phba, xritag, rrq);
821         spin_unlock_irqrestore(&phba->hbalock, iflags);
822         return;
823 }
824
825
826
827 /**
828  * lpfc_test_rrq_active - Test RRQ bit in xri_bitmap.
829  * @phba: Pointer to HBA context object.
830  * @ndlp: Targets nodelist pointer for this exchange.
831  * @xritag the xri in the bitmap to test.
832  *
833  * This function takes the hbalock.
834  * returns 0 = rrq not active for this xri
835  *         1 = rrq is valid for this xri.
836  **/
837 int
838 lpfc_test_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
839                         uint16_t  xritag)
840 {
841         int ret;
842         unsigned long iflags;
843
844         spin_lock_irqsave(&phba->hbalock, iflags);
845         ret = __lpfc_test_rrq_active(phba, ndlp, xritag);
846         spin_unlock_irqrestore(&phba->hbalock, iflags);
847         return ret;
848 }
849
850 /**
851  * __lpfc_sli_get_sglq - Allocates an iocb object from sgl pool
852  * @phba: Pointer to HBA context object.
853  * @piocb: Pointer to the iocbq.
854  *
855  * This function is called with hbalock held. This function
856  * Gets a new driver sglq object from the sglq list. If the
857  * list is not empty then it is successful, it returns pointer to the newly
858  * allocated sglq object else it returns NULL.
859  **/
860 static struct lpfc_sglq *
861 __lpfc_sli_get_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
862 {
863         struct list_head *lpfc_sgl_list = &phba->sli4_hba.lpfc_sgl_list;
864         struct lpfc_sglq *sglq = NULL;
865         struct lpfc_sglq *start_sglq = NULL;
866         uint16_t adj_xri;
867         struct lpfc_scsi_buf *lpfc_cmd;
868         struct lpfc_nodelist *ndlp;
869         int found = 0;
870
871         if (piocbq->iocb_flag &  LPFC_IO_FCP) {
872                 lpfc_cmd = (struct lpfc_scsi_buf *) piocbq->context1;
873                 ndlp = lpfc_cmd->rdata->pnode;
874         } else  if ((piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) &&
875                         !(piocbq->iocb_flag & LPFC_IO_LIBDFC))
876                 ndlp = piocbq->context_un.ndlp;
877         else
878                 ndlp = piocbq->context1;
879
880         list_remove_head(lpfc_sgl_list, sglq, struct lpfc_sglq, list);
881         start_sglq = sglq;
882         while (!found) {
883                 if (!sglq)
884                         return NULL;
885                 adj_xri = sglq->sli4_xritag -
886                                 phba->sli4_hba.max_cfg_param.xri_base;
887                 if (__lpfc_test_rrq_active(phba, ndlp, sglq->sli4_xritag)) {
888                         /* This xri has an rrq outstanding for this DID.
889                          * put it back in the list and get another xri.
890                          */
891                         list_add_tail(&sglq->list, lpfc_sgl_list);
892                         sglq = NULL;
893                         list_remove_head(lpfc_sgl_list, sglq,
894                                                 struct lpfc_sglq, list);
895                         if (sglq == start_sglq) {
896                                 sglq = NULL;
897                                 break;
898                         } else
899                                 continue;
900                 }
901                 sglq->ndlp = ndlp;
902                 found = 1;
903                 phba->sli4_hba.lpfc_sglq_active_list[adj_xri] = sglq;
904                 sglq->state = SGL_ALLOCATED;
905         }
906         return sglq;
907 }
908
909 /**
910  * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
911  * @phba: Pointer to HBA context object.
912  *
913  * This function is called with no lock held. This function
914  * allocates a new driver iocb object from the iocb pool. If the
915  * allocation is successful, it returns pointer to the newly
916  * allocated iocb object else it returns NULL.
917  **/
918 struct lpfc_iocbq *
919 lpfc_sli_get_iocbq(struct lpfc_hba *phba)
920 {
921         struct lpfc_iocbq * iocbq = NULL;
922         unsigned long iflags;
923
924         spin_lock_irqsave(&phba->hbalock, iflags);
925         iocbq = __lpfc_sli_get_iocbq(phba);
926         spin_unlock_irqrestore(&phba->hbalock, iflags);
927         return iocbq;
928 }
929
930 /**
931  * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
932  * @phba: Pointer to HBA context object.
933  * @iocbq: Pointer to driver iocb object.
934  *
935  * This function is called with hbalock held to release driver
936  * iocb object to the iocb pool. The iotag in the iocb object
937  * does not change for each use of the iocb object. This function
938  * clears all other fields of the iocb object when it is freed.
939  * The sqlq structure that holds the xritag and phys and virtual
940  * mappings for the scatter gather list is retrieved from the
941  * active array of sglq. The get of the sglq pointer also clears
942  * the entry in the array. If the status of the IO indiactes that
943  * this IO was aborted then the sglq entry it put on the
944  * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
945  * IO has good status or fails for any other reason then the sglq
946  * entry is added to the free list (lpfc_sgl_list).
947  **/
948 static void
949 __lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
950 {
951         struct lpfc_sglq *sglq;
952         size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
953         unsigned long iflag = 0;
954         struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
955
956         if (iocbq->sli4_xritag == NO_XRI)
957                 sglq = NULL;
958         else
959                 sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_xritag);
960         if (sglq)  {
961                 if ((iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) &&
962                         (sglq->state != SGL_XRI_ABORTED)) {
963                         spin_lock_irqsave(&phba->sli4_hba.abts_sgl_list_lock,
964                                         iflag);
965                         list_add(&sglq->list,
966                                 &phba->sli4_hba.lpfc_abts_els_sgl_list);
967                         spin_unlock_irqrestore(
968                                 &phba->sli4_hba.abts_sgl_list_lock, iflag);
969                 } else {
970                         sglq->state = SGL_FREED;
971                         sglq->ndlp = NULL;
972                         list_add(&sglq->list, &phba->sli4_hba.lpfc_sgl_list);
973
974                         /* Check if TXQ queue needs to be serviced */
975                         if (pring->txq_cnt)
976                                 lpfc_worker_wake_up(phba);
977                 }
978         }
979
980
981         /*
982          * Clean all volatile data fields, preserve iotag and node struct.
983          */
984         memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
985         iocbq->sli4_xritag = NO_XRI;
986         list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
987 }
988
989
990 /**
991  * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
992  * @phba: Pointer to HBA context object.
993  * @iocbq: Pointer to driver iocb object.
994  *
995  * This function is called with hbalock held to release driver
996  * iocb object to the iocb pool. The iotag in the iocb object
997  * does not change for each use of the iocb object. This function
998  * clears all other fields of the iocb object when it is freed.
999  **/
1000 static void
1001 __lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1002 {
1003         size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
1004
1005         /*
1006          * Clean all volatile data fields, preserve iotag and node struct.
1007          */
1008         memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
1009         iocbq->sli4_xritag = NO_XRI;
1010         list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1011 }
1012
1013 /**
1014  * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
1015  * @phba: Pointer to HBA context object.
1016  * @iocbq: Pointer to driver iocb object.
1017  *
1018  * This function is called with hbalock held to release driver
1019  * iocb object to the iocb pool. The iotag in the iocb object
1020  * does not change for each use of the iocb object. This function
1021  * clears all other fields of the iocb object when it is freed.
1022  **/
1023 static void
1024 __lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1025 {
1026         phba->__lpfc_sli_release_iocbq(phba, iocbq);
1027         phba->iocb_cnt--;
1028 }
1029
1030 /**
1031  * lpfc_sli_release_iocbq - Release iocb to the iocb pool
1032  * @phba: Pointer to HBA context object.
1033  * @iocbq: Pointer to driver iocb object.
1034  *
1035  * This function is called with no lock held to release the iocb to
1036  * iocb pool.
1037  **/
1038 void
1039 lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1040 {
1041         unsigned long iflags;
1042
1043         /*
1044          * Clean all volatile data fields, preserve iotag and node struct.
1045          */
1046         spin_lock_irqsave(&phba->hbalock, iflags);
1047         __lpfc_sli_release_iocbq(phba, iocbq);
1048         spin_unlock_irqrestore(&phba->hbalock, iflags);
1049 }
1050
1051 /**
1052  * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
1053  * @phba: Pointer to HBA context object.
1054  * @iocblist: List of IOCBs.
1055  * @ulpstatus: ULP status in IOCB command field.
1056  * @ulpWord4: ULP word-4 in IOCB command field.
1057  *
1058  * This function is called with a list of IOCBs to cancel. It cancels the IOCB
1059  * on the list by invoking the complete callback function associated with the
1060  * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
1061  * fields.
1062  **/
1063 void
1064 lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
1065                       uint32_t ulpstatus, uint32_t ulpWord4)
1066 {
1067         struct lpfc_iocbq *piocb;
1068
1069         while (!list_empty(iocblist)) {
1070                 list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
1071
1072                 if (!piocb->iocb_cmpl)
1073                         lpfc_sli_release_iocbq(phba, piocb);
1074                 else {
1075                         piocb->iocb.ulpStatus = ulpstatus;
1076                         piocb->iocb.un.ulpWord[4] = ulpWord4;
1077                         (piocb->iocb_cmpl) (phba, piocb, piocb);
1078                 }
1079         }
1080         return;
1081 }
1082
1083 /**
1084  * lpfc_sli_iocb_cmd_type - Get the iocb type
1085  * @iocb_cmnd: iocb command code.
1086  *
1087  * This function is called by ring event handler function to get the iocb type.
1088  * This function translates the iocb command to an iocb command type used to
1089  * decide the final disposition of each completed IOCB.
1090  * The function returns
1091  * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
1092  * LPFC_SOL_IOCB     if it is a solicited iocb completion
1093  * LPFC_ABORT_IOCB   if it is an abort iocb
1094  * LPFC_UNSOL_IOCB   if it is an unsolicited iocb
1095  *
1096  * The caller is not required to hold any lock.
1097  **/
1098 static lpfc_iocb_type
1099 lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
1100 {
1101         lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
1102
1103         if (iocb_cmnd > CMD_MAX_IOCB_CMD)
1104                 return 0;
1105
1106         switch (iocb_cmnd) {
1107         case CMD_XMIT_SEQUENCE_CR:
1108         case CMD_XMIT_SEQUENCE_CX:
1109         case CMD_XMIT_BCAST_CN:
1110         case CMD_XMIT_BCAST_CX:
1111         case CMD_ELS_REQUEST_CR:
1112         case CMD_ELS_REQUEST_CX:
1113         case CMD_CREATE_XRI_CR:
1114         case CMD_CREATE_XRI_CX:
1115         case CMD_GET_RPI_CN:
1116         case CMD_XMIT_ELS_RSP_CX:
1117         case CMD_GET_RPI_CR:
1118         case CMD_FCP_IWRITE_CR:
1119         case CMD_FCP_IWRITE_CX:
1120         case CMD_FCP_IREAD_CR:
1121         case CMD_FCP_IREAD_CX:
1122         case CMD_FCP_ICMND_CR:
1123         case CMD_FCP_ICMND_CX:
1124         case CMD_FCP_TSEND_CX:
1125         case CMD_FCP_TRSP_CX:
1126         case CMD_FCP_TRECEIVE_CX:
1127         case CMD_FCP_AUTO_TRSP_CX:
1128         case CMD_ADAPTER_MSG:
1129         case CMD_ADAPTER_DUMP:
1130         case CMD_XMIT_SEQUENCE64_CR:
1131         case CMD_XMIT_SEQUENCE64_CX:
1132         case CMD_XMIT_BCAST64_CN:
1133         case CMD_XMIT_BCAST64_CX:
1134         case CMD_ELS_REQUEST64_CR:
1135         case CMD_ELS_REQUEST64_CX:
1136         case CMD_FCP_IWRITE64_CR:
1137         case CMD_FCP_IWRITE64_CX:
1138         case CMD_FCP_IREAD64_CR:
1139         case CMD_FCP_IREAD64_CX:
1140         case CMD_FCP_ICMND64_CR:
1141         case CMD_FCP_ICMND64_CX:
1142         case CMD_FCP_TSEND64_CX:
1143         case CMD_FCP_TRSP64_CX:
1144         case CMD_FCP_TRECEIVE64_CX:
1145         case CMD_GEN_REQUEST64_CR:
1146         case CMD_GEN_REQUEST64_CX:
1147         case CMD_XMIT_ELS_RSP64_CX:
1148         case DSSCMD_IWRITE64_CR:
1149         case DSSCMD_IWRITE64_CX:
1150         case DSSCMD_IREAD64_CR:
1151         case DSSCMD_IREAD64_CX:
1152                 type = LPFC_SOL_IOCB;
1153                 break;
1154         case CMD_ABORT_XRI_CN:
1155         case CMD_ABORT_XRI_CX:
1156         case CMD_CLOSE_XRI_CN:
1157         case CMD_CLOSE_XRI_CX:
1158         case CMD_XRI_ABORTED_CX:
1159         case CMD_ABORT_MXRI64_CN:
1160         case CMD_XMIT_BLS_RSP64_CX:
1161                 type = LPFC_ABORT_IOCB;
1162                 break;
1163         case CMD_RCV_SEQUENCE_CX:
1164         case CMD_RCV_ELS_REQ_CX:
1165         case CMD_RCV_SEQUENCE64_CX:
1166         case CMD_RCV_ELS_REQ64_CX:
1167         case CMD_ASYNC_STATUS:
1168         case CMD_IOCB_RCV_SEQ64_CX:
1169         case CMD_IOCB_RCV_ELS64_CX:
1170         case CMD_IOCB_RCV_CONT64_CX:
1171         case CMD_IOCB_RET_XRI64_CX:
1172                 type = LPFC_UNSOL_IOCB;
1173                 break;
1174         case CMD_IOCB_XMIT_MSEQ64_CR:
1175         case CMD_IOCB_XMIT_MSEQ64_CX:
1176         case CMD_IOCB_RCV_SEQ_LIST64_CX:
1177         case CMD_IOCB_RCV_ELS_LIST64_CX:
1178         case CMD_IOCB_CLOSE_EXTENDED_CN:
1179         case CMD_IOCB_ABORT_EXTENDED_CN:
1180         case CMD_IOCB_RET_HBQE64_CN:
1181         case CMD_IOCB_FCP_IBIDIR64_CR:
1182         case CMD_IOCB_FCP_IBIDIR64_CX:
1183         case CMD_IOCB_FCP_ITASKMGT64_CX:
1184         case CMD_IOCB_LOGENTRY_CN:
1185         case CMD_IOCB_LOGENTRY_ASYNC_CN:
1186                 printk("%s - Unhandled SLI-3 Command x%x\n",
1187                                 __func__, iocb_cmnd);
1188                 type = LPFC_UNKNOWN_IOCB;
1189                 break;
1190         default:
1191                 type = LPFC_UNKNOWN_IOCB;
1192                 break;
1193         }
1194
1195         return type;
1196 }
1197
1198 /**
1199  * lpfc_sli_ring_map - Issue config_ring mbox for all rings
1200  * @phba: Pointer to HBA context object.
1201  *
1202  * This function is called from SLI initialization code
1203  * to configure every ring of the HBA's SLI interface. The
1204  * caller is not required to hold any lock. This function issues
1205  * a config_ring mailbox command for each ring.
1206  * This function returns zero if successful else returns a negative
1207  * error code.
1208  **/
1209 static int
1210 lpfc_sli_ring_map(struct lpfc_hba *phba)
1211 {
1212         struct lpfc_sli *psli = &phba->sli;
1213         LPFC_MBOXQ_t *pmb;
1214         MAILBOX_t *pmbox;
1215         int i, rc, ret = 0;
1216
1217         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1218         if (!pmb)
1219                 return -ENOMEM;
1220         pmbox = &pmb->u.mb;
1221         phba->link_state = LPFC_INIT_MBX_CMDS;
1222         for (i = 0; i < psli->num_rings; i++) {
1223                 lpfc_config_ring(phba, i, pmb);
1224                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
1225                 if (rc != MBX_SUCCESS) {
1226                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1227                                         "0446 Adapter failed to init (%d), "
1228                                         "mbxCmd x%x CFG_RING, mbxStatus x%x, "
1229                                         "ring %d\n",
1230                                         rc, pmbox->mbxCommand,
1231                                         pmbox->mbxStatus, i);
1232                         phba->link_state = LPFC_HBA_ERROR;
1233                         ret = -ENXIO;
1234                         break;
1235                 }
1236         }
1237         mempool_free(pmb, phba->mbox_mem_pool);
1238         return ret;
1239 }
1240
1241 /**
1242  * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
1243  * @phba: Pointer to HBA context object.
1244  * @pring: Pointer to driver SLI ring object.
1245  * @piocb: Pointer to the driver iocb object.
1246  *
1247  * This function is called with hbalock held. The function adds the
1248  * new iocb to txcmplq of the given ring. This function always returns
1249  * 0. If this function is called for ELS ring, this function checks if
1250  * there is a vport associated with the ELS command. This function also
1251  * starts els_tmofunc timer if this is an ELS command.
1252  **/
1253 static int
1254 lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1255                         struct lpfc_iocbq *piocb)
1256 {
1257         list_add_tail(&piocb->list, &pring->txcmplq);
1258         piocb->iocb_flag |= LPFC_IO_ON_Q;
1259         pring->txcmplq_cnt++;
1260         if (pring->txcmplq_cnt > pring->txcmplq_max)
1261                 pring->txcmplq_max = pring->txcmplq_cnt;
1262
1263         if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
1264            (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
1265            (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
1266                 if (!piocb->vport)
1267                         BUG();
1268                 else
1269                         mod_timer(&piocb->vport->els_tmofunc,
1270                                   jiffies + HZ * (phba->fc_ratov << 1));
1271         }
1272
1273
1274         return 0;
1275 }
1276
1277 /**
1278  * lpfc_sli_ringtx_get - Get first element of the txq
1279  * @phba: Pointer to HBA context object.
1280  * @pring: Pointer to driver SLI ring object.
1281  *
1282  * This function is called with hbalock held to get next
1283  * iocb in txq of the given ring. If there is any iocb in
1284  * the txq, the function returns first iocb in the list after
1285  * removing the iocb from the list, else it returns NULL.
1286  **/
1287 struct lpfc_iocbq *
1288 lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1289 {
1290         struct lpfc_iocbq *cmd_iocb;
1291
1292         list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
1293         if (cmd_iocb != NULL)
1294                 pring->txq_cnt--;
1295         return cmd_iocb;
1296 }
1297
1298 /**
1299  * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
1300  * @phba: Pointer to HBA context object.
1301  * @pring: Pointer to driver SLI ring object.
1302  *
1303  * This function is called with hbalock held and the caller must post the
1304  * iocb without releasing the lock. If the caller releases the lock,
1305  * iocb slot returned by the function is not guaranteed to be available.
1306  * The function returns pointer to the next available iocb slot if there
1307  * is available slot in the ring, else it returns NULL.
1308  * If the get index of the ring is ahead of the put index, the function
1309  * will post an error attention event to the worker thread to take the
1310  * HBA to offline state.
1311  **/
1312 static IOCB_t *
1313 lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1314 {
1315         struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
1316         uint32_t  max_cmd_idx = pring->numCiocb;
1317         if ((pring->next_cmdidx == pring->cmdidx) &&
1318            (++pring->next_cmdidx >= max_cmd_idx))
1319                 pring->next_cmdidx = 0;
1320
1321         if (unlikely(pring->local_getidx == pring->next_cmdidx)) {
1322
1323                 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
1324
1325                 if (unlikely(pring->local_getidx >= max_cmd_idx)) {
1326                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
1327                                         "0315 Ring %d issue: portCmdGet %d "
1328                                         "is bigger than cmd ring %d\n",
1329                                         pring->ringno,
1330                                         pring->local_getidx, max_cmd_idx);
1331
1332                         phba->link_state = LPFC_HBA_ERROR;
1333                         /*
1334                          * All error attention handlers are posted to
1335                          * worker thread
1336                          */
1337                         phba->work_ha |= HA_ERATT;
1338                         phba->work_hs = HS_FFER3;
1339
1340                         lpfc_worker_wake_up(phba);
1341
1342                         return NULL;
1343                 }
1344
1345                 if (pring->local_getidx == pring->next_cmdidx)
1346                         return NULL;
1347         }
1348
1349         return lpfc_cmd_iocb(phba, pring);
1350 }
1351
1352 /**
1353  * lpfc_sli_next_iotag - Get an iotag for the iocb
1354  * @phba: Pointer to HBA context object.
1355  * @iocbq: Pointer to driver iocb object.
1356  *
1357  * This function gets an iotag for the iocb. If there is no unused iotag and
1358  * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
1359  * array and assigns a new iotag.
1360  * The function returns the allocated iotag if successful, else returns zero.
1361  * Zero is not a valid iotag.
1362  * The caller is not required to hold any lock.
1363  **/
1364 uint16_t
1365 lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1366 {
1367         struct lpfc_iocbq **new_arr;
1368         struct lpfc_iocbq **old_arr;
1369         size_t new_len;
1370         struct lpfc_sli *psli = &phba->sli;
1371         uint16_t iotag;
1372
1373         spin_lock_irq(&phba->hbalock);
1374         iotag = psli->last_iotag;
1375         if(++iotag < psli->iocbq_lookup_len) {
1376                 psli->last_iotag = iotag;
1377                 psli->iocbq_lookup[iotag] = iocbq;
1378                 spin_unlock_irq(&phba->hbalock);
1379                 iocbq->iotag = iotag;
1380                 return iotag;
1381         } else if (psli->iocbq_lookup_len < (0xffff
1382                                            - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
1383                 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
1384                 spin_unlock_irq(&phba->hbalock);
1385                 new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
1386                                   GFP_KERNEL);
1387                 if (new_arr) {
1388                         spin_lock_irq(&phba->hbalock);
1389                         old_arr = psli->iocbq_lookup;
1390                         if (new_len <= psli->iocbq_lookup_len) {
1391                                 /* highly unprobable case */
1392                                 kfree(new_arr);
1393                                 iotag = psli->last_iotag;
1394                                 if(++iotag < psli->iocbq_lookup_len) {
1395                                         psli->last_iotag = iotag;
1396                                         psli->iocbq_lookup[iotag] = iocbq;
1397                                         spin_unlock_irq(&phba->hbalock);
1398                                         iocbq->iotag = iotag;
1399                                         return iotag;
1400                                 }
1401                                 spin_unlock_irq(&phba->hbalock);
1402                                 return 0;
1403                         }
1404                         if (psli->iocbq_lookup)
1405                                 memcpy(new_arr, old_arr,
1406                                        ((psli->last_iotag  + 1) *
1407                                         sizeof (struct lpfc_iocbq *)));
1408                         psli->iocbq_lookup = new_arr;
1409                         psli->iocbq_lookup_len = new_len;
1410                         psli->last_iotag = iotag;
1411                         psli->iocbq_lookup[iotag] = iocbq;
1412                         spin_unlock_irq(&phba->hbalock);
1413                         iocbq->iotag = iotag;
1414                         kfree(old_arr);
1415                         return iotag;
1416                 }
1417         } else
1418                 spin_unlock_irq(&phba->hbalock);
1419
1420         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
1421                         "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
1422                         psli->last_iotag);
1423
1424         return 0;
1425 }
1426
1427 /**
1428  * lpfc_sli_submit_iocb - Submit an iocb to the firmware
1429  * @phba: Pointer to HBA context object.
1430  * @pring: Pointer to driver SLI ring object.
1431  * @iocb: Pointer to iocb slot in the ring.
1432  * @nextiocb: Pointer to driver iocb object which need to be
1433  *            posted to firmware.
1434  *
1435  * This function is called with hbalock held to post a new iocb to
1436  * the firmware. This function copies the new iocb to ring iocb slot and
1437  * updates the ring pointers. It adds the new iocb to txcmplq if there is
1438  * a completion call back for this iocb else the function will free the
1439  * iocb object.
1440  **/
1441 static void
1442 lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1443                 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
1444 {
1445         /*
1446          * Set up an iotag
1447          */
1448         nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
1449
1450
1451         if (pring->ringno == LPFC_ELS_RING) {
1452                 lpfc_debugfs_slow_ring_trc(phba,
1453                         "IOCB cmd ring:   wd4:x%08x wd6:x%08x wd7:x%08x",
1454                         *(((uint32_t *) &nextiocb->iocb) + 4),
1455                         *(((uint32_t *) &nextiocb->iocb) + 6),
1456                         *(((uint32_t *) &nextiocb->iocb) + 7));
1457         }
1458
1459         /*
1460          * Issue iocb command to adapter
1461          */
1462         lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
1463         wmb();
1464         pring->stats.iocb_cmd++;
1465
1466         /*
1467          * If there is no completion routine to call, we can release the
1468          * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
1469          * that have no rsp ring completion, iocb_cmpl MUST be NULL.
1470          */
1471         if (nextiocb->iocb_cmpl)
1472                 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
1473         else
1474                 __lpfc_sli_release_iocbq(phba, nextiocb);
1475
1476         /*
1477          * Let the HBA know what IOCB slot will be the next one the
1478          * driver will put a command into.
1479          */
1480         pring->cmdidx = pring->next_cmdidx;
1481         writel(pring->cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
1482 }
1483
1484 /**
1485  * lpfc_sli_update_full_ring - Update the chip attention register
1486  * @phba: Pointer to HBA context object.
1487  * @pring: Pointer to driver SLI ring object.
1488  *
1489  * The caller is not required to hold any lock for calling this function.
1490  * This function updates the chip attention bits for the ring to inform firmware
1491  * that there are pending work to be done for this ring and requests an
1492  * interrupt when there is space available in the ring. This function is
1493  * called when the driver is unable to post more iocbs to the ring due
1494  * to unavailability of space in the ring.
1495  **/
1496 static void
1497 lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1498 {
1499         int ringno = pring->ringno;
1500
1501         pring->flag |= LPFC_CALL_RING_AVAILABLE;
1502
1503         wmb();
1504
1505         /*
1506          * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
1507          * The HBA will tell us when an IOCB entry is available.
1508          */
1509         writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
1510         readl(phba->CAregaddr); /* flush */
1511
1512         pring->stats.iocb_cmd_full++;
1513 }
1514
1515 /**
1516  * lpfc_sli_update_ring - Update chip attention register
1517  * @phba: Pointer to HBA context object.
1518  * @pring: Pointer to driver SLI ring object.
1519  *
1520  * This function updates the chip attention register bit for the
1521  * given ring to inform HBA that there is more work to be done
1522  * in this ring. The caller is not required to hold any lock.
1523  **/
1524 static void
1525 lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1526 {
1527         int ringno = pring->ringno;
1528
1529         /*
1530          * Tell the HBA that there is work to do in this ring.
1531          */
1532         if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
1533                 wmb();
1534                 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
1535                 readl(phba->CAregaddr); /* flush */
1536         }
1537 }
1538
1539 /**
1540  * lpfc_sli_resume_iocb - Process iocbs in the txq
1541  * @phba: Pointer to HBA context object.
1542  * @pring: Pointer to driver SLI ring object.
1543  *
1544  * This function is called with hbalock held to post pending iocbs
1545  * in the txq to the firmware. This function is called when driver
1546  * detects space available in the ring.
1547  **/
1548 static void
1549 lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1550 {
1551         IOCB_t *iocb;
1552         struct lpfc_iocbq *nextiocb;
1553
1554         /*
1555          * Check to see if:
1556          *  (a) there is anything on the txq to send
1557          *  (b) link is up
1558          *  (c) link attention events can be processed (fcp ring only)
1559          *  (d) IOCB processing is not blocked by the outstanding mbox command.
1560          */
1561         if (pring->txq_cnt &&
1562             lpfc_is_link_up(phba) &&
1563             (pring->ringno != phba->sli.fcp_ring ||
1564              phba->sli.sli_flag & LPFC_PROCESS_LA)) {
1565
1566                 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
1567                        (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
1568                         lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
1569
1570                 if (iocb)
1571                         lpfc_sli_update_ring(phba, pring);
1572                 else
1573                         lpfc_sli_update_full_ring(phba, pring);
1574         }
1575
1576         return;
1577 }
1578
1579 /**
1580  * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
1581  * @phba: Pointer to HBA context object.
1582  * @hbqno: HBQ number.
1583  *
1584  * This function is called with hbalock held to get the next
1585  * available slot for the given HBQ. If there is free slot
1586  * available for the HBQ it will return pointer to the next available
1587  * HBQ entry else it will return NULL.
1588  **/
1589 static struct lpfc_hbq_entry *
1590 lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
1591 {
1592         struct hbq_s *hbqp = &phba->hbqs[hbqno];
1593
1594         if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
1595             ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
1596                 hbqp->next_hbqPutIdx = 0;
1597
1598         if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
1599                 uint32_t raw_index = phba->hbq_get[hbqno];
1600                 uint32_t getidx = le32_to_cpu(raw_index);
1601
1602                 hbqp->local_hbqGetIdx = getidx;
1603
1604                 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
1605                         lpfc_printf_log(phba, KERN_ERR,
1606                                         LOG_SLI | LOG_VPORT,
1607                                         "1802 HBQ %d: local_hbqGetIdx "
1608                                         "%u is > than hbqp->entry_count %u\n",
1609                                         hbqno, hbqp->local_hbqGetIdx,
1610                                         hbqp->entry_count);
1611
1612                         phba->link_state = LPFC_HBA_ERROR;
1613                         return NULL;
1614                 }
1615
1616                 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
1617                         return NULL;
1618         }
1619
1620         return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
1621                         hbqp->hbqPutIdx;
1622 }
1623
1624 /**
1625  * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
1626  * @phba: Pointer to HBA context object.
1627  *
1628  * This function is called with no lock held to free all the
1629  * hbq buffers while uninitializing the SLI interface. It also
1630  * frees the HBQ buffers returned by the firmware but not yet
1631  * processed by the upper layers.
1632  **/
1633 void
1634 lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
1635 {
1636         struct lpfc_dmabuf *dmabuf, *next_dmabuf;
1637         struct hbq_dmabuf *hbq_buf;
1638         unsigned long flags;
1639         int i, hbq_count;
1640         uint32_t hbqno;
1641
1642         hbq_count = lpfc_sli_hbq_count();
1643         /* Return all memory used by all HBQs */
1644         spin_lock_irqsave(&phba->hbalock, flags);
1645         for (i = 0; i < hbq_count; ++i) {
1646                 list_for_each_entry_safe(dmabuf, next_dmabuf,
1647                                 &phba->hbqs[i].hbq_buffer_list, list) {
1648                         hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1649                         list_del(&hbq_buf->dbuf.list);
1650                         (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
1651                 }
1652                 phba->hbqs[i].buffer_count = 0;
1653         }
1654         /* Return all HBQ buffer that are in-fly */
1655         list_for_each_entry_safe(dmabuf, next_dmabuf, &phba->rb_pend_list,
1656                                  list) {
1657                 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1658                 list_del(&hbq_buf->dbuf.list);
1659                 if (hbq_buf->tag == -1) {
1660                         (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1661                                 (phba, hbq_buf);
1662                 } else {
1663                         hbqno = hbq_buf->tag >> 16;
1664                         if (hbqno >= LPFC_MAX_HBQS)
1665                                 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1666                                         (phba, hbq_buf);
1667                         else
1668                                 (phba->hbqs[hbqno].hbq_free_buffer)(phba,
1669                                         hbq_buf);
1670                 }
1671         }
1672
1673         /* Mark the HBQs not in use */
1674         phba->hbq_in_use = 0;
1675         spin_unlock_irqrestore(&phba->hbalock, flags);
1676 }
1677
1678 /**
1679  * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
1680  * @phba: Pointer to HBA context object.
1681  * @hbqno: HBQ number.
1682  * @hbq_buf: Pointer to HBQ buffer.
1683  *
1684  * This function is called with the hbalock held to post a
1685  * hbq buffer to the firmware. If the function finds an empty
1686  * slot in the HBQ, it will post the buffer. The function will return
1687  * pointer to the hbq entry if it successfully post the buffer
1688  * else it will return NULL.
1689  **/
1690 static int
1691 lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
1692                          struct hbq_dmabuf *hbq_buf)
1693 {
1694         return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
1695 }
1696
1697 /**
1698  * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
1699  * @phba: Pointer to HBA context object.
1700  * @hbqno: HBQ number.
1701  * @hbq_buf: Pointer to HBQ buffer.
1702  *
1703  * This function is called with the hbalock held to post a hbq buffer to the
1704  * firmware. If the function finds an empty slot in the HBQ, it will post the
1705  * buffer and place it on the hbq_buffer_list. The function will return zero if
1706  * it successfully post the buffer else it will return an error.
1707  **/
1708 static int
1709 lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
1710                             struct hbq_dmabuf *hbq_buf)
1711 {
1712         struct lpfc_hbq_entry *hbqe;
1713         dma_addr_t physaddr = hbq_buf->dbuf.phys;
1714
1715         /* Get next HBQ entry slot to use */
1716         hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
1717         if (hbqe) {
1718                 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1719
1720                 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1721                 hbqe->bde.addrLow  = le32_to_cpu(putPaddrLow(physaddr));
1722                 hbqe->bde.tus.f.bdeSize = hbq_buf->size;
1723                 hbqe->bde.tus.f.bdeFlags = 0;
1724                 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
1725                 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
1726                                 /* Sync SLIM */
1727                 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
1728                 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
1729                                 /* flush */
1730                 readl(phba->hbq_put + hbqno);
1731                 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
1732                 return 0;
1733         } else
1734                 return -ENOMEM;
1735 }
1736
1737 /**
1738  * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
1739  * @phba: Pointer to HBA context object.
1740  * @hbqno: HBQ number.
1741  * @hbq_buf: Pointer to HBQ buffer.
1742  *
1743  * This function is called with the hbalock held to post an RQE to the SLI4
1744  * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
1745  * the hbq_buffer_list and return zero, otherwise it will return an error.
1746  **/
1747 static int
1748 lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
1749                             struct hbq_dmabuf *hbq_buf)
1750 {
1751         int rc;
1752         struct lpfc_rqe hrqe;
1753         struct lpfc_rqe drqe;
1754
1755         hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
1756         hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
1757         drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
1758         drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
1759         rc = lpfc_sli4_rq_put(phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
1760                               &hrqe, &drqe);
1761         if (rc < 0)
1762                 return rc;
1763         hbq_buf->tag = rc;
1764         list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
1765         return 0;
1766 }
1767
1768 /* HBQ for ELS and CT traffic. */
1769 static struct lpfc_hbq_init lpfc_els_hbq = {
1770         .rn = 1,
1771         .entry_count = 256,
1772         .mask_count = 0,
1773         .profile = 0,
1774         .ring_mask = (1 << LPFC_ELS_RING),
1775         .buffer_count = 0,
1776         .init_count = 40,
1777         .add_count = 40,
1778 };
1779
1780 /* HBQ for the extra ring if needed */
1781 static struct lpfc_hbq_init lpfc_extra_hbq = {
1782         .rn = 1,
1783         .entry_count = 200,
1784         .mask_count = 0,
1785         .profile = 0,
1786         .ring_mask = (1 << LPFC_EXTRA_RING),
1787         .buffer_count = 0,
1788         .init_count = 0,
1789         .add_count = 5,
1790 };
1791
1792 /* Array of HBQs */
1793 struct lpfc_hbq_init *lpfc_hbq_defs[] = {
1794         &lpfc_els_hbq,
1795         &lpfc_extra_hbq,
1796 };
1797
1798 /**
1799  * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
1800  * @phba: Pointer to HBA context object.
1801  * @hbqno: HBQ number.
1802  * @count: Number of HBQ buffers to be posted.
1803  *
1804  * This function is called with no lock held to post more hbq buffers to the
1805  * given HBQ. The function returns the number of HBQ buffers successfully
1806  * posted.
1807  **/
1808 static int
1809 lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
1810 {
1811         uint32_t i, posted = 0;
1812         unsigned long flags;
1813         struct hbq_dmabuf *hbq_buffer;
1814         LIST_HEAD(hbq_buf_list);
1815         if (!phba->hbqs[hbqno].hbq_alloc_buffer)
1816                 return 0;
1817
1818         if ((phba->hbqs[hbqno].buffer_count + count) >
1819             lpfc_hbq_defs[hbqno]->entry_count)
1820                 count = lpfc_hbq_defs[hbqno]->entry_count -
1821                                         phba->hbqs[hbqno].buffer_count;
1822         if (!count)
1823                 return 0;
1824         /* Allocate HBQ entries */
1825         for (i = 0; i < count; i++) {
1826                 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
1827                 if (!hbq_buffer)
1828                         break;
1829                 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
1830         }
1831         /* Check whether HBQ is still in use */
1832         spin_lock_irqsave(&phba->hbalock, flags);
1833         if (!phba->hbq_in_use)
1834                 goto err;
1835         while (!list_empty(&hbq_buf_list)) {
1836                 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1837                                  dbuf.list);
1838                 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
1839                                       (hbqno << 16));
1840                 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
1841                         phba->hbqs[hbqno].buffer_count++;
1842                         posted++;
1843                 } else
1844                         (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1845         }
1846         spin_unlock_irqrestore(&phba->hbalock, flags);
1847         return posted;
1848 err:
1849         spin_unlock_irqrestore(&phba->hbalock, flags);
1850         while (!list_empty(&hbq_buf_list)) {
1851                 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1852                                  dbuf.list);
1853                 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1854         }
1855         return 0;
1856 }
1857
1858 /**
1859  * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
1860  * @phba: Pointer to HBA context object.
1861  * @qno: HBQ number.
1862  *
1863  * This function posts more buffers to the HBQ. This function
1864  * is called with no lock held. The function returns the number of HBQ entries
1865  * successfully allocated.
1866  **/
1867 int
1868 lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
1869 {
1870         if (phba->sli_rev == LPFC_SLI_REV4)
1871                 return 0;
1872         else
1873                 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1874                                          lpfc_hbq_defs[qno]->add_count);
1875 }
1876
1877 /**
1878  * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
1879  * @phba: Pointer to HBA context object.
1880  * @qno:  HBQ queue number.
1881  *
1882  * This function is called from SLI initialization code path with
1883  * no lock held to post initial HBQ buffers to firmware. The
1884  * function returns the number of HBQ entries successfully allocated.
1885  **/
1886 static int
1887 lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
1888 {
1889         if (phba->sli_rev == LPFC_SLI_REV4)
1890                 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1891                                          lpfc_hbq_defs[qno]->entry_count);
1892         else
1893                 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1894                                          lpfc_hbq_defs[qno]->init_count);
1895 }
1896
1897 /**
1898  * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
1899  * @phba: Pointer to HBA context object.
1900  * @hbqno: HBQ number.
1901  *
1902  * This function removes the first hbq buffer on an hbq list and returns a
1903  * pointer to that buffer. If it finds no buffers on the list it returns NULL.
1904  **/
1905 static struct hbq_dmabuf *
1906 lpfc_sli_hbqbuf_get(struct list_head *rb_list)
1907 {
1908         struct lpfc_dmabuf *d_buf;
1909
1910         list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
1911         if (!d_buf)
1912                 return NULL;
1913         return container_of(d_buf, struct hbq_dmabuf, dbuf);
1914 }
1915
1916 /**
1917  * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
1918  * @phba: Pointer to HBA context object.
1919  * @tag: Tag of the hbq buffer.
1920  *
1921  * This function is called with hbalock held. This function searches
1922  * for the hbq buffer associated with the given tag in the hbq buffer
1923  * list. If it finds the hbq buffer, it returns the hbq_buffer other wise
1924  * it returns NULL.
1925  **/
1926 static struct hbq_dmabuf *
1927 lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
1928 {
1929         struct lpfc_dmabuf *d_buf;
1930         struct hbq_dmabuf *hbq_buf;
1931         uint32_t hbqno;
1932
1933         hbqno = tag >> 16;
1934         if (hbqno >= LPFC_MAX_HBQS)
1935                 return NULL;
1936
1937         spin_lock_irq(&phba->hbalock);
1938         list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
1939                 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
1940                 if (hbq_buf->tag == tag) {
1941                         spin_unlock_irq(&phba->hbalock);
1942                         return hbq_buf;
1943                 }
1944         }
1945         spin_unlock_irq(&phba->hbalock);
1946         lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
1947                         "1803 Bad hbq tag. Data: x%x x%x\n",
1948                         tag, phba->hbqs[tag >> 16].buffer_count);
1949         return NULL;
1950 }
1951
1952 /**
1953  * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
1954  * @phba: Pointer to HBA context object.
1955  * @hbq_buffer: Pointer to HBQ buffer.
1956  *
1957  * This function is called with hbalock. This function gives back
1958  * the hbq buffer to firmware. If the HBQ does not have space to
1959  * post the buffer, it will free the buffer.
1960  **/
1961 void
1962 lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
1963 {
1964         uint32_t hbqno;
1965
1966         if (hbq_buffer) {
1967                 hbqno = hbq_buffer->tag >> 16;
1968                 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
1969                         (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1970         }
1971 }
1972
1973 /**
1974  * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
1975  * @mbxCommand: mailbox command code.
1976  *
1977  * This function is called by the mailbox event handler function to verify
1978  * that the completed mailbox command is a legitimate mailbox command. If the
1979  * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
1980  * and the mailbox event handler will take the HBA offline.
1981  **/
1982 static int
1983 lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
1984 {
1985         uint8_t ret;
1986
1987         switch (mbxCommand) {
1988         case MBX_LOAD_SM:
1989         case MBX_READ_NV:
1990         case MBX_WRITE_NV:
1991         case MBX_WRITE_VPARMS:
1992         case MBX_RUN_BIU_DIAG:
1993         case MBX_INIT_LINK:
1994         case MBX_DOWN_LINK:
1995         case MBX_CONFIG_LINK:
1996         case MBX_CONFIG_RING:
1997         case MBX_RESET_RING:
1998         case MBX_READ_CONFIG:
1999         case MBX_READ_RCONFIG:
2000         case MBX_READ_SPARM:
2001         case MBX_READ_STATUS:
2002         case MBX_READ_RPI:
2003         case MBX_READ_XRI:
2004         case MBX_READ_REV:
2005         case MBX_READ_LNK_STAT:
2006         case MBX_REG_LOGIN:
2007         case MBX_UNREG_LOGIN:
2008         case MBX_CLEAR_LA:
2009         case MBX_DUMP_MEMORY:
2010         case MBX_DUMP_CONTEXT:
2011         case MBX_RUN_DIAGS:
2012         case MBX_RESTART:
2013         case MBX_UPDATE_CFG:
2014         case MBX_DOWN_LOAD:
2015         case MBX_DEL_LD_ENTRY:
2016         case MBX_RUN_PROGRAM:
2017         case MBX_SET_MASK:
2018         case MBX_SET_VARIABLE:
2019         case MBX_UNREG_D_ID:
2020         case MBX_KILL_BOARD:
2021         case MBX_CONFIG_FARP:
2022         case MBX_BEACON:
2023         case MBX_LOAD_AREA:
2024         case MBX_RUN_BIU_DIAG64:
2025         case MBX_CONFIG_PORT:
2026         case MBX_READ_SPARM64:
2027         case MBX_READ_RPI64:
2028         case MBX_REG_LOGIN64:
2029         case MBX_READ_TOPOLOGY:
2030         case MBX_WRITE_WWN:
2031         case MBX_SET_DEBUG:
2032         case MBX_LOAD_EXP_ROM:
2033         case MBX_ASYNCEVT_ENABLE:
2034         case MBX_REG_VPI:
2035         case MBX_UNREG_VPI:
2036         case MBX_HEARTBEAT:
2037         case MBX_PORT_CAPABILITIES:
2038         case MBX_PORT_IOV_CONTROL:
2039         case MBX_SLI4_CONFIG:
2040         case MBX_SLI4_REQ_FTRS:
2041         case MBX_REG_FCFI:
2042         case MBX_UNREG_FCFI:
2043         case MBX_REG_VFI:
2044         case MBX_UNREG_VFI:
2045         case MBX_INIT_VPI:
2046         case MBX_INIT_VFI:
2047         case MBX_RESUME_RPI:
2048         case MBX_READ_EVENT_LOG_STATUS:
2049         case MBX_READ_EVENT_LOG:
2050         case MBX_SECURITY_MGMT:
2051         case MBX_AUTH_PORT:
2052                 ret = mbxCommand;
2053                 break;
2054         default:
2055                 ret = MBX_SHUTDOWN;
2056                 break;
2057         }
2058         return ret;
2059 }
2060
2061 /**
2062  * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
2063  * @phba: Pointer to HBA context object.
2064  * @pmboxq: Pointer to mailbox command.
2065  *
2066  * This is completion handler function for mailbox commands issued from
2067  * lpfc_sli_issue_mbox_wait function. This function is called by the
2068  * mailbox event handler function with no lock held. This function
2069  * will wake up thread waiting on the wait queue pointed by context1
2070  * of the mailbox.
2071  **/
2072 void
2073 lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
2074 {
2075         wait_queue_head_t *pdone_q;
2076         unsigned long drvr_flag;
2077
2078         /*
2079          * If pdone_q is empty, the driver thread gave up waiting and
2080          * continued running.
2081          */
2082         pmboxq->mbox_flag |= LPFC_MBX_WAKE;
2083         spin_lock_irqsave(&phba->hbalock, drvr_flag);
2084         pdone_q = (wait_queue_head_t *) pmboxq->context1;
2085         if (pdone_q)
2086                 wake_up_interruptible(pdone_q);
2087         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
2088         return;
2089 }
2090
2091
2092 /**
2093  * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
2094  * @phba: Pointer to HBA context object.
2095  * @pmb: Pointer to mailbox object.
2096  *
2097  * This function is the default mailbox completion handler. It
2098  * frees the memory resources associated with the completed mailbox
2099  * command. If the completed command is a REG_LOGIN mailbox command,
2100  * this function will issue a UREG_LOGIN to re-claim the RPI.
2101  **/
2102 void
2103 lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2104 {
2105         struct lpfc_vport  *vport = pmb->vport;
2106         struct lpfc_dmabuf *mp;
2107         struct lpfc_nodelist *ndlp;
2108         struct Scsi_Host *shost;
2109         uint16_t rpi, vpi;
2110         int rc;
2111
2112         mp = (struct lpfc_dmabuf *) (pmb->context1);
2113
2114         if (mp) {
2115                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2116                 kfree(mp);
2117         }
2118
2119         if ((pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) &&
2120             (phba->sli_rev == LPFC_SLI_REV4) &&
2121             (pmb->u.mb.un.varUnregLogin.rsvd1 == 0x0))
2122                 lpfc_sli4_free_rpi(phba, pmb->u.mb.un.varUnregLogin.rpi);
2123
2124         /*
2125          * If a REG_LOGIN succeeded  after node is destroyed or node
2126          * is in re-discovery driver need to cleanup the RPI.
2127          */
2128         if (!(phba->pport->load_flag & FC_UNLOADING) &&
2129             pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
2130             !pmb->u.mb.mbxStatus) {
2131                 rpi = pmb->u.mb.un.varWords[0];
2132                 vpi = pmb->u.mb.un.varRegLogin.vpi - phba->vpi_base;
2133                 lpfc_unreg_login(phba, vpi, rpi, pmb);
2134                 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
2135                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2136                 if (rc != MBX_NOT_FINISHED)
2137                         return;
2138         }
2139
2140         if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
2141                 !(phba->pport->load_flag & FC_UNLOADING) &&
2142                 !pmb->u.mb.mbxStatus) {
2143                 shost = lpfc_shost_from_vport(vport);
2144                 spin_lock_irq(shost->host_lock);
2145                 vport->vpi_state |= LPFC_VPI_REGISTERED;
2146                 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
2147                 spin_unlock_irq(shost->host_lock);
2148         }
2149
2150         if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
2151                 ndlp = (struct lpfc_nodelist *)pmb->context2;
2152                 lpfc_nlp_put(ndlp);
2153                 pmb->context2 = NULL;
2154         }
2155
2156         /* Check security permission status on INIT_LINK mailbox command */
2157         if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
2158             (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
2159                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
2160                                 "2860 SLI authentication is required "
2161                                 "for INIT_LINK but has not done yet\n");
2162
2163         if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
2164                 lpfc_sli4_mbox_cmd_free(phba, pmb);
2165         else
2166                 mempool_free(pmb, phba->mbox_mem_pool);
2167 }
2168
2169 /**
2170  * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
2171  * @phba: Pointer to HBA context object.
2172  *
2173  * This function is called with no lock held. This function processes all
2174  * the completed mailbox commands and gives it to upper layers. The interrupt
2175  * service routine processes mailbox completion interrupt and adds completed
2176  * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
2177  * Worker thread call lpfc_sli_handle_mb_event, which will return the
2178  * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
2179  * function returns the mailbox commands to the upper layer by calling the
2180  * completion handler function of each mailbox.
2181  **/
2182 int
2183 lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
2184 {
2185         MAILBOX_t *pmbox;
2186         LPFC_MBOXQ_t *pmb;
2187         int rc;
2188         LIST_HEAD(cmplq);
2189
2190         phba->sli.slistat.mbox_event++;
2191
2192         /* Get all completed mailboxe buffers into the cmplq */
2193         spin_lock_irq(&phba->hbalock);
2194         list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
2195         spin_unlock_irq(&phba->hbalock);
2196
2197         /* Get a Mailbox buffer to setup mailbox commands for callback */
2198         do {
2199                 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
2200                 if (pmb == NULL)
2201                         break;
2202
2203                 pmbox = &pmb->u.mb;
2204
2205                 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
2206                         if (pmb->vport) {
2207                                 lpfc_debugfs_disc_trc(pmb->vport,
2208                                         LPFC_DISC_TRC_MBOX_VPORT,
2209                                         "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
2210                                         (uint32_t)pmbox->mbxCommand,
2211                                         pmbox->un.varWords[0],
2212                                         pmbox->un.varWords[1]);
2213                         }
2214                         else {
2215                                 lpfc_debugfs_disc_trc(phba->pport,
2216                                         LPFC_DISC_TRC_MBOX,
2217                                         "MBOX cmpl:       cmd:x%x mb:x%x x%x",
2218                                         (uint32_t)pmbox->mbxCommand,
2219                                         pmbox->un.varWords[0],
2220                                         pmbox->un.varWords[1]);
2221                         }
2222                 }
2223
2224                 /*
2225                  * It is a fatal error if unknown mbox command completion.
2226                  */
2227                 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
2228                     MBX_SHUTDOWN) {
2229                         /* Unknown mailbox command compl */
2230                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
2231                                         "(%d):0323 Unknown Mailbox command "
2232                                         "x%x (x%x) Cmpl\n",
2233                                         pmb->vport ? pmb->vport->vpi : 0,
2234                                         pmbox->mbxCommand,
2235                                         lpfc_sli4_mbox_opcode_get(phba, pmb));
2236                         phba->link_state = LPFC_HBA_ERROR;
2237                         phba->work_hs = HS_FFER3;
2238                         lpfc_handle_eratt(phba);
2239                         continue;
2240                 }
2241
2242                 if (pmbox->mbxStatus) {
2243                         phba->sli.slistat.mbox_stat_err++;
2244                         if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
2245                                 /* Mbox cmd cmpl error - RETRYing */
2246                                 lpfc_printf_log(phba, KERN_INFO,
2247                                                 LOG_MBOX | LOG_SLI,
2248                                                 "(%d):0305 Mbox cmd cmpl "
2249                                                 "error - RETRYing Data: x%x "
2250                                                 "(x%x) x%x x%x x%x\n",
2251                                                 pmb->vport ? pmb->vport->vpi :0,
2252                                                 pmbox->mbxCommand,
2253                                                 lpfc_sli4_mbox_opcode_get(phba,
2254                                                                           pmb),
2255                                                 pmbox->mbxStatus,
2256                                                 pmbox->un.varWords[0],
2257                                                 pmb->vport->port_state);
2258                                 pmbox->mbxStatus = 0;
2259                                 pmbox->mbxOwner = OWN_HOST;
2260                                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2261                                 if (rc != MBX_NOT_FINISHED)
2262                                         continue;
2263                         }
2264                 }
2265
2266                 /* Mailbox cmd <cmd> Cmpl <cmpl> */
2267                 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
2268                                 "(%d):0307 Mailbox cmd x%x (x%x) Cmpl x%p "
2269                                 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x\n",
2270                                 pmb->vport ? pmb->vport->vpi : 0,
2271                                 pmbox->mbxCommand,
2272                                 lpfc_sli4_mbox_opcode_get(phba, pmb),
2273                                 pmb->mbox_cmpl,
2274                                 *((uint32_t *) pmbox),
2275                                 pmbox->un.varWords[0],
2276                                 pmbox->un.varWords[1],
2277                                 pmbox->un.varWords[2],
2278                                 pmbox->un.varWords[3],
2279                                 pmbox->un.varWords[4],
2280                                 pmbox->un.varWords[5],
2281                                 pmbox->un.varWords[6],
2282                                 pmbox->un.varWords[7]);
2283
2284                 if (pmb->mbox_cmpl)
2285                         pmb->mbox_cmpl(phba,pmb);
2286         } while (1);
2287         return 0;
2288 }
2289
2290 /**
2291  * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
2292  * @phba: Pointer to HBA context object.
2293  * @pring: Pointer to driver SLI ring object.
2294  * @tag: buffer tag.
2295  *
2296  * This function is called with no lock held. When QUE_BUFTAG_BIT bit
2297  * is set in the tag the buffer is posted for a particular exchange,
2298  * the function will return the buffer without replacing the buffer.
2299  * If the buffer is for unsolicited ELS or CT traffic, this function
2300  * returns the buffer and also posts another buffer to the firmware.
2301  **/
2302 static struct lpfc_dmabuf *
2303 lpfc_sli_get_buff(struct lpfc_hba *phba,
2304                   struct lpfc_sli_ring *pring,
2305                   uint32_t tag)
2306 {
2307         struct hbq_dmabuf *hbq_entry;
2308
2309         if (tag & QUE_BUFTAG_BIT)
2310                 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
2311         hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
2312         if (!hbq_entry)
2313                 return NULL;
2314         return &hbq_entry->dbuf;
2315 }
2316
2317 /**
2318  * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
2319  * @phba: Pointer to HBA context object.
2320  * @pring: Pointer to driver SLI ring object.
2321  * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
2322  * @fch_r_ctl: the r_ctl for the first frame of the sequence.
2323  * @fch_type: the type for the first frame of the sequence.
2324  *
2325  * This function is called with no lock held. This function uses the r_ctl and
2326  * type of the received sequence to find the correct callback function to call
2327  * to process the sequence.
2328  **/
2329 static int
2330 lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2331                          struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
2332                          uint32_t fch_type)
2333 {
2334         int i;
2335
2336         /* unSolicited Responses */
2337         if (pring->prt[0].profile) {
2338                 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
2339                         (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
2340                                                                         saveq);
2341                 return 1;
2342         }
2343         /* We must search, based on rctl / type
2344            for the right routine */
2345         for (i = 0; i < pring->num_mask; i++) {
2346                 if ((pring->prt[i].rctl == fch_r_ctl) &&
2347                     (pring->prt[i].type == fch_type)) {
2348                         if (pring->prt[i].lpfc_sli_rcv_unsol_event)
2349                                 (pring->prt[i].lpfc_sli_rcv_unsol_event)
2350                                                 (phba, pring, saveq);
2351                         return 1;
2352                 }
2353         }
2354         return 0;
2355 }
2356
2357 /**
2358  * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
2359  * @phba: Pointer to HBA context object.
2360  * @pring: Pointer to driver SLI ring object.
2361  * @saveq: Pointer to the unsolicited iocb.
2362  *
2363  * This function is called with no lock held by the ring event handler
2364  * when there is an unsolicited iocb posted to the response ring by the
2365  * firmware. This function gets the buffer associated with the iocbs
2366  * and calls the event handler for the ring. This function handles both
2367  * qring buffers and hbq buffers.
2368  * When the function returns 1 the caller can free the iocb object otherwise
2369  * upper layer functions will free the iocb objects.
2370  **/
2371 static int
2372 lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2373                             struct lpfc_iocbq *saveq)
2374 {
2375         IOCB_t           * irsp;
2376         WORD5            * w5p;
2377         uint32_t           Rctl, Type;
2378         uint32_t           match;
2379         struct lpfc_iocbq *iocbq;
2380         struct lpfc_dmabuf *dmzbuf;
2381
2382         match = 0;
2383         irsp = &(saveq->iocb);
2384
2385         if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
2386                 if (pring->lpfc_sli_rcv_async_status)
2387                         pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
2388                 else
2389                         lpfc_printf_log(phba,
2390                                         KERN_WARNING,
2391                                         LOG_SLI,
2392                                         "0316 Ring %d handler: unexpected "
2393                                         "ASYNC_STATUS iocb received evt_code "
2394                                         "0x%x\n",
2395                                         pring->ringno,
2396                                         irsp->un.asyncstat.evt_code);
2397                 return 1;
2398         }
2399
2400         if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
2401                 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
2402                 if (irsp->ulpBdeCount > 0) {
2403                         dmzbuf = lpfc_sli_get_buff(phba, pring,
2404                                         irsp->un.ulpWord[3]);
2405                         lpfc_in_buf_free(phba, dmzbuf);
2406                 }
2407
2408                 if (irsp->ulpBdeCount > 1) {
2409                         dmzbuf = lpfc_sli_get_buff(phba, pring,
2410                                         irsp->unsli3.sli3Words[3]);
2411                         lpfc_in_buf_free(phba, dmzbuf);
2412                 }
2413
2414                 if (irsp->ulpBdeCount > 2) {
2415                         dmzbuf = lpfc_sli_get_buff(phba, pring,
2416                                 irsp->unsli3.sli3Words[7]);
2417                         lpfc_in_buf_free(phba, dmzbuf);
2418                 }
2419
2420                 return 1;
2421         }
2422
2423         if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
2424                 if (irsp->ulpBdeCount != 0) {
2425                         saveq->context2 = lpfc_sli_get_buff(phba, pring,
2426                                                 irsp->un.ulpWord[3]);
2427                         if (!saveq->context2)
2428                                 lpfc_printf_log(phba,
2429                                         KERN_ERR,
2430                                         LOG_SLI,
2431                                         "0341 Ring %d Cannot find buffer for "
2432                                         "an unsolicited iocb. tag 0x%x\n",
2433                                         pring->ringno,
2434                                         irsp->un.ulpWord[3]);
2435                 }
2436                 if (irsp->ulpBdeCount == 2) {
2437                         saveq->context3 = lpfc_sli_get_buff(phba, pring,
2438                                                 irsp->unsli3.sli3Words[7]);
2439                         if (!saveq->context3)
2440                                 lpfc_printf_log(phba,
2441                                         KERN_ERR,
2442                                         LOG_SLI,
2443                                         "0342 Ring %d Cannot find buffer for an"
2444                                         " unsolicited iocb. tag 0x%x\n",
2445                                         pring->ringno,
2446                                         irsp->unsli3.sli3Words[7]);
2447                 }
2448                 list_for_each_entry(iocbq, &saveq->list, list) {
2449                         irsp = &(iocbq->iocb);
2450                         if (irsp->ulpBdeCount != 0) {
2451                                 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
2452                                                         irsp->un.ulpWord[3]);
2453                                 if (!iocbq->context2)
2454                                         lpfc_printf_log(phba,
2455                                                 KERN_ERR,
2456                                                 LOG_SLI,
2457                                                 "0343 Ring %d Cannot find "
2458                                                 "buffer for an unsolicited iocb"
2459                                                 ". tag 0x%x\n", pring->ringno,
2460                                                 irsp->un.ulpWord[3]);
2461                         }
2462                         if (irsp->ulpBdeCount == 2) {
2463                                 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
2464                                                 irsp->unsli3.sli3Words[7]);
2465                                 if (!iocbq->context3)
2466                                         lpfc_printf_log(phba,
2467                                                 KERN_ERR,
2468                                                 LOG_SLI,
2469                                                 "0344 Ring %d Cannot find "
2470                                                 "buffer for an unsolicited "
2471                                                 "iocb. tag 0x%x\n",
2472                                                 pring->ringno,
2473                                                 irsp->unsli3.sli3Words[7]);
2474                         }
2475                 }
2476         }
2477         if (irsp->ulpBdeCount != 0 &&
2478             (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
2479              irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
2480                 int found = 0;
2481
2482                 /* search continue save q for same XRI */
2483                 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
2484                         if (iocbq->iocb.ulpContext == saveq->iocb.ulpContext) {
2485                                 list_add_tail(&saveq->list, &iocbq->list);
2486                                 found = 1;
2487                                 break;
2488                         }
2489                 }
2490                 if (!found)
2491                         list_add_tail(&saveq->clist,
2492                                       &pring->iocb_continue_saveq);
2493                 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
2494                         list_del_init(&iocbq->clist);
2495                         saveq = iocbq;
2496                         irsp = &(saveq->iocb);
2497                 } else
2498                         return 0;
2499         }
2500         if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
2501             (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
2502             (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
2503                 Rctl = FC_RCTL_ELS_REQ;
2504                 Type = FC_TYPE_ELS;
2505         } else {
2506                 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
2507                 Rctl = w5p->hcsw.Rctl;
2508                 Type = w5p->hcsw.Type;
2509
2510                 /* Firmware Workaround */
2511                 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
2512                         (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
2513                          irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
2514                         Rctl = FC_RCTL_ELS_REQ;
2515                         Type = FC_TYPE_ELS;
2516                         w5p->hcsw.Rctl = Rctl;
2517                         w5p->hcsw.Type = Type;
2518                 }
2519         }
2520
2521         if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
2522                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
2523                                 "0313 Ring %d handler: unexpected Rctl x%x "
2524                                 "Type x%x received\n",
2525                                 pring->ringno, Rctl, Type);
2526
2527         return 1;
2528 }
2529
2530 /**
2531  * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
2532  * @phba: Pointer to HBA context object.
2533  * @pring: Pointer to driver SLI ring object.
2534  * @prspiocb: Pointer to response iocb object.
2535  *
2536  * This function looks up the iocb_lookup table to get the command iocb
2537  * corresponding to the given response iocb using the iotag of the
2538  * response iocb. This function is called with the hbalock held.
2539  * This function returns the command iocb object if it finds the command
2540  * iocb else returns NULL.
2541  **/
2542 static struct lpfc_iocbq *
2543 lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
2544                       struct lpfc_sli_ring *pring,
2545                       struct lpfc_iocbq *prspiocb)
2546 {
2547         struct lpfc_iocbq *cmd_iocb = NULL;
2548         uint16_t iotag;
2549
2550         iotag = prspiocb->iocb.ulpIoTag;
2551
2552         if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2553                 cmd_iocb = phba->sli.iocbq_lookup[iotag];
2554                 list_del_init(&cmd_iocb->list);
2555                 if (cmd_iocb->iocb_flag & LPFC_IO_ON_Q) {
2556                         pring->txcmplq_cnt--;
2557                         cmd_iocb->iocb_flag &= ~LPFC_IO_ON_Q;
2558                 }
2559                 return cmd_iocb;
2560         }
2561
2562         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2563                         "0317 iotag x%x is out off "
2564                         "range: max iotag x%x wd0 x%x\n",
2565                         iotag, phba->sli.last_iotag,
2566                         *(((uint32_t *) &prspiocb->iocb) + 7));
2567         return NULL;
2568 }
2569
2570 /**
2571  * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
2572  * @phba: Pointer to HBA context object.
2573  * @pring: Pointer to driver SLI ring object.
2574  * @iotag: IOCB tag.
2575  *
2576  * This function looks up the iocb_lookup table to get the command iocb
2577  * corresponding to the given iotag. This function is called with the
2578  * hbalock held.
2579  * This function returns the command iocb object if it finds the command
2580  * iocb else returns NULL.
2581  **/
2582 static struct lpfc_iocbq *
2583 lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
2584                              struct lpfc_sli_ring *pring, uint16_t iotag)
2585 {
2586         struct lpfc_iocbq *cmd_iocb;
2587
2588         if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2589                 cmd_iocb = phba->sli.iocbq_lookup[iotag];
2590                 list_del_init(&cmd_iocb->list);
2591                 if (cmd_iocb->iocb_flag & LPFC_IO_ON_Q) {
2592                         cmd_iocb->iocb_flag &= ~LPFC_IO_ON_Q;
2593                         pring->txcmplq_cnt--;
2594                 }
2595                 return cmd_iocb;
2596         }
2597
2598         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2599                         "0372 iotag x%x is out off range: max iotag (x%x)\n",
2600                         iotag, phba->sli.last_iotag);
2601         return NULL;
2602 }
2603
2604 /**
2605  * lpfc_sli_process_sol_iocb - process solicited iocb completion
2606  * @phba: Pointer to HBA context object.
2607  * @pring: Pointer to driver SLI ring object.
2608  * @saveq: Pointer to the response iocb to be processed.
2609  *
2610  * This function is called by the ring event handler for non-fcp
2611  * rings when there is a new response iocb in the response ring.
2612  * The caller is not required to hold any locks. This function
2613  * gets the command iocb associated with the response iocb and
2614  * calls the completion handler for the command iocb. If there
2615  * is no completion handler, the function will free the resources
2616  * associated with command iocb. If the response iocb is for
2617  * an already aborted command iocb, the status of the completion
2618  * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
2619  * This function always returns 1.
2620  **/
2621 static int
2622 lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2623                           struct lpfc_iocbq *saveq)
2624 {
2625         struct lpfc_iocbq *cmdiocbp;
2626         int rc = 1;
2627         unsigned long iflag;
2628
2629         /* Based on the iotag field, get the cmd IOCB from the txcmplq */
2630         spin_lock_irqsave(&phba->hbalock, iflag);
2631         cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
2632         spin_unlock_irqrestore(&phba->hbalock, iflag);
2633
2634         if (cmdiocbp) {
2635                 if (cmdiocbp->iocb_cmpl) {
2636                         /*
2637                          * If an ELS command failed send an event to mgmt
2638                          * application.
2639                          */
2640                         if (saveq->iocb.ulpStatus &&
2641                              (pring->ringno == LPFC_ELS_RING) &&
2642                              (cmdiocbp->iocb.ulpCommand ==
2643                                 CMD_ELS_REQUEST64_CR))
2644                                 lpfc_send_els_failure_event(phba,
2645                                         cmdiocbp, saveq);
2646
2647                         /*
2648                          * Post all ELS completions to the worker thread.
2649                          * All other are passed to the completion callback.
2650                          */
2651                         if (pring->ringno == LPFC_ELS_RING) {
2652                                 if ((phba->sli_rev < LPFC_SLI_REV4) &&
2653                                     (cmdiocbp->iocb_flag &
2654                                                         LPFC_DRIVER_ABORTED)) {
2655                                         spin_lock_irqsave(&phba->hbalock,
2656                                                           iflag);
2657                                         cmdiocbp->iocb_flag &=
2658                                                 ~LPFC_DRIVER_ABORTED;
2659                                         spin_unlock_irqrestore(&phba->hbalock,
2660                                                                iflag);
2661                                         saveq->iocb.ulpStatus =
2662                                                 IOSTAT_LOCAL_REJECT;
2663                                         saveq->iocb.un.ulpWord[4] =
2664                                                 IOERR_SLI_ABORTED;
2665
2666                                         /* Firmware could still be in progress
2667                                          * of DMAing payload, so don't free data
2668                                          * buffer till after a hbeat.
2669                                          */
2670                                         spin_lock_irqsave(&phba->hbalock,
2671                                                           iflag);
2672                                         saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
2673                                         spin_unlock_irqrestore(&phba->hbalock,
2674                                                                iflag);
2675                                 }
2676                                 if (phba->sli_rev == LPFC_SLI_REV4) {
2677                                         if (saveq->iocb_flag &
2678                                             LPFC_EXCHANGE_BUSY) {
2679                                                 /* Set cmdiocb flag for the
2680                                                  * exchange busy so sgl (xri)
2681                                                  * will not be released until
2682                                                  * the abort xri is received
2683                                                  * from hba.
2684                                                  */
2685                                                 spin_lock_irqsave(
2686                                                         &phba->hbalock, iflag);
2687                                                 cmdiocbp->iocb_flag |=
2688                                                         LPFC_EXCHANGE_BUSY;
2689                                                 spin_unlock_irqrestore(
2690                                                         &phba->hbalock, iflag);
2691                                         }
2692                                         if (cmdiocbp->iocb_flag &
2693                                             LPFC_DRIVER_ABORTED) {
2694                                                 /*
2695                                                  * Clear LPFC_DRIVER_ABORTED
2696                                                  * bit in case it was driver
2697                                                  * initiated abort.
2698                                                  */
2699                                                 spin_lock_irqsave(
2700                                                         &phba->hbalock, iflag);
2701                                                 cmdiocbp->iocb_flag &=
2702                                                         ~LPFC_DRIVER_ABORTED;
2703                                                 spin_unlock_irqrestore(
2704                                                         &phba->hbalock, iflag);
2705                                                 cmdiocbp->iocb.ulpStatus =
2706                                                         IOSTAT_LOCAL_REJECT;
2707                                                 cmdiocbp->iocb.un.ulpWord[4] =
2708                                                         IOERR_ABORT_REQUESTED;
2709                                                 /*
2710                                                  * For SLI4, irsiocb contains
2711                                                  * NO_XRI in sli_xritag, it
2712                                                  * shall not affect releasing
2713                                                  * sgl (xri) process.
2714                                                  */
2715                                                 saveq->iocb.ulpStatus =
2716                                                         IOSTAT_LOCAL_REJECT;
2717                                                 saveq->iocb.un.ulpWord[4] =
2718                                                         IOERR_SLI_ABORTED;
2719                                                 spin_lock_irqsave(
2720                                                         &phba->hbalock, iflag);
2721                                                 saveq->iocb_flag |=
2722                                                         LPFC_DELAY_MEM_FREE;
2723                                                 spin_unlock_irqrestore(
2724                                                         &phba->hbalock, iflag);
2725                                         }
2726                                 }
2727                         }
2728                         (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
2729                 } else
2730                         lpfc_sli_release_iocbq(phba, cmdiocbp);
2731         } else {
2732                 /*
2733                  * Unknown initiating command based on the response iotag.
2734                  * This could be the case on the ELS ring because of
2735                  * lpfc_els_abort().
2736                  */
2737                 if (pring->ringno != LPFC_ELS_RING) {
2738                         /*
2739                          * Ring <ringno> handler: unexpected completion IoTag
2740                          * <IoTag>
2741                          */
2742                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
2743                                          "0322 Ring %d handler: "
2744                                          "unexpected completion IoTag x%x "
2745                                          "Data: x%x x%x x%x x%x\n",
2746                                          pring->ringno,
2747                                          saveq->iocb.ulpIoTag,
2748                                          saveq->iocb.ulpStatus,
2749                                          saveq->iocb.un.ulpWord[4],
2750                                          saveq->iocb.ulpCommand,
2751                                          saveq->iocb.ulpContext);
2752                 }
2753         }
2754
2755         return rc;
2756 }
2757
2758 /**
2759  * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
2760  * @phba: Pointer to HBA context object.
2761  * @pring: Pointer to driver SLI ring object.
2762  *
2763  * This function is called from the iocb ring event handlers when
2764  * put pointer is ahead of the get pointer for a ring. This function signal
2765  * an error attention condition to the worker thread and the worker
2766  * thread will transition the HBA to offline state.
2767  **/
2768 static void
2769 lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
2770 {
2771         struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
2772         /*
2773          * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
2774          * rsp ring <portRspMax>
2775          */
2776         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2777                         "0312 Ring %d handler: portRspPut %d "
2778                         "is bigger than rsp ring %d\n",
2779                         pring->ringno, le32_to_cpu(pgp->rspPutInx),
2780                         pring->numRiocb);
2781
2782         phba->link_state = LPFC_HBA_ERROR;
2783
2784         /*
2785          * All error attention handlers are posted to
2786          * worker thread
2787          */
2788         phba->work_ha |= HA_ERATT;
2789         phba->work_hs = HS_FFER3;
2790
2791         lpfc_worker_wake_up(phba);
2792
2793         return;
2794 }
2795
2796 /**
2797  * lpfc_poll_eratt - Error attention polling timer timeout handler
2798  * @ptr: Pointer to address of HBA context object.
2799  *
2800  * This function is invoked by the Error Attention polling timer when the
2801  * timer times out. It will check the SLI Error Attention register for
2802  * possible attention events. If so, it will post an Error Attention event
2803  * and wake up worker thread to process it. Otherwise, it will set up the
2804  * Error Attention polling timer for the next poll.
2805  **/
2806 void lpfc_poll_eratt(unsigned long ptr)
2807 {
2808         struct lpfc_hba *phba;
2809         uint32_t eratt = 0;
2810
2811         phba = (struct lpfc_hba *)ptr;
2812
2813         /* Check chip HA register for error event */
2814         eratt = lpfc_sli_check_eratt(phba);
2815
2816         if (eratt)
2817                 /* Tell the worker thread there is work to do */
2818                 lpfc_worker_wake_up(phba);
2819         else
2820                 /* Restart the timer for next eratt poll */
2821                 mod_timer(&phba->eratt_poll, jiffies +
2822                                         HZ * LPFC_ERATT_POLL_INTERVAL);
2823         return;
2824 }
2825
2826
2827 /**
2828  * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
2829  * @phba: Pointer to HBA context object.
2830  * @pring: Pointer to driver SLI ring object.
2831  * @mask: Host attention register mask for this ring.
2832  *
2833  * This function is called from the interrupt context when there is a ring
2834  * event for the fcp ring. The caller does not hold any lock.
2835  * The function processes each response iocb in the response ring until it
2836  * finds an iocb with LE bit set and chains all the iocbs upto the iocb with
2837  * LE bit set. The function will call the completion handler of the command iocb
2838  * if the response iocb indicates a completion for a command iocb or it is
2839  * an abort completion. The function will call lpfc_sli_process_unsol_iocb
2840  * function if this is an unsolicited iocb.
2841  * This routine presumes LPFC_FCP_RING handling and doesn't bother
2842  * to check it explicitly.
2843  */
2844 int
2845 lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
2846                                 struct lpfc_sli_ring *pring, uint32_t mask)
2847 {
2848         struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
2849         IOCB_t *irsp = NULL;
2850         IOCB_t *entry = NULL;
2851         struct lpfc_iocbq *cmdiocbq = NULL;
2852         struct lpfc_iocbq rspiocbq;
2853         uint32_t status;
2854         uint32_t portRspPut, portRspMax;
2855         int rc = 1;
2856         lpfc_iocb_type type;
2857         unsigned long iflag;
2858         uint32_t rsp_cmpl = 0;
2859
2860         spin_lock_irqsave(&phba->hbalock, iflag);
2861         pring->stats.iocb_event++;
2862
2863         /*
2864          * The next available response entry should never exceed the maximum
2865          * entries.  If it does, treat it as an adapter hardware error.
2866          */
2867         portRspMax = pring->numRiocb;
2868         portRspPut = le32_to_cpu(pgp->rspPutInx);
2869         if (unlikely(portRspPut >= portRspMax)) {
2870                 lpfc_sli_rsp_pointers_error(phba, pring);
2871                 spin_unlock_irqrestore(&phba->hbalock, iflag);
2872                 return 1;
2873         }
2874         if (phba->fcp_ring_in_use) {
2875                 spin_unlock_irqrestore(&phba->hbalock, iflag);
2876                 return 1;
2877         } else
2878                 phba->fcp_ring_in_use = 1;
2879
2880         rmb();
2881         while (pring->rspidx != portRspPut) {
2882                 /*
2883                  * Fetch an entry off the ring and copy it into a local data
2884                  * structure.  The copy involves a byte-swap since the
2885                  * network byte order and pci byte orders are different.
2886                  */
2887                 entry = lpfc_resp_iocb(phba, pring);
2888                 phba->last_completion_time = jiffies;
2889
2890                 if (++pring->rspidx >= portRspMax)
2891                         pring->rspidx = 0;
2892
2893                 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
2894                                       (uint32_t *) &rspiocbq.iocb,
2895                                       phba->iocb_rsp_size);
2896                 INIT_LIST_HEAD(&(rspiocbq.list));
2897                 irsp = &rspiocbq.iocb;
2898
2899                 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
2900                 pring->stats.iocb_rsp++;
2901                 rsp_cmpl++;
2902
2903                 if (unlikely(irsp->ulpStatus)) {
2904                         /*
2905                          * If resource errors reported from HBA, reduce
2906                          * queuedepths of the SCSI device.
2907                          */
2908                         if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
2909                                 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
2910                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
2911                                 phba->lpfc_rampdown_queue_depth(phba);
2912                                 spin_lock_irqsave(&phba->hbalock, iflag);
2913                         }
2914
2915                         /* Rsp ring <ringno> error: IOCB */
2916                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
2917                                         "0336 Rsp Ring %d error: IOCB Data: "
2918                                         "x%x x%x x%x x%x x%x x%x x%x x%x\n",
2919                                         pring->ringno,
2920                                         irsp->un.ulpWord[0],
2921                                         irsp->un.ulpWord[1],
2922                                         irsp->un.ulpWord[2],
2923                                         irsp->un.ulpWord[3],
2924                                         irsp->un.ulpWord[4],
2925                                         irsp->un.ulpWord[5],
2926                                         *(uint32_t *)&irsp->un1,
2927                                         *((uint32_t *)&irsp->un1 + 1));
2928                 }
2929
2930                 switch (type) {
2931                 case LPFC_ABORT_IOCB:
2932                 case LPFC_SOL_IOCB:
2933                         /*
2934                          * Idle exchange closed via ABTS from port.  No iocb
2935                          * resources need to be recovered.
2936                          */
2937                         if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
2938                                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
2939                                                 "0333 IOCB cmd 0x%x"
2940                                                 " processed. Skipping"
2941                                                 " completion\n",
2942                                                 irsp->ulpCommand);
2943                                 break;
2944                         }
2945
2946                         cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
2947                                                          &rspiocbq);
2948                         if (unlikely(!cmdiocbq))
2949                                 break;
2950                         if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
2951                                 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
2952                         if (cmdiocbq->iocb_cmpl) {
2953                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
2954                                 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
2955                                                       &rspiocbq);
2956                                 spin_lock_irqsave(&phba->hbalock, iflag);
2957                         }
2958                         break;
2959                 case LPFC_UNSOL_IOCB:
2960                         spin_unlock_irqrestore(&phba->hbalock, iflag);
2961                         lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
2962                         spin_lock_irqsave(&phba->hbalock, iflag);
2963                         break;
2964                 default:
2965                         if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
2966                                 char adaptermsg[LPFC_MAX_ADPTMSG];
2967                                 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
2968                                 memcpy(&adaptermsg[0], (uint8_t *) irsp,
2969                                        MAX_MSG_DATA);
2970                                 dev_warn(&((phba->pcidev)->dev),
2971                                          "lpfc%d: %s\n",
2972                                          phba->brd_no, adaptermsg);
2973                         } else {
2974                                 /* Unknown IOCB command */
2975                                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2976                                                 "0334 Unknown IOCB command "
2977                                                 "Data: x%x, x%x x%x x%x x%x\n",
2978                                                 type, irsp->ulpCommand,
2979                                                 irsp->ulpStatus,
2980                                                 irsp->ulpIoTag,
2981                                                 irsp->ulpContext);
2982                         }
2983                         break;
2984                 }
2985
2986                 /*
2987                  * The response IOCB has been processed.  Update the ring
2988                  * pointer in SLIM.  If the port response put pointer has not
2989                  * been updated, sync the pgp->rspPutInx and fetch the new port
2990                  * response put pointer.
2991                  */
2992                 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
2993
2994                 if (pring->rspidx == portRspPut)
2995                         portRspPut = le32_to_cpu(pgp->rspPutInx);
2996         }
2997
2998         if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
2999                 pring->stats.iocb_rsp_full++;
3000                 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3001                 writel(status, phba->CAregaddr);
3002                 readl(phba->CAregaddr);
3003         }
3004         if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3005                 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3006                 pring->stats.iocb_cmd_empty++;
3007
3008                 /* Force update of the local copy of cmdGetInx */
3009                 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
3010                 lpfc_sli_resume_iocb(phba, pring);
3011
3012                 if ((pring->lpfc_sli_cmd_available))
3013                         (pring->lpfc_sli_cmd_available) (phba, pring);
3014
3015         }
3016
3017         phba->fcp_ring_in_use = 0;
3018         spin_unlock_irqrestore(&phba->hbalock, iflag);
3019         return rc;
3020 }
3021
3022 /**
3023  * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
3024  * @phba: Pointer to HBA context object.
3025  * @pring: Pointer to driver SLI ring object.
3026  * @rspiocbp: Pointer to driver response IOCB object.
3027  *
3028  * This function is called from the worker thread when there is a slow-path
3029  * response IOCB to process. This function chains all the response iocbs until
3030  * seeing the iocb with the LE bit set. The function will call
3031  * lpfc_sli_process_sol_iocb function if the response iocb indicates a
3032  * completion of a command iocb. The function will call the
3033  * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
3034  * The function frees the resources or calls the completion handler if this
3035  * iocb is an abort completion. The function returns NULL when the response
3036  * iocb has the LE bit set and all the chained iocbs are processed, otherwise
3037  * this function shall chain the iocb on to the iocb_continueq and return the
3038  * response iocb passed in.
3039  **/
3040 static struct lpfc_iocbq *
3041 lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3042                         struct lpfc_iocbq *rspiocbp)
3043 {
3044         struct lpfc_iocbq *saveq;
3045         struct lpfc_iocbq *cmdiocbp;
3046         struct lpfc_iocbq *next_iocb;
3047         IOCB_t *irsp = NULL;
3048         uint32_t free_saveq;
3049         uint8_t iocb_cmd_type;
3050         lpfc_iocb_type type;
3051         unsigned long iflag;
3052         int rc;
3053
3054         spin_lock_irqsave(&phba->hbalock, iflag);
3055         /* First add the response iocb to the countinueq list */
3056         list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
3057         pring->iocb_continueq_cnt++;
3058
3059         /* Now, determine whetehr the list is completed for processing */
3060         irsp = &rspiocbp->iocb;
3061         if (irsp->ulpLe) {
3062                 /*
3063                  * By default, the driver expects to free all resources
3064                  * associated with this iocb completion.
3065                  */
3066                 free_saveq = 1;
3067                 saveq = list_get_first(&pring->iocb_continueq,
3068                                        struct lpfc_iocbq, list);
3069                 irsp = &(saveq->iocb);
3070                 list_del_init(&pring->iocb_continueq);
3071                 pring->iocb_continueq_cnt = 0;
3072
3073                 pring->stats.iocb_rsp++;
3074
3075                 /*
3076                  * If resource errors reported from HBA, reduce
3077                  * queuedepths of the SCSI device.
3078                  */
3079                 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
3080                     (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
3081                         spin_unlock_irqrestore(&phba->hbalock, iflag);
3082                         phba->lpfc_rampdown_queue_depth(phba);
3083                         spin_lock_irqsave(&phba->hbalock, iflag);
3084                 }
3085
3086                 if (irsp->ulpStatus) {
3087                         /* Rsp ring <ringno> error: IOCB */
3088                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3089                                         "0328 Rsp Ring %d error: "
3090                                         "IOCB Data: "
3091                                         "x%x x%x x%x x%x "
3092                                         "x%x x%x x%x x%x "
3093                                         "x%x x%x x%x x%x "
3094                                         "x%x x%x x%x x%x\n",
3095                                         pring->ringno,
3096                                         irsp->un.ulpWord[0],
3097                                         irsp->un.ulpWord[1],
3098                                         irsp->un.ulpWord[2],
3099                                         irsp->un.ulpWord[3],
3100                                         irsp->un.ulpWord[4],
3101                                         irsp->un.ulpWord[5],
3102                                         *(((uint32_t *) irsp) + 6),
3103                                         *(((uint32_t *) irsp) + 7),
3104                                         *(((uint32_t *) irsp) + 8),
3105                                         *(((uint32_t *) irsp) + 9),
3106                                         *(((uint32_t *) irsp) + 10),
3107                                         *(((uint32_t *) irsp) + 11),
3108                                         *(((uint32_t *) irsp) + 12),
3109                                         *(((uint32_t *) irsp) + 13),
3110                                         *(((uint32_t *) irsp) + 14),
3111                                         *(((uint32_t *) irsp) + 15));
3112                 }
3113
3114                 /*
3115                  * Fetch the IOCB command type and call the correct completion
3116                  * routine. Solicited and Unsolicited IOCBs on the ELS ring
3117                  * get freed back to the lpfc_iocb_list by the discovery
3118                  * kernel thread.
3119                  */
3120                 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
3121                 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
3122                 switch (type) {
3123                 case LPFC_SOL_IOCB:
3124                         spin_unlock_irqrestore(&phba->hbalock, iflag);
3125                         rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
3126                         spin_lock_irqsave(&phba->hbalock, iflag);
3127                         break;
3128
3129                 case LPFC_UNSOL_IOCB:
3130                         spin_unlock_irqrestore(&phba->hbalock, iflag);
3131                         rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
3132                         spin_lock_irqsave(&phba->hbalock, iflag);
3133                         if (!rc)
3134                                 free_saveq = 0;
3135                         break;
3136
3137                 case LPFC_ABORT_IOCB:
3138                         cmdiocbp = NULL;
3139                         if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
3140                                 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
3141                                                                  saveq);
3142                         if (cmdiocbp) {
3143                                 /* Call the specified completion routine */
3144                                 if (cmdiocbp->iocb_cmpl) {
3145                                         spin_unlock_irqrestore(&phba->hbalock,
3146                                                                iflag);
3147                                         (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
3148                                                               saveq);
3149                                         spin_lock_irqsave(&phba->hbalock,
3150                                                           iflag);
3151                                 } else
3152                                         __lpfc_sli_release_iocbq(phba,
3153                                                                  cmdiocbp);
3154                         }
3155                         break;
3156
3157                 case LPFC_UNKNOWN_IOCB:
3158                         if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3159                                 char adaptermsg[LPFC_MAX_ADPTMSG];
3160                                 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3161                                 memcpy(&adaptermsg[0], (uint8_t *)irsp,
3162                                        MAX_MSG_DATA);
3163                                 dev_warn(&((phba->pcidev)->dev),
3164                                          "lpfc%d: %s\n",
3165                                          phba->brd_no, adaptermsg);
3166                         } else {
3167                                 /* Unknown IOCB command */
3168                                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3169                                                 "0335 Unknown IOCB "
3170                                                 "command Data: x%x "
3171                                                 "x%x x%x x%x\n",
3172                                                 irsp->ulpCommand,
3173                                                 irsp->ulpStatus,
3174                                                 irsp->ulpIoTag,
3175                                                 irsp->ulpContext);
3176                         }
3177                         break;
3178                 }
3179
3180                 if (free_saveq) {
3181                         list_for_each_entry_safe(rspiocbp, next_iocb,
3182                                                  &saveq->list, list) {
3183                                 list_del(&rspiocbp->list);
3184                                 __lpfc_sli_release_iocbq(phba, rspiocbp);
3185                         }
3186                         __lpfc_sli_release_iocbq(phba, saveq);
3187                 }
3188                 rspiocbp = NULL;
3189         }
3190         spin_unlock_irqrestore(&phba->hbalock, iflag);
3191         return rspiocbp;
3192 }
3193
3194 /**
3195  * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
3196  * @phba: Pointer to HBA context object.
3197  * @pring: Pointer to driver SLI ring object.
3198  * @mask: Host attention register mask for this ring.
3199  *
3200  * This routine wraps the actual slow_ring event process routine from the
3201  * API jump table function pointer from the lpfc_hba struct.
3202  **/
3203 void
3204 lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
3205                                 struct lpfc_sli_ring *pring, uint32_t mask)
3206 {
3207         phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
3208 }
3209
3210 /**
3211  * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
3212  * @phba: Pointer to HBA context object.
3213  * @pring: Pointer to driver SLI ring object.
3214  * @mask: Host attention register mask for this ring.
3215  *
3216  * This function is called from the worker thread when there is a ring event
3217  * for non-fcp rings. The caller does not hold any lock. The function will
3218  * remove each response iocb in the response ring and calls the handle
3219  * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3220  **/
3221 static void
3222 lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
3223                                    struct lpfc_sli_ring *pring, uint32_t mask)
3224 {
3225         struct lpfc_pgp *pgp;
3226         IOCB_t *entry;
3227         IOCB_t *irsp = NULL;
3228         struct lpfc_iocbq *rspiocbp = NULL;
3229         uint32_t portRspPut, portRspMax;
3230         unsigned long iflag;
3231         uint32_t status;
3232
3233         pgp = &phba->port_gp[pring->ringno];
3234         spin_lock_irqsave(&phba->hbalock, iflag);
3235         pring->stats.iocb_event++;
3236
3237         /*
3238          * The next available response entry should never exceed the maximum
3239          * entries.  If it does, treat it as an adapter hardware error.
3240          */
3241         portRspMax = pring->numRiocb;
3242         portRspPut = le32_to_cpu(pgp->rspPutInx);
3243         if (portRspPut >= portRspMax) {
3244                 /*
3245                  * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
3246                  * rsp ring <portRspMax>
3247                  */
3248                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3249                                 "0303 Ring %d handler: portRspPut %d "
3250                                 "is bigger than rsp ring %d\n",
3251                                 pring->ringno, portRspPut, portRspMax);
3252
3253                 phba->link_state = LPFC_HBA_ERROR;
3254                 spin_unlock_irqrestore(&phba->hbalock, iflag);
3255
3256                 phba->work_hs = HS_FFER3;
3257                 lpfc_handle_eratt(phba);
3258
3259                 return;
3260         }
3261
3262         rmb();
3263         while (pring->rspidx != portRspPut) {
3264                 /*
3265                  * Build a completion list and call the appropriate handler.
3266                  * The process is to get the next available response iocb, get
3267                  * a free iocb from the list, copy the response data into the
3268                  * free iocb, insert to the continuation list, and update the
3269                  * next response index to slim.  This process makes response
3270                  * iocb's in the ring available to DMA as fast as possible but
3271                  * pays a penalty for a copy operation.  Since the iocb is
3272                  * only 32 bytes, this penalty is considered small relative to
3273                  * the PCI reads for register values and a slim write.  When
3274                  * the ulpLe field is set, the entire Command has been
3275                  * received.
3276                  */
3277                 entry = lpfc_resp_iocb(phba, pring);
3278
3279                 phba->last_completion_time = jiffies;
3280                 rspiocbp = __lpfc_sli_get_iocbq(phba);
3281                 if (rspiocbp == NULL) {
3282                         printk(KERN_ERR "%s: out of buffers! Failing "
3283                                "completion.\n", __func__);
3284                         break;
3285                 }
3286
3287                 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
3288                                       phba->iocb_rsp_size);
3289                 irsp = &rspiocbp->iocb;
3290
3291                 if (++pring->rspidx >= portRspMax)
3292                         pring->rspidx = 0;
3293
3294                 if (pring->ringno == LPFC_ELS_RING) {
3295                         lpfc_debugfs_slow_ring_trc(phba,
3296                         "IOCB rsp ring:   wd4:x%08x wd6:x%08x wd7:x%08x",
3297                                 *(((uint32_t *) irsp) + 4),
3298                                 *(((uint32_t *) irsp) + 6),
3299                                 *(((uint32_t *) irsp) + 7));
3300                 }
3301
3302                 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
3303
3304                 spin_unlock_irqrestore(&phba->hbalock, iflag);
3305                 /* Handle the response IOCB */
3306                 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
3307                 spin_lock_irqsave(&phba->hbalock, iflag);
3308
3309                 /*
3310                  * If the port response put pointer has not been updated, sync
3311                  * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
3312                  * response put pointer.
3313                  */
3314                 if (pring->rspidx == portRspPut) {
3315                         portRspPut = le32_to_cpu(pgp->rspPutInx);
3316                 }
3317         } /* while (pring->rspidx != portRspPut) */
3318
3319         if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
3320                 /* At least one response entry has been freed */
3321                 pring->stats.iocb_rsp_full++;
3322                 /* SET RxRE_RSP in Chip Att register */
3323                 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3324                 writel(status, phba->CAregaddr);
3325                 readl(phba->CAregaddr); /* flush */
3326         }
3327         if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3328                 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3329                 pring->stats.iocb_cmd_empty++;
3330
3331                 /* Force update of the local copy of cmdGetInx */
3332                 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
3333                 lpfc_sli_resume_iocb(phba, pring);
3334
3335                 if ((pring->lpfc_sli_cmd_available))
3336                         (pring->lpfc_sli_cmd_available) (phba, pring);
3337
3338         }
3339
3340         spin_unlock_irqrestore(&phba->hbalock, iflag);
3341         return;
3342 }
3343
3344 /**
3345  * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
3346  * @phba: Pointer to HBA context object.
3347  * @pring: Pointer to driver SLI ring object.
3348  * @mask: Host attention register mask for this ring.
3349  *
3350  * This function is called from the worker thread when there is a pending
3351  * ELS response iocb on the driver internal slow-path response iocb worker
3352  * queue. The caller does not hold any lock. The function will remove each
3353  * response iocb from the response worker queue and calls the handle
3354  * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3355  **/
3356 static void
3357 lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
3358                                    struct lpfc_sli_ring *pring, uint32_t mask)
3359 {
3360         struct lpfc_iocbq *irspiocbq;
3361         struct hbq_dmabuf *dmabuf;
3362         struct lpfc_cq_event *cq_event;
3363         unsigned long iflag;
3364
3365         spin_lock_irqsave(&phba->hbalock, iflag);
3366         phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
3367         spin_unlock_irqrestore(&phba->hbalock, iflag);
3368         while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
3369                 /* Get the response iocb from the head of work queue */
3370                 spin_lock_irqsave(&phba->hbalock, iflag);
3371                 list_remove_head(&phba->sli4_hba.sp_queue_event,
3372                                  cq_event, struct lpfc_cq_event, list);
3373                 spin_unlock_irqrestore(&phba->hbalock, iflag);
3374
3375                 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
3376                 case CQE_CODE_COMPL_WQE:
3377                         irspiocbq = container_of(cq_event, struct lpfc_iocbq,
3378                                                  cq_event);
3379                         /* Translate ELS WCQE to response IOCBQ */
3380                         irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
3381                                                                    irspiocbq);
3382                         if (irspiocbq)
3383                                 lpfc_sli_sp_handle_rspiocb(phba, pring,
3384                                                            irspiocbq);
3385                         break;
3386                 case CQE_CODE_RECEIVE:
3387                         dmabuf = container_of(cq_event, struct hbq_dmabuf,
3388                                               cq_event);
3389                         lpfc_sli4_handle_received_buffer(phba, dmabuf);
3390                         break;
3391                 default:
3392                         break;
3393                 }
3394         }
3395 }
3396
3397 /**
3398  * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
3399  * @phba: Pointer to HBA context object.
3400  * @pring: Pointer to driver SLI ring object.
3401  *
3402  * This function aborts all iocbs in the given ring and frees all the iocb
3403  * objects in txq. This function issues an abort iocb for all the iocb commands
3404  * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3405  * the return of this function. The caller is not required to hold any locks.
3406  **/
3407 void
3408 lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3409 {
3410         LIST_HEAD(completions);
3411         struct lpfc_iocbq *iocb, *next_iocb;
3412
3413         if (pring->ringno == LPFC_ELS_RING) {
3414                 lpfc_fabric_abort_hba(phba);
3415         }
3416
3417         /* Error everything on txq and txcmplq
3418          * First do the txq.
3419          */
3420         spin_lock_irq(&phba->hbalock);
3421         list_splice_init(&pring->txq, &completions);
3422         pring->txq_cnt = 0;
3423
3424         /* Next issue ABTS for everything on the txcmplq */
3425         list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3426                 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3427
3428         spin_unlock_irq(&phba->hbalock);
3429
3430         /* Cancel all the IOCBs from the completions list */
3431         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3432                               IOERR_SLI_ABORTED);
3433 }
3434
3435 /**
3436  * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
3437  * @phba: Pointer to HBA context object.
3438  *
3439  * This function flushes all iocbs in the fcp ring and frees all the iocb
3440  * objects in txq and txcmplq. This function will not issue abort iocbs
3441  * for all the iocb commands in txcmplq, they will just be returned with
3442  * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3443  * slot has been permanently disabled.
3444  **/
3445 void
3446 lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
3447 {
3448         LIST_HEAD(txq);
3449         LIST_HEAD(txcmplq);
3450         struct lpfc_sli *psli = &phba->sli;
3451         struct lpfc_sli_ring  *pring;
3452
3453         /* Currently, only one fcp ring */
3454         pring = &psli->ring[psli->fcp_ring];
3455
3456         spin_lock_irq(&phba->hbalock);
3457         /* Retrieve everything on txq */
3458         list_splice_init(&pring->txq, &txq);
3459         pring->txq_cnt = 0;
3460
3461         /* Retrieve everything on the txcmplq */
3462         list_splice_init(&pring->txcmplq, &txcmplq);
3463         pring->txcmplq_cnt = 0;
3464         spin_unlock_irq(&phba->hbalock);
3465
3466         /* Flush the txq */
3467         lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
3468                               IOERR_SLI_DOWN);
3469
3470         /* Flush the txcmpq */
3471         lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
3472                               IOERR_SLI_DOWN);
3473 }
3474
3475 /**
3476  * lpfc_sli_brdready_s3 - Check for sli3 host ready status
3477  * @phba: Pointer to HBA context object.
3478  * @mask: Bit mask to be checked.
3479  *
3480  * This function reads the host status register and compares
3481  * with the provided bit mask to check if HBA completed
3482  * the restart. This function will wait in a loop for the
3483  * HBA to complete restart. If the HBA does not restart within
3484  * 15 iterations, the function will reset the HBA again. The
3485  * function returns 1 when HBA fail to restart otherwise returns
3486  * zero.
3487  **/
3488 static int
3489 lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
3490 {
3491         uint32_t status;
3492         int i = 0;
3493         int retval = 0;
3494
3495         /* Read the HBA Host Status Register */
3496         status = readl(phba->HSregaddr);
3497
3498         /*
3499          * Check status register every 100ms for 5 retries, then every
3500          * 500ms for 5, then every 2.5 sec for 5, then reset board and
3501          * every 2.5 sec for 4.
3502          * Break our of the loop if errors occurred during init.
3503          */
3504         while (((status & mask) != mask) &&
3505                !(status & HS_FFERM) &&
3506                i++ < 20) {
3507
3508                 if (i <= 5)
3509                         msleep(10);
3510                 else if (i <= 10)
3511                         msleep(500);
3512                 else
3513                         msleep(2500);
3514
3515                 if (i == 15) {
3516                                 /* Do post */
3517                         phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3518                         lpfc_sli_brdrestart(phba);
3519                 }
3520                 /* Read the HBA Host Status Register */
3521                 status = readl(phba->HSregaddr);
3522         }
3523
3524         /* Check to see if any errors occurred during init */
3525         if ((status & HS_FFERM) || (i >= 20)) {
3526                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3527                                 "2751 Adapter failed to restart, "
3528                                 "status reg x%x, FW Data: A8 x%x AC x%x\n",
3529                                 status,
3530                                 readl(phba->MBslimaddr + 0xa8),
3531                                 readl(phba->MBslimaddr + 0xac));
3532                 phba->link_state = LPFC_HBA_ERROR;
3533                 retval = 1;
3534         }
3535
3536         return retval;
3537 }
3538
3539 /**
3540  * lpfc_sli_brdready_s4 - Check for sli4 host ready status
3541  * @phba: Pointer to HBA context object.
3542  * @mask: Bit mask to be checked.
3543  *
3544  * This function checks the host status register to check if HBA is
3545  * ready. This function will wait in a loop for the HBA to be ready
3546  * If the HBA is not ready , the function will will reset the HBA PCI
3547  * function again. The function returns 1 when HBA fail to be ready
3548  * otherwise returns zero.
3549  **/
3550 static int
3551 lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
3552 {
3553         uint32_t status;
3554         int retval = 0;
3555
3556         /* Read the HBA Host Status Register */
3557         status = lpfc_sli4_post_status_check(phba);
3558
3559         if (status) {
3560                 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3561                 lpfc_sli_brdrestart(phba);
3562                 status = lpfc_sli4_post_status_check(phba);
3563         }
3564
3565         /* Check to see if any errors occurred during init */
3566         if (status) {
3567                 phba->link_state = LPFC_HBA_ERROR;
3568                 retval = 1;
3569         } else
3570                 phba->sli4_hba.intr_enable = 0;
3571
3572         return retval;
3573 }
3574
3575 /**
3576  * lpfc_sli_brdready - Wrapper func for checking the hba readyness
3577  * @phba: Pointer to HBA context object.
3578  * @mask: Bit mask to be checked.
3579  *
3580  * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
3581  * from the API jump table function pointer from the lpfc_hba struct.
3582  **/
3583 int
3584 lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
3585 {
3586         return phba->lpfc_sli_brdready(phba, mask);
3587 }
3588
3589 #define BARRIER_TEST_PATTERN (0xdeadbeef)
3590
3591 /**
3592  * lpfc_reset_barrier - Make HBA ready for HBA reset
3593  * @phba: Pointer to HBA context object.
3594  *
3595  * This function is called before resetting an HBA. This
3596  * function requests HBA to quiesce DMAs before a reset.
3597  **/
3598 void lpfc_reset_barrier(struct lpfc_hba *phba)
3599 {
3600         uint32_t __iomem *resp_buf;
3601         uint32_t __iomem *mbox_buf;
3602         volatile uint32_t mbox;
3603         uint32_t hc_copy;
3604         int  i;
3605         uint8_t hdrtype;
3606
3607         pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
3608         if (hdrtype != 0x80 ||
3609             (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
3610              FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
3611                 return;
3612
3613         /*
3614          * Tell the other part of the chip to suspend temporarily all
3615          * its DMA activity.
3616          */
3617         resp_buf = phba->MBslimaddr;
3618
3619         /* Disable the error attention */
3620         hc_copy = readl(phba->HCregaddr);
3621         writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
3622         readl(phba->HCregaddr); /* flush */
3623         phba->link_flag |= LS_IGNORE_ERATT;
3624
3625         if (readl(phba->HAregaddr) & HA_ERATT) {
3626                 /* Clear Chip error bit */
3627                 writel(HA_ERATT, phba->HAregaddr);
3628                 phba->pport->stopped = 1;
3629         }
3630
3631         mbox = 0;
3632         ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
3633         ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
3634
3635         writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
3636         mbox_buf = phba->MBslimaddr;
3637         writel(mbox, mbox_buf);
3638
3639         for (i = 0;
3640              readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN) && i < 50; i++)
3641                 mdelay(1);
3642
3643         if (readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN)) {
3644                 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
3645                     phba->pport->stopped)
3646                         goto restore_hc;
3647                 else
3648                         goto clear_errat;
3649         }
3650
3651         ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
3652         for (i = 0; readl(resp_buf) != mbox &&  i < 500; i++)
3653                 mdelay(1);
3654
3655 clear_errat:
3656
3657         while (!(readl(phba->HAregaddr) & HA_ERATT) && ++i < 500)
3658                 mdelay(1);
3659
3660         if (readl(phba->HAregaddr) & HA_ERATT) {
3661                 writel(HA_ERATT, phba->HAregaddr);
3662                 phba->pport->stopped = 1;
3663         }
3664
3665 restore_hc:
3666         phba->link_flag &= ~LS_IGNORE_ERATT;
3667         writel(hc_copy, phba->HCregaddr);
3668         readl(phba->HCregaddr); /* flush */
3669 }
3670
3671 /**
3672  * lpfc_sli_brdkill - Issue a kill_board mailbox command
3673  * @phba: Pointer to HBA context object.
3674  *
3675  * This function issues a kill_board mailbox command and waits for
3676  * the error attention interrupt. This function is called for stopping
3677  * the firmware processing. The caller is not required to hold any
3678  * locks. This function calls lpfc_hba_down_post function to free
3679  * any pending commands after the kill. The function will return 1 when it
3680  * fails to kill the board else will return 0.
3681  **/
3682 int
3683 lpfc_sli_brdkill(struct lpfc_hba *phba)
3684 {
3685         struct lpfc_sli *psli;
3686         LPFC_MBOXQ_t *pmb;
3687         uint32_t status;
3688         uint32_t ha_copy;
3689         int retval;
3690         int i = 0;
3691
3692         psli = &phba->sli;
3693
3694         /* Kill HBA */
3695         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3696                         "0329 Kill HBA Data: x%x x%x\n",
3697                         phba->pport->port_state, psli->sli_flag);
3698
3699         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3700         if (!pmb)
3701                 return 1;
3702
3703         /* Disable the error attention */
3704         spin_lock_irq(&phba->hbalock);
3705         status = readl(phba->HCregaddr);
3706         status &= ~HC_ERINT_ENA;
3707         writel(status, phba->HCregaddr);
3708         readl(phba->HCregaddr); /* flush */
3709         phba->link_flag |= LS_IGNORE_ERATT;
3710         spin_unlock_irq(&phba->hbalock);
3711
3712         lpfc_kill_board(phba, pmb);
3713         pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3714         retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3715
3716         if (retval != MBX_SUCCESS) {
3717                 if (retval != MBX_BUSY)
3718                         mempool_free(pmb, phba->mbox_mem_pool);
3719                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3720                                 "2752 KILL_BOARD command failed retval %d\n",
3721                                 retval);
3722                 spin_lock_irq(&phba->hbalock);
3723                 phba->link_flag &= ~LS_IGNORE_ERATT;
3724                 spin_unlock_irq(&phba->hbalock);
3725                 return 1;
3726         }
3727
3728         spin_lock_irq(&phba->hbalock);
3729         psli->sli_flag &= ~LPFC_SLI_ACTIVE;
3730         spin_unlock_irq(&phba->hbalock);
3731
3732         mempool_free(pmb, phba->mbox_mem_pool);
3733
3734         /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
3735          * attention every 100ms for 3 seconds. If we don't get ERATT after
3736          * 3 seconds we still set HBA_ERROR state because the status of the
3737          * board is now undefined.
3738          */
3739         ha_copy = readl(phba->HAregaddr);
3740
3741         while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
3742                 mdelay(100);
3743                 ha_copy = readl(phba->HAregaddr);
3744         }
3745
3746         del_timer_sync(&psli->mbox_tmo);
3747         if (ha_copy & HA_ERATT) {
3748                 writel(HA_ERATT, phba->HAregaddr);
3749                 phba->pport->stopped = 1;
3750         }
3751         spin_lock_irq(&phba->hbalock);
3752         psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
3753         psli->mbox_active = NULL;
3754         phba->link_flag &= ~LS_IGNORE_ERATT;
3755         spin_unlock_irq(&phba->hbalock);
3756
3757         lpfc_hba_down_post(phba);
3758         phba->link_state = LPFC_HBA_ERROR;
3759
3760         return ha_copy & HA_ERATT ? 0 : 1;
3761 }
3762
3763 /**
3764  * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
3765  * @phba: Pointer to HBA context object.
3766  *
3767  * This function resets the HBA by writing HC_INITFF to the control
3768  * register. After the HBA resets, this function resets all the iocb ring
3769  * indices. This function disables PCI layer parity checking during
3770  * the reset.
3771  * This function returns 0 always.
3772  * The caller is not required to hold any locks.
3773  **/
3774 int
3775 lpfc_sli_brdreset(struct lpfc_hba *phba)
3776 {
3777         struct lpfc_sli *psli;
3778         struct lpfc_sli_ring *pring;
3779         uint16_t cfg_value;
3780         int i;
3781
3782         psli = &phba->sli;
3783
3784         /* Reset HBA */
3785         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3786                         "0325 Reset HBA Data: x%x x%x\n",
3787                         phba->pport->port_state, psli->sli_flag);
3788
3789         /* perform board reset */
3790         phba->fc_eventTag = 0;
3791         phba->link_events = 0;
3792         phba->pport->fc_myDID = 0;
3793         phba->pport->fc_prevDID = 0;
3794
3795         /* Turn off parity checking and serr during the physical reset */
3796         pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3797         pci_write_config_word(phba->pcidev, PCI_COMMAND,
3798                               (cfg_value &
3799                                ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3800
3801         psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
3802
3803         /* Now toggle INITFF bit in the Host Control Register */
3804         writel(HC_INITFF, phba->HCregaddr);
3805         mdelay(1);
3806         readl(phba->HCregaddr); /* flush */
3807         writel(0, phba->HCregaddr);
3808         readl(phba->HCregaddr); /* flush */
3809
3810         /* Restore PCI cmd register */
3811         pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
3812
3813         /* Initialize relevant SLI info */
3814         for (i = 0; i < psli->num_rings; i++) {
3815                 pring = &psli->ring[i];
3816                 pring->flag = 0;
3817                 pring->rspidx = 0;
3818                 pring->next_cmdidx  = 0;
3819                 pring->local_getidx = 0;
3820                 pring->cmdidx = 0;
3821                 pring->missbufcnt = 0;
3822         }
3823
3824         phba->link_state = LPFC_WARM_START;
3825         return 0;
3826 }
3827
3828 /**
3829  * lpfc_sli4_brdreset - Reset a sli-4 HBA
3830  * @phba: Pointer to HBA context object.
3831  *
3832  * This function resets a SLI4 HBA. This function disables PCI layer parity
3833  * checking during resets the device. The caller is not required to hold
3834  * any locks.
3835  *
3836  * This function returns 0 always.
3837  **/
3838 int
3839 lpfc_sli4_brdreset(struct lpfc_hba *phba)
3840 {
3841         struct lpfc_sli *psli = &phba->sli;
3842         uint16_t cfg_value;
3843         uint8_t qindx;
3844
3845         /* Reset HBA */
3846         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3847                         "0295 Reset HBA Data: x%x x%x\n",
3848                         phba->pport->port_state, psli->sli_flag);
3849
3850         /* perform board reset */
3851         phba->fc_eventTag = 0;
3852         phba->link_events = 0;
3853         phba->pport->fc_myDID = 0;
3854         phba->pport->fc_prevDID = 0;
3855
3856         spin_lock_irq(&phba->hbalock);
3857         psli->sli_flag &= ~(LPFC_PROCESS_LA);
3858         phba->fcf.fcf_flag = 0;
3859         /* Clean up the child queue list for the CQs */
3860         list_del_init(&phba->sli4_hba.mbx_wq->list);
3861         list_del_init(&phba->sli4_hba.els_wq->list);
3862         list_del_init(&phba->sli4_hba.hdr_rq->list);
3863         list_del_init(&phba->sli4_hba.dat_rq->list);
3864         list_del_init(&phba->sli4_hba.mbx_cq->list);
3865         list_del_init(&phba->sli4_hba.els_cq->list);
3866         for (qindx = 0; qindx < phba->cfg_fcp_wq_count; qindx++)
3867                 list_del_init(&phba->sli4_hba.fcp_wq[qindx]->list);
3868         for (qindx = 0; qindx < phba->cfg_fcp_eq_count; qindx++)
3869                 list_del_init(&phba->sli4_hba.fcp_cq[qindx]->list);
3870         spin_unlock_irq(&phba->hbalock);
3871
3872         /* Now physically reset the device */
3873         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3874                         "0389 Performing PCI function reset!\n");
3875
3876         /* Turn off parity checking and serr during the physical reset */
3877         pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3878         pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
3879                               ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3880
3881         /* Perform FCoE PCI function reset */
3882         lpfc_pci_function_reset(phba);
3883
3884         /* Restore PCI cmd register */
3885         pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
3886
3887         return 0;
3888 }
3889
3890 /**
3891  * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
3892  * @phba: Pointer to HBA context object.
3893  *
3894  * This function is called in the SLI initialization code path to
3895  * restart the HBA. The caller is not required to hold any lock.
3896  * This function writes MBX_RESTART mailbox command to the SLIM and
3897  * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
3898  * function to free any pending commands. The function enables
3899  * POST only during the first initialization. The function returns zero.
3900  * The function does not guarantee completion of MBX_RESTART mailbox
3901  * command before the return of this function.
3902  **/
3903 static int
3904 lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
3905 {
3906         MAILBOX_t *mb;
3907         struct lpfc_sli *psli;
3908         volatile uint32_t word0;
3909         void __iomem *to_slim;
3910         uint32_t hba_aer_enabled;
3911
3912         spin_lock_irq(&phba->hbalock);
3913
3914         /* Take PCIe device Advanced Error Reporting (AER) state */
3915         hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
3916
3917         psli = &phba->sli;
3918
3919         /* Restart HBA */
3920         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3921                         "0337 Restart HBA Data: x%x x%x\n",
3922                         phba->pport->port_state, psli->sli_flag);
3923
3924         word0 = 0;
3925         mb = (MAILBOX_t *) &word0;
3926         mb->mbxCommand = MBX_RESTART;
3927         mb->mbxHc = 1;
3928
3929         lpfc_reset_barrier(phba);
3930
3931         to_slim = phba->MBslimaddr;
3932         writel(*(uint32_t *) mb, to_slim);
3933         readl(to_slim); /* flush */
3934
3935         /* Only skip post after fc_ffinit is completed */
3936         if (phba->pport->port_state)
3937                 word0 = 1;      /* This is really setting up word1 */
3938         else
3939                 word0 = 0;      /* This is really setting up word1 */
3940         to_slim = phba->MBslimaddr + sizeof (uint32_t);
3941         writel(*(uint32_t *) mb, to_slim);
3942         readl(to_slim); /* flush */
3943
3944         lpfc_sli_brdreset(phba);
3945         phba->pport->stopped = 0;
3946         phba->link_state = LPFC_INIT_START;
3947         phba->hba_flag = 0;
3948         spin_unlock_irq(&phba->hbalock);
3949
3950         memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
3951         psli->stats_start = get_seconds();
3952
3953         /* Give the INITFF and Post time to settle. */
3954         mdelay(100);
3955
3956         /* Reset HBA AER if it was enabled, note hba_flag was reset above */
3957         if (hba_aer_enabled)
3958                 pci_disable_pcie_error_reporting(phba->pcidev);
3959
3960         lpfc_hba_down_post(phba);
3961
3962         return 0;
3963 }
3964
3965 /**
3966  * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
3967  * @phba: Pointer to HBA context object.
3968  *
3969  * This function is called in the SLI initialization code path to restart
3970  * a SLI4 HBA. The caller is not required to hold any lock.
3971  * At the end of the function, it calls lpfc_hba_down_post function to
3972  * free any pending commands.
3973  **/
3974 static int
3975 lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
3976 {
3977         struct lpfc_sli *psli = &phba->sli;
3978         uint32_t hba_aer_enabled;
3979
3980         /* Restart HBA */
3981         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3982                         "0296 Restart HBA Data: x%x x%x\n",
3983                         phba->pport->port_state, psli->sli_flag);
3984
3985         /* Take PCIe device Advanced Error Reporting (AER) state */
3986         hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
3987
3988         lpfc_sli4_brdreset(phba);
3989
3990         spin_lock_irq(&phba->hbalock);
3991         phba->pport->stopped = 0;
3992         phba->link_state = LPFC_INIT_START;
3993         phba->hba_flag = 0;
3994         spin_unlock_irq(&phba->hbalock);
3995
3996         memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
3997         psli->stats_start = get_seconds();
3998
3999         /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4000         if (hba_aer_enabled)
4001                 pci_disable_pcie_error_reporting(phba->pcidev);
4002
4003         lpfc_hba_down_post(phba);
4004
4005         return 0;
4006 }
4007
4008 /**
4009  * lpfc_sli_brdrestart - Wrapper func for restarting hba
4010  * @phba: Pointer to HBA context object.
4011  *
4012  * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
4013  * API jump table function pointer from the lpfc_hba struct.
4014 **/
4015 int
4016 lpfc_sli_brdrestart(struct lpfc_hba *phba)
4017 {
4018         return phba->lpfc_sli_brdrestart(phba);
4019 }
4020
4021 /**
4022  * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
4023  * @phba: Pointer to HBA context object.
4024  *
4025  * This function is called after a HBA restart to wait for successful
4026  * restart of the HBA. Successful restart of the HBA is indicated by
4027  * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
4028  * iteration, the function will restart the HBA again. The function returns
4029  * zero if HBA successfully restarted else returns negative error code.
4030  **/
4031 static int
4032 lpfc_sli_chipset_init(struct lpfc_hba *phba)
4033 {
4034         uint32_t status, i = 0;
4035
4036         /* Read the HBA Host Status Register */
4037         status = readl(phba->HSregaddr);
4038
4039         /* Check status register to see what current state is */
4040         i = 0;
4041         while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
4042
4043                 /* Check every 10ms for 10 retries, then every 100ms for 90
4044                  * retries, then every 1 sec for 50 retires for a total of
4045                  * ~60 seconds before reset the board again and check every
4046                  * 1 sec for 50 retries. The up to 60 seconds before the
4047                  * board ready is required by the Falcon FIPS zeroization
4048                  * complete, and any reset the board in between shall cause
4049                  * restart of zeroization, further delay the board ready.
4050                  */
4051                 if (i++ >= 200) {
4052                         /* Adapter failed to init, timeout, status reg
4053                            <status> */
4054                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4055                                         "0436 Adapter failed to init, "
4056                                         "timeout, status reg x%x, "
4057                                         "FW Data: A8 x%x AC x%x\n", status,
4058                                         readl(phba->MBslimaddr + 0xa8),
4059                                         readl(phba->MBslimaddr + 0xac));
4060                         phba->link_state = LPFC_HBA_ERROR;
4061                         return -ETIMEDOUT;
4062                 }
4063
4064                 /* Check to see if any errors occurred during init */
4065                 if (status & HS_FFERM) {
4066                         /* ERROR: During chipset initialization */
4067                         /* Adapter failed to init, chipset, status reg
4068                            <status> */
4069                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4070                                         "0437 Adapter failed to init, "
4071                                         "chipset, status reg x%x, "
4072                                         "FW Data: A8 x%x AC x%x\n", status,
4073                                         readl(phba->MBslimaddr + 0xa8),
4074                                         readl(phba->MBslimaddr + 0xac));
4075                         phba->link_state = LPFC_HBA_ERROR;
4076                         return -EIO;
4077                 }
4078
4079                 if (i <= 10)
4080                         msleep(10);
4081                 else if (i <= 100)
4082                         msleep(100);
4083                 else
4084                         msleep(1000);
4085
4086                 if (i == 150) {
4087                         /* Do post */
4088                         phba->pport->port_state = LPFC_VPORT_UNKNOWN;
4089                         lpfc_sli_brdrestart(phba);
4090                 }
4091                 /* Read the HBA Host Status Register */
4092                 status = readl(phba->HSregaddr);
4093         }
4094
4095         /* Check to see if any errors occurred during init */
4096         if (status & HS_FFERM) {
4097                 /* ERROR: During chipset initialization */
4098                 /* Adapter failed to init, chipset, status reg <status> */
4099                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4100                                 "0438 Adapter failed to init, chipset, "
4101                                 "status reg x%x, "
4102                                 "FW Data: A8 x%x AC x%x\n", status,
4103                                 readl(phba->MBslimaddr + 0xa8),
4104                                 readl(phba->MBslimaddr + 0xac));
4105                 phba->link_state = LPFC_HBA_ERROR;
4106                 return -EIO;
4107         }
4108
4109         /* Clear all interrupt enable conditions */
4110         writel(0, phba->HCregaddr);
4111         readl(phba->HCregaddr); /* flush */
4112
4113         /* setup host attn register */
4114         writel(0xffffffff, phba->HAregaddr);
4115         readl(phba->HAregaddr); /* flush */
4116         return 0;
4117 }
4118
4119 /**
4120  * lpfc_sli_hbq_count - Get the number of HBQs to be configured
4121  *
4122  * This function calculates and returns the number of HBQs required to be
4123  * configured.
4124  **/
4125 int
4126 lpfc_sli_hbq_count(void)
4127 {
4128         return ARRAY_SIZE(lpfc_hbq_defs);
4129 }
4130
4131 /**
4132  * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
4133  *
4134  * This function adds the number of hbq entries in every HBQ to get
4135  * the total number of hbq entries required for the HBA and returns
4136  * the total count.
4137  **/
4138 static int
4139 lpfc_sli_hbq_entry_count(void)
4140 {
4141         int  hbq_count = lpfc_sli_hbq_count();
4142         int  count = 0;
4143         int  i;
4144
4145         for (i = 0; i < hbq_count; ++i)
4146                 count += lpfc_hbq_defs[i]->entry_count;
4147         return count;
4148 }
4149
4150 /**
4151  * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
4152  *
4153  * This function calculates amount of memory required for all hbq entries
4154  * to be configured and returns the total memory required.
4155  **/
4156 int
4157 lpfc_sli_hbq_size(void)
4158 {
4159         return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
4160 }
4161
4162 /**
4163  * lpfc_sli_hbq_setup - configure and initialize HBQs
4164  * @phba: Pointer to HBA context object.
4165  *
4166  * This function is called during the SLI initialization to configure
4167  * all the HBQs and post buffers to the HBQ. The caller is not
4168  * required to hold any locks. This function will return zero if successful
4169  * else it will return negative error code.
4170  **/
4171 static int
4172 lpfc_sli_hbq_setup(struct lpfc_hba *phba)
4173 {
4174         int  hbq_count = lpfc_sli_hbq_count();
4175         LPFC_MBOXQ_t *pmb;
4176         MAILBOX_t *pmbox;
4177         uint32_t hbqno;
4178         uint32_t hbq_entry_index;
4179
4180                                 /* Get a Mailbox buffer to setup mailbox
4181                                  * commands for HBA initialization
4182                                  */
4183         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4184
4185         if (!pmb)
4186                 return -ENOMEM;
4187
4188         pmbox = &pmb->u.mb;
4189
4190         /* Initialize the struct lpfc_sli_hbq structure for each hbq */
4191         phba->link_state = LPFC_INIT_MBX_CMDS;
4192         phba->hbq_in_use = 1;
4193
4194         hbq_entry_index = 0;
4195         for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
4196                 phba->hbqs[hbqno].next_hbqPutIdx = 0;
4197                 phba->hbqs[hbqno].hbqPutIdx      = 0;
4198                 phba->hbqs[hbqno].local_hbqGetIdx   = 0;
4199                 phba->hbqs[hbqno].entry_count =
4200                         lpfc_hbq_defs[hbqno]->entry_count;
4201                 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
4202                         hbq_entry_index, pmb);
4203                 hbq_entry_index += phba->hbqs[hbqno].entry_count;
4204
4205                 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
4206                         /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
4207                            mbxStatus <status>, ring <num> */
4208
4209                         lpfc_printf_log(phba, KERN_ERR,
4210                                         LOG_SLI | LOG_VPORT,
4211                                         "1805 Adapter failed to init. "
4212                                         "Data: x%x x%x x%x\n",
4213                                         pmbox->mbxCommand,
4214                                         pmbox->mbxStatus, hbqno);
4215
4216                         phba->link_state = LPFC_HBA_ERROR;
4217                         mempool_free(pmb, phba->mbox_mem_pool);
4218                         return -ENXIO;
4219                 }
4220         }
4221         phba->hbq_count = hbq_count;
4222
4223         mempool_free(pmb, phba->mbox_mem_pool);
4224
4225         /* Initially populate or replenish the HBQs */
4226         for (hbqno = 0; hbqno < hbq_count; ++hbqno)
4227                 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
4228         return 0;
4229 }
4230
4231 /**
4232  * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
4233  * @phba: Pointer to HBA context object.
4234  *
4235  * This function is called during the SLI initialization to configure
4236  * all the HBQs and post buffers to the HBQ. The caller is not
4237  * required to hold any locks. This function will return zero if successful
4238  * else it will return negative error code.
4239  **/
4240 static int
4241 lpfc_sli4_rb_setup(struct lpfc_hba *phba)
4242 {
4243         phba->hbq_in_use = 1;
4244         phba->hbqs[0].entry_count = lpfc_hbq_defs[0]->entry_count;
4245         phba->hbq_count = 1;
4246         /* Initially populate or replenish the HBQs */
4247         lpfc_sli_hbqbuf_init_hbqs(phba, 0);
4248         return 0;
4249 }
4250
4251 /**
4252  * lpfc_sli_config_port - Issue config port mailbox command
4253  * @phba: Pointer to HBA context object.
4254  * @sli_mode: sli mode - 2/3
4255  *
4256  * This function is called by the sli intialization code path
4257  * to issue config_port mailbox command. This function restarts the
4258  * HBA firmware and issues a config_port mailbox command to configure
4259  * the SLI interface in the sli mode specified by sli_mode
4260  * variable. The caller is not required to hold any locks.
4261  * The function returns 0 if successful, else returns negative error
4262  * code.
4263  **/
4264 int
4265 lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
4266 {
4267         LPFC_MBOXQ_t *pmb;
4268         uint32_t resetcount = 0, rc = 0, done = 0;
4269
4270         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4271         if (!pmb) {
4272                 phba->link_state = LPFC_HBA_ERROR;
4273                 return -ENOMEM;
4274         }
4275
4276         phba->sli_rev = sli_mode;
4277         while (resetcount < 2 && !done) {
4278                 spin_lock_irq(&phba->hbalock);
4279                 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
4280                 spin_unlock_irq(&phba->hbalock);
4281                 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
4282                 lpfc_sli_brdrestart(phba);
4283                 rc = lpfc_sli_chipset_init(phba);
4284                 if (rc)
4285                         break;
4286
4287                 spin_lock_irq(&phba->hbalock);
4288                 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
4289                 spin_unlock_irq(&phba->hbalock);
4290                 resetcount++;
4291
4292                 /* Call pre CONFIG_PORT mailbox command initialization.  A
4293                  * value of 0 means the call was successful.  Any other
4294                  * nonzero value is a failure, but if ERESTART is returned,
4295                  * the driver may reset the HBA and try again.
4296                  */
4297                 rc = lpfc_config_port_prep(phba);
4298                 if (rc == -ERESTART) {
4299                         phba->link_state = LPFC_LINK_UNKNOWN;
4300                         continue;
4301                 } else if (rc)
4302                         break;
4303                 phba->link_state = LPFC_INIT_MBX_CMDS;
4304                 lpfc_config_port(phba, pmb);
4305                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
4306                 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
4307                                         LPFC_SLI3_HBQ_ENABLED |
4308                                         LPFC_SLI3_CRP_ENABLED |
4309                                         LPFC_SLI3_BG_ENABLED |
4310                                         LPFC_SLI3_DSS_ENABLED);
4311                 if (rc != MBX_SUCCESS) {
4312                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4313                                 "0442 Adapter failed to init, mbxCmd x%x "
4314                                 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
4315                                 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
4316                         spin_lock_irq(&phba->hbalock);
4317                         phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
4318                         spin_unlock_irq(&phba->hbalock);
4319                         rc = -ENXIO;
4320                 } else {
4321                         /* Allow asynchronous mailbox command to go through */
4322                         spin_lock_irq(&phba->hbalock);
4323                         phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4324                         spin_unlock_irq(&phba->hbalock);
4325                         done = 1;
4326                 }
4327         }
4328         if (!done) {
4329                 rc = -EINVAL;
4330                 goto do_prep_failed;
4331         }
4332         if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
4333                 if (!pmb->u.mb.un.varCfgPort.cMA) {
4334                         rc = -ENXIO;
4335                         goto do_prep_failed;
4336                 }
4337                 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
4338                         phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
4339                         phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
4340                         phba->max_vports = (phba->max_vpi > phba->max_vports) ?
4341                                 phba->max_vpi : phba->max_vports;
4342
4343                 } else
4344                         phba->max_vpi = 0;
4345                 phba->fips_level = 0;
4346                 phba->fips_spec_rev = 0;
4347                 if (pmb->u.mb.un.varCfgPort.gdss) {
4348                         phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
4349                         phba->fips_level = pmb->u.mb.un.varCfgPort.fips_level;
4350                         phba->fips_spec_rev = pmb->u.mb.un.varCfgPort.fips_rev;
4351                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4352                                         "2850 Security Crypto Active. FIPS x%d "
4353                                         "(Spec Rev: x%d)",
4354                                         phba->fips_level, phba->fips_spec_rev);
4355                 }
4356                 if (pmb->u.mb.un.varCfgPort.sec_err) {
4357                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4358                                         "2856 Config Port Security Crypto "
4359                                         "Error: x%x ",
4360                                         pmb->u.mb.un.varCfgPort.sec_err);
4361                 }
4362                 if (pmb->u.mb.un.varCfgPort.gerbm)
4363                         phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
4364                 if (pmb->u.mb.un.varCfgPort.gcrp)
4365                         phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
4366
4367                 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
4368                 phba->port_gp = phba->mbox->us.s3_pgp.port;
4369
4370                 if (phba->cfg_enable_bg) {
4371                         if (pmb->u.mb.un.varCfgPort.gbg)
4372                                 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
4373                         else
4374                                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4375                                                 "0443 Adapter did not grant "
4376                                                 "BlockGuard\n");
4377                 }
4378         } else {
4379                 phba->hbq_get = NULL;
4380                 phba->port_gp = phba->mbox->us.s2.port;
4381                 phba->max_vpi = 0;
4382         }
4383 do_prep_failed:
4384         mempool_free(pmb, phba->mbox_mem_pool);
4385         return rc;
4386 }
4387
4388
4389 /**
4390  * lpfc_sli_hba_setup - SLI intialization function
4391  * @phba: Pointer to HBA context object.
4392  *
4393  * This function is the main SLI intialization function. This function
4394  * is called by the HBA intialization code, HBA reset code and HBA
4395  * error attention handler code. Caller is not required to hold any
4396  * locks. This function issues config_port mailbox command to configure
4397  * the SLI, setup iocb rings and HBQ rings. In the end the function
4398  * calls the config_port_post function to issue init_link mailbox
4399  * command and to start the discovery. The function will return zero
4400  * if successful, else it will return negative error code.
4401  **/
4402 int
4403 lpfc_sli_hba_setup(struct lpfc_hba *phba)
4404 {
4405         uint32_t rc;
4406         int  mode = 3;
4407
4408         switch (lpfc_sli_mode) {
4409         case 2:
4410                 if (phba->cfg_enable_npiv) {
4411                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
4412                                 "1824 NPIV enabled: Override lpfc_sli_mode "
4413                                 "parameter (%d) to auto (0).\n",
4414                                 lpfc_sli_mode);
4415                         break;
4416                 }
4417                 mode = 2;
4418                 break;
4419         case 0:
4420         case 3:
4421                 break;
4422         default:
4423                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
4424                                 "1819 Unrecognized lpfc_sli_mode "
4425                                 "parameter: %d.\n", lpfc_sli_mode);
4426
4427                 break;
4428         }
4429
4430         rc = lpfc_sli_config_port(phba, mode);
4431
4432         if (rc && lpfc_sli_mode == 3)
4433                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
4434                                 "1820 Unable to select SLI-3.  "
4435                                 "Not supported by adapter.\n");
4436         if (rc && mode != 2)
4437                 rc = lpfc_sli_config_port(phba, 2);
4438         if (rc)
4439                 goto lpfc_sli_hba_setup_error;
4440
4441         /* Enable PCIe device Advanced Error Reporting (AER) if configured */
4442         if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
4443                 rc = pci_enable_pcie_error_reporting(phba->pcidev);
4444                 if (!rc) {
4445                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4446                                         "2709 This device supports "
4447                                         "Advanced Error Reporting (AER)\n");
4448                         spin_lock_irq(&phba->hbalock);
4449                         phba->hba_flag |= HBA_AER_ENABLED;
4450                         spin_unlock_irq(&phba->hbalock);
4451                 } else {
4452                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4453                                         "2708 This device does not support "
4454                                         "Advanced Error Reporting (AER)\n");
4455                         phba->cfg_aer_support = 0;
4456                 }
4457         }
4458
4459         if (phba->sli_rev == 3) {
4460                 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
4461                 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
4462         } else {
4463                 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
4464                 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
4465                 phba->sli3_options = 0;
4466         }
4467
4468         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4469                         "0444 Firmware in SLI %x mode. Max_vpi %d\n",
4470                         phba->sli_rev, phba->max_vpi);
4471         rc = lpfc_sli_ring_map(phba);
4472
4473         if (rc)
4474                 goto lpfc_sli_hba_setup_error;
4475
4476         /* Init HBQs */
4477         if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
4478                 rc = lpfc_sli_hbq_setup(phba);
4479                 if (rc)
4480                         goto lpfc_sli_hba_setup_error;
4481         }
4482         spin_lock_irq(&phba->hbalock);
4483         phba->sli.sli_flag |= LPFC_PROCESS_LA;
4484         spin_unlock_irq(&phba->hbalock);
4485
4486         rc = lpfc_config_port_post(phba);
4487         if (rc)
4488                 goto lpfc_sli_hba_setup_error;
4489
4490         return rc;
4491
4492 lpfc_sli_hba_setup_error:
4493         phba->link_state = LPFC_HBA_ERROR;
4494         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4495                         "0445 Firmware initialization failed\n");
4496         return rc;
4497 }
4498
4499 /**
4500  * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
4501  * @phba: Pointer to HBA context object.
4502  * @mboxq: mailbox pointer.
4503  * This function issue a dump mailbox command to read config region
4504  * 23 and parse the records in the region and populate driver
4505  * data structure.
4506  **/
4507 static int
4508 lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba,
4509                 LPFC_MBOXQ_t *mboxq)
4510 {
4511         struct lpfc_dmabuf *mp;
4512         struct lpfc_mqe *mqe;
4513         uint32_t data_length;
4514         int rc;
4515
4516         /* Program the default value of vlan_id and fc_map */
4517         phba->valid_vlan = 0;
4518         phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4519         phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4520         phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
4521
4522         mqe = &mboxq->u.mqe;
4523         if (lpfc_dump_fcoe_param(phba, mboxq))
4524                 return -ENOMEM;
4525
4526         mp = (struct lpfc_dmabuf *) mboxq->context1;
4527         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4528
4529         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4530                         "(%d):2571 Mailbox cmd x%x Status x%x "
4531                         "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4532                         "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4533                         "CQ: x%x x%x x%x x%x\n",
4534                         mboxq->vport ? mboxq->vport->vpi : 0,
4535                         bf_get(lpfc_mqe_command, mqe),
4536                         bf_get(lpfc_mqe_status, mqe),
4537                         mqe->un.mb_words[0], mqe->un.mb_words[1],
4538                         mqe->un.mb_words[2], mqe->un.mb_words[3],
4539                         mqe->un.mb_words[4], mqe->un.mb_words[5],
4540                         mqe->un.mb_words[6], mqe->un.mb_words[7],
4541                         mqe->un.mb_words[8], mqe->un.mb_words[9],
4542                         mqe->un.mb_words[10], mqe->un.mb_words[11],
4543                         mqe->un.mb_words[12], mqe->un.mb_words[13],
4544                         mqe->un.mb_words[14], mqe->un.mb_words[15],
4545                         mqe->un.mb_words[16], mqe->un.mb_words[50],
4546                         mboxq->mcqe.word0,
4547                         mboxq->mcqe.mcqe_tag0,  mboxq->mcqe.mcqe_tag1,
4548                         mboxq->mcqe.trailer);
4549
4550         if (rc) {
4551                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4552                 kfree(mp);
4553                 return -EIO;
4554         }
4555         data_length = mqe->un.mb_words[5];
4556         if (data_length > DMP_RGN23_SIZE) {
4557                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4558                 kfree(mp);
4559                 return -EIO;
4560         }
4561
4562         lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
4563         lpfc_mbuf_free(phba, mp->virt, mp->phys);
4564         kfree(mp);
4565         return 0;
4566 }
4567
4568 /**
4569  * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
4570  * @phba: pointer to lpfc hba data structure.
4571  * @mboxq: pointer to the LPFC_MBOXQ_t structure.
4572  * @vpd: pointer to the memory to hold resulting port vpd data.
4573  * @vpd_size: On input, the number of bytes allocated to @vpd.
4574  *            On output, the number of data bytes in @vpd.
4575  *
4576  * This routine executes a READ_REV SLI4 mailbox command.  In
4577  * addition, this routine gets the port vpd data.
4578  *
4579  * Return codes
4580  *      0 - successful
4581  *      -ENOMEM - could not allocated memory.
4582  **/
4583 static int
4584 lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
4585                     uint8_t *vpd, uint32_t *vpd_size)
4586 {
4587         int rc = 0;
4588         uint32_t dma_size;
4589         struct lpfc_dmabuf *dmabuf;
4590         struct lpfc_mqe *mqe;
4591
4592         dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4593         if (!dmabuf)
4594                 return -ENOMEM;
4595
4596         /*
4597          * Get a DMA buffer for the vpd data resulting from the READ_REV
4598          * mailbox command.
4599          */
4600         dma_size = *vpd_size;
4601         dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
4602                                           dma_size,
4603                                           &dmabuf->phys,
4604                                           GFP_KERNEL);
4605         if (!dmabuf->virt) {
4606                 kfree(dmabuf);
4607                 return -ENOMEM;
4608         }
4609         memset(dmabuf->virt, 0, dma_size);
4610
4611         /*
4612          * The SLI4 implementation of READ_REV conflicts at word1,
4613          * bits 31:16 and SLI4 adds vpd functionality not present
4614          * in SLI3.  This code corrects the conflicts.
4615          */
4616         lpfc_read_rev(phba, mboxq);
4617         mqe = &mboxq->u.mqe;
4618         mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
4619         mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
4620         mqe->un.read_rev.word1 &= 0x0000FFFF;
4621         bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
4622         bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
4623
4624         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4625         if (rc) {
4626                 dma_free_coherent(&phba->pcidev->dev, dma_size,
4627                                   dmabuf->virt, dmabuf->phys);
4628                 kfree(dmabuf);
4629                 return -EIO;
4630         }
4631
4632         /*
4633          * The available vpd length cannot be bigger than the
4634          * DMA buffer passed to the port.  Catch the less than
4635          * case and update the caller's size.
4636          */
4637         if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
4638                 *vpd_size = mqe->un.read_rev.avail_vpd_len;
4639
4640         memcpy(vpd, dmabuf->virt, *vpd_size);
4641
4642         dma_free_coherent(&phba->pcidev->dev, dma_size,
4643                           dmabuf->virt, dmabuf->phys);
4644         kfree(dmabuf);
4645         return 0;
4646 }
4647
4648 /**
4649  * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
4650  * @phba: pointer to lpfc hba data structure.
4651  *
4652  * This routine is called to explicitly arm the SLI4 device's completion and
4653  * event queues
4654  **/
4655 static void
4656 lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
4657 {
4658         uint8_t fcp_eqidx;
4659
4660         lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
4661         lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
4662         for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++)
4663                 lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[fcp_eqidx],
4664                                      LPFC_QUEUE_REARM);
4665         lpfc_sli4_eq_release(phba->sli4_hba.sp_eq, LPFC_QUEUE_REARM);
4666         for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++)
4667                 lpfc_sli4_eq_release(phba->sli4_hba.fp_eq[fcp_eqidx],
4668                                      LPFC_QUEUE_REARM);
4669 }
4670
4671 /**
4672  * lpfc_sli4_hba_setup - SLI4 device intialization PCI function
4673  * @phba: Pointer to HBA context object.
4674  *
4675  * This function is the main SLI4 device intialization PCI function. This
4676  * function is called by the HBA intialization code, HBA reset code and
4677  * HBA error attention handler code. Caller is not required to hold any
4678  * locks.
4679  **/
4680 int
4681 lpfc_sli4_hba_setup(struct lpfc_hba *phba)
4682 {
4683         int rc;
4684         LPFC_MBOXQ_t *mboxq;
4685         struct lpfc_mqe *mqe;
4686         uint8_t *vpd;
4687         uint32_t vpd_size;
4688         uint32_t ftr_rsp = 0;
4689         struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
4690         struct lpfc_vport *vport = phba->pport;
4691         struct lpfc_dmabuf *mp;
4692
4693         /*
4694          * TODO:  Why does this routine execute these task in a different
4695          * order from probe?
4696          */
4697         /* Perform a PCI function reset to start from clean */
4698         rc = lpfc_pci_function_reset(phba);
4699         if (unlikely(rc))
4700                 return -ENODEV;
4701
4702         /* Check the HBA Host Status Register for readyness */
4703         rc = lpfc_sli4_post_status_check(phba);
4704         if (unlikely(rc))
4705                 return -ENODEV;
4706         else {
4707                 spin_lock_irq(&phba->hbalock);
4708                 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
4709                 spin_unlock_irq(&phba->hbalock);
4710         }
4711
4712         /*
4713          * Allocate a single mailbox container for initializing the
4714          * port.
4715          */
4716         mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4717         if (!mboxq)
4718                 return -ENOMEM;
4719
4720         /*
4721          * Continue initialization with default values even if driver failed
4722          * to read FCoE param config regions
4723          */
4724         if (lpfc_sli4_read_fcoe_params(phba, mboxq))
4725                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
4726                         "2570 Failed to read FCoE parameters\n");
4727
4728         /* Issue READ_REV to collect vpd and FW information. */
4729         vpd_size = SLI4_PAGE_SIZE;
4730         vpd = kzalloc(vpd_size, GFP_KERNEL);
4731         if (!vpd) {
4732                 rc = -ENOMEM;
4733                 goto out_free_mbox;
4734         }
4735
4736         rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
4737         if (unlikely(rc)) {
4738                 kfree(vpd);
4739                 goto out_free_mbox;
4740         }
4741         mqe = &mboxq->u.mqe;
4742         phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
4743         if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev))
4744                 phba->hba_flag |= HBA_FCOE_MODE;
4745         else
4746                 phba->hba_flag &= ~HBA_FCOE_MODE;
4747
4748         if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
4749                 LPFC_DCBX_CEE_MODE)
4750                 phba->hba_flag |= HBA_FIP_SUPPORT;
4751         else
4752                 phba->hba_flag &= ~HBA_FIP_SUPPORT;
4753
4754         if (phba->sli_rev != LPFC_SLI_REV4 ||
4755             !(phba->hba_flag & HBA_FCOE_MODE)) {
4756                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4757                         "0376 READ_REV Error. SLI Level %d "
4758                         "FCoE enabled %d\n",
4759                         phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
4760                 rc = -EIO;
4761                 kfree(vpd);
4762                 goto out_free_mbox;
4763         }
4764         /*
4765          * Evaluate the read rev and vpd data. Populate the driver
4766          * state with the results. If this routine fails, the failure
4767          * is not fatal as the driver will use generic values.
4768          */
4769         rc = lpfc_parse_vpd(phba, vpd, vpd_size);
4770         if (unlikely(!rc)) {
4771                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4772                                 "0377 Error %d parsing vpd. "
4773                                 "Using defaults.\n", rc);
4774                 rc = 0;
4775         }
4776         kfree(vpd);
4777
4778         /* Save information as VPD data */
4779         phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
4780         phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
4781         phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
4782         phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
4783                                          &mqe->un.read_rev);
4784         phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
4785                                        &mqe->un.read_rev);
4786         phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
4787                                             &mqe->un.read_rev);
4788         phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
4789                                            &mqe->un.read_rev);
4790         phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
4791         memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
4792         phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
4793         memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
4794         phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
4795         memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
4796         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4797                         "(%d):0380 READ_REV Status x%x "
4798                         "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
4799                         mboxq->vport ? mboxq->vport->vpi : 0,
4800                         bf_get(lpfc_mqe_status, mqe),
4801                         phba->vpd.rev.opFwName,
4802                         phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
4803                         phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
4804
4805         /*
4806          * Discover the port's supported feature set and match it against the
4807          * hosts requests.
4808          */
4809         lpfc_request_features(phba, mboxq);
4810         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4811         if (unlikely(rc)) {
4812                 rc = -EIO;
4813                 goto out_free_mbox;
4814         }
4815
4816         /*
4817          * The port must support FCP initiator mode as this is the
4818          * only mode running in the host.
4819          */
4820         if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
4821                 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
4822                                 "0378 No support for fcpi mode.\n");
4823                 ftr_rsp++;
4824         }
4825
4826         /*
4827          * If the port cannot support the host's requested features
4828          * then turn off the global config parameters to disable the
4829          * feature in the driver.  This is not a fatal error.
4830          */
4831         if ((phba->cfg_enable_bg) &&
4832             !(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
4833                 ftr_rsp++;
4834
4835         if (phba->max_vpi && phba->cfg_enable_npiv &&
4836             !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
4837                 ftr_rsp++;
4838
4839         if (ftr_rsp) {
4840                 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
4841                                 "0379 Feature Mismatch Data: x%08x %08x "
4842                                 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
4843                                 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
4844                                 phba->cfg_enable_npiv, phba->max_vpi);
4845                 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
4846                         phba->cfg_enable_bg = 0;
4847                 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
4848                         phba->cfg_enable_npiv = 0;
4849         }
4850
4851         /* These SLI3 features are assumed in SLI4 */
4852         spin_lock_irq(&phba->hbalock);
4853         phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
4854         spin_unlock_irq(&phba->hbalock);
4855
4856         /* Read the port's service parameters. */
4857         rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
4858         if (rc) {
4859                 phba->link_state = LPFC_HBA_ERROR;
4860                 rc = -ENOMEM;
4861                 goto out_free_mbox;
4862         }
4863
4864         mboxq->vport = vport;
4865         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4866         mp = (struct lpfc_dmabuf *) mboxq->context1;
4867         if (rc == MBX_SUCCESS) {
4868                 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
4869                 rc = 0;
4870         }
4871
4872         /*
4873          * This memory was allocated by the lpfc_read_sparam routine. Release
4874          * it to the mbuf pool.
4875          */
4876         lpfc_mbuf_free(phba, mp->virt, mp->phys);
4877         kfree(mp);
4878         mboxq->context1 = NULL;
4879         if (unlikely(rc)) {
4880                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4881                                 "0382 READ_SPARAM command failed "
4882                                 "status %d, mbxStatus x%x\n",
4883                                 rc, bf_get(lpfc_mqe_status, mqe));
4884                 phba->link_state = LPFC_HBA_ERROR;
4885                 rc = -EIO;
4886                 goto out_free_mbox;
4887         }
4888
4889         if (phba->cfg_soft_wwnn)
4890                 u64_to_wwn(phba->cfg_soft_wwnn,
4891                            vport->fc_sparam.nodeName.u.wwn);
4892         if (phba->cfg_soft_wwpn)
4893                 u64_to_wwn(phba->cfg_soft_wwpn,
4894                            vport->fc_sparam.portName.u.wwn);
4895         memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
4896                sizeof(struct lpfc_name));
4897         memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
4898                sizeof(struct lpfc_name));
4899
4900         /* Update the fc_host data structures with new wwn. */
4901         fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
4902         fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
4903
4904         /* Register SGL pool to the device using non-embedded mailbox command */
4905         rc = lpfc_sli4_post_sgl_list(phba);
4906         if (unlikely(rc)) {
4907                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4908                                 "0582 Error %d during sgl post operation\n",
4909                                         rc);
4910                 rc = -ENODEV;
4911                 goto out_free_mbox;
4912         }
4913
4914         /* Register SCSI SGL pool to the device */
4915         rc = lpfc_sli4_repost_scsi_sgl_list(phba);
4916         if (unlikely(rc)) {
4917                 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
4918                                 "0383 Error %d during scsi sgl post "
4919                                 "operation\n", rc);
4920                 /* Some Scsi buffers were moved to the abort scsi list */
4921                 /* A pci function reset will repost them */
4922                 rc = -ENODEV;
4923                 goto out_free_mbox;
4924         }
4925
4926         /* Post the rpi header region to the device. */
4927         rc = lpfc_sli4_post_all_rpi_hdrs(phba);
4928         if (unlikely(rc)) {
4929                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4930                                 "0393 Error %d during rpi post operation\n",
4931                                 rc);
4932                 rc = -ENODEV;
4933                 goto out_free_mbox;
4934         }
4935
4936         /* Set up all the queues to the device */
4937         rc = lpfc_sli4_queue_setup(phba);
4938         if (unlikely(rc)) {
4939                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4940                                 "0381 Error %d during queue setup.\n ", rc);
4941                 goto out_stop_timers;
4942         }
4943
4944         /* Arm the CQs and then EQs on device */
4945         lpfc_sli4_arm_cqeq_intr(phba);
4946
4947         /* Indicate device interrupt mode */
4948         phba->sli4_hba.intr_enable = 1;
4949
4950         /* Allow asynchronous mailbox command to go through */
4951         spin_lock_irq(&phba->hbalock);
4952         phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4953         spin_unlock_irq(&phba->hbalock);
4954
4955         /* Post receive buffers to the device */
4956         lpfc_sli4_rb_setup(phba);
4957
4958         /* Reset HBA FCF states after HBA reset */
4959         phba->fcf.fcf_flag = 0;
4960         phba->fcf.current_rec.flag = 0;
4961
4962         /* Start the ELS watchdog timer */
4963         mod_timer(&vport->els_tmofunc,
4964                   jiffies + HZ * (phba->fc_ratov * 2));
4965
4966         /* Start heart beat timer */
4967         mod_timer(&phba->hb_tmofunc,
4968                   jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
4969         phba->hb_outstanding = 0;
4970         phba->last_completion_time = jiffies;
4971
4972         /* Start error attention (ERATT) polling timer */
4973         mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
4974
4975         /* Enable PCIe device Advanced Error Reporting (AER) if configured */
4976         if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
4977                 rc = pci_enable_pcie_error_reporting(phba->pcidev);
4978                 if (!rc) {
4979                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4980                                         "2829 This device supports "
4981                                         "Advanced Error Reporting (AER)\n");
4982                         spin_lock_irq(&phba->hbalock);
4983                         phba->hba_flag |= HBA_AER_ENABLED;
4984                         spin_unlock_irq(&phba->hbalock);
4985                 } else {
4986                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4987                                         "2830 This device does not support "
4988                                         "Advanced Error Reporting (AER)\n");
4989                         phba->cfg_aer_support = 0;
4990                 }
4991         }
4992
4993         if (!(phba->hba_flag & HBA_FCOE_MODE)) {
4994                 /*
4995                  * The FC Port needs to register FCFI (index 0)
4996                  */
4997                 lpfc_reg_fcfi(phba, mboxq);
4998                 mboxq->vport = phba->pport;
4999                 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5000                 if (rc == MBX_SUCCESS)
5001                         rc = 0;
5002                 else
5003                         goto out_unset_queue;
5004         }
5005         /*
5006          * The port is ready, set the host's link state to LINK_DOWN
5007          * in preparation for link interrupts.
5008          */
5009         spin_lock_irq(&phba->hbalock);
5010         phba->link_state = LPFC_LINK_DOWN;
5011         spin_unlock_irq(&phba->hbalock);
5012         rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
5013 out_unset_queue:
5014         /* Unset all the queues set up in this routine when error out */
5015         if (rc)
5016                 lpfc_sli4_queue_unset(phba);
5017 out_stop_timers:
5018         if (rc)
5019                 lpfc_stop_hba_timers(phba);
5020 out_free_mbox:
5021         mempool_free(mboxq, phba->mbox_mem_pool);
5022         return rc;
5023 }
5024
5025 /**
5026  * lpfc_mbox_timeout - Timeout call back function for mbox timer
5027  * @ptr: context object - pointer to hba structure.
5028  *
5029  * This is the callback function for mailbox timer. The mailbox
5030  * timer is armed when a new mailbox command is issued and the timer
5031  * is deleted when the mailbox complete. The function is called by
5032  * the kernel timer code when a mailbox does not complete within
5033  * expected time. This function wakes up the worker thread to
5034  * process the mailbox timeout and returns. All the processing is
5035  * done by the worker thread function lpfc_mbox_timeout_handler.
5036  **/
5037 void
5038 lpfc_mbox_timeout(unsigned long ptr)
5039 {
5040         struct lpfc_hba  *phba = (struct lpfc_hba *) ptr;
5041         unsigned long iflag;
5042         uint32_t tmo_posted;
5043
5044         spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
5045         tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
5046         if (!tmo_posted)
5047                 phba->pport->work_port_events |= WORKER_MBOX_TMO;
5048         spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
5049
5050         if (!tmo_posted)
5051                 lpfc_worker_wake_up(phba);
5052         return;
5053 }
5054
5055
5056 /**
5057  * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
5058  * @phba: Pointer to HBA context object.
5059  *
5060  * This function is called from worker thread when a mailbox command times out.
5061  * The caller is not required to hold any locks. This function will reset the
5062  * HBA and recover all the pending commands.
5063  **/
5064 void
5065 lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
5066 {
5067         LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
5068         MAILBOX_t *mb = &pmbox->u.mb;
5069         struct lpfc_sli *psli = &phba->sli;
5070         struct lpfc_sli_ring *pring;
5071
5072         /* Check the pmbox pointer first.  There is a race condition
5073          * between the mbox timeout handler getting executed in the
5074          * worklist and the mailbox actually completing. When this
5075          * race condition occurs, the mbox_active will be NULL.
5076          */
5077         spin_lock_irq(&phba->hbalock);
5078         if (pmbox == NULL) {
5079                 lpfc_printf_log(phba, KERN_WARNING,
5080                                 LOG_MBOX | LOG_SLI,
5081                                 "0353 Active Mailbox cleared - mailbox timeout "
5082                                 "exiting\n");
5083                 spin_unlock_irq(&phba->hbalock);
5084                 return;
5085         }
5086
5087         /* Mbox cmd <mbxCommand> timeout */
5088         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5089                         "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
5090                         mb->mbxCommand,
5091                         phba->pport->port_state,
5092                         phba->sli.sli_flag,
5093                         phba->sli.mbox_active);
5094         spin_unlock_irq(&phba->hbalock);
5095
5096         /* Setting state unknown so lpfc_sli_abort_iocb_ring
5097          * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
5098          * it to fail all oustanding SCSI IO.
5099          */
5100         spin_lock_irq(&phba->pport->work_port_lock);
5101         phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
5102         spin_unlock_irq(&phba->pport->work_port_lock);
5103         spin_lock_irq(&phba->hbalock);
5104         phba->link_state = LPFC_LINK_UNKNOWN;
5105         psli->sli_flag &= ~LPFC_SLI_ACTIVE;
5106         spin_unlock_irq(&phba->hbalock);
5107
5108         pring = &psli->ring[psli->fcp_ring];
5109         lpfc_sli_abort_iocb_ring(phba, pring);
5110
5111         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5112                         "0345 Resetting board due to mailbox timeout\n");
5113
5114         /* Reset the HBA device */
5115         lpfc_reset_hba(phba);
5116 }
5117
5118 /**
5119  * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
5120  * @phba: Pointer to HBA context object.
5121  * @pmbox: Pointer to mailbox object.
5122  * @flag: Flag indicating how the mailbox need to be processed.
5123  *
5124  * This function is called by discovery code and HBA management code
5125  * to submit a mailbox command to firmware with SLI-3 interface spec. This
5126  * function gets the hbalock to protect the data structures.
5127  * The mailbox command can be submitted in polling mode, in which case
5128  * this function will wait in a polling loop for the completion of the
5129  * mailbox.
5130  * If the mailbox is submitted in no_wait mode (not polling) the
5131  * function will submit the command and returns immediately without waiting
5132  * for the mailbox completion. The no_wait is supported only when HBA
5133  * is in SLI2/SLI3 mode - interrupts are enabled.
5134  * The SLI interface allows only one mailbox pending at a time. If the
5135  * mailbox is issued in polling mode and there is already a mailbox
5136  * pending, then the function will return an error. If the mailbox is issued
5137  * in NO_WAIT mode and there is a mailbox pending already, the function
5138  * will return MBX_BUSY after queuing the mailbox into mailbox queue.
5139  * The sli layer owns the mailbox object until the completion of mailbox
5140  * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
5141  * return codes the caller owns the mailbox command after the return of
5142  * the function.
5143  **/
5144 static int
5145 lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
5146                        uint32_t flag)
5147 {
5148         MAILBOX_t *mb;
5149         struct lpfc_sli *psli = &phba->sli;
5150         uint32_t status, evtctr;
5151         uint32_t ha_copy;
5152         int i;
5153         unsigned long timeout;
5154         unsigned long drvr_flag = 0;
5155         uint32_t word0, ldata;
5156         void __iomem *to_slim;
5157         int processing_queue = 0;
5158
5159         spin_lock_irqsave(&phba->hbalock, drvr_flag);
5160         if (!pmbox) {
5161                 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5162                 /* processing mbox queue from intr_handler */
5163                 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
5164                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
5165                         return MBX_SUCCESS;
5166                 }
5167                 processing_queue = 1;
5168                 pmbox = lpfc_mbox_get(phba);
5169                 if (!pmbox) {
5170                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
5171                         return MBX_SUCCESS;
5172                 }
5173         }
5174
5175         if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
5176                 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
5177                 if(!pmbox->vport) {
5178                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
5179                         lpfc_printf_log(phba, KERN_ERR,
5180                                         LOG_MBOX | LOG_VPORT,
5181                                         "1806 Mbox x%x failed. No vport\n",
5182                                         pmbox->u.mb.mbxCommand);
5183                         dump_stack();
5184                         goto out_not_finished;
5185                 }
5186         }
5187
5188         /* If the PCI channel is in offline state, do not post mbox. */
5189         if (unlikely(pci_channel_offline(phba->pcidev))) {
5190                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
5191                 goto out_not_finished;
5192         }
5193
5194         /* If HBA has a deferred error attention, fail the iocb. */
5195         if (unlikely(phba->hba_flag & DEFER_ERATT)) {
5196                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
5197                 goto out_not_finished;
5198         }
5199
5200         psli = &phba->sli;
5201
5202         mb = &pmbox->u.mb;
5203         status = MBX_SUCCESS;
5204
5205         if (phba->link_state == LPFC_HBA_ERROR) {
5206                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
5207
5208                 /* Mbox command <mbxCommand> cannot issue */
5209                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5210                                 "(%d):0311 Mailbox command x%x cannot "
5211                                 "issue Data: x%x x%x\n",
5212                                 pmbox->vport ? pmbox->vport->vpi : 0,
5213                                 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
5214                 goto out_not_finished;
5215         }
5216
5217         if (mb->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT &&
5218             !(readl(phba->HCregaddr) & HC_MBINT_ENA)) {
5219                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
5220                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5221                                 "(%d):2528 Mailbox command x%x cannot "
5222                                 "issue Data: x%x x%x\n",
5223                                 pmbox->vport ? pmbox->vport->vpi : 0,
5224                                 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
5225                 goto out_not_finished;
5226         }
5227
5228         if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
5229                 /* Polling for a mbox command when another one is already active
5230                  * is not allowed in SLI. Also, the driver must have established
5231                  * SLI2 mode to queue and process multiple mbox commands.
5232                  */
5233
5234                 if (flag & MBX_POLL) {
5235                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
5236
5237                         /* Mbox command <mbxCommand> cannot issue */
5238                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5239                                         "(%d):2529 Mailbox command x%x "
5240                                         "cannot issue Data: x%x x%x\n",
5241                                         pmbox->vport ? pmbox->vport->vpi : 0,
5242                                         pmbox->u.mb.mbxCommand,
5243                                         psli->sli_flag, flag);
5244                         goto out_not_finished;
5245                 }
5246
5247                 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
5248                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
5249                         /* Mbox command <mbxCommand> cannot issue */
5250                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5251                                         "(%d):2530 Mailbox command x%x "
5252                                         "cannot issue Data: x%x x%x\n",
5253                                         pmbox->vport ? pmbox->vport->vpi : 0,
5254                                         pmbox->u.mb.mbxCommand,
5255                                         psli->sli_flag, flag);
5256                         goto out_not_finished;
5257                 }
5258
5259                 /* Another mailbox command is still being processed, queue this
5260                  * command to be processed later.
5261                  */
5262                 lpfc_mbox_put(phba, pmbox);
5263
5264                 /* Mbox cmd issue - BUSY */
5265                 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5266                                 "(%d):0308 Mbox cmd issue - BUSY Data: "
5267                                 "x%x x%x x%x x%x\n",
5268                                 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
5269                                 mb->mbxCommand, phba->pport->port_state,
5270                                 psli->sli_flag, flag);
5271
5272                 psli->slistat.mbox_busy++;
5273                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
5274
5275                 if (pmbox->vport) {
5276                         lpfc_debugfs_disc_trc(pmbox->vport,
5277                                 LPFC_DISC_TRC_MBOX_VPORT,
5278                                 "MBOX Bsy vport:  cmd:x%x mb:x%x x%x",
5279                                 (uint32_t)mb->mbxCommand,
5280                                 mb->un.varWords[0], mb->un.varWords[1]);
5281                 }
5282                 else {
5283                         lpfc_debugfs_disc_trc(phba->pport,
5284                                 LPFC_DISC_TRC_MBOX,
5285                                 "MBOX Bsy:        cmd:x%x mb:x%x x%x",
5286                                 (uint32_t)mb->mbxCommand,
5287                                 mb->un.varWords[0], mb->un.varWords[1]);
5288                 }
5289
5290                 return MBX_BUSY;
5291         }
5292
5293         psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
5294
5295         /* If we are not polling, we MUST be in SLI2 mode */
5296         if (flag != MBX_POLL) {
5297                 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
5298                     (mb->mbxCommand != MBX_KILL_BOARD)) {
5299                         psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5300                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
5301                         /* Mbox command <mbxCommand> cannot issue */
5302                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5303                                         "(%d):2531 Mailbox command x%x "
5304                                         "cannot issue Data: x%x x%x\n",
5305                                         pmbox->vport ? pmbox->vport->vpi : 0,
5306                                         pmbox->u.mb.mbxCommand,
5307                                         psli->sli_flag, flag);
5308                         goto out_not_finished;
5309                 }
5310                 /* timeout active mbox command */
5311                 mod_timer(&psli->mbox_tmo, (jiffies +
5312                                (HZ * lpfc_mbox_tmo_val(phba, mb->mbxCommand))));
5313         }
5314
5315         /* Mailbox cmd <cmd> issue */
5316         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5317                         "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
5318                         "x%x\n",
5319                         pmbox->vport ? pmbox->vport->vpi : 0,
5320                         mb->mbxCommand, phba->pport->port_state,
5321                         psli->sli_flag, flag);
5322
5323         if (mb->mbxCommand != MBX_HEARTBEAT) {
5324                 if (pmbox->vport) {
5325                         lpfc_debugfs_disc_trc(pmbox->vport,
5326                                 LPFC_DISC_TRC_MBOX_VPORT,
5327                                 "MBOX Send vport: cmd:x%x mb:x%x x%x",
5328                                 (uint32_t)mb->mbxCommand,
5329                                 mb->un.varWords[0], mb->un.varWords[1]);
5330                 }
5331                 else {
5332                         lpfc_debugfs_disc_trc(phba->pport,
5333                                 LPFC_DISC_TRC_MBOX,
5334                                 "MBOX Send:       cmd:x%x mb:x%x x%x",
5335                                 (uint32_t)mb->mbxCommand,
5336                                 mb->un.varWords[0], mb->un.varWords[1]);
5337                 }
5338         }
5339
5340         psli->slistat.mbox_cmd++;
5341         evtctr = psli->slistat.mbox_event;
5342
5343         /* next set own bit for the adapter and copy over command word */
5344         mb->mbxOwner = OWN_CHIP;
5345
5346         if (psli->sli_flag & LPFC_SLI_ACTIVE) {
5347                 /* Populate mbox extension offset word. */
5348                 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
5349                         *(((uint32_t *)mb) + pmbox->mbox_offset_word)
5350                                 = (uint8_t *)phba->mbox_ext
5351                                   - (uint8_t *)phba->mbox;
5352                 }
5353
5354                 /* Copy the mailbox extension data */
5355                 if (pmbox->in_ext_byte_len && pmbox->context2) {
5356                         lpfc_sli_pcimem_bcopy(pmbox->context2,
5357                                 (uint8_t *)phba->mbox_ext,
5358                                 pmbox->in_ext_byte_len);
5359                 }
5360                 /* Copy command data to host SLIM area */
5361                 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
5362         } else {
5363                 /* Populate mbox extension offset word. */
5364                 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
5365                         *(((uint32_t *)mb) + pmbox->mbox_offset_word)
5366                                 = MAILBOX_HBA_EXT_OFFSET;
5367
5368                 /* Copy the mailbox extension data */
5369                 if (pmbox->in_ext_byte_len && pmbox->context2) {
5370                         lpfc_memcpy_to_slim(phba->MBslimaddr +
5371                                 MAILBOX_HBA_EXT_OFFSET,
5372                                 pmbox->context2, pmbox->in_ext_byte_len);
5373
5374                 }
5375                 if (mb->mbxCommand == MBX_CONFIG_PORT) {
5376                         /* copy command data into host mbox for cmpl */
5377                         lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
5378                 }
5379
5380                 /* First copy mbox command data to HBA SLIM, skip past first
5381                    word */
5382                 to_slim = phba->MBslimaddr + sizeof (uint32_t);
5383                 lpfc_memcpy_to_slim(to_slim, &mb->un.varWords[0],
5384                             MAILBOX_CMD_SIZE - sizeof (uint32_t));
5385
5386                 /* Next copy over first word, with mbxOwner set */
5387                 ldata = *((uint32_t *)mb);
5388                 to_slim = phba->MBslimaddr;
5389                 writel(ldata, to_slim);
5390                 readl(to_slim); /* flush */
5391
5392                 if (mb->mbxCommand == MBX_CONFIG_PORT) {
5393                         /* switch over to host mailbox */
5394                         psli->sli_flag |= LPFC_SLI_ACTIVE;
5395                 }
5396         }
5397
5398         wmb();
5399
5400         switch (flag) {
5401         case MBX_NOWAIT:
5402                 /* Set up reference to mailbox command */
5403                 psli->mbox_active = pmbox;
5404                 /* Interrupt board to do it */
5405                 writel(CA_MBATT, phba->CAregaddr);
5406                 readl(phba->CAregaddr); /* flush */
5407                 /* Don't wait for it to finish, just return */
5408                 break;
5409
5410         case MBX_POLL:
5411                 /* Set up null reference to mailbox command */
5412                 psli->mbox_active = NULL;
5413                 /* Interrupt board to do it */
5414                 writel(CA_MBATT, phba->CAregaddr);
5415                 readl(phba->CAregaddr); /* flush */
5416
5417                 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
5418                         /* First read mbox status word */
5419                         word0 = *((uint32_t *)phba->mbox);
5420                         word0 = le32_to_cpu(word0);
5421                 } else {
5422                         /* First read mbox status word */
5423                         word0 = readl(phba->MBslimaddr);
5424                 }
5425
5426                 /* Read the HBA Host Attention Register */
5427                 ha_copy = readl(phba->HAregaddr);
5428                 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
5429                                                              mb->mbxCommand) *
5430                                            1000) + jiffies;
5431                 i = 0;
5432                 /* Wait for command to complete */
5433                 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
5434                        (!(ha_copy & HA_MBATT) &&
5435                         (phba->link_state > LPFC_WARM_START))) {
5436                         if (time_after(jiffies, timeout)) {
5437                                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5438                                 spin_unlock_irqrestore(&phba->hbalock,
5439                                                        drvr_flag);
5440                                 goto out_not_finished;
5441                         }
5442
5443                         /* Check if we took a mbox interrupt while we were
5444                            polling */
5445                         if (((word0 & OWN_CHIP) != OWN_CHIP)
5446                             && (evtctr != psli->slistat.mbox_event))
5447                                 break;
5448
5449                         if (i++ > 10) {
5450                                 spin_unlock_irqrestore(&phba->hbalock,
5451                                                        drvr_flag);
5452                                 msleep(1);
5453                                 spin_lock_irqsave(&phba->hbalock, drvr_flag);
5454                         }
5455
5456                         if (psli->sli_flag & LPFC_SLI_ACTIVE) {
5457                                 /* First copy command data */
5458                                 word0 = *((uint32_t *)phba->mbox);
5459                                 word0 = le32_to_cpu(word0);
5460                                 if (mb->mbxCommand == MBX_CONFIG_PORT) {
5461                                         MAILBOX_t *slimmb;
5462                                         uint32_t slimword0;
5463                                         /* Check real SLIM for any errors */
5464                                         slimword0 = readl(phba->MBslimaddr);
5465                                         slimmb = (MAILBOX_t *) & slimword0;
5466                                         if (((slimword0 & OWN_CHIP) != OWN_CHIP)
5467                                             && slimmb->mbxStatus) {
5468                                                 psli->sli_flag &=
5469                                                     ~LPFC_SLI_ACTIVE;
5470                                                 word0 = slimword0;
5471                                         }
5472                                 }
5473                         } else {
5474                                 /* First copy command data */
5475                                 word0 = readl(phba->MBslimaddr);
5476                         }
5477                         /* Read the HBA Host Attention Register */
5478                         ha_copy = readl(phba->HAregaddr);
5479                 }
5480
5481                 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
5482                         /* copy results back to user */
5483                         lpfc_sli_pcimem_bcopy(phba->mbox, mb, MAILBOX_CMD_SIZE);
5484                         /* Copy the mailbox extension data */
5485                         if (pmbox->out_ext_byte_len && pmbox->context2) {
5486                                 lpfc_sli_pcimem_bcopy(phba->mbox_ext,
5487                                                       pmbox->context2,
5488                                                       pmbox->out_ext_byte_len);
5489                         }
5490                 } else {
5491                         /* First copy command data */
5492                         lpfc_memcpy_from_slim(mb, phba->MBslimaddr,
5493                                                         MAILBOX_CMD_SIZE);
5494                         /* Copy the mailbox extension data */
5495                         if (pmbox->out_ext_byte_len && pmbox->context2) {
5496                                 lpfc_memcpy_from_slim(pmbox->context2,
5497                                         phba->MBslimaddr +
5498                                         MAILBOX_HBA_EXT_OFFSET,
5499                                         pmbox->out_ext_byte_len);
5500                         }
5501                 }
5502
5503                 writel(HA_MBATT, phba->HAregaddr);
5504                 readl(phba->HAregaddr); /* flush */
5505
5506                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5507                 status = mb->mbxStatus;
5508         }
5509
5510         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
5511         return status;
5512
5513 out_not_finished:
5514         if (processing_queue) {
5515                 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
5516                 lpfc_mbox_cmpl_put(phba, pmbox);
5517         }
5518         return MBX_NOT_FINISHED;
5519 }
5520
5521 /**
5522  * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
5523  * @phba: Pointer to HBA context object.
5524  *
5525  * The function blocks the posting of SLI4 asynchronous mailbox commands from
5526  * the driver internal pending mailbox queue. It will then try to wait out the
5527  * possible outstanding mailbox command before return.
5528  *
5529  * Returns:
5530  *      0 - the outstanding mailbox command completed; otherwise, the wait for
5531  *      the outstanding mailbox command timed out.
5532  **/
5533 static int
5534 lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
5535 {
5536         struct lpfc_sli *psli = &phba->sli;
5537         uint8_t actcmd = MBX_HEARTBEAT;
5538         int rc = 0;
5539         unsigned long timeout;
5540
5541         /* Mark the asynchronous mailbox command posting as blocked */
5542         spin_lock_irq(&phba->hbalock);
5543         psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
5544         if (phba->sli.mbox_active)
5545                 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
5546         spin_unlock_irq(&phba->hbalock);
5547         /* Determine how long we might wait for the active mailbox
5548          * command to be gracefully completed by firmware.
5549          */
5550         timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, actcmd) * 1000) +
5551                                    jiffies;
5552         /* Wait for the outstnading mailbox command to complete */
5553         while (phba->sli.mbox_active) {
5554                 /* Check active mailbox complete status every 2ms */
5555                 msleep(2);
5556                 if (time_after(jiffies, timeout)) {
5557                         /* Timeout, marked the outstanding cmd not complete */
5558                         rc = 1;
5559                         break;
5560                 }
5561         }
5562
5563         /* Can not cleanly block async mailbox command, fails it */
5564         if (rc) {
5565                 spin_lock_irq(&phba->hbalock);
5566                 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
5567                 spin_unlock_irq(&phba->hbalock);
5568         }
5569         return rc;
5570 }
5571
5572 /**
5573  * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
5574  * @phba: Pointer to HBA context object.
5575  *
5576  * The function unblocks and resume posting of SLI4 asynchronous mailbox
5577  * commands from the driver internal pending mailbox queue. It makes sure
5578  * that there is no outstanding mailbox command before resuming posting
5579  * asynchronous mailbox commands. If, for any reason, there is outstanding
5580  * mailbox command, it will try to wait it out before resuming asynchronous
5581  * mailbox command posting.
5582  **/
5583 static void
5584 lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
5585 {
5586         struct lpfc_sli *psli = &phba->sli;
5587
5588         spin_lock_irq(&phba->hbalock);
5589         if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
5590                 /* Asynchronous mailbox posting is not blocked, do nothing */
5591                 spin_unlock_irq(&phba->hbalock);
5592                 return;
5593         }
5594
5595         /* Outstanding synchronous mailbox command is guaranteed to be done,
5596          * successful or timeout, after timing-out the outstanding mailbox
5597          * command shall always be removed, so just unblock posting async
5598          * mailbox command and resume
5599          */
5600         psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
5601         spin_unlock_irq(&phba->hbalock);
5602
5603         /* wake up worker thread to post asynchronlous mailbox command */
5604         lpfc_worker_wake_up(phba);
5605 }
5606
5607 /**
5608  * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
5609  * @phba: Pointer to HBA context object.
5610  * @mboxq: Pointer to mailbox object.
5611  *
5612  * The function posts a mailbox to the port.  The mailbox is expected
5613  * to be comletely filled in and ready for the port to operate on it.
5614  * This routine executes a synchronous completion operation on the
5615  * mailbox by polling for its completion.
5616  *
5617  * The caller must not be holding any locks when calling this routine.
5618  *
5619  * Returns:
5620  *      MBX_SUCCESS - mailbox posted successfully
5621  *      Any of the MBX error values.
5622  **/
5623 static int
5624 lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
5625 {
5626         int rc = MBX_SUCCESS;
5627         unsigned long iflag;
5628         uint32_t db_ready;
5629         uint32_t mcqe_status;
5630         uint32_t mbx_cmnd;
5631         unsigned long timeout;
5632         struct lpfc_sli *psli = &phba->sli;
5633         struct lpfc_mqe *mb = &mboxq->u.mqe;
5634         struct lpfc_bmbx_create *mbox_rgn;
5635         struct dma_address *dma_address;
5636         struct lpfc_register bmbx_reg;
5637
5638         /*
5639          * Only one mailbox can be active to the bootstrap mailbox region
5640          * at a time and there is no queueing provided.
5641          */
5642         spin_lock_irqsave(&phba->hbalock, iflag);
5643         if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
5644                 spin_unlock_irqrestore(&phba->hbalock, iflag);
5645                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5646                                 "(%d):2532 Mailbox command x%x (x%x) "
5647                                 "cannot issue Data: x%x x%x\n",
5648                                 mboxq->vport ? mboxq->vport->vpi : 0,
5649                                 mboxq->u.mb.mbxCommand,
5650                                 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5651                                 psli->sli_flag, MBX_POLL);
5652                 return MBXERR_ERROR;
5653         }
5654         /* The server grabs the token and owns it until release */
5655         psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
5656         phba->sli.mbox_active = mboxq;
5657         spin_unlock_irqrestore(&phba->hbalock, iflag);
5658
5659         /*
5660          * Initialize the bootstrap memory region to avoid stale data areas
5661          * in the mailbox post.  Then copy the caller's mailbox contents to
5662          * the bmbx mailbox region.
5663          */
5664         mbx_cmnd = bf_get(lpfc_mqe_command, mb);
5665         memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
5666         lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
5667                               sizeof(struct lpfc_mqe));
5668
5669         /* Post the high mailbox dma address to the port and wait for ready. */
5670         dma_address = &phba->sli4_hba.bmbx.dma_address;
5671         writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
5672
5673         timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mbx_cmnd)
5674                                    * 1000) + jiffies;
5675         do {
5676                 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
5677                 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
5678                 if (!db_ready)
5679                         msleep(2);
5680
5681                 if (time_after(jiffies, timeout)) {
5682                         rc = MBXERR_ERROR;
5683                         goto exit;
5684                 }
5685         } while (!db_ready);
5686
5687         /* Post the low mailbox dma address to the port. */
5688         writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
5689         timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mbx_cmnd)
5690                                    * 1000) + jiffies;
5691         do {
5692                 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
5693                 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
5694                 if (!db_ready)
5695                         msleep(2);
5696
5697                 if (time_after(jiffies, timeout)) {
5698                         rc = MBXERR_ERROR;
5699                         goto exit;
5700                 }
5701         } while (!db_ready);
5702
5703         /*
5704          * Read the CQ to ensure the mailbox has completed.
5705          * If so, update the mailbox status so that the upper layers
5706          * can complete the request normally.
5707          */
5708         lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
5709                               sizeof(struct lpfc_mqe));
5710         mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
5711         lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
5712                               sizeof(struct lpfc_mcqe));
5713         mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
5714
5715         /* Prefix the mailbox status with range x4000 to note SLI4 status. */
5716         if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
5717                 bf_set(lpfc_mqe_status, mb, LPFC_MBX_ERROR_RANGE | mcqe_status);
5718                 rc = MBXERR_ERROR;
5719         } else
5720                 lpfc_sli4_swap_str(phba, mboxq);
5721
5722         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5723                         "(%d):0356 Mailbox cmd x%x (x%x) Status x%x "
5724                         "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
5725                         " x%x x%x CQ: x%x x%x x%x x%x\n",
5726                         mboxq->vport ? mboxq->vport->vpi : 0,
5727                         mbx_cmnd, lpfc_sli4_mbox_opcode_get(phba, mboxq),
5728                         bf_get(lpfc_mqe_status, mb),
5729                         mb->un.mb_words[0], mb->un.mb_words[1],
5730                         mb->un.mb_words[2], mb->un.mb_words[3],
5731                         mb->un.mb_words[4], mb->un.mb_words[5],
5732                         mb->un.mb_words[6], mb->un.mb_words[7],
5733                         mb->un.mb_words[8], mb->un.mb_words[9],
5734                         mb->un.mb_words[10], mb->un.mb_words[11],
5735                         mb->un.mb_words[12], mboxq->mcqe.word0,
5736                         mboxq->mcqe.mcqe_tag0,  mboxq->mcqe.mcqe_tag1,
5737                         mboxq->mcqe.trailer);
5738 exit:
5739         /* We are holding the token, no needed for lock when release */
5740         spin_lock_irqsave(&phba->hbalock, iflag);
5741         psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5742         phba->sli.mbox_active = NULL;
5743         spin_unlock_irqrestore(&phba->hbalock, iflag);
5744         return rc;
5745 }
5746
5747 /**
5748  * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
5749  * @phba: Pointer to HBA context object.
5750  * @pmbox: Pointer to mailbox object.
5751  * @flag: Flag indicating how the mailbox need to be processed.
5752  *
5753  * This function is called by discovery code and HBA management code to submit
5754  * a mailbox command to firmware with SLI-4 interface spec.
5755  *
5756  * Return codes the caller owns the mailbox command after the return of the
5757  * function.
5758  **/
5759 static int
5760 lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
5761                        uint32_t flag)
5762 {
5763         struct lpfc_sli *psli = &phba->sli;
5764         unsigned long iflags;
5765         int rc;
5766
5767         rc = lpfc_mbox_dev_check(phba);
5768         if (unlikely(rc)) {
5769                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5770                                 "(%d):2544 Mailbox command x%x (x%x) "
5771                                 "cannot issue Data: x%x x%x\n",
5772                                 mboxq->vport ? mboxq->vport->vpi : 0,
5773                                 mboxq->u.mb.mbxCommand,
5774                                 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5775                                 psli->sli_flag, flag);
5776                 goto out_not_finished;
5777         }
5778
5779         /* Detect polling mode and jump to a handler */
5780         if (!phba->sli4_hba.intr_enable) {
5781                 if (flag == MBX_POLL)
5782                         rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
5783                 else
5784                         rc = -EIO;
5785                 if (rc != MBX_SUCCESS)
5786                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5787                                         "(%d):2541 Mailbox command x%x "
5788                                         "(x%x) cannot issue Data: x%x x%x\n",
5789                                         mboxq->vport ? mboxq->vport->vpi : 0,
5790                                         mboxq->u.mb.mbxCommand,
5791                                         lpfc_sli4_mbox_opcode_get(phba, mboxq),
5792                                         psli->sli_flag, flag);
5793                 return rc;
5794         } else if (flag == MBX_POLL) {
5795                 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
5796                                 "(%d):2542 Try to issue mailbox command "
5797                                 "x%x (x%x) synchronously ahead of async"
5798                                 "mailbox command queue: x%x x%x\n",
5799                                 mboxq->vport ? mboxq->vport->vpi : 0,
5800                                 mboxq->u.mb.mbxCommand,
5801                                 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5802                                 psli->sli_flag, flag);
5803                 /* Try to block the asynchronous mailbox posting */
5804                 rc = lpfc_sli4_async_mbox_block(phba);
5805                 if (!rc) {
5806                         /* Successfully blocked, now issue sync mbox cmd */
5807                         rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
5808                         if (rc != MBX_SUCCESS)
5809                                 lpfc_printf_log(phba, KERN_ERR,
5810                                                 LOG_MBOX | LOG_SLI,
5811                                                 "(%d):2597 Mailbox command "
5812                                                 "x%x (x%x) cannot issue "
5813                                                 "Data: x%x x%x\n",
5814                                                 mboxq->vport ?
5815                                                 mboxq->vport->vpi : 0,
5816                                                 mboxq->u.mb.mbxCommand,
5817                                                 lpfc_sli4_mbox_opcode_get(phba,
5818                                                                 mboxq),
5819                                                 psli->sli_flag, flag);
5820                         /* Unblock the async mailbox posting afterward */
5821                         lpfc_sli4_async_mbox_unblock(phba);
5822                 }
5823                 return rc;
5824         }
5825
5826         /* Now, interrupt mode asynchrous mailbox command */
5827         rc = lpfc_mbox_cmd_check(phba, mboxq);
5828         if (rc) {
5829                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5830                                 "(%d):2543 Mailbox command x%x (x%x) "
5831                                 "cannot issue Data: x%x x%x\n",
5832                                 mboxq->vport ? mboxq->vport->vpi : 0,
5833                                 mboxq->u.mb.mbxCommand,
5834                                 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5835                                 psli->sli_flag, flag);
5836                 goto out_not_finished;
5837         }
5838
5839         /* Put the mailbox command to the driver internal FIFO */
5840         psli->slistat.mbox_busy++;
5841         spin_lock_irqsave(&phba->hbalock, iflags);
5842         lpfc_mbox_put(phba, mboxq);
5843         spin_unlock_irqrestore(&phba->hbalock, iflags);
5844         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5845                         "(%d):0354 Mbox cmd issue - Enqueue Data: "
5846                         "x%x (x%x) x%x x%x x%x\n",
5847                         mboxq->vport ? mboxq->vport->vpi : 0xffffff,
5848                         bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5849                         lpfc_sli4_mbox_opcode_get(phba, mboxq),
5850                         phba->pport->port_state,
5851                         psli->sli_flag, MBX_NOWAIT);
5852         /* Wake up worker thread to transport mailbox command from head */
5853         lpfc_worker_wake_up(phba);
5854
5855         return MBX_BUSY;
5856
5857 out_not_finished:
5858         return MBX_NOT_FINISHED;
5859 }
5860
5861 /**
5862  * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
5863  * @phba: Pointer to HBA context object.
5864  *
5865  * This function is called by worker thread to send a mailbox command to
5866  * SLI4 HBA firmware.
5867  *
5868  **/
5869 int
5870 lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
5871 {
5872         struct lpfc_sli *psli = &phba->sli;
5873         LPFC_MBOXQ_t *mboxq;
5874         int rc = MBX_SUCCESS;
5875         unsigned long iflags;
5876         struct lpfc_mqe *mqe;
5877         uint32_t mbx_cmnd;
5878
5879         /* Check interrupt mode before post async mailbox command */
5880         if (unlikely(!phba->sli4_hba.intr_enable))
5881                 return MBX_NOT_FINISHED;
5882
5883         /* Check for mailbox command service token */
5884         spin_lock_irqsave(&phba->hbalock, iflags);
5885         if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
5886                 spin_unlock_irqrestore(&phba->hbalock, iflags);
5887                 return MBX_NOT_FINISHED;
5888         }
5889         if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
5890                 spin_unlock_irqrestore(&phba->hbalock, iflags);
5891                 return MBX_NOT_FINISHED;
5892         }
5893         if (unlikely(phba->sli.mbox_active)) {
5894                 spin_unlock_irqrestore(&phba->hbalock, iflags);
5895                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5896                                 "0384 There is pending active mailbox cmd\n");
5897                 return MBX_NOT_FINISHED;
5898         }
5899         /* Take the mailbox command service token */
5900         psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
5901
5902         /* Get the next mailbox command from head of queue */
5903         mboxq = lpfc_mbox_get(phba);
5904
5905         /* If no more mailbox command waiting for post, we're done */
5906         if (!mboxq) {
5907                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5908                 spin_unlock_irqrestore(&phba->hbalock, iflags);
5909                 return MBX_SUCCESS;
5910         }
5911         phba->sli.mbox_active = mboxq;
5912         spin_unlock_irqrestore(&phba->hbalock, iflags);
5913
5914         /* Check device readiness for posting mailbox command */
5915         rc = lpfc_mbox_dev_check(phba);
5916         if (unlikely(rc))
5917                 /* Driver clean routine will clean up pending mailbox */
5918                 goto out_not_finished;
5919
5920         /* Prepare the mbox command to be posted */
5921         mqe = &mboxq->u.mqe;
5922         mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
5923
5924         /* Start timer for the mbox_tmo and log some mailbox post messages */
5925         mod_timer(&psli->mbox_tmo, (jiffies +
5926                   (HZ * lpfc_mbox_tmo_val(phba, mbx_cmnd))));
5927
5928         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5929                         "(%d):0355 Mailbox cmd x%x (x%x) issue Data: "
5930                         "x%x x%x\n",
5931                         mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
5932                         lpfc_sli4_mbox_opcode_get(phba, mboxq),
5933                         phba->pport->port_state, psli->sli_flag);
5934
5935         if (mbx_cmnd != MBX_HEARTBEAT) {
5936                 if (mboxq->vport) {
5937                         lpfc_debugfs_disc_trc(mboxq->vport,
5938                                 LPFC_DISC_TRC_MBOX_VPORT,
5939                                 "MBOX Send vport: cmd:x%x mb:x%x x%x",
5940                                 mbx_cmnd, mqe->un.mb_words[0],
5941                                 mqe->un.mb_words[1]);
5942                 } else {
5943                         lpfc_debugfs_disc_trc(phba->pport,
5944                                 LPFC_DISC_TRC_MBOX,
5945                                 "MBOX Send: cmd:x%x mb:x%x x%x",
5946                                 mbx_cmnd, mqe->un.mb_words[0],
5947                                 mqe->un.mb_words[1]);
5948                 }
5949         }
5950         psli->slistat.mbox_cmd++;
5951
5952         /* Post the mailbox command to the port */
5953         rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
5954         if (rc != MBX_SUCCESS) {
5955                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5956                                 "(%d):2533 Mailbox command x%x (x%x) "
5957                                 "cannot issue Data: x%x x%x\n",
5958                                 mboxq->vport ? mboxq->vport->vpi : 0,
5959                                 mboxq->u.mb.mbxCommand,
5960                                 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5961                                 psli->sli_flag, MBX_NOWAIT);
5962                 goto out_not_finished;
5963         }
5964
5965         return rc;
5966
5967 out_not_finished:
5968         spin_lock_irqsave(&phba->hbalock, iflags);
5969         mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
5970         __lpfc_mbox_cmpl_put(phba, mboxq);
5971         /* Release the token */
5972         psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5973         phba->sli.mbox_active = NULL;
5974         spin_unlock_irqrestore(&phba->hbalock, iflags);
5975
5976         return MBX_NOT_FINISHED;
5977 }
5978
5979 /**
5980  * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
5981  * @phba: Pointer to HBA context object.
5982  * @pmbox: Pointer to mailbox object.
5983  * @flag: Flag indicating how the mailbox need to be processed.
5984  *
5985  * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
5986  * the API jump table function pointer from the lpfc_hba struct.
5987  *
5988  * Return codes the caller owns the mailbox command after the return of the
5989  * function.
5990  **/
5991 int
5992 lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
5993 {
5994         return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
5995 }
5996
5997 /**
5998  * lpfc_mbox_api_table_setup - Set up mbox api fucntion jump table
5999  * @phba: The hba struct for which this call is being executed.
6000  * @dev_grp: The HBA PCI-Device group number.
6001  *
6002  * This routine sets up the mbox interface API function jump table in @phba
6003  * struct.
6004  * Returns: 0 - success, -ENODEV - failure.
6005  **/
6006 int
6007 lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
6008 {
6009
6010         switch (dev_grp) {
6011         case LPFC_PCI_DEV_LP:
6012                 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
6013                 phba->lpfc_sli_handle_slow_ring_event =
6014                                 lpfc_sli_handle_slow_ring_event_s3;
6015                 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
6016                 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
6017                 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
6018                 break;
6019         case LPFC_PCI_DEV_OC:
6020                 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
6021                 phba->lpfc_sli_handle_slow_ring_event =
6022                                 lpfc_sli_handle_slow_ring_event_s4;
6023                 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
6024                 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
6025                 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
6026                 break;
6027         default:
6028                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6029                                 "1420 Invalid HBA PCI-device group: 0x%x\n",
6030                                 dev_grp);
6031                 return -ENODEV;
6032                 break;
6033         }
6034         return 0;
6035 }
6036
6037 /**
6038  * __lpfc_sli_ringtx_put - Add an iocb to the txq
6039  * @phba: Pointer to HBA context object.
6040  * @pring: Pointer to driver SLI ring object.
6041  * @piocb: Pointer to address of newly added command iocb.
6042  *
6043  * This function is called with hbalock held to add a command
6044  * iocb to the txq when SLI layer cannot submit the command iocb
6045  * to the ring.
6046  **/
6047 void
6048 __lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
6049                     struct lpfc_iocbq *piocb)
6050 {
6051         /* Insert the caller's iocb in the txq tail for later processing. */
6052         list_add_tail(&piocb->list, &pring->txq);
6053         pring->txq_cnt++;
6054 }
6055
6056 /**
6057  * lpfc_sli_next_iocb - Get the next iocb in the txq
6058  * @phba: Pointer to HBA context object.
6059  * @pring: Pointer to driver SLI ring object.
6060  * @piocb: Pointer to address of newly added command iocb.
6061  *
6062  * This function is called with hbalock held before a new
6063  * iocb is submitted to the firmware. This function checks
6064  * txq to flush the iocbs in txq to Firmware before
6065  * submitting new iocbs to the Firmware.
6066  * If there are iocbs in the txq which need to be submitted
6067  * to firmware, lpfc_sli_next_iocb returns the first element
6068  * of the txq after dequeuing it from txq.
6069  * If there is no iocb in the txq then the function will return
6070  * *piocb and *piocb is set to NULL. Caller needs to check
6071  * *piocb to find if there are more commands in the txq.
6072  **/
6073 static struct lpfc_iocbq *
6074 lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
6075                    struct lpfc_iocbq **piocb)
6076 {
6077         struct lpfc_iocbq * nextiocb;
6078
6079         nextiocb = lpfc_sli_ringtx_get(phba, pring);
6080         if (!nextiocb) {
6081                 nextiocb = *piocb;
6082                 *piocb = NULL;
6083         }
6084
6085         return nextiocb;
6086 }
6087
6088 /**
6089  * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
6090  * @phba: Pointer to HBA context object.
6091  * @ring_number: SLI ring number to issue iocb on.
6092  * @piocb: Pointer to command iocb.
6093  * @flag: Flag indicating if this command can be put into txq.
6094  *
6095  * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
6096  * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
6097  * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
6098  * flag is turned on, the function returns IOCB_ERROR. When the link is down,
6099  * this function allows only iocbs for posting buffers. This function finds
6100  * next available slot in the command ring and posts the command to the
6101  * available slot and writes the port attention register to request HBA start
6102  * processing new iocb. If there is no slot available in the ring and
6103  * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
6104  * the function returns IOCB_BUSY.
6105  *
6106  * This function is called with hbalock held. The function will return success
6107  * after it successfully submit the iocb to firmware or after adding to the
6108  * txq.
6109  **/
6110 static int
6111 __lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
6112                     struct lpfc_iocbq *piocb, uint32_t flag)
6113 {
6114         struct lpfc_iocbq *nextiocb;
6115         IOCB_t *iocb;
6116         struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
6117
6118         if (piocb->iocb_cmpl && (!piocb->vport) &&
6119            (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
6120            (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
6121                 lpfc_printf_log(phba, KERN_ERR,
6122                                 LOG_SLI | LOG_VPORT,
6123                                 "1807 IOCB x%x failed. No vport\n",
6124                                 piocb->iocb.ulpCommand);
6125                 dump_stack();
6126                 return IOCB_ERROR;
6127         }
6128
6129
6130         /* If the PCI channel is in offline state, do not post iocbs. */
6131         if (unlikely(pci_channel_offline(phba->pcidev)))
6132                 return IOCB_ERROR;
6133
6134         /* If HBA has a deferred error attention, fail the iocb. */
6135         if (unlikely(phba->hba_flag & DEFER_ERATT))
6136                 return IOCB_ERROR;
6137
6138         /*
6139          * We should never get an IOCB if we are in a < LINK_DOWN state
6140          */
6141         if (unlikely(phba->link_state < LPFC_LINK_DOWN))
6142                 return IOCB_ERROR;
6143
6144         /*
6145          * Check to see if we are blocking IOCB processing because of a
6146          * outstanding event.
6147          */
6148         if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
6149                 goto iocb_busy;
6150
6151         if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
6152                 /*
6153                  * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
6154                  * can be issued if the link is not up.
6155                  */
6156                 switch (piocb->iocb.ulpCommand) {
6157                 case CMD_GEN_REQUEST64_CR:
6158                 case CMD_GEN_REQUEST64_CX:
6159                         if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
6160                                 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
6161                                         FC_RCTL_DD_UNSOL_CMD) ||
6162                                 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
6163                                         MENLO_TRANSPORT_TYPE))
6164
6165                                 goto iocb_busy;
6166                         break;
6167                 case CMD_QUE_RING_BUF_CN:
6168                 case CMD_QUE_RING_BUF64_CN:
6169                         /*
6170                          * For IOCBs, like QUE_RING_BUF, that have no rsp ring
6171                          * completion, iocb_cmpl MUST be 0.
6172                          */
6173                         if (piocb->iocb_cmpl)
6174                                 piocb->iocb_cmpl = NULL;
6175                         /*FALLTHROUGH*/
6176                 case CMD_CREATE_XRI_CR:
6177                 case CMD_CLOSE_XRI_CN:
6178                 case CMD_CLOSE_XRI_CX:
6179                         break;
6180                 default:
6181                         goto iocb_busy;
6182                 }
6183
6184         /*
6185          * For FCP commands, we must be in a state where we can process link
6186          * attention events.
6187          */
6188         } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
6189                             !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
6190                 goto iocb_busy;
6191         }
6192
6193         while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
6194                (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
6195                 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
6196
6197         if (iocb)
6198                 lpfc_sli_update_ring(phba, pring);
6199         else
6200                 lpfc_sli_update_full_ring(phba, pring);
6201
6202         if (!piocb)
6203                 return IOCB_SUCCESS;
6204
6205         goto out_busy;
6206
6207  iocb_busy:
6208         pring->stats.iocb_cmd_delay++;
6209
6210  out_busy:
6211
6212         if (!(flag & SLI_IOCB_RET_IOCB)) {
6213                 __lpfc_sli_ringtx_put(phba, pring, piocb);
6214                 return IOCB_SUCCESS;
6215         }
6216
6217         return IOCB_BUSY;
6218 }
6219
6220 /**
6221  * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
6222  * @phba: Pointer to HBA context object.
6223  * @piocb: Pointer to command iocb.
6224  * @sglq: Pointer to the scatter gather queue object.
6225  *
6226  * This routine converts the bpl or bde that is in the IOCB
6227  * to a sgl list for the sli4 hardware. The physical address
6228  * of the bpl/bde is converted back to a virtual address.
6229  * If the IOCB contains a BPL then the list of BDE's is
6230  * converted to sli4_sge's. If the IOCB contains a single
6231  * BDE then it is converted to a single sli_sge.
6232  * The IOCB is still in cpu endianess so the contents of
6233  * the bpl can be used without byte swapping.
6234  *
6235  * Returns valid XRI = Success, NO_XRI = Failure.
6236 **/
6237 static uint16_t
6238 lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
6239                 struct lpfc_sglq *sglq)
6240 {
6241         uint16_t xritag = NO_XRI;
6242         struct ulp_bde64 *bpl = NULL;
6243         struct ulp_bde64 bde;
6244         struct sli4_sge *sgl  = NULL;
6245         IOCB_t *icmd;
6246         int numBdes = 0;
6247         int i = 0;
6248         uint32_t offset = 0; /* accumulated offset in the sg request list */
6249         int inbound = 0; /* number of sg reply entries inbound from firmware */
6250
6251         if (!piocbq || !sglq)
6252                 return xritag;
6253
6254         sgl  = (struct sli4_sge *)sglq->sgl;
6255         icmd = &piocbq->iocb;
6256         if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
6257                 numBdes = icmd->un.genreq64.bdl.bdeSize /
6258                                 sizeof(struct ulp_bde64);
6259                 /* The addrHigh and addrLow fields within the IOCB
6260                  * have not been byteswapped yet so there is no
6261                  * need to swap them back.
6262                  */
6263                 bpl  = (struct ulp_bde64 *)
6264                         ((struct lpfc_dmabuf *)piocbq->context3)->virt;
6265
6266                 if (!bpl)
6267                         return xritag;
6268
6269                 for (i = 0; i < numBdes; i++) {
6270                         /* Should already be byte swapped. */
6271                         sgl->addr_hi = bpl->addrHigh;
6272                         sgl->addr_lo = bpl->addrLow;
6273
6274                         if ((i+1) == numBdes)
6275                                 bf_set(lpfc_sli4_sge_last, sgl, 1);
6276                         else
6277                                 bf_set(lpfc_sli4_sge_last, sgl, 0);
6278                         sgl->word2 = cpu_to_le32(sgl->word2);
6279                         /* swap the size field back to the cpu so we
6280                          * can assign it to the sgl.
6281                          */
6282                         bde.tus.w = le32_to_cpu(bpl->tus.w);
6283                         sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
6284                         /* The offsets in the sgl need to be accumulated
6285                          * separately for the request and reply lists.
6286                          * The request is always first, the reply follows.
6287                          */
6288                         if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
6289                                 /* add up the reply sg entries */
6290                                 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
6291                                         inbound++;
6292                                 /* first inbound? reset the offset */
6293                                 if (inbound == 1)
6294                                         offset = 0;
6295                                 bf_set(lpfc_sli4_sge_offset, sgl, offset);
6296                                 offset += bde.tus.f.bdeSize;
6297                         }
6298                         bpl++;
6299                         sgl++;
6300                 }
6301         } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
6302                         /* The addrHigh and addrLow fields of the BDE have not
6303                          * been byteswapped yet so they need to be swapped
6304                          * before putting them in the sgl.
6305                          */
6306                         sgl->addr_hi =
6307                                 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
6308                         sgl->addr_lo =
6309                                 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
6310                         bf_set(lpfc_sli4_sge_last, sgl, 1);
6311                         sgl->word2 = cpu_to_le32(sgl->word2);
6312                         sgl->sge_len =
6313                                 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
6314         }
6315         return sglq->sli4_xritag;
6316 }
6317
6318 /**
6319  * lpfc_sli4_scmd_to_wqidx_distr - scsi command to SLI4 WQ index distribution
6320  * @phba: Pointer to HBA context object.
6321  *
6322  * This routine performs a roundrobin SCSI command to SLI4 FCP WQ index
6323  * distribution.  This is called by __lpfc_sli_issue_iocb_s4() with the hbalock
6324  * held.
6325  *
6326  * Return: index into SLI4 fast-path FCP queue index.
6327  **/
6328 static uint32_t
6329 lpfc_sli4_scmd_to_wqidx_distr(struct lpfc_hba *phba)
6330 {
6331         ++phba->fcp_qidx;
6332         if (phba->fcp_qidx >= phba->cfg_fcp_wq_count)
6333                 phba->fcp_qidx = 0;
6334
6335         return phba->fcp_qidx;
6336 }
6337
6338 /**
6339  * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
6340  * @phba: Pointer to HBA context object.
6341  * @piocb: Pointer to command iocb.
6342  * @wqe: Pointer to the work queue entry.
6343  *
6344  * This routine converts the iocb command to its Work Queue Entry
6345  * equivalent. The wqe pointer should not have any fields set when
6346  * this routine is called because it will memcpy over them.
6347  * This routine does not set the CQ_ID or the WQEC bits in the
6348  * wqe.
6349  *
6350  * Returns: 0 = Success, IOCB_ERROR = Failure.
6351  **/
6352 static int
6353 lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
6354                 union lpfc_wqe *wqe)
6355 {
6356         uint32_t xmit_len = 0, total_len = 0;
6357         uint8_t ct = 0;
6358         uint32_t fip;
6359         uint32_t abort_tag;
6360         uint8_t command_type = ELS_COMMAND_NON_FIP;
6361         uint8_t cmnd;
6362         uint16_t xritag;
6363         uint16_t abrt_iotag;
6364         struct lpfc_iocbq *abrtiocbq;
6365         struct ulp_bde64 *bpl = NULL;
6366         uint32_t els_id = LPFC_ELS_ID_DEFAULT;
6367         int numBdes, i;
6368         struct ulp_bde64 bde;
6369
6370         fip = phba->hba_flag & HBA_FIP_SUPPORT;
6371         /* The fcp commands will set command type */
6372         if (iocbq->iocb_flag &  LPFC_IO_FCP)
6373                 command_type = FCP_COMMAND;
6374         else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
6375                 command_type = ELS_COMMAND_FIP;
6376         else
6377                 command_type = ELS_COMMAND_NON_FIP;
6378
6379         /* Some of the fields are in the right position already */
6380         memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
6381         abort_tag = (uint32_t) iocbq->iotag;
6382         xritag = iocbq->sli4_xritag;
6383         wqe->generic.wqe_com.word7 = 0; /* The ct field has moved so reset */
6384         /* words0-2 bpl convert bde */
6385         if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
6386                 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
6387                                 sizeof(struct ulp_bde64);
6388                 bpl  = (struct ulp_bde64 *)
6389                         ((struct lpfc_dmabuf *)iocbq->context3)->virt;
6390                 if (!bpl)
6391                         return IOCB_ERROR;
6392
6393                 /* Should already be byte swapped. */
6394                 wqe->generic.bde.addrHigh =  le32_to_cpu(bpl->addrHigh);
6395                 wqe->generic.bde.addrLow =  le32_to_cpu(bpl->addrLow);
6396                 /* swap the size field back to the cpu so we
6397                  * can assign it to the sgl.
6398                  */
6399                 wqe->generic.bde.tus.w  = le32_to_cpu(bpl->tus.w);
6400                 xmit_len = wqe->generic.bde.tus.f.bdeSize;
6401                 total_len = 0;
6402                 for (i = 0; i < numBdes; i++) {
6403                         bde.tus.w  = le32_to_cpu(bpl[i].tus.w);
6404                         total_len += bde.tus.f.bdeSize;
6405                 }
6406         } else
6407                 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
6408
6409         iocbq->iocb.ulpIoTag = iocbq->iotag;
6410         cmnd = iocbq->iocb.ulpCommand;
6411
6412         switch (iocbq->iocb.ulpCommand) {
6413         case CMD_ELS_REQUEST64_CR:
6414                 if (!iocbq->iocb.ulpLe) {
6415                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6416                                 "2007 Only Limited Edition cmd Format"
6417                                 " supported 0x%x\n",
6418                                 iocbq->iocb.ulpCommand);
6419                         return IOCB_ERROR;
6420                 }
6421                 wqe->els_req.payload_len = xmit_len;
6422                 /* Els_reguest64 has a TMO */
6423                 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
6424                         iocbq->iocb.ulpTimeout);
6425                 /* Need a VF for word 4 set the vf bit*/
6426                 bf_set(els_req64_vf, &wqe->els_req, 0);
6427                 /* And a VFID for word 12 */
6428                 bf_set(els_req64_vfid, &wqe->els_req, 0);
6429                 /*
6430                  * Set ct field to 3, indicates that the context_tag field
6431                  * contains the FCFI and remote N_Port_ID is
6432                  * in word 5.
6433                  */
6434                 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
6435                 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
6436                        iocbq->iocb.ulpContext);
6437                 bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
6438                 bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
6439                 /* CCP CCPE PV PRI in word10 were set in the memcpy */
6440                 if (command_type == ELS_COMMAND_FIP) {
6441                         els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
6442                                         >> LPFC_FIP_ELS_ID_SHIFT);
6443                 }
6444                 bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
6445                 bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
6446                 bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
6447                 bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
6448                 bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
6449                 bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
6450         break;
6451         case CMD_XMIT_SEQUENCE64_CX:
6452                 bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
6453                        iocbq->iocb.un.ulpWord[3]);
6454                 bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
6455                        iocbq->iocb.ulpContext);
6456                 /* The entire sequence is transmitted for this IOCB */
6457                 xmit_len = total_len;
6458                 cmnd = CMD_XMIT_SEQUENCE64_CR;
6459         case CMD_XMIT_SEQUENCE64_CR:
6460                 /* word3 iocb=io_tag32 wqe=reserved */
6461                 wqe->xmit_sequence.rsvd3 = 0;
6462                 /* word4 relative_offset memcpy */
6463                 /* word5 r_ctl/df_ctl memcpy */
6464                 bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
6465                 bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
6466                 bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
6467                        LPFC_WQE_IOD_WRITE);
6468                 bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
6469                        LPFC_WQE_LENLOC_WORD12);
6470                 bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
6471                 wqe->xmit_sequence.xmit_len = xmit_len;
6472                 command_type = OTHER_COMMAND;
6473         break;
6474         case CMD_XMIT_BCAST64_CN:
6475                 /* word3 iocb=iotag32 wqe=seq_payload_len */
6476                 wqe->xmit_bcast64.seq_payload_len = xmit_len;
6477                 /* word4 iocb=rsvd wqe=rsvd */
6478                 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
6479                 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
6480                 bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
6481                         ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
6482                 bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
6483                 bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
6484                 bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
6485                        LPFC_WQE_LENLOC_WORD3);
6486                 bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
6487         break;
6488         case CMD_FCP_IWRITE64_CR:
6489                 command_type = FCP_COMMAND_DATA_OUT;
6490                 /* word3 iocb=iotag wqe=payload_offset_len */
6491                 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
6492                 wqe->fcp_iwrite.payload_offset_len =
6493                         xmit_len + sizeof(struct fcp_rsp);
6494                 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
6495                 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
6496                 bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
6497                        iocbq->iocb.ulpFCP2Rcvy);
6498                 bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
6499                 /* Always open the exchange */
6500                 bf_set(wqe_xc, &wqe->fcp_iwrite.wqe_com, 0);
6501                 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
6502                 bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
6503                 bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
6504                        LPFC_WQE_LENLOC_WORD4);
6505                 bf_set(wqe_ebde_cnt, &wqe->fcp_iwrite.wqe_com, 0);
6506                 bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
6507         break;
6508         case CMD_FCP_IREAD64_CR:
6509                 /* word3 iocb=iotag wqe=payload_offset_len */
6510                 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
6511                 wqe->fcp_iread.payload_offset_len =
6512                         xmit_len + sizeof(struct fcp_rsp);
6513                 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
6514                 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
6515                 bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
6516                        iocbq->iocb.ulpFCP2Rcvy);
6517                 bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
6518                 /* Always open the exchange */
6519                 bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
6520                 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
6521                 bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
6522                 bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
6523                        LPFC_WQE_LENLOC_WORD4);
6524                 bf_set(wqe_ebde_cnt, &wqe->fcp_iread.wqe_com, 0);
6525                 bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
6526         break;
6527         case CMD_FCP_ICMND64_CR:
6528                 /* word3 iocb=IO_TAG wqe=reserved */
6529                 wqe->fcp_icmd.rsrvd3 = 0;
6530                 bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
6531                 /* Always open the exchange */
6532                 bf_set(wqe_xc, &wqe->fcp_icmd.wqe_com, 0);
6533                 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
6534                 bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
6535                 bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
6536                 bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
6537                        LPFC_WQE_LENLOC_NONE);
6538                 bf_set(wqe_ebde_cnt, &wqe->fcp_icmd.wqe_com, 0);
6539         break;
6540         case CMD_GEN_REQUEST64_CR:
6541                 /* For this command calculate the xmit length of the
6542                  * request bde.
6543                  */
6544                 xmit_len = 0;
6545                 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
6546                         sizeof(struct ulp_bde64);
6547                 for (i = 0; i < numBdes; i++) {
6548                         if (bpl[i].tus.f.bdeFlags != BUFF_TYPE_BDE_64)
6549                                 break;
6550                         bde.tus.w = le32_to_cpu(bpl[i].tus.w);
6551                         xmit_len += bde.tus.f.bdeSize;
6552                 }
6553                 /* word3 iocb=IO_TAG wqe=request_payload_len */
6554                 wqe->gen_req.request_payload_len = xmit_len;
6555                 /* word4 iocb=parameter wqe=relative_offset memcpy */
6556                 /* word5 [rctl, type, df_ctl, la] copied in memcpy */
6557                 /* word6 context tag copied in memcpy */
6558                 if (iocbq->iocb.ulpCt_h  || iocbq->iocb.ulpCt_l) {
6559                         ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
6560                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6561                                 "2015 Invalid CT %x command 0x%x\n",
6562                                 ct, iocbq->iocb.ulpCommand);
6563                         return IOCB_ERROR;
6564                 }
6565                 bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
6566                 bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
6567                 bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
6568                 bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
6569                 bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
6570                 bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
6571                 bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
6572                 bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
6573                 command_type = OTHER_COMMAND;
6574         break;
6575         case CMD_XMIT_ELS_RSP64_CX:
6576                 /* words0-2 BDE memcpy */
6577                 /* word3 iocb=iotag32 wqe=response_payload_len */
6578                 wqe->xmit_els_rsp.response_payload_len = xmit_len;
6579                 /* word4 iocb=did wge=rsvd. */
6580                 wqe->xmit_els_rsp.rsvd4 = 0;
6581                 /* word5 iocb=rsvd wge=did */
6582                 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
6583                          iocbq->iocb.un.elsreq64.remoteID);
6584                 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
6585                        ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
6586                 bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
6587                 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
6588                        iocbq->iocb.ulpContext);
6589                 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
6590                         bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
6591                                iocbq->vport->vpi + phba->vpi_base);
6592                 bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
6593                 bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
6594                 bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
6595                 bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
6596                        LPFC_WQE_LENLOC_WORD3);
6597                 bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
6598                 command_type = OTHER_COMMAND;
6599         break;
6600         case CMD_CLOSE_XRI_CN:
6601         case CMD_ABORT_XRI_CN:
6602         case CMD_ABORT_XRI_CX:
6603                 /* words 0-2 memcpy should be 0 rserved */
6604                 /* port will send abts */
6605                 abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
6606                 if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
6607                         abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
6608                         fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
6609                 } else
6610                         fip = 0;
6611
6612                 if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
6613                         /*
6614                          * The link is down, or the command was ELS_FIP
6615                          * so the fw does not need to send abts
6616                          * on the wire.
6617                          */
6618                         bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
6619                 else
6620                         bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
6621                 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
6622                 /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
6623                 wqe->abort_cmd.rsrvd5 = 0;
6624                 bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
6625                         ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
6626                 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
6627                 /*
6628                  * The abort handler will send us CMD_ABORT_XRI_CN or
6629                  * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
6630                  */
6631                 bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
6632                 bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
6633                 bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
6634                        LPFC_WQE_LENLOC_NONE);
6635                 cmnd = CMD_ABORT_XRI_CX;
6636                 command_type = OTHER_COMMAND;
6637                 xritag = 0;
6638         break;
6639         case CMD_XMIT_BLS_RSP64_CX:
6640                 /* As BLS ABTS-ACC WQE is very different from other WQEs,
6641                  * we re-construct this WQE here based on information in
6642                  * iocbq from scratch.
6643                  */
6644                 memset(wqe, 0, sizeof(union lpfc_wqe));
6645                 /* OX_ID is invariable to who sent ABTS to CT exchange */
6646                 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
6647                        bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_acc));
6648                 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_acc) ==
6649                     LPFC_ABTS_UNSOL_INT) {
6650                         /* ABTS sent by initiator to CT exchange, the
6651                          * RX_ID field will be filled with the newly
6652                          * allocated responder XRI.
6653                          */
6654                         bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
6655                                iocbq->sli4_xritag);
6656                 } else {
6657                         /* ABTS sent by responder to CT exchange, the
6658                          * RX_ID field will be filled with the responder
6659                          * RX_ID from ABTS.
6660                          */
6661                         bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
6662                                bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_acc));
6663                 }
6664                 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
6665                 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
6666                 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
6667                        iocbq->iocb.ulpContext);
6668                 bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
6669                 bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
6670                        LPFC_WQE_LENLOC_NONE);
6671                 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
6672                 command_type = OTHER_COMMAND;
6673         break;
6674         case CMD_XRI_ABORTED_CX:
6675         case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
6676         case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
6677         case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
6678         case CMD_FCP_TRSP64_CX: /* Target mode rcv */
6679         case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
6680         default:
6681                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6682                                 "2014 Invalid command 0x%x\n",
6683                                 iocbq->iocb.ulpCommand);
6684                 return IOCB_ERROR;
6685         break;
6686         }
6687         bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
6688         bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
6689         wqe->generic.wqe_com.abort_tag = abort_tag;
6690         bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
6691         bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
6692         bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
6693         bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
6694         return 0;
6695 }
6696
6697 /**
6698  * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
6699  * @phba: Pointer to HBA context object.
6700  * @ring_number: SLI ring number to issue iocb on.
6701  * @piocb: Pointer to command iocb.
6702  * @flag: Flag indicating if this command can be put into txq.
6703  *
6704  * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
6705  * an iocb command to an HBA with SLI-4 interface spec.
6706  *
6707  * This function is called with hbalock held. The function will return success
6708  * after it successfully submit the iocb to firmware or after adding to the
6709  * txq.
6710  **/
6711 static int
6712 __lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
6713                          struct lpfc_iocbq *piocb, uint32_t flag)
6714 {
6715         struct lpfc_sglq *sglq;
6716         union lpfc_wqe wqe;
6717         struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
6718
6719         if (piocb->sli4_xritag == NO_XRI) {
6720                 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
6721                     piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
6722                         sglq = NULL;
6723                 else {
6724                         if (pring->txq_cnt) {
6725                                 if (!(flag & SLI_IOCB_RET_IOCB)) {
6726                                         __lpfc_sli_ringtx_put(phba,
6727                                                 pring, piocb);
6728                                         return IOCB_SUCCESS;
6729                                 } else {
6730                                         return IOCB_BUSY;
6731                                 }
6732                         } else {
6733                         sglq = __lpfc_sli_get_sglq(phba, piocb);
6734                                 if (!sglq) {
6735                                         if (!(flag & SLI_IOCB_RET_IOCB)) {
6736                                                 __lpfc_sli_ringtx_put(phba,
6737                                                                 pring,
6738                                                                 piocb);
6739                                                 return IOCB_SUCCESS;
6740                                         } else
6741                                                 return IOCB_BUSY;
6742                                 }
6743                         }
6744                 }
6745         } else if (piocb->iocb_flag &  LPFC_IO_FCP) {
6746                 sglq = NULL; /* These IO's already have an XRI and
6747                               * a mapped sgl.
6748                               */
6749         } else {
6750                 /* This is a continuation of a commandi,(CX) so this
6751                  * sglq is on the active list
6752                  */
6753                 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_xritag);
6754                 if (!sglq)
6755                         return IOCB_ERROR;
6756         }
6757
6758         if (sglq) {
6759                 piocb->sli4_xritag = sglq->sli4_xritag;
6760
6761                 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
6762                         return IOCB_ERROR;
6763         }
6764
6765         if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
6766                 return IOCB_ERROR;
6767
6768         if ((piocb->iocb_flag & LPFC_IO_FCP) ||
6769                 (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
6770                 /*
6771                  * For FCP command IOCB, get a new WQ index to distribute
6772                  * WQE across the WQsr. On the other hand, for abort IOCB,
6773                  * it carries the same WQ index to the original command
6774                  * IOCB.
6775                  */
6776                 if (piocb->iocb_flag & LPFC_IO_FCP)
6777                         piocb->fcp_wqidx = lpfc_sli4_scmd_to_wqidx_distr(phba);
6778                 if (lpfc_sli4_wq_put(phba->sli4_hba.fcp_wq[piocb->fcp_wqidx],
6779                                      &wqe))
6780                         return IOCB_ERROR;
6781         } else {
6782                 if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
6783                         return IOCB_ERROR;
6784         }
6785         lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
6786
6787         return 0;
6788 }
6789
6790 /**
6791  * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
6792  *
6793  * This routine wraps the actual lockless version for issusing IOCB function
6794  * pointer from the lpfc_hba struct.
6795  *
6796  * Return codes:
6797  *      IOCB_ERROR - Error
6798  *      IOCB_SUCCESS - Success
6799  *      IOCB_BUSY - Busy
6800  **/
6801 int
6802 __lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
6803                 struct lpfc_iocbq *piocb, uint32_t flag)
6804 {
6805         return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
6806 }
6807
6808 /**
6809  * lpfc_sli_api_table_setup - Set up sli api fucntion jump table
6810  * @phba: The hba struct for which this call is being executed.
6811  * @dev_grp: The HBA PCI-Device group number.
6812  *
6813  * This routine sets up the SLI interface API function jump table in @phba
6814  * struct.
6815  * Returns: 0 - success, -ENODEV - failure.
6816  **/
6817 int
6818 lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
6819 {
6820
6821         switch (dev_grp) {
6822         case LPFC_PCI_DEV_LP:
6823                 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
6824                 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
6825                 break;
6826         case LPFC_PCI_DEV_OC:
6827                 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
6828                 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
6829                 break;
6830         default:
6831                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6832                                 "1419 Invalid HBA PCI-device group: 0x%x\n",
6833                                 dev_grp);
6834                 return -ENODEV;
6835                 break;
6836         }
6837         phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
6838         return 0;
6839 }
6840
6841 /**
6842  * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
6843  * @phba: Pointer to HBA context object.
6844  * @pring: Pointer to driver SLI ring object.
6845  * @piocb: Pointer to command iocb.
6846  * @flag: Flag indicating if this command can be put into txq.
6847  *
6848  * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
6849  * function. This function gets the hbalock and calls
6850  * __lpfc_sli_issue_iocb function and will return the error returned
6851  * by __lpfc_sli_issue_iocb function. This wrapper is used by
6852  * functions which do not hold hbalock.
6853  **/
6854 int
6855 lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
6856                     struct lpfc_iocbq *piocb, uint32_t flag)
6857 {
6858         unsigned long iflags;
6859         int rc;
6860
6861         spin_lock_irqsave(&phba->hbalock, iflags);
6862         rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
6863         spin_unlock_irqrestore(&phba->hbalock, iflags);
6864
6865         return rc;
6866 }
6867
6868 /**
6869  * lpfc_extra_ring_setup - Extra ring setup function
6870  * @phba: Pointer to HBA context object.
6871  *
6872  * This function is called while driver attaches with the
6873  * HBA to setup the extra ring. The extra ring is used
6874  * only when driver needs to support target mode functionality
6875  * or IP over FC functionalities.
6876  *
6877  * This function is called with no lock held.
6878  **/
6879 static int
6880 lpfc_extra_ring_setup( struct lpfc_hba *phba)
6881 {
6882         struct lpfc_sli *psli;
6883         struct lpfc_sli_ring *pring;
6884
6885         psli = &phba->sli;
6886
6887         /* Adjust cmd/rsp ring iocb entries more evenly */
6888
6889         /* Take some away from the FCP ring */
6890         pring = &psli->ring[psli->fcp_ring];
6891         pring->numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
6892         pring->numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
6893         pring->numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
6894         pring->numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
6895
6896         /* and give them to the extra ring */
6897         pring = &psli->ring[psli->extra_ring];
6898
6899         pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
6900         pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
6901         pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
6902         pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
6903
6904         /* Setup default profile for this ring */
6905         pring->iotag_max = 4096;
6906         pring->num_mask = 1;
6907         pring->prt[0].profile = 0;      /* Mask 0 */
6908         pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
6909         pring->prt[0].type = phba->cfg_multi_ring_type;
6910         pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
6911         return 0;
6912 }
6913
6914 /**
6915  * lpfc_sli_async_event_handler - ASYNC iocb handler function
6916  * @phba: Pointer to HBA context object.
6917  * @pring: Pointer to driver SLI ring object.
6918  * @iocbq: Pointer to iocb object.
6919  *
6920  * This function is called by the slow ring event handler
6921  * function when there is an ASYNC event iocb in the ring.
6922  * This function is called with no lock held.
6923  * Currently this function handles only temperature related
6924  * ASYNC events. The function decodes the temperature sensor
6925  * event message and posts events for the management applications.
6926  **/
6927 static void
6928 lpfc_sli_async_event_handler(struct lpfc_hba * phba,
6929         struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
6930 {
6931         IOCB_t *icmd;
6932         uint16_t evt_code;
6933         uint16_t temp;
6934         struct temp_event temp_event_data;
6935         struct Scsi_Host *shost;
6936         uint32_t *iocb_w;
6937
6938         icmd = &iocbq->iocb;
6939         evt_code = icmd->un.asyncstat.evt_code;
6940         temp = icmd->ulpContext;
6941
6942         if ((evt_code != ASYNC_TEMP_WARN) &&
6943                 (evt_code != ASYNC_TEMP_SAFE)) {
6944                 iocb_w = (uint32_t *) icmd;
6945                 lpfc_printf_log(phba,
6946                         KERN_ERR,
6947                         LOG_SLI,
6948                         "0346 Ring %d handler: unexpected ASYNC_STATUS"
6949                         " evt_code 0x%x\n"
6950                         "W0  0x%08x W1  0x%08x W2  0x%08x W3  0x%08x\n"
6951                         "W4  0x%08x W5  0x%08x W6  0x%08x W7  0x%08x\n"
6952                         "W8  0x%08x W9  0x%08x W10 0x%08x W11 0x%08x\n"
6953                         "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
6954                         pring->ringno,
6955                         icmd->un.asyncstat.evt_code,
6956                         iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
6957                         iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
6958                         iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
6959                         iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
6960
6961                 return;
6962         }
6963         temp_event_data.data = (uint32_t)temp;
6964         temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
6965         if (evt_code == ASYNC_TEMP_WARN) {
6966                 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
6967                 lpfc_printf_log(phba,
6968                                 KERN_ERR,
6969                                 LOG_TEMP,
6970                                 "0347 Adapter is very hot, please take "
6971                                 "corrective action. temperature : %d Celsius\n",
6972                                 temp);
6973         }
6974         if (evt_code == ASYNC_TEMP_SAFE) {
6975                 temp_event_data.event_code = LPFC_NORMAL_TEMP;
6976                 lpfc_printf_log(phba,
6977                                 KERN_ERR,
6978                                 LOG_TEMP,
6979                                 "0340 Adapter temperature is OK now. "
6980                                 "temperature : %d Celsius\n",
6981                                 temp);
6982         }
6983
6984         /* Send temperature change event to applications */
6985         shost = lpfc_shost_from_vport(phba->pport);
6986         fc_host_post_vendor_event(shost, fc_get_event_number(),
6987                 sizeof(temp_event_data), (char *) &temp_event_data,
6988                 LPFC_NL_VENDOR_ID);
6989
6990 }
6991
6992
6993 /**
6994  * lpfc_sli_setup - SLI ring setup function
6995  * @phba: Pointer to HBA context object.
6996  *
6997  * lpfc_sli_setup sets up rings of the SLI interface with
6998  * number of iocbs per ring and iotags. This function is
6999  * called while driver attach to the HBA and before the
7000  * interrupts are enabled. So there is no need for locking.
7001  *
7002  * This function always returns 0.
7003  **/
7004 int
7005 lpfc_sli_setup(struct lpfc_hba *phba)
7006 {
7007         int i, totiocbsize = 0;
7008         struct lpfc_sli *psli = &phba->sli;
7009         struct lpfc_sli_ring *pring;
7010
7011         psli->num_rings = MAX_CONFIGURED_RINGS;
7012         psli->sli_flag = 0;
7013         psli->fcp_ring = LPFC_FCP_RING;
7014         psli->next_ring = LPFC_FCP_NEXT_RING;
7015         psli->extra_ring = LPFC_EXTRA_RING;
7016
7017         psli->iocbq_lookup = NULL;
7018         psli->iocbq_lookup_len = 0;
7019         psli->last_iotag = 0;
7020
7021         for (i = 0; i < psli->num_rings; i++) {
7022                 pring = &psli->ring[i];
7023                 switch (i) {
7024                 case LPFC_FCP_RING:     /* ring 0 - FCP */
7025                         /* numCiocb and numRiocb are used in config_port */
7026                         pring->numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
7027                         pring->numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
7028                         pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
7029                         pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
7030                         pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
7031                         pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
7032                         pring->sizeCiocb = (phba->sli_rev == 3) ?
7033                                                         SLI3_IOCB_CMD_SIZE :
7034                                                         SLI2_IOCB_CMD_SIZE;
7035                         pring->sizeRiocb = (phba->sli_rev == 3) ?
7036                                                         SLI3_IOCB_RSP_SIZE :
7037                                                         SLI2_IOCB_RSP_SIZE;
7038                         pring->iotag_ctr = 0;
7039                         pring->iotag_max =
7040                             (phba->cfg_hba_queue_depth * 2);
7041                         pring->fast_iotag = pring->iotag_max;
7042                         pring->num_mask = 0;
7043                         break;
7044                 case LPFC_EXTRA_RING:   /* ring 1 - EXTRA */
7045                         /* numCiocb and numRiocb are used in config_port */
7046                         pring->numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
7047                         pring->numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
7048                         pring->sizeCiocb = (phba->sli_rev == 3) ?
7049                                                         SLI3_IOCB_CMD_SIZE :
7050                                                         SLI2_IOCB_CMD_SIZE;
7051                         pring->sizeRiocb = (phba->sli_rev == 3) ?
7052                                                         SLI3_IOCB_RSP_SIZE :
7053                                                         SLI2_IOCB_RSP_SIZE;
7054                         pring->iotag_max = phba->cfg_hba_queue_depth;
7055                         pring->num_mask = 0;
7056                         break;
7057                 case LPFC_ELS_RING:     /* ring 2 - ELS / CT */
7058                         /* numCiocb and numRiocb are used in config_port */
7059                         pring->numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
7060                         pring->numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
7061                         pring->sizeCiocb = (phba->sli_rev == 3) ?
7062                                                         SLI3_IOCB_CMD_SIZE :
7063                                                         SLI2_IOCB_CMD_SIZE;
7064                         pring->sizeRiocb = (phba->sli_rev == 3) ?
7065                                                         SLI3_IOCB_RSP_SIZE :
7066                                                         SLI2_IOCB_RSP_SIZE;
7067                         pring->fast_iotag = 0;
7068                         pring->iotag_ctr = 0;
7069                         pring->iotag_max = 4096;
7070                         pring->lpfc_sli_rcv_async_status =
7071                                 lpfc_sli_async_event_handler;
7072                         pring->num_mask = LPFC_MAX_RING_MASK;
7073                         pring->prt[0].profile = 0;      /* Mask 0 */
7074                         pring->prt[0].rctl = FC_RCTL_ELS_REQ;
7075                         pring->prt[0].type = FC_TYPE_ELS;
7076                         pring->prt[0].lpfc_sli_rcv_unsol_event =
7077                             lpfc_els_unsol_event;
7078                         pring->prt[1].profile = 0;      /* Mask 1 */
7079                         pring->prt[1].rctl = FC_RCTL_ELS_REP;
7080                         pring->prt[1].type = FC_TYPE_ELS;
7081                         pring->prt[1].lpfc_sli_rcv_unsol_event =
7082                             lpfc_els_unsol_event;
7083                         pring->prt[2].profile = 0;      /* Mask 2 */
7084                         /* NameServer Inquiry */
7085                         pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
7086                         /* NameServer */
7087                         pring->prt[2].type = FC_TYPE_CT;
7088                         pring->prt[2].lpfc_sli_rcv_unsol_event =
7089                             lpfc_ct_unsol_event;
7090                         pring->prt[3].profile = 0;      /* Mask 3 */
7091                         /* NameServer response */
7092                         pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
7093                         /* NameServer */
7094                         pring->prt[3].type = FC_TYPE_CT;
7095                         pring->prt[3].lpfc_sli_rcv_unsol_event =
7096                             lpfc_ct_unsol_event;
7097                         /* abort unsolicited sequence */
7098                         pring->prt[4].profile = 0;      /* Mask 4 */
7099                         pring->prt[4].rctl = FC_RCTL_BA_ABTS;
7100                         pring->prt[4].type = FC_TYPE_BLS;
7101                         pring->prt[4].lpfc_sli_rcv_unsol_event =
7102                             lpfc_sli4_ct_abort_unsol_event;
7103                         break;
7104                 }
7105                 totiocbsize += (pring->numCiocb * pring->sizeCiocb) +
7106                                 (pring->numRiocb * pring->sizeRiocb);
7107         }
7108         if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
7109                 /* Too many cmd / rsp ring entries in SLI2 SLIM */
7110                 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
7111                        "SLI2 SLIM Data: x%x x%lx\n",
7112                        phba->brd_no, totiocbsize,
7113                        (unsigned long) MAX_SLIM_IOCB_SIZE);
7114         }
7115         if (phba->cfg_multi_ring_support == 2)
7116                 lpfc_extra_ring_setup(phba);
7117
7118         return 0;
7119 }
7120
7121 /**
7122  * lpfc_sli_queue_setup - Queue initialization function
7123  * @phba: Pointer to HBA context object.
7124  *
7125  * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
7126  * ring. This function also initializes ring indices of each ring.
7127  * This function is called during the initialization of the SLI
7128  * interface of an HBA.
7129  * This function is called with no lock held and always returns
7130  * 1.
7131  **/
7132 int
7133 lpfc_sli_queue_setup(struct lpfc_hba *phba)
7134 {
7135         struct lpfc_sli *psli;
7136         struct lpfc_sli_ring *pring;
7137         int i;
7138
7139         psli = &phba->sli;
7140         spin_lock_irq(&phba->hbalock);
7141         INIT_LIST_HEAD(&psli->mboxq);
7142         INIT_LIST_HEAD(&psli->mboxq_cmpl);
7143         /* Initialize list headers for txq and txcmplq as double linked lists */
7144         for (i = 0; i < psli->num_rings; i++) {
7145                 pring = &psli->ring[i];
7146                 pring->ringno = i;
7147                 pring->next_cmdidx  = 0;
7148                 pring->local_getidx = 0;
7149                 pring->cmdidx = 0;
7150                 INIT_LIST_HEAD(&pring->txq);
7151                 INIT_LIST_HEAD(&pring->txcmplq);
7152                 INIT_LIST_HEAD(&pring->iocb_continueq);
7153                 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
7154                 INIT_LIST_HEAD(&pring->postbufq);
7155         }
7156         spin_unlock_irq(&phba->hbalock);
7157         return 1;
7158 }
7159
7160 /**
7161  * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
7162  * @phba: Pointer to HBA context object.
7163  *
7164  * This routine flushes the mailbox command subsystem. It will unconditionally
7165  * flush all the mailbox commands in the three possible stages in the mailbox
7166  * command sub-system: pending mailbox command queue; the outstanding mailbox
7167  * command; and completed mailbox command queue. It is caller's responsibility
7168  * to make sure that the driver is in the proper state to flush the mailbox
7169  * command sub-system. Namely, the posting of mailbox commands into the
7170  * pending mailbox command queue from the various clients must be stopped;
7171  * either the HBA is in a state that it will never works on the outstanding
7172  * mailbox command (such as in EEH or ERATT conditions) or the outstanding
7173  * mailbox command has been completed.
7174  **/
7175 static void
7176 lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
7177 {
7178         LIST_HEAD(completions);
7179         struct lpfc_sli *psli = &phba->sli;
7180         LPFC_MBOXQ_t *pmb;
7181         unsigned long iflag;
7182
7183         /* Flush all the mailbox commands in the mbox system */
7184         spin_lock_irqsave(&phba->hbalock, iflag);
7185         /* The pending mailbox command queue */
7186         list_splice_init(&phba->sli.mboxq, &completions);
7187         /* The outstanding active mailbox command */
7188         if (psli->mbox_active) {
7189                 list_add_tail(&psli->mbox_active->list, &completions);
7190                 psli->mbox_active = NULL;
7191                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7192         }
7193         /* The completed mailbox command queue */
7194         list_splice_init(&phba->sli.mboxq_cmpl, &completions);
7195         spin_unlock_irqrestore(&phba->hbalock, iflag);
7196
7197         /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
7198         while (!list_empty(&completions)) {
7199                 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
7200                 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
7201                 if (pmb->mbox_cmpl)
7202                         pmb->mbox_cmpl(phba, pmb);
7203         }
7204 }
7205
7206 /**
7207  * lpfc_sli_host_down - Vport cleanup function
7208  * @vport: Pointer to virtual port object.
7209  *
7210  * lpfc_sli_host_down is called to clean up the resources
7211  * associated with a vport before destroying virtual
7212  * port data structures.
7213  * This function does following operations:
7214  * - Free discovery resources associated with this virtual
7215  *   port.
7216  * - Free iocbs associated with this virtual port in
7217  *   the txq.
7218  * - Send abort for all iocb commands associated with this
7219  *   vport in txcmplq.
7220  *
7221  * This function is called with no lock held and always returns 1.
7222  **/
7223 int
7224 lpfc_sli_host_down(struct lpfc_vport *vport)
7225 {
7226         LIST_HEAD(completions);
7227         struct lpfc_hba *phba = vport->phba;
7228         struct lpfc_sli *psli = &phba->sli;
7229         struct lpfc_sli_ring *pring;
7230         struct lpfc_iocbq *iocb, *next_iocb;
7231         int i;
7232         unsigned long flags = 0;
7233         uint16_t prev_pring_flag;
7234
7235         lpfc_cleanup_discovery_resources(vport);
7236
7237         spin_lock_irqsave(&phba->hbalock, flags);
7238         for (i = 0; i < psli->num_rings; i++) {
7239                 pring = &psli->ring[i];
7240                 prev_pring_flag = pring->flag;
7241                 /* Only slow rings */
7242                 if (pring->ringno == LPFC_ELS_RING) {
7243                         pring->flag |= LPFC_DEFERRED_RING_EVENT;
7244                         /* Set the lpfc data pending flag */
7245                         set_bit(LPFC_DATA_READY, &phba->data_flags);
7246                 }
7247                 /*
7248                  * Error everything on the txq since these iocbs have not been
7249                  * given to the FW yet.
7250                  */
7251                 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
7252                         if (iocb->vport != vport)
7253                                 continue;
7254                         list_move_tail(&iocb->list, &completions);
7255                         pring->txq_cnt--;
7256                 }
7257
7258                 /* Next issue ABTS for everything on the txcmplq */
7259                 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
7260                                                                         list) {
7261                         if (iocb->vport != vport)
7262                                 continue;
7263                         lpfc_sli_issue_abort_iotag(phba, pring, iocb);
7264                 }
7265
7266                 pring->flag = prev_pring_flag;
7267         }
7268
7269         spin_unlock_irqrestore(&phba->hbalock, flags);
7270
7271         /* Cancel all the IOCBs from the completions list */
7272         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
7273                               IOERR_SLI_DOWN);
7274         return 1;
7275 }
7276
7277 /**
7278  * lpfc_sli_hba_down - Resource cleanup function for the HBA
7279  * @phba: Pointer to HBA context object.
7280  *
7281  * This function cleans up all iocb, buffers, mailbox commands
7282  * while shutting down the HBA. This function is called with no
7283  * lock held and always returns 1.
7284  * This function does the following to cleanup driver resources:
7285  * - Free discovery resources for each virtual port
7286  * - Cleanup any pending fabric iocbs
7287  * - Iterate through the iocb txq and free each entry
7288  *   in the list.
7289  * - Free up any buffer posted to the HBA
7290  * - Free mailbox commands in the mailbox queue.
7291  **/
7292 int
7293 lpfc_sli_hba_down(struct lpfc_hba *phba)
7294 {
7295         LIST_HEAD(completions);
7296         struct lpfc_sli *psli = &phba->sli;
7297         struct lpfc_sli_ring *pring;
7298         struct lpfc_dmabuf *buf_ptr;
7299         unsigned long flags = 0;
7300         int i;
7301
7302         /* Shutdown the mailbox command sub-system */
7303         lpfc_sli_mbox_sys_shutdown(phba);
7304
7305         lpfc_hba_down_prep(phba);
7306
7307         lpfc_fabric_abort_hba(phba);
7308
7309         spin_lock_irqsave(&phba->hbalock, flags);
7310         for (i = 0; i < psli->num_rings; i++) {
7311                 pring = &psli->ring[i];
7312                 /* Only slow rings */
7313                 if (pring->ringno == LPFC_ELS_RING) {
7314                         pring->flag |= LPFC_DEFERRED_RING_EVENT;
7315                         /* Set the lpfc data pending flag */
7316                         set_bit(LPFC_DATA_READY, &phba->data_flags);
7317                 }
7318
7319                 /*
7320                  * Error everything on the txq since these iocbs have not been
7321                  * given to the FW yet.
7322                  */
7323                 list_splice_init(&pring->txq, &completions);
7324                 pring->txq_cnt = 0;
7325
7326         }
7327         spin_unlock_irqrestore(&phba->hbalock, flags);
7328
7329         /* Cancel all the IOCBs from the completions list */
7330         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
7331                               IOERR_SLI_DOWN);
7332
7333         spin_lock_irqsave(&phba->hbalock, flags);
7334         list_splice_init(&phba->elsbuf, &completions);
7335         phba->elsbuf_cnt = 0;
7336         phba->elsbuf_prev_cnt = 0;
7337         spin_unlock_irqrestore(&phba->hbalock, flags);
7338
7339         while (!list_empty(&completions)) {
7340                 list_remove_head(&completions, buf_ptr,
7341                         struct lpfc_dmabuf, list);
7342                 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
7343                 kfree(buf_ptr);
7344         }
7345
7346         /* Return any active mbox cmds */
7347         del_timer_sync(&psli->mbox_tmo);
7348
7349         spin_lock_irqsave(&phba->pport->work_port_lock, flags);
7350         phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
7351         spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
7352
7353         return 1;
7354 }
7355
7356 /**
7357  * lpfc_sli_pcimem_bcopy - SLI memory copy function
7358  * @srcp: Source memory pointer.
7359  * @destp: Destination memory pointer.
7360  * @cnt: Number of words required to be copied.
7361  *
7362  * This function is used for copying data between driver memory
7363  * and the SLI memory. This function also changes the endianness
7364  * of each word if native endianness is different from SLI
7365  * endianness. This function can be called with or without
7366  * lock.
7367  **/
7368 void
7369 lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
7370 {
7371         uint32_t *src = srcp;
7372         uint32_t *dest = destp;
7373         uint32_t ldata;
7374         int i;
7375
7376         for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
7377                 ldata = *src;
7378                 ldata = le32_to_cpu(ldata);
7379                 *dest = ldata;
7380                 src++;
7381                 dest++;
7382         }
7383 }
7384
7385
7386 /**
7387  * lpfc_sli_bemem_bcopy - SLI memory copy function
7388  * @srcp: Source memory pointer.
7389  * @destp: Destination memory pointer.
7390  * @cnt: Number of words required to be copied.
7391  *
7392  * This function is used for copying data between a data structure
7393  * with big endian representation to local endianness.
7394  * This function can be called with or without lock.
7395  **/
7396 void
7397 lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
7398 {
7399         uint32_t *src = srcp;
7400         uint32_t *dest = destp;
7401         uint32_t ldata;
7402         int i;
7403
7404         for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
7405                 ldata = *src;
7406                 ldata = be32_to_cpu(ldata);
7407                 *dest = ldata;
7408                 src++;
7409                 dest++;
7410         }
7411 }
7412
7413 /**
7414  * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
7415  * @phba: Pointer to HBA context object.
7416  * @pring: Pointer to driver SLI ring object.
7417  * @mp: Pointer to driver buffer object.
7418  *
7419  * This function is called with no lock held.
7420  * It always return zero after adding the buffer to the postbufq
7421  * buffer list.
7422  **/
7423 int
7424 lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7425                          struct lpfc_dmabuf *mp)
7426 {
7427         /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
7428            later */
7429         spin_lock_irq(&phba->hbalock);
7430         list_add_tail(&mp->list, &pring->postbufq);
7431         pring->postbufq_cnt++;
7432         spin_unlock_irq(&phba->hbalock);
7433         return 0;
7434 }
7435
7436 /**
7437  * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
7438  * @phba: Pointer to HBA context object.
7439  *
7440  * When HBQ is enabled, buffers are searched based on tags. This function
7441  * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
7442  * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
7443  * does not conflict with tags of buffer posted for unsolicited events.
7444  * The function returns the allocated tag. The function is called with
7445  * no locks held.
7446  **/
7447 uint32_t
7448 lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
7449 {
7450         spin_lock_irq(&phba->hbalock);
7451         phba->buffer_tag_count++;
7452         /*
7453          * Always set the QUE_BUFTAG_BIT to distiguish between
7454          * a tag assigned by HBQ.
7455          */
7456         phba->buffer_tag_count |= QUE_BUFTAG_BIT;
7457         spin_unlock_irq(&phba->hbalock);
7458         return phba->buffer_tag_count;
7459 }
7460
7461 /**
7462  * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
7463  * @phba: Pointer to HBA context object.
7464  * @pring: Pointer to driver SLI ring object.
7465  * @tag: Buffer tag.
7466  *
7467  * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
7468  * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
7469  * iocb is posted to the response ring with the tag of the buffer.
7470  * This function searches the pring->postbufq list using the tag
7471  * to find buffer associated with CMD_IOCB_RET_XRI64_CX
7472  * iocb. If the buffer is found then lpfc_dmabuf object of the
7473  * buffer is returned to the caller else NULL is returned.
7474  * This function is called with no lock held.
7475  **/
7476 struct lpfc_dmabuf *
7477 lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7478                         uint32_t tag)
7479 {
7480         struct lpfc_dmabuf *mp, *next_mp;
7481         struct list_head *slp = &pring->postbufq;
7482
7483         /* Search postbufq, from the begining, looking for a match on tag */
7484         spin_lock_irq(&phba->hbalock);
7485         list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
7486                 if (mp->buffer_tag == tag) {
7487                         list_del_init(&mp->list);
7488                         pring->postbufq_cnt--;
7489                         spin_unlock_irq(&phba->hbalock);
7490                         return mp;
7491                 }
7492         }
7493
7494         spin_unlock_irq(&phba->hbalock);
7495         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7496                         "0402 Cannot find virtual addr for buffer tag on "
7497                         "ring %d Data x%lx x%p x%p x%x\n",
7498                         pring->ringno, (unsigned long) tag,
7499                         slp->next, slp->prev, pring->postbufq_cnt);
7500
7501         return NULL;
7502 }
7503
7504 /**
7505  * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
7506  * @phba: Pointer to HBA context object.
7507  * @pring: Pointer to driver SLI ring object.
7508  * @phys: DMA address of the buffer.
7509  *
7510  * This function searches the buffer list using the dma_address
7511  * of unsolicited event to find the driver's lpfc_dmabuf object
7512  * corresponding to the dma_address. The function returns the
7513  * lpfc_dmabuf object if a buffer is found else it returns NULL.
7514  * This function is called by the ct and els unsolicited event
7515  * handlers to get the buffer associated with the unsolicited
7516  * event.
7517  *
7518  * This function is called with no lock held.
7519  **/
7520 struct lpfc_dmabuf *
7521 lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7522                          dma_addr_t phys)
7523 {
7524         struct lpfc_dmabuf *mp, *next_mp;
7525         struct list_head *slp = &pring->postbufq;
7526
7527         /* Search postbufq, from the begining, looking for a match on phys */
7528         spin_lock_irq(&phba->hbalock);
7529         list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
7530                 if (mp->phys == phys) {
7531                         list_del_init(&mp->list);
7532                         pring->postbufq_cnt--;
7533                         spin_unlock_irq(&phba->hbalock);
7534                         return mp;
7535                 }
7536         }
7537
7538         spin_unlock_irq(&phba->hbalock);
7539         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7540                         "0410 Cannot find virtual addr for mapped buf on "
7541                         "ring %d Data x%llx x%p x%p x%x\n",
7542                         pring->ringno, (unsigned long long)phys,
7543                         slp->next, slp->prev, pring->postbufq_cnt);
7544         return NULL;
7545 }
7546
7547 /**
7548  * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
7549  * @phba: Pointer to HBA context object.
7550  * @cmdiocb: Pointer to driver command iocb object.
7551  * @rspiocb: Pointer to driver response iocb object.
7552  *
7553  * This function is the completion handler for the abort iocbs for
7554  * ELS commands. This function is called from the ELS ring event
7555  * handler with no lock held. This function frees memory resources
7556  * associated with the abort iocb.
7557  **/
7558 static void
7559 lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7560                         struct lpfc_iocbq *rspiocb)
7561 {
7562         IOCB_t *irsp = &rspiocb->iocb;
7563         uint16_t abort_iotag, abort_context;
7564         struct lpfc_iocbq *abort_iocb;
7565         struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
7566
7567         abort_iocb = NULL;
7568
7569         if (irsp->ulpStatus) {
7570                 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
7571                 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
7572
7573                 spin_lock_irq(&phba->hbalock);
7574                 if (phba->sli_rev < LPFC_SLI_REV4) {
7575                         if (abort_iotag != 0 &&
7576                                 abort_iotag <= phba->sli.last_iotag)
7577                                 abort_iocb =
7578                                         phba->sli.iocbq_lookup[abort_iotag];
7579                 } else
7580                         /* For sli4 the abort_tag is the XRI,
7581                          * so the abort routine puts the iotag  of the iocb
7582                          * being aborted in the context field of the abort
7583                          * IOCB.
7584                          */
7585                         abort_iocb = phba->sli.iocbq_lookup[abort_context];
7586
7587                 /*
7588                  *  If the iocb is not found in Firmware queue the iocb
7589                  *  might have completed already. Do not free it again.
7590                  */
7591                 if (irsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
7592                         if (irsp->un.ulpWord[4] != IOERR_NO_XRI) {
7593                                 spin_unlock_irq(&phba->hbalock);
7594                                 lpfc_sli_release_iocbq(phba, cmdiocb);
7595                                 return;
7596                         }
7597                         /* For SLI4 the ulpContext field for abort IOCB
7598                          * holds the iotag of the IOCB being aborted so
7599                          * the local abort_context needs to be reset to
7600                          * match the aborted IOCBs ulpContext.
7601                          */
7602                         if (abort_iocb && phba->sli_rev == LPFC_SLI_REV4)
7603                                 abort_context = abort_iocb->iocb.ulpContext;
7604                 }
7605
7606                 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
7607                                 "0327 Cannot abort els iocb %p "
7608                                 "with tag %x context %x, abort status %x, "
7609                                 "abort code %x\n",
7610                                 abort_iocb, abort_iotag, abort_context,
7611                                 irsp->ulpStatus, irsp->un.ulpWord[4]);
7612                 /*
7613                  * make sure we have the right iocbq before taking it
7614                  * off the txcmplq and try to call completion routine.
7615                  */
7616                 if (!abort_iocb ||
7617                     abort_iocb->iocb.ulpContext != abort_context ||
7618                     (abort_iocb->iocb_flag & LPFC_DRIVER_ABORTED) == 0)
7619                         spin_unlock_irq(&phba->hbalock);
7620                 else if (phba->sli_rev < LPFC_SLI_REV4) {
7621                         /*
7622                          * leave the SLI4 aborted command on the txcmplq
7623                          * list and the command complete WCQE's XB bit
7624                          * will tell whether the SGL (XRI) can be released
7625                          * immediately or to the aborted SGL list for the
7626                          * following abort XRI from the HBA.
7627                          */
7628                         list_del_init(&abort_iocb->list);
7629                         if (abort_iocb->iocb_flag & LPFC_IO_ON_Q) {
7630                                 abort_iocb->iocb_flag &= ~LPFC_IO_ON_Q;
7631                                 pring->txcmplq_cnt--;
7632                         }
7633
7634                         /* Firmware could still be in progress of DMAing
7635                          * payload, so don't free data buffer till after
7636                          * a hbeat.
7637                          */
7638                         abort_iocb->iocb_flag |= LPFC_DELAY_MEM_FREE;
7639                         abort_iocb->iocb_flag &= ~LPFC_DRIVER_ABORTED;
7640                         spin_unlock_irq(&phba->hbalock);
7641
7642                         abort_iocb->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
7643                         abort_iocb->iocb.un.ulpWord[4] = IOERR_ABORT_REQUESTED;
7644                         (abort_iocb->iocb_cmpl)(phba, abort_iocb, abort_iocb);
7645                 } else
7646                         spin_unlock_irq(&phba->hbalock);
7647         }
7648
7649         lpfc_sli_release_iocbq(phba, cmdiocb);
7650         return;
7651 }
7652
7653 /**
7654  * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
7655  * @phba: Pointer to HBA context object.
7656  * @cmdiocb: Pointer to driver command iocb object.
7657  * @rspiocb: Pointer to driver response iocb object.
7658  *
7659  * The function is called from SLI ring event handler with no
7660  * lock held. This function is the completion handler for ELS commands
7661  * which are aborted. The function frees memory resources used for
7662  * the aborted ELS commands.
7663  **/
7664 static void
7665 lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7666                      struct lpfc_iocbq *rspiocb)
7667 {
7668         IOCB_t *irsp = &rspiocb->iocb;
7669
7670         /* ELS cmd tag <ulpIoTag> completes */
7671         lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
7672                         "0139 Ignoring ELS cmd tag x%x completion Data: "
7673                         "x%x x%x x%x\n",
7674                         irsp->ulpIoTag, irsp->ulpStatus,
7675                         irsp->un.ulpWord[4], irsp->ulpTimeout);
7676         if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
7677                 lpfc_ct_free_iocb(phba, cmdiocb);
7678         else
7679                 lpfc_els_free_iocb(phba, cmdiocb);
7680         return;
7681 }
7682
7683 /**
7684  * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
7685  * @phba: Pointer to HBA context object.
7686  * @pring: Pointer to driver SLI ring object.
7687  * @cmdiocb: Pointer to driver command iocb object.
7688  *
7689  * This function issues an abort iocb for the provided command iocb down to
7690  * the port. Other than the case the outstanding command iocb is an abort
7691  * request, this function issues abort out unconditionally. This function is
7692  * called with hbalock held. The function returns 0 when it fails due to
7693  * memory allocation failure or when the command iocb is an abort request.
7694  **/
7695 static int
7696 lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7697                            struct lpfc_iocbq *cmdiocb)
7698 {
7699         struct lpfc_vport *vport = cmdiocb->vport;
7700         struct lpfc_iocbq *abtsiocbp;
7701         IOCB_t *icmd = NULL;
7702         IOCB_t *iabt = NULL;
7703         int retval;
7704
7705         /*
7706          * There are certain command types we don't want to abort.  And we
7707          * don't want to abort commands that are already in the process of
7708          * being aborted.
7709          */
7710         icmd = &cmdiocb->iocb;
7711         if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
7712             icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
7713             (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
7714                 return 0;
7715
7716         /* issue ABTS for this IOCB based on iotag */
7717         abtsiocbp = __lpfc_sli_get_iocbq(phba);
7718         if (abtsiocbp == NULL)
7719                 return 0;
7720
7721         /* This signals the response to set the correct status
7722          * before calling the completion handler
7723          */
7724         cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
7725
7726         iabt = &abtsiocbp->iocb;
7727         iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
7728         iabt->un.acxri.abortContextTag = icmd->ulpContext;
7729         if (phba->sli_rev == LPFC_SLI_REV4) {
7730                 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
7731                 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
7732         }
7733         else
7734                 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
7735         iabt->ulpLe = 1;
7736         iabt->ulpClass = icmd->ulpClass;
7737
7738         /* ABTS WQE must go to the same WQ as the WQE to be aborted */
7739         abtsiocbp->fcp_wqidx = cmdiocb->fcp_wqidx;
7740         if (cmdiocb->iocb_flag & LPFC_IO_FCP)
7741                 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
7742
7743         if (phba->link_state >= LPFC_LINK_UP)
7744                 iabt->ulpCommand = CMD_ABORT_XRI_CN;
7745         else
7746                 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
7747
7748         abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
7749
7750         lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
7751                          "0339 Abort xri x%x, original iotag x%x, "
7752                          "abort cmd iotag x%x\n",
7753                          iabt->un.acxri.abortIoTag,
7754                          iabt->un.acxri.abortContextTag,
7755                          abtsiocbp->iotag);
7756         retval = __lpfc_sli_issue_iocb(phba, pring->ringno, abtsiocbp, 0);
7757
7758         if (retval)
7759                 __lpfc_sli_release_iocbq(phba, abtsiocbp);
7760
7761         /*
7762          * Caller to this routine should check for IOCB_ERROR
7763          * and handle it properly.  This routine no longer removes
7764          * iocb off txcmplq and call compl in case of IOCB_ERROR.
7765          */
7766         return retval;
7767 }
7768
7769 /**
7770  * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
7771  * @phba: Pointer to HBA context object.
7772  * @pring: Pointer to driver SLI ring object.
7773  * @cmdiocb: Pointer to driver command iocb object.
7774  *
7775  * This function issues an abort iocb for the provided command iocb. In case
7776  * of unloading, the abort iocb will not be issued to commands on the ELS
7777  * ring. Instead, the callback function shall be changed to those commands
7778  * so that nothing happens when them finishes. This function is called with
7779  * hbalock held. The function returns 0 when the command iocb is an abort
7780  * request.
7781  **/
7782 int
7783 lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7784                            struct lpfc_iocbq *cmdiocb)
7785 {
7786         struct lpfc_vport *vport = cmdiocb->vport;
7787         int retval = IOCB_ERROR;
7788         IOCB_t *icmd = NULL;
7789
7790         /*
7791          * There are certain command types we don't want to abort.  And we
7792          * don't want to abort commands that are already in the process of
7793          * being aborted.
7794          */
7795         icmd = &cmdiocb->iocb;
7796         if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
7797             icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
7798             (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
7799                 return 0;
7800
7801         /*
7802          * If we're unloading, don't abort iocb on the ELS ring, but change
7803          * the callback so that nothing happens when it finishes.
7804          */
7805         if ((vport->load_flag & FC_UNLOADING) &&
7806             (pring->ringno == LPFC_ELS_RING)) {
7807                 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
7808                         cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
7809                 else
7810                         cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
7811                 goto abort_iotag_exit;
7812         }
7813
7814         /* Now, we try to issue the abort to the cmdiocb out */
7815         retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
7816
7817 abort_iotag_exit:
7818         /*
7819          * Caller to this routine should check for IOCB_ERROR
7820          * and handle it properly.  This routine no longer removes
7821          * iocb off txcmplq and call compl in case of IOCB_ERROR.
7822          */
7823         return retval;
7824 }
7825
7826 /**
7827  * lpfc_sli_iocb_ring_abort - Unconditionally abort all iocbs on an iocb ring
7828  * @phba: Pointer to HBA context object.
7829  * @pring: Pointer to driver SLI ring object.
7830  *
7831  * This function aborts all iocbs in the given ring and frees all the iocb
7832  * objects in txq. This function issues abort iocbs unconditionally for all
7833  * the iocb commands in txcmplq. The iocbs in the txcmplq is not guaranteed
7834  * to complete before the return of this function. The caller is not required
7835  * to hold any locks.
7836  **/
7837 static void
7838 lpfc_sli_iocb_ring_abort(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
7839 {
7840         LIST_HEAD(completions);
7841         struct lpfc_iocbq *iocb, *next_iocb;
7842
7843         if (pring->ringno == LPFC_ELS_RING)
7844                 lpfc_fabric_abort_hba(phba);
7845
7846         spin_lock_irq(&phba->hbalock);
7847
7848         /* Take off all the iocbs on txq for cancelling */
7849         list_splice_init(&pring->txq, &completions);
7850         pring->txq_cnt = 0;
7851
7852         /* Next issue ABTS for everything on the txcmplq */
7853         list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
7854                 lpfc_sli_abort_iotag_issue(phba, pring, iocb);
7855
7856         spin_unlock_irq(&phba->hbalock);
7857
7858         /* Cancel all the IOCBs from the completions list */
7859         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
7860                               IOERR_SLI_ABORTED);
7861 }
7862
7863 /**
7864  * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
7865  * @phba: pointer to lpfc HBA data structure.
7866  *
7867  * This routine will abort all pending and outstanding iocbs to an HBA.
7868  **/
7869 void
7870 lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
7871 {
7872         struct lpfc_sli *psli = &phba->sli;
7873         struct lpfc_sli_ring *pring;
7874         int i;
7875
7876         for (i = 0; i < psli->num_rings; i++) {
7877                 pring = &psli->ring[i];
7878                 lpfc_sli_iocb_ring_abort(phba, pring);
7879         }
7880 }
7881
7882 /**
7883  * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
7884  * @iocbq: Pointer to driver iocb object.
7885  * @vport: Pointer to driver virtual port object.
7886  * @tgt_id: SCSI ID of the target.
7887  * @lun_id: LUN ID of the scsi device.
7888  * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
7889  *
7890  * This function acts as an iocb filter for functions which abort or count
7891  * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
7892  * 0 if the filtering criteria is met for the given iocb and will return
7893  * 1 if the filtering criteria is not met.
7894  * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
7895  * given iocb is for the SCSI device specified by vport, tgt_id and
7896  * lun_id parameter.
7897  * If ctx_cmd == LPFC_CTX_TGT,  the function returns 0 only if the
7898  * given iocb is for the SCSI target specified by vport and tgt_id
7899  * parameters.
7900  * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
7901  * given iocb is for the SCSI host associated with the given vport.
7902  * This function is called with no locks held.
7903  **/
7904 static int
7905 lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
7906                            uint16_t tgt_id, uint64_t lun_id,
7907                            lpfc_ctx_cmd ctx_cmd)
7908 {
7909         struct lpfc_scsi_buf *lpfc_cmd;
7910         int rc = 1;
7911
7912         if (!(iocbq->iocb_flag &  LPFC_IO_FCP))
7913                 return rc;
7914
7915         if (iocbq->vport != vport)
7916                 return rc;
7917
7918         lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
7919
7920         if (lpfc_cmd->pCmd == NULL)
7921                 return rc;
7922
7923         switch (ctx_cmd) {
7924         case LPFC_CTX_LUN:
7925                 if ((lpfc_cmd->rdata->pnode) &&
7926                     (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
7927                     (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
7928                         rc = 0;
7929                 break;
7930         case LPFC_CTX_TGT:
7931                 if ((lpfc_cmd->rdata->pnode) &&
7932                     (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
7933                         rc = 0;
7934                 break;
7935         case LPFC_CTX_HOST:
7936                 rc = 0;
7937                 break;
7938         default:
7939                 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
7940                         __func__, ctx_cmd);
7941                 break;
7942         }
7943
7944         return rc;
7945 }
7946
7947 /**
7948  * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
7949  * @vport: Pointer to virtual port.
7950  * @tgt_id: SCSI ID of the target.
7951  * @lun_id: LUN ID of the scsi device.
7952  * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
7953  *
7954  * This function returns number of FCP commands pending for the vport.
7955  * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
7956  * commands pending on the vport associated with SCSI device specified
7957  * by tgt_id and lun_id parameters.
7958  * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
7959  * commands pending on the vport associated with SCSI target specified
7960  * by tgt_id parameter.
7961  * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
7962  * commands pending on the vport.
7963  * This function returns the number of iocbs which satisfy the filter.
7964  * This function is called without any lock held.
7965  **/
7966 int
7967 lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
7968                   lpfc_ctx_cmd ctx_cmd)
7969 {
7970         struct lpfc_hba *phba = vport->phba;
7971         struct lpfc_iocbq *iocbq;
7972         int sum, i;
7973
7974         for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
7975                 iocbq = phba->sli.iocbq_lookup[i];
7976
7977                 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
7978                                                 ctx_cmd) == 0)
7979                         sum++;
7980         }
7981
7982         return sum;
7983 }
7984
7985 /**
7986  * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
7987  * @phba: Pointer to HBA context object
7988  * @cmdiocb: Pointer to command iocb object.
7989  * @rspiocb: Pointer to response iocb object.
7990  *
7991  * This function is called when an aborted FCP iocb completes. This
7992  * function is called by the ring event handler with no lock held.
7993  * This function frees the iocb.
7994  **/
7995 void
7996 lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7997                         struct lpfc_iocbq *rspiocb)
7998 {
7999         lpfc_sli_release_iocbq(phba, cmdiocb);
8000         return;
8001 }
8002
8003 /**
8004  * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
8005  * @vport: Pointer to virtual port.
8006  * @pring: Pointer to driver SLI ring object.
8007  * @tgt_id: SCSI ID of the target.
8008  * @lun_id: LUN ID of the scsi device.
8009  * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
8010  *
8011  * This function sends an abort command for every SCSI command
8012  * associated with the given virtual port pending on the ring
8013  * filtered by lpfc_sli_validate_fcp_iocb function.
8014  * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
8015  * FCP iocbs associated with lun specified by tgt_id and lun_id
8016  * parameters
8017  * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
8018  * FCP iocbs associated with SCSI target specified by tgt_id parameter.
8019  * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
8020  * FCP iocbs associated with virtual port.
8021  * This function returns number of iocbs it failed to abort.
8022  * This function is called with no locks held.
8023  **/
8024 int
8025 lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
8026                     uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
8027 {
8028         struct lpfc_hba *phba = vport->phba;
8029         struct lpfc_iocbq *iocbq;
8030         struct lpfc_iocbq *abtsiocb;
8031         IOCB_t *cmd = NULL;
8032         int errcnt = 0, ret_val = 0;
8033         int i;
8034
8035         for (i = 1; i <= phba->sli.last_iotag; i++) {
8036                 iocbq = phba->sli.iocbq_lookup[i];
8037
8038                 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
8039                                                abort_cmd) != 0)
8040                         continue;
8041
8042                 /* issue ABTS for this IOCB based on iotag */
8043                 abtsiocb = lpfc_sli_get_iocbq(phba);
8044                 if (abtsiocb == NULL) {
8045                         errcnt++;
8046                         continue;
8047                 }
8048
8049                 cmd = &iocbq->iocb;
8050                 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
8051                 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
8052                 if (phba->sli_rev == LPFC_SLI_REV4)
8053                         abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
8054                 else
8055                         abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
8056                 abtsiocb->iocb.ulpLe = 1;
8057                 abtsiocb->iocb.ulpClass = cmd->ulpClass;
8058                 abtsiocb->vport = phba->pport;
8059
8060                 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
8061                 abtsiocb->fcp_wqidx = iocbq->fcp_wqidx;
8062                 if (iocbq->iocb_flag & LPFC_IO_FCP)
8063                         abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
8064
8065                 if (lpfc_is_link_up(phba))
8066                         abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
8067                 else
8068                         abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
8069
8070                 /* Setup callback routine and issue the command. */
8071                 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
8072                 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
8073                                               abtsiocb, 0);
8074                 if (ret_val == IOCB_ERROR) {
8075                         lpfc_sli_release_iocbq(phba, abtsiocb);
8076                         errcnt++;
8077                         continue;
8078                 }
8079         }
8080
8081         return errcnt;
8082 }
8083
8084 /**
8085  * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
8086  * @phba: Pointer to HBA context object.
8087  * @cmdiocbq: Pointer to command iocb.
8088  * @rspiocbq: Pointer to response iocb.
8089  *
8090  * This function is the completion handler for iocbs issued using
8091  * lpfc_sli_issue_iocb_wait function. This function is called by the
8092  * ring event handler function without any lock held. This function
8093  * can be called from both worker thread context and interrupt
8094  * context. This function also can be called from other thread which
8095  * cleans up the SLI layer objects.
8096  * This function copy the contents of the response iocb to the
8097  * response iocb memory object provided by the caller of
8098  * lpfc_sli_issue_iocb_wait and then wakes up the thread which
8099  * sleeps for the iocb completion.
8100  **/
8101 static void
8102 lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
8103                         struct lpfc_iocbq *cmdiocbq,
8104                         struct lpfc_iocbq *rspiocbq)
8105 {
8106         wait_queue_head_t *pdone_q;
8107         unsigned long iflags;
8108         struct lpfc_scsi_buf *lpfc_cmd;
8109
8110         spin_lock_irqsave(&phba->hbalock, iflags);
8111         cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
8112         if (cmdiocbq->context2 && rspiocbq)
8113                 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
8114                        &rspiocbq->iocb, sizeof(IOCB_t));
8115
8116         /* Set the exchange busy flag for task management commands */
8117         if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
8118                 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
8119                 lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
8120                         cur_iocbq);
8121                 lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
8122         }
8123
8124         pdone_q = cmdiocbq->context_un.wait_queue;
8125         if (pdone_q)
8126                 wake_up(pdone_q);
8127         spin_unlock_irqrestore(&phba->hbalock, iflags);
8128         return;
8129 }
8130
8131 /**
8132  * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
8133  * @phba: Pointer to HBA context object..
8134  * @piocbq: Pointer to command iocb.
8135  * @flag: Flag to test.
8136  *
8137  * This routine grabs the hbalock and then test the iocb_flag to
8138  * see if the passed in flag is set.
8139  * Returns:
8140  * 1 if flag is set.
8141  * 0 if flag is not set.
8142  **/
8143 static int
8144 lpfc_chk_iocb_flg(struct lpfc_hba *phba,
8145                  struct lpfc_iocbq *piocbq, uint32_t flag)
8146 {
8147         unsigned long iflags;
8148         int ret;
8149
8150         spin_lock_irqsave(&phba->hbalock, iflags);
8151         ret = piocbq->iocb_flag & flag;
8152         spin_unlock_irqrestore(&phba->hbalock, iflags);
8153         return ret;
8154
8155 }
8156
8157 /**
8158  * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
8159  * @phba: Pointer to HBA context object..
8160  * @pring: Pointer to sli ring.
8161  * @piocb: Pointer to command iocb.
8162  * @prspiocbq: Pointer to response iocb.
8163  * @timeout: Timeout in number of seconds.
8164  *
8165  * This function issues the iocb to firmware and waits for the
8166  * iocb to complete. If the iocb command is not
8167  * completed within timeout seconds, it returns IOCB_TIMEDOUT.
8168  * Caller should not free the iocb resources if this function
8169  * returns IOCB_TIMEDOUT.
8170  * The function waits for the iocb completion using an
8171  * non-interruptible wait.
8172  * This function will sleep while waiting for iocb completion.
8173  * So, this function should not be called from any context which
8174  * does not allow sleeping. Due to the same reason, this function
8175  * cannot be called with interrupt disabled.
8176  * This function assumes that the iocb completions occur while
8177  * this function sleep. So, this function cannot be called from
8178  * the thread which process iocb completion for this ring.
8179  * This function clears the iocb_flag of the iocb object before
8180  * issuing the iocb and the iocb completion handler sets this
8181  * flag and wakes this thread when the iocb completes.
8182  * The contents of the response iocb will be copied to prspiocbq
8183  * by the completion handler when the command completes.
8184  * This function returns IOCB_SUCCESS when success.
8185  * This function is called with no lock held.
8186  **/
8187 int
8188 lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
8189                          uint32_t ring_number,
8190                          struct lpfc_iocbq *piocb,
8191                          struct lpfc_iocbq *prspiocbq,
8192                          uint32_t timeout)
8193 {
8194         DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
8195         long timeleft, timeout_req = 0;
8196         int retval = IOCB_SUCCESS;
8197         uint32_t creg_val;
8198         struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
8199         /*
8200          * If the caller has provided a response iocbq buffer, then context2
8201          * is NULL or its an error.
8202          */
8203         if (prspiocbq) {
8204                 if (piocb->context2)
8205                         return IOCB_ERROR;
8206                 piocb->context2 = prspiocbq;
8207         }
8208
8209         piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
8210         piocb->context_un.wait_queue = &done_q;
8211         piocb->iocb_flag &= ~LPFC_IO_WAKE;
8212
8213         if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
8214                 creg_val = readl(phba->HCregaddr);
8215                 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
8216                 writel(creg_val, phba->HCregaddr);
8217                 readl(phba->HCregaddr); /* flush */
8218         }
8219
8220         retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
8221                                      SLI_IOCB_RET_IOCB);
8222         if (retval == IOCB_SUCCESS) {
8223                 timeout_req = timeout * HZ;
8224                 timeleft = wait_event_timeout(done_q,
8225                                 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
8226                                 timeout_req);
8227
8228                 if (piocb->iocb_flag & LPFC_IO_WAKE) {
8229                         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8230                                         "0331 IOCB wake signaled\n");
8231                 } else if (timeleft == 0) {
8232                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8233                                         "0338 IOCB wait timeout error - no "
8234                                         "wake response Data x%x\n", timeout);
8235                         retval = IOCB_TIMEDOUT;
8236                 } else {
8237                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8238                                         "0330 IOCB wake NOT set, "
8239                                         "Data x%x x%lx\n",
8240                                         timeout, (timeleft / jiffies));
8241                         retval = IOCB_TIMEDOUT;
8242                 }
8243         } else if (retval == IOCB_BUSY) {
8244                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8245                         "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
8246                         phba->iocb_cnt, pring->txq_cnt, pring->txcmplq_cnt);
8247                 return retval;
8248         } else {
8249                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8250                                 "0332 IOCB wait issue failed, Data x%x\n",
8251                                 retval);
8252                 retval = IOCB_ERROR;
8253         }
8254
8255         if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
8256                 creg_val = readl(phba->HCregaddr);
8257                 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
8258                 writel(creg_val, phba->HCregaddr);
8259                 readl(phba->HCregaddr); /* flush */
8260         }
8261
8262         if (prspiocbq)
8263                 piocb->context2 = NULL;
8264
8265         piocb->context_un.wait_queue = NULL;
8266         piocb->iocb_cmpl = NULL;
8267         return retval;
8268 }
8269
8270 /**
8271  * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
8272  * @phba: Pointer to HBA context object.
8273  * @pmboxq: Pointer to driver mailbox object.
8274  * @timeout: Timeout in number of seconds.
8275  *
8276  * This function issues the mailbox to firmware and waits for the
8277  * mailbox command to complete. If the mailbox command is not
8278  * completed within timeout seconds, it returns MBX_TIMEOUT.
8279  * The function waits for the mailbox completion using an
8280  * interruptible wait. If the thread is woken up due to a
8281  * signal, MBX_TIMEOUT error is returned to the caller. Caller
8282  * should not free the mailbox resources, if this function returns
8283  * MBX_TIMEOUT.
8284  * This function will sleep while waiting for mailbox completion.
8285  * So, this function should not be called from any context which
8286  * does not allow sleeping. Due to the same reason, this function
8287  * cannot be called with interrupt disabled.
8288  * This function assumes that the mailbox completion occurs while
8289  * this function sleep. So, this function cannot be called from
8290  * the worker thread which processes mailbox completion.
8291  * This function is called in the context of HBA management
8292  * applications.
8293  * This function returns MBX_SUCCESS when successful.
8294  * This function is called with no lock held.
8295  **/
8296 int
8297 lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
8298                          uint32_t timeout)
8299 {
8300         DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
8301         int retval;
8302         unsigned long flag;
8303
8304         /* The caller must leave context1 empty. */
8305         if (pmboxq->context1)
8306                 return MBX_NOT_FINISHED;
8307
8308         pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
8309         /* setup wake call as IOCB callback */
8310         pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
8311         /* setup context field to pass wait_queue pointer to wake function  */
8312         pmboxq->context1 = &done_q;
8313
8314         /* now issue the command */
8315         retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
8316
8317         if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
8318                 wait_event_interruptible_timeout(done_q,
8319                                 pmboxq->mbox_flag & LPFC_MBX_WAKE,
8320                                 timeout * HZ);
8321
8322                 spin_lock_irqsave(&phba->hbalock, flag);
8323                 pmboxq->context1 = NULL;
8324                 /*
8325                  * if LPFC_MBX_WAKE flag is set the mailbox is completed
8326                  * else do not free the resources.
8327                  */
8328                 if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
8329                         retval = MBX_SUCCESS;
8330                         lpfc_sli4_swap_str(phba, pmboxq);
8331                 } else {
8332                         retval = MBX_TIMEOUT;
8333                         pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
8334                 }
8335                 spin_unlock_irqrestore(&phba->hbalock, flag);
8336         }
8337
8338         return retval;
8339 }
8340
8341 /**
8342  * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
8343  * @phba: Pointer to HBA context.
8344  *
8345  * This function is called to shutdown the driver's mailbox sub-system.
8346  * It first marks the mailbox sub-system is in a block state to prevent
8347  * the asynchronous mailbox command from issued off the pending mailbox
8348  * command queue. If the mailbox command sub-system shutdown is due to
8349  * HBA error conditions such as EEH or ERATT, this routine shall invoke
8350  * the mailbox sub-system flush routine to forcefully bring down the
8351  * mailbox sub-system. Otherwise, if it is due to normal condition (such
8352  * as with offline or HBA function reset), this routine will wait for the
8353  * outstanding mailbox command to complete before invoking the mailbox
8354  * sub-system flush routine to gracefully bring down mailbox sub-system.
8355  **/
8356 void
8357 lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba)
8358 {
8359         struct lpfc_sli *psli = &phba->sli;
8360         uint8_t actcmd = MBX_HEARTBEAT;
8361         unsigned long timeout;
8362
8363         spin_lock_irq(&phba->hbalock);
8364         psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
8365         spin_unlock_irq(&phba->hbalock);
8366
8367         if (psli->sli_flag & LPFC_SLI_ACTIVE) {
8368                 spin_lock_irq(&phba->hbalock);
8369                 if (phba->sli.mbox_active)
8370                         actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
8371                 spin_unlock_irq(&phba->hbalock);
8372                 /* Determine how long we might wait for the active mailbox
8373                  * command to be gracefully completed by firmware.
8374                  */
8375                 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, actcmd) *
8376                                            1000) + jiffies;
8377                 while (phba->sli.mbox_active) {
8378                         /* Check active mailbox complete status every 2ms */
8379                         msleep(2);
8380                         if (time_after(jiffies, timeout))
8381                                 /* Timeout, let the mailbox flush routine to
8382                                  * forcefully release active mailbox command
8383                                  */
8384                                 break;
8385                 }
8386         }
8387         lpfc_sli_mbox_sys_flush(phba);
8388 }
8389
8390 /**
8391  * lpfc_sli_eratt_read - read sli-3 error attention events
8392  * @phba: Pointer to HBA context.
8393  *
8394  * This function is called to read the SLI3 device error attention registers
8395  * for possible error attention events. The caller must hold the hostlock
8396  * with spin_lock_irq().
8397  *
8398  * This fucntion returns 1 when there is Error Attention in the Host Attention
8399  * Register and returns 0 otherwise.
8400  **/
8401 static int
8402 lpfc_sli_eratt_read(struct lpfc_hba *phba)
8403 {
8404         uint32_t ha_copy;
8405
8406         /* Read chip Host Attention (HA) register */
8407         ha_copy = readl(phba->HAregaddr);
8408         if (ha_copy & HA_ERATT) {
8409                 /* Read host status register to retrieve error event */
8410                 lpfc_sli_read_hs(phba);
8411
8412                 /* Check if there is a deferred error condition is active */
8413                 if ((HS_FFER1 & phba->work_hs) &&
8414                     ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
8415                       HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
8416                         phba->hba_flag |= DEFER_ERATT;
8417                         /* Clear all interrupt enable conditions */
8418                         writel(0, phba->HCregaddr);
8419                         readl(phba->HCregaddr);
8420                 }
8421
8422                 /* Set the driver HA work bitmap */
8423                 phba->work_ha |= HA_ERATT;
8424                 /* Indicate polling handles this ERATT */
8425                 phba->hba_flag |= HBA_ERATT_HANDLED;
8426                 return 1;
8427         }
8428         return 0;
8429 }
8430
8431 /**
8432  * lpfc_sli4_eratt_read - read sli-4 error attention events
8433  * @phba: Pointer to HBA context.
8434  *
8435  * This function is called to read the SLI4 device error attention registers
8436  * for possible error attention events. The caller must hold the hostlock
8437  * with spin_lock_irq().
8438  *
8439  * This fucntion returns 1 when there is Error Attention in the Host Attention
8440  * Register and returns 0 otherwise.
8441  **/
8442 static int
8443 lpfc_sli4_eratt_read(struct lpfc_hba *phba)
8444 {
8445         uint32_t uerr_sta_hi, uerr_sta_lo;
8446         uint32_t if_type, portsmphr;
8447         struct lpfc_register portstat_reg;
8448
8449         /*
8450          * For now, use the SLI4 device internal unrecoverable error
8451          * registers for error attention. This can be changed later.
8452          */
8453         if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
8454         switch (if_type) {
8455         case LPFC_SLI_INTF_IF_TYPE_0:
8456                 uerr_sta_lo = readl(phba->sli4_hba.u.if_type0.UERRLOregaddr);
8457                 uerr_sta_hi = readl(phba->sli4_hba.u.if_type0.UERRHIregaddr);
8458                 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
8459                     (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
8460                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8461                                         "1423 HBA Unrecoverable error: "
8462                                         "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
8463                                         "ue_mask_lo_reg=0x%x, "
8464                                         "ue_mask_hi_reg=0x%x\n",
8465                                         uerr_sta_lo, uerr_sta_hi,
8466                                         phba->sli4_hba.ue_mask_lo,
8467                                         phba->sli4_hba.ue_mask_hi);
8468                         phba->work_status[0] = uerr_sta_lo;
8469                         phba->work_status[1] = uerr_sta_hi;
8470                         phba->work_ha |= HA_ERATT;
8471                         phba->hba_flag |= HBA_ERATT_HANDLED;
8472                         return 1;
8473                 }
8474                 break;
8475         case LPFC_SLI_INTF_IF_TYPE_2:
8476                 portstat_reg.word0 =
8477                         readl(phba->sli4_hba.u.if_type2.STATUSregaddr);
8478                 portsmphr = readl(phba->sli4_hba.PSMPHRregaddr);
8479                 if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
8480                         phba->work_status[0] =
8481                                 readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
8482                         phba->work_status[1] =
8483                                 readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
8484                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8485                                         "2885 Port Error Detected: "
8486                                         "port status reg 0x%x, "
8487                                         "port smphr reg 0x%x, "
8488                                         "error 1=0x%x, error 2=0x%x\n",
8489                                         portstat_reg.word0,
8490                                         portsmphr,
8491                                         phba->work_status[0],
8492                                         phba->work_status[1]);
8493                         phba->work_ha |= HA_ERATT;
8494                         phba->hba_flag |= HBA_ERATT_HANDLED;
8495                         return 1;
8496                 }
8497                 break;
8498         case LPFC_SLI_INTF_IF_TYPE_1:
8499         default:
8500                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8501                                 "2886 HBA Error Attention on unsupported "
8502                                 "if type %d.", if_type);
8503                 return 1;
8504         }
8505
8506         return 0;
8507 }
8508
8509 /**
8510  * lpfc_sli_check_eratt - check error attention events
8511  * @phba: Pointer to HBA context.
8512  *
8513  * This function is called from timer soft interrupt context to check HBA's
8514  * error attention register bit for error attention events.
8515  *
8516  * This fucntion returns 1 when there is Error Attention in the Host Attention
8517  * Register and returns 0 otherwise.
8518  **/
8519 int
8520 lpfc_sli_check_eratt(struct lpfc_hba *phba)
8521 {
8522         uint32_t ha_copy;
8523
8524         /* If somebody is waiting to handle an eratt, don't process it
8525          * here. The brdkill function will do this.
8526          */
8527         if (phba->link_flag & LS_IGNORE_ERATT)
8528                 return 0;
8529
8530         /* Check if interrupt handler handles this ERATT */
8531         spin_lock_irq(&phba->hbalock);
8532         if (phba->hba_flag & HBA_ERATT_HANDLED) {
8533                 /* Interrupt handler has handled ERATT */
8534                 spin_unlock_irq(&phba->hbalock);
8535                 return 0;
8536         }
8537
8538         /*
8539          * If there is deferred error attention, do not check for error
8540          * attention
8541          */
8542         if (unlikely(phba->hba_flag & DEFER_ERATT)) {
8543                 spin_unlock_irq(&phba->hbalock);
8544                 return 0;
8545         }
8546
8547         /* If PCI channel is offline, don't process it */
8548         if (unlikely(pci_channel_offline(phba->pcidev))) {
8549                 spin_unlock_irq(&phba->hbalock);
8550                 return 0;
8551         }
8552
8553         switch (phba->sli_rev) {
8554         case LPFC_SLI_REV2:
8555         case LPFC_SLI_REV3:
8556                 /* Read chip Host Attention (HA) register */
8557                 ha_copy = lpfc_sli_eratt_read(phba);
8558                 break;
8559         case LPFC_SLI_REV4:
8560                 /* Read device Uncoverable Error (UERR) registers */
8561                 ha_copy = lpfc_sli4_eratt_read(phba);
8562                 break;
8563         default:
8564                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8565                                 "0299 Invalid SLI revision (%d)\n",
8566                                 phba->sli_rev);
8567                 ha_copy = 0;
8568                 break;
8569         }
8570         spin_unlock_irq(&phba->hbalock);
8571
8572         return ha_copy;
8573 }
8574
8575 /**
8576  * lpfc_intr_state_check - Check device state for interrupt handling
8577  * @phba: Pointer to HBA context.
8578  *
8579  * This inline routine checks whether a device or its PCI slot is in a state
8580  * that the interrupt should be handled.
8581  *
8582  * This function returns 0 if the device or the PCI slot is in a state that
8583  * interrupt should be handled, otherwise -EIO.
8584  */
8585 static inline int
8586 lpfc_intr_state_check(struct lpfc_hba *phba)
8587 {
8588         /* If the pci channel is offline, ignore all the interrupts */
8589         if (unlikely(pci_channel_offline(phba->pcidev)))
8590                 return -EIO;
8591
8592         /* Update device level interrupt statistics */
8593         phba->sli.slistat.sli_intr++;
8594
8595         /* Ignore all interrupts during initialization. */
8596         if (unlikely(phba->link_state < LPFC_LINK_DOWN))
8597                 return -EIO;
8598
8599         return 0;
8600 }
8601
8602 /**
8603  * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
8604  * @irq: Interrupt number.
8605  * @dev_id: The device context pointer.
8606  *
8607  * This function is directly called from the PCI layer as an interrupt
8608  * service routine when device with SLI-3 interface spec is enabled with
8609  * MSI-X multi-message interrupt mode and there are slow-path events in
8610  * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
8611  * interrupt mode, this function is called as part of the device-level
8612  * interrupt handler. When the PCI slot is in error recovery or the HBA
8613  * is undergoing initialization, the interrupt handler will not process
8614  * the interrupt. The link attention and ELS ring attention events are
8615  * handled by the worker thread. The interrupt handler signals the worker
8616  * thread and returns for these events. This function is called without
8617  * any lock held. It gets the hbalock to access and update SLI data
8618  * structures.
8619  *
8620  * This function returns IRQ_HANDLED when interrupt is handled else it
8621  * returns IRQ_NONE.
8622  **/
8623 irqreturn_t
8624 lpfc_sli_sp_intr_handler(int irq, void *dev_id)
8625 {
8626         struct lpfc_hba  *phba;
8627         uint32_t ha_copy, hc_copy;
8628         uint32_t work_ha_copy;
8629         unsigned long status;
8630         unsigned long iflag;
8631         uint32_t control;
8632
8633         MAILBOX_t *mbox, *pmbox;
8634         struct lpfc_vport *vport;
8635         struct lpfc_nodelist *ndlp;
8636         struct lpfc_dmabuf *mp;
8637         LPFC_MBOXQ_t *pmb;
8638         int rc;
8639
8640         /*
8641          * Get the driver's phba structure from the dev_id and
8642          * assume the HBA is not interrupting.
8643          */
8644         phba = (struct lpfc_hba *)dev_id;
8645
8646         if (unlikely(!phba))
8647                 return IRQ_NONE;
8648
8649         /*
8650          * Stuff needs to be attented to when this function is invoked as an
8651          * individual interrupt handler in MSI-X multi-message interrupt mode
8652          */
8653         if (phba->intr_type == MSIX) {
8654                 /* Check device state for handling interrupt */
8655                 if (lpfc_intr_state_check(phba))
8656                         return IRQ_NONE;
8657                 /* Need to read HA REG for slow-path events */
8658                 spin_lock_irqsave(&phba->hbalock, iflag);
8659                 ha_copy = readl(phba->HAregaddr);
8660                 /* If somebody is waiting to handle an eratt don't process it
8661                  * here. The brdkill function will do this.
8662                  */
8663                 if (phba->link_flag & LS_IGNORE_ERATT)
8664                         ha_copy &= ~HA_ERATT;
8665                 /* Check the need for handling ERATT in interrupt handler */
8666                 if (ha_copy & HA_ERATT) {
8667                         if (phba->hba_flag & HBA_ERATT_HANDLED)
8668                                 /* ERATT polling has handled ERATT */
8669                                 ha_copy &= ~HA_ERATT;
8670                         else
8671                                 /* Indicate interrupt handler handles ERATT */
8672                                 phba->hba_flag |= HBA_ERATT_HANDLED;
8673                 }
8674
8675                 /*
8676                  * If there is deferred error attention, do not check for any
8677                  * interrupt.
8678                  */
8679                 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
8680                         spin_unlock_irqrestore(&phba->hbalock, iflag);
8681                         return IRQ_NONE;
8682                 }
8683
8684                 /* Clear up only attention source related to slow-path */
8685                 hc_copy = readl(phba->HCregaddr);
8686                 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
8687                         HC_LAINT_ENA | HC_ERINT_ENA),
8688                         phba->HCregaddr);
8689                 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
8690                         phba->HAregaddr);
8691                 writel(hc_copy, phba->HCregaddr);
8692                 readl(phba->HAregaddr); /* flush */
8693                 spin_unlock_irqrestore(&phba->hbalock, iflag);
8694         } else
8695                 ha_copy = phba->ha_copy;
8696
8697         work_ha_copy = ha_copy & phba->work_ha_mask;
8698
8699         if (work_ha_copy) {
8700                 if (work_ha_copy & HA_LATT) {
8701                         if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
8702                                 /*
8703                                  * Turn off Link Attention interrupts
8704                                  * until CLEAR_LA done
8705                                  */
8706                                 spin_lock_irqsave(&phba->hbalock, iflag);
8707                                 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
8708                                 control = readl(phba->HCregaddr);
8709                                 control &= ~HC_LAINT_ENA;
8710                                 writel(control, phba->HCregaddr);
8711                                 readl(phba->HCregaddr); /* flush */
8712                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
8713                         }
8714                         else
8715                                 work_ha_copy &= ~HA_LATT;
8716                 }
8717
8718                 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
8719                         /*
8720                          * Turn off Slow Rings interrupts, LPFC_ELS_RING is
8721                          * the only slow ring.
8722                          */
8723                         status = (work_ha_copy &
8724                                 (HA_RXMASK  << (4*LPFC_ELS_RING)));
8725                         status >>= (4*LPFC_ELS_RING);
8726                         if (status & HA_RXMASK) {
8727                                 spin_lock_irqsave(&phba->hbalock, iflag);
8728                                 control = readl(phba->HCregaddr);
8729
8730                                 lpfc_debugfs_slow_ring_trc(phba,
8731                                 "ISR slow ring:   ctl:x%x stat:x%x isrcnt:x%x",
8732                                 control, status,
8733                                 (uint32_t)phba->sli.slistat.sli_intr);
8734
8735                                 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
8736                                         lpfc_debugfs_slow_ring_trc(phba,
8737                                                 "ISR Disable ring:"
8738                                                 "pwork:x%x hawork:x%x wait:x%x",
8739                                                 phba->work_ha, work_ha_copy,
8740                                                 (uint32_t)((unsigned long)
8741                                                 &phba->work_waitq));
8742
8743                                         control &=
8744                                             ~(HC_R0INT_ENA << LPFC_ELS_RING);
8745                                         writel(control, phba->HCregaddr);
8746                                         readl(phba->HCregaddr); /* flush */
8747                                 }
8748                                 else {
8749                                         lpfc_debugfs_slow_ring_trc(phba,
8750                                                 "ISR slow ring:   pwork:"
8751                                                 "x%x hawork:x%x wait:x%x",
8752                                                 phba->work_ha, work_ha_copy,
8753                                                 (uint32_t)((unsigned long)
8754                                                 &phba->work_waitq));
8755                                 }
8756                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
8757                         }
8758                 }
8759                 spin_lock_irqsave(&phba->hbalock, iflag);
8760                 if (work_ha_copy & HA_ERATT) {
8761                         lpfc_sli_read_hs(phba);
8762                         /*
8763                          * Check if there is a deferred error condition
8764                          * is active
8765                          */
8766                         if ((HS_FFER1 & phba->work_hs) &&
8767                                 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
8768                                   HS_FFER6 | HS_FFER7 | HS_FFER8) &
8769                                   phba->work_hs)) {
8770                                 phba->hba_flag |= DEFER_ERATT;
8771                                 /* Clear all interrupt enable conditions */
8772                                 writel(0, phba->HCregaddr);
8773                                 readl(phba->HCregaddr);
8774                         }
8775                 }
8776
8777                 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
8778                         pmb = phba->sli.mbox_active;
8779                         pmbox = &pmb->u.mb;
8780                         mbox = phba->mbox;
8781                         vport = pmb->vport;
8782
8783                         /* First check out the status word */
8784                         lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
8785                         if (pmbox->mbxOwner != OWN_HOST) {
8786                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
8787                                 /*
8788                                  * Stray Mailbox Interrupt, mbxCommand <cmd>
8789                                  * mbxStatus <status>
8790                                  */
8791                                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
8792                                                 LOG_SLI,
8793                                                 "(%d):0304 Stray Mailbox "
8794                                                 "Interrupt mbxCommand x%x "
8795                                                 "mbxStatus x%x\n",
8796                                                 (vport ? vport->vpi : 0),
8797                                                 pmbox->mbxCommand,
8798                                                 pmbox->mbxStatus);
8799                                 /* clear mailbox attention bit */
8800                                 work_ha_copy &= ~HA_MBATT;
8801                         } else {
8802                                 phba->sli.mbox_active = NULL;
8803                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
8804                                 phba->last_completion_time = jiffies;
8805                                 del_timer(&phba->sli.mbox_tmo);
8806                                 if (pmb->mbox_cmpl) {
8807                                         lpfc_sli_pcimem_bcopy(mbox, pmbox,
8808                                                         MAILBOX_CMD_SIZE);
8809                                         if (pmb->out_ext_byte_len &&
8810                                                 pmb->context2)
8811                                                 lpfc_sli_pcimem_bcopy(
8812                                                 phba->mbox_ext,
8813                                                 pmb->context2,
8814                                                 pmb->out_ext_byte_len);
8815                                 }
8816                                 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
8817                                         pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
8818
8819                                         lpfc_debugfs_disc_trc(vport,
8820                                                 LPFC_DISC_TRC_MBOX_VPORT,
8821                                                 "MBOX dflt rpi: : "
8822                                                 "status:x%x rpi:x%x",
8823                                                 (uint32_t)pmbox->mbxStatus,
8824                                                 pmbox->un.varWords[0], 0);
8825
8826                                         if (!pmbox->mbxStatus) {
8827                                                 mp = (struct lpfc_dmabuf *)
8828                                                         (pmb->context1);
8829                                                 ndlp = (struct lpfc_nodelist *)
8830                                                         pmb->context2;
8831
8832                                                 /* Reg_LOGIN of dflt RPI was
8833                                                  * successful. new lets get
8834                                                  * rid of the RPI using the
8835                                                  * same mbox buffer.
8836                                                  */
8837                                                 lpfc_unreg_login(phba,
8838                                                         vport->vpi,
8839                                                         pmbox->un.varWords[0],
8840                                                         pmb);
8841                                                 pmb->mbox_cmpl =
8842                                                         lpfc_mbx_cmpl_dflt_rpi;
8843                                                 pmb->context1 = mp;
8844                                                 pmb->context2 = ndlp;
8845                                                 pmb->vport = vport;
8846                                                 rc = lpfc_sli_issue_mbox(phba,
8847                                                                 pmb,
8848                                                                 MBX_NOWAIT);
8849                                                 if (rc != MBX_BUSY)
8850                                                         lpfc_printf_log(phba,
8851                                                         KERN_ERR,
8852                                                         LOG_MBOX | LOG_SLI,
8853                                                         "0350 rc should have"
8854                                                         "been MBX_BUSY\n");
8855                                                 if (rc != MBX_NOT_FINISHED)
8856                                                         goto send_current_mbox;
8857                                         }
8858                                 }
8859                                 spin_lock_irqsave(
8860                                                 &phba->pport->work_port_lock,
8861                                                 iflag);
8862                                 phba->pport->work_port_events &=
8863                                         ~WORKER_MBOX_TMO;
8864                                 spin_unlock_irqrestore(
8865                                                 &phba->pport->work_port_lock,
8866                                                 iflag);
8867                                 lpfc_mbox_cmpl_put(phba, pmb);
8868                         }
8869                 } else
8870                         spin_unlock_irqrestore(&phba->hbalock, iflag);
8871
8872                 if ((work_ha_copy & HA_MBATT) &&
8873                     (phba->sli.mbox_active == NULL)) {
8874 send_current_mbox:
8875                         /* Process next mailbox command if there is one */
8876                         do {
8877                                 rc = lpfc_sli_issue_mbox(phba, NULL,
8878                                                          MBX_NOWAIT);
8879                         } while (rc == MBX_NOT_FINISHED);
8880                         if (rc != MBX_SUCCESS)
8881                                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
8882                                                 LOG_SLI, "0349 rc should be "
8883                                                 "MBX_SUCCESS\n");
8884                 }
8885
8886                 spin_lock_irqsave(&phba->hbalock, iflag);
8887                 phba->work_ha |= work_ha_copy;
8888                 spin_unlock_irqrestore(&phba->hbalock, iflag);
8889                 lpfc_worker_wake_up(phba);
8890         }
8891         return IRQ_HANDLED;
8892
8893 } /* lpfc_sli_sp_intr_handler */
8894
8895 /**
8896  * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
8897  * @irq: Interrupt number.
8898  * @dev_id: The device context pointer.
8899  *
8900  * This function is directly called from the PCI layer as an interrupt
8901  * service routine when device with SLI-3 interface spec is enabled with
8902  * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
8903  * ring event in the HBA. However, when the device is enabled with either
8904  * MSI or Pin-IRQ interrupt mode, this function is called as part of the
8905  * device-level interrupt handler. When the PCI slot is in error recovery
8906  * or the HBA is undergoing initialization, the interrupt handler will not
8907  * process the interrupt. The SCSI FCP fast-path ring event are handled in
8908  * the intrrupt context. This function is called without any lock held.
8909  * It gets the hbalock to access and update SLI data structures.
8910  *
8911  * This function returns IRQ_HANDLED when interrupt is handled else it
8912  * returns IRQ_NONE.
8913  **/
8914 irqreturn_t
8915 lpfc_sli_fp_intr_handler(int irq, void *dev_id)
8916 {
8917         struct lpfc_hba  *phba;
8918         uint32_t ha_copy;
8919         unsigned long status;
8920         unsigned long iflag;
8921
8922         /* Get the driver's phba structure from the dev_id and
8923          * assume the HBA is not interrupting.
8924          */
8925         phba = (struct lpfc_hba *) dev_id;
8926
8927         if (unlikely(!phba))
8928                 return IRQ_NONE;
8929
8930         /*
8931          * Stuff needs to be attented to when this function is invoked as an
8932          * individual interrupt handler in MSI-X multi-message interrupt mode
8933          */
8934         if (phba->intr_type == MSIX) {
8935                 /* Check device state for handling interrupt */
8936                 if (lpfc_intr_state_check(phba))
8937                         return IRQ_NONE;
8938                 /* Need to read HA REG for FCP ring and other ring events */
8939                 ha_copy = readl(phba->HAregaddr);
8940                 /* Clear up only attention source related to fast-path */
8941                 spin_lock_irqsave(&phba->hbalock, iflag);
8942                 /*
8943                  * If there is deferred error attention, do not check for
8944                  * any interrupt.
8945                  */
8946                 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
8947                         spin_unlock_irqrestore(&phba->hbalock, iflag);
8948                         return IRQ_NONE;
8949                 }
8950                 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
8951                         phba->HAregaddr);
8952                 readl(phba->HAregaddr); /* flush */
8953                 spin_unlock_irqrestore(&phba->hbalock, iflag);
8954         } else
8955                 ha_copy = phba->ha_copy;
8956
8957         /*
8958          * Process all events on FCP ring. Take the optimized path for FCP IO.
8959          */
8960         ha_copy &= ~(phba->work_ha_mask);
8961
8962         status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
8963         status >>= (4*LPFC_FCP_RING);
8964         if (status & HA_RXMASK)
8965                 lpfc_sli_handle_fast_ring_event(phba,
8966                                                 &phba->sli.ring[LPFC_FCP_RING],
8967                                                 status);
8968
8969         if (phba->cfg_multi_ring_support == 2) {
8970                 /*
8971                  * Process all events on extra ring. Take the optimized path
8972                  * for extra ring IO.
8973                  */
8974                 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
8975                 status >>= (4*LPFC_EXTRA_RING);
8976                 if (status & HA_RXMASK) {
8977                         lpfc_sli_handle_fast_ring_event(phba,
8978                                         &phba->sli.ring[LPFC_EXTRA_RING],
8979                                         status);
8980                 }
8981         }
8982         return IRQ_HANDLED;
8983 }  /* lpfc_sli_fp_intr_handler */
8984
8985 /**
8986  * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
8987  * @irq: Interrupt number.
8988  * @dev_id: The device context pointer.
8989  *
8990  * This function is the HBA device-level interrupt handler to device with
8991  * SLI-3 interface spec, called from the PCI layer when either MSI or
8992  * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
8993  * requires driver attention. This function invokes the slow-path interrupt
8994  * attention handling function and fast-path interrupt attention handling
8995  * function in turn to process the relevant HBA attention events. This
8996  * function is called without any lock held. It gets the hbalock to access
8997  * and update SLI data structures.
8998  *
8999  * This function returns IRQ_HANDLED when interrupt is handled, else it
9000  * returns IRQ_NONE.
9001  **/
9002 irqreturn_t
9003 lpfc_sli_intr_handler(int irq, void *dev_id)
9004 {
9005         struct lpfc_hba  *phba;
9006         irqreturn_t sp_irq_rc, fp_irq_rc;
9007         unsigned long status1, status2;
9008         uint32_t hc_copy;
9009
9010         /*
9011          * Get the driver's phba structure from the dev_id and
9012          * assume the HBA is not interrupting.
9013          */
9014         phba = (struct lpfc_hba *) dev_id;
9015
9016         if (unlikely(!phba))
9017                 return IRQ_NONE;
9018
9019         /* Check device state for handling interrupt */
9020         if (lpfc_intr_state_check(phba))
9021                 return IRQ_NONE;
9022
9023         spin_lock(&phba->hbalock);
9024         phba->ha_copy = readl(phba->HAregaddr);
9025         if (unlikely(!phba->ha_copy)) {
9026                 spin_unlock(&phba->hbalock);
9027                 return IRQ_NONE;
9028         } else if (phba->ha_copy & HA_ERATT) {
9029                 if (phba->hba_flag & HBA_ERATT_HANDLED)
9030                         /* ERATT polling has handled ERATT */
9031                         phba->ha_copy &= ~HA_ERATT;
9032                 else
9033                         /* Indicate interrupt handler handles ERATT */
9034                         phba->hba_flag |= HBA_ERATT_HANDLED;
9035         }
9036
9037         /*
9038          * If there is deferred error attention, do not check for any interrupt.
9039          */
9040         if (unlikely(phba->hba_flag & DEFER_ERATT)) {
9041                 spin_unlock(&phba->hbalock);
9042                 return IRQ_NONE;
9043         }
9044
9045         /* Clear attention sources except link and error attentions */
9046         hc_copy = readl(phba->HCregaddr);
9047         writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
9048                 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
9049                 phba->HCregaddr);
9050         writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
9051         writel(hc_copy, phba->HCregaddr);
9052         readl(phba->HAregaddr); /* flush */
9053         spin_unlock(&phba->hbalock);
9054
9055         /*
9056          * Invokes slow-path host attention interrupt handling as appropriate.
9057          */
9058
9059         /* status of events with mailbox and link attention */
9060         status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
9061
9062         /* status of events with ELS ring */
9063         status2 = (phba->ha_copy & (HA_RXMASK  << (4*LPFC_ELS_RING)));
9064         status2 >>= (4*LPFC_ELS_RING);
9065
9066         if (status1 || (status2 & HA_RXMASK))
9067                 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
9068         else
9069                 sp_irq_rc = IRQ_NONE;
9070
9071         /*
9072          * Invoke fast-path host attention interrupt handling as appropriate.
9073          */
9074
9075         /* status of events with FCP ring */
9076         status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
9077         status1 >>= (4*LPFC_FCP_RING);
9078
9079         /* status of events with extra ring */
9080         if (phba->cfg_multi_ring_support == 2) {
9081                 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
9082                 status2 >>= (4*LPFC_EXTRA_RING);
9083         } else
9084                 status2 = 0;
9085
9086         if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
9087                 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
9088         else
9089                 fp_irq_rc = IRQ_NONE;
9090
9091         /* Return device-level interrupt handling status */
9092         return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
9093 }  /* lpfc_sli_intr_handler */
9094
9095 /**
9096  * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
9097  * @phba: pointer to lpfc hba data structure.
9098  *
9099  * This routine is invoked by the worker thread to process all the pending
9100  * SLI4 FCP abort XRI events.
9101  **/
9102 void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
9103 {
9104         struct lpfc_cq_event *cq_event;
9105
9106         /* First, declare the fcp xri abort event has been handled */
9107         spin_lock_irq(&phba->hbalock);
9108         phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
9109         spin_unlock_irq(&phba->hbalock);
9110         /* Now, handle all the fcp xri abort events */
9111         while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
9112                 /* Get the first event from the head of the event queue */
9113                 spin_lock_irq(&phba->hbalock);
9114                 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
9115                                  cq_event, struct lpfc_cq_event, list);
9116                 spin_unlock_irq(&phba->hbalock);
9117                 /* Notify aborted XRI for FCP work queue */
9118                 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
9119                 /* Free the event processed back to the free pool */
9120                 lpfc_sli4_cq_event_release(phba, cq_event);
9121         }
9122 }
9123
9124 /**
9125  * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
9126  * @phba: pointer to lpfc hba data structure.
9127  *
9128  * This routine is invoked by the worker thread to process all the pending
9129  * SLI4 els abort xri events.
9130  **/
9131 void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
9132 {
9133         struct lpfc_cq_event *cq_event;
9134
9135         /* First, declare the els xri abort event has been handled */
9136         spin_lock_irq(&phba->hbalock);
9137         phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
9138         spin_unlock_irq(&phba->hbalock);
9139         /* Now, handle all the els xri abort events */
9140         while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
9141                 /* Get the first event from the head of the event queue */
9142                 spin_lock_irq(&phba->hbalock);
9143                 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
9144                                  cq_event, struct lpfc_cq_event, list);
9145                 spin_unlock_irq(&phba->hbalock);
9146                 /* Notify aborted XRI for ELS work queue */
9147                 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
9148                 /* Free the event processed back to the free pool */
9149                 lpfc_sli4_cq_event_release(phba, cq_event);
9150         }
9151 }
9152
9153 /**
9154  * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
9155  * @phba: pointer to lpfc hba data structure
9156  * @pIocbIn: pointer to the rspiocbq
9157  * @pIocbOut: pointer to the cmdiocbq
9158  * @wcqe: pointer to the complete wcqe
9159  *
9160  * This routine transfers the fields of a command iocbq to a response iocbq
9161  * by copying all the IOCB fields from command iocbq and transferring the
9162  * completion status information from the complete wcqe.
9163  **/
9164 static void
9165 lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
9166                               struct lpfc_iocbq *pIocbIn,
9167                               struct lpfc_iocbq *pIocbOut,
9168                               struct lpfc_wcqe_complete *wcqe)
9169 {
9170         unsigned long iflags;
9171         size_t offset = offsetof(struct lpfc_iocbq, iocb);
9172
9173         memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
9174                sizeof(struct lpfc_iocbq) - offset);
9175         /* Map WCQE parameters into irspiocb parameters */
9176         pIocbIn->iocb.ulpStatus = bf_get(lpfc_wcqe_c_status, wcqe);
9177         if (pIocbOut->iocb_flag & LPFC_IO_FCP)
9178                 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
9179                         pIocbIn->iocb.un.fcpi.fcpi_parm =
9180                                         pIocbOut->iocb.un.fcpi.fcpi_parm -
9181                                         wcqe->total_data_placed;
9182                 else
9183                         pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
9184         else {
9185                 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
9186                 pIocbIn->iocb.un.genreq64.bdl.bdeSize = wcqe->total_data_placed;
9187         }
9188
9189         /* Pick up HBA exchange busy condition */
9190         if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
9191                 spin_lock_irqsave(&phba->hbalock, iflags);
9192                 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
9193                 spin_unlock_irqrestore(&phba->hbalock, iflags);
9194         }
9195 }
9196
9197 /**
9198  * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
9199  * @phba: Pointer to HBA context object.
9200  * @wcqe: Pointer to work-queue completion queue entry.
9201  *
9202  * This routine handles an ELS work-queue completion event and construct
9203  * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
9204  * discovery engine to handle.
9205  *
9206  * Return: Pointer to the receive IOCBQ, NULL otherwise.
9207  **/
9208 static struct lpfc_iocbq *
9209 lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
9210                                struct lpfc_iocbq *irspiocbq)
9211 {
9212         struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
9213         struct lpfc_iocbq *cmdiocbq;
9214         struct lpfc_wcqe_complete *wcqe;
9215         unsigned long iflags;
9216
9217         wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
9218         spin_lock_irqsave(&phba->hbalock, iflags);
9219         pring->stats.iocb_event++;
9220         /* Look up the ELS command IOCB and create pseudo response IOCB */
9221         cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
9222                                 bf_get(lpfc_wcqe_c_request_tag, wcqe));
9223         spin_unlock_irqrestore(&phba->hbalock, iflags);
9224
9225         if (unlikely(!cmdiocbq)) {
9226                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9227                                 "0386 ELS complete with no corresponding "
9228                                 "cmdiocb: iotag (%d)\n",
9229                                 bf_get(lpfc_wcqe_c_request_tag, wcqe));
9230                 lpfc_sli_release_iocbq(phba, irspiocbq);
9231                 return NULL;
9232         }
9233
9234         /* Fake the irspiocbq and copy necessary response information */
9235         lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
9236
9237         return irspiocbq;
9238 }
9239
9240 /**
9241  * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
9242  * @phba: Pointer to HBA context object.
9243  * @cqe: Pointer to mailbox completion queue entry.
9244  *
9245  * This routine process a mailbox completion queue entry with asynchrous
9246  * event.
9247  *
9248  * Return: true if work posted to worker thread, otherwise false.
9249  **/
9250 static bool
9251 lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
9252 {
9253         struct lpfc_cq_event *cq_event;
9254         unsigned long iflags;
9255
9256         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9257                         "0392 Async Event: word0:x%x, word1:x%x, "
9258                         "word2:x%x, word3:x%x\n", mcqe->word0,
9259                         mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
9260
9261         /* Allocate a new internal CQ_EVENT entry */
9262         cq_event = lpfc_sli4_cq_event_alloc(phba);
9263         if (!cq_event) {
9264                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9265                                 "0394 Failed to allocate CQ_EVENT entry\n");
9266                 return false;
9267         }
9268
9269         /* Move the CQE into an asynchronous event entry */
9270         memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
9271         spin_lock_irqsave(&phba->hbalock, iflags);
9272         list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
9273         /* Set the async event flag */
9274         phba->hba_flag |= ASYNC_EVENT;
9275         spin_unlock_irqrestore(&phba->hbalock, iflags);
9276
9277         return true;
9278 }
9279
9280 /**
9281  * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
9282  * @phba: Pointer to HBA context object.
9283  * @cqe: Pointer to mailbox completion queue entry.
9284  *
9285  * This routine process a mailbox completion queue entry with mailbox
9286  * completion event.
9287  *
9288  * Return: true if work posted to worker thread, otherwise false.
9289  **/
9290 static bool
9291 lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
9292 {
9293         uint32_t mcqe_status;
9294         MAILBOX_t *mbox, *pmbox;
9295         struct lpfc_mqe *mqe;
9296         struct lpfc_vport *vport;
9297         struct lpfc_nodelist *ndlp;
9298         struct lpfc_dmabuf *mp;
9299         unsigned long iflags;
9300         LPFC_MBOXQ_t *pmb;
9301         bool workposted = false;
9302         int rc;
9303
9304         /* If not a mailbox complete MCQE, out by checking mailbox consume */
9305         if (!bf_get(lpfc_trailer_completed, mcqe))
9306                 goto out_no_mqe_complete;
9307
9308         /* Get the reference to the active mbox command */
9309         spin_lock_irqsave(&phba->hbalock, iflags);
9310         pmb = phba->sli.mbox_active;
9311         if (unlikely(!pmb)) {
9312                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
9313                                 "1832 No pending MBOX command to handle\n");
9314                 spin_unlock_irqrestore(&phba->hbalock, iflags);
9315                 goto out_no_mqe_complete;
9316         }
9317         spin_unlock_irqrestore(&phba->hbalock, iflags);
9318         mqe = &pmb->u.mqe;
9319         pmbox = (MAILBOX_t *)&pmb->u.mqe;
9320         mbox = phba->mbox;
9321         vport = pmb->vport;
9322
9323         /* Reset heartbeat timer */
9324         phba->last_completion_time = jiffies;
9325         del_timer(&phba->sli.mbox_tmo);
9326
9327         /* Move mbox data to caller's mailbox region, do endian swapping */
9328         if (pmb->mbox_cmpl && mbox)
9329                 lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
9330         /* Set the mailbox status with SLI4 range 0x4000 */
9331         mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
9332         if (mcqe_status != MB_CQE_STATUS_SUCCESS)
9333                 bf_set(lpfc_mqe_status, mqe,
9334                        (LPFC_MBX_ERROR_RANGE | mcqe_status));
9335
9336         if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
9337                 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
9338                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
9339                                       "MBOX dflt rpi: status:x%x rpi:x%x",
9340                                       mcqe_status,
9341                                       pmbox->un.varWords[0], 0);
9342                 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
9343                         mp = (struct lpfc_dmabuf *)(pmb->context1);
9344                         ndlp = (struct lpfc_nodelist *)pmb->context2;
9345                         /* Reg_LOGIN of dflt RPI was successful. Now lets get
9346                          * RID of the PPI using the same mbox buffer.
9347                          */
9348                         lpfc_unreg_login(phba, vport->vpi,
9349                                          pmbox->un.varWords[0], pmb);
9350                         pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
9351                         pmb->context1 = mp;
9352                         pmb->context2 = ndlp;
9353                         pmb->vport = vport;
9354                         rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
9355                         if (rc != MBX_BUSY)
9356                                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
9357                                                 LOG_SLI, "0385 rc should "
9358                                                 "have been MBX_BUSY\n");
9359                         if (rc != MBX_NOT_FINISHED)
9360                                 goto send_current_mbox;
9361                 }
9362         }
9363         spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
9364         phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
9365         spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
9366
9367         /* There is mailbox completion work to do */
9368         spin_lock_irqsave(&phba->hbalock, iflags);
9369         __lpfc_mbox_cmpl_put(phba, pmb);
9370         phba->work_ha |= HA_MBATT;
9371         spin_unlock_irqrestore(&phba->hbalock, iflags);
9372         workposted = true;
9373
9374 send_current_mbox:
9375         spin_lock_irqsave(&phba->hbalock, iflags);
9376         /* Release the mailbox command posting token */
9377         phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
9378         /* Setting active mailbox pointer need to be in sync to flag clear */
9379         phba->sli.mbox_active = NULL;
9380         spin_unlock_irqrestore(&phba->hbalock, iflags);
9381         /* Wake up worker thread to post the next pending mailbox command */
9382         lpfc_worker_wake_up(phba);
9383 out_no_mqe_complete:
9384         if (bf_get(lpfc_trailer_consumed, mcqe))
9385                 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
9386         return workposted;
9387 }
9388
9389 /**
9390  * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
9391  * @phba: Pointer to HBA context object.
9392  * @cqe: Pointer to mailbox completion queue entry.
9393  *
9394  * This routine process a mailbox completion queue entry, it invokes the
9395  * proper mailbox complete handling or asynchrous event handling routine
9396  * according to the MCQE's async bit.
9397  *
9398  * Return: true if work posted to worker thread, otherwise false.
9399  **/
9400 static bool
9401 lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
9402 {
9403         struct lpfc_mcqe mcqe;
9404         bool workposted;
9405
9406         /* Copy the mailbox MCQE and convert endian order as needed */
9407         lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
9408
9409         /* Invoke the proper event handling routine */
9410         if (!bf_get(lpfc_trailer_async, &mcqe))
9411                 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
9412         else
9413                 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
9414         return workposted;
9415 }
9416
9417 /**
9418  * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
9419  * @phba: Pointer to HBA context object.
9420  * @wcqe: Pointer to work-queue completion queue entry.
9421  *
9422  * This routine handles an ELS work-queue completion event.
9423  *
9424  * Return: true if work posted to worker thread, otherwise false.
9425  **/
9426 static bool
9427 lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba,
9428                              struct lpfc_wcqe_complete *wcqe)
9429 {
9430         struct lpfc_iocbq *irspiocbq;
9431         unsigned long iflags;
9432         struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
9433
9434         /* Get an irspiocbq for later ELS response processing use */
9435         irspiocbq = lpfc_sli_get_iocbq(phba);
9436         if (!irspiocbq) {
9437                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9438                         "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
9439                         "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
9440                         pring->txq_cnt, phba->iocb_cnt,
9441                         phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt,
9442                         phba->sli.ring[LPFC_ELS_RING].txcmplq_cnt);
9443                 return false;
9444         }
9445
9446         /* Save off the slow-path queue event for work thread to process */
9447         memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
9448         spin_lock_irqsave(&phba->hbalock, iflags);
9449         list_add_tail(&irspiocbq->cq_event.list,
9450                       &phba->sli4_hba.sp_queue_event);
9451         phba->hba_flag |= HBA_SP_QUEUE_EVT;
9452         spin_unlock_irqrestore(&phba->hbalock, iflags);
9453
9454         return true;
9455 }
9456
9457 /**
9458  * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
9459  * @phba: Pointer to HBA context object.
9460  * @wcqe: Pointer to work-queue completion queue entry.
9461  *
9462  * This routine handles slow-path WQ entry comsumed event by invoking the
9463  * proper WQ release routine to the slow-path WQ.
9464  **/
9465 static void
9466 lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
9467                              struct lpfc_wcqe_release *wcqe)
9468 {
9469         /* Check for the slow-path ELS work queue */
9470         if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
9471                 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
9472                                      bf_get(lpfc_wcqe_r_wqe_index, wcqe));
9473         else
9474                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9475                                 "2579 Slow-path wqe consume event carries "
9476                                 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
9477                                 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
9478                                 phba->sli4_hba.els_wq->queue_id);
9479 }
9480
9481 /**
9482  * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
9483  * @phba: Pointer to HBA context object.
9484  * @cq: Pointer to a WQ completion queue.
9485  * @wcqe: Pointer to work-queue completion queue entry.
9486  *
9487  * This routine handles an XRI abort event.
9488  *
9489  * Return: true if work posted to worker thread, otherwise false.
9490  **/
9491 static bool
9492 lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
9493                                    struct lpfc_queue *cq,
9494                                    struct sli4_wcqe_xri_aborted *wcqe)
9495 {
9496         bool workposted = false;
9497         struct lpfc_cq_event *cq_event;
9498         unsigned long iflags;
9499
9500         /* Allocate a new internal CQ_EVENT entry */
9501         cq_event = lpfc_sli4_cq_event_alloc(phba);
9502         if (!cq_event) {
9503                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9504                                 "0602 Failed to allocate CQ_EVENT entry\n");
9505                 return false;
9506         }
9507
9508         /* Move the CQE into the proper xri abort event list */
9509         memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
9510         switch (cq->subtype) {
9511         case LPFC_FCP:
9512                 spin_lock_irqsave(&phba->hbalock, iflags);
9513                 list_add_tail(&cq_event->list,
9514                               &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
9515                 /* Set the fcp xri abort event flag */
9516                 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
9517                 spin_unlock_irqrestore(&phba->hbalock, iflags);
9518                 workposted = true;
9519                 break;
9520         case LPFC_ELS:
9521                 spin_lock_irqsave(&phba->hbalock, iflags);
9522                 list_add_tail(&cq_event->list,
9523                               &phba->sli4_hba.sp_els_xri_aborted_work_queue);
9524                 /* Set the els xri abort event flag */
9525                 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
9526                 spin_unlock_irqrestore(&phba->hbalock, iflags);
9527                 workposted = true;
9528                 break;
9529         default:
9530                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9531                                 "0603 Invalid work queue CQE subtype (x%x)\n",
9532                                 cq->subtype);
9533                 workposted = false;
9534                 break;
9535         }
9536         return workposted;
9537 }
9538
9539 /**
9540  * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
9541  * @phba: Pointer to HBA context object.
9542  * @rcqe: Pointer to receive-queue completion queue entry.
9543  *
9544  * This routine process a receive-queue completion queue entry.
9545  *
9546  * Return: true if work posted to worker thread, otherwise false.
9547  **/
9548 static bool
9549 lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
9550 {
9551         bool workposted = false;
9552         struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
9553         struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
9554         struct hbq_dmabuf *dma_buf;
9555         uint32_t status;
9556         unsigned long iflags;
9557
9558         if (bf_get(lpfc_rcqe_rq_id, rcqe) != hrq->queue_id)
9559                 goto out;
9560
9561         status = bf_get(lpfc_rcqe_status, rcqe);
9562         switch (status) {
9563         case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
9564                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9565                                 "2537 Receive Frame Truncated!!\n");
9566         case FC_STATUS_RQ_SUCCESS:
9567                 lpfc_sli4_rq_release(hrq, drq);
9568                 spin_lock_irqsave(&phba->hbalock, iflags);
9569                 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
9570                 if (!dma_buf) {
9571                         spin_unlock_irqrestore(&phba->hbalock, iflags);
9572                         goto out;
9573                 }
9574                 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
9575                 /* save off the frame for the word thread to process */
9576                 list_add_tail(&dma_buf->cq_event.list,
9577                               &phba->sli4_hba.sp_queue_event);
9578                 /* Frame received */
9579                 phba->hba_flag |= HBA_SP_QUEUE_EVT;
9580                 spin_unlock_irqrestore(&phba->hbalock, iflags);
9581                 workposted = true;
9582                 break;
9583         case FC_STATUS_INSUFF_BUF_NEED_BUF:
9584         case FC_STATUS_INSUFF_BUF_FRM_DISC:
9585                 /* Post more buffers if possible */
9586                 spin_lock_irqsave(&phba->hbalock, iflags);
9587                 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
9588                 spin_unlock_irqrestore(&phba->hbalock, iflags);
9589                 workposted = true;
9590                 break;
9591         }
9592 out:
9593         return workposted;
9594 }
9595
9596 /**
9597  * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
9598  * @phba: Pointer to HBA context object.
9599  * @cq: Pointer to the completion queue.
9600  * @wcqe: Pointer to a completion queue entry.
9601  *
9602  * This routine process a slow-path work-queue or recieve queue completion queue
9603  * entry.
9604  *
9605  * Return: true if work posted to worker thread, otherwise false.
9606  **/
9607 static bool
9608 lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
9609                          struct lpfc_cqe *cqe)
9610 {
9611         struct lpfc_cqe cqevt;
9612         bool workposted = false;
9613
9614         /* Copy the work queue CQE and convert endian order if needed */
9615         lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
9616
9617         /* Check and process for different type of WCQE and dispatch */
9618         switch (bf_get(lpfc_cqe_code, &cqevt)) {
9619         case CQE_CODE_COMPL_WQE:
9620                 /* Process the WQ/RQ complete event */
9621                 phba->last_completion_time = jiffies;
9622                 workposted = lpfc_sli4_sp_handle_els_wcqe(phba,
9623                                 (struct lpfc_wcqe_complete *)&cqevt);
9624                 break;
9625         case CQE_CODE_RELEASE_WQE:
9626                 /* Process the WQ release event */
9627                 lpfc_sli4_sp_handle_rel_wcqe(phba,
9628                                 (struct lpfc_wcqe_release *)&cqevt);
9629                 break;
9630         case CQE_CODE_XRI_ABORTED:
9631                 /* Process the WQ XRI abort event */
9632                 phba->last_completion_time = jiffies;
9633                 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
9634                                 (struct sli4_wcqe_xri_aborted *)&cqevt);
9635                 break;
9636         case CQE_CODE_RECEIVE:
9637                 /* Process the RQ event */
9638                 phba->last_completion_time = jiffies;
9639                 workposted = lpfc_sli4_sp_handle_rcqe(phba,
9640                                 (struct lpfc_rcqe *)&cqevt);
9641                 break;
9642         default:
9643                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9644                                 "0388 Not a valid WCQE code: x%x\n",
9645                                 bf_get(lpfc_cqe_code, &cqevt));
9646                 break;
9647         }
9648         return workposted;
9649 }
9650
9651 /**
9652  * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
9653  * @phba: Pointer to HBA context object.
9654  * @eqe: Pointer to fast-path event queue entry.
9655  *
9656  * This routine process a event queue entry from the slow-path event queue.
9657  * It will check the MajorCode and MinorCode to determine this is for a
9658  * completion event on a completion queue, if not, an error shall be logged
9659  * and just return. Otherwise, it will get to the corresponding completion
9660  * queue and process all the entries on that completion queue, rearm the
9661  * completion queue, and then return.
9662  *
9663  **/
9664 static void
9665 lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
9666 {
9667         struct lpfc_queue *cq = NULL, *childq, *speq;
9668         struct lpfc_cqe *cqe;
9669         bool workposted = false;
9670         int ecount = 0;
9671         uint16_t cqid;
9672
9673         if (bf_get_le32(lpfc_eqe_major_code, eqe) != 0) {
9674                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9675                                 "0359 Not a valid slow-path completion "
9676                                 "event: majorcode=x%x, minorcode=x%x\n",
9677                                 bf_get_le32(lpfc_eqe_major_code, eqe),
9678                                 bf_get_le32(lpfc_eqe_minor_code, eqe));
9679                 return;
9680         }
9681
9682         /* Get the reference to the corresponding CQ */
9683         cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
9684
9685         /* Search for completion queue pointer matching this cqid */
9686         speq = phba->sli4_hba.sp_eq;
9687         list_for_each_entry(childq, &speq->child_list, list) {
9688                 if (childq->queue_id == cqid) {
9689                         cq = childq;
9690                         break;
9691                 }
9692         }
9693         if (unlikely(!cq)) {
9694                 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
9695                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9696                                         "0365 Slow-path CQ identifier "
9697                                         "(%d) does not exist\n", cqid);
9698                 return;
9699         }
9700
9701         /* Process all the entries to the CQ */
9702         switch (cq->type) {
9703         case LPFC_MCQ:
9704                 while ((cqe = lpfc_sli4_cq_get(cq))) {
9705                         workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
9706                         if (!(++ecount % LPFC_GET_QE_REL_INT))
9707                                 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
9708                 }
9709                 break;
9710         case LPFC_WCQ:
9711                 while ((cqe = lpfc_sli4_cq_get(cq))) {
9712                         workposted |= lpfc_sli4_sp_handle_cqe(phba, cq, cqe);
9713                         if (!(++ecount % LPFC_GET_QE_REL_INT))
9714                                 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
9715                 }
9716                 break;
9717         default:
9718                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9719                                 "0370 Invalid completion queue type (%d)\n",
9720                                 cq->type);
9721                 return;
9722         }
9723
9724         /* Catch the no cq entry condition, log an error */
9725         if (unlikely(ecount == 0))
9726                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9727                                 "0371 No entry from the CQ: identifier "
9728                                 "(x%x), type (%d)\n", cq->queue_id, cq->type);
9729
9730         /* In any case, flash and re-arm the RCQ */
9731         lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
9732
9733         /* wake up worker thread if there are works to be done */
9734         if (workposted)
9735                 lpfc_worker_wake_up(phba);
9736 }
9737
9738 /**
9739  * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
9740  * @eqe: Pointer to fast-path completion queue entry.
9741  *
9742  * This routine process a fast-path work queue completion entry from fast-path
9743  * event queue for FCP command response completion.
9744  **/
9745 static void
9746 lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba,
9747                              struct lpfc_wcqe_complete *wcqe)
9748 {
9749         struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
9750         struct lpfc_iocbq *cmdiocbq;
9751         struct lpfc_iocbq irspiocbq;
9752         unsigned long iflags;
9753
9754         spin_lock_irqsave(&phba->hbalock, iflags);
9755         pring->stats.iocb_event++;
9756         spin_unlock_irqrestore(&phba->hbalock, iflags);
9757
9758         /* Check for response status */
9759         if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
9760                 /* If resource errors reported from HBA, reduce queue
9761                  * depth of the SCSI device.
9762                  */
9763                 if ((bf_get(lpfc_wcqe_c_status, wcqe) ==
9764                      IOSTAT_LOCAL_REJECT) &&
9765                     (wcqe->parameter == IOERR_NO_RESOURCES)) {
9766                         phba->lpfc_rampdown_queue_depth(phba);
9767                 }
9768                 /* Log the error status */
9769                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9770                                 "0373 FCP complete error: status=x%x, "
9771                                 "hw_status=x%x, total_data_specified=%d, "
9772                                 "parameter=x%x, word3=x%x\n",
9773                                 bf_get(lpfc_wcqe_c_status, wcqe),
9774                                 bf_get(lpfc_wcqe_c_hw_status, wcqe),
9775                                 wcqe->total_data_placed, wcqe->parameter,
9776                                 wcqe->word3);
9777         }
9778
9779         /* Look up the FCP command IOCB and create pseudo response IOCB */
9780         spin_lock_irqsave(&phba->hbalock, iflags);
9781         cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
9782                                 bf_get(lpfc_wcqe_c_request_tag, wcqe));
9783         spin_unlock_irqrestore(&phba->hbalock, iflags);
9784         if (unlikely(!cmdiocbq)) {
9785                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9786                                 "0374 FCP complete with no corresponding "
9787                                 "cmdiocb: iotag (%d)\n",
9788                                 bf_get(lpfc_wcqe_c_request_tag, wcqe));
9789                 return;
9790         }
9791         if (unlikely(!cmdiocbq->iocb_cmpl)) {
9792                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9793                                 "0375 FCP cmdiocb not callback function "
9794                                 "iotag: (%d)\n",
9795                                 bf_get(lpfc_wcqe_c_request_tag, wcqe));
9796                 return;
9797         }
9798
9799         /* Fake the irspiocb and copy necessary response information */
9800         lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
9801
9802         if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
9803                 spin_lock_irqsave(&phba->hbalock, iflags);
9804                 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
9805                 spin_unlock_irqrestore(&phba->hbalock, iflags);
9806         }
9807
9808         /* Pass the cmd_iocb and the rsp state to the upper layer */
9809         (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
9810 }
9811
9812 /**
9813  * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
9814  * @phba: Pointer to HBA context object.
9815  * @cq: Pointer to completion queue.
9816  * @wcqe: Pointer to work-queue completion queue entry.
9817  *
9818  * This routine handles an fast-path WQ entry comsumed event by invoking the
9819  * proper WQ release routine to the slow-path WQ.
9820  **/
9821 static void
9822 lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
9823                              struct lpfc_wcqe_release *wcqe)
9824 {
9825         struct lpfc_queue *childwq;
9826         bool wqid_matched = false;
9827         uint16_t fcp_wqid;
9828
9829         /* Check for fast-path FCP work queue release */
9830         fcp_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
9831         list_for_each_entry(childwq, &cq->child_list, list) {
9832                 if (childwq->queue_id == fcp_wqid) {
9833                         lpfc_sli4_wq_release(childwq,
9834                                         bf_get(lpfc_wcqe_r_wqe_index, wcqe));
9835                         wqid_matched = true;
9836                         break;
9837                 }
9838         }
9839         /* Report warning log message if no match found */
9840         if (wqid_matched != true)
9841                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9842                                 "2580 Fast-path wqe consume event carries "
9843                                 "miss-matched qid: wcqe-qid=x%x\n", fcp_wqid);
9844 }
9845
9846 /**
9847  * lpfc_sli4_fp_handle_wcqe - Process fast-path work queue completion entry
9848  * @cq: Pointer to the completion queue.
9849  * @eqe: Pointer to fast-path completion queue entry.
9850  *
9851  * This routine process a fast-path work queue completion entry from fast-path
9852  * event queue for FCP command response completion.
9853  **/
9854 static int
9855 lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
9856                          struct lpfc_cqe *cqe)
9857 {
9858         struct lpfc_wcqe_release wcqe;
9859         bool workposted = false;
9860
9861         /* Copy the work queue CQE and convert endian order if needed */
9862         lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
9863
9864         /* Check and process for different type of WCQE and dispatch */
9865         switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
9866         case CQE_CODE_COMPL_WQE:
9867                 /* Process the WQ complete event */
9868                 phba->last_completion_time = jiffies;
9869                 lpfc_sli4_fp_handle_fcp_wcqe(phba,
9870                                 (struct lpfc_wcqe_complete *)&wcqe);
9871                 break;
9872         case CQE_CODE_RELEASE_WQE:
9873                 /* Process the WQ release event */
9874                 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
9875                                 (struct lpfc_wcqe_release *)&wcqe);
9876                 break;
9877         case CQE_CODE_XRI_ABORTED:
9878                 /* Process the WQ XRI abort event */
9879                 phba->last_completion_time = jiffies;
9880                 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
9881                                 (struct sli4_wcqe_xri_aborted *)&wcqe);
9882                 break;
9883         default:
9884                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9885                                 "0144 Not a valid WCQE code: x%x\n",
9886                                 bf_get(lpfc_wcqe_c_code, &wcqe));
9887                 break;
9888         }
9889         return workposted;
9890 }
9891
9892 /**
9893  * lpfc_sli4_fp_handle_eqe - Process a fast-path event queue entry
9894  * @phba: Pointer to HBA context object.
9895  * @eqe: Pointer to fast-path event queue entry.
9896  *
9897  * This routine process a event queue entry from the fast-path event queue.
9898  * It will check the MajorCode and MinorCode to determine this is for a
9899  * completion event on a completion queue, if not, an error shall be logged
9900  * and just return. Otherwise, it will get to the corresponding completion
9901  * queue and process all the entries on the completion queue, rearm the
9902  * completion queue, and then return.
9903  **/
9904 static void
9905 lpfc_sli4_fp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
9906                         uint32_t fcp_cqidx)
9907 {
9908         struct lpfc_queue *cq;
9909         struct lpfc_cqe *cqe;
9910         bool workposted = false;
9911         uint16_t cqid;
9912         int ecount = 0;
9913
9914         if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
9915                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9916                                 "0366 Not a valid fast-path completion "
9917                                 "event: majorcode=x%x, minorcode=x%x\n",
9918                                 bf_get_le32(lpfc_eqe_major_code, eqe),
9919                                 bf_get_le32(lpfc_eqe_minor_code, eqe));
9920                 return;
9921         }
9922
9923         cq = phba->sli4_hba.fcp_cq[fcp_cqidx];
9924         if (unlikely(!cq)) {
9925                 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
9926                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9927                                         "0367 Fast-path completion queue "
9928                                         "does not exist\n");
9929                 return;
9930         }
9931
9932         /* Get the reference to the corresponding CQ */
9933         cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
9934         if (unlikely(cqid != cq->queue_id)) {
9935                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9936                                 "0368 Miss-matched fast-path completion "
9937                                 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
9938                                 cqid, cq->queue_id);
9939                 return;
9940         }
9941
9942         /* Process all the entries to the CQ */
9943         while ((cqe = lpfc_sli4_cq_get(cq))) {
9944                 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
9945                 if (!(++ecount % LPFC_GET_QE_REL_INT))
9946                         lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
9947         }
9948
9949         /* Catch the no cq entry condition */
9950         if (unlikely(ecount == 0))
9951                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9952                                 "0369 No entry from fast-path completion "
9953                                 "queue fcpcqid=%d\n", cq->queue_id);
9954
9955         /* In any case, flash and re-arm the CQ */
9956         lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
9957
9958         /* wake up worker thread if there are works to be done */
9959         if (workposted)
9960                 lpfc_worker_wake_up(phba);
9961 }
9962
9963 static void
9964 lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
9965 {
9966         struct lpfc_eqe *eqe;
9967
9968         /* walk all the EQ entries and drop on the floor */
9969         while ((eqe = lpfc_sli4_eq_get(eq)))
9970                 ;
9971
9972         /* Clear and re-arm the EQ */
9973         lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
9974 }
9975
9976 /**
9977  * lpfc_sli4_sp_intr_handler - Slow-path interrupt handler to SLI-4 device
9978  * @irq: Interrupt number.
9979  * @dev_id: The device context pointer.
9980  *
9981  * This function is directly called from the PCI layer as an interrupt
9982  * service routine when device with SLI-4 interface spec is enabled with
9983  * MSI-X multi-message interrupt mode and there are slow-path events in
9984  * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
9985  * interrupt mode, this function is called as part of the device-level
9986  * interrupt handler. When the PCI slot is in error recovery or the HBA is
9987  * undergoing initialization, the interrupt handler will not process the
9988  * interrupt. The link attention and ELS ring attention events are handled
9989  * by the worker thread. The interrupt handler signals the worker thread
9990  * and returns for these events. This function is called without any lock
9991  * held. It gets the hbalock to access and update SLI data structures.
9992  *
9993  * This function returns IRQ_HANDLED when interrupt is handled else it
9994  * returns IRQ_NONE.
9995  **/
9996 irqreturn_t
9997 lpfc_sli4_sp_intr_handler(int irq, void *dev_id)
9998 {
9999         struct lpfc_hba *phba;
10000         struct lpfc_queue *speq;
10001         struct lpfc_eqe *eqe;
10002         unsigned long iflag;
10003         int ecount = 0;
10004
10005         /*
10006          * Get the driver's phba structure from the dev_id
10007          */
10008         phba = (struct lpfc_hba *)dev_id;
10009
10010         if (unlikely(!phba))
10011                 return IRQ_NONE;
10012
10013         /* Get to the EQ struct associated with this vector */
10014         speq = phba->sli4_hba.sp_eq;
10015
10016         /* Check device state for handling interrupt */
10017         if (unlikely(lpfc_intr_state_check(phba))) {
10018                 /* Check again for link_state with lock held */
10019                 spin_lock_irqsave(&phba->hbalock, iflag);
10020                 if (phba->link_state < LPFC_LINK_DOWN)
10021                         /* Flush, clear interrupt, and rearm the EQ */
10022                         lpfc_sli4_eq_flush(phba, speq);
10023                 spin_unlock_irqrestore(&phba->hbalock, iflag);
10024                 return IRQ_NONE;
10025         }
10026
10027         /*
10028          * Process all the event on FCP slow-path EQ
10029          */
10030         while ((eqe = lpfc_sli4_eq_get(speq))) {
10031                 lpfc_sli4_sp_handle_eqe(phba, eqe);
10032                 if (!(++ecount % LPFC_GET_QE_REL_INT))
10033                         lpfc_sli4_eq_release(speq, LPFC_QUEUE_NOARM);
10034         }
10035
10036         /* Always clear and re-arm the slow-path EQ */
10037         lpfc_sli4_eq_release(speq, LPFC_QUEUE_REARM);
10038
10039         /* Catch the no cq entry condition */
10040         if (unlikely(ecount == 0)) {
10041                 if (phba->intr_type == MSIX)
10042                         /* MSI-X treated interrupt served as no EQ share INT */
10043                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
10044                                         "0357 MSI-X interrupt with no EQE\n");
10045                 else
10046                         /* Non MSI-X treated on interrupt as EQ share INT */
10047                         return IRQ_NONE;
10048         }
10049
10050         return IRQ_HANDLED;
10051 } /* lpfc_sli4_sp_intr_handler */
10052
10053 /**
10054  * lpfc_sli4_fp_intr_handler - Fast-path interrupt handler to SLI-4 device
10055  * @irq: Interrupt number.
10056  * @dev_id: The device context pointer.
10057  *
10058  * This function is directly called from the PCI layer as an interrupt
10059  * service routine when device with SLI-4 interface spec is enabled with
10060  * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
10061  * ring event in the HBA. However, when the device is enabled with either
10062  * MSI or Pin-IRQ interrupt mode, this function is called as part of the
10063  * device-level interrupt handler. When the PCI slot is in error recovery
10064  * or the HBA is undergoing initialization, the interrupt handler will not
10065  * process the interrupt. The SCSI FCP fast-path ring event are handled in
10066  * the intrrupt context. This function is called without any lock held.
10067  * It gets the hbalock to access and update SLI data structures. Note that,
10068  * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
10069  * equal to that of FCP CQ index.
10070  *
10071  * This function returns IRQ_HANDLED when interrupt is handled else it
10072  * returns IRQ_NONE.
10073  **/
10074 irqreturn_t
10075 lpfc_sli4_fp_intr_handler(int irq, void *dev_id)
10076 {
10077         struct lpfc_hba *phba;
10078         struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
10079         struct lpfc_queue *fpeq;
10080         struct lpfc_eqe *eqe;
10081         unsigned long iflag;
10082         int ecount = 0;
10083         uint32_t fcp_eqidx;
10084
10085         /* Get the driver's phba structure from the dev_id */
10086         fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
10087         phba = fcp_eq_hdl->phba;
10088         fcp_eqidx = fcp_eq_hdl->idx;
10089
10090         if (unlikely(!phba))
10091                 return IRQ_NONE;
10092
10093         /* Get to the EQ struct associated with this vector */
10094         fpeq = phba->sli4_hba.fp_eq[fcp_eqidx];
10095
10096         /* Check device state for handling interrupt */
10097         if (unlikely(lpfc_intr_state_check(phba))) {
10098                 /* Check again for link_state with lock held */
10099                 spin_lock_irqsave(&phba->hbalock, iflag);
10100                 if (phba->link_state < LPFC_LINK_DOWN)
10101                         /* Flush, clear interrupt, and rearm the EQ */
10102                         lpfc_sli4_eq_flush(phba, fpeq);
10103                 spin_unlock_irqrestore(&phba->hbalock, iflag);
10104                 return IRQ_NONE;
10105         }
10106
10107         /*
10108          * Process all the event on FCP fast-path EQ
10109          */
10110         while ((eqe = lpfc_sli4_eq_get(fpeq))) {
10111                 lpfc_sli4_fp_handle_eqe(phba, eqe, fcp_eqidx);
10112                 if (!(++ecount % LPFC_GET_QE_REL_INT))
10113                         lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
10114         }
10115
10116         /* Always clear and re-arm the fast-path EQ */
10117         lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
10118
10119         if (unlikely(ecount == 0)) {
10120                 if (phba->intr_type == MSIX)
10121                         /* MSI-X treated interrupt served as no EQ share INT */
10122                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
10123                                         "0358 MSI-X interrupt with no EQE\n");
10124                 else
10125                         /* Non MSI-X treated on interrupt as EQ share INT */
10126                         return IRQ_NONE;
10127         }
10128
10129         return IRQ_HANDLED;
10130 } /* lpfc_sli4_fp_intr_handler */
10131
10132 /**
10133  * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
10134  * @irq: Interrupt number.
10135  * @dev_id: The device context pointer.
10136  *
10137  * This function is the device-level interrupt handler to device with SLI-4
10138  * interface spec, called from the PCI layer when either MSI or Pin-IRQ
10139  * interrupt mode is enabled and there is an event in the HBA which requires
10140  * driver attention. This function invokes the slow-path interrupt attention
10141  * handling function and fast-path interrupt attention handling function in
10142  * turn to process the relevant HBA attention events. This function is called
10143  * without any lock held. It gets the hbalock to access and update SLI data
10144  * structures.
10145  *
10146  * This function returns IRQ_HANDLED when interrupt is handled, else it
10147  * returns IRQ_NONE.
10148  **/
10149 irqreturn_t
10150 lpfc_sli4_intr_handler(int irq, void *dev_id)
10151 {
10152         struct lpfc_hba  *phba;
10153         irqreturn_t sp_irq_rc, fp_irq_rc;
10154         bool fp_handled = false;
10155         uint32_t fcp_eqidx;
10156
10157         /* Get the driver's phba structure from the dev_id */
10158         phba = (struct lpfc_hba *)dev_id;
10159
10160         if (unlikely(!phba))
10161                 return IRQ_NONE;
10162
10163         /*
10164          * Invokes slow-path host attention interrupt handling as appropriate.
10165          */
10166         sp_irq_rc = lpfc_sli4_sp_intr_handler(irq, dev_id);
10167
10168         /*
10169          * Invoke fast-path host attention interrupt handling as appropriate.
10170          */
10171         for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
10172                 fp_irq_rc = lpfc_sli4_fp_intr_handler(irq,
10173                                         &phba->sli4_hba.fcp_eq_hdl[fcp_eqidx]);
10174                 if (fp_irq_rc == IRQ_HANDLED)
10175                         fp_handled |= true;
10176         }
10177
10178         return (fp_handled == true) ? IRQ_HANDLED : sp_irq_rc;
10179 } /* lpfc_sli4_intr_handler */
10180
10181 /**
10182  * lpfc_sli4_queue_free - free a queue structure and associated memory
10183  * @queue: The queue structure to free.
10184  *
10185  * This function frees a queue structure and the DMAable memeory used for
10186  * the host resident queue. This function must be called after destroying the
10187  * queue on the HBA.
10188  **/
10189 void
10190 lpfc_sli4_queue_free(struct lpfc_queue *queue)
10191 {
10192         struct lpfc_dmabuf *dmabuf;
10193
10194         if (!queue)
10195                 return;
10196
10197         while (!list_empty(&queue->page_list)) {
10198                 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
10199                                  list);
10200                 dma_free_coherent(&queue->phba->pcidev->dev, SLI4_PAGE_SIZE,
10201                                   dmabuf->virt, dmabuf->phys);
10202                 kfree(dmabuf);
10203         }
10204         kfree(queue);
10205         return;
10206 }
10207
10208 /**
10209  * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
10210  * @phba: The HBA that this queue is being created on.
10211  * @entry_size: The size of each queue entry for this queue.
10212  * @entry count: The number of entries that this queue will handle.
10213  *
10214  * This function allocates a queue structure and the DMAable memory used for
10215  * the host resident queue. This function must be called before creating the
10216  * queue on the HBA.
10217  **/
10218 struct lpfc_queue *
10219 lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
10220                       uint32_t entry_count)
10221 {
10222         struct lpfc_queue *queue;
10223         struct lpfc_dmabuf *dmabuf;
10224         int x, total_qe_count;
10225         void *dma_pointer;
10226         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
10227
10228         if (!phba->sli4_hba.pc_sli4_params.supported)
10229                 hw_page_size = SLI4_PAGE_SIZE;
10230
10231         queue = kzalloc(sizeof(struct lpfc_queue) +
10232                         (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
10233         if (!queue)
10234                 return NULL;
10235         queue->page_count = (ALIGN(entry_size * entry_count,
10236                         hw_page_size))/hw_page_size;
10237         INIT_LIST_HEAD(&queue->list);
10238         INIT_LIST_HEAD(&queue->page_list);
10239         INIT_LIST_HEAD(&queue->child_list);
10240         for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
10241                 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
10242                 if (!dmabuf)
10243                         goto out_fail;
10244                 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
10245                                                   hw_page_size, &dmabuf->phys,
10246                                                   GFP_KERNEL);
10247                 if (!dmabuf->virt) {
10248                         kfree(dmabuf);
10249                         goto out_fail;
10250                 }
10251                 memset(dmabuf->virt, 0, hw_page_size);
10252                 dmabuf->buffer_tag = x;
10253                 list_add_tail(&dmabuf->list, &queue->page_list);
10254                 /* initialize queue's entry array */
10255                 dma_pointer = dmabuf->virt;
10256                 for (; total_qe_count < entry_count &&
10257                      dma_pointer < (hw_page_size + dmabuf->virt);
10258                      total_qe_count++, dma_pointer += entry_size) {
10259                         queue->qe[total_qe_count].address = dma_pointer;
10260                 }
10261         }
10262         queue->entry_size = entry_size;
10263         queue->entry_count = entry_count;
10264         queue->phba = phba;
10265
10266         return queue;
10267 out_fail:
10268         lpfc_sli4_queue_free(queue);
10269         return NULL;
10270 }
10271
10272 /**
10273  * lpfc_eq_create - Create an Event Queue on the HBA
10274  * @phba: HBA structure that indicates port to create a queue on.
10275  * @eq: The queue structure to use to create the event queue.
10276  * @imax: The maximum interrupt per second limit.
10277  *
10278  * This function creates an event queue, as detailed in @eq, on a port,
10279  * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
10280  *
10281  * The @phba struct is used to send mailbox command to HBA. The @eq struct
10282  * is used to get the entry count and entry size that are necessary to
10283  * determine the number of pages to allocate and use for this queue. This
10284  * function will send the EQ_CREATE mailbox command to the HBA to setup the
10285  * event queue. This function is asynchronous and will wait for the mailbox
10286  * command to finish before continuing.
10287  *
10288  * On success this function will return a zero. If unable to allocate enough
10289  * memory this function will return -ENOMEM. If the queue create mailbox command
10290  * fails this function will return -ENXIO.
10291  **/
10292 uint32_t
10293 lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint16_t imax)
10294 {
10295         struct lpfc_mbx_eq_create *eq_create;
10296         LPFC_MBOXQ_t *mbox;
10297         int rc, length, status = 0;
10298         struct lpfc_dmabuf *dmabuf;
10299         uint32_t shdr_status, shdr_add_status;
10300         union lpfc_sli4_cfg_shdr *shdr;
10301         uint16_t dmult;
10302         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
10303
10304         if (!phba->sli4_hba.pc_sli4_params.supported)
10305                 hw_page_size = SLI4_PAGE_SIZE;
10306
10307         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10308         if (!mbox)
10309                 return -ENOMEM;
10310         length = (sizeof(struct lpfc_mbx_eq_create) -
10311                   sizeof(struct lpfc_sli4_cfg_mhdr));
10312         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10313                          LPFC_MBOX_OPCODE_EQ_CREATE,
10314                          length, LPFC_SLI4_MBX_EMBED);
10315         eq_create = &mbox->u.mqe.un.eq_create;
10316         bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
10317                eq->page_count);
10318         bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
10319                LPFC_EQE_SIZE);
10320         bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
10321         /* Calculate delay multiper from maximum interrupt per second */
10322         dmult = LPFC_DMULT_CONST/imax - 1;
10323         bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
10324                dmult);
10325         switch (eq->entry_count) {
10326         default:
10327                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10328                                 "0360 Unsupported EQ count. (%d)\n",
10329                                 eq->entry_count);
10330                 if (eq->entry_count < 256)
10331                         return -EINVAL;
10332                 /* otherwise default to smallest count (drop through) */
10333         case 256:
10334                 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
10335                        LPFC_EQ_CNT_256);
10336                 break;
10337         case 512:
10338                 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
10339                        LPFC_EQ_CNT_512);
10340                 break;
10341         case 1024:
10342                 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
10343                        LPFC_EQ_CNT_1024);
10344                 break;
10345         case 2048:
10346                 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
10347                        LPFC_EQ_CNT_2048);
10348                 break;
10349         case 4096:
10350                 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
10351                        LPFC_EQ_CNT_4096);
10352                 break;
10353         }
10354         list_for_each_entry(dmabuf, &eq->page_list, list) {
10355                 memset(dmabuf->virt, 0, hw_page_size);
10356                 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
10357                                         putPaddrLow(dmabuf->phys);
10358                 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
10359                                         putPaddrHigh(dmabuf->phys);
10360         }
10361         mbox->vport = phba->pport;
10362         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10363         mbox->context1 = NULL;
10364         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10365         shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
10366         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10367         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10368         if (shdr_status || shdr_add_status || rc) {
10369                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10370                                 "2500 EQ_CREATE mailbox failed with "
10371                                 "status x%x add_status x%x, mbx status x%x\n",
10372                                 shdr_status, shdr_add_status, rc);
10373                 status = -ENXIO;
10374         }
10375         eq->type = LPFC_EQ;
10376         eq->subtype = LPFC_NONE;
10377         eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
10378         if (eq->queue_id == 0xFFFF)
10379                 status = -ENXIO;
10380         eq->host_index = 0;
10381         eq->hba_index = 0;
10382
10383         mempool_free(mbox, phba->mbox_mem_pool);
10384         return status;
10385 }
10386
10387 /**
10388  * lpfc_cq_create - Create a Completion Queue on the HBA
10389  * @phba: HBA structure that indicates port to create a queue on.
10390  * @cq: The queue structure to use to create the completion queue.
10391  * @eq: The event queue to bind this completion queue to.
10392  *
10393  * This function creates a completion queue, as detailed in @wq, on a port,
10394  * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
10395  *
10396  * The @phba struct is used to send mailbox command to HBA. The @cq struct
10397  * is used to get the entry count and entry size that are necessary to
10398  * determine the number of pages to allocate and use for this queue. The @eq
10399  * is used to indicate which event queue to bind this completion queue to. This
10400  * function will send the CQ_CREATE mailbox command to the HBA to setup the
10401  * completion queue. This function is asynchronous and will wait for the mailbox
10402  * command to finish before continuing.
10403  *
10404  * On success this function will return a zero. If unable to allocate enough
10405  * memory this function will return -ENOMEM. If the queue create mailbox command
10406  * fails this function will return -ENXIO.
10407  **/
10408 uint32_t
10409 lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
10410                struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
10411 {
10412         struct lpfc_mbx_cq_create *cq_create;
10413         struct lpfc_dmabuf *dmabuf;
10414         LPFC_MBOXQ_t *mbox;
10415         int rc, length, status = 0;
10416         uint32_t shdr_status, shdr_add_status;
10417         union lpfc_sli4_cfg_shdr *shdr;
10418         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
10419
10420         if (!phba->sli4_hba.pc_sli4_params.supported)
10421                 hw_page_size = SLI4_PAGE_SIZE;
10422
10423
10424         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10425         if (!mbox)
10426                 return -ENOMEM;
10427         length = (sizeof(struct lpfc_mbx_cq_create) -
10428                   sizeof(struct lpfc_sli4_cfg_mhdr));
10429         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10430                          LPFC_MBOX_OPCODE_CQ_CREATE,
10431                          length, LPFC_SLI4_MBX_EMBED);
10432         cq_create = &mbox->u.mqe.un.cq_create;
10433         bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
10434                     cq->page_count);
10435         bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
10436         bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
10437         bf_set(lpfc_cq_eq_id, &cq_create->u.request.context, eq->queue_id);
10438         switch (cq->entry_count) {
10439         default:
10440                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10441                                 "0361 Unsupported CQ count. (%d)\n",
10442                                 cq->entry_count);
10443                 if (cq->entry_count < 256)
10444                         return -EINVAL;
10445                 /* otherwise default to smallest count (drop through) */
10446         case 256:
10447                 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
10448                        LPFC_CQ_CNT_256);
10449                 break;
10450         case 512:
10451                 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
10452                        LPFC_CQ_CNT_512);
10453                 break;
10454         case 1024:
10455                 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
10456                        LPFC_CQ_CNT_1024);
10457                 break;
10458         }
10459         list_for_each_entry(dmabuf, &cq->page_list, list) {
10460                 memset(dmabuf->virt, 0, hw_page_size);
10461                 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
10462                                         putPaddrLow(dmabuf->phys);
10463                 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
10464                                         putPaddrHigh(dmabuf->phys);
10465         }
10466         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10467
10468         /* The IOCTL status is embedded in the mailbox subheader. */
10469         shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
10470         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10471         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10472         if (shdr_status || shdr_add_status || rc) {
10473                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10474                                 "2501 CQ_CREATE mailbox failed with "
10475                                 "status x%x add_status x%x, mbx status x%x\n",
10476                                 shdr_status, shdr_add_status, rc);
10477                 status = -ENXIO;
10478                 goto out;
10479         }
10480         cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
10481         if (cq->queue_id == 0xFFFF) {
10482                 status = -ENXIO;
10483                 goto out;
10484         }
10485         /* link the cq onto the parent eq child list */
10486         list_add_tail(&cq->list, &eq->child_list);
10487         /* Set up completion queue's type and subtype */
10488         cq->type = type;
10489         cq->subtype = subtype;
10490         cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
10491         cq->host_index = 0;
10492         cq->hba_index = 0;
10493
10494 out:
10495         mempool_free(mbox, phba->mbox_mem_pool);
10496         return status;
10497 }
10498
10499 /**
10500  * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
10501  * @phba: HBA structure that indicates port to create a queue on.
10502  * @mq: The queue structure to use to create the mailbox queue.
10503  * @mbox: An allocated pointer to type LPFC_MBOXQ_t
10504  * @cq: The completion queue to associate with this cq.
10505  *
10506  * This function provides failback (fb) functionality when the
10507  * mq_create_ext fails on older FW generations.  It's purpose is identical
10508  * to mq_create_ext otherwise.
10509  *
10510  * This routine cannot fail as all attributes were previously accessed and
10511  * initialized in mq_create_ext.
10512  **/
10513 static void
10514 lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
10515                        LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
10516 {
10517         struct lpfc_mbx_mq_create *mq_create;
10518         struct lpfc_dmabuf *dmabuf;
10519         int length;
10520
10521         length = (sizeof(struct lpfc_mbx_mq_create) -
10522                   sizeof(struct lpfc_sli4_cfg_mhdr));
10523         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10524                          LPFC_MBOX_OPCODE_MQ_CREATE,
10525                          length, LPFC_SLI4_MBX_EMBED);
10526         mq_create = &mbox->u.mqe.un.mq_create;
10527         bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
10528                mq->page_count);
10529         bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
10530                cq->queue_id);
10531         bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
10532         switch (mq->entry_count) {
10533         case 16:
10534                 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
10535                        LPFC_MQ_CNT_16);
10536                 break;
10537         case 32:
10538                 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
10539                        LPFC_MQ_CNT_32);
10540                 break;
10541         case 64:
10542                 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
10543                        LPFC_MQ_CNT_64);
10544                 break;
10545         case 128:
10546                 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
10547                        LPFC_MQ_CNT_128);
10548                 break;
10549         }
10550         list_for_each_entry(dmabuf, &mq->page_list, list) {
10551                 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
10552                         putPaddrLow(dmabuf->phys);
10553                 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
10554                         putPaddrHigh(dmabuf->phys);
10555         }
10556 }
10557
10558 /**
10559  * lpfc_mq_create - Create a mailbox Queue on the HBA
10560  * @phba: HBA structure that indicates port to create a queue on.
10561  * @mq: The queue structure to use to create the mailbox queue.
10562  * @cq: The completion queue to associate with this cq.
10563  * @subtype: The queue's subtype.
10564  *
10565  * This function creates a mailbox queue, as detailed in @mq, on a port,
10566  * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
10567  *
10568  * The @phba struct is used to send mailbox command to HBA. The @cq struct
10569  * is used to get the entry count and entry size that are necessary to
10570  * determine the number of pages to allocate and use for this queue. This
10571  * function will send the MQ_CREATE mailbox command to the HBA to setup the
10572  * mailbox queue. This function is asynchronous and will wait for the mailbox
10573  * command to finish before continuing.
10574  *
10575  * On success this function will return a zero. If unable to allocate enough
10576  * memory this function will return -ENOMEM. If the queue create mailbox command
10577  * fails this function will return -ENXIO.
10578  **/
10579 int32_t
10580 lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
10581                struct lpfc_queue *cq, uint32_t subtype)
10582 {
10583         struct lpfc_mbx_mq_create *mq_create;
10584         struct lpfc_mbx_mq_create_ext *mq_create_ext;
10585         struct lpfc_dmabuf *dmabuf;
10586         LPFC_MBOXQ_t *mbox;
10587         int rc, length, status = 0;
10588         uint32_t shdr_status, shdr_add_status;
10589         union lpfc_sli4_cfg_shdr *shdr;
10590         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
10591
10592         if (!phba->sli4_hba.pc_sli4_params.supported)
10593                 hw_page_size = SLI4_PAGE_SIZE;
10594
10595         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10596         if (!mbox)
10597                 return -ENOMEM;
10598         length = (sizeof(struct lpfc_mbx_mq_create_ext) -
10599                   sizeof(struct lpfc_sli4_cfg_mhdr));
10600         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10601                          LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
10602                          length, LPFC_SLI4_MBX_EMBED);
10603
10604         mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
10605         bf_set(lpfc_mbx_mq_create_ext_num_pages,
10606                &mq_create_ext->u.request, mq->page_count);
10607         bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
10608                &mq_create_ext->u.request, 1);
10609         bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
10610                &mq_create_ext->u.request, 1);
10611         bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
10612                &mq_create_ext->u.request, 1);
10613         bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
10614                &mq_create_ext->u.request, 1);
10615         bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
10616                &mq_create_ext->u.request, 1);
10617         bf_set(lpfc_mq_context_cq_id,
10618                &mq_create_ext->u.request.context, cq->queue_id);
10619         bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
10620         switch (mq->entry_count) {
10621         default:
10622                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10623                                 "0362 Unsupported MQ count. (%d)\n",
10624                                 mq->entry_count);
10625                 if (mq->entry_count < 16)
10626                         return -EINVAL;
10627                 /* otherwise default to smallest count (drop through) */
10628         case 16:
10629                 bf_set(lpfc_mq_context_count, &mq_create_ext->u.request.context,
10630                        LPFC_MQ_CNT_16);
10631                 break;
10632         case 32:
10633                 bf_set(lpfc_mq_context_count, &mq_create_ext->u.request.context,
10634                        LPFC_MQ_CNT_32);
10635                 break;
10636         case 64:
10637                 bf_set(lpfc_mq_context_count, &mq_create_ext->u.request.context,
10638                        LPFC_MQ_CNT_64);
10639                 break;
10640         case 128:
10641                 bf_set(lpfc_mq_context_count, &mq_create_ext->u.request.context,
10642                        LPFC_MQ_CNT_128);
10643                 break;
10644         }
10645         list_for_each_entry(dmabuf, &mq->page_list, list) {
10646                 memset(dmabuf->virt, 0, hw_page_size);
10647                 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
10648                                         putPaddrLow(dmabuf->phys);
10649                 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
10650                                         putPaddrHigh(dmabuf->phys);
10651         }
10652         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10653         shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
10654         mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
10655                               &mq_create_ext->u.response);
10656         if (rc != MBX_SUCCESS) {
10657                 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10658                                 "2795 MQ_CREATE_EXT failed with "
10659                                 "status x%x. Failback to MQ_CREATE.\n",
10660                                 rc);
10661                 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
10662                 mq_create = &mbox->u.mqe.un.mq_create;
10663                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10664                 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
10665                 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
10666                                       &mq_create->u.response);
10667         }
10668
10669         /* The IOCTL status is embedded in the mailbox subheader. */
10670         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10671         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10672         if (shdr_status || shdr_add_status || rc) {
10673                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10674                                 "2502 MQ_CREATE mailbox failed with "
10675                                 "status x%x add_status x%x, mbx status x%x\n",
10676                                 shdr_status, shdr_add_status, rc);
10677                 status = -ENXIO;
10678                 goto out;
10679         }
10680         if (mq->queue_id == 0xFFFF) {
10681                 status = -ENXIO;
10682                 goto out;
10683         }
10684         mq->type = LPFC_MQ;
10685         mq->subtype = subtype;
10686         mq->host_index = 0;
10687         mq->hba_index = 0;
10688
10689         /* link the mq onto the parent cq child list */
10690         list_add_tail(&mq->list, &cq->child_list);
10691 out:
10692         mempool_free(mbox, phba->mbox_mem_pool);
10693         return status;
10694 }
10695
10696 /**
10697  * lpfc_wq_create - Create a Work Queue on the HBA
10698  * @phba: HBA structure that indicates port to create a queue on.
10699  * @wq: The queue structure to use to create the work queue.
10700  * @cq: The completion queue to bind this work queue to.
10701  * @subtype: The subtype of the work queue indicating its functionality.
10702  *
10703  * This function creates a work queue, as detailed in @wq, on a port, described
10704  * by @phba by sending a WQ_CREATE mailbox command to the HBA.
10705  *
10706  * The @phba struct is used to send mailbox command to HBA. The @wq struct
10707  * is used to get the entry count and entry size that are necessary to
10708  * determine the number of pages to allocate and use for this queue. The @cq
10709  * is used to indicate which completion queue to bind this work queue to. This
10710  * function will send the WQ_CREATE mailbox command to the HBA to setup the
10711  * work queue. This function is asynchronous and will wait for the mailbox
10712  * command to finish before continuing.
10713  *
10714  * On success this function will return a zero. If unable to allocate enough
10715  * memory this function will return -ENOMEM. If the queue create mailbox command
10716  * fails this function will return -ENXIO.
10717  **/
10718 uint32_t
10719 lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
10720                struct lpfc_queue *cq, uint32_t subtype)
10721 {
10722         struct lpfc_mbx_wq_create *wq_create;
10723         struct lpfc_dmabuf *dmabuf;
10724         LPFC_MBOXQ_t *mbox;
10725         int rc, length, status = 0;
10726         uint32_t shdr_status, shdr_add_status;
10727         union lpfc_sli4_cfg_shdr *shdr;
10728         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
10729
10730         if (!phba->sli4_hba.pc_sli4_params.supported)
10731                 hw_page_size = SLI4_PAGE_SIZE;
10732
10733         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10734         if (!mbox)
10735                 return -ENOMEM;
10736         length = (sizeof(struct lpfc_mbx_wq_create) -
10737                   sizeof(struct lpfc_sli4_cfg_mhdr));
10738         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10739                          LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
10740                          length, LPFC_SLI4_MBX_EMBED);
10741         wq_create = &mbox->u.mqe.un.wq_create;
10742         bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
10743                     wq->page_count);
10744         bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
10745                     cq->queue_id);
10746         list_for_each_entry(dmabuf, &wq->page_list, list) {
10747                 memset(dmabuf->virt, 0, hw_page_size);
10748                 wq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
10749                                         putPaddrLow(dmabuf->phys);
10750                 wq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
10751                                         putPaddrHigh(dmabuf->phys);
10752         }
10753         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10754         /* The IOCTL status is embedded in the mailbox subheader. */
10755         shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
10756         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10757         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10758         if (shdr_status || shdr_add_status || rc) {
10759                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10760                                 "2503 WQ_CREATE mailbox failed with "
10761                                 "status x%x add_status x%x, mbx status x%x\n",
10762                                 shdr_status, shdr_add_status, rc);
10763                 status = -ENXIO;
10764                 goto out;
10765         }
10766         wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
10767         if (wq->queue_id == 0xFFFF) {
10768                 status = -ENXIO;
10769                 goto out;
10770         }
10771         wq->type = LPFC_WQ;
10772         wq->subtype = subtype;
10773         wq->host_index = 0;
10774         wq->hba_index = 0;
10775
10776         /* link the wq onto the parent cq child list */
10777         list_add_tail(&wq->list, &cq->child_list);
10778 out:
10779         mempool_free(mbox, phba->mbox_mem_pool);
10780         return status;
10781 }
10782
10783 /**
10784  * lpfc_rq_create - Create a Receive Queue on the HBA
10785  * @phba: HBA structure that indicates port to create a queue on.
10786  * @hrq: The queue structure to use to create the header receive queue.
10787  * @drq: The queue structure to use to create the data receive queue.
10788  * @cq: The completion queue to bind this work queue to.
10789  *
10790  * This function creates a receive buffer queue pair , as detailed in @hrq and
10791  * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
10792  * to the HBA.
10793  *
10794  * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
10795  * struct is used to get the entry count that is necessary to determine the
10796  * number of pages to use for this queue. The @cq is used to indicate which
10797  * completion queue to bind received buffers that are posted to these queues to.
10798  * This function will send the RQ_CREATE mailbox command to the HBA to setup the
10799  * receive queue pair. This function is asynchronous and will wait for the
10800  * mailbox command to finish before continuing.
10801  *
10802  * On success this function will return a zero. If unable to allocate enough
10803  * memory this function will return -ENOMEM. If the queue create mailbox command
10804  * fails this function will return -ENXIO.
10805  **/
10806 uint32_t
10807 lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
10808                struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
10809 {
10810         struct lpfc_mbx_rq_create *rq_create;
10811         struct lpfc_dmabuf *dmabuf;
10812         LPFC_MBOXQ_t *mbox;
10813         int rc, length, status = 0;
10814         uint32_t shdr_status, shdr_add_status;
10815         union lpfc_sli4_cfg_shdr *shdr;
10816         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
10817
10818         if (!phba->sli4_hba.pc_sli4_params.supported)
10819                 hw_page_size = SLI4_PAGE_SIZE;
10820
10821         if (hrq->entry_count != drq->entry_count)
10822                 return -EINVAL;
10823         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10824         if (!mbox)
10825                 return -ENOMEM;
10826         length = (sizeof(struct lpfc_mbx_rq_create) -
10827                   sizeof(struct lpfc_sli4_cfg_mhdr));
10828         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10829                          LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
10830                          length, LPFC_SLI4_MBX_EMBED);
10831         rq_create = &mbox->u.mqe.un.rq_create;
10832         switch (hrq->entry_count) {
10833         default:
10834                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10835                                 "2535 Unsupported RQ count. (%d)\n",
10836                                 hrq->entry_count);
10837                 if (hrq->entry_count < 512)
10838                         return -EINVAL;
10839                 /* otherwise default to smallest count (drop through) */
10840         case 512:
10841                 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10842                        LPFC_RQ_RING_SIZE_512);
10843                 break;
10844         case 1024:
10845                 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10846                        LPFC_RQ_RING_SIZE_1024);
10847                 break;
10848         case 2048:
10849                 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10850                        LPFC_RQ_RING_SIZE_2048);
10851                 break;
10852         case 4096:
10853                 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10854                        LPFC_RQ_RING_SIZE_4096);
10855                 break;
10856         }
10857         bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
10858                cq->queue_id);
10859         bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
10860                hrq->page_count);
10861         bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
10862                LPFC_HDR_BUF_SIZE);
10863         list_for_each_entry(dmabuf, &hrq->page_list, list) {
10864                 memset(dmabuf->virt, 0, hw_page_size);
10865                 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
10866                                         putPaddrLow(dmabuf->phys);
10867                 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
10868                                         putPaddrHigh(dmabuf->phys);
10869         }
10870         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10871         /* The IOCTL status is embedded in the mailbox subheader. */
10872         shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
10873         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10874         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10875         if (shdr_status || shdr_add_status || rc) {
10876                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10877                                 "2504 RQ_CREATE mailbox failed with "
10878                                 "status x%x add_status x%x, mbx status x%x\n",
10879                                 shdr_status, shdr_add_status, rc);
10880                 status = -ENXIO;
10881                 goto out;
10882         }
10883         hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
10884         if (hrq->queue_id == 0xFFFF) {
10885                 status = -ENXIO;
10886                 goto out;
10887         }
10888         hrq->type = LPFC_HRQ;
10889         hrq->subtype = subtype;
10890         hrq->host_index = 0;
10891         hrq->hba_index = 0;
10892
10893         /* now create the data queue */
10894         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10895                          LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
10896                          length, LPFC_SLI4_MBX_EMBED);
10897         switch (drq->entry_count) {
10898         default:
10899                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10900                                 "2536 Unsupported RQ count. (%d)\n",
10901                                 drq->entry_count);
10902                 if (drq->entry_count < 512)
10903                         return -EINVAL;
10904                 /* otherwise default to smallest count (drop through) */
10905         case 512:
10906                 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10907                        LPFC_RQ_RING_SIZE_512);
10908                 break;
10909         case 1024:
10910                 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10911                        LPFC_RQ_RING_SIZE_1024);
10912                 break;
10913         case 2048:
10914                 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10915                        LPFC_RQ_RING_SIZE_2048);
10916                 break;
10917         case 4096:
10918                 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10919                        LPFC_RQ_RING_SIZE_4096);
10920                 break;
10921         }
10922         bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
10923                cq->queue_id);
10924         bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
10925                drq->page_count);
10926         bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
10927                LPFC_DATA_BUF_SIZE);
10928         list_for_each_entry(dmabuf, &drq->page_list, list) {
10929                 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
10930                                         putPaddrLow(dmabuf->phys);
10931                 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
10932                                         putPaddrHigh(dmabuf->phys);
10933         }
10934         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10935         /* The IOCTL status is embedded in the mailbox subheader. */
10936         shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
10937         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10938         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10939         if (shdr_status || shdr_add_status || rc) {
10940                 status = -ENXIO;
10941                 goto out;
10942         }
10943         drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
10944         if (drq->queue_id == 0xFFFF) {
10945                 status = -ENXIO;
10946                 goto out;
10947         }
10948         drq->type = LPFC_DRQ;
10949         drq->subtype = subtype;
10950         drq->host_index = 0;
10951         drq->hba_index = 0;
10952
10953         /* link the header and data RQs onto the parent cq child list */
10954         list_add_tail(&hrq->list, &cq->child_list);
10955         list_add_tail(&drq->list, &cq->child_list);
10956
10957 out:
10958         mempool_free(mbox, phba->mbox_mem_pool);
10959         return status;
10960 }
10961
10962 /**
10963  * lpfc_eq_destroy - Destroy an event Queue on the HBA
10964  * @eq: The queue structure associated with the queue to destroy.
10965  *
10966  * This function destroys a queue, as detailed in @eq by sending an mailbox
10967  * command, specific to the type of queue, to the HBA.
10968  *
10969  * The @eq struct is used to get the queue ID of the queue to destroy.
10970  *
10971  * On success this function will return a zero. If the queue destroy mailbox
10972  * command fails this function will return -ENXIO.
10973  **/
10974 uint32_t
10975 lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
10976 {
10977         LPFC_MBOXQ_t *mbox;
10978         int rc, length, status = 0;
10979         uint32_t shdr_status, shdr_add_status;
10980         union lpfc_sli4_cfg_shdr *shdr;
10981
10982         if (!eq)
10983                 return -ENODEV;
10984         mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
10985         if (!mbox)
10986                 return -ENOMEM;
10987         length = (sizeof(struct lpfc_mbx_eq_destroy) -
10988                   sizeof(struct lpfc_sli4_cfg_mhdr));
10989         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10990                          LPFC_MBOX_OPCODE_EQ_DESTROY,
10991                          length, LPFC_SLI4_MBX_EMBED);
10992         bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
10993                eq->queue_id);
10994         mbox->vport = eq->phba->pport;
10995         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10996
10997         rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
10998         /* The IOCTL status is embedded in the mailbox subheader. */
10999         shdr = (union lpfc_sli4_cfg_shdr *)
11000                 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
11001         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11002         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11003         if (shdr_status || shdr_add_status || rc) {
11004                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11005                                 "2505 EQ_DESTROY mailbox failed with "
11006                                 "status x%x add_status x%x, mbx status x%x\n",
11007                                 shdr_status, shdr_add_status, rc);
11008                 status = -ENXIO;
11009         }
11010
11011         /* Remove eq from any list */
11012         list_del_init(&eq->list);
11013         mempool_free(mbox, eq->phba->mbox_mem_pool);
11014         return status;
11015 }
11016
11017 /**
11018  * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
11019  * @cq: The queue structure associated with the queue to destroy.
11020  *
11021  * This function destroys a queue, as detailed in @cq by sending an mailbox
11022  * command, specific to the type of queue, to the HBA.
11023  *
11024  * The @cq struct is used to get the queue ID of the queue to destroy.
11025  *
11026  * On success this function will return a zero. If the queue destroy mailbox
11027  * command fails this function will return -ENXIO.
11028  **/
11029 uint32_t
11030 lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
11031 {
11032         LPFC_MBOXQ_t *mbox;
11033         int rc, length, status = 0;
11034         uint32_t shdr_status, shdr_add_status;
11035         union lpfc_sli4_cfg_shdr *shdr;
11036
11037         if (!cq)
11038                 return -ENODEV;
11039         mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
11040         if (!mbox)
11041                 return -ENOMEM;
11042         length = (sizeof(struct lpfc_mbx_cq_destroy) -
11043                   sizeof(struct lpfc_sli4_cfg_mhdr));
11044         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
11045                          LPFC_MBOX_OPCODE_CQ_DESTROY,
11046                          length, LPFC_SLI4_MBX_EMBED);
11047         bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
11048                cq->queue_id);
11049         mbox->vport = cq->phba->pport;
11050         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
11051         rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
11052         /* The IOCTL status is embedded in the mailbox subheader. */
11053         shdr = (union lpfc_sli4_cfg_shdr *)
11054                 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
11055         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11056         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11057         if (shdr_status || shdr_add_status || rc) {
11058                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11059                                 "2506 CQ_DESTROY mailbox failed with "
11060                                 "status x%x add_status x%x, mbx status x%x\n",
11061                                 shdr_status, shdr_add_status, rc);
11062                 status = -ENXIO;
11063         }
11064         /* Remove cq from any list */
11065         list_del_init(&cq->list);
11066         mempool_free(mbox, cq->phba->mbox_mem_pool);
11067         return status;
11068 }
11069
11070 /**
11071  * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
11072  * @qm: The queue structure associated with the queue to destroy.
11073  *
11074  * This function destroys a queue, as detailed in @mq by sending an mailbox
11075  * command, specific to the type of queue, to the HBA.
11076  *
11077  * The @mq struct is used to get the queue ID of the queue to destroy.
11078  *
11079  * On success this function will return a zero. If the queue destroy mailbox
11080  * command fails this function will return -ENXIO.
11081  **/
11082 uint32_t
11083 lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
11084 {
11085         LPFC_MBOXQ_t *mbox;
11086         int rc, length, status = 0;
11087         uint32_t shdr_status, shdr_add_status;
11088         union lpfc_sli4_cfg_shdr *shdr;
11089
11090         if (!mq)
11091                 return -ENODEV;
11092         mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
11093         if (!mbox)
11094                 return -ENOMEM;
11095         length = (sizeof(struct lpfc_mbx_mq_destroy) -
11096                   sizeof(struct lpfc_sli4_cfg_mhdr));
11097         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
11098                          LPFC_MBOX_OPCODE_MQ_DESTROY,
11099                          length, LPFC_SLI4_MBX_EMBED);
11100         bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
11101                mq->queue_id);
11102         mbox->vport = mq->phba->pport;
11103         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
11104         rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
11105         /* The IOCTL status is embedded in the mailbox subheader. */
11106         shdr = (union lpfc_sli4_cfg_shdr *)
11107                 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
11108         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11109         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11110         if (shdr_status || shdr_add_status || rc) {
11111                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11112                                 "2507 MQ_DESTROY mailbox failed with "
11113                                 "status x%x add_status x%x, mbx status x%x\n",
11114                                 shdr_status, shdr_add_status, rc);
11115                 status = -ENXIO;
11116         }
11117         /* Remove mq from any list */
11118         list_del_init(&mq->list);
11119         mempool_free(mbox, mq->phba->mbox_mem_pool);
11120         return status;
11121 }
11122
11123 /**
11124  * lpfc_wq_destroy - Destroy a Work Queue on the HBA
11125  * @wq: The queue structure associated with the queue to destroy.
11126  *
11127  * This function destroys a queue, as detailed in @wq by sending an mailbox
11128  * command, specific to the type of queue, to the HBA.
11129  *
11130  * The @wq struct is used to get the queue ID of the queue to destroy.
11131  *
11132  * On success this function will return a zero. If the queue destroy mailbox
11133  * command fails this function will return -ENXIO.
11134  **/
11135 uint32_t
11136 lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
11137 {
11138         LPFC_MBOXQ_t *mbox;
11139         int rc, length, status = 0;
11140         uint32_t shdr_status, shdr_add_status;
11141         union lpfc_sli4_cfg_shdr *shdr;
11142
11143         if (!wq)
11144                 return -ENODEV;
11145         mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
11146         if (!mbox)
11147                 return -ENOMEM;
11148         length = (sizeof(struct lpfc_mbx_wq_destroy) -
11149                   sizeof(struct lpfc_sli4_cfg_mhdr));
11150         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
11151                          LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
11152                          length, LPFC_SLI4_MBX_EMBED);
11153         bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
11154                wq->queue_id);
11155         mbox->vport = wq->phba->pport;
11156         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
11157         rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
11158         shdr = (union lpfc_sli4_cfg_shdr *)
11159                 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
11160         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11161         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11162         if (shdr_status || shdr_add_status || rc) {
11163                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11164                                 "2508 WQ_DESTROY mailbox failed with "
11165                                 "status x%x add_status x%x, mbx status x%x\n",
11166                                 shdr_status, shdr_add_status, rc);
11167                 status = -ENXIO;
11168         }
11169         /* Remove wq from any list */
11170         list_del_init(&wq->list);
11171         mempool_free(mbox, wq->phba->mbox_mem_pool);
11172         return status;
11173 }
11174
11175 /**
11176  * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
11177  * @rq: The queue structure associated with the queue to destroy.
11178  *
11179  * This function destroys a queue, as detailed in @rq by sending an mailbox
11180  * command, specific to the type of queue, to the HBA.
11181  *
11182  * The @rq struct is used to get the queue ID of the queue to destroy.
11183  *
11184  * On success this function will return a zero. If the queue destroy mailbox
11185  * command fails this function will return -ENXIO.
11186  **/
11187 uint32_t
11188 lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
11189                 struct lpfc_queue *drq)
11190 {
11191         LPFC_MBOXQ_t *mbox;
11192         int rc, length, status = 0;
11193         uint32_t shdr_status, shdr_add_status;
11194         union lpfc_sli4_cfg_shdr *shdr;
11195
11196         if (!hrq || !drq)
11197                 return -ENODEV;
11198         mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
11199         if (!mbox)
11200                 return -ENOMEM;
11201         length = (sizeof(struct lpfc_mbx_rq_destroy) -
11202                   sizeof(struct mbox_header));
11203         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
11204                          LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
11205                          length, LPFC_SLI4_MBX_EMBED);
11206         bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
11207                hrq->queue_id);
11208         mbox->vport = hrq->phba->pport;
11209         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
11210         rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
11211         /* The IOCTL status is embedded in the mailbox subheader. */
11212         shdr = (union lpfc_sli4_cfg_shdr *)
11213                 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
11214         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11215         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11216         if (shdr_status || shdr_add_status || rc) {
11217                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11218                                 "2509 RQ_DESTROY mailbox failed with "
11219                                 "status x%x add_status x%x, mbx status x%x\n",
11220                                 shdr_status, shdr_add_status, rc);
11221                 if (rc != MBX_TIMEOUT)
11222                         mempool_free(mbox, hrq->phba->mbox_mem_pool);
11223                 return -ENXIO;
11224         }
11225         bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
11226                drq->queue_id);
11227         rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
11228         shdr = (union lpfc_sli4_cfg_shdr *)
11229                 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
11230         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11231         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11232         if (shdr_status || shdr_add_status || rc) {
11233                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11234                                 "2510 RQ_DESTROY mailbox failed with "
11235                                 "status x%x add_status x%x, mbx status x%x\n",
11236                                 shdr_status, shdr_add_status, rc);
11237                 status = -ENXIO;
11238         }
11239         list_del_init(&hrq->list);
11240         list_del_init(&drq->list);
11241         mempool_free(mbox, hrq->phba->mbox_mem_pool);
11242         return status;
11243 }
11244
11245 /**
11246  * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
11247  * @phba: The virtual port for which this call being executed.
11248  * @pdma_phys_addr0: Physical address of the 1st SGL page.
11249  * @pdma_phys_addr1: Physical address of the 2nd SGL page.
11250  * @xritag: the xritag that ties this io to the SGL pages.
11251  *
11252  * This routine will post the sgl pages for the IO that has the xritag
11253  * that is in the iocbq structure. The xritag is assigned during iocbq
11254  * creation and persists for as long as the driver is loaded.
11255  * if the caller has fewer than 256 scatter gather segments to map then
11256  * pdma_phys_addr1 should be 0.
11257  * If the caller needs to map more than 256 scatter gather segment then
11258  * pdma_phys_addr1 should be a valid physical address.
11259  * physical address for SGLs must be 64 byte aligned.
11260  * If you are going to map 2 SGL's then the first one must have 256 entries
11261  * the second sgl can have between 1 and 256 entries.
11262  *
11263  * Return codes:
11264  *      0 - Success
11265  *      -ENXIO, -ENOMEM - Failure
11266  **/
11267 int
11268 lpfc_sli4_post_sgl(struct lpfc_hba *phba,
11269                 dma_addr_t pdma_phys_addr0,
11270                 dma_addr_t pdma_phys_addr1,
11271                 uint16_t xritag)
11272 {
11273         struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
11274         LPFC_MBOXQ_t *mbox;
11275         int rc;
11276         uint32_t shdr_status, shdr_add_status;
11277         union lpfc_sli4_cfg_shdr *shdr;
11278
11279         if (xritag == NO_XRI) {
11280                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11281                                 "0364 Invalid param:\n");
11282                 return -EINVAL;
11283         }
11284
11285         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11286         if (!mbox)
11287                 return -ENOMEM;
11288
11289         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
11290                         LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
11291                         sizeof(struct lpfc_mbx_post_sgl_pages) -
11292                         sizeof(struct mbox_header), LPFC_SLI4_MBX_EMBED);
11293
11294         post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
11295                                 &mbox->u.mqe.un.post_sgl_pages;
11296         bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
11297         bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
11298
11299         post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
11300                                 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
11301         post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
11302                                 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
11303
11304         post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
11305                                 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
11306         post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
11307                                 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
11308         if (!phba->sli4_hba.intr_enable)
11309                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
11310         else
11311                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
11312         /* The IOCTL status is embedded in the mailbox subheader. */
11313         shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
11314         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11315         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11316         if (rc != MBX_TIMEOUT)
11317                 mempool_free(mbox, phba->mbox_mem_pool);
11318         if (shdr_status || shdr_add_status || rc) {
11319                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11320                                 "2511 POST_SGL mailbox failed with "
11321                                 "status x%x add_status x%x, mbx status x%x\n",
11322                                 shdr_status, shdr_add_status, rc);
11323                 rc = -ENXIO;
11324         }
11325         return 0;
11326 }
11327
11328 /**
11329  * lpfc_sli4_next_xritag - Get an xritag for the io
11330  * @phba: Pointer to HBA context object.
11331  *
11332  * This function gets an xritag for the iocb. If there is no unused xritag
11333  * it will return 0xffff.
11334  * The function returns the allocated xritag if successful, else returns zero.
11335  * Zero is not a valid xritag.
11336  * The caller is not required to hold any lock.
11337  **/
11338 uint16_t
11339 lpfc_sli4_next_xritag(struct lpfc_hba *phba)
11340 {
11341         uint16_t xritag;
11342
11343         spin_lock_irq(&phba->hbalock);
11344         xritag = phba->sli4_hba.next_xri;
11345         if ((xritag != (uint16_t) -1) && xritag <
11346                 (phba->sli4_hba.max_cfg_param.max_xri
11347                         + phba->sli4_hba.max_cfg_param.xri_base)) {
11348                 phba->sli4_hba.next_xri++;
11349                 phba->sli4_hba.max_cfg_param.xri_used++;
11350                 spin_unlock_irq(&phba->hbalock);
11351                 return xritag;
11352         }
11353         spin_unlock_irq(&phba->hbalock);
11354         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11355                         "2004 Failed to allocate XRI.last XRITAG is %d"
11356                         " Max XRI is %d, Used XRI is %d\n",
11357                         phba->sli4_hba.next_xri,
11358                         phba->sli4_hba.max_cfg_param.max_xri,
11359                         phba->sli4_hba.max_cfg_param.xri_used);
11360         return -1;
11361 }
11362
11363 /**
11364  * lpfc_sli4_post_sgl_list - post a block of sgl list to the firmware.
11365  * @phba: pointer to lpfc hba data structure.
11366  *
11367  * This routine is invoked to post a block of driver's sgl pages to the
11368  * HBA using non-embedded mailbox command. No Lock is held. This routine
11369  * is only called when the driver is loading and after all IO has been
11370  * stopped.
11371  **/
11372 int
11373 lpfc_sli4_post_sgl_list(struct lpfc_hba *phba)
11374 {
11375         struct lpfc_sglq *sglq_entry;
11376         struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
11377         struct sgl_page_pairs *sgl_pg_pairs;
11378         void *viraddr;
11379         LPFC_MBOXQ_t *mbox;
11380         uint32_t reqlen, alloclen, pg_pairs;
11381         uint32_t mbox_tmo;
11382         uint16_t xritag_start = 0;
11383         int els_xri_cnt, rc = 0;
11384         uint32_t shdr_status, shdr_add_status;
11385         union lpfc_sli4_cfg_shdr *shdr;
11386
11387         /* The number of sgls to be posted */
11388         els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
11389
11390         reqlen = els_xri_cnt * sizeof(struct sgl_page_pairs) +
11391                  sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
11392         if (reqlen > SLI4_PAGE_SIZE) {
11393                 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
11394                                 "2559 Block sgl registration required DMA "
11395                                 "size (%d) great than a page\n", reqlen);
11396                 return -ENOMEM;
11397         }
11398         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11399         if (!mbox) {
11400                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11401                                 "2560 Failed to allocate mbox cmd memory\n");
11402                 return -ENOMEM;
11403         }
11404
11405         /* Allocate DMA memory and set up the non-embedded mailbox command */
11406         alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
11407                          LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
11408                          LPFC_SLI4_MBX_NEMBED);
11409
11410         if (alloclen < reqlen) {
11411                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11412                                 "0285 Allocated DMA memory size (%d) is "
11413                                 "less than the requested DMA memory "
11414                                 "size (%d)\n", alloclen, reqlen);
11415                 lpfc_sli4_mbox_cmd_free(phba, mbox);
11416                 return -ENOMEM;
11417         }
11418         /* Get the first SGE entry from the non-embedded DMA memory */
11419         viraddr = mbox->sge_array->addr[0];
11420
11421         /* Set up the SGL pages in the non-embedded DMA pages */
11422         sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
11423         sgl_pg_pairs = &sgl->sgl_pg_pairs;
11424
11425         for (pg_pairs = 0; pg_pairs < els_xri_cnt; pg_pairs++) {
11426                 sglq_entry = phba->sli4_hba.lpfc_els_sgl_array[pg_pairs];
11427                 /* Set up the sge entry */
11428                 sgl_pg_pairs->sgl_pg0_addr_lo =
11429                                 cpu_to_le32(putPaddrLow(sglq_entry->phys));
11430                 sgl_pg_pairs->sgl_pg0_addr_hi =
11431                                 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
11432                 sgl_pg_pairs->sgl_pg1_addr_lo =
11433                                 cpu_to_le32(putPaddrLow(0));
11434                 sgl_pg_pairs->sgl_pg1_addr_hi =
11435                                 cpu_to_le32(putPaddrHigh(0));
11436                 /* Keep the first xritag on the list */
11437                 if (pg_pairs == 0)
11438                         xritag_start = sglq_entry->sli4_xritag;
11439                 sgl_pg_pairs++;
11440         }
11441         bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
11442         bf_set(lpfc_post_sgl_pages_xricnt, sgl, els_xri_cnt);
11443         /* Perform endian conversion if necessary */
11444         sgl->word0 = cpu_to_le32(sgl->word0);
11445
11446         if (!phba->sli4_hba.intr_enable)
11447                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
11448         else {
11449                 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
11450                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
11451         }
11452         shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
11453         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11454         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11455         if (rc != MBX_TIMEOUT)
11456                 lpfc_sli4_mbox_cmd_free(phba, mbox);
11457         if (shdr_status || shdr_add_status || rc) {
11458                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11459                                 "2513 POST_SGL_BLOCK mailbox command failed "
11460                                 "status x%x add_status x%x mbx status x%x\n",
11461                                 shdr_status, shdr_add_status, rc);
11462                 rc = -ENXIO;
11463         }
11464         return rc;
11465 }
11466
11467 /**
11468  * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
11469  * @phba: pointer to lpfc hba data structure.
11470  * @sblist: pointer to scsi buffer list.
11471  * @count: number of scsi buffers on the list.
11472  *
11473  * This routine is invoked to post a block of @count scsi sgl pages from a
11474  * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
11475  * No Lock is held.
11476  *
11477  **/
11478 int
11479 lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba, struct list_head *sblist,
11480                               int cnt)
11481 {
11482         struct lpfc_scsi_buf *psb;
11483         struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
11484         struct sgl_page_pairs *sgl_pg_pairs;
11485         void *viraddr;
11486         LPFC_MBOXQ_t *mbox;
11487         uint32_t reqlen, alloclen, pg_pairs;
11488         uint32_t mbox_tmo;
11489         uint16_t xritag_start = 0;
11490         int rc = 0;
11491         uint32_t shdr_status, shdr_add_status;
11492         dma_addr_t pdma_phys_bpl1;
11493         union lpfc_sli4_cfg_shdr *shdr;
11494
11495         /* Calculate the requested length of the dma memory */
11496         reqlen = cnt * sizeof(struct sgl_page_pairs) +
11497                  sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
11498         if (reqlen > SLI4_PAGE_SIZE) {
11499                 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
11500                                 "0217 Block sgl registration required DMA "
11501                                 "size (%d) great than a page\n", reqlen);
11502                 return -ENOMEM;
11503         }
11504         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11505         if (!mbox) {
11506                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11507                                 "0283 Failed to allocate mbox cmd memory\n");
11508                 return -ENOMEM;
11509         }
11510
11511         /* Allocate DMA memory and set up the non-embedded mailbox command */
11512         alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
11513                                 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
11514                                 LPFC_SLI4_MBX_NEMBED);
11515
11516         if (alloclen < reqlen) {
11517                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11518                                 "2561 Allocated DMA memory size (%d) is "
11519                                 "less than the requested DMA memory "
11520                                 "size (%d)\n", alloclen, reqlen);
11521                 lpfc_sli4_mbox_cmd_free(phba, mbox);
11522                 return -ENOMEM;
11523         }
11524         /* Get the first SGE entry from the non-embedded DMA memory */
11525         viraddr = mbox->sge_array->addr[0];
11526
11527         /* Set up the SGL pages in the non-embedded DMA pages */
11528         sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
11529         sgl_pg_pairs = &sgl->sgl_pg_pairs;
11530
11531         pg_pairs = 0;
11532         list_for_each_entry(psb, sblist, list) {
11533                 /* Set up the sge entry */
11534                 sgl_pg_pairs->sgl_pg0_addr_lo =
11535                         cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
11536                 sgl_pg_pairs->sgl_pg0_addr_hi =
11537                         cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
11538                 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
11539                         pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
11540                 else
11541                         pdma_phys_bpl1 = 0;
11542                 sgl_pg_pairs->sgl_pg1_addr_lo =
11543                         cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
11544                 sgl_pg_pairs->sgl_pg1_addr_hi =
11545                         cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
11546                 /* Keep the first xritag on the list */
11547                 if (pg_pairs == 0)
11548                         xritag_start = psb->cur_iocbq.sli4_xritag;
11549                 sgl_pg_pairs++;
11550                 pg_pairs++;
11551         }
11552         bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
11553         bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
11554         /* Perform endian conversion if necessary */
11555         sgl->word0 = cpu_to_le32(sgl->word0);
11556
11557         if (!phba->sli4_hba.intr_enable)
11558                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
11559         else {
11560                 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
11561                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
11562         }
11563         shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
11564         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11565         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11566         if (rc != MBX_TIMEOUT)
11567                 lpfc_sli4_mbox_cmd_free(phba, mbox);
11568         if (shdr_status || shdr_add_status || rc) {
11569                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11570                                 "2564 POST_SGL_BLOCK mailbox command failed "
11571                                 "status x%x add_status x%x mbx status x%x\n",
11572                                 shdr_status, shdr_add_status, rc);
11573                 rc = -ENXIO;
11574         }
11575         return rc;
11576 }
11577
11578 /**
11579  * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
11580  * @phba: pointer to lpfc_hba struct that the frame was received on
11581  * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
11582  *
11583  * This function checks the fields in the @fc_hdr to see if the FC frame is a
11584  * valid type of frame that the LPFC driver will handle. This function will
11585  * return a zero if the frame is a valid frame or a non zero value when the
11586  * frame does not pass the check.
11587  **/
11588 static int
11589 lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
11590 {
11591         char *rctl_names[] = FC_RCTL_NAMES_INIT;
11592         char *type_names[] = FC_TYPE_NAMES_INIT;
11593         struct fc_vft_header *fc_vft_hdr;
11594
11595         switch (fc_hdr->fh_r_ctl) {
11596         case FC_RCTL_DD_UNCAT:          /* uncategorized information */
11597         case FC_RCTL_DD_SOL_DATA:       /* solicited data */
11598         case FC_RCTL_DD_UNSOL_CTL:      /* unsolicited control */
11599         case FC_RCTL_DD_SOL_CTL:        /* solicited control or reply */
11600         case FC_RCTL_DD_UNSOL_DATA:     /* unsolicited data */
11601         case FC_RCTL_DD_DATA_DESC:      /* data descriptor */
11602         case FC_RCTL_DD_UNSOL_CMD:      /* unsolicited command */
11603         case FC_RCTL_DD_CMD_STATUS:     /* command status */
11604         case FC_RCTL_ELS_REQ:   /* extended link services request */
11605         case FC_RCTL_ELS_REP:   /* extended link services reply */
11606         case FC_RCTL_ELS4_REQ:  /* FC-4 ELS request */
11607         case FC_RCTL_ELS4_REP:  /* FC-4 ELS reply */
11608         case FC_RCTL_BA_NOP:    /* basic link service NOP */
11609         case FC_RCTL_BA_ABTS:   /* basic link service abort */
11610         case FC_RCTL_BA_RMC:    /* remove connection */
11611         case FC_RCTL_BA_ACC:    /* basic accept */
11612         case FC_RCTL_BA_RJT:    /* basic reject */
11613         case FC_RCTL_BA_PRMT:
11614         case FC_RCTL_ACK_1:     /* acknowledge_1 */
11615         case FC_RCTL_ACK_0:     /* acknowledge_0 */
11616         case FC_RCTL_P_RJT:     /* port reject */
11617         case FC_RCTL_F_RJT:     /* fabric reject */
11618         case FC_RCTL_P_BSY:     /* port busy */
11619         case FC_RCTL_F_BSY:     /* fabric busy to data frame */
11620         case FC_RCTL_F_BSYL:    /* fabric busy to link control frame */
11621         case FC_RCTL_LCR:       /* link credit reset */
11622         case FC_RCTL_END:       /* end */
11623                 break;
11624         case FC_RCTL_VFTH:      /* Virtual Fabric tagging Header */
11625                 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
11626                 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
11627                 return lpfc_fc_frame_check(phba, fc_hdr);
11628         default:
11629                 goto drop;
11630         }
11631         switch (fc_hdr->fh_type) {
11632         case FC_TYPE_BLS:
11633         case FC_TYPE_ELS:
11634         case FC_TYPE_FCP:
11635         case FC_TYPE_CT:
11636                 break;
11637         case FC_TYPE_IP:
11638         case FC_TYPE_ILS:
11639         default:
11640                 goto drop;
11641         }
11642         lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
11643                         "2538 Received frame rctl:%s type:%s\n",
11644                         rctl_names[fc_hdr->fh_r_ctl],
11645                         type_names[fc_hdr->fh_type]);
11646         return 0;
11647 drop:
11648         lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
11649                         "2539 Dropped frame rctl:%s type:%s\n",
11650                         rctl_names[fc_hdr->fh_r_ctl],
11651                         type_names[fc_hdr->fh_type]);
11652         return 1;
11653 }
11654
11655 /**
11656  * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
11657  * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
11658  *
11659  * This function processes the FC header to retrieve the VFI from the VF
11660  * header, if one exists. This function will return the VFI if one exists
11661  * or 0 if no VSAN Header exists.
11662  **/
11663 static uint32_t
11664 lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
11665 {
11666         struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
11667
11668         if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
11669                 return 0;
11670         return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
11671 }
11672
11673 /**
11674  * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
11675  * @phba: Pointer to the HBA structure to search for the vport on
11676  * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
11677  * @fcfi: The FC Fabric ID that the frame came from
11678  *
11679  * This function searches the @phba for a vport that matches the content of the
11680  * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
11681  * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
11682  * returns the matching vport pointer or NULL if unable to match frame to a
11683  * vport.
11684  **/
11685 static struct lpfc_vport *
11686 lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
11687                        uint16_t fcfi)
11688 {
11689         struct lpfc_vport **vports;
11690         struct lpfc_vport *vport = NULL;
11691         int i;
11692         uint32_t did = (fc_hdr->fh_d_id[0] << 16 |
11693                         fc_hdr->fh_d_id[1] << 8 |
11694                         fc_hdr->fh_d_id[2]);
11695
11696         vports = lpfc_create_vport_work_array(phba);
11697         if (vports != NULL)
11698                 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
11699                         if (phba->fcf.fcfi == fcfi &&
11700                             vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
11701                             vports[i]->fc_myDID == did) {
11702                                 vport = vports[i];
11703                                 break;
11704                         }
11705                 }
11706         lpfc_destroy_vport_work_array(phba, vports);
11707         return vport;
11708 }
11709
11710 /**
11711  * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
11712  * @vport: The vport to work on.
11713  *
11714  * This function updates the receive sequence time stamp for this vport. The
11715  * receive sequence time stamp indicates the time that the last frame of the
11716  * the sequence that has been idle for the longest amount of time was received.
11717  * the driver uses this time stamp to indicate if any received sequences have
11718  * timed out.
11719  **/
11720 void
11721 lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
11722 {
11723         struct lpfc_dmabuf *h_buf;
11724         struct hbq_dmabuf *dmabuf = NULL;
11725
11726         /* get the oldest sequence on the rcv list */
11727         h_buf = list_get_first(&vport->rcv_buffer_list,
11728                                struct lpfc_dmabuf, list);
11729         if (!h_buf)
11730                 return;
11731         dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11732         vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
11733 }
11734
11735 /**
11736  * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
11737  * @vport: The vport that the received sequences were sent to.
11738  *
11739  * This function cleans up all outstanding received sequences. This is called
11740  * by the driver when a link event or user action invalidates all the received
11741  * sequences.
11742  **/
11743 void
11744 lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
11745 {
11746         struct lpfc_dmabuf *h_buf, *hnext;
11747         struct lpfc_dmabuf *d_buf, *dnext;
11748         struct hbq_dmabuf *dmabuf = NULL;
11749
11750         /* start with the oldest sequence on the rcv list */
11751         list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
11752                 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11753                 list_del_init(&dmabuf->hbuf.list);
11754                 list_for_each_entry_safe(d_buf, dnext,
11755                                          &dmabuf->dbuf.list, list) {
11756                         list_del_init(&d_buf->list);
11757                         lpfc_in_buf_free(vport->phba, d_buf);
11758                 }
11759                 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
11760         }
11761 }
11762
11763 /**
11764  * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
11765  * @vport: The vport that the received sequences were sent to.
11766  *
11767  * This function determines whether any received sequences have timed out by
11768  * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
11769  * indicates that there is at least one timed out sequence this routine will
11770  * go through the received sequences one at a time from most inactive to most
11771  * active to determine which ones need to be cleaned up. Once it has determined
11772  * that a sequence needs to be cleaned up it will simply free up the resources
11773  * without sending an abort.
11774  **/
11775 void
11776 lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
11777 {
11778         struct lpfc_dmabuf *h_buf, *hnext;
11779         struct lpfc_dmabuf *d_buf, *dnext;
11780         struct hbq_dmabuf *dmabuf = NULL;
11781         unsigned long timeout;
11782         int abort_count = 0;
11783
11784         timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
11785                    vport->rcv_buffer_time_stamp);
11786         if (list_empty(&vport->rcv_buffer_list) ||
11787             time_before(jiffies, timeout))
11788                 return;
11789         /* start with the oldest sequence on the rcv list */
11790         list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
11791                 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11792                 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
11793                            dmabuf->time_stamp);
11794                 if (time_before(jiffies, timeout))
11795                         break;
11796                 abort_count++;
11797                 list_del_init(&dmabuf->hbuf.list);
11798                 list_for_each_entry_safe(d_buf, dnext,
11799                                          &dmabuf->dbuf.list, list) {
11800                         list_del_init(&d_buf->list);
11801                         lpfc_in_buf_free(vport->phba, d_buf);
11802                 }
11803                 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
11804         }
11805         if (abort_count)
11806                 lpfc_update_rcv_time_stamp(vport);
11807 }
11808
11809 /**
11810  * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
11811  * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
11812  *
11813  * This function searches through the existing incomplete sequences that have
11814  * been sent to this @vport. If the frame matches one of the incomplete
11815  * sequences then the dbuf in the @dmabuf is added to the list of frames that
11816  * make up that sequence. If no sequence is found that matches this frame then
11817  * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
11818  * This function returns a pointer to the first dmabuf in the sequence list that
11819  * the frame was linked to.
11820  **/
11821 static struct hbq_dmabuf *
11822 lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
11823 {
11824         struct fc_frame_header *new_hdr;
11825         struct fc_frame_header *temp_hdr;
11826         struct lpfc_dmabuf *d_buf;
11827         struct lpfc_dmabuf *h_buf;
11828         struct hbq_dmabuf *seq_dmabuf = NULL;
11829         struct hbq_dmabuf *temp_dmabuf = NULL;
11830
11831         INIT_LIST_HEAD(&dmabuf->dbuf.list);
11832         dmabuf->time_stamp = jiffies;
11833         new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11834         /* Use the hdr_buf to find the sequence that this frame belongs to */
11835         list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
11836                 temp_hdr = (struct fc_frame_header *)h_buf->virt;
11837                 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
11838                     (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
11839                     (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
11840                         continue;
11841                 /* found a pending sequence that matches this frame */
11842                 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11843                 break;
11844         }
11845         if (!seq_dmabuf) {
11846                 /*
11847                  * This indicates first frame received for this sequence.
11848                  * Queue the buffer on the vport's rcv_buffer_list.
11849                  */
11850                 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
11851                 lpfc_update_rcv_time_stamp(vport);
11852                 return dmabuf;
11853         }
11854         temp_hdr = seq_dmabuf->hbuf.virt;
11855         if (be16_to_cpu(new_hdr->fh_seq_cnt) <
11856                 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
11857                 list_del_init(&seq_dmabuf->hbuf.list);
11858                 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
11859                 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
11860                 lpfc_update_rcv_time_stamp(vport);
11861                 return dmabuf;
11862         }
11863         /* move this sequence to the tail to indicate a young sequence */
11864         list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
11865         seq_dmabuf->time_stamp = jiffies;
11866         lpfc_update_rcv_time_stamp(vport);
11867         if (list_empty(&seq_dmabuf->dbuf.list)) {
11868                 temp_hdr = dmabuf->hbuf.virt;
11869                 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
11870                 return seq_dmabuf;
11871         }
11872         /* find the correct place in the sequence to insert this frame */
11873         list_for_each_entry_reverse(d_buf, &seq_dmabuf->dbuf.list, list) {
11874                 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
11875                 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
11876                 /*
11877                  * If the frame's sequence count is greater than the frame on
11878                  * the list then insert the frame right after this frame
11879                  */
11880                 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
11881                         be16_to_cpu(temp_hdr->fh_seq_cnt)) {
11882                         list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
11883                         return seq_dmabuf;
11884                 }
11885         }
11886         return NULL;
11887 }
11888
11889 /**
11890  * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
11891  * @vport: pointer to a vitural port
11892  * @dmabuf: pointer to a dmabuf that describes the FC sequence
11893  *
11894  * This function tries to abort from the partially assembed sequence, described
11895  * by the information from basic abbort @dmabuf. It checks to see whether such
11896  * partially assembled sequence held by the driver. If so, it shall free up all
11897  * the frames from the partially assembled sequence.
11898  *
11899  * Return
11900  * true  -- if there is matching partially assembled sequence present and all
11901  *          the frames freed with the sequence;
11902  * false -- if there is no matching partially assembled sequence present so
11903  *          nothing got aborted in the lower layer driver
11904  **/
11905 static bool
11906 lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
11907                             struct hbq_dmabuf *dmabuf)
11908 {
11909         struct fc_frame_header *new_hdr;
11910         struct fc_frame_header *temp_hdr;
11911         struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
11912         struct hbq_dmabuf *seq_dmabuf = NULL;
11913
11914         /* Use the hdr_buf to find the sequence that matches this frame */
11915         INIT_LIST_HEAD(&dmabuf->dbuf.list);
11916         INIT_LIST_HEAD(&dmabuf->hbuf.list);
11917         new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11918         list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
11919                 temp_hdr = (struct fc_frame_header *)h_buf->virt;
11920                 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
11921                     (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
11922                     (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
11923                         continue;
11924                 /* found a pending sequence that matches this frame */
11925                 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11926                 break;
11927         }
11928
11929         /* Free up all the frames from the partially assembled sequence */
11930         if (seq_dmabuf) {
11931                 list_for_each_entry_safe(d_buf, n_buf,
11932                                          &seq_dmabuf->dbuf.list, list) {
11933                         list_del_init(&d_buf->list);
11934                         lpfc_in_buf_free(vport->phba, d_buf);
11935                 }
11936                 return true;
11937         }
11938         return false;
11939 }
11940
11941 /**
11942  * lpfc_sli4_seq_abort_acc_cmpl - Accept seq abort iocb complete handler
11943  * @phba: Pointer to HBA context object.
11944  * @cmd_iocbq: pointer to the command iocbq structure.
11945  * @rsp_iocbq: pointer to the response iocbq structure.
11946  *
11947  * This function handles the sequence abort accept iocb command complete
11948  * event. It properly releases the memory allocated to the sequence abort
11949  * accept iocb.
11950  **/
11951 static void
11952 lpfc_sli4_seq_abort_acc_cmpl(struct lpfc_hba *phba,
11953                              struct lpfc_iocbq *cmd_iocbq,
11954                              struct lpfc_iocbq *rsp_iocbq)
11955 {
11956         if (cmd_iocbq)
11957                 lpfc_sli_release_iocbq(phba, cmd_iocbq);
11958 }
11959
11960 /**
11961  * lpfc_sli4_seq_abort_acc - Accept sequence abort
11962  * @phba: Pointer to HBA context object.
11963  * @fc_hdr: pointer to a FC frame header.
11964  *
11965  * This function sends a basic accept to a previous unsol sequence abort
11966  * event after aborting the sequence handling.
11967  **/
11968 static void
11969 lpfc_sli4_seq_abort_acc(struct lpfc_hba *phba,
11970                         struct fc_frame_header *fc_hdr)
11971 {
11972         struct lpfc_iocbq *ctiocb = NULL;
11973         struct lpfc_nodelist *ndlp;
11974         uint16_t oxid, rxid;
11975         uint32_t sid, fctl;
11976         IOCB_t *icmd;
11977
11978         if (!lpfc_is_link_up(phba))
11979                 return;
11980
11981         sid = sli4_sid_from_fc_hdr(fc_hdr);
11982         oxid = be16_to_cpu(fc_hdr->fh_ox_id);
11983         rxid = be16_to_cpu(fc_hdr->fh_rx_id);
11984
11985         ndlp = lpfc_findnode_did(phba->pport, sid);
11986         if (!ndlp) {
11987                 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
11988                                 "1268 Find ndlp returned NULL for oxid:x%x "
11989                                 "SID:x%x\n", oxid, sid);
11990                 return;
11991         }
11992         if (rxid >= phba->sli4_hba.max_cfg_param.xri_base
11993                 && rxid <= (phba->sli4_hba.max_cfg_param.max_xri
11994                 + phba->sli4_hba.max_cfg_param.xri_base))
11995                 lpfc_set_rrq_active(phba, ndlp, rxid, oxid, 0);
11996
11997         /* Allocate buffer for acc iocb */
11998         ctiocb = lpfc_sli_get_iocbq(phba);
11999         if (!ctiocb)
12000                 return;
12001
12002         /* Extract the F_CTL field from FC_HDR */
12003         fctl = sli4_fctl_from_fc_hdr(fc_hdr);
12004
12005         icmd = &ctiocb->iocb;
12006         icmd->un.xseq64.bdl.bdeSize = 0;
12007         icmd->un.xseq64.bdl.ulpIoTag32 = 0;
12008         icmd->un.xseq64.w5.hcsw.Dfctl = 0;
12009         icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
12010         icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
12011
12012         /* Fill in the rest of iocb fields */
12013         icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
12014         icmd->ulpBdeCount = 0;
12015         icmd->ulpLe = 1;
12016         icmd->ulpClass = CLASS3;
12017         icmd->ulpContext = ndlp->nlp_rpi;
12018         ctiocb->context1 = ndlp;
12019
12020         ctiocb->iocb_cmpl = NULL;
12021         ctiocb->vport = phba->pport;
12022         ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_acc_cmpl;
12023
12024         if (fctl & FC_FC_EX_CTX) {
12025                 /* ABTS sent by responder to CT exchange, construction
12026                  * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
12027                  * field and RX_ID from ABTS for RX_ID field.
12028                  */
12029                 bf_set(lpfc_abts_orig, &icmd->un.bls_acc, LPFC_ABTS_UNSOL_RSP);
12030                 bf_set(lpfc_abts_rxid, &icmd->un.bls_acc, rxid);
12031                 ctiocb->sli4_xritag = oxid;
12032         } else {
12033                 /* ABTS sent by initiator to CT exchange, construction
12034                  * of BA_ACC will need to allocate a new XRI as for the
12035                  * XRI_TAG and RX_ID fields.
12036                  */
12037                 bf_set(lpfc_abts_orig, &icmd->un.bls_acc, LPFC_ABTS_UNSOL_INT);
12038                 bf_set(lpfc_abts_rxid, &icmd->un.bls_acc, NO_XRI);
12039                 ctiocb->sli4_xritag = NO_XRI;
12040         }
12041         bf_set(lpfc_abts_oxid, &icmd->un.bls_acc, oxid);
12042
12043         /* Xmit CT abts accept on exchange <xid> */
12044         lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
12045                         "1200 Xmit CT ABTS ACC on exchange x%x Data: x%x\n",
12046                         CMD_XMIT_BLS_RSP64_CX, phba->link_state);
12047         lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
12048 }
12049
12050 /**
12051  * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
12052  * @vport: Pointer to the vport on which this sequence was received
12053  * @dmabuf: pointer to a dmabuf that describes the FC sequence
12054  *
12055  * This function handles an SLI-4 unsolicited abort event. If the unsolicited
12056  * receive sequence is only partially assembed by the driver, it shall abort
12057  * the partially assembled frames for the sequence. Otherwise, if the
12058  * unsolicited receive sequence has been completely assembled and passed to
12059  * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
12060  * unsolicited sequence has been aborted. After that, it will issue a basic
12061  * accept to accept the abort.
12062  **/
12063 void
12064 lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
12065                              struct hbq_dmabuf *dmabuf)
12066 {
12067         struct lpfc_hba *phba = vport->phba;
12068         struct fc_frame_header fc_hdr;
12069         uint32_t fctl;
12070         bool abts_par;
12071
12072         /* Make a copy of fc_hdr before the dmabuf being released */
12073         memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
12074         fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
12075
12076         if (fctl & FC_FC_EX_CTX) {
12077                 /*
12078                  * ABTS sent by responder to exchange, just free the buffer
12079                  */
12080                 lpfc_in_buf_free(phba, &dmabuf->dbuf);
12081         } else {
12082                 /*
12083                  * ABTS sent by initiator to exchange, need to do cleanup
12084                  */
12085                 /* Try to abort partially assembled seq */
12086                 abts_par = lpfc_sli4_abort_partial_seq(vport, dmabuf);
12087
12088                 /* Send abort to ULP if partially seq abort failed */
12089                 if (abts_par == false)
12090                         lpfc_sli4_send_seq_to_ulp(vport, dmabuf);
12091                 else
12092                         lpfc_in_buf_free(phba, &dmabuf->dbuf);
12093         }
12094         /* Send basic accept (BA_ACC) to the abort requester */
12095         lpfc_sli4_seq_abort_acc(phba, &fc_hdr);
12096 }
12097
12098 /**
12099  * lpfc_seq_complete - Indicates if a sequence is complete
12100  * @dmabuf: pointer to a dmabuf that describes the FC sequence
12101  *
12102  * This function checks the sequence, starting with the frame described by
12103  * @dmabuf, to see if all the frames associated with this sequence are present.
12104  * the frames associated with this sequence are linked to the @dmabuf using the
12105  * dbuf list. This function looks for two major things. 1) That the first frame
12106  * has a sequence count of zero. 2) There is a frame with last frame of sequence
12107  * set. 3) That there are no holes in the sequence count. The function will
12108  * return 1 when the sequence is complete, otherwise it will return 0.
12109  **/
12110 static int
12111 lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
12112 {
12113         struct fc_frame_header *hdr;
12114         struct lpfc_dmabuf *d_buf;
12115         struct hbq_dmabuf *seq_dmabuf;
12116         uint32_t fctl;
12117         int seq_count = 0;
12118
12119         hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
12120         /* make sure first fame of sequence has a sequence count of zero */
12121         if (hdr->fh_seq_cnt != seq_count)
12122                 return 0;
12123         fctl = (hdr->fh_f_ctl[0] << 16 |
12124                 hdr->fh_f_ctl[1] << 8 |
12125                 hdr->fh_f_ctl[2]);
12126         /* If last frame of sequence we can return success. */
12127         if (fctl & FC_FC_END_SEQ)
12128                 return 1;
12129         list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
12130                 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
12131                 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
12132                 /* If there is a hole in the sequence count then fail. */
12133                 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
12134                         return 0;
12135                 fctl = (hdr->fh_f_ctl[0] << 16 |
12136                         hdr->fh_f_ctl[1] << 8 |
12137                         hdr->fh_f_ctl[2]);
12138                 /* If last frame of sequence we can return success. */
12139                 if (fctl & FC_FC_END_SEQ)
12140                         return 1;
12141         }
12142         return 0;
12143 }
12144
12145 /**
12146  * lpfc_prep_seq - Prep sequence for ULP processing
12147  * @vport: Pointer to the vport on which this sequence was received
12148  * @dmabuf: pointer to a dmabuf that describes the FC sequence
12149  *
12150  * This function takes a sequence, described by a list of frames, and creates
12151  * a list of iocbq structures to describe the sequence. This iocbq list will be
12152  * used to issue to the generic unsolicited sequence handler. This routine
12153  * returns a pointer to the first iocbq in the list. If the function is unable
12154  * to allocate an iocbq then it throw out the received frames that were not
12155  * able to be described and return a pointer to the first iocbq. If unable to
12156  * allocate any iocbqs (including the first) this function will return NULL.
12157  **/
12158 static struct lpfc_iocbq *
12159 lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
12160 {
12161         struct lpfc_dmabuf *d_buf, *n_buf;
12162         struct lpfc_iocbq *first_iocbq, *iocbq;
12163         struct fc_frame_header *fc_hdr;
12164         uint32_t sid;
12165         struct ulp_bde64 *pbde;
12166
12167         fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
12168         /* remove from receive buffer list */
12169         list_del_init(&seq_dmabuf->hbuf.list);
12170         lpfc_update_rcv_time_stamp(vport);
12171         /* get the Remote Port's SID */
12172         sid = sli4_sid_from_fc_hdr(fc_hdr);
12173         /* Get an iocbq struct to fill in. */
12174         first_iocbq = lpfc_sli_get_iocbq(vport->phba);
12175         if (first_iocbq) {
12176                 /* Initialize the first IOCB. */
12177                 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
12178                 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
12179                 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
12180                 first_iocbq->iocb.ulpContext = be16_to_cpu(fc_hdr->fh_ox_id);
12181                 first_iocbq->iocb.unsli3.rcvsli3.vpi =
12182                                         vport->vpi + vport->phba->vpi_base;
12183                 /* put the first buffer into the first IOCBq */
12184                 first_iocbq->context2 = &seq_dmabuf->dbuf;
12185                 first_iocbq->context3 = NULL;
12186                 first_iocbq->iocb.ulpBdeCount = 1;
12187                 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
12188                                                         LPFC_DATA_BUF_SIZE;
12189                 first_iocbq->iocb.un.rcvels.remoteID = sid;
12190                 first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
12191                                 bf_get(lpfc_rcqe_length,
12192                                        &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
12193         }
12194         iocbq = first_iocbq;
12195         /*
12196          * Each IOCBq can have two Buffers assigned, so go through the list
12197          * of buffers for this sequence and save two buffers in each IOCBq
12198          */
12199         list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
12200                 if (!iocbq) {
12201                         lpfc_in_buf_free(vport->phba, d_buf);
12202                         continue;
12203                 }
12204                 if (!iocbq->context3) {
12205                         iocbq->context3 = d_buf;
12206                         iocbq->iocb.ulpBdeCount++;
12207                         pbde = (struct ulp_bde64 *)
12208                                         &iocbq->iocb.unsli3.sli3Words[4];
12209                         pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
12210                         first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
12211                                 bf_get(lpfc_rcqe_length,
12212                                        &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
12213                 } else {
12214                         iocbq = lpfc_sli_get_iocbq(vport->phba);
12215                         if (!iocbq) {
12216                                 if (first_iocbq) {
12217                                         first_iocbq->iocb.ulpStatus =
12218                                                         IOSTAT_FCP_RSP_ERROR;
12219                                         first_iocbq->iocb.un.ulpWord[4] =
12220                                                         IOERR_NO_RESOURCES;
12221                                 }
12222                                 lpfc_in_buf_free(vport->phba, d_buf);
12223                                 continue;
12224                         }
12225                         iocbq->context2 = d_buf;
12226                         iocbq->context3 = NULL;
12227                         iocbq->iocb.ulpBdeCount = 1;
12228                         iocbq->iocb.un.cont64[0].tus.f.bdeSize =
12229                                                         LPFC_DATA_BUF_SIZE;
12230                         first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
12231                                 bf_get(lpfc_rcqe_length,
12232                                        &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
12233                         iocbq->iocb.un.rcvels.remoteID = sid;
12234                         list_add_tail(&iocbq->list, &first_iocbq->list);
12235                 }
12236         }
12237         return first_iocbq;
12238 }
12239
12240 static void
12241 lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
12242                           struct hbq_dmabuf *seq_dmabuf)
12243 {
12244         struct fc_frame_header *fc_hdr;
12245         struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
12246         struct lpfc_hba *phba = vport->phba;
12247
12248         fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
12249         iocbq = lpfc_prep_seq(vport, seq_dmabuf);
12250         if (!iocbq) {
12251                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12252                                 "2707 Ring %d handler: Failed to allocate "
12253                                 "iocb Rctl x%x Type x%x received\n",
12254                                 LPFC_ELS_RING,
12255                                 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
12256                 return;
12257         }
12258         if (!lpfc_complete_unsol_iocb(phba,
12259                                       &phba->sli.ring[LPFC_ELS_RING],
12260                                       iocbq, fc_hdr->fh_r_ctl,
12261                                       fc_hdr->fh_type))
12262                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12263                                 "2540 Ring %d handler: unexpected Rctl "
12264                                 "x%x Type x%x received\n",
12265                                 LPFC_ELS_RING,
12266                                 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
12267
12268         /* Free iocb created in lpfc_prep_seq */
12269         list_for_each_entry_safe(curr_iocb, next_iocb,
12270                 &iocbq->list, list) {
12271                 list_del_init(&curr_iocb->list);
12272                 lpfc_sli_release_iocbq(phba, curr_iocb);
12273         }
12274         lpfc_sli_release_iocbq(phba, iocbq);
12275 }
12276
12277 /**
12278  * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
12279  * @phba: Pointer to HBA context object.
12280  *
12281  * This function is called with no lock held. This function processes all
12282  * the received buffers and gives it to upper layers when a received buffer
12283  * indicates that it is the final frame in the sequence. The interrupt
12284  * service routine processes received buffers at interrupt contexts and adds
12285  * received dma buffers to the rb_pend_list queue and signals the worker thread.
12286  * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
12287  * appropriate receive function when the final frame in a sequence is received.
12288  **/
12289 void
12290 lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
12291                                  struct hbq_dmabuf *dmabuf)
12292 {
12293         struct hbq_dmabuf *seq_dmabuf;
12294         struct fc_frame_header *fc_hdr;
12295         struct lpfc_vport *vport;
12296         uint32_t fcfi;
12297
12298         /* Process each received buffer */
12299         fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
12300         /* check to see if this a valid type of frame */
12301         if (lpfc_fc_frame_check(phba, fc_hdr)) {
12302                 lpfc_in_buf_free(phba, &dmabuf->dbuf);
12303                 return;
12304         }
12305         fcfi = bf_get(lpfc_rcqe_fcf_id, &dmabuf->cq_event.cqe.rcqe_cmpl);
12306         vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi);
12307         if (!vport || !(vport->vpi_state & LPFC_VPI_REGISTERED)) {
12308                 /* throw out the frame */
12309                 lpfc_in_buf_free(phba, &dmabuf->dbuf);
12310                 return;
12311         }
12312         /* Handle the basic abort sequence (BA_ABTS) event */
12313         if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
12314                 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
12315                 return;
12316         }
12317
12318         /* Link this frame */
12319         seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
12320         if (!seq_dmabuf) {
12321                 /* unable to add frame to vport - throw it out */
12322                 lpfc_in_buf_free(phba, &dmabuf->dbuf);
12323                 return;
12324         }
12325         /* If not last frame in sequence continue processing frames. */
12326         if (!lpfc_seq_complete(seq_dmabuf))
12327                 return;
12328
12329         /* Send the complete sequence to the upper layer protocol */
12330         lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
12331 }
12332
12333 /**
12334  * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
12335  * @phba: pointer to lpfc hba data structure.
12336  *
12337  * This routine is invoked to post rpi header templates to the
12338  * HBA consistent with the SLI-4 interface spec.  This routine
12339  * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
12340  * SLI4_PAGE_SIZE modulo 64 rpi context headers.
12341  *
12342  * This routine does not require any locks.  It's usage is expected
12343  * to be driver load or reset recovery when the driver is
12344  * sequential.
12345  *
12346  * Return codes
12347  *      0 - successful
12348  *      -EIO - The mailbox failed to complete successfully.
12349  *      When this error occurs, the driver is not guaranteed
12350  *      to have any rpi regions posted to the device and
12351  *      must either attempt to repost the regions or take a
12352  *      fatal error.
12353  **/
12354 int
12355 lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
12356 {
12357         struct lpfc_rpi_hdr *rpi_page;
12358         uint32_t rc = 0;
12359
12360         /* Post all rpi memory regions to the port. */
12361         list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
12362                 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
12363                 if (rc != MBX_SUCCESS) {
12364                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12365                                         "2008 Error %d posting all rpi "
12366                                         "headers\n", rc);
12367                         rc = -EIO;
12368                         break;
12369                 }
12370         }
12371
12372         return rc;
12373 }
12374
12375 /**
12376  * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
12377  * @phba: pointer to lpfc hba data structure.
12378  * @rpi_page:  pointer to the rpi memory region.
12379  *
12380  * This routine is invoked to post a single rpi header to the
12381  * HBA consistent with the SLI-4 interface spec.  This memory region
12382  * maps up to 64 rpi context regions.
12383  *
12384  * Return codes
12385  *      0 - successful
12386  *      -ENOMEM - No available memory
12387  *      -EIO - The mailbox failed to complete successfully.
12388  **/
12389 int
12390 lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
12391 {
12392         LPFC_MBOXQ_t *mboxq;
12393         struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
12394         uint32_t rc = 0;
12395         uint32_t mbox_tmo;
12396         uint32_t shdr_status, shdr_add_status;
12397         union lpfc_sli4_cfg_shdr *shdr;
12398
12399         /* The port is notified of the header region via a mailbox command. */
12400         mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12401         if (!mboxq) {
12402                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12403                                 "2001 Unable to allocate memory for issuing "
12404                                 "SLI_CONFIG_SPECIAL mailbox command\n");
12405                 return -ENOMEM;
12406         }
12407
12408         /* Post all rpi memory regions to the port. */
12409         hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
12410         mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
12411         lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
12412                          LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
12413                          sizeof(struct lpfc_mbx_post_hdr_tmpl) -
12414                          sizeof(struct mbox_header), LPFC_SLI4_MBX_EMBED);
12415         bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
12416                hdr_tmpl, rpi_page->page_count);
12417         bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
12418                rpi_page->start_rpi);
12419         hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
12420         hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
12421         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
12422         shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
12423         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12424         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12425         if (rc != MBX_TIMEOUT)
12426                 mempool_free(mboxq, phba->mbox_mem_pool);
12427         if (shdr_status || shdr_add_status || rc) {
12428                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12429                                 "2514 POST_RPI_HDR mailbox failed with "
12430                                 "status x%x add_status x%x, mbx status x%x\n",
12431                                 shdr_status, shdr_add_status, rc);
12432                 rc = -ENXIO;
12433         }
12434         return rc;
12435 }
12436
12437 /**
12438  * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
12439  * @phba: pointer to lpfc hba data structure.
12440  *
12441  * This routine is invoked to post rpi header templates to the
12442  * HBA consistent with the SLI-4 interface spec.  This routine
12443  * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
12444  * SLI4_PAGE_SIZE modulo 64 rpi context headers.
12445  *
12446  * Returns
12447  *      A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
12448  *      LPFC_RPI_ALLOC_ERROR if no rpis are available.
12449  **/
12450 int
12451 lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
12452 {
12453         int rpi;
12454         uint16_t max_rpi, rpi_base, rpi_limit;
12455         uint16_t rpi_remaining;
12456         struct lpfc_rpi_hdr *rpi_hdr;
12457
12458         max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
12459         rpi_base = phba->sli4_hba.max_cfg_param.rpi_base;
12460         rpi_limit = phba->sli4_hba.next_rpi;
12461
12462         /*
12463          * The valid rpi range is not guaranteed to be zero-based.  Start
12464          * the search at the rpi_base as reported by the port.
12465          */
12466         spin_lock_irq(&phba->hbalock);
12467         rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, rpi_base);
12468         if (rpi >= rpi_limit || rpi < rpi_base)
12469                 rpi = LPFC_RPI_ALLOC_ERROR;
12470         else {
12471                 set_bit(rpi, phba->sli4_hba.rpi_bmask);
12472                 phba->sli4_hba.max_cfg_param.rpi_used++;
12473                 phba->sli4_hba.rpi_count++;
12474         }
12475
12476         /*
12477          * Don't try to allocate more rpi header regions if the device limit
12478          * on available rpis max has been exhausted.
12479          */
12480         if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
12481             (phba->sli4_hba.rpi_count >= max_rpi)) {
12482                 spin_unlock_irq(&phba->hbalock);
12483                 return rpi;
12484         }
12485
12486         /*
12487          * If the driver is running low on rpi resources, allocate another
12488          * page now.  Note that the next_rpi value is used because
12489          * it represents how many are actually in use whereas max_rpi notes
12490          * how many are supported max by the device.
12491          */
12492         rpi_remaining = phba->sli4_hba.next_rpi - rpi_base -
12493                 phba->sli4_hba.rpi_count;
12494         spin_unlock_irq(&phba->hbalock);
12495         if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
12496                 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
12497                 if (!rpi_hdr) {
12498                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12499                                         "2002 Error Could not grow rpi "
12500                                         "count\n");
12501                 } else {
12502                         lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
12503                 }
12504         }
12505
12506         return rpi;
12507 }
12508
12509 /**
12510  * lpfc_sli4_free_rpi - Release an rpi for reuse.
12511  * @phba: pointer to lpfc hba data structure.
12512  *
12513  * This routine is invoked to release an rpi to the pool of
12514  * available rpis maintained by the driver.
12515  **/
12516 void
12517 __lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
12518 {
12519         if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
12520                 phba->sli4_hba.rpi_count--;
12521                 phba->sli4_hba.max_cfg_param.rpi_used--;
12522         }
12523 }
12524
12525 /**
12526  * lpfc_sli4_free_rpi - Release an rpi for reuse.
12527  * @phba: pointer to lpfc hba data structure.
12528  *
12529  * This routine is invoked to release an rpi to the pool of
12530  * available rpis maintained by the driver.
12531  **/
12532 void
12533 lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
12534 {
12535         spin_lock_irq(&phba->hbalock);
12536         __lpfc_sli4_free_rpi(phba, rpi);
12537         spin_unlock_irq(&phba->hbalock);
12538 }
12539
12540 /**
12541  * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
12542  * @phba: pointer to lpfc hba data structure.
12543  *
12544  * This routine is invoked to remove the memory region that
12545  * provided rpi via a bitmask.
12546  **/
12547 void
12548 lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
12549 {
12550         kfree(phba->sli4_hba.rpi_bmask);
12551 }
12552
12553 /**
12554  * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
12555  * @phba: pointer to lpfc hba data structure.
12556  *
12557  * This routine is invoked to remove the memory region that
12558  * provided rpi via a bitmask.
12559  **/
12560 int
12561 lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp)
12562 {
12563         LPFC_MBOXQ_t *mboxq;
12564         struct lpfc_hba *phba = ndlp->phba;
12565         int rc;
12566
12567         /* The port is notified of the header region via a mailbox command. */
12568         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12569         if (!mboxq)
12570                 return -ENOMEM;
12571
12572         /* Post all rpi memory regions to the port. */
12573         lpfc_resume_rpi(mboxq, ndlp);
12574         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
12575         if (rc == MBX_NOT_FINISHED) {
12576                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12577                                 "2010 Resume RPI Mailbox failed "
12578                                 "status %d, mbxStatus x%x\n", rc,
12579                                 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
12580                 mempool_free(mboxq, phba->mbox_mem_pool);
12581                 return -EIO;
12582         }
12583         return 0;
12584 }
12585
12586 /**
12587  * lpfc_sli4_init_vpi - Initialize a vpi with the port
12588  * @vport: Pointer to the vport for which the vpi is being initialized
12589  *
12590  * This routine is invoked to activate a vpi with the port.
12591  *
12592  * Returns:
12593  *    0 success
12594  *    -Evalue otherwise
12595  **/
12596 int
12597 lpfc_sli4_init_vpi(struct lpfc_vport *vport)
12598 {
12599         LPFC_MBOXQ_t *mboxq;
12600         int rc = 0;
12601         int retval = MBX_SUCCESS;
12602         uint32_t mbox_tmo;
12603         struct lpfc_hba *phba = vport->phba;
12604         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12605         if (!mboxq)
12606                 return -ENOMEM;
12607         lpfc_init_vpi(phba, mboxq, vport->vpi);
12608         mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_INIT_VPI);
12609         rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
12610         if (rc != MBX_SUCCESS) {
12611                 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
12612                                 "2022 INIT VPI Mailbox failed "
12613                                 "status %d, mbxStatus x%x\n", rc,
12614                                 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
12615                 retval = -EIO;
12616         }
12617         if (rc != MBX_TIMEOUT)
12618                 mempool_free(mboxq, vport->phba->mbox_mem_pool);
12619
12620         return retval;
12621 }
12622
12623 /**
12624  * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
12625  * @phba: pointer to lpfc hba data structure.
12626  * @mboxq: Pointer to mailbox object.
12627  *
12628  * This routine is invoked to manually add a single FCF record. The caller
12629  * must pass a completely initialized FCF_Record.  This routine takes
12630  * care of the nonembedded mailbox operations.
12631  **/
12632 static void
12633 lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
12634 {
12635         void *virt_addr;
12636         union lpfc_sli4_cfg_shdr *shdr;
12637         uint32_t shdr_status, shdr_add_status;
12638
12639         virt_addr = mboxq->sge_array->addr[0];
12640         /* The IOCTL status is embedded in the mailbox subheader. */
12641         shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
12642         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12643         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12644
12645         if ((shdr_status || shdr_add_status) &&
12646                 (shdr_status != STATUS_FCF_IN_USE))
12647                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12648                         "2558 ADD_FCF_RECORD mailbox failed with "
12649                         "status x%x add_status x%x\n",
12650                         shdr_status, shdr_add_status);
12651
12652         lpfc_sli4_mbox_cmd_free(phba, mboxq);
12653 }
12654
12655 /**
12656  * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
12657  * @phba: pointer to lpfc hba data structure.
12658  * @fcf_record:  pointer to the initialized fcf record to add.
12659  *
12660  * This routine is invoked to manually add a single FCF record. The caller
12661  * must pass a completely initialized FCF_Record.  This routine takes
12662  * care of the nonembedded mailbox operations.
12663  **/
12664 int
12665 lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
12666 {
12667         int rc = 0;
12668         LPFC_MBOXQ_t *mboxq;
12669         uint8_t *bytep;
12670         void *virt_addr;
12671         dma_addr_t phys_addr;
12672         struct lpfc_mbx_sge sge;
12673         uint32_t alloc_len, req_len;
12674         uint32_t fcfindex;
12675
12676         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12677         if (!mboxq) {
12678                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12679                         "2009 Failed to allocate mbox for ADD_FCF cmd\n");
12680                 return -ENOMEM;
12681         }
12682
12683         req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
12684                   sizeof(uint32_t);
12685
12686         /* Allocate DMA memory and set up the non-embedded mailbox command */
12687         alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
12688                                      LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
12689                                      req_len, LPFC_SLI4_MBX_NEMBED);
12690         if (alloc_len < req_len) {
12691                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12692                         "2523 Allocated DMA memory size (x%x) is "
12693                         "less than the requested DMA memory "
12694                         "size (x%x)\n", alloc_len, req_len);
12695                 lpfc_sli4_mbox_cmd_free(phba, mboxq);
12696                 return -ENOMEM;
12697         }
12698
12699         /*
12700          * Get the first SGE entry from the non-embedded DMA memory.  This
12701          * routine only uses a single SGE.
12702          */
12703         lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
12704         phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
12705         virt_addr = mboxq->sge_array->addr[0];
12706         /*
12707          * Configure the FCF record for FCFI 0.  This is the driver's
12708          * hardcoded default and gets used in nonFIP mode.
12709          */
12710         fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
12711         bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
12712         lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
12713
12714         /*
12715          * Copy the fcf_index and the FCF Record Data. The data starts after
12716          * the FCoE header plus word10. The data copy needs to be endian
12717          * correct.
12718          */
12719         bytep += sizeof(uint32_t);
12720         lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
12721         mboxq->vport = phba->pport;
12722         mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
12723         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
12724         if (rc == MBX_NOT_FINISHED) {
12725                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12726                         "2515 ADD_FCF_RECORD mailbox failed with "
12727                         "status 0x%x\n", rc);
12728                 lpfc_sli4_mbox_cmd_free(phba, mboxq);
12729                 rc = -EIO;
12730         } else
12731                 rc = 0;
12732
12733         return rc;
12734 }
12735
12736 /**
12737  * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
12738  * @phba: pointer to lpfc hba data structure.
12739  * @fcf_record:  pointer to the fcf record to write the default data.
12740  * @fcf_index: FCF table entry index.
12741  *
12742  * This routine is invoked to build the driver's default FCF record.  The
12743  * values used are hardcoded.  This routine handles memory initialization.
12744  *
12745  **/
12746 void
12747 lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
12748                                 struct fcf_record *fcf_record,
12749                                 uint16_t fcf_index)
12750 {
12751         memset(fcf_record, 0, sizeof(struct fcf_record));
12752         fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
12753         fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
12754         fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
12755         bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
12756         bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
12757         bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
12758         bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
12759         bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
12760         bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
12761         bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
12762         bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
12763         bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
12764         bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
12765         bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
12766         bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
12767         bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
12768                 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
12769         /* Set the VLAN bit map */
12770         if (phba->valid_vlan) {
12771                 fcf_record->vlan_bitmap[phba->vlan_id / 8]
12772                         = 1 << (phba->vlan_id % 8);
12773         }
12774 }
12775
12776 /**
12777  * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
12778  * @phba: pointer to lpfc hba data structure.
12779  * @fcf_index: FCF table entry offset.
12780  *
12781  * This routine is invoked to scan the entire FCF table by reading FCF
12782  * record and processing it one at a time starting from the @fcf_index
12783  * for initial FCF discovery or fast FCF failover rediscovery.
12784  *
12785  * Return 0 if the mailbox command is submitted sucessfully, none 0
12786  * otherwise.
12787  **/
12788 int
12789 lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
12790 {
12791         int rc = 0, error;
12792         LPFC_MBOXQ_t *mboxq;
12793
12794         phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
12795         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12796         if (!mboxq) {
12797                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12798                                 "2000 Failed to allocate mbox for "
12799                                 "READ_FCF cmd\n");
12800                 error = -ENOMEM;
12801                 goto fail_fcf_scan;
12802         }
12803         /* Construct the read FCF record mailbox command */
12804         rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
12805         if (rc) {
12806                 error = -EINVAL;
12807                 goto fail_fcf_scan;
12808         }
12809         /* Issue the mailbox command asynchronously */
12810         mboxq->vport = phba->pport;
12811         mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
12812
12813         spin_lock_irq(&phba->hbalock);
12814         phba->hba_flag |= FCF_TS_INPROG;
12815         spin_unlock_irq(&phba->hbalock);
12816
12817         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
12818         if (rc == MBX_NOT_FINISHED)
12819                 error = -EIO;
12820         else {
12821                 /* Reset eligible FCF count for new scan */
12822                 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
12823                         phba->fcf.eligible_fcf_cnt = 0;
12824                 error = 0;
12825         }
12826 fail_fcf_scan:
12827         if (error) {
12828                 if (mboxq)
12829                         lpfc_sli4_mbox_cmd_free(phba, mboxq);
12830                 /* FCF scan failed, clear FCF_TS_INPROG flag */
12831                 spin_lock_irq(&phba->hbalock);
12832                 phba->hba_flag &= ~FCF_TS_INPROG;
12833                 spin_unlock_irq(&phba->hbalock);
12834         }
12835         return error;
12836 }
12837
12838 /**
12839  * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
12840  * @phba: pointer to lpfc hba data structure.
12841  * @fcf_index: FCF table entry offset.
12842  *
12843  * This routine is invoked to read an FCF record indicated by @fcf_index
12844  * and to use it for FLOGI roundrobin FCF failover.
12845  *
12846  * Return 0 if the mailbox command is submitted sucessfully, none 0
12847  * otherwise.
12848  **/
12849 int
12850 lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
12851 {
12852         int rc = 0, error;
12853         LPFC_MBOXQ_t *mboxq;
12854
12855         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12856         if (!mboxq) {
12857                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
12858                                 "2763 Failed to allocate mbox for "
12859                                 "READ_FCF cmd\n");
12860                 error = -ENOMEM;
12861                 goto fail_fcf_read;
12862         }
12863         /* Construct the read FCF record mailbox command */
12864         rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
12865         if (rc) {
12866                 error = -EINVAL;
12867                 goto fail_fcf_read;
12868         }
12869         /* Issue the mailbox command asynchronously */
12870         mboxq->vport = phba->pport;
12871         mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
12872         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
12873         if (rc == MBX_NOT_FINISHED)
12874                 error = -EIO;
12875         else
12876                 error = 0;
12877
12878 fail_fcf_read:
12879         if (error && mboxq)
12880                 lpfc_sli4_mbox_cmd_free(phba, mboxq);
12881         return error;
12882 }
12883
12884 /**
12885  * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
12886  * @phba: pointer to lpfc hba data structure.
12887  * @fcf_index: FCF table entry offset.
12888  *
12889  * This routine is invoked to read an FCF record indicated by @fcf_index to
12890  * determine whether it's eligible for FLOGI roundrobin failover list.
12891  *
12892  * Return 0 if the mailbox command is submitted sucessfully, none 0
12893  * otherwise.
12894  **/
12895 int
12896 lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
12897 {
12898         int rc = 0, error;
12899         LPFC_MBOXQ_t *mboxq;
12900
12901         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12902         if (!mboxq) {
12903                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
12904                                 "2758 Failed to allocate mbox for "
12905                                 "READ_FCF cmd\n");
12906                                 error = -ENOMEM;
12907                                 goto fail_fcf_read;
12908         }
12909         /* Construct the read FCF record mailbox command */
12910         rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
12911         if (rc) {
12912                 error = -EINVAL;
12913                 goto fail_fcf_read;
12914         }
12915         /* Issue the mailbox command asynchronously */
12916         mboxq->vport = phba->pport;
12917         mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
12918         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
12919         if (rc == MBX_NOT_FINISHED)
12920                 error = -EIO;
12921         else
12922                 error = 0;
12923
12924 fail_fcf_read:
12925         if (error && mboxq)
12926                 lpfc_sli4_mbox_cmd_free(phba, mboxq);
12927         return error;
12928 }
12929
12930 /**
12931  * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
12932  * @phba: pointer to lpfc hba data structure.
12933  *
12934  * This routine is to get the next eligible FCF record index in a round
12935  * robin fashion. If the next eligible FCF record index equals to the
12936  * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
12937  * shall be returned, otherwise, the next eligible FCF record's index
12938  * shall be returned.
12939  **/
12940 uint16_t
12941 lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
12942 {
12943         uint16_t next_fcf_index;
12944
12945         /* Search start from next bit of currently registered FCF index */
12946         next_fcf_index = (phba->fcf.current_rec.fcf_indx + 1) %
12947                                         LPFC_SLI4_FCF_TBL_INDX_MAX;
12948         next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
12949                                        LPFC_SLI4_FCF_TBL_INDX_MAX,
12950                                        next_fcf_index);
12951
12952         /* Wrap around condition on phba->fcf.fcf_rr_bmask */
12953         if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
12954                 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
12955                                                LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
12956
12957         /* Check roundrobin failover list empty condition */
12958         if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
12959                 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
12960                                 "2844 No roundrobin failover FCF available\n");
12961                 return LPFC_FCOE_FCF_NEXT_NONE;
12962         }
12963
12964         lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
12965                         "2845 Get next roundrobin failover FCF (x%x)\n",
12966                         next_fcf_index);
12967
12968         return next_fcf_index;
12969 }
12970
12971 /**
12972  * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
12973  * @phba: pointer to lpfc hba data structure.
12974  *
12975  * This routine sets the FCF record index in to the eligible bmask for
12976  * roundrobin failover search. It checks to make sure that the index
12977  * does not go beyond the range of the driver allocated bmask dimension
12978  * before setting the bit.
12979  *
12980  * Returns 0 if the index bit successfully set, otherwise, it returns
12981  * -EINVAL.
12982  **/
12983 int
12984 lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
12985 {
12986         if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
12987                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
12988                                 "2610 FCF (x%x) reached driver's book "
12989                                 "keeping dimension:x%x\n",
12990                                 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
12991                 return -EINVAL;
12992         }
12993         /* Set the eligible FCF record index bmask */
12994         set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
12995
12996         lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
12997                         "2790 Set FCF (x%x) to roundrobin FCF failover "
12998                         "bmask\n", fcf_index);
12999
13000         return 0;
13001 }
13002
13003 /**
13004  * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
13005  * @phba: pointer to lpfc hba data structure.
13006  *
13007  * This routine clears the FCF record index from the eligible bmask for
13008  * roundrobin failover search. It checks to make sure that the index
13009  * does not go beyond the range of the driver allocated bmask dimension
13010  * before clearing the bit.
13011  **/
13012 void
13013 lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
13014 {
13015         if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
13016                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
13017                                 "2762 FCF (x%x) reached driver's book "
13018                                 "keeping dimension:x%x\n",
13019                                 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
13020                 return;
13021         }
13022         /* Clear the eligible FCF record index bmask */
13023         clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
13024
13025         lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
13026                         "2791 Clear FCF (x%x) from roundrobin failover "
13027                         "bmask\n", fcf_index);
13028 }
13029
13030 /**
13031  * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
13032  * @phba: pointer to lpfc hba data structure.
13033  *
13034  * This routine is the completion routine for the rediscover FCF table mailbox
13035  * command. If the mailbox command returned failure, it will try to stop the
13036  * FCF rediscover wait timer.
13037  **/
13038 void
13039 lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
13040 {
13041         struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
13042         uint32_t shdr_status, shdr_add_status;
13043
13044         redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
13045
13046         shdr_status = bf_get(lpfc_mbox_hdr_status,
13047                              &redisc_fcf->header.cfg_shdr.response);
13048         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
13049                              &redisc_fcf->header.cfg_shdr.response);
13050         if (shdr_status || shdr_add_status) {
13051                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
13052                                 "2746 Requesting for FCF rediscovery failed "
13053                                 "status x%x add_status x%x\n",
13054                                 shdr_status, shdr_add_status);
13055                 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
13056                         spin_lock_irq(&phba->hbalock);
13057                         phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
13058                         spin_unlock_irq(&phba->hbalock);
13059                         /*
13060                          * CVL event triggered FCF rediscover request failed,
13061                          * last resort to re-try current registered FCF entry.
13062                          */
13063                         lpfc_retry_pport_discovery(phba);
13064                 } else {
13065                         spin_lock_irq(&phba->hbalock);
13066                         phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
13067                         spin_unlock_irq(&phba->hbalock);
13068                         /*
13069                          * DEAD FCF event triggered FCF rediscover request
13070                          * failed, last resort to fail over as a link down
13071                          * to FCF registration.
13072                          */
13073                         lpfc_sli4_fcf_dead_failthrough(phba);
13074                 }
13075         } else {
13076                 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
13077                                 "2775 Start FCF rediscover quiescent timer\n");
13078                 /*
13079                  * Start FCF rediscovery wait timer for pending FCF
13080                  * before rescan FCF record table.
13081                  */
13082                 lpfc_fcf_redisc_wait_start_timer(phba);
13083         }
13084
13085         mempool_free(mbox, phba->mbox_mem_pool);
13086 }
13087
13088 /**
13089  * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
13090  * @phba: pointer to lpfc hba data structure.
13091  *
13092  * This routine is invoked to request for rediscovery of the entire FCF table
13093  * by the port.
13094  **/
13095 int
13096 lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
13097 {
13098         LPFC_MBOXQ_t *mbox;
13099         struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
13100         int rc, length;
13101
13102         /* Cancel retry delay timers to all vports before FCF rediscover */
13103         lpfc_cancel_all_vport_retry_delay_timer(phba);
13104
13105         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13106         if (!mbox) {
13107                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13108                                 "2745 Failed to allocate mbox for "
13109                                 "requesting FCF rediscover.\n");
13110                 return -ENOMEM;
13111         }
13112
13113         length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
13114                   sizeof(struct lpfc_sli4_cfg_mhdr));
13115         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13116                          LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
13117                          length, LPFC_SLI4_MBX_EMBED);
13118
13119         redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
13120         /* Set count to 0 for invalidating the entire FCF database */
13121         bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
13122
13123         /* Issue the mailbox command asynchronously */
13124         mbox->vport = phba->pport;
13125         mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
13126         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
13127
13128         if (rc == MBX_NOT_FINISHED) {
13129                 mempool_free(mbox, phba->mbox_mem_pool);
13130                 return -EIO;
13131         }
13132         return 0;
13133 }
13134
13135 /**
13136  * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
13137  * @phba: pointer to lpfc hba data structure.
13138  *
13139  * This function is the failover routine as a last resort to the FCF DEAD
13140  * event when driver failed to perform fast FCF failover.
13141  **/
13142 void
13143 lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
13144 {
13145         uint32_t link_state;
13146
13147         /*
13148          * Last resort as FCF DEAD event failover will treat this as
13149          * a link down, but save the link state because we don't want
13150          * it to be changed to Link Down unless it is already down.
13151          */
13152         link_state = phba->link_state;
13153         lpfc_linkdown(phba);
13154         phba->link_state = link_state;
13155
13156         /* Unregister FCF if no devices connected to it */
13157         lpfc_unregister_unused_fcf(phba);
13158 }
13159
13160 /**
13161  * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
13162  * @phba: pointer to lpfc hba data structure.
13163  *
13164  * This function read region 23 and parse TLV for port status to
13165  * decide if the user disaled the port. If the TLV indicates the
13166  * port is disabled, the hba_flag is set accordingly.
13167  **/
13168 void
13169 lpfc_sli_read_link_ste(struct lpfc_hba *phba)
13170 {
13171         LPFC_MBOXQ_t *pmb = NULL;
13172         MAILBOX_t *mb;
13173         uint8_t *rgn23_data = NULL;
13174         uint32_t offset = 0, data_size, sub_tlv_len, tlv_offset;
13175         int rc;
13176
13177         pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13178         if (!pmb) {
13179                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13180                         "2600 lpfc_sli_read_serdes_param failed to"
13181                         " allocate mailbox memory\n");
13182                 goto out;
13183         }
13184         mb = &pmb->u.mb;
13185
13186         /* Get adapter Region 23 data */
13187         rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
13188         if (!rgn23_data)
13189                 goto out;
13190
13191         do {
13192                 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
13193                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
13194
13195                 if (rc != MBX_SUCCESS) {
13196                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
13197                                 "2601 lpfc_sli_read_link_ste failed to"
13198                                 " read config region 23 rc 0x%x Status 0x%x\n",
13199                                 rc, mb->mbxStatus);
13200                         mb->un.varDmp.word_cnt = 0;
13201                 }
13202                 /*
13203                  * dump mem may return a zero when finished or we got a
13204                  * mailbox error, either way we are done.
13205                  */
13206                 if (mb->un.varDmp.word_cnt == 0)
13207                         break;
13208                 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
13209                         mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
13210
13211                 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
13212                         rgn23_data + offset,
13213                         mb->un.varDmp.word_cnt);
13214                 offset += mb->un.varDmp.word_cnt;
13215         } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
13216
13217         data_size = offset;
13218         offset = 0;
13219
13220         if (!data_size)
13221                 goto out;
13222
13223         /* Check the region signature first */
13224         if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
13225                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13226                         "2619 Config region 23 has bad signature\n");
13227                         goto out;
13228         }
13229         offset += 4;
13230
13231         /* Check the data structure version */
13232         if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
13233                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13234                         "2620 Config region 23 has bad version\n");
13235                 goto out;
13236         }
13237         offset += 4;
13238
13239         /* Parse TLV entries in the region */
13240         while (offset < data_size) {
13241                 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
13242                         break;
13243                 /*
13244                  * If the TLV is not driver specific TLV or driver id is
13245                  * not linux driver id, skip the record.
13246                  */
13247                 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
13248                     (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
13249                     (rgn23_data[offset + 3] != 0)) {
13250                         offset += rgn23_data[offset + 1] * 4 + 4;
13251                         continue;
13252                 }
13253
13254                 /* Driver found a driver specific TLV in the config region */
13255                 sub_tlv_len = rgn23_data[offset + 1] * 4;
13256                 offset += 4;
13257                 tlv_offset = 0;
13258
13259                 /*
13260                  * Search for configured port state sub-TLV.
13261                  */
13262                 while ((offset < data_size) &&
13263                         (tlv_offset < sub_tlv_len)) {
13264                         if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
13265                                 offset += 4;
13266                                 tlv_offset += 4;
13267                                 break;
13268                         }
13269                         if (rgn23_data[offset] != PORT_STE_TYPE) {
13270                                 offset += rgn23_data[offset + 1] * 4 + 4;
13271                                 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
13272                                 continue;
13273                         }
13274
13275                         /* This HBA contains PORT_STE configured */
13276                         if (!rgn23_data[offset + 2])
13277                                 phba->hba_flag |= LINK_DISABLED;
13278
13279                         goto out;
13280                 }
13281         }
13282 out:
13283         if (pmb)
13284                 mempool_free(pmb, phba->mbox_mem_pool);
13285         kfree(rgn23_data);
13286         return;
13287 }
13288
13289 /**
13290  * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
13291  * @vport: pointer to vport data structure.
13292  *
13293  * This function iterate through the mailboxq and clean up all REG_LOGIN
13294  * and REG_VPI mailbox commands associated with the vport. This function
13295  * is called when driver want to restart discovery of the vport due to
13296  * a Clear Virtual Link event.
13297  **/
13298 void
13299 lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
13300 {
13301         struct lpfc_hba *phba = vport->phba;
13302         LPFC_MBOXQ_t *mb, *nextmb;
13303         struct lpfc_dmabuf *mp;
13304         struct lpfc_nodelist *ndlp;
13305         struct lpfc_nodelist *act_mbx_ndlp = NULL;
13306         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
13307         LIST_HEAD(mbox_cmd_list);
13308         uint8_t restart_loop;
13309
13310         /* Clean up internally queued mailbox commands with the vport */
13311         spin_lock_irq(&phba->hbalock);
13312         list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
13313                 if (mb->vport != vport)
13314                         continue;
13315
13316                 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
13317                         (mb->u.mb.mbxCommand != MBX_REG_VPI))
13318                         continue;
13319
13320                 list_del(&mb->list);
13321                 list_add_tail(&mb->list, &mbox_cmd_list);
13322         }
13323         /* Clean up active mailbox command with the vport */
13324         mb = phba->sli.mbox_active;
13325         if (mb && (mb->vport == vport)) {
13326                 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
13327                         (mb->u.mb.mbxCommand == MBX_REG_VPI))
13328                         mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13329                 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
13330                         act_mbx_ndlp = (struct lpfc_nodelist *)mb->context2;
13331                         /* Put reference count for delayed processing */
13332                         act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
13333                         /* Unregister the RPI when mailbox complete */
13334                         mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
13335                 }
13336         }
13337         /* Cleanup any mailbox completions which are not yet processed */
13338         do {
13339                 restart_loop = 0;
13340                 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
13341                         /*
13342                          * If this mailox is already processed or it is
13343                          * for another vport ignore it.
13344                          */
13345                         if ((mb->vport != vport) ||
13346                                 (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
13347                                 continue;
13348
13349                         if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
13350                                 (mb->u.mb.mbxCommand != MBX_REG_VPI))
13351                                 continue;
13352
13353                         mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13354                         if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
13355                                 ndlp = (struct lpfc_nodelist *)mb->context2;
13356                                 /* Unregister the RPI when mailbox complete */
13357                                 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
13358                                 restart_loop = 1;
13359                                 spin_unlock_irq(&phba->hbalock);
13360                                 spin_lock(shost->host_lock);
13361                                 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
13362                                 spin_unlock(shost->host_lock);
13363                                 spin_lock_irq(&phba->hbalock);
13364                                 break;
13365                         }
13366                 }
13367         } while (restart_loop);
13368
13369         spin_unlock_irq(&phba->hbalock);
13370
13371         /* Release the cleaned-up mailbox commands */
13372         while (!list_empty(&mbox_cmd_list)) {
13373                 list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
13374                 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
13375                         if (phba->sli_rev == LPFC_SLI_REV4)
13376                                 __lpfc_sli4_free_rpi(phba,
13377                                                 mb->u.mb.un.varRegLogin.rpi);
13378                         mp = (struct lpfc_dmabuf *) (mb->context1);
13379                         if (mp) {
13380                                 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
13381                                 kfree(mp);
13382                         }
13383                         ndlp = (struct lpfc_nodelist *) mb->context2;
13384                         mb->context2 = NULL;
13385                         if (ndlp) {
13386                                 spin_lock(shost->host_lock);
13387                                 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
13388                                 spin_unlock(shost->host_lock);
13389                                 lpfc_nlp_put(ndlp);
13390                         }
13391                 }
13392                 mempool_free(mb, phba->mbox_mem_pool);
13393         }
13394
13395         /* Release the ndlp with the cleaned-up active mailbox command */
13396         if (act_mbx_ndlp) {
13397                 spin_lock(shost->host_lock);
13398                 act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
13399                 spin_unlock(shost->host_lock);
13400                 lpfc_nlp_put(act_mbx_ndlp);
13401         }
13402 }
13403
13404 /**
13405  * lpfc_drain_txq - Drain the txq
13406  * @phba: Pointer to HBA context object.
13407  *
13408  * This function attempt to submit IOCBs on the txq
13409  * to the adapter.  For SLI4 adapters, the txq contains
13410  * ELS IOCBs that have been deferred because the there
13411  * are no SGLs.  This congestion can occur with large
13412  * vport counts during node discovery.
13413  **/
13414
13415 uint32_t
13416 lpfc_drain_txq(struct lpfc_hba *phba)
13417 {
13418         LIST_HEAD(completions);
13419         struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
13420         struct lpfc_iocbq *piocbq = 0;
13421         unsigned long iflags = 0;
13422         char *fail_msg = NULL;
13423         struct lpfc_sglq *sglq;
13424         union lpfc_wqe wqe;
13425
13426         spin_lock_irqsave(&phba->hbalock, iflags);
13427         if (pring->txq_cnt > pring->txq_max)
13428                 pring->txq_max = pring->txq_cnt;
13429
13430         spin_unlock_irqrestore(&phba->hbalock, iflags);
13431
13432         while (pring->txq_cnt) {
13433                 spin_lock_irqsave(&phba->hbalock, iflags);
13434
13435                 piocbq = lpfc_sli_ringtx_get(phba, pring);
13436                 sglq = __lpfc_sli_get_sglq(phba, piocbq);
13437                 if (!sglq) {
13438                         __lpfc_sli_ringtx_put(phba, pring, piocbq);
13439                         spin_unlock_irqrestore(&phba->hbalock, iflags);
13440                         break;
13441                 } else {
13442                         if (!piocbq) {
13443                                 /* The txq_cnt out of sync. This should
13444                                  * never happen
13445                                  */
13446                                 sglq = __lpfc_clear_active_sglq(phba,
13447                                                  sglq->sli4_xritag);
13448                                 spin_unlock_irqrestore(&phba->hbalock, iflags);
13449                                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13450                                         "2823 txq empty and txq_cnt is %d\n ",
13451                                         pring->txq_cnt);
13452                                 break;
13453                         }
13454                 }
13455
13456                 /* The xri and iocb resources secured,
13457                  * attempt to issue request
13458                  */
13459                 piocbq->sli4_xritag = sglq->sli4_xritag;
13460                 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
13461                         fail_msg = "to convert bpl to sgl";
13462                 else if (lpfc_sli4_iocb2wqe(phba, piocbq, &wqe))
13463                         fail_msg = "to convert iocb to wqe";
13464                 else if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
13465                         fail_msg = " - Wq is full";
13466                 else
13467                         lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
13468
13469                 if (fail_msg) {
13470                         /* Failed means we can't issue and need to cancel */
13471                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13472                                         "2822 IOCB failed %s iotag 0x%x "
13473                                         "xri 0x%x\n",
13474                                         fail_msg,
13475                                         piocbq->iotag, piocbq->sli4_xritag);
13476                         list_add_tail(&piocbq->list, &completions);
13477                 }
13478                 spin_unlock_irqrestore(&phba->hbalock, iflags);
13479         }
13480
13481         /* Cancel all the IOCBs that cannot be issued */
13482         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
13483                                 IOERR_SLI_ABORTED);
13484
13485         return pring->txq_cnt;
13486 }