[SCSI] lpfc 8.3.15: FCoE Related Fixes
[linux-flexiantxendom0-natty.git] / drivers / scsi / lpfc / lpfc_sli.c
1 /*******************************************************************
2  * This file is part of the Emulex Linux Device Driver for         *
3  * Fibre Channel Host Bus Adapters.                                *
4  * Copyright (C) 2004-2009 Emulex.  All rights reserved.           *
5  * EMULEX and SLI are trademarks of Emulex.                        *
6  * www.emulex.com                                                  *
7  * Portions Copyright (C) 2004-2005 Christoph Hellwig              *
8  *                                                                 *
9  * This program is free software; you can redistribute it and/or   *
10  * modify it under the terms of version 2 of the GNU General       *
11  * Public License as published by the Free Software Foundation.    *
12  * This program is distributed in the hope that it will be useful. *
13  * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND          *
14  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,  *
15  * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE      *
16  * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
17  * TO BE LEGALLY INVALID.  See the GNU General Public License for  *
18  * more details, a copy of which can be found in the file COPYING  *
19  * included with this package.                                     *
20  *******************************************************************/
21
22 #include <linux/blkdev.h>
23 #include <linux/pci.h>
24 #include <linux/interrupt.h>
25 #include <linux/delay.h>
26 #include <linux/slab.h>
27
28 #include <scsi/scsi.h>
29 #include <scsi/scsi_cmnd.h>
30 #include <scsi/scsi_device.h>
31 #include <scsi/scsi_host.h>
32 #include <scsi/scsi_transport_fc.h>
33 #include <scsi/fc/fc_fs.h>
34 #include <linux/aer.h>
35
36 #include "lpfc_hw4.h"
37 #include "lpfc_hw.h"
38 #include "lpfc_sli.h"
39 #include "lpfc_sli4.h"
40 #include "lpfc_nl.h"
41 #include "lpfc_disc.h"
42 #include "lpfc_scsi.h"
43 #include "lpfc.h"
44 #include "lpfc_crtn.h"
45 #include "lpfc_logmsg.h"
46 #include "lpfc_compat.h"
47 #include "lpfc_debugfs.h"
48 #include "lpfc_vport.h"
49
50 /* There are only four IOCB completion types. */
51 typedef enum _lpfc_iocb_type {
52         LPFC_UNKNOWN_IOCB,
53         LPFC_UNSOL_IOCB,
54         LPFC_SOL_IOCB,
55         LPFC_ABORT_IOCB
56 } lpfc_iocb_type;
57
58
59 /* Provide function prototypes local to this module. */
60 static int lpfc_sli_issue_mbox_s4(struct lpfc_hba *, LPFC_MBOXQ_t *,
61                                   uint32_t);
62 static int lpfc_sli4_read_rev(struct lpfc_hba *, LPFC_MBOXQ_t *,
63                               uint8_t *, uint32_t *);
64 static struct lpfc_iocbq *lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *,
65                                                          struct lpfc_iocbq *);
66 static void lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *,
67                                       struct hbq_dmabuf *);
68 static IOCB_t *
69 lpfc_get_iocb_from_iocbq(struct lpfc_iocbq *iocbq)
70 {
71         return &iocbq->iocb;
72 }
73
74 /**
75  * lpfc_sli4_wq_put - Put a Work Queue Entry on an Work Queue
76  * @q: The Work Queue to operate on.
77  * @wqe: The work Queue Entry to put on the Work queue.
78  *
79  * This routine will copy the contents of @wqe to the next available entry on
80  * the @q. This function will then ring the Work Queue Doorbell to signal the
81  * HBA to start processing the Work Queue Entry. This function returns 0 if
82  * successful. If no entries are available on @q then this function will return
83  * -ENOMEM.
84  * The caller is expected to hold the hbalock when calling this routine.
85  **/
86 static uint32_t
87 lpfc_sli4_wq_put(struct lpfc_queue *q, union lpfc_wqe *wqe)
88 {
89         union lpfc_wqe *temp_wqe = q->qe[q->host_index].wqe;
90         struct lpfc_register doorbell;
91         uint32_t host_index;
92
93         /* If the host has not yet processed the next entry then we are done */
94         if (((q->host_index + 1) % q->entry_count) == q->hba_index)
95                 return -ENOMEM;
96         /* set consumption flag every once in a while */
97         if (!((q->host_index + 1) % LPFC_RELEASE_NOTIFICATION_INTERVAL))
98                 bf_set(lpfc_wqe_gen_wqec, &wqe->generic, 1);
99
100         lpfc_sli_pcimem_bcopy(wqe, temp_wqe, q->entry_size);
101
102         /* Update the host index before invoking device */
103         host_index = q->host_index;
104         q->host_index = ((q->host_index + 1) % q->entry_count);
105
106         /* Ring Doorbell */
107         doorbell.word0 = 0;
108         bf_set(lpfc_wq_doorbell_num_posted, &doorbell, 1);
109         bf_set(lpfc_wq_doorbell_index, &doorbell, host_index);
110         bf_set(lpfc_wq_doorbell_id, &doorbell, q->queue_id);
111         writel(doorbell.word0, q->phba->sli4_hba.WQDBregaddr);
112         readl(q->phba->sli4_hba.WQDBregaddr); /* Flush */
113
114         return 0;
115 }
116
117 /**
118  * lpfc_sli4_wq_release - Updates internal hba index for WQ
119  * @q: The Work Queue to operate on.
120  * @index: The index to advance the hba index to.
121  *
122  * This routine will update the HBA index of a queue to reflect consumption of
123  * Work Queue Entries by the HBA. When the HBA indicates that it has consumed
124  * an entry the host calls this function to update the queue's internal
125  * pointers. This routine returns the number of entries that were consumed by
126  * the HBA.
127  **/
128 static uint32_t
129 lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)
130 {
131         uint32_t released = 0;
132
133         if (q->hba_index == index)
134                 return 0;
135         do {
136                 q->hba_index = ((q->hba_index + 1) % q->entry_count);
137                 released++;
138         } while (q->hba_index != index);
139         return released;
140 }
141
142 /**
143  * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue
144  * @q: The Mailbox Queue to operate on.
145  * @wqe: The Mailbox Queue Entry to put on the Work queue.
146  *
147  * This routine will copy the contents of @mqe to the next available entry on
148  * the @q. This function will then ring the Work Queue Doorbell to signal the
149  * HBA to start processing the Work Queue Entry. This function returns 0 if
150  * successful. If no entries are available on @q then this function will return
151  * -ENOMEM.
152  * The caller is expected to hold the hbalock when calling this routine.
153  **/
154 static uint32_t
155 lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)
156 {
157         struct lpfc_mqe *temp_mqe = q->qe[q->host_index].mqe;
158         struct lpfc_register doorbell;
159         uint32_t host_index;
160
161         /* If the host has not yet processed the next entry then we are done */
162         if (((q->host_index + 1) % q->entry_count) == q->hba_index)
163                 return -ENOMEM;
164         lpfc_sli_pcimem_bcopy(mqe, temp_mqe, q->entry_size);
165         /* Save off the mailbox pointer for completion */
166         q->phba->mbox = (MAILBOX_t *)temp_mqe;
167
168         /* Update the host index before invoking device */
169         host_index = q->host_index;
170         q->host_index = ((q->host_index + 1) % q->entry_count);
171
172         /* Ring Doorbell */
173         doorbell.word0 = 0;
174         bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);
175         bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);
176         writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);
177         readl(q->phba->sli4_hba.MQDBregaddr); /* Flush */
178         return 0;
179 }
180
181 /**
182  * lpfc_sli4_mq_release - Updates internal hba index for MQ
183  * @q: The Mailbox Queue to operate on.
184  *
185  * This routine will update the HBA index of a queue to reflect consumption of
186  * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed
187  * an entry the host calls this function to update the queue's internal
188  * pointers. This routine returns the number of entries that were consumed by
189  * the HBA.
190  **/
191 static uint32_t
192 lpfc_sli4_mq_release(struct lpfc_queue *q)
193 {
194         /* Clear the mailbox pointer for completion */
195         q->phba->mbox = NULL;
196         q->hba_index = ((q->hba_index + 1) % q->entry_count);
197         return 1;
198 }
199
200 /**
201  * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ
202  * @q: The Event Queue to get the first valid EQE from
203  *
204  * This routine will get the first valid Event Queue Entry from @q, update
205  * the queue's internal hba index, and return the EQE. If no valid EQEs are in
206  * the Queue (no more work to do), or the Queue is full of EQEs that have been
207  * processed, but not popped back to the HBA then this routine will return NULL.
208  **/
209 static struct lpfc_eqe *
210 lpfc_sli4_eq_get(struct lpfc_queue *q)
211 {
212         struct lpfc_eqe *eqe = q->qe[q->hba_index].eqe;
213
214         /* If the next EQE is not valid then we are done */
215         if (!bf_get_le32(lpfc_eqe_valid, eqe))
216                 return NULL;
217         /* If the host has not yet processed the next entry then we are done */
218         if (((q->hba_index + 1) % q->entry_count) == q->host_index)
219                 return NULL;
220
221         q->hba_index = ((q->hba_index + 1) % q->entry_count);
222         return eqe;
223 }
224
225 /**
226  * lpfc_sli4_eq_release - Indicates the host has finished processing an EQ
227  * @q: The Event Queue that the host has completed processing for.
228  * @arm: Indicates whether the host wants to arms this CQ.
229  *
230  * This routine will mark all Event Queue Entries on @q, from the last
231  * known completed entry to the last entry that was processed, as completed
232  * by clearing the valid bit for each completion queue entry. Then it will
233  * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
234  * The internal host index in the @q will be updated by this routine to indicate
235  * that the host has finished processing the entries. The @arm parameter
236  * indicates that the queue should be rearmed when ringing the doorbell.
237  *
238  * This function will return the number of EQEs that were popped.
239  **/
240 uint32_t
241 lpfc_sli4_eq_release(struct lpfc_queue *q, bool arm)
242 {
243         uint32_t released = 0;
244         struct lpfc_eqe *temp_eqe;
245         struct lpfc_register doorbell;
246
247         /* while there are valid entries */
248         while (q->hba_index != q->host_index) {
249                 temp_eqe = q->qe[q->host_index].eqe;
250                 bf_set_le32(lpfc_eqe_valid, temp_eqe, 0);
251                 released++;
252                 q->host_index = ((q->host_index + 1) % q->entry_count);
253         }
254         if (unlikely(released == 0 && !arm))
255                 return 0;
256
257         /* ring doorbell for number popped */
258         doorbell.word0 = 0;
259         if (arm) {
260                 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
261                 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
262         }
263         bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
264         bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
265         bf_set(lpfc_eqcq_doorbell_eqid, &doorbell, q->queue_id);
266         writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
267         /* PCI read to flush PCI pipeline on re-arming for INTx mode */
268         if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
269                 readl(q->phba->sli4_hba.EQCQDBregaddr);
270         return released;
271 }
272
273 /**
274  * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
275  * @q: The Completion Queue to get the first valid CQE from
276  *
277  * This routine will get the first valid Completion Queue Entry from @q, update
278  * the queue's internal hba index, and return the CQE. If no valid CQEs are in
279  * the Queue (no more work to do), or the Queue is full of CQEs that have been
280  * processed, but not popped back to the HBA then this routine will return NULL.
281  **/
282 static struct lpfc_cqe *
283 lpfc_sli4_cq_get(struct lpfc_queue *q)
284 {
285         struct lpfc_cqe *cqe;
286
287         /* If the next CQE is not valid then we are done */
288         if (!bf_get_le32(lpfc_cqe_valid, q->qe[q->hba_index].cqe))
289                 return NULL;
290         /* If the host has not yet processed the next entry then we are done */
291         if (((q->hba_index + 1) % q->entry_count) == q->host_index)
292                 return NULL;
293
294         cqe = q->qe[q->hba_index].cqe;
295         q->hba_index = ((q->hba_index + 1) % q->entry_count);
296         return cqe;
297 }
298
299 /**
300  * lpfc_sli4_cq_release - Indicates the host has finished processing a CQ
301  * @q: The Completion Queue that the host has completed processing for.
302  * @arm: Indicates whether the host wants to arms this CQ.
303  *
304  * This routine will mark all Completion queue entries on @q, from the last
305  * known completed entry to the last entry that was processed, as completed
306  * by clearing the valid bit for each completion queue entry. Then it will
307  * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
308  * The internal host index in the @q will be updated by this routine to indicate
309  * that the host has finished processing the entries. The @arm parameter
310  * indicates that the queue should be rearmed when ringing the doorbell.
311  *
312  * This function will return the number of CQEs that were released.
313  **/
314 uint32_t
315 lpfc_sli4_cq_release(struct lpfc_queue *q, bool arm)
316 {
317         uint32_t released = 0;
318         struct lpfc_cqe *temp_qe;
319         struct lpfc_register doorbell;
320
321         /* while there are valid entries */
322         while (q->hba_index != q->host_index) {
323                 temp_qe = q->qe[q->host_index].cqe;
324                 bf_set_le32(lpfc_cqe_valid, temp_qe, 0);
325                 released++;
326                 q->host_index = ((q->host_index + 1) % q->entry_count);
327         }
328         if (unlikely(released == 0 && !arm))
329                 return 0;
330
331         /* ring doorbell for number popped */
332         doorbell.word0 = 0;
333         if (arm)
334                 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
335         bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
336         bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
337         bf_set(lpfc_eqcq_doorbell_cqid, &doorbell, q->queue_id);
338         writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
339         return released;
340 }
341
342 /**
343  * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
344  * @q: The Header Receive Queue to operate on.
345  * @wqe: The Receive Queue Entry to put on the Receive queue.
346  *
347  * This routine will copy the contents of @wqe to the next available entry on
348  * the @q. This function will then ring the Receive Queue Doorbell to signal the
349  * HBA to start processing the Receive Queue Entry. This function returns the
350  * index that the rqe was copied to if successful. If no entries are available
351  * on @q then this function will return -ENOMEM.
352  * The caller is expected to hold the hbalock when calling this routine.
353  **/
354 static int
355 lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
356                  struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
357 {
358         struct lpfc_rqe *temp_hrqe = hq->qe[hq->host_index].rqe;
359         struct lpfc_rqe *temp_drqe = dq->qe[dq->host_index].rqe;
360         struct lpfc_register doorbell;
361         int put_index = hq->host_index;
362
363         if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
364                 return -EINVAL;
365         if (hq->host_index != dq->host_index)
366                 return -EINVAL;
367         /* If the host has not yet processed the next entry then we are done */
368         if (((hq->host_index + 1) % hq->entry_count) == hq->hba_index)
369                 return -EBUSY;
370         lpfc_sli_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
371         lpfc_sli_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
372
373         /* Update the host index to point to the next slot */
374         hq->host_index = ((hq->host_index + 1) % hq->entry_count);
375         dq->host_index = ((dq->host_index + 1) % dq->entry_count);
376
377         /* Ring The Header Receive Queue Doorbell */
378         if (!(hq->host_index % LPFC_RQ_POST_BATCH)) {
379                 doorbell.word0 = 0;
380                 bf_set(lpfc_rq_doorbell_num_posted, &doorbell,
381                        LPFC_RQ_POST_BATCH);
382                 bf_set(lpfc_rq_doorbell_id, &doorbell, hq->queue_id);
383                 writel(doorbell.word0, hq->phba->sli4_hba.RQDBregaddr);
384         }
385         return put_index;
386 }
387
388 /**
389  * lpfc_sli4_rq_release - Updates internal hba index for RQ
390  * @q: The Header Receive Queue to operate on.
391  *
392  * This routine will update the HBA index of a queue to reflect consumption of
393  * one Receive Queue Entry by the HBA. When the HBA indicates that it has
394  * consumed an entry the host calls this function to update the queue's
395  * internal pointers. This routine returns the number of entries that were
396  * consumed by the HBA.
397  **/
398 static uint32_t
399 lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
400 {
401         if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
402                 return 0;
403         hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
404         dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
405         return 1;
406 }
407
408 /**
409  * lpfc_cmd_iocb - Get next command iocb entry in the ring
410  * @phba: Pointer to HBA context object.
411  * @pring: Pointer to driver SLI ring object.
412  *
413  * This function returns pointer to next command iocb entry
414  * in the command ring. The caller must hold hbalock to prevent
415  * other threads consume the next command iocb.
416  * SLI-2/SLI-3 provide different sized iocbs.
417  **/
418 static inline IOCB_t *
419 lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
420 {
421         return (IOCB_t *) (((char *) pring->cmdringaddr) +
422                            pring->cmdidx * phba->iocb_cmd_size);
423 }
424
425 /**
426  * lpfc_resp_iocb - Get next response iocb entry in the ring
427  * @phba: Pointer to HBA context object.
428  * @pring: Pointer to driver SLI ring object.
429  *
430  * This function returns pointer to next response iocb entry
431  * in the response ring. The caller must hold hbalock to make sure
432  * that no other thread consume the next response iocb.
433  * SLI-2/SLI-3 provide different sized iocbs.
434  **/
435 static inline IOCB_t *
436 lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
437 {
438         return (IOCB_t *) (((char *) pring->rspringaddr) +
439                            pring->rspidx * phba->iocb_rsp_size);
440 }
441
442 /**
443  * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
444  * @phba: Pointer to HBA context object.
445  *
446  * This function is called with hbalock held. This function
447  * allocates a new driver iocb object from the iocb pool. If the
448  * allocation is successful, it returns pointer to the newly
449  * allocated iocb object else it returns NULL.
450  **/
451 static struct lpfc_iocbq *
452 __lpfc_sli_get_iocbq(struct lpfc_hba *phba)
453 {
454         struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
455         struct lpfc_iocbq * iocbq = NULL;
456
457         list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
458
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_sli_get_sglq - Allocates an iocb object from sgl pool
517  * @phba: Pointer to HBA context object.
518  *
519  * This function is called with hbalock held. This function
520  * Gets a new driver sglq object from the sglq list. If the
521  * list is not empty then it is successful, it returns pointer to the newly
522  * allocated sglq object else it returns NULL.
523  **/
524 static struct lpfc_sglq *
525 __lpfc_sli_get_sglq(struct lpfc_hba *phba)
526 {
527         struct list_head *lpfc_sgl_list = &phba->sli4_hba.lpfc_sgl_list;
528         struct lpfc_sglq *sglq = NULL;
529         uint16_t adj_xri;
530         list_remove_head(lpfc_sgl_list, sglq, struct lpfc_sglq, list);
531         if (!sglq)
532                 return NULL;
533         adj_xri = sglq->sli4_xritag - phba->sli4_hba.max_cfg_param.xri_base;
534         phba->sli4_hba.lpfc_sglq_active_list[adj_xri] = sglq;
535         sglq->state = SGL_ALLOCATED;
536         return sglq;
537 }
538
539 /**
540  * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
541  * @phba: Pointer to HBA context object.
542  *
543  * This function is called with no lock held. This function
544  * allocates a new driver iocb object from the iocb pool. If the
545  * allocation is successful, it returns pointer to the newly
546  * allocated iocb object else it returns NULL.
547  **/
548 struct lpfc_iocbq *
549 lpfc_sli_get_iocbq(struct lpfc_hba *phba)
550 {
551         struct lpfc_iocbq * iocbq = NULL;
552         unsigned long iflags;
553
554         spin_lock_irqsave(&phba->hbalock, iflags);
555         iocbq = __lpfc_sli_get_iocbq(phba);
556         spin_unlock_irqrestore(&phba->hbalock, iflags);
557         return iocbq;
558 }
559
560 /**
561  * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
562  * @phba: Pointer to HBA context object.
563  * @iocbq: Pointer to driver iocb object.
564  *
565  * This function is called with hbalock held to release driver
566  * iocb object to the iocb pool. The iotag in the iocb object
567  * does not change for each use of the iocb object. This function
568  * clears all other fields of the iocb object when it is freed.
569  * The sqlq structure that holds the xritag and phys and virtual
570  * mappings for the scatter gather list is retrieved from the
571  * active array of sglq. The get of the sglq pointer also clears
572  * the entry in the array. If the status of the IO indiactes that
573  * this IO was aborted then the sglq entry it put on the
574  * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
575  * IO has good status or fails for any other reason then the sglq
576  * entry is added to the free list (lpfc_sgl_list).
577  **/
578 static void
579 __lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
580 {
581         struct lpfc_sglq *sglq;
582         size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
583         unsigned long iflag = 0;
584         struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
585
586         if (iocbq->sli4_xritag == NO_XRI)
587                 sglq = NULL;
588         else
589                 sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_xritag);
590         if (sglq)  {
591                 if ((iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) &&
592                         (sglq->state != SGL_XRI_ABORTED)) {
593                         spin_lock_irqsave(&phba->sli4_hba.abts_sgl_list_lock,
594                                         iflag);
595                         list_add(&sglq->list,
596                                 &phba->sli4_hba.lpfc_abts_els_sgl_list);
597                         spin_unlock_irqrestore(
598                                 &phba->sli4_hba.abts_sgl_list_lock, iflag);
599                 } else {
600                         sglq->state = SGL_FREED;
601                         list_add(&sglq->list, &phba->sli4_hba.lpfc_sgl_list);
602
603                         /* Check if TXQ queue needs to be serviced */
604                         if (pring->txq_cnt)
605                                 lpfc_worker_wake_up(phba);
606                 }
607         }
608
609
610         /*
611          * Clean all volatile data fields, preserve iotag and node struct.
612          */
613         memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
614         iocbq->sli4_xritag = NO_XRI;
615         list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
616 }
617
618
619 /**
620  * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
621  * @phba: Pointer to HBA context object.
622  * @iocbq: Pointer to driver iocb object.
623  *
624  * This function is called with hbalock held to release driver
625  * iocb object to the iocb pool. The iotag in the iocb object
626  * does not change for each use of the iocb object. This function
627  * clears all other fields of the iocb object when it is freed.
628  **/
629 static void
630 __lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
631 {
632         size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
633
634         /*
635          * Clean all volatile data fields, preserve iotag and node struct.
636          */
637         memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
638         iocbq->sli4_xritag = NO_XRI;
639         list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
640 }
641
642 /**
643  * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
644  * @phba: Pointer to HBA context object.
645  * @iocbq: Pointer to driver iocb object.
646  *
647  * This function is called with hbalock held to release driver
648  * iocb object to the iocb pool. The iotag in the iocb object
649  * does not change for each use of the iocb object. This function
650  * clears all other fields of the iocb object when it is freed.
651  **/
652 static void
653 __lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
654 {
655         phba->__lpfc_sli_release_iocbq(phba, iocbq);
656         phba->iocb_cnt--;
657 }
658
659 /**
660  * lpfc_sli_release_iocbq - Release iocb to the iocb pool
661  * @phba: Pointer to HBA context object.
662  * @iocbq: Pointer to driver iocb object.
663  *
664  * This function is called with no lock held to release the iocb to
665  * iocb pool.
666  **/
667 void
668 lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
669 {
670         unsigned long iflags;
671
672         /*
673          * Clean all volatile data fields, preserve iotag and node struct.
674          */
675         spin_lock_irqsave(&phba->hbalock, iflags);
676         __lpfc_sli_release_iocbq(phba, iocbq);
677         spin_unlock_irqrestore(&phba->hbalock, iflags);
678 }
679
680 /**
681  * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
682  * @phba: Pointer to HBA context object.
683  * @iocblist: List of IOCBs.
684  * @ulpstatus: ULP status in IOCB command field.
685  * @ulpWord4: ULP word-4 in IOCB command field.
686  *
687  * This function is called with a list of IOCBs to cancel. It cancels the IOCB
688  * on the list by invoking the complete callback function associated with the
689  * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
690  * fields.
691  **/
692 void
693 lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
694                       uint32_t ulpstatus, uint32_t ulpWord4)
695 {
696         struct lpfc_iocbq *piocb;
697
698         while (!list_empty(iocblist)) {
699                 list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
700
701                 if (!piocb->iocb_cmpl)
702                         lpfc_sli_release_iocbq(phba, piocb);
703                 else {
704                         piocb->iocb.ulpStatus = ulpstatus;
705                         piocb->iocb.un.ulpWord[4] = ulpWord4;
706                         (piocb->iocb_cmpl) (phba, piocb, piocb);
707                 }
708         }
709         return;
710 }
711
712 /**
713  * lpfc_sli_iocb_cmd_type - Get the iocb type
714  * @iocb_cmnd: iocb command code.
715  *
716  * This function is called by ring event handler function to get the iocb type.
717  * This function translates the iocb command to an iocb command type used to
718  * decide the final disposition of each completed IOCB.
719  * The function returns
720  * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
721  * LPFC_SOL_IOCB     if it is a solicited iocb completion
722  * LPFC_ABORT_IOCB   if it is an abort iocb
723  * LPFC_UNSOL_IOCB   if it is an unsolicited iocb
724  *
725  * The caller is not required to hold any lock.
726  **/
727 static lpfc_iocb_type
728 lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
729 {
730         lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
731
732         if (iocb_cmnd > CMD_MAX_IOCB_CMD)
733                 return 0;
734
735         switch (iocb_cmnd) {
736         case CMD_XMIT_SEQUENCE_CR:
737         case CMD_XMIT_SEQUENCE_CX:
738         case CMD_XMIT_BCAST_CN:
739         case CMD_XMIT_BCAST_CX:
740         case CMD_ELS_REQUEST_CR:
741         case CMD_ELS_REQUEST_CX:
742         case CMD_CREATE_XRI_CR:
743         case CMD_CREATE_XRI_CX:
744         case CMD_GET_RPI_CN:
745         case CMD_XMIT_ELS_RSP_CX:
746         case CMD_GET_RPI_CR:
747         case CMD_FCP_IWRITE_CR:
748         case CMD_FCP_IWRITE_CX:
749         case CMD_FCP_IREAD_CR:
750         case CMD_FCP_IREAD_CX:
751         case CMD_FCP_ICMND_CR:
752         case CMD_FCP_ICMND_CX:
753         case CMD_FCP_TSEND_CX:
754         case CMD_FCP_TRSP_CX:
755         case CMD_FCP_TRECEIVE_CX:
756         case CMD_FCP_AUTO_TRSP_CX:
757         case CMD_ADAPTER_MSG:
758         case CMD_ADAPTER_DUMP:
759         case CMD_XMIT_SEQUENCE64_CR:
760         case CMD_XMIT_SEQUENCE64_CX:
761         case CMD_XMIT_BCAST64_CN:
762         case CMD_XMIT_BCAST64_CX:
763         case CMD_ELS_REQUEST64_CR:
764         case CMD_ELS_REQUEST64_CX:
765         case CMD_FCP_IWRITE64_CR:
766         case CMD_FCP_IWRITE64_CX:
767         case CMD_FCP_IREAD64_CR:
768         case CMD_FCP_IREAD64_CX:
769         case CMD_FCP_ICMND64_CR:
770         case CMD_FCP_ICMND64_CX:
771         case CMD_FCP_TSEND64_CX:
772         case CMD_FCP_TRSP64_CX:
773         case CMD_FCP_TRECEIVE64_CX:
774         case CMD_GEN_REQUEST64_CR:
775         case CMD_GEN_REQUEST64_CX:
776         case CMD_XMIT_ELS_RSP64_CX:
777         case DSSCMD_IWRITE64_CR:
778         case DSSCMD_IWRITE64_CX:
779         case DSSCMD_IREAD64_CR:
780         case DSSCMD_IREAD64_CX:
781                 type = LPFC_SOL_IOCB;
782                 break;
783         case CMD_ABORT_XRI_CN:
784         case CMD_ABORT_XRI_CX:
785         case CMD_CLOSE_XRI_CN:
786         case CMD_CLOSE_XRI_CX:
787         case CMD_XRI_ABORTED_CX:
788         case CMD_ABORT_MXRI64_CN:
789         case CMD_XMIT_BLS_RSP64_CX:
790                 type = LPFC_ABORT_IOCB;
791                 break;
792         case CMD_RCV_SEQUENCE_CX:
793         case CMD_RCV_ELS_REQ_CX:
794         case CMD_RCV_SEQUENCE64_CX:
795         case CMD_RCV_ELS_REQ64_CX:
796         case CMD_ASYNC_STATUS:
797         case CMD_IOCB_RCV_SEQ64_CX:
798         case CMD_IOCB_RCV_ELS64_CX:
799         case CMD_IOCB_RCV_CONT64_CX:
800         case CMD_IOCB_RET_XRI64_CX:
801                 type = LPFC_UNSOL_IOCB;
802                 break;
803         case CMD_IOCB_XMIT_MSEQ64_CR:
804         case CMD_IOCB_XMIT_MSEQ64_CX:
805         case CMD_IOCB_RCV_SEQ_LIST64_CX:
806         case CMD_IOCB_RCV_ELS_LIST64_CX:
807         case CMD_IOCB_CLOSE_EXTENDED_CN:
808         case CMD_IOCB_ABORT_EXTENDED_CN:
809         case CMD_IOCB_RET_HBQE64_CN:
810         case CMD_IOCB_FCP_IBIDIR64_CR:
811         case CMD_IOCB_FCP_IBIDIR64_CX:
812         case CMD_IOCB_FCP_ITASKMGT64_CX:
813         case CMD_IOCB_LOGENTRY_CN:
814         case CMD_IOCB_LOGENTRY_ASYNC_CN:
815                 printk("%s - Unhandled SLI-3 Command x%x\n",
816                                 __func__, iocb_cmnd);
817                 type = LPFC_UNKNOWN_IOCB;
818                 break;
819         default:
820                 type = LPFC_UNKNOWN_IOCB;
821                 break;
822         }
823
824         return type;
825 }
826
827 /**
828  * lpfc_sli_ring_map - Issue config_ring mbox for all rings
829  * @phba: Pointer to HBA context object.
830  *
831  * This function is called from SLI initialization code
832  * to configure every ring of the HBA's SLI interface. The
833  * caller is not required to hold any lock. This function issues
834  * a config_ring mailbox command for each ring.
835  * This function returns zero if successful else returns a negative
836  * error code.
837  **/
838 static int
839 lpfc_sli_ring_map(struct lpfc_hba *phba)
840 {
841         struct lpfc_sli *psli = &phba->sli;
842         LPFC_MBOXQ_t *pmb;
843         MAILBOX_t *pmbox;
844         int i, rc, ret = 0;
845
846         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
847         if (!pmb)
848                 return -ENOMEM;
849         pmbox = &pmb->u.mb;
850         phba->link_state = LPFC_INIT_MBX_CMDS;
851         for (i = 0; i < psli->num_rings; i++) {
852                 lpfc_config_ring(phba, i, pmb);
853                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
854                 if (rc != MBX_SUCCESS) {
855                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
856                                         "0446 Adapter failed to init (%d), "
857                                         "mbxCmd x%x CFG_RING, mbxStatus x%x, "
858                                         "ring %d\n",
859                                         rc, pmbox->mbxCommand,
860                                         pmbox->mbxStatus, i);
861                         phba->link_state = LPFC_HBA_ERROR;
862                         ret = -ENXIO;
863                         break;
864                 }
865         }
866         mempool_free(pmb, phba->mbox_mem_pool);
867         return ret;
868 }
869
870 /**
871  * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
872  * @phba: Pointer to HBA context object.
873  * @pring: Pointer to driver SLI ring object.
874  * @piocb: Pointer to the driver iocb object.
875  *
876  * This function is called with hbalock held. The function adds the
877  * new iocb to txcmplq of the given ring. This function always returns
878  * 0. If this function is called for ELS ring, this function checks if
879  * there is a vport associated with the ELS command. This function also
880  * starts els_tmofunc timer if this is an ELS command.
881  **/
882 static int
883 lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
884                         struct lpfc_iocbq *piocb)
885 {
886         list_add_tail(&piocb->list, &pring->txcmplq);
887         piocb->iocb_flag |= LPFC_IO_ON_Q;
888         pring->txcmplq_cnt++;
889         if (pring->txcmplq_cnt > pring->txcmplq_max)
890                 pring->txcmplq_max = pring->txcmplq_cnt;
891
892         if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
893            (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
894            (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
895                 if (!piocb->vport)
896                         BUG();
897                 else
898                         mod_timer(&piocb->vport->els_tmofunc,
899                                   jiffies + HZ * (phba->fc_ratov << 1));
900         }
901
902
903         return 0;
904 }
905
906 /**
907  * lpfc_sli_ringtx_get - Get first element of the txq
908  * @phba: Pointer to HBA context object.
909  * @pring: Pointer to driver SLI ring object.
910  *
911  * This function is called with hbalock held to get next
912  * iocb in txq of the given ring. If there is any iocb in
913  * the txq, the function returns first iocb in the list after
914  * removing the iocb from the list, else it returns NULL.
915  **/
916 struct lpfc_iocbq *
917 lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
918 {
919         struct lpfc_iocbq *cmd_iocb;
920
921         list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
922         if (cmd_iocb != NULL)
923                 pring->txq_cnt--;
924         return cmd_iocb;
925 }
926
927 /**
928  * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
929  * @phba: Pointer to HBA context object.
930  * @pring: Pointer to driver SLI ring object.
931  *
932  * This function is called with hbalock held and the caller must post the
933  * iocb without releasing the lock. If the caller releases the lock,
934  * iocb slot returned by the function is not guaranteed to be available.
935  * The function returns pointer to the next available iocb slot if there
936  * is available slot in the ring, else it returns NULL.
937  * If the get index of the ring is ahead of the put index, the function
938  * will post an error attention event to the worker thread to take the
939  * HBA to offline state.
940  **/
941 static IOCB_t *
942 lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
943 {
944         struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
945         uint32_t  max_cmd_idx = pring->numCiocb;
946         if ((pring->next_cmdidx == pring->cmdidx) &&
947            (++pring->next_cmdidx >= max_cmd_idx))
948                 pring->next_cmdidx = 0;
949
950         if (unlikely(pring->local_getidx == pring->next_cmdidx)) {
951
952                 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
953
954                 if (unlikely(pring->local_getidx >= max_cmd_idx)) {
955                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
956                                         "0315 Ring %d issue: portCmdGet %d "
957                                         "is bigger than cmd ring %d\n",
958                                         pring->ringno,
959                                         pring->local_getidx, max_cmd_idx);
960
961                         phba->link_state = LPFC_HBA_ERROR;
962                         /*
963                          * All error attention handlers are posted to
964                          * worker thread
965                          */
966                         phba->work_ha |= HA_ERATT;
967                         phba->work_hs = HS_FFER3;
968
969                         lpfc_worker_wake_up(phba);
970
971                         return NULL;
972                 }
973
974                 if (pring->local_getidx == pring->next_cmdidx)
975                         return NULL;
976         }
977
978         return lpfc_cmd_iocb(phba, pring);
979 }
980
981 /**
982  * lpfc_sli_next_iotag - Get an iotag for the iocb
983  * @phba: Pointer to HBA context object.
984  * @iocbq: Pointer to driver iocb object.
985  *
986  * This function gets an iotag for the iocb. If there is no unused iotag and
987  * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
988  * array and assigns a new iotag.
989  * The function returns the allocated iotag if successful, else returns zero.
990  * Zero is not a valid iotag.
991  * The caller is not required to hold any lock.
992  **/
993 uint16_t
994 lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
995 {
996         struct lpfc_iocbq **new_arr;
997         struct lpfc_iocbq **old_arr;
998         size_t new_len;
999         struct lpfc_sli *psli = &phba->sli;
1000         uint16_t iotag;
1001
1002         spin_lock_irq(&phba->hbalock);
1003         iotag = psli->last_iotag;
1004         if(++iotag < psli->iocbq_lookup_len) {
1005                 psli->last_iotag = iotag;
1006                 psli->iocbq_lookup[iotag] = iocbq;
1007                 spin_unlock_irq(&phba->hbalock);
1008                 iocbq->iotag = iotag;
1009                 return iotag;
1010         } else if (psli->iocbq_lookup_len < (0xffff
1011                                            - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
1012                 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
1013                 spin_unlock_irq(&phba->hbalock);
1014                 new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
1015                                   GFP_KERNEL);
1016                 if (new_arr) {
1017                         spin_lock_irq(&phba->hbalock);
1018                         old_arr = psli->iocbq_lookup;
1019                         if (new_len <= psli->iocbq_lookup_len) {
1020                                 /* highly unprobable case */
1021                                 kfree(new_arr);
1022                                 iotag = psli->last_iotag;
1023                                 if(++iotag < psli->iocbq_lookup_len) {
1024                                         psli->last_iotag = iotag;
1025                                         psli->iocbq_lookup[iotag] = iocbq;
1026                                         spin_unlock_irq(&phba->hbalock);
1027                                         iocbq->iotag = iotag;
1028                                         return iotag;
1029                                 }
1030                                 spin_unlock_irq(&phba->hbalock);
1031                                 return 0;
1032                         }
1033                         if (psli->iocbq_lookup)
1034                                 memcpy(new_arr, old_arr,
1035                                        ((psli->last_iotag  + 1) *
1036                                         sizeof (struct lpfc_iocbq *)));
1037                         psli->iocbq_lookup = new_arr;
1038                         psli->iocbq_lookup_len = new_len;
1039                         psli->last_iotag = iotag;
1040                         psli->iocbq_lookup[iotag] = iocbq;
1041                         spin_unlock_irq(&phba->hbalock);
1042                         iocbq->iotag = iotag;
1043                         kfree(old_arr);
1044                         return iotag;
1045                 }
1046         } else
1047                 spin_unlock_irq(&phba->hbalock);
1048
1049         lpfc_printf_log(phba, KERN_ERR,LOG_SLI,
1050                         "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
1051                         psli->last_iotag);
1052
1053         return 0;
1054 }
1055
1056 /**
1057  * lpfc_sli_submit_iocb - Submit an iocb to the firmware
1058  * @phba: Pointer to HBA context object.
1059  * @pring: Pointer to driver SLI ring object.
1060  * @iocb: Pointer to iocb slot in the ring.
1061  * @nextiocb: Pointer to driver iocb object which need to be
1062  *            posted to firmware.
1063  *
1064  * This function is called with hbalock held to post a new iocb to
1065  * the firmware. This function copies the new iocb to ring iocb slot and
1066  * updates the ring pointers. It adds the new iocb to txcmplq if there is
1067  * a completion call back for this iocb else the function will free the
1068  * iocb object.
1069  **/
1070 static void
1071 lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1072                 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
1073 {
1074         /*
1075          * Set up an iotag
1076          */
1077         nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
1078
1079
1080         if (pring->ringno == LPFC_ELS_RING) {
1081                 lpfc_debugfs_slow_ring_trc(phba,
1082                         "IOCB cmd ring:   wd4:x%08x wd6:x%08x wd7:x%08x",
1083                         *(((uint32_t *) &nextiocb->iocb) + 4),
1084                         *(((uint32_t *) &nextiocb->iocb) + 6),
1085                         *(((uint32_t *) &nextiocb->iocb) + 7));
1086         }
1087
1088         /*
1089          * Issue iocb command to adapter
1090          */
1091         lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
1092         wmb();
1093         pring->stats.iocb_cmd++;
1094
1095         /*
1096          * If there is no completion routine to call, we can release the
1097          * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
1098          * that have no rsp ring completion, iocb_cmpl MUST be NULL.
1099          */
1100         if (nextiocb->iocb_cmpl)
1101                 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
1102         else
1103                 __lpfc_sli_release_iocbq(phba, nextiocb);
1104
1105         /*
1106          * Let the HBA know what IOCB slot will be the next one the
1107          * driver will put a command into.
1108          */
1109         pring->cmdidx = pring->next_cmdidx;
1110         writel(pring->cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
1111 }
1112
1113 /**
1114  * lpfc_sli_update_full_ring - Update the chip attention register
1115  * @phba: Pointer to HBA context object.
1116  * @pring: Pointer to driver SLI ring object.
1117  *
1118  * The caller is not required to hold any lock for calling this function.
1119  * This function updates the chip attention bits for the ring to inform firmware
1120  * that there are pending work to be done for this ring and requests an
1121  * interrupt when there is space available in the ring. This function is
1122  * called when the driver is unable to post more iocbs to the ring due
1123  * to unavailability of space in the ring.
1124  **/
1125 static void
1126 lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1127 {
1128         int ringno = pring->ringno;
1129
1130         pring->flag |= LPFC_CALL_RING_AVAILABLE;
1131
1132         wmb();
1133
1134         /*
1135          * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
1136          * The HBA will tell us when an IOCB entry is available.
1137          */
1138         writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
1139         readl(phba->CAregaddr); /* flush */
1140
1141         pring->stats.iocb_cmd_full++;
1142 }
1143
1144 /**
1145  * lpfc_sli_update_ring - Update chip attention register
1146  * @phba: Pointer to HBA context object.
1147  * @pring: Pointer to driver SLI ring object.
1148  *
1149  * This function updates the chip attention register bit for the
1150  * given ring to inform HBA that there is more work to be done
1151  * in this ring. The caller is not required to hold any lock.
1152  **/
1153 static void
1154 lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1155 {
1156         int ringno = pring->ringno;
1157
1158         /*
1159          * Tell the HBA that there is work to do in this ring.
1160          */
1161         if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
1162                 wmb();
1163                 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
1164                 readl(phba->CAregaddr); /* flush */
1165         }
1166 }
1167
1168 /**
1169  * lpfc_sli_resume_iocb - Process iocbs in the txq
1170  * @phba: Pointer to HBA context object.
1171  * @pring: Pointer to driver SLI ring object.
1172  *
1173  * This function is called with hbalock held to post pending iocbs
1174  * in the txq to the firmware. This function is called when driver
1175  * detects space available in the ring.
1176  **/
1177 static void
1178 lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1179 {
1180         IOCB_t *iocb;
1181         struct lpfc_iocbq *nextiocb;
1182
1183         /*
1184          * Check to see if:
1185          *  (a) there is anything on the txq to send
1186          *  (b) link is up
1187          *  (c) link attention events can be processed (fcp ring only)
1188          *  (d) IOCB processing is not blocked by the outstanding mbox command.
1189          */
1190         if (pring->txq_cnt &&
1191             lpfc_is_link_up(phba) &&
1192             (pring->ringno != phba->sli.fcp_ring ||
1193              phba->sli.sli_flag & LPFC_PROCESS_LA)) {
1194
1195                 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
1196                        (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
1197                         lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
1198
1199                 if (iocb)
1200                         lpfc_sli_update_ring(phba, pring);
1201                 else
1202                         lpfc_sli_update_full_ring(phba, pring);
1203         }
1204
1205         return;
1206 }
1207
1208 /**
1209  * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
1210  * @phba: Pointer to HBA context object.
1211  * @hbqno: HBQ number.
1212  *
1213  * This function is called with hbalock held to get the next
1214  * available slot for the given HBQ. If there is free slot
1215  * available for the HBQ it will return pointer to the next available
1216  * HBQ entry else it will return NULL.
1217  **/
1218 static struct lpfc_hbq_entry *
1219 lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
1220 {
1221         struct hbq_s *hbqp = &phba->hbqs[hbqno];
1222
1223         if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
1224             ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
1225                 hbqp->next_hbqPutIdx = 0;
1226
1227         if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
1228                 uint32_t raw_index = phba->hbq_get[hbqno];
1229                 uint32_t getidx = le32_to_cpu(raw_index);
1230
1231                 hbqp->local_hbqGetIdx = getidx;
1232
1233                 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
1234                         lpfc_printf_log(phba, KERN_ERR,
1235                                         LOG_SLI | LOG_VPORT,
1236                                         "1802 HBQ %d: local_hbqGetIdx "
1237                                         "%u is > than hbqp->entry_count %u\n",
1238                                         hbqno, hbqp->local_hbqGetIdx,
1239                                         hbqp->entry_count);
1240
1241                         phba->link_state = LPFC_HBA_ERROR;
1242                         return NULL;
1243                 }
1244
1245                 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
1246                         return NULL;
1247         }
1248
1249         return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
1250                         hbqp->hbqPutIdx;
1251 }
1252
1253 /**
1254  * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
1255  * @phba: Pointer to HBA context object.
1256  *
1257  * This function is called with no lock held to free all the
1258  * hbq buffers while uninitializing the SLI interface. It also
1259  * frees the HBQ buffers returned by the firmware but not yet
1260  * processed by the upper layers.
1261  **/
1262 void
1263 lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
1264 {
1265         struct lpfc_dmabuf *dmabuf, *next_dmabuf;
1266         struct hbq_dmabuf *hbq_buf;
1267         unsigned long flags;
1268         int i, hbq_count;
1269         uint32_t hbqno;
1270
1271         hbq_count = lpfc_sli_hbq_count();
1272         /* Return all memory used by all HBQs */
1273         spin_lock_irqsave(&phba->hbalock, flags);
1274         for (i = 0; i < hbq_count; ++i) {
1275                 list_for_each_entry_safe(dmabuf, next_dmabuf,
1276                                 &phba->hbqs[i].hbq_buffer_list, list) {
1277                         hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1278                         list_del(&hbq_buf->dbuf.list);
1279                         (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
1280                 }
1281                 phba->hbqs[i].buffer_count = 0;
1282         }
1283         /* Return all HBQ buffer that are in-fly */
1284         list_for_each_entry_safe(dmabuf, next_dmabuf, &phba->rb_pend_list,
1285                                  list) {
1286                 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1287                 list_del(&hbq_buf->dbuf.list);
1288                 if (hbq_buf->tag == -1) {
1289                         (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1290                                 (phba, hbq_buf);
1291                 } else {
1292                         hbqno = hbq_buf->tag >> 16;
1293                         if (hbqno >= LPFC_MAX_HBQS)
1294                                 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1295                                         (phba, hbq_buf);
1296                         else
1297                                 (phba->hbqs[hbqno].hbq_free_buffer)(phba,
1298                                         hbq_buf);
1299                 }
1300         }
1301
1302         /* Mark the HBQs not in use */
1303         phba->hbq_in_use = 0;
1304         spin_unlock_irqrestore(&phba->hbalock, flags);
1305 }
1306
1307 /**
1308  * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
1309  * @phba: Pointer to HBA context object.
1310  * @hbqno: HBQ number.
1311  * @hbq_buf: Pointer to HBQ buffer.
1312  *
1313  * This function is called with the hbalock held to post a
1314  * hbq buffer to the firmware. If the function finds an empty
1315  * slot in the HBQ, it will post the buffer. The function will return
1316  * pointer to the hbq entry if it successfully post the buffer
1317  * else it will return NULL.
1318  **/
1319 static int
1320 lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
1321                          struct hbq_dmabuf *hbq_buf)
1322 {
1323         return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
1324 }
1325
1326 /**
1327  * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
1328  * @phba: Pointer to HBA context object.
1329  * @hbqno: HBQ number.
1330  * @hbq_buf: Pointer to HBQ buffer.
1331  *
1332  * This function is called with the hbalock held to post a hbq buffer to the
1333  * firmware. If the function finds an empty slot in the HBQ, it will post the
1334  * buffer and place it on the hbq_buffer_list. The function will return zero if
1335  * it successfully post the buffer else it will return an error.
1336  **/
1337 static int
1338 lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
1339                             struct hbq_dmabuf *hbq_buf)
1340 {
1341         struct lpfc_hbq_entry *hbqe;
1342         dma_addr_t physaddr = hbq_buf->dbuf.phys;
1343
1344         /* Get next HBQ entry slot to use */
1345         hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
1346         if (hbqe) {
1347                 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1348
1349                 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1350                 hbqe->bde.addrLow  = le32_to_cpu(putPaddrLow(physaddr));
1351                 hbqe->bde.tus.f.bdeSize = hbq_buf->size;
1352                 hbqe->bde.tus.f.bdeFlags = 0;
1353                 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
1354                 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
1355                                 /* Sync SLIM */
1356                 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
1357                 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
1358                                 /* flush */
1359                 readl(phba->hbq_put + hbqno);
1360                 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
1361                 return 0;
1362         } else
1363                 return -ENOMEM;
1364 }
1365
1366 /**
1367  * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
1368  * @phba: Pointer to HBA context object.
1369  * @hbqno: HBQ number.
1370  * @hbq_buf: Pointer to HBQ buffer.
1371  *
1372  * This function is called with the hbalock held to post an RQE to the SLI4
1373  * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
1374  * the hbq_buffer_list and return zero, otherwise it will return an error.
1375  **/
1376 static int
1377 lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
1378                             struct hbq_dmabuf *hbq_buf)
1379 {
1380         int rc;
1381         struct lpfc_rqe hrqe;
1382         struct lpfc_rqe drqe;
1383
1384         hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
1385         hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
1386         drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
1387         drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
1388         rc = lpfc_sli4_rq_put(phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
1389                               &hrqe, &drqe);
1390         if (rc < 0)
1391                 return rc;
1392         hbq_buf->tag = rc;
1393         list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
1394         return 0;
1395 }
1396
1397 /* HBQ for ELS and CT traffic. */
1398 static struct lpfc_hbq_init lpfc_els_hbq = {
1399         .rn = 1,
1400         .entry_count = 256,
1401         .mask_count = 0,
1402         .profile = 0,
1403         .ring_mask = (1 << LPFC_ELS_RING),
1404         .buffer_count = 0,
1405         .init_count = 40,
1406         .add_count = 40,
1407 };
1408
1409 /* HBQ for the extra ring if needed */
1410 static struct lpfc_hbq_init lpfc_extra_hbq = {
1411         .rn = 1,
1412         .entry_count = 200,
1413         .mask_count = 0,
1414         .profile = 0,
1415         .ring_mask = (1 << LPFC_EXTRA_RING),
1416         .buffer_count = 0,
1417         .init_count = 0,
1418         .add_count = 5,
1419 };
1420
1421 /* Array of HBQs */
1422 struct lpfc_hbq_init *lpfc_hbq_defs[] = {
1423         &lpfc_els_hbq,
1424         &lpfc_extra_hbq,
1425 };
1426
1427 /**
1428  * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
1429  * @phba: Pointer to HBA context object.
1430  * @hbqno: HBQ number.
1431  * @count: Number of HBQ buffers to be posted.
1432  *
1433  * This function is called with no lock held to post more hbq buffers to the
1434  * given HBQ. The function returns the number of HBQ buffers successfully
1435  * posted.
1436  **/
1437 static int
1438 lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
1439 {
1440         uint32_t i, posted = 0;
1441         unsigned long flags;
1442         struct hbq_dmabuf *hbq_buffer;
1443         LIST_HEAD(hbq_buf_list);
1444         if (!phba->hbqs[hbqno].hbq_alloc_buffer)
1445                 return 0;
1446
1447         if ((phba->hbqs[hbqno].buffer_count + count) >
1448             lpfc_hbq_defs[hbqno]->entry_count)
1449                 count = lpfc_hbq_defs[hbqno]->entry_count -
1450                                         phba->hbqs[hbqno].buffer_count;
1451         if (!count)
1452                 return 0;
1453         /* Allocate HBQ entries */
1454         for (i = 0; i < count; i++) {
1455                 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
1456                 if (!hbq_buffer)
1457                         break;
1458                 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
1459         }
1460         /* Check whether HBQ is still in use */
1461         spin_lock_irqsave(&phba->hbalock, flags);
1462         if (!phba->hbq_in_use)
1463                 goto err;
1464         while (!list_empty(&hbq_buf_list)) {
1465                 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1466                                  dbuf.list);
1467                 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
1468                                       (hbqno << 16));
1469                 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
1470                         phba->hbqs[hbqno].buffer_count++;
1471                         posted++;
1472                 } else
1473                         (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1474         }
1475         spin_unlock_irqrestore(&phba->hbalock, flags);
1476         return posted;
1477 err:
1478         spin_unlock_irqrestore(&phba->hbalock, flags);
1479         while (!list_empty(&hbq_buf_list)) {
1480                 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1481                                  dbuf.list);
1482                 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1483         }
1484         return 0;
1485 }
1486
1487 /**
1488  * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
1489  * @phba: Pointer to HBA context object.
1490  * @qno: HBQ number.
1491  *
1492  * This function posts more buffers to the HBQ. This function
1493  * is called with no lock held. The function returns the number of HBQ entries
1494  * successfully allocated.
1495  **/
1496 int
1497 lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
1498 {
1499         if (phba->sli_rev == LPFC_SLI_REV4)
1500                 return 0;
1501         else
1502                 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1503                                          lpfc_hbq_defs[qno]->add_count);
1504 }
1505
1506 /**
1507  * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
1508  * @phba: Pointer to HBA context object.
1509  * @qno:  HBQ queue number.
1510  *
1511  * This function is called from SLI initialization code path with
1512  * no lock held to post initial HBQ buffers to firmware. The
1513  * function returns the number of HBQ entries successfully allocated.
1514  **/
1515 static int
1516 lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
1517 {
1518         if (phba->sli_rev == LPFC_SLI_REV4)
1519                 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1520                                          lpfc_hbq_defs[qno]->entry_count);
1521         else
1522                 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1523                                          lpfc_hbq_defs[qno]->init_count);
1524 }
1525
1526 /**
1527  * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
1528  * @phba: Pointer to HBA context object.
1529  * @hbqno: HBQ number.
1530  *
1531  * This function removes the first hbq buffer on an hbq list and returns a
1532  * pointer to that buffer. If it finds no buffers on the list it returns NULL.
1533  **/
1534 static struct hbq_dmabuf *
1535 lpfc_sli_hbqbuf_get(struct list_head *rb_list)
1536 {
1537         struct lpfc_dmabuf *d_buf;
1538
1539         list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
1540         if (!d_buf)
1541                 return NULL;
1542         return container_of(d_buf, struct hbq_dmabuf, dbuf);
1543 }
1544
1545 /**
1546  * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
1547  * @phba: Pointer to HBA context object.
1548  * @tag: Tag of the hbq buffer.
1549  *
1550  * This function is called with hbalock held. This function searches
1551  * for the hbq buffer associated with the given tag in the hbq buffer
1552  * list. If it finds the hbq buffer, it returns the hbq_buffer other wise
1553  * it returns NULL.
1554  **/
1555 static struct hbq_dmabuf *
1556 lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
1557 {
1558         struct lpfc_dmabuf *d_buf;
1559         struct hbq_dmabuf *hbq_buf;
1560         uint32_t hbqno;
1561
1562         hbqno = tag >> 16;
1563         if (hbqno >= LPFC_MAX_HBQS)
1564                 return NULL;
1565
1566         spin_lock_irq(&phba->hbalock);
1567         list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
1568                 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
1569                 if (hbq_buf->tag == tag) {
1570                         spin_unlock_irq(&phba->hbalock);
1571                         return hbq_buf;
1572                 }
1573         }
1574         spin_unlock_irq(&phba->hbalock);
1575         lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
1576                         "1803 Bad hbq tag. Data: x%x x%x\n",
1577                         tag, phba->hbqs[tag >> 16].buffer_count);
1578         return NULL;
1579 }
1580
1581 /**
1582  * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
1583  * @phba: Pointer to HBA context object.
1584  * @hbq_buffer: Pointer to HBQ buffer.
1585  *
1586  * This function is called with hbalock. This function gives back
1587  * the hbq buffer to firmware. If the HBQ does not have space to
1588  * post the buffer, it will free the buffer.
1589  **/
1590 void
1591 lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
1592 {
1593         uint32_t hbqno;
1594
1595         if (hbq_buffer) {
1596                 hbqno = hbq_buffer->tag >> 16;
1597                 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
1598                         (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1599         }
1600 }
1601
1602 /**
1603  * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
1604  * @mbxCommand: mailbox command code.
1605  *
1606  * This function is called by the mailbox event handler function to verify
1607  * that the completed mailbox command is a legitimate mailbox command. If the
1608  * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
1609  * and the mailbox event handler will take the HBA offline.
1610  **/
1611 static int
1612 lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
1613 {
1614         uint8_t ret;
1615
1616         switch (mbxCommand) {
1617         case MBX_LOAD_SM:
1618         case MBX_READ_NV:
1619         case MBX_WRITE_NV:
1620         case MBX_WRITE_VPARMS:
1621         case MBX_RUN_BIU_DIAG:
1622         case MBX_INIT_LINK:
1623         case MBX_DOWN_LINK:
1624         case MBX_CONFIG_LINK:
1625         case MBX_CONFIG_RING:
1626         case MBX_RESET_RING:
1627         case MBX_READ_CONFIG:
1628         case MBX_READ_RCONFIG:
1629         case MBX_READ_SPARM:
1630         case MBX_READ_STATUS:
1631         case MBX_READ_RPI:
1632         case MBX_READ_XRI:
1633         case MBX_READ_REV:
1634         case MBX_READ_LNK_STAT:
1635         case MBX_REG_LOGIN:
1636         case MBX_UNREG_LOGIN:
1637         case MBX_READ_LA:
1638         case MBX_CLEAR_LA:
1639         case MBX_DUMP_MEMORY:
1640         case MBX_DUMP_CONTEXT:
1641         case MBX_RUN_DIAGS:
1642         case MBX_RESTART:
1643         case MBX_UPDATE_CFG:
1644         case MBX_DOWN_LOAD:
1645         case MBX_DEL_LD_ENTRY:
1646         case MBX_RUN_PROGRAM:
1647         case MBX_SET_MASK:
1648         case MBX_SET_VARIABLE:
1649         case MBX_UNREG_D_ID:
1650         case MBX_KILL_BOARD:
1651         case MBX_CONFIG_FARP:
1652         case MBX_BEACON:
1653         case MBX_LOAD_AREA:
1654         case MBX_RUN_BIU_DIAG64:
1655         case MBX_CONFIG_PORT:
1656         case MBX_READ_SPARM64:
1657         case MBX_READ_RPI64:
1658         case MBX_REG_LOGIN64:
1659         case MBX_READ_LA64:
1660         case MBX_WRITE_WWN:
1661         case MBX_SET_DEBUG:
1662         case MBX_LOAD_EXP_ROM:
1663         case MBX_ASYNCEVT_ENABLE:
1664         case MBX_REG_VPI:
1665         case MBX_UNREG_VPI:
1666         case MBX_HEARTBEAT:
1667         case MBX_PORT_CAPABILITIES:
1668         case MBX_PORT_IOV_CONTROL:
1669         case MBX_SLI4_CONFIG:
1670         case MBX_SLI4_REQ_FTRS:
1671         case MBX_REG_FCFI:
1672         case MBX_UNREG_FCFI:
1673         case MBX_REG_VFI:
1674         case MBX_UNREG_VFI:
1675         case MBX_INIT_VPI:
1676         case MBX_INIT_VFI:
1677         case MBX_RESUME_RPI:
1678         case MBX_READ_EVENT_LOG_STATUS:
1679         case MBX_READ_EVENT_LOG:
1680                 ret = mbxCommand;
1681                 break;
1682         default:
1683                 ret = MBX_SHUTDOWN;
1684                 break;
1685         }
1686         return ret;
1687 }
1688
1689 /**
1690  * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
1691  * @phba: Pointer to HBA context object.
1692  * @pmboxq: Pointer to mailbox command.
1693  *
1694  * This is completion handler function for mailbox commands issued from
1695  * lpfc_sli_issue_mbox_wait function. This function is called by the
1696  * mailbox event handler function with no lock held. This function
1697  * will wake up thread waiting on the wait queue pointed by context1
1698  * of the mailbox.
1699  **/
1700 void
1701 lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
1702 {
1703         wait_queue_head_t *pdone_q;
1704         unsigned long drvr_flag;
1705
1706         /*
1707          * If pdone_q is empty, the driver thread gave up waiting and
1708          * continued running.
1709          */
1710         pmboxq->mbox_flag |= LPFC_MBX_WAKE;
1711         spin_lock_irqsave(&phba->hbalock, drvr_flag);
1712         pdone_q = (wait_queue_head_t *) pmboxq->context1;
1713         if (pdone_q)
1714                 wake_up_interruptible(pdone_q);
1715         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
1716         return;
1717 }
1718
1719
1720 /**
1721  * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
1722  * @phba: Pointer to HBA context object.
1723  * @pmb: Pointer to mailbox object.
1724  *
1725  * This function is the default mailbox completion handler. It
1726  * frees the memory resources associated with the completed mailbox
1727  * command. If the completed command is a REG_LOGIN mailbox command,
1728  * this function will issue a UREG_LOGIN to re-claim the RPI.
1729  **/
1730 void
1731 lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
1732 {
1733         struct lpfc_dmabuf *mp;
1734         uint16_t rpi, vpi;
1735         int rc;
1736         struct lpfc_vport  *vport = pmb->vport;
1737
1738         mp = (struct lpfc_dmabuf *) (pmb->context1);
1739
1740         if (mp) {
1741                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1742                 kfree(mp);
1743         }
1744
1745         if ((pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) &&
1746             (phba->sli_rev == LPFC_SLI_REV4))
1747                 lpfc_sli4_free_rpi(phba, pmb->u.mb.un.varUnregLogin.rpi);
1748
1749         /*
1750          * If a REG_LOGIN succeeded  after node is destroyed or node
1751          * is in re-discovery driver need to cleanup the RPI.
1752          */
1753         if (!(phba->pport->load_flag & FC_UNLOADING) &&
1754             pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
1755             !pmb->u.mb.mbxStatus) {
1756                 rpi = pmb->u.mb.un.varWords[0];
1757                 vpi = pmb->u.mb.un.varRegLogin.vpi - phba->vpi_base;
1758                 lpfc_unreg_login(phba, vpi, rpi, pmb);
1759                 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
1760                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
1761                 if (rc != MBX_NOT_FINISHED)
1762                         return;
1763         }
1764
1765         /* Unreg VPI, if the REG_VPI succeed after VLink failure */
1766         if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
1767                 !(phba->pport->load_flag & FC_UNLOADING) &&
1768                 !pmb->u.mb.mbxStatus) {
1769                 lpfc_unreg_vpi(phba, pmb->u.mb.un.varRegVpi.vpi, pmb);
1770                 pmb->vport = vport;
1771                 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
1772                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
1773                 if (rc != MBX_NOT_FINISHED)
1774                         return;
1775         }
1776
1777         if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
1778                 lpfc_sli4_mbox_cmd_free(phba, pmb);
1779         else
1780                 mempool_free(pmb, phba->mbox_mem_pool);
1781 }
1782
1783 /**
1784  * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
1785  * @phba: Pointer to HBA context object.
1786  *
1787  * This function is called with no lock held. This function processes all
1788  * the completed mailbox commands and gives it to upper layers. The interrupt
1789  * service routine processes mailbox completion interrupt and adds completed
1790  * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
1791  * Worker thread call lpfc_sli_handle_mb_event, which will return the
1792  * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
1793  * function returns the mailbox commands to the upper layer by calling the
1794  * completion handler function of each mailbox.
1795  **/
1796 int
1797 lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
1798 {
1799         MAILBOX_t *pmbox;
1800         LPFC_MBOXQ_t *pmb;
1801         int rc;
1802         LIST_HEAD(cmplq);
1803
1804         phba->sli.slistat.mbox_event++;
1805
1806         /* Get all completed mailboxe buffers into the cmplq */
1807         spin_lock_irq(&phba->hbalock);
1808         list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
1809         spin_unlock_irq(&phba->hbalock);
1810
1811         /* Get a Mailbox buffer to setup mailbox commands for callback */
1812         do {
1813                 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
1814                 if (pmb == NULL)
1815                         break;
1816
1817                 pmbox = &pmb->u.mb;
1818
1819                 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
1820                         if (pmb->vport) {
1821                                 lpfc_debugfs_disc_trc(pmb->vport,
1822                                         LPFC_DISC_TRC_MBOX_VPORT,
1823                                         "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
1824                                         (uint32_t)pmbox->mbxCommand,
1825                                         pmbox->un.varWords[0],
1826                                         pmbox->un.varWords[1]);
1827                         }
1828                         else {
1829                                 lpfc_debugfs_disc_trc(phba->pport,
1830                                         LPFC_DISC_TRC_MBOX,
1831                                         "MBOX cmpl:       cmd:x%x mb:x%x x%x",
1832                                         (uint32_t)pmbox->mbxCommand,
1833                                         pmbox->un.varWords[0],
1834                                         pmbox->un.varWords[1]);
1835                         }
1836                 }
1837
1838                 /*
1839                  * It is a fatal error if unknown mbox command completion.
1840                  */
1841                 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
1842                     MBX_SHUTDOWN) {
1843                         /* Unknown mailbox command compl */
1844                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
1845                                         "(%d):0323 Unknown Mailbox command "
1846                                         "x%x (x%x) Cmpl\n",
1847                                         pmb->vport ? pmb->vport->vpi : 0,
1848                                         pmbox->mbxCommand,
1849                                         lpfc_sli4_mbox_opcode_get(phba, pmb));
1850                         phba->link_state = LPFC_HBA_ERROR;
1851                         phba->work_hs = HS_FFER3;
1852                         lpfc_handle_eratt(phba);
1853                         continue;
1854                 }
1855
1856                 if (pmbox->mbxStatus) {
1857                         phba->sli.slistat.mbox_stat_err++;
1858                         if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
1859                                 /* Mbox cmd cmpl error - RETRYing */
1860                                 lpfc_printf_log(phba, KERN_INFO,
1861                                                 LOG_MBOX | LOG_SLI,
1862                                                 "(%d):0305 Mbox cmd cmpl "
1863                                                 "error - RETRYing Data: x%x "
1864                                                 "(x%x) x%x x%x x%x\n",
1865                                                 pmb->vport ? pmb->vport->vpi :0,
1866                                                 pmbox->mbxCommand,
1867                                                 lpfc_sli4_mbox_opcode_get(phba,
1868                                                                           pmb),
1869                                                 pmbox->mbxStatus,
1870                                                 pmbox->un.varWords[0],
1871                                                 pmb->vport->port_state);
1872                                 pmbox->mbxStatus = 0;
1873                                 pmbox->mbxOwner = OWN_HOST;
1874                                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
1875                                 if (rc != MBX_NOT_FINISHED)
1876                                         continue;
1877                         }
1878                 }
1879
1880                 /* Mailbox cmd <cmd> Cmpl <cmpl> */
1881                 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
1882                                 "(%d):0307 Mailbox cmd x%x (x%x) Cmpl x%p "
1883                                 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x\n",
1884                                 pmb->vport ? pmb->vport->vpi : 0,
1885                                 pmbox->mbxCommand,
1886                                 lpfc_sli4_mbox_opcode_get(phba, pmb),
1887                                 pmb->mbox_cmpl,
1888                                 *((uint32_t *) pmbox),
1889                                 pmbox->un.varWords[0],
1890                                 pmbox->un.varWords[1],
1891                                 pmbox->un.varWords[2],
1892                                 pmbox->un.varWords[3],
1893                                 pmbox->un.varWords[4],
1894                                 pmbox->un.varWords[5],
1895                                 pmbox->un.varWords[6],
1896                                 pmbox->un.varWords[7]);
1897
1898                 if (pmb->mbox_cmpl)
1899                         pmb->mbox_cmpl(phba,pmb);
1900         } while (1);
1901         return 0;
1902 }
1903
1904 /**
1905  * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
1906  * @phba: Pointer to HBA context object.
1907  * @pring: Pointer to driver SLI ring object.
1908  * @tag: buffer tag.
1909  *
1910  * This function is called with no lock held. When QUE_BUFTAG_BIT bit
1911  * is set in the tag the buffer is posted for a particular exchange,
1912  * the function will return the buffer without replacing the buffer.
1913  * If the buffer is for unsolicited ELS or CT traffic, this function
1914  * returns the buffer and also posts another buffer to the firmware.
1915  **/
1916 static struct lpfc_dmabuf *
1917 lpfc_sli_get_buff(struct lpfc_hba *phba,
1918                   struct lpfc_sli_ring *pring,
1919                   uint32_t tag)
1920 {
1921         struct hbq_dmabuf *hbq_entry;
1922
1923         if (tag & QUE_BUFTAG_BIT)
1924                 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
1925         hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
1926         if (!hbq_entry)
1927                 return NULL;
1928         return &hbq_entry->dbuf;
1929 }
1930
1931 /**
1932  * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
1933  * @phba: Pointer to HBA context object.
1934  * @pring: Pointer to driver SLI ring object.
1935  * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
1936  * @fch_r_ctl: the r_ctl for the first frame of the sequence.
1937  * @fch_type: the type for the first frame of the sequence.
1938  *
1939  * This function is called with no lock held. This function uses the r_ctl and
1940  * type of the received sequence to find the correct callback function to call
1941  * to process the sequence.
1942  **/
1943 static int
1944 lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1945                          struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
1946                          uint32_t fch_type)
1947 {
1948         int i;
1949
1950         /* unSolicited Responses */
1951         if (pring->prt[0].profile) {
1952                 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
1953                         (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
1954                                                                         saveq);
1955                 return 1;
1956         }
1957         /* We must search, based on rctl / type
1958            for the right routine */
1959         for (i = 0; i < pring->num_mask; i++) {
1960                 if ((pring->prt[i].rctl == fch_r_ctl) &&
1961                     (pring->prt[i].type == fch_type)) {
1962                         if (pring->prt[i].lpfc_sli_rcv_unsol_event)
1963                                 (pring->prt[i].lpfc_sli_rcv_unsol_event)
1964                                                 (phba, pring, saveq);
1965                         return 1;
1966                 }
1967         }
1968         return 0;
1969 }
1970
1971 /**
1972  * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
1973  * @phba: Pointer to HBA context object.
1974  * @pring: Pointer to driver SLI ring object.
1975  * @saveq: Pointer to the unsolicited iocb.
1976  *
1977  * This function is called with no lock held by the ring event handler
1978  * when there is an unsolicited iocb posted to the response ring by the
1979  * firmware. This function gets the buffer associated with the iocbs
1980  * and calls the event handler for the ring. This function handles both
1981  * qring buffers and hbq buffers.
1982  * When the function returns 1 the caller can free the iocb object otherwise
1983  * upper layer functions will free the iocb objects.
1984  **/
1985 static int
1986 lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1987                             struct lpfc_iocbq *saveq)
1988 {
1989         IOCB_t           * irsp;
1990         WORD5            * w5p;
1991         uint32_t           Rctl, Type;
1992         uint32_t           match;
1993         struct lpfc_iocbq *iocbq;
1994         struct lpfc_dmabuf *dmzbuf;
1995
1996         match = 0;
1997         irsp = &(saveq->iocb);
1998
1999         if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
2000                 if (pring->lpfc_sli_rcv_async_status)
2001                         pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
2002                 else
2003                         lpfc_printf_log(phba,
2004                                         KERN_WARNING,
2005                                         LOG_SLI,
2006                                         "0316 Ring %d handler: unexpected "
2007                                         "ASYNC_STATUS iocb received evt_code "
2008                                         "0x%x\n",
2009                                         pring->ringno,
2010                                         irsp->un.asyncstat.evt_code);
2011                 return 1;
2012         }
2013
2014         if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
2015                 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
2016                 if (irsp->ulpBdeCount > 0) {
2017                         dmzbuf = lpfc_sli_get_buff(phba, pring,
2018                                         irsp->un.ulpWord[3]);
2019                         lpfc_in_buf_free(phba, dmzbuf);
2020                 }
2021
2022                 if (irsp->ulpBdeCount > 1) {
2023                         dmzbuf = lpfc_sli_get_buff(phba, pring,
2024                                         irsp->unsli3.sli3Words[3]);
2025                         lpfc_in_buf_free(phba, dmzbuf);
2026                 }
2027
2028                 if (irsp->ulpBdeCount > 2) {
2029                         dmzbuf = lpfc_sli_get_buff(phba, pring,
2030                                 irsp->unsli3.sli3Words[7]);
2031                         lpfc_in_buf_free(phba, dmzbuf);
2032                 }
2033
2034                 return 1;
2035         }
2036
2037         if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
2038                 if (irsp->ulpBdeCount != 0) {
2039                         saveq->context2 = lpfc_sli_get_buff(phba, pring,
2040                                                 irsp->un.ulpWord[3]);
2041                         if (!saveq->context2)
2042                                 lpfc_printf_log(phba,
2043                                         KERN_ERR,
2044                                         LOG_SLI,
2045                                         "0341 Ring %d Cannot find buffer for "
2046                                         "an unsolicited iocb. tag 0x%x\n",
2047                                         pring->ringno,
2048                                         irsp->un.ulpWord[3]);
2049                 }
2050                 if (irsp->ulpBdeCount == 2) {
2051                         saveq->context3 = lpfc_sli_get_buff(phba, pring,
2052                                                 irsp->unsli3.sli3Words[7]);
2053                         if (!saveq->context3)
2054                                 lpfc_printf_log(phba,
2055                                         KERN_ERR,
2056                                         LOG_SLI,
2057                                         "0342 Ring %d Cannot find buffer for an"
2058                                         " unsolicited iocb. tag 0x%x\n",
2059                                         pring->ringno,
2060                                         irsp->unsli3.sli3Words[7]);
2061                 }
2062                 list_for_each_entry(iocbq, &saveq->list, list) {
2063                         irsp = &(iocbq->iocb);
2064                         if (irsp->ulpBdeCount != 0) {
2065                                 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
2066                                                         irsp->un.ulpWord[3]);
2067                                 if (!iocbq->context2)
2068                                         lpfc_printf_log(phba,
2069                                                 KERN_ERR,
2070                                                 LOG_SLI,
2071                                                 "0343 Ring %d Cannot find "
2072                                                 "buffer for an unsolicited iocb"
2073                                                 ". tag 0x%x\n", pring->ringno,
2074                                                 irsp->un.ulpWord[3]);
2075                         }
2076                         if (irsp->ulpBdeCount == 2) {
2077                                 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
2078                                                 irsp->unsli3.sli3Words[7]);
2079                                 if (!iocbq->context3)
2080                                         lpfc_printf_log(phba,
2081                                                 KERN_ERR,
2082                                                 LOG_SLI,
2083                                                 "0344 Ring %d Cannot find "
2084                                                 "buffer for an unsolicited "
2085                                                 "iocb. tag 0x%x\n",
2086                                                 pring->ringno,
2087                                                 irsp->unsli3.sli3Words[7]);
2088                         }
2089                 }
2090         }
2091         if (irsp->ulpBdeCount != 0 &&
2092             (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
2093              irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
2094                 int found = 0;
2095
2096                 /* search continue save q for same XRI */
2097                 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
2098                         if (iocbq->iocb.ulpContext == saveq->iocb.ulpContext) {
2099                                 list_add_tail(&saveq->list, &iocbq->list);
2100                                 found = 1;
2101                                 break;
2102                         }
2103                 }
2104                 if (!found)
2105                         list_add_tail(&saveq->clist,
2106                                       &pring->iocb_continue_saveq);
2107                 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
2108                         list_del_init(&iocbq->clist);
2109                         saveq = iocbq;
2110                         irsp = &(saveq->iocb);
2111                 } else
2112                         return 0;
2113         }
2114         if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
2115             (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
2116             (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
2117                 Rctl = FC_RCTL_ELS_REQ;
2118                 Type = FC_TYPE_ELS;
2119         } else {
2120                 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
2121                 Rctl = w5p->hcsw.Rctl;
2122                 Type = w5p->hcsw.Type;
2123
2124                 /* Firmware Workaround */
2125                 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
2126                         (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
2127                          irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
2128                         Rctl = FC_RCTL_ELS_REQ;
2129                         Type = FC_TYPE_ELS;
2130                         w5p->hcsw.Rctl = Rctl;
2131                         w5p->hcsw.Type = Type;
2132                 }
2133         }
2134
2135         if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
2136                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
2137                                 "0313 Ring %d handler: unexpected Rctl x%x "
2138                                 "Type x%x received\n",
2139                                 pring->ringno, Rctl, Type);
2140
2141         return 1;
2142 }
2143
2144 /**
2145  * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
2146  * @phba: Pointer to HBA context object.
2147  * @pring: Pointer to driver SLI ring object.
2148  * @prspiocb: Pointer to response iocb object.
2149  *
2150  * This function looks up the iocb_lookup table to get the command iocb
2151  * corresponding to the given response iocb using the iotag of the
2152  * response iocb. This function is called with the hbalock held.
2153  * This function returns the command iocb object if it finds the command
2154  * iocb else returns NULL.
2155  **/
2156 static struct lpfc_iocbq *
2157 lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
2158                       struct lpfc_sli_ring *pring,
2159                       struct lpfc_iocbq *prspiocb)
2160 {
2161         struct lpfc_iocbq *cmd_iocb = NULL;
2162         uint16_t iotag;
2163
2164         iotag = prspiocb->iocb.ulpIoTag;
2165
2166         if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2167                 cmd_iocb = phba->sli.iocbq_lookup[iotag];
2168                 list_del_init(&cmd_iocb->list);
2169                 if (cmd_iocb->iocb_flag & LPFC_IO_ON_Q) {
2170                         pring->txcmplq_cnt--;
2171                         cmd_iocb->iocb_flag &= ~LPFC_IO_ON_Q;
2172                 }
2173                 return cmd_iocb;
2174         }
2175
2176         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2177                         "0317 iotag x%x is out off "
2178                         "range: max iotag x%x wd0 x%x\n",
2179                         iotag, phba->sli.last_iotag,
2180                         *(((uint32_t *) &prspiocb->iocb) + 7));
2181         return NULL;
2182 }
2183
2184 /**
2185  * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
2186  * @phba: Pointer to HBA context object.
2187  * @pring: Pointer to driver SLI ring object.
2188  * @iotag: IOCB tag.
2189  *
2190  * This function looks up the iocb_lookup table to get the command iocb
2191  * corresponding to the given iotag. This function is called with the
2192  * hbalock held.
2193  * This function returns the command iocb object if it finds the command
2194  * iocb else returns NULL.
2195  **/
2196 static struct lpfc_iocbq *
2197 lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
2198                              struct lpfc_sli_ring *pring, uint16_t iotag)
2199 {
2200         struct lpfc_iocbq *cmd_iocb;
2201
2202         if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2203                 cmd_iocb = phba->sli.iocbq_lookup[iotag];
2204                 list_del_init(&cmd_iocb->list);
2205                 if (cmd_iocb->iocb_flag & LPFC_IO_ON_Q) {
2206                         cmd_iocb->iocb_flag &= ~LPFC_IO_ON_Q;
2207                         pring->txcmplq_cnt--;
2208                 }
2209                 return cmd_iocb;
2210         }
2211
2212         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2213                         "0372 iotag x%x is out off range: max iotag (x%x)\n",
2214                         iotag, phba->sli.last_iotag);
2215         return NULL;
2216 }
2217
2218 /**
2219  * lpfc_sli_process_sol_iocb - process solicited iocb completion
2220  * @phba: Pointer to HBA context object.
2221  * @pring: Pointer to driver SLI ring object.
2222  * @saveq: Pointer to the response iocb to be processed.
2223  *
2224  * This function is called by the ring event handler for non-fcp
2225  * rings when there is a new response iocb in the response ring.
2226  * The caller is not required to hold any locks. This function
2227  * gets the command iocb associated with the response iocb and
2228  * calls the completion handler for the command iocb. If there
2229  * is no completion handler, the function will free the resources
2230  * associated with command iocb. If the response iocb is for
2231  * an already aborted command iocb, the status of the completion
2232  * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
2233  * This function always returns 1.
2234  **/
2235 static int
2236 lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2237                           struct lpfc_iocbq *saveq)
2238 {
2239         struct lpfc_iocbq *cmdiocbp;
2240         int rc = 1;
2241         unsigned long iflag;
2242
2243         /* Based on the iotag field, get the cmd IOCB from the txcmplq */
2244         spin_lock_irqsave(&phba->hbalock, iflag);
2245         cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
2246         spin_unlock_irqrestore(&phba->hbalock, iflag);
2247
2248         if (cmdiocbp) {
2249                 if (cmdiocbp->iocb_cmpl) {
2250                         /*
2251                          * If an ELS command failed send an event to mgmt
2252                          * application.
2253                          */
2254                         if (saveq->iocb.ulpStatus &&
2255                              (pring->ringno == LPFC_ELS_RING) &&
2256                              (cmdiocbp->iocb.ulpCommand ==
2257                                 CMD_ELS_REQUEST64_CR))
2258                                 lpfc_send_els_failure_event(phba,
2259                                         cmdiocbp, saveq);
2260
2261                         /*
2262                          * Post all ELS completions to the worker thread.
2263                          * All other are passed to the completion callback.
2264                          */
2265                         if (pring->ringno == LPFC_ELS_RING) {
2266                                 if ((phba->sli_rev < LPFC_SLI_REV4) &&
2267                                     (cmdiocbp->iocb_flag &
2268                                                         LPFC_DRIVER_ABORTED)) {
2269                                         spin_lock_irqsave(&phba->hbalock,
2270                                                           iflag);
2271                                         cmdiocbp->iocb_flag &=
2272                                                 ~LPFC_DRIVER_ABORTED;
2273                                         spin_unlock_irqrestore(&phba->hbalock,
2274                                                                iflag);
2275                                         saveq->iocb.ulpStatus =
2276                                                 IOSTAT_LOCAL_REJECT;
2277                                         saveq->iocb.un.ulpWord[4] =
2278                                                 IOERR_SLI_ABORTED;
2279
2280                                         /* Firmware could still be in progress
2281                                          * of DMAing payload, so don't free data
2282                                          * buffer till after a hbeat.
2283                                          */
2284                                         spin_lock_irqsave(&phba->hbalock,
2285                                                           iflag);
2286                                         saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
2287                                         spin_unlock_irqrestore(&phba->hbalock,
2288                                                                iflag);
2289                                 }
2290                                 if (phba->sli_rev == LPFC_SLI_REV4) {
2291                                         if (saveq->iocb_flag &
2292                                             LPFC_EXCHANGE_BUSY) {
2293                                                 /* Set cmdiocb flag for the
2294                                                  * exchange busy so sgl (xri)
2295                                                  * will not be released until
2296                                                  * the abort xri is received
2297                                                  * from hba.
2298                                                  */
2299                                                 spin_lock_irqsave(
2300                                                         &phba->hbalock, iflag);
2301                                                 cmdiocbp->iocb_flag |=
2302                                                         LPFC_EXCHANGE_BUSY;
2303                                                 spin_unlock_irqrestore(
2304                                                         &phba->hbalock, iflag);
2305                                         }
2306                                         if (cmdiocbp->iocb_flag &
2307                                             LPFC_DRIVER_ABORTED) {
2308                                                 /*
2309                                                  * Clear LPFC_DRIVER_ABORTED
2310                                                  * bit in case it was driver
2311                                                  * initiated abort.
2312                                                  */
2313                                                 spin_lock_irqsave(
2314                                                         &phba->hbalock, iflag);
2315                                                 cmdiocbp->iocb_flag &=
2316                                                         ~LPFC_DRIVER_ABORTED;
2317                                                 spin_unlock_irqrestore(
2318                                                         &phba->hbalock, iflag);
2319                                                 cmdiocbp->iocb.ulpStatus =
2320                                                         IOSTAT_LOCAL_REJECT;
2321                                                 cmdiocbp->iocb.un.ulpWord[4] =
2322                                                         IOERR_ABORT_REQUESTED;
2323                                                 /*
2324                                                  * For SLI4, irsiocb contains
2325                                                  * NO_XRI in sli_xritag, it
2326                                                  * shall not affect releasing
2327                                                  * sgl (xri) process.
2328                                                  */
2329                                                 saveq->iocb.ulpStatus =
2330                                                         IOSTAT_LOCAL_REJECT;
2331                                                 saveq->iocb.un.ulpWord[4] =
2332                                                         IOERR_SLI_ABORTED;
2333                                                 spin_lock_irqsave(
2334                                                         &phba->hbalock, iflag);
2335                                                 saveq->iocb_flag |=
2336                                                         LPFC_DELAY_MEM_FREE;
2337                                                 spin_unlock_irqrestore(
2338                                                         &phba->hbalock, iflag);
2339                                         }
2340                                 }
2341                         }
2342                         (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
2343                 } else
2344                         lpfc_sli_release_iocbq(phba, cmdiocbp);
2345         } else {
2346                 /*
2347                  * Unknown initiating command based on the response iotag.
2348                  * This could be the case on the ELS ring because of
2349                  * lpfc_els_abort().
2350                  */
2351                 if (pring->ringno != LPFC_ELS_RING) {
2352                         /*
2353                          * Ring <ringno> handler: unexpected completion IoTag
2354                          * <IoTag>
2355                          */
2356                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
2357                                          "0322 Ring %d handler: "
2358                                          "unexpected completion IoTag x%x "
2359                                          "Data: x%x x%x x%x x%x\n",
2360                                          pring->ringno,
2361                                          saveq->iocb.ulpIoTag,
2362                                          saveq->iocb.ulpStatus,
2363                                          saveq->iocb.un.ulpWord[4],
2364                                          saveq->iocb.ulpCommand,
2365                                          saveq->iocb.ulpContext);
2366                 }
2367         }
2368
2369         return rc;
2370 }
2371
2372 /**
2373  * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
2374  * @phba: Pointer to HBA context object.
2375  * @pring: Pointer to driver SLI ring object.
2376  *
2377  * This function is called from the iocb ring event handlers when
2378  * put pointer is ahead of the get pointer for a ring. This function signal
2379  * an error attention condition to the worker thread and the worker
2380  * thread will transition the HBA to offline state.
2381  **/
2382 static void
2383 lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
2384 {
2385         struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
2386         /*
2387          * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
2388          * rsp ring <portRspMax>
2389          */
2390         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2391                         "0312 Ring %d handler: portRspPut %d "
2392                         "is bigger than rsp ring %d\n",
2393                         pring->ringno, le32_to_cpu(pgp->rspPutInx),
2394                         pring->numRiocb);
2395
2396         phba->link_state = LPFC_HBA_ERROR;
2397
2398         /*
2399          * All error attention handlers are posted to
2400          * worker thread
2401          */
2402         phba->work_ha |= HA_ERATT;
2403         phba->work_hs = HS_FFER3;
2404
2405         lpfc_worker_wake_up(phba);
2406
2407         return;
2408 }
2409
2410 /**
2411  * lpfc_poll_eratt - Error attention polling timer timeout handler
2412  * @ptr: Pointer to address of HBA context object.
2413  *
2414  * This function is invoked by the Error Attention polling timer when the
2415  * timer times out. It will check the SLI Error Attention register for
2416  * possible attention events. If so, it will post an Error Attention event
2417  * and wake up worker thread to process it. Otherwise, it will set up the
2418  * Error Attention polling timer for the next poll.
2419  **/
2420 void lpfc_poll_eratt(unsigned long ptr)
2421 {
2422         struct lpfc_hba *phba;
2423         uint32_t eratt = 0;
2424
2425         phba = (struct lpfc_hba *)ptr;
2426
2427         /* Check chip HA register for error event */
2428         eratt = lpfc_sli_check_eratt(phba);
2429
2430         if (eratt)
2431                 /* Tell the worker thread there is work to do */
2432                 lpfc_worker_wake_up(phba);
2433         else
2434                 /* Restart the timer for next eratt poll */
2435                 mod_timer(&phba->eratt_poll, jiffies +
2436                                         HZ * LPFC_ERATT_POLL_INTERVAL);
2437         return;
2438 }
2439
2440
2441 /**
2442  * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
2443  * @phba: Pointer to HBA context object.
2444  * @pring: Pointer to driver SLI ring object.
2445  * @mask: Host attention register mask for this ring.
2446  *
2447  * This function is called from the interrupt context when there is a ring
2448  * event for the fcp ring. The caller does not hold any lock.
2449  * The function processes each response iocb in the response ring until it
2450  * finds an iocb with LE bit set and chains all the iocbs upto the iocb with
2451  * LE bit set. The function will call the completion handler of the command iocb
2452  * if the response iocb indicates a completion for a command iocb or it is
2453  * an abort completion. The function will call lpfc_sli_process_unsol_iocb
2454  * function if this is an unsolicited iocb.
2455  * This routine presumes LPFC_FCP_RING handling and doesn't bother
2456  * to check it explicitly.
2457  */
2458 int
2459 lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
2460                                 struct lpfc_sli_ring *pring, uint32_t mask)
2461 {
2462         struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
2463         IOCB_t *irsp = NULL;
2464         IOCB_t *entry = NULL;
2465         struct lpfc_iocbq *cmdiocbq = NULL;
2466         struct lpfc_iocbq rspiocbq;
2467         uint32_t status;
2468         uint32_t portRspPut, portRspMax;
2469         int rc = 1;
2470         lpfc_iocb_type type;
2471         unsigned long iflag;
2472         uint32_t rsp_cmpl = 0;
2473
2474         spin_lock_irqsave(&phba->hbalock, iflag);
2475         pring->stats.iocb_event++;
2476
2477         /*
2478          * The next available response entry should never exceed the maximum
2479          * entries.  If it does, treat it as an adapter hardware error.
2480          */
2481         portRspMax = pring->numRiocb;
2482         portRspPut = le32_to_cpu(pgp->rspPutInx);
2483         if (unlikely(portRspPut >= portRspMax)) {
2484                 lpfc_sli_rsp_pointers_error(phba, pring);
2485                 spin_unlock_irqrestore(&phba->hbalock, iflag);
2486                 return 1;
2487         }
2488         if (phba->fcp_ring_in_use) {
2489                 spin_unlock_irqrestore(&phba->hbalock, iflag);
2490                 return 1;
2491         } else
2492                 phba->fcp_ring_in_use = 1;
2493
2494         rmb();
2495         while (pring->rspidx != portRspPut) {
2496                 /*
2497                  * Fetch an entry off the ring and copy it into a local data
2498                  * structure.  The copy involves a byte-swap since the
2499                  * network byte order and pci byte orders are different.
2500                  */
2501                 entry = lpfc_resp_iocb(phba, pring);
2502                 phba->last_completion_time = jiffies;
2503
2504                 if (++pring->rspidx >= portRspMax)
2505                         pring->rspidx = 0;
2506
2507                 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
2508                                       (uint32_t *) &rspiocbq.iocb,
2509                                       phba->iocb_rsp_size);
2510                 INIT_LIST_HEAD(&(rspiocbq.list));
2511                 irsp = &rspiocbq.iocb;
2512
2513                 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
2514                 pring->stats.iocb_rsp++;
2515                 rsp_cmpl++;
2516
2517                 if (unlikely(irsp->ulpStatus)) {
2518                         /*
2519                          * If resource errors reported from HBA, reduce
2520                          * queuedepths of the SCSI device.
2521                          */
2522                         if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
2523                                 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
2524                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
2525                                 phba->lpfc_rampdown_queue_depth(phba);
2526                                 spin_lock_irqsave(&phba->hbalock, iflag);
2527                         }
2528
2529                         /* Rsp ring <ringno> error: IOCB */
2530                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
2531                                         "0336 Rsp Ring %d error: IOCB Data: "
2532                                         "x%x x%x x%x x%x x%x x%x x%x x%x\n",
2533                                         pring->ringno,
2534                                         irsp->un.ulpWord[0],
2535                                         irsp->un.ulpWord[1],
2536                                         irsp->un.ulpWord[2],
2537                                         irsp->un.ulpWord[3],
2538                                         irsp->un.ulpWord[4],
2539                                         irsp->un.ulpWord[5],
2540                                         *(uint32_t *)&irsp->un1,
2541                                         *((uint32_t *)&irsp->un1 + 1));
2542                 }
2543
2544                 switch (type) {
2545                 case LPFC_ABORT_IOCB:
2546                 case LPFC_SOL_IOCB:
2547                         /*
2548                          * Idle exchange closed via ABTS from port.  No iocb
2549                          * resources need to be recovered.
2550                          */
2551                         if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
2552                                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
2553                                                 "0333 IOCB cmd 0x%x"
2554                                                 " processed. Skipping"
2555                                                 " completion\n",
2556                                                 irsp->ulpCommand);
2557                                 break;
2558                         }
2559
2560                         cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
2561                                                          &rspiocbq);
2562                         if (unlikely(!cmdiocbq))
2563                                 break;
2564                         if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
2565                                 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
2566                         if (cmdiocbq->iocb_cmpl) {
2567                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
2568                                 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
2569                                                       &rspiocbq);
2570                                 spin_lock_irqsave(&phba->hbalock, iflag);
2571                         }
2572                         break;
2573                 case LPFC_UNSOL_IOCB:
2574                         spin_unlock_irqrestore(&phba->hbalock, iflag);
2575                         lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
2576                         spin_lock_irqsave(&phba->hbalock, iflag);
2577                         break;
2578                 default:
2579                         if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
2580                                 char adaptermsg[LPFC_MAX_ADPTMSG];
2581                                 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
2582                                 memcpy(&adaptermsg[0], (uint8_t *) irsp,
2583                                        MAX_MSG_DATA);
2584                                 dev_warn(&((phba->pcidev)->dev),
2585                                          "lpfc%d: %s\n",
2586                                          phba->brd_no, adaptermsg);
2587                         } else {
2588                                 /* Unknown IOCB command */
2589                                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2590                                                 "0334 Unknown IOCB command "
2591                                                 "Data: x%x, x%x x%x x%x x%x\n",
2592                                                 type, irsp->ulpCommand,
2593                                                 irsp->ulpStatus,
2594                                                 irsp->ulpIoTag,
2595                                                 irsp->ulpContext);
2596                         }
2597                         break;
2598                 }
2599
2600                 /*
2601                  * The response IOCB has been processed.  Update the ring
2602                  * pointer in SLIM.  If the port response put pointer has not
2603                  * been updated, sync the pgp->rspPutInx and fetch the new port
2604                  * response put pointer.
2605                  */
2606                 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
2607
2608                 if (pring->rspidx == portRspPut)
2609                         portRspPut = le32_to_cpu(pgp->rspPutInx);
2610         }
2611
2612         if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
2613                 pring->stats.iocb_rsp_full++;
2614                 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
2615                 writel(status, phba->CAregaddr);
2616                 readl(phba->CAregaddr);
2617         }
2618         if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
2619                 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
2620                 pring->stats.iocb_cmd_empty++;
2621
2622                 /* Force update of the local copy of cmdGetInx */
2623                 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
2624                 lpfc_sli_resume_iocb(phba, pring);
2625
2626                 if ((pring->lpfc_sli_cmd_available))
2627                         (pring->lpfc_sli_cmd_available) (phba, pring);
2628
2629         }
2630
2631         phba->fcp_ring_in_use = 0;
2632         spin_unlock_irqrestore(&phba->hbalock, iflag);
2633         return rc;
2634 }
2635
2636 /**
2637  * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
2638  * @phba: Pointer to HBA context object.
2639  * @pring: Pointer to driver SLI ring object.
2640  * @rspiocbp: Pointer to driver response IOCB object.
2641  *
2642  * This function is called from the worker thread when there is a slow-path
2643  * response IOCB to process. This function chains all the response iocbs until
2644  * seeing the iocb with the LE bit set. The function will call
2645  * lpfc_sli_process_sol_iocb function if the response iocb indicates a
2646  * completion of a command iocb. The function will call the
2647  * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
2648  * The function frees the resources or calls the completion handler if this
2649  * iocb is an abort completion. The function returns NULL when the response
2650  * iocb has the LE bit set and all the chained iocbs are processed, otherwise
2651  * this function shall chain the iocb on to the iocb_continueq and return the
2652  * response iocb passed in.
2653  **/
2654 static struct lpfc_iocbq *
2655 lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2656                         struct lpfc_iocbq *rspiocbp)
2657 {
2658         struct lpfc_iocbq *saveq;
2659         struct lpfc_iocbq *cmdiocbp;
2660         struct lpfc_iocbq *next_iocb;
2661         IOCB_t *irsp = NULL;
2662         uint32_t free_saveq;
2663         uint8_t iocb_cmd_type;
2664         lpfc_iocb_type type;
2665         unsigned long iflag;
2666         int rc;
2667
2668         spin_lock_irqsave(&phba->hbalock, iflag);
2669         /* First add the response iocb to the countinueq list */
2670         list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
2671         pring->iocb_continueq_cnt++;
2672
2673         /* Now, determine whetehr the list is completed for processing */
2674         irsp = &rspiocbp->iocb;
2675         if (irsp->ulpLe) {
2676                 /*
2677                  * By default, the driver expects to free all resources
2678                  * associated with this iocb completion.
2679                  */
2680                 free_saveq = 1;
2681                 saveq = list_get_first(&pring->iocb_continueq,
2682                                        struct lpfc_iocbq, list);
2683                 irsp = &(saveq->iocb);
2684                 list_del_init(&pring->iocb_continueq);
2685                 pring->iocb_continueq_cnt = 0;
2686
2687                 pring->stats.iocb_rsp++;
2688
2689                 /*
2690                  * If resource errors reported from HBA, reduce
2691                  * queuedepths of the SCSI device.
2692                  */
2693                 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
2694                     (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
2695                         spin_unlock_irqrestore(&phba->hbalock, iflag);
2696                         phba->lpfc_rampdown_queue_depth(phba);
2697                         spin_lock_irqsave(&phba->hbalock, iflag);
2698                 }
2699
2700                 if (irsp->ulpStatus) {
2701                         /* Rsp ring <ringno> error: IOCB */
2702                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
2703                                         "0328 Rsp Ring %d error: "
2704                                         "IOCB Data: "
2705                                         "x%x x%x x%x x%x "
2706                                         "x%x x%x x%x x%x "
2707                                         "x%x x%x x%x x%x "
2708                                         "x%x x%x x%x x%x\n",
2709                                         pring->ringno,
2710                                         irsp->un.ulpWord[0],
2711                                         irsp->un.ulpWord[1],
2712                                         irsp->un.ulpWord[2],
2713                                         irsp->un.ulpWord[3],
2714                                         irsp->un.ulpWord[4],
2715                                         irsp->un.ulpWord[5],
2716                                         *(((uint32_t *) irsp) + 6),
2717                                         *(((uint32_t *) irsp) + 7),
2718                                         *(((uint32_t *) irsp) + 8),
2719                                         *(((uint32_t *) irsp) + 9),
2720                                         *(((uint32_t *) irsp) + 10),
2721                                         *(((uint32_t *) irsp) + 11),
2722                                         *(((uint32_t *) irsp) + 12),
2723                                         *(((uint32_t *) irsp) + 13),
2724                                         *(((uint32_t *) irsp) + 14),
2725                                         *(((uint32_t *) irsp) + 15));
2726                 }
2727
2728                 /*
2729                  * Fetch the IOCB command type and call the correct completion
2730                  * routine. Solicited and Unsolicited IOCBs on the ELS ring
2731                  * get freed back to the lpfc_iocb_list by the discovery
2732                  * kernel thread.
2733                  */
2734                 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
2735                 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
2736                 switch (type) {
2737                 case LPFC_SOL_IOCB:
2738                         spin_unlock_irqrestore(&phba->hbalock, iflag);
2739                         rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
2740                         spin_lock_irqsave(&phba->hbalock, iflag);
2741                         break;
2742
2743                 case LPFC_UNSOL_IOCB:
2744                         spin_unlock_irqrestore(&phba->hbalock, iflag);
2745                         rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
2746                         spin_lock_irqsave(&phba->hbalock, iflag);
2747                         if (!rc)
2748                                 free_saveq = 0;
2749                         break;
2750
2751                 case LPFC_ABORT_IOCB:
2752                         cmdiocbp = NULL;
2753                         if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
2754                                 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
2755                                                                  saveq);
2756                         if (cmdiocbp) {
2757                                 /* Call the specified completion routine */
2758                                 if (cmdiocbp->iocb_cmpl) {
2759                                         spin_unlock_irqrestore(&phba->hbalock,
2760                                                                iflag);
2761                                         (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
2762                                                               saveq);
2763                                         spin_lock_irqsave(&phba->hbalock,
2764                                                           iflag);
2765                                 } else
2766                                         __lpfc_sli_release_iocbq(phba,
2767                                                                  cmdiocbp);
2768                         }
2769                         break;
2770
2771                 case LPFC_UNKNOWN_IOCB:
2772                         if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
2773                                 char adaptermsg[LPFC_MAX_ADPTMSG];
2774                                 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
2775                                 memcpy(&adaptermsg[0], (uint8_t *)irsp,
2776                                        MAX_MSG_DATA);
2777                                 dev_warn(&((phba->pcidev)->dev),
2778                                          "lpfc%d: %s\n",
2779                                          phba->brd_no, adaptermsg);
2780                         } else {
2781                                 /* Unknown IOCB command */
2782                                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2783                                                 "0335 Unknown IOCB "
2784                                                 "command Data: x%x "
2785                                                 "x%x x%x x%x\n",
2786                                                 irsp->ulpCommand,
2787                                                 irsp->ulpStatus,
2788                                                 irsp->ulpIoTag,
2789                                                 irsp->ulpContext);
2790                         }
2791                         break;
2792                 }
2793
2794                 if (free_saveq) {
2795                         list_for_each_entry_safe(rspiocbp, next_iocb,
2796                                                  &saveq->list, list) {
2797                                 list_del(&rspiocbp->list);
2798                                 __lpfc_sli_release_iocbq(phba, rspiocbp);
2799                         }
2800                         __lpfc_sli_release_iocbq(phba, saveq);
2801                 }
2802                 rspiocbp = NULL;
2803         }
2804         spin_unlock_irqrestore(&phba->hbalock, iflag);
2805         return rspiocbp;
2806 }
2807
2808 /**
2809  * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
2810  * @phba: Pointer to HBA context object.
2811  * @pring: Pointer to driver SLI ring object.
2812  * @mask: Host attention register mask for this ring.
2813  *
2814  * This routine wraps the actual slow_ring event process routine from the
2815  * API jump table function pointer from the lpfc_hba struct.
2816  **/
2817 void
2818 lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
2819                                 struct lpfc_sli_ring *pring, uint32_t mask)
2820 {
2821         phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
2822 }
2823
2824 /**
2825  * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
2826  * @phba: Pointer to HBA context object.
2827  * @pring: Pointer to driver SLI ring object.
2828  * @mask: Host attention register mask for this ring.
2829  *
2830  * This function is called from the worker thread when there is a ring event
2831  * for non-fcp rings. The caller does not hold any lock. The function will
2832  * remove each response iocb in the response ring and calls the handle
2833  * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
2834  **/
2835 static void
2836 lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
2837                                    struct lpfc_sli_ring *pring, uint32_t mask)
2838 {
2839         struct lpfc_pgp *pgp;
2840         IOCB_t *entry;
2841         IOCB_t *irsp = NULL;
2842         struct lpfc_iocbq *rspiocbp = NULL;
2843         uint32_t portRspPut, portRspMax;
2844         unsigned long iflag;
2845         uint32_t status;
2846
2847         pgp = &phba->port_gp[pring->ringno];
2848         spin_lock_irqsave(&phba->hbalock, iflag);
2849         pring->stats.iocb_event++;
2850
2851         /*
2852          * The next available response entry should never exceed the maximum
2853          * entries.  If it does, treat it as an adapter hardware error.
2854          */
2855         portRspMax = pring->numRiocb;
2856         portRspPut = le32_to_cpu(pgp->rspPutInx);
2857         if (portRspPut >= portRspMax) {
2858                 /*
2859                  * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
2860                  * rsp ring <portRspMax>
2861                  */
2862                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2863                                 "0303 Ring %d handler: portRspPut %d "
2864                                 "is bigger than rsp ring %d\n",
2865                                 pring->ringno, portRspPut, portRspMax);
2866
2867                 phba->link_state = LPFC_HBA_ERROR;
2868                 spin_unlock_irqrestore(&phba->hbalock, iflag);
2869
2870                 phba->work_hs = HS_FFER3;
2871                 lpfc_handle_eratt(phba);
2872
2873                 return;
2874         }
2875
2876         rmb();
2877         while (pring->rspidx != portRspPut) {
2878                 /*
2879                  * Build a completion list and call the appropriate handler.
2880                  * The process is to get the next available response iocb, get
2881                  * a free iocb from the list, copy the response data into the
2882                  * free iocb, insert to the continuation list, and update the
2883                  * next response index to slim.  This process makes response
2884                  * iocb's in the ring available to DMA as fast as possible but
2885                  * pays a penalty for a copy operation.  Since the iocb is
2886                  * only 32 bytes, this penalty is considered small relative to
2887                  * the PCI reads for register values and a slim write.  When
2888                  * the ulpLe field is set, the entire Command has been
2889                  * received.
2890                  */
2891                 entry = lpfc_resp_iocb(phba, pring);
2892
2893                 phba->last_completion_time = jiffies;
2894                 rspiocbp = __lpfc_sli_get_iocbq(phba);
2895                 if (rspiocbp == NULL) {
2896                         printk(KERN_ERR "%s: out of buffers! Failing "
2897                                "completion.\n", __func__);
2898                         break;
2899                 }
2900
2901                 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
2902                                       phba->iocb_rsp_size);
2903                 irsp = &rspiocbp->iocb;
2904
2905                 if (++pring->rspidx >= portRspMax)
2906                         pring->rspidx = 0;
2907
2908                 if (pring->ringno == LPFC_ELS_RING) {
2909                         lpfc_debugfs_slow_ring_trc(phba,
2910                         "IOCB rsp ring:   wd4:x%08x wd6:x%08x wd7:x%08x",
2911                                 *(((uint32_t *) irsp) + 4),
2912                                 *(((uint32_t *) irsp) + 6),
2913                                 *(((uint32_t *) irsp) + 7));
2914                 }
2915
2916                 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
2917
2918                 spin_unlock_irqrestore(&phba->hbalock, iflag);
2919                 /* Handle the response IOCB */
2920                 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
2921                 spin_lock_irqsave(&phba->hbalock, iflag);
2922
2923                 /*
2924                  * If the port response put pointer has not been updated, sync
2925                  * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
2926                  * response put pointer.
2927                  */
2928                 if (pring->rspidx == portRspPut) {
2929                         portRspPut = le32_to_cpu(pgp->rspPutInx);
2930                 }
2931         } /* while (pring->rspidx != portRspPut) */
2932
2933         if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
2934                 /* At least one response entry has been freed */
2935                 pring->stats.iocb_rsp_full++;
2936                 /* SET RxRE_RSP in Chip Att register */
2937                 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
2938                 writel(status, phba->CAregaddr);
2939                 readl(phba->CAregaddr); /* flush */
2940         }
2941         if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
2942                 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
2943                 pring->stats.iocb_cmd_empty++;
2944
2945                 /* Force update of the local copy of cmdGetInx */
2946                 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
2947                 lpfc_sli_resume_iocb(phba, pring);
2948
2949                 if ((pring->lpfc_sli_cmd_available))
2950                         (pring->lpfc_sli_cmd_available) (phba, pring);
2951
2952         }
2953
2954         spin_unlock_irqrestore(&phba->hbalock, iflag);
2955         return;
2956 }
2957
2958 /**
2959  * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
2960  * @phba: Pointer to HBA context object.
2961  * @pring: Pointer to driver SLI ring object.
2962  * @mask: Host attention register mask for this ring.
2963  *
2964  * This function is called from the worker thread when there is a pending
2965  * ELS response iocb on the driver internal slow-path response iocb worker
2966  * queue. The caller does not hold any lock. The function will remove each
2967  * response iocb from the response worker queue and calls the handle
2968  * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
2969  **/
2970 static void
2971 lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
2972                                    struct lpfc_sli_ring *pring, uint32_t mask)
2973 {
2974         struct lpfc_iocbq *irspiocbq;
2975         struct hbq_dmabuf *dmabuf;
2976         struct lpfc_cq_event *cq_event;
2977         unsigned long iflag;
2978
2979         spin_lock_irqsave(&phba->hbalock, iflag);
2980         phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
2981         spin_unlock_irqrestore(&phba->hbalock, iflag);
2982         while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
2983                 /* Get the response iocb from the head of work queue */
2984                 spin_lock_irqsave(&phba->hbalock, iflag);
2985                 list_remove_head(&phba->sli4_hba.sp_queue_event,
2986                                  cq_event, struct lpfc_cq_event, list);
2987                 spin_unlock_irqrestore(&phba->hbalock, iflag);
2988
2989                 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
2990                 case CQE_CODE_COMPL_WQE:
2991                         irspiocbq = container_of(cq_event, struct lpfc_iocbq,
2992                                                  cq_event);
2993                         /* Translate ELS WCQE to response IOCBQ */
2994                         irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
2995                                                                    irspiocbq);
2996                         if (irspiocbq)
2997                                 lpfc_sli_sp_handle_rspiocb(phba, pring,
2998                                                            irspiocbq);
2999                         break;
3000                 case CQE_CODE_RECEIVE:
3001                         dmabuf = container_of(cq_event, struct hbq_dmabuf,
3002                                               cq_event);
3003                         lpfc_sli4_handle_received_buffer(phba, dmabuf);
3004                         break;
3005                 default:
3006                         break;
3007                 }
3008         }
3009 }
3010
3011 /**
3012  * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
3013  * @phba: Pointer to HBA context object.
3014  * @pring: Pointer to driver SLI ring object.
3015  *
3016  * This function aborts all iocbs in the given ring and frees all the iocb
3017  * objects in txq. This function issues an abort iocb for all the iocb commands
3018  * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3019  * the return of this function. The caller is not required to hold any locks.
3020  **/
3021 void
3022 lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3023 {
3024         LIST_HEAD(completions);
3025         struct lpfc_iocbq *iocb, *next_iocb;
3026
3027         if (pring->ringno == LPFC_ELS_RING) {
3028                 lpfc_fabric_abort_hba(phba);
3029         }
3030
3031         /* Error everything on txq and txcmplq
3032          * First do the txq.
3033          */
3034         spin_lock_irq(&phba->hbalock);
3035         list_splice_init(&pring->txq, &completions);
3036         pring->txq_cnt = 0;
3037
3038         /* Next issue ABTS for everything on the txcmplq */
3039         list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3040                 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3041
3042         spin_unlock_irq(&phba->hbalock);
3043
3044         /* Cancel all the IOCBs from the completions list */
3045         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3046                               IOERR_SLI_ABORTED);
3047 }
3048
3049 /**
3050  * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
3051  * @phba: Pointer to HBA context object.
3052  *
3053  * This function flushes all iocbs in the fcp ring and frees all the iocb
3054  * objects in txq and txcmplq. This function will not issue abort iocbs
3055  * for all the iocb commands in txcmplq, they will just be returned with
3056  * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3057  * slot has been permanently disabled.
3058  **/
3059 void
3060 lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
3061 {
3062         LIST_HEAD(txq);
3063         LIST_HEAD(txcmplq);
3064         struct lpfc_sli *psli = &phba->sli;
3065         struct lpfc_sli_ring  *pring;
3066
3067         /* Currently, only one fcp ring */
3068         pring = &psli->ring[psli->fcp_ring];
3069
3070         spin_lock_irq(&phba->hbalock);
3071         /* Retrieve everything on txq */
3072         list_splice_init(&pring->txq, &txq);
3073         pring->txq_cnt = 0;
3074
3075         /* Retrieve everything on the txcmplq */
3076         list_splice_init(&pring->txcmplq, &txcmplq);
3077         pring->txcmplq_cnt = 0;
3078         spin_unlock_irq(&phba->hbalock);
3079
3080         /* Flush the txq */
3081         lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
3082                               IOERR_SLI_DOWN);
3083
3084         /* Flush the txcmpq */
3085         lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
3086                               IOERR_SLI_DOWN);
3087 }
3088
3089 /**
3090  * lpfc_sli_brdready_s3 - Check for sli3 host ready status
3091  * @phba: Pointer to HBA context object.
3092  * @mask: Bit mask to be checked.
3093  *
3094  * This function reads the host status register and compares
3095  * with the provided bit mask to check if HBA completed
3096  * the restart. This function will wait in a loop for the
3097  * HBA to complete restart. If the HBA does not restart within
3098  * 15 iterations, the function will reset the HBA again. The
3099  * function returns 1 when HBA fail to restart otherwise returns
3100  * zero.
3101  **/
3102 static int
3103 lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
3104 {
3105         uint32_t status;
3106         int i = 0;
3107         int retval = 0;
3108
3109         /* Read the HBA Host Status Register */
3110         status = readl(phba->HSregaddr);
3111
3112         /*
3113          * Check status register every 100ms for 5 retries, then every
3114          * 500ms for 5, then every 2.5 sec for 5, then reset board and
3115          * every 2.5 sec for 4.
3116          * Break our of the loop if errors occurred during init.
3117          */
3118         while (((status & mask) != mask) &&
3119                !(status & HS_FFERM) &&
3120                i++ < 20) {
3121
3122                 if (i <= 5)
3123                         msleep(10);
3124                 else if (i <= 10)
3125                         msleep(500);
3126                 else
3127                         msleep(2500);
3128
3129                 if (i == 15) {
3130                                 /* Do post */
3131                         phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3132                         lpfc_sli_brdrestart(phba);
3133                 }
3134                 /* Read the HBA Host Status Register */
3135                 status = readl(phba->HSregaddr);
3136         }
3137
3138         /* Check to see if any errors occurred during init */
3139         if ((status & HS_FFERM) || (i >= 20)) {
3140                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3141                                 "2751 Adapter failed to restart, "
3142                                 "status reg x%x, FW Data: A8 x%x AC x%x\n",
3143                                 status,
3144                                 readl(phba->MBslimaddr + 0xa8),
3145                                 readl(phba->MBslimaddr + 0xac));
3146                 phba->link_state = LPFC_HBA_ERROR;
3147                 retval = 1;
3148         }
3149
3150         return retval;
3151 }
3152
3153 /**
3154  * lpfc_sli_brdready_s4 - Check for sli4 host ready status
3155  * @phba: Pointer to HBA context object.
3156  * @mask: Bit mask to be checked.
3157  *
3158  * This function checks the host status register to check if HBA is
3159  * ready. This function will wait in a loop for the HBA to be ready
3160  * If the HBA is not ready , the function will will reset the HBA PCI
3161  * function again. The function returns 1 when HBA fail to be ready
3162  * otherwise returns zero.
3163  **/
3164 static int
3165 lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
3166 {
3167         uint32_t status;
3168         int retval = 0;
3169
3170         /* Read the HBA Host Status Register */
3171         status = lpfc_sli4_post_status_check(phba);
3172
3173         if (status) {
3174                 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3175                 lpfc_sli_brdrestart(phba);
3176                 status = lpfc_sli4_post_status_check(phba);
3177         }
3178
3179         /* Check to see if any errors occurred during init */
3180         if (status) {
3181                 phba->link_state = LPFC_HBA_ERROR;
3182                 retval = 1;
3183         } else
3184                 phba->sli4_hba.intr_enable = 0;
3185
3186         return retval;
3187 }
3188
3189 /**
3190  * lpfc_sli_brdready - Wrapper func for checking the hba readyness
3191  * @phba: Pointer to HBA context object.
3192  * @mask: Bit mask to be checked.
3193  *
3194  * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
3195  * from the API jump table function pointer from the lpfc_hba struct.
3196  **/
3197 int
3198 lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
3199 {
3200         return phba->lpfc_sli_brdready(phba, mask);
3201 }
3202
3203 #define BARRIER_TEST_PATTERN (0xdeadbeef)
3204
3205 /**
3206  * lpfc_reset_barrier - Make HBA ready for HBA reset
3207  * @phba: Pointer to HBA context object.
3208  *
3209  * This function is called before resetting an HBA. This
3210  * function requests HBA to quiesce DMAs before a reset.
3211  **/
3212 void lpfc_reset_barrier(struct lpfc_hba *phba)
3213 {
3214         uint32_t __iomem *resp_buf;
3215         uint32_t __iomem *mbox_buf;
3216         volatile uint32_t mbox;
3217         uint32_t hc_copy;
3218         int  i;
3219         uint8_t hdrtype;
3220
3221         pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
3222         if (hdrtype != 0x80 ||
3223             (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
3224              FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
3225                 return;
3226
3227         /*
3228          * Tell the other part of the chip to suspend temporarily all
3229          * its DMA activity.
3230          */
3231         resp_buf = phba->MBslimaddr;
3232
3233         /* Disable the error attention */
3234         hc_copy = readl(phba->HCregaddr);
3235         writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
3236         readl(phba->HCregaddr); /* flush */
3237         phba->link_flag |= LS_IGNORE_ERATT;
3238
3239         if (readl(phba->HAregaddr) & HA_ERATT) {
3240                 /* Clear Chip error bit */
3241                 writel(HA_ERATT, phba->HAregaddr);
3242                 phba->pport->stopped = 1;
3243         }
3244
3245         mbox = 0;
3246         ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
3247         ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
3248
3249         writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
3250         mbox_buf = phba->MBslimaddr;
3251         writel(mbox, mbox_buf);
3252
3253         for (i = 0;
3254              readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN) && i < 50; i++)
3255                 mdelay(1);
3256
3257         if (readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN)) {
3258                 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
3259                     phba->pport->stopped)
3260                         goto restore_hc;
3261                 else
3262                         goto clear_errat;
3263         }
3264
3265         ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
3266         for (i = 0; readl(resp_buf) != mbox &&  i < 500; i++)
3267                 mdelay(1);
3268
3269 clear_errat:
3270
3271         while (!(readl(phba->HAregaddr) & HA_ERATT) && ++i < 500)
3272                 mdelay(1);
3273
3274         if (readl(phba->HAregaddr) & HA_ERATT) {
3275                 writel(HA_ERATT, phba->HAregaddr);
3276                 phba->pport->stopped = 1;
3277         }
3278
3279 restore_hc:
3280         phba->link_flag &= ~LS_IGNORE_ERATT;
3281         writel(hc_copy, phba->HCregaddr);
3282         readl(phba->HCregaddr); /* flush */
3283 }
3284
3285 /**
3286  * lpfc_sli_brdkill - Issue a kill_board mailbox command
3287  * @phba: Pointer to HBA context object.
3288  *
3289  * This function issues a kill_board mailbox command and waits for
3290  * the error attention interrupt. This function is called for stopping
3291  * the firmware processing. The caller is not required to hold any
3292  * locks. This function calls lpfc_hba_down_post function to free
3293  * any pending commands after the kill. The function will return 1 when it
3294  * fails to kill the board else will return 0.
3295  **/
3296 int
3297 lpfc_sli_brdkill(struct lpfc_hba *phba)
3298 {
3299         struct lpfc_sli *psli;
3300         LPFC_MBOXQ_t *pmb;
3301         uint32_t status;
3302         uint32_t ha_copy;
3303         int retval;
3304         int i = 0;
3305
3306         psli = &phba->sli;
3307
3308         /* Kill HBA */
3309         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3310                         "0329 Kill HBA Data: x%x x%x\n",
3311                         phba->pport->port_state, psli->sli_flag);
3312
3313         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3314         if (!pmb)
3315                 return 1;
3316
3317         /* Disable the error attention */
3318         spin_lock_irq(&phba->hbalock);
3319         status = readl(phba->HCregaddr);
3320         status &= ~HC_ERINT_ENA;
3321         writel(status, phba->HCregaddr);
3322         readl(phba->HCregaddr); /* flush */
3323         phba->link_flag |= LS_IGNORE_ERATT;
3324         spin_unlock_irq(&phba->hbalock);
3325
3326         lpfc_kill_board(phba, pmb);
3327         pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3328         retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3329
3330         if (retval != MBX_SUCCESS) {
3331                 if (retval != MBX_BUSY)
3332                         mempool_free(pmb, phba->mbox_mem_pool);
3333                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3334                                 "2752 KILL_BOARD command failed retval %d\n",
3335                                 retval);
3336                 spin_lock_irq(&phba->hbalock);
3337                 phba->link_flag &= ~LS_IGNORE_ERATT;
3338                 spin_unlock_irq(&phba->hbalock);
3339                 return 1;
3340         }
3341
3342         spin_lock_irq(&phba->hbalock);
3343         psli->sli_flag &= ~LPFC_SLI_ACTIVE;
3344         spin_unlock_irq(&phba->hbalock);
3345
3346         mempool_free(pmb, phba->mbox_mem_pool);
3347
3348         /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
3349          * attention every 100ms for 3 seconds. If we don't get ERATT after
3350          * 3 seconds we still set HBA_ERROR state because the status of the
3351          * board is now undefined.
3352          */
3353         ha_copy = readl(phba->HAregaddr);
3354
3355         while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
3356                 mdelay(100);
3357                 ha_copy = readl(phba->HAregaddr);
3358         }
3359
3360         del_timer_sync(&psli->mbox_tmo);
3361         if (ha_copy & HA_ERATT) {
3362                 writel(HA_ERATT, phba->HAregaddr);
3363                 phba->pport->stopped = 1;
3364         }
3365         spin_lock_irq(&phba->hbalock);
3366         psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
3367         psli->mbox_active = NULL;
3368         phba->link_flag &= ~LS_IGNORE_ERATT;
3369         spin_unlock_irq(&phba->hbalock);
3370
3371         lpfc_hba_down_post(phba);
3372         phba->link_state = LPFC_HBA_ERROR;
3373
3374         return ha_copy & HA_ERATT ? 0 : 1;
3375 }
3376
3377 /**
3378  * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
3379  * @phba: Pointer to HBA context object.
3380  *
3381  * This function resets the HBA by writing HC_INITFF to the control
3382  * register. After the HBA resets, this function resets all the iocb ring
3383  * indices. This function disables PCI layer parity checking during
3384  * the reset.
3385  * This function returns 0 always.
3386  * The caller is not required to hold any locks.
3387  **/
3388 int
3389 lpfc_sli_brdreset(struct lpfc_hba *phba)
3390 {
3391         struct lpfc_sli *psli;
3392         struct lpfc_sli_ring *pring;
3393         uint16_t cfg_value;
3394         int i;
3395
3396         psli = &phba->sli;
3397
3398         /* Reset HBA */
3399         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3400                         "0325 Reset HBA Data: x%x x%x\n",
3401                         phba->pport->port_state, psli->sli_flag);
3402
3403         /* perform board reset */
3404         phba->fc_eventTag = 0;
3405         phba->link_events = 0;
3406         phba->pport->fc_myDID = 0;
3407         phba->pport->fc_prevDID = 0;
3408
3409         /* Turn off parity checking and serr during the physical reset */
3410         pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3411         pci_write_config_word(phba->pcidev, PCI_COMMAND,
3412                               (cfg_value &
3413                                ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3414
3415         psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
3416
3417         /* Now toggle INITFF bit in the Host Control Register */
3418         writel(HC_INITFF, phba->HCregaddr);
3419         mdelay(1);
3420         readl(phba->HCregaddr); /* flush */
3421         writel(0, phba->HCregaddr);
3422         readl(phba->HCregaddr); /* flush */
3423
3424         /* Restore PCI cmd register */
3425         pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
3426
3427         /* Initialize relevant SLI info */
3428         for (i = 0; i < psli->num_rings; i++) {
3429                 pring = &psli->ring[i];
3430                 pring->flag = 0;
3431                 pring->rspidx = 0;
3432                 pring->next_cmdidx  = 0;
3433                 pring->local_getidx = 0;
3434                 pring->cmdidx = 0;
3435                 pring->missbufcnt = 0;
3436         }
3437
3438         phba->link_state = LPFC_WARM_START;
3439         return 0;
3440 }
3441
3442 /**
3443  * lpfc_sli4_brdreset - Reset a sli-4 HBA
3444  * @phba: Pointer to HBA context object.
3445  *
3446  * This function resets a SLI4 HBA. This function disables PCI layer parity
3447  * checking during resets the device. The caller is not required to hold
3448  * any locks.
3449  *
3450  * This function returns 0 always.
3451  **/
3452 int
3453 lpfc_sli4_brdreset(struct lpfc_hba *phba)
3454 {
3455         struct lpfc_sli *psli = &phba->sli;
3456         uint16_t cfg_value;
3457         uint8_t qindx;
3458
3459         /* Reset HBA */
3460         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3461                         "0295 Reset HBA Data: x%x x%x\n",
3462                         phba->pport->port_state, psli->sli_flag);
3463
3464         /* perform board reset */
3465         phba->fc_eventTag = 0;
3466         phba->link_events = 0;
3467         phba->pport->fc_myDID = 0;
3468         phba->pport->fc_prevDID = 0;
3469
3470         /* Turn off parity checking and serr during the physical reset */
3471         pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3472         pci_write_config_word(phba->pcidev, PCI_COMMAND,
3473                               (cfg_value &
3474                               ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3475
3476         spin_lock_irq(&phba->hbalock);
3477         psli->sli_flag &= ~(LPFC_PROCESS_LA);
3478         phba->fcf.fcf_flag = 0;
3479         /* Clean up the child queue list for the CQs */
3480         list_del_init(&phba->sli4_hba.mbx_wq->list);
3481         list_del_init(&phba->sli4_hba.els_wq->list);
3482         list_del_init(&phba->sli4_hba.hdr_rq->list);
3483         list_del_init(&phba->sli4_hba.dat_rq->list);
3484         list_del_init(&phba->sli4_hba.mbx_cq->list);
3485         list_del_init(&phba->sli4_hba.els_cq->list);
3486         for (qindx = 0; qindx < phba->cfg_fcp_wq_count; qindx++)
3487                 list_del_init(&phba->sli4_hba.fcp_wq[qindx]->list);
3488         for (qindx = 0; qindx < phba->cfg_fcp_eq_count; qindx++)
3489                 list_del_init(&phba->sli4_hba.fcp_cq[qindx]->list);
3490         spin_unlock_irq(&phba->hbalock);
3491
3492         /* Now physically reset the device */
3493         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3494                         "0389 Performing PCI function reset!\n");
3495         /* Perform FCoE PCI function reset */
3496         lpfc_pci_function_reset(phba);
3497
3498         return 0;
3499 }
3500
3501 /**
3502  * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
3503  * @phba: Pointer to HBA context object.
3504  *
3505  * This function is called in the SLI initialization code path to
3506  * restart the HBA. The caller is not required to hold any lock.
3507  * This function writes MBX_RESTART mailbox command to the SLIM and
3508  * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
3509  * function to free any pending commands. The function enables
3510  * POST only during the first initialization. The function returns zero.
3511  * The function does not guarantee completion of MBX_RESTART mailbox
3512  * command before the return of this function.
3513  **/
3514 static int
3515 lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
3516 {
3517         MAILBOX_t *mb;
3518         struct lpfc_sli *psli;
3519         volatile uint32_t word0;
3520         void __iomem *to_slim;
3521         uint32_t hba_aer_enabled;
3522
3523         spin_lock_irq(&phba->hbalock);
3524
3525         /* Take PCIe device Advanced Error Reporting (AER) state */
3526         hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
3527
3528         psli = &phba->sli;
3529
3530         /* Restart HBA */
3531         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3532                         "0337 Restart HBA Data: x%x x%x\n",
3533                         phba->pport->port_state, psli->sli_flag);
3534
3535         word0 = 0;
3536         mb = (MAILBOX_t *) &word0;
3537         mb->mbxCommand = MBX_RESTART;
3538         mb->mbxHc = 1;
3539
3540         lpfc_reset_barrier(phba);
3541
3542         to_slim = phba->MBslimaddr;
3543         writel(*(uint32_t *) mb, to_slim);
3544         readl(to_slim); /* flush */
3545
3546         /* Only skip post after fc_ffinit is completed */
3547         if (phba->pport->port_state)
3548                 word0 = 1;      /* This is really setting up word1 */
3549         else
3550                 word0 = 0;      /* This is really setting up word1 */
3551         to_slim = phba->MBslimaddr + sizeof (uint32_t);
3552         writel(*(uint32_t *) mb, to_slim);
3553         readl(to_slim); /* flush */
3554
3555         lpfc_sli_brdreset(phba);
3556         phba->pport->stopped = 0;
3557         phba->link_state = LPFC_INIT_START;
3558         phba->hba_flag = 0;
3559         spin_unlock_irq(&phba->hbalock);
3560
3561         memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
3562         psli->stats_start = get_seconds();
3563
3564         /* Give the INITFF and Post time to settle. */
3565         mdelay(100);
3566
3567         /* Reset HBA AER if it was enabled, note hba_flag was reset above */
3568         if (hba_aer_enabled)
3569                 pci_disable_pcie_error_reporting(phba->pcidev);
3570
3571         lpfc_hba_down_post(phba);
3572
3573         return 0;
3574 }
3575
3576 /**
3577  * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
3578  * @phba: Pointer to HBA context object.
3579  *
3580  * This function is called in the SLI initialization code path to restart
3581  * a SLI4 HBA. The caller is not required to hold any lock.
3582  * At the end of the function, it calls lpfc_hba_down_post function to
3583  * free any pending commands.
3584  **/
3585 static int
3586 lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
3587 {
3588         struct lpfc_sli *psli = &phba->sli;
3589         uint32_t hba_aer_enabled;
3590
3591         /* Restart HBA */
3592         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3593                         "0296 Restart HBA Data: x%x x%x\n",
3594                         phba->pport->port_state, psli->sli_flag);
3595
3596         /* Take PCIe device Advanced Error Reporting (AER) state */
3597         hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
3598
3599         lpfc_sli4_brdreset(phba);
3600
3601         spin_lock_irq(&phba->hbalock);
3602         phba->pport->stopped = 0;
3603         phba->link_state = LPFC_INIT_START;
3604         phba->hba_flag = 0;
3605         spin_unlock_irq(&phba->hbalock);
3606
3607         memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
3608         psli->stats_start = get_seconds();
3609
3610         /* Reset HBA AER if it was enabled, note hba_flag was reset above */
3611         if (hba_aer_enabled)
3612                 pci_disable_pcie_error_reporting(phba->pcidev);
3613
3614         lpfc_hba_down_post(phba);
3615
3616         return 0;
3617 }
3618
3619 /**
3620  * lpfc_sli_brdrestart - Wrapper func for restarting hba
3621  * @phba: Pointer to HBA context object.
3622  *
3623  * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
3624  * API jump table function pointer from the lpfc_hba struct.
3625 **/
3626 int
3627 lpfc_sli_brdrestart(struct lpfc_hba *phba)
3628 {
3629         return phba->lpfc_sli_brdrestart(phba);
3630 }
3631
3632 /**
3633  * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
3634  * @phba: Pointer to HBA context object.
3635  *
3636  * This function is called after a HBA restart to wait for successful
3637  * restart of the HBA. Successful restart of the HBA is indicated by
3638  * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
3639  * iteration, the function will restart the HBA again. The function returns
3640  * zero if HBA successfully restarted else returns negative error code.
3641  **/
3642 static int
3643 lpfc_sli_chipset_init(struct lpfc_hba *phba)
3644 {
3645         uint32_t status, i = 0;
3646
3647         /* Read the HBA Host Status Register */
3648         status = readl(phba->HSregaddr);
3649
3650         /* Check status register to see what current state is */
3651         i = 0;
3652         while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
3653
3654                 /* Check every 100ms for 5 retries, then every 500ms for 5, then
3655                  * every 2.5 sec for 5, then reset board and every 2.5 sec for
3656                  * 4.
3657                  */
3658                 if (i++ >= 20) {
3659                         /* Adapter failed to init, timeout, status reg
3660                            <status> */
3661                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3662                                         "0436 Adapter failed to init, "
3663                                         "timeout, status reg x%x, "
3664                                         "FW Data: A8 x%x AC x%x\n", status,
3665                                         readl(phba->MBslimaddr + 0xa8),
3666                                         readl(phba->MBslimaddr + 0xac));
3667                         phba->link_state = LPFC_HBA_ERROR;
3668                         return -ETIMEDOUT;
3669                 }
3670
3671                 /* Check to see if any errors occurred during init */
3672                 if (status & HS_FFERM) {
3673                         /* ERROR: During chipset initialization */
3674                         /* Adapter failed to init, chipset, status reg
3675                            <status> */
3676                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3677                                         "0437 Adapter failed to init, "
3678                                         "chipset, status reg x%x, "
3679                                         "FW Data: A8 x%x AC x%x\n", status,
3680                                         readl(phba->MBslimaddr + 0xa8),
3681                                         readl(phba->MBslimaddr + 0xac));
3682                         phba->link_state = LPFC_HBA_ERROR;
3683                         return -EIO;
3684                 }
3685
3686                 if (i <= 5) {
3687                         msleep(10);
3688                 } else if (i <= 10) {
3689                         msleep(500);
3690                 } else {
3691                         msleep(2500);
3692                 }
3693
3694                 if (i == 15) {
3695                                 /* Do post */
3696                         phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3697                         lpfc_sli_brdrestart(phba);
3698                 }
3699                 /* Read the HBA Host Status Register */
3700                 status = readl(phba->HSregaddr);
3701         }
3702
3703         /* Check to see if any errors occurred during init */
3704         if (status & HS_FFERM) {
3705                 /* ERROR: During chipset initialization */
3706                 /* Adapter failed to init, chipset, status reg <status> */
3707                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3708                                 "0438 Adapter failed to init, chipset, "
3709                                 "status reg x%x, "
3710                                 "FW Data: A8 x%x AC x%x\n", status,
3711                                 readl(phba->MBslimaddr + 0xa8),
3712                                 readl(phba->MBslimaddr + 0xac));
3713                 phba->link_state = LPFC_HBA_ERROR;
3714                 return -EIO;
3715         }
3716
3717         /* Clear all interrupt enable conditions */
3718         writel(0, phba->HCregaddr);
3719         readl(phba->HCregaddr); /* flush */
3720
3721         /* setup host attn register */
3722         writel(0xffffffff, phba->HAregaddr);
3723         readl(phba->HAregaddr); /* flush */
3724         return 0;
3725 }
3726
3727 /**
3728  * lpfc_sli_hbq_count - Get the number of HBQs to be configured
3729  *
3730  * This function calculates and returns the number of HBQs required to be
3731  * configured.
3732  **/
3733 int
3734 lpfc_sli_hbq_count(void)
3735 {
3736         return ARRAY_SIZE(lpfc_hbq_defs);
3737 }
3738
3739 /**
3740  * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
3741  *
3742  * This function adds the number of hbq entries in every HBQ to get
3743  * the total number of hbq entries required for the HBA and returns
3744  * the total count.
3745  **/
3746 static int
3747 lpfc_sli_hbq_entry_count(void)
3748 {
3749         int  hbq_count = lpfc_sli_hbq_count();
3750         int  count = 0;
3751         int  i;
3752
3753         for (i = 0; i < hbq_count; ++i)
3754                 count += lpfc_hbq_defs[i]->entry_count;
3755         return count;
3756 }
3757
3758 /**
3759  * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
3760  *
3761  * This function calculates amount of memory required for all hbq entries
3762  * to be configured and returns the total memory required.
3763  **/
3764 int
3765 lpfc_sli_hbq_size(void)
3766 {
3767         return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
3768 }
3769
3770 /**
3771  * lpfc_sli_hbq_setup - configure and initialize HBQs
3772  * @phba: Pointer to HBA context object.
3773  *
3774  * This function is called during the SLI initialization to configure
3775  * all the HBQs and post buffers to the HBQ. The caller is not
3776  * required to hold any locks. This function will return zero if successful
3777  * else it will return negative error code.
3778  **/
3779 static int
3780 lpfc_sli_hbq_setup(struct lpfc_hba *phba)
3781 {
3782         int  hbq_count = lpfc_sli_hbq_count();
3783         LPFC_MBOXQ_t *pmb;
3784         MAILBOX_t *pmbox;
3785         uint32_t hbqno;
3786         uint32_t hbq_entry_index;
3787
3788                                 /* Get a Mailbox buffer to setup mailbox
3789                                  * commands for HBA initialization
3790                                  */
3791         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3792
3793         if (!pmb)
3794                 return -ENOMEM;
3795
3796         pmbox = &pmb->u.mb;
3797
3798         /* Initialize the struct lpfc_sli_hbq structure for each hbq */
3799         phba->link_state = LPFC_INIT_MBX_CMDS;
3800         phba->hbq_in_use = 1;
3801
3802         hbq_entry_index = 0;
3803         for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
3804                 phba->hbqs[hbqno].next_hbqPutIdx = 0;
3805                 phba->hbqs[hbqno].hbqPutIdx      = 0;
3806                 phba->hbqs[hbqno].local_hbqGetIdx   = 0;
3807                 phba->hbqs[hbqno].entry_count =
3808                         lpfc_hbq_defs[hbqno]->entry_count;
3809                 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
3810                         hbq_entry_index, pmb);
3811                 hbq_entry_index += phba->hbqs[hbqno].entry_count;
3812
3813                 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
3814                         /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
3815                            mbxStatus <status>, ring <num> */
3816
3817                         lpfc_printf_log(phba, KERN_ERR,
3818                                         LOG_SLI | LOG_VPORT,
3819                                         "1805 Adapter failed to init. "
3820                                         "Data: x%x x%x x%x\n",
3821                                         pmbox->mbxCommand,
3822                                         pmbox->mbxStatus, hbqno);
3823
3824                         phba->link_state = LPFC_HBA_ERROR;
3825                         mempool_free(pmb, phba->mbox_mem_pool);
3826                         return -ENXIO;
3827                 }
3828         }
3829         phba->hbq_count = hbq_count;
3830
3831         mempool_free(pmb, phba->mbox_mem_pool);
3832
3833         /* Initially populate or replenish the HBQs */
3834         for (hbqno = 0; hbqno < hbq_count; ++hbqno)
3835                 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
3836         return 0;
3837 }
3838
3839 /**
3840  * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
3841  * @phba: Pointer to HBA context object.
3842  *
3843  * This function is called during the SLI initialization to configure
3844  * all the HBQs and post buffers to the HBQ. The caller is not
3845  * required to hold any locks. This function will return zero if successful
3846  * else it will return negative error code.
3847  **/
3848 static int
3849 lpfc_sli4_rb_setup(struct lpfc_hba *phba)
3850 {
3851         phba->hbq_in_use = 1;
3852         phba->hbqs[0].entry_count = lpfc_hbq_defs[0]->entry_count;
3853         phba->hbq_count = 1;
3854         /* Initially populate or replenish the HBQs */
3855         lpfc_sli_hbqbuf_init_hbqs(phba, 0);
3856         return 0;
3857 }
3858
3859 /**
3860  * lpfc_sli_config_port - Issue config port mailbox command
3861  * @phba: Pointer to HBA context object.
3862  * @sli_mode: sli mode - 2/3
3863  *
3864  * This function is called by the sli intialization code path
3865  * to issue config_port mailbox command. This function restarts the
3866  * HBA firmware and issues a config_port mailbox command to configure
3867  * the SLI interface in the sli mode specified by sli_mode
3868  * variable. The caller is not required to hold any locks.
3869  * The function returns 0 if successful, else returns negative error
3870  * code.
3871  **/
3872 int
3873 lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
3874 {
3875         LPFC_MBOXQ_t *pmb;
3876         uint32_t resetcount = 0, rc = 0, done = 0;
3877
3878         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3879         if (!pmb) {
3880                 phba->link_state = LPFC_HBA_ERROR;
3881                 return -ENOMEM;
3882         }
3883
3884         phba->sli_rev = sli_mode;
3885         while (resetcount < 2 && !done) {
3886                 spin_lock_irq(&phba->hbalock);
3887                 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
3888                 spin_unlock_irq(&phba->hbalock);
3889                 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3890                 lpfc_sli_brdrestart(phba);
3891                 rc = lpfc_sli_chipset_init(phba);
3892                 if (rc)
3893                         break;
3894
3895                 spin_lock_irq(&phba->hbalock);
3896                 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
3897                 spin_unlock_irq(&phba->hbalock);
3898                 resetcount++;
3899
3900                 /* Call pre CONFIG_PORT mailbox command initialization.  A
3901                  * value of 0 means the call was successful.  Any other
3902                  * nonzero value is a failure, but if ERESTART is returned,
3903                  * the driver may reset the HBA and try again.
3904                  */
3905                 rc = lpfc_config_port_prep(phba);
3906                 if (rc == -ERESTART) {
3907                         phba->link_state = LPFC_LINK_UNKNOWN;
3908                         continue;
3909                 } else if (rc)
3910                         break;
3911                 phba->link_state = LPFC_INIT_MBX_CMDS;
3912                 lpfc_config_port(phba, pmb);
3913                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
3914                 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
3915                                         LPFC_SLI3_HBQ_ENABLED |
3916                                         LPFC_SLI3_CRP_ENABLED |
3917                                         LPFC_SLI3_BG_ENABLED);
3918                 if (rc != MBX_SUCCESS) {
3919                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3920                                 "0442 Adapter failed to init, mbxCmd x%x "
3921                                 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
3922                                 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
3923                         spin_lock_irq(&phba->hbalock);
3924                         phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
3925                         spin_unlock_irq(&phba->hbalock);
3926                         rc = -ENXIO;
3927                 } else {
3928                         /* Allow asynchronous mailbox command to go through */
3929                         spin_lock_irq(&phba->hbalock);
3930                         phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
3931                         spin_unlock_irq(&phba->hbalock);
3932                         done = 1;
3933                 }
3934         }
3935         if (!done) {
3936                 rc = -EINVAL;
3937                 goto do_prep_failed;
3938         }
3939         if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
3940                 if (!pmb->u.mb.un.varCfgPort.cMA) {
3941                         rc = -ENXIO;
3942                         goto do_prep_failed;
3943                 }
3944                 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
3945                         phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
3946                         phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
3947                         phba->max_vports = (phba->max_vpi > phba->max_vports) ?
3948                                 phba->max_vpi : phba->max_vports;
3949
3950                 } else
3951                         phba->max_vpi = 0;
3952                 if (pmb->u.mb.un.varCfgPort.gdss)
3953                         phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
3954                 if (pmb->u.mb.un.varCfgPort.gerbm)
3955                         phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
3956                 if (pmb->u.mb.un.varCfgPort.gcrp)
3957                         phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
3958
3959                 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
3960                 phba->port_gp = phba->mbox->us.s3_pgp.port;
3961
3962                 if (phba->cfg_enable_bg) {
3963                         if (pmb->u.mb.un.varCfgPort.gbg)
3964                                 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
3965                         else
3966                                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3967                                                 "0443 Adapter did not grant "
3968                                                 "BlockGuard\n");
3969                 }
3970         } else {
3971                 phba->hbq_get = NULL;
3972                 phba->port_gp = phba->mbox->us.s2.port;
3973                 phba->max_vpi = 0;
3974         }
3975 do_prep_failed:
3976         mempool_free(pmb, phba->mbox_mem_pool);
3977         return rc;
3978 }
3979
3980
3981 /**
3982  * lpfc_sli_hba_setup - SLI intialization function
3983  * @phba: Pointer to HBA context object.
3984  *
3985  * This function is the main SLI intialization function. This function
3986  * is called by the HBA intialization code, HBA reset code and HBA
3987  * error attention handler code. Caller is not required to hold any
3988  * locks. This function issues config_port mailbox command to configure
3989  * the SLI, setup iocb rings and HBQ rings. In the end the function
3990  * calls the config_port_post function to issue init_link mailbox
3991  * command and to start the discovery. The function will return zero
3992  * if successful, else it will return negative error code.
3993  **/
3994 int
3995 lpfc_sli_hba_setup(struct lpfc_hba *phba)
3996 {
3997         uint32_t rc;
3998         int  mode = 3;
3999
4000         switch (lpfc_sli_mode) {
4001         case 2:
4002                 if (phba->cfg_enable_npiv) {
4003                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
4004                                 "1824 NPIV enabled: Override lpfc_sli_mode "
4005                                 "parameter (%d) to auto (0).\n",
4006                                 lpfc_sli_mode);
4007                         break;
4008                 }
4009                 mode = 2;
4010                 break;
4011         case 0:
4012         case 3:
4013                 break;
4014         default:
4015                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
4016                                 "1819 Unrecognized lpfc_sli_mode "
4017                                 "parameter: %d.\n", lpfc_sli_mode);
4018
4019                 break;
4020         }
4021
4022         rc = lpfc_sli_config_port(phba, mode);
4023
4024         if (rc && lpfc_sli_mode == 3)
4025                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
4026                                 "1820 Unable to select SLI-3.  "
4027                                 "Not supported by adapter.\n");
4028         if (rc && mode != 2)
4029                 rc = lpfc_sli_config_port(phba, 2);
4030         if (rc)
4031                 goto lpfc_sli_hba_setup_error;
4032
4033         /* Enable PCIe device Advanced Error Reporting (AER) if configured */
4034         if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
4035                 rc = pci_enable_pcie_error_reporting(phba->pcidev);
4036                 if (!rc) {
4037                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4038                                         "2709 This device supports "
4039                                         "Advanced Error Reporting (AER)\n");
4040                         spin_lock_irq(&phba->hbalock);
4041                         phba->hba_flag |= HBA_AER_ENABLED;
4042                         spin_unlock_irq(&phba->hbalock);
4043                 } else {
4044                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4045                                         "2708 This device does not support "
4046                                         "Advanced Error Reporting (AER)\n");
4047                         phba->cfg_aer_support = 0;
4048                 }
4049         }
4050
4051         if (phba->sli_rev == 3) {
4052                 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
4053                 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
4054         } else {
4055                 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
4056                 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
4057                 phba->sli3_options = 0;
4058         }
4059
4060         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4061                         "0444 Firmware in SLI %x mode. Max_vpi %d\n",
4062                         phba->sli_rev, phba->max_vpi);
4063         rc = lpfc_sli_ring_map(phba);
4064
4065         if (rc)
4066                 goto lpfc_sli_hba_setup_error;
4067
4068         /* Init HBQs */
4069         if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
4070                 rc = lpfc_sli_hbq_setup(phba);
4071                 if (rc)
4072                         goto lpfc_sli_hba_setup_error;
4073         }
4074         spin_lock_irq(&phba->hbalock);
4075         phba->sli.sli_flag |= LPFC_PROCESS_LA;
4076         spin_unlock_irq(&phba->hbalock);
4077
4078         rc = lpfc_config_port_post(phba);
4079         if (rc)
4080                 goto lpfc_sli_hba_setup_error;
4081
4082         return rc;
4083
4084 lpfc_sli_hba_setup_error:
4085         phba->link_state = LPFC_HBA_ERROR;
4086         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4087                         "0445 Firmware initialization failed\n");
4088         return rc;
4089 }
4090
4091 /**
4092  * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
4093  * @phba: Pointer to HBA context object.
4094  * @mboxq: mailbox pointer.
4095  * This function issue a dump mailbox command to read config region
4096  * 23 and parse the records in the region and populate driver
4097  * data structure.
4098  **/
4099 static int
4100 lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba,
4101                 LPFC_MBOXQ_t *mboxq)
4102 {
4103         struct lpfc_dmabuf *mp;
4104         struct lpfc_mqe *mqe;
4105         uint32_t data_length;
4106         int rc;
4107
4108         /* Program the default value of vlan_id and fc_map */
4109         phba->valid_vlan = 0;
4110         phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4111         phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4112         phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
4113
4114         mqe = &mboxq->u.mqe;
4115         if (lpfc_dump_fcoe_param(phba, mboxq))
4116                 return -ENOMEM;
4117
4118         mp = (struct lpfc_dmabuf *) mboxq->context1;
4119         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4120
4121         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4122                         "(%d):2571 Mailbox cmd x%x Status x%x "
4123                         "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4124                         "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4125                         "CQ: x%x x%x x%x x%x\n",
4126                         mboxq->vport ? mboxq->vport->vpi : 0,
4127                         bf_get(lpfc_mqe_command, mqe),
4128                         bf_get(lpfc_mqe_status, mqe),
4129                         mqe->un.mb_words[0], mqe->un.mb_words[1],
4130                         mqe->un.mb_words[2], mqe->un.mb_words[3],
4131                         mqe->un.mb_words[4], mqe->un.mb_words[5],
4132                         mqe->un.mb_words[6], mqe->un.mb_words[7],
4133                         mqe->un.mb_words[8], mqe->un.mb_words[9],
4134                         mqe->un.mb_words[10], mqe->un.mb_words[11],
4135                         mqe->un.mb_words[12], mqe->un.mb_words[13],
4136                         mqe->un.mb_words[14], mqe->un.mb_words[15],
4137                         mqe->un.mb_words[16], mqe->un.mb_words[50],
4138                         mboxq->mcqe.word0,
4139                         mboxq->mcqe.mcqe_tag0,  mboxq->mcqe.mcqe_tag1,
4140                         mboxq->mcqe.trailer);
4141
4142         if (rc) {
4143                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4144                 kfree(mp);
4145                 return -EIO;
4146         }
4147         data_length = mqe->un.mb_words[5];
4148         if (data_length > DMP_RGN23_SIZE) {
4149                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4150                 kfree(mp);
4151                 return -EIO;
4152         }
4153
4154         lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
4155         lpfc_mbuf_free(phba, mp->virt, mp->phys);
4156         kfree(mp);
4157         return 0;
4158 }
4159
4160 /**
4161  * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
4162  * @phba: pointer to lpfc hba data structure.
4163  * @mboxq: pointer to the LPFC_MBOXQ_t structure.
4164  * @vpd: pointer to the memory to hold resulting port vpd data.
4165  * @vpd_size: On input, the number of bytes allocated to @vpd.
4166  *            On output, the number of data bytes in @vpd.
4167  *
4168  * This routine executes a READ_REV SLI4 mailbox command.  In
4169  * addition, this routine gets the port vpd data.
4170  *
4171  * Return codes
4172  *      0 - successful
4173  *      ENOMEM - could not allocated memory.
4174  **/
4175 static int
4176 lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
4177                     uint8_t *vpd, uint32_t *vpd_size)
4178 {
4179         int rc = 0;
4180         uint32_t dma_size;
4181         struct lpfc_dmabuf *dmabuf;
4182         struct lpfc_mqe *mqe;
4183
4184         dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4185         if (!dmabuf)
4186                 return -ENOMEM;
4187
4188         /*
4189          * Get a DMA buffer for the vpd data resulting from the READ_REV
4190          * mailbox command.
4191          */
4192         dma_size = *vpd_size;
4193         dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
4194                                           dma_size,
4195                                           &dmabuf->phys,
4196                                           GFP_KERNEL);
4197         if (!dmabuf->virt) {
4198                 kfree(dmabuf);
4199                 return -ENOMEM;
4200         }
4201         memset(dmabuf->virt, 0, dma_size);
4202
4203         /*
4204          * The SLI4 implementation of READ_REV conflicts at word1,
4205          * bits 31:16 and SLI4 adds vpd functionality not present
4206          * in SLI3.  This code corrects the conflicts.
4207          */
4208         lpfc_read_rev(phba, mboxq);
4209         mqe = &mboxq->u.mqe;
4210         mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
4211         mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
4212         mqe->un.read_rev.word1 &= 0x0000FFFF;
4213         bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
4214         bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
4215
4216         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4217         if (rc) {
4218                 dma_free_coherent(&phba->pcidev->dev, dma_size,
4219                                   dmabuf->virt, dmabuf->phys);
4220                 kfree(dmabuf);
4221                 return -EIO;
4222         }
4223
4224         /*
4225          * The available vpd length cannot be bigger than the
4226          * DMA buffer passed to the port.  Catch the less than
4227          * case and update the caller's size.
4228          */
4229         if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
4230                 *vpd_size = mqe->un.read_rev.avail_vpd_len;
4231
4232         memcpy(vpd, dmabuf->virt, *vpd_size);
4233
4234         dma_free_coherent(&phba->pcidev->dev, dma_size,
4235                           dmabuf->virt, dmabuf->phys);
4236         kfree(dmabuf);
4237         return 0;
4238 }
4239
4240 /**
4241  * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
4242  * @phba: pointer to lpfc hba data structure.
4243  *
4244  * This routine is called to explicitly arm the SLI4 device's completion and
4245  * event queues
4246  **/
4247 static void
4248 lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
4249 {
4250         uint8_t fcp_eqidx;
4251
4252         lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
4253         lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
4254         for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++)
4255                 lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[fcp_eqidx],
4256                                      LPFC_QUEUE_REARM);
4257         lpfc_sli4_eq_release(phba->sli4_hba.sp_eq, LPFC_QUEUE_REARM);
4258         for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++)
4259                 lpfc_sli4_eq_release(phba->sli4_hba.fp_eq[fcp_eqidx],
4260                                      LPFC_QUEUE_REARM);
4261 }
4262
4263 /**
4264  * lpfc_sli4_hba_setup - SLI4 device intialization PCI function
4265  * @phba: Pointer to HBA context object.
4266  *
4267  * This function is the main SLI4 device intialization PCI function. This
4268  * function is called by the HBA intialization code, HBA reset code and
4269  * HBA error attention handler code. Caller is not required to hold any
4270  * locks.
4271  **/
4272 int
4273 lpfc_sli4_hba_setup(struct lpfc_hba *phba)
4274 {
4275         int rc;
4276         LPFC_MBOXQ_t *mboxq;
4277         struct lpfc_mqe *mqe;
4278         uint8_t *vpd;
4279         uint32_t vpd_size;
4280         uint32_t ftr_rsp = 0;
4281         struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
4282         struct lpfc_vport *vport = phba->pport;
4283         struct lpfc_dmabuf *mp;
4284
4285         /* Perform a PCI function reset to start from clean */
4286         rc = lpfc_pci_function_reset(phba);
4287         if (unlikely(rc))
4288                 return -ENODEV;
4289
4290         /* Check the HBA Host Status Register for readyness */
4291         rc = lpfc_sli4_post_status_check(phba);
4292         if (unlikely(rc))
4293                 return -ENODEV;
4294         else {
4295                 spin_lock_irq(&phba->hbalock);
4296                 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
4297                 spin_unlock_irq(&phba->hbalock);
4298         }
4299
4300         /*
4301          * Allocate a single mailbox container for initializing the
4302          * port.
4303          */
4304         mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4305         if (!mboxq)
4306                 return -ENOMEM;
4307
4308         /*
4309          * Continue initialization with default values even if driver failed
4310          * to read FCoE param config regions
4311          */
4312         if (lpfc_sli4_read_fcoe_params(phba, mboxq))
4313                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
4314                         "2570 Failed to read FCoE parameters\n");
4315
4316         /* Issue READ_REV to collect vpd and FW information. */
4317         vpd_size = SLI4_PAGE_SIZE;
4318         vpd = kzalloc(vpd_size, GFP_KERNEL);
4319         if (!vpd) {
4320                 rc = -ENOMEM;
4321                 goto out_free_mbox;
4322         }
4323
4324         rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
4325         if (unlikely(rc))
4326                 goto out_free_vpd;
4327
4328         mqe = &mboxq->u.mqe;
4329         phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
4330         if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev))
4331                 phba->hba_flag |= HBA_FCOE_SUPPORT;
4332
4333         if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
4334                 LPFC_DCBX_CEE_MODE)
4335                 phba->hba_flag |= HBA_FIP_SUPPORT;
4336         else
4337                 phba->hba_flag &= ~HBA_FIP_SUPPORT;
4338
4339         if (phba->sli_rev != LPFC_SLI_REV4 ||
4340             !(phba->hba_flag & HBA_FCOE_SUPPORT)) {
4341                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4342                         "0376 READ_REV Error. SLI Level %d "
4343                         "FCoE enabled %d\n",
4344                         phba->sli_rev, phba->hba_flag & HBA_FCOE_SUPPORT);
4345                 rc = -EIO;
4346                 goto out_free_vpd;
4347         }
4348         /*
4349          * Evaluate the read rev and vpd data. Populate the driver
4350          * state with the results. If this routine fails, the failure
4351          * is not fatal as the driver will use generic values.
4352          */
4353         rc = lpfc_parse_vpd(phba, vpd, vpd_size);
4354         if (unlikely(!rc)) {
4355                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4356                                 "0377 Error %d parsing vpd. "
4357                                 "Using defaults.\n", rc);
4358                 rc = 0;
4359         }
4360
4361         /* Save information as VPD data */
4362         phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
4363         phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
4364         phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
4365         phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
4366                                          &mqe->un.read_rev);
4367         phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
4368                                        &mqe->un.read_rev);
4369         phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
4370                                             &mqe->un.read_rev);
4371         phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
4372                                            &mqe->un.read_rev);
4373         phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
4374         memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
4375         phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
4376         memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
4377         phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
4378         memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
4379         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4380                         "(%d):0380 READ_REV Status x%x "
4381                         "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
4382                         mboxq->vport ? mboxq->vport->vpi : 0,
4383                         bf_get(lpfc_mqe_status, mqe),
4384                         phba->vpd.rev.opFwName,
4385                         phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
4386                         phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
4387
4388         /*
4389          * Discover the port's supported feature set and match it against the
4390          * hosts requests.
4391          */
4392         lpfc_request_features(phba, mboxq);
4393         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4394         if (unlikely(rc)) {
4395                 rc = -EIO;
4396                 goto out_free_vpd;
4397         }
4398
4399         /*
4400          * The port must support FCP initiator mode as this is the
4401          * only mode running in the host.
4402          */
4403         if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
4404                 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
4405                                 "0378 No support for fcpi mode.\n");
4406                 ftr_rsp++;
4407         }
4408
4409         /*
4410          * If the port cannot support the host's requested features
4411          * then turn off the global config parameters to disable the
4412          * feature in the driver.  This is not a fatal error.
4413          */
4414         if ((phba->cfg_enable_bg) &&
4415             !(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
4416                 ftr_rsp++;
4417
4418         if (phba->max_vpi && phba->cfg_enable_npiv &&
4419             !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
4420                 ftr_rsp++;
4421
4422         if (ftr_rsp) {
4423                 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
4424                                 "0379 Feature Mismatch Data: x%08x %08x "
4425                                 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
4426                                 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
4427                                 phba->cfg_enable_npiv, phba->max_vpi);
4428                 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
4429                         phba->cfg_enable_bg = 0;
4430                 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
4431                         phba->cfg_enable_npiv = 0;
4432         }
4433
4434         /* These SLI3 features are assumed in SLI4 */
4435         spin_lock_irq(&phba->hbalock);
4436         phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
4437         spin_unlock_irq(&phba->hbalock);
4438
4439         /* Read the port's service parameters. */
4440         rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
4441         if (rc) {
4442                 phba->link_state = LPFC_HBA_ERROR;
4443                 rc = -ENOMEM;
4444                 goto out_free_vpd;
4445         }
4446
4447         mboxq->vport = vport;
4448         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4449         mp = (struct lpfc_dmabuf *) mboxq->context1;
4450         if (rc == MBX_SUCCESS) {
4451                 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
4452                 rc = 0;
4453         }
4454
4455         /*
4456          * This memory was allocated by the lpfc_read_sparam routine. Release
4457          * it to the mbuf pool.
4458          */
4459         lpfc_mbuf_free(phba, mp->virt, mp->phys);
4460         kfree(mp);
4461         mboxq->context1 = NULL;
4462         if (unlikely(rc)) {
4463                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4464                                 "0382 READ_SPARAM command failed "
4465                                 "status %d, mbxStatus x%x\n",
4466                                 rc, bf_get(lpfc_mqe_status, mqe));
4467                 phba->link_state = LPFC_HBA_ERROR;
4468                 rc = -EIO;
4469                 goto out_free_vpd;
4470         }
4471
4472         if (phba->cfg_soft_wwnn)
4473                 u64_to_wwn(phba->cfg_soft_wwnn,
4474                            vport->fc_sparam.nodeName.u.wwn);
4475         if (phba->cfg_soft_wwpn)
4476                 u64_to_wwn(phba->cfg_soft_wwpn,
4477                            vport->fc_sparam.portName.u.wwn);
4478         memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
4479                sizeof(struct lpfc_name));
4480         memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
4481                sizeof(struct lpfc_name));
4482
4483         /* Update the fc_host data structures with new wwn. */
4484         fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
4485         fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
4486
4487         /* Register SGL pool to the device using non-embedded mailbox command */
4488         rc = lpfc_sli4_post_sgl_list(phba);
4489         if (unlikely(rc)) {
4490                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4491                                 "0582 Error %d during sgl post operation\n",
4492                                         rc);
4493                 rc = -ENODEV;
4494                 goto out_free_vpd;
4495         }
4496
4497         /* Register SCSI SGL pool to the device */
4498         rc = lpfc_sli4_repost_scsi_sgl_list(phba);
4499         if (unlikely(rc)) {
4500                 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
4501                                 "0383 Error %d during scsi sgl post "
4502                                 "operation\n", rc);
4503                 /* Some Scsi buffers were moved to the abort scsi list */
4504                 /* A pci function reset will repost them */
4505                 rc = -ENODEV;
4506                 goto out_free_vpd;
4507         }
4508
4509         /* Post the rpi header region to the device. */
4510         rc = lpfc_sli4_post_all_rpi_hdrs(phba);
4511         if (unlikely(rc)) {
4512                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4513                                 "0393 Error %d during rpi post operation\n",
4514                                 rc);
4515                 rc = -ENODEV;
4516                 goto out_free_vpd;
4517         }
4518
4519         /* Set up all the queues to the device */
4520         rc = lpfc_sli4_queue_setup(phba);
4521         if (unlikely(rc)) {
4522                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4523                                 "0381 Error %d during queue setup.\n ", rc);
4524                 goto out_stop_timers;
4525         }
4526
4527         /* Arm the CQs and then EQs on device */
4528         lpfc_sli4_arm_cqeq_intr(phba);
4529
4530         /* Indicate device interrupt mode */
4531         phba->sli4_hba.intr_enable = 1;
4532
4533         /* Allow asynchronous mailbox command to go through */
4534         spin_lock_irq(&phba->hbalock);
4535         phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4536         spin_unlock_irq(&phba->hbalock);
4537
4538         /* Post receive buffers to the device */
4539         lpfc_sli4_rb_setup(phba);
4540
4541         /* Reset HBA FCF states after HBA reset */
4542         phba->fcf.fcf_flag = 0;
4543         phba->fcf.current_rec.flag = 0;
4544
4545         /* Start the ELS watchdog timer */
4546         mod_timer(&vport->els_tmofunc,
4547                   jiffies + HZ * (phba->fc_ratov * 2));
4548
4549         /* Start heart beat timer */
4550         mod_timer(&phba->hb_tmofunc,
4551                   jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
4552         phba->hb_outstanding = 0;
4553         phba->last_completion_time = jiffies;
4554
4555         /* Start error attention (ERATT) polling timer */
4556         mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
4557
4558         /* Enable PCIe device Advanced Error Reporting (AER) if configured */
4559         if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
4560                 rc = pci_enable_pcie_error_reporting(phba->pcidev);
4561                 if (!rc) {
4562                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4563                                         "2829 This device supports "
4564                                         "Advanced Error Reporting (AER)\n");
4565                         spin_lock_irq(&phba->hbalock);
4566                         phba->hba_flag |= HBA_AER_ENABLED;
4567                         spin_unlock_irq(&phba->hbalock);
4568                 } else {
4569                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4570                                         "2830 This device does not support "
4571                                         "Advanced Error Reporting (AER)\n");
4572                         phba->cfg_aer_support = 0;
4573                 }
4574         }
4575
4576         /*
4577          * The port is ready, set the host's link state to LINK_DOWN
4578          * in preparation for link interrupts.
4579          */
4580         lpfc_init_link(phba, mboxq, phba->cfg_topology, phba->cfg_link_speed);
4581         mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4582         lpfc_set_loopback_flag(phba);
4583         /* Change driver state to LPFC_LINK_DOWN right before init link */
4584         spin_lock_irq(&phba->hbalock);
4585         phba->link_state = LPFC_LINK_DOWN;
4586         spin_unlock_irq(&phba->hbalock);
4587         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
4588         if (unlikely(rc != MBX_NOT_FINISHED)) {
4589                 kfree(vpd);
4590                 return 0;
4591         } else
4592                 rc = -EIO;
4593
4594         /* Unset all the queues set up in this routine when error out */
4595         if (rc)
4596                 lpfc_sli4_queue_unset(phba);
4597
4598 out_stop_timers:
4599         if (rc)
4600                 lpfc_stop_hba_timers(phba);
4601 out_free_vpd:
4602         kfree(vpd);
4603 out_free_mbox:
4604         mempool_free(mboxq, phba->mbox_mem_pool);
4605         return rc;
4606 }
4607
4608 /**
4609  * lpfc_mbox_timeout - Timeout call back function for mbox timer
4610  * @ptr: context object - pointer to hba structure.
4611  *
4612  * This is the callback function for mailbox timer. The mailbox
4613  * timer is armed when a new mailbox command is issued and the timer
4614  * is deleted when the mailbox complete. The function is called by
4615  * the kernel timer code when a mailbox does not complete within
4616  * expected time. This function wakes up the worker thread to
4617  * process the mailbox timeout and returns. All the processing is
4618  * done by the worker thread function lpfc_mbox_timeout_handler.
4619  **/
4620 void
4621 lpfc_mbox_timeout(unsigned long ptr)
4622 {
4623         struct lpfc_hba  *phba = (struct lpfc_hba *) ptr;
4624         unsigned long iflag;
4625         uint32_t tmo_posted;
4626
4627         spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
4628         tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
4629         if (!tmo_posted)
4630                 phba->pport->work_port_events |= WORKER_MBOX_TMO;
4631         spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
4632
4633         if (!tmo_posted)
4634                 lpfc_worker_wake_up(phba);
4635         return;
4636 }
4637
4638
4639 /**
4640  * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
4641  * @phba: Pointer to HBA context object.
4642  *
4643  * This function is called from worker thread when a mailbox command times out.
4644  * The caller is not required to hold any locks. This function will reset the
4645  * HBA and recover all the pending commands.
4646  **/
4647 void
4648 lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
4649 {
4650         LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
4651         MAILBOX_t *mb = &pmbox->u.mb;
4652         struct lpfc_sli *psli = &phba->sli;
4653         struct lpfc_sli_ring *pring;
4654
4655         /* Check the pmbox pointer first.  There is a race condition
4656          * between the mbox timeout handler getting executed in the
4657          * worklist and the mailbox actually completing. When this
4658          * race condition occurs, the mbox_active will be NULL.
4659          */
4660         spin_lock_irq(&phba->hbalock);
4661         if (pmbox == NULL) {
4662                 lpfc_printf_log(phba, KERN_WARNING,
4663                                 LOG_MBOX | LOG_SLI,
4664                                 "0353 Active Mailbox cleared - mailbox timeout "
4665                                 "exiting\n");
4666                 spin_unlock_irq(&phba->hbalock);
4667                 return;
4668         }
4669
4670         /* Mbox cmd <mbxCommand> timeout */
4671         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4672                         "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
4673                         mb->mbxCommand,
4674                         phba->pport->port_state,
4675                         phba->sli.sli_flag,
4676                         phba->sli.mbox_active);
4677         spin_unlock_irq(&phba->hbalock);
4678
4679         /* Setting state unknown so lpfc_sli_abort_iocb_ring
4680          * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
4681          * it to fail all oustanding SCSI IO.
4682          */
4683         spin_lock_irq(&phba->pport->work_port_lock);
4684         phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
4685         spin_unlock_irq(&phba->pport->work_port_lock);
4686         spin_lock_irq(&phba->hbalock);
4687         phba->link_state = LPFC_LINK_UNKNOWN;
4688         psli->sli_flag &= ~LPFC_SLI_ACTIVE;
4689         spin_unlock_irq(&phba->hbalock);
4690
4691         pring = &psli->ring[psli->fcp_ring];
4692         lpfc_sli_abort_iocb_ring(phba, pring);
4693
4694         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4695                         "0345 Resetting board due to mailbox timeout\n");
4696
4697         /* Reset the HBA device */
4698         lpfc_reset_hba(phba);
4699 }
4700
4701 /**
4702  * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
4703  * @phba: Pointer to HBA context object.
4704  * @pmbox: Pointer to mailbox object.
4705  * @flag: Flag indicating how the mailbox need to be processed.
4706  *
4707  * This function is called by discovery code and HBA management code
4708  * to submit a mailbox command to firmware with SLI-3 interface spec. This
4709  * function gets the hbalock to protect the data structures.
4710  * The mailbox command can be submitted in polling mode, in which case
4711  * this function will wait in a polling loop for the completion of the
4712  * mailbox.
4713  * If the mailbox is submitted in no_wait mode (not polling) the
4714  * function will submit the command and returns immediately without waiting
4715  * for the mailbox completion. The no_wait is supported only when HBA
4716  * is in SLI2/SLI3 mode - interrupts are enabled.
4717  * The SLI interface allows only one mailbox pending at a time. If the
4718  * mailbox is issued in polling mode and there is already a mailbox
4719  * pending, then the function will return an error. If the mailbox is issued
4720  * in NO_WAIT mode and there is a mailbox pending already, the function
4721  * will return MBX_BUSY after queuing the mailbox into mailbox queue.
4722  * The sli layer owns the mailbox object until the completion of mailbox
4723  * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
4724  * return codes the caller owns the mailbox command after the return of
4725  * the function.
4726  **/
4727 static int
4728 lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
4729                        uint32_t flag)
4730 {
4731         MAILBOX_t *mb;
4732         struct lpfc_sli *psli = &phba->sli;
4733         uint32_t status, evtctr;
4734         uint32_t ha_copy;
4735         int i;
4736         unsigned long timeout;
4737         unsigned long drvr_flag = 0;
4738         uint32_t word0, ldata;
4739         void __iomem *to_slim;
4740         int processing_queue = 0;
4741
4742         spin_lock_irqsave(&phba->hbalock, drvr_flag);
4743         if (!pmbox) {
4744                 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
4745                 /* processing mbox queue from intr_handler */
4746                 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
4747                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4748                         return MBX_SUCCESS;
4749                 }
4750                 processing_queue = 1;
4751                 pmbox = lpfc_mbox_get(phba);
4752                 if (!pmbox) {
4753                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4754                         return MBX_SUCCESS;
4755                 }
4756         }
4757
4758         if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
4759                 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
4760                 if(!pmbox->vport) {
4761                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4762                         lpfc_printf_log(phba, KERN_ERR,
4763                                         LOG_MBOX | LOG_VPORT,
4764                                         "1806 Mbox x%x failed. No vport\n",
4765                                         pmbox->u.mb.mbxCommand);
4766                         dump_stack();
4767                         goto out_not_finished;
4768                 }
4769         }
4770
4771         /* If the PCI channel is in offline state, do not post mbox. */
4772         if (unlikely(pci_channel_offline(phba->pcidev))) {
4773                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4774                 goto out_not_finished;
4775         }
4776
4777         /* If HBA has a deferred error attention, fail the iocb. */
4778         if (unlikely(phba->hba_flag & DEFER_ERATT)) {
4779                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4780                 goto out_not_finished;
4781         }
4782
4783         psli = &phba->sli;
4784
4785         mb = &pmbox->u.mb;
4786         status = MBX_SUCCESS;
4787
4788         if (phba->link_state == LPFC_HBA_ERROR) {
4789                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4790
4791                 /* Mbox command <mbxCommand> cannot issue */
4792                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4793                                 "(%d):0311 Mailbox command x%x cannot "
4794                                 "issue Data: x%x x%x\n",
4795                                 pmbox->vport ? pmbox->vport->vpi : 0,
4796                                 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
4797                 goto out_not_finished;
4798         }
4799
4800         if (mb->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT &&
4801             !(readl(phba->HCregaddr) & HC_MBINT_ENA)) {
4802                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4803                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4804                                 "(%d):2528 Mailbox command x%x cannot "
4805                                 "issue Data: x%x x%x\n",
4806                                 pmbox->vport ? pmbox->vport->vpi : 0,
4807                                 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
4808                 goto out_not_finished;
4809         }
4810
4811         if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
4812                 /* Polling for a mbox command when another one is already active
4813                  * is not allowed in SLI. Also, the driver must have established
4814                  * SLI2 mode to queue and process multiple mbox commands.
4815                  */
4816
4817                 if (flag & MBX_POLL) {
4818                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4819
4820                         /* Mbox command <mbxCommand> cannot issue */
4821                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4822                                         "(%d):2529 Mailbox command x%x "
4823                                         "cannot issue Data: x%x x%x\n",
4824                                         pmbox->vport ? pmbox->vport->vpi : 0,
4825                                         pmbox->u.mb.mbxCommand,
4826                                         psli->sli_flag, flag);
4827                         goto out_not_finished;
4828                 }
4829
4830                 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
4831                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4832                         /* Mbox command <mbxCommand> cannot issue */
4833                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4834                                         "(%d):2530 Mailbox command x%x "
4835                                         "cannot issue Data: x%x x%x\n",
4836                                         pmbox->vport ? pmbox->vport->vpi : 0,
4837                                         pmbox->u.mb.mbxCommand,
4838                                         psli->sli_flag, flag);
4839                         goto out_not_finished;
4840                 }
4841
4842                 /* Another mailbox command is still being processed, queue this
4843                  * command to be processed later.
4844                  */
4845                 lpfc_mbox_put(phba, pmbox);
4846
4847                 /* Mbox cmd issue - BUSY */
4848                 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4849                                 "(%d):0308 Mbox cmd issue - BUSY Data: "
4850                                 "x%x x%x x%x x%x\n",
4851                                 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
4852                                 mb->mbxCommand, phba->pport->port_state,
4853                                 psli->sli_flag, flag);
4854
4855                 psli->slistat.mbox_busy++;
4856                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4857
4858                 if (pmbox->vport) {
4859                         lpfc_debugfs_disc_trc(pmbox->vport,
4860                                 LPFC_DISC_TRC_MBOX_VPORT,
4861                                 "MBOX Bsy vport:  cmd:x%x mb:x%x x%x",
4862                                 (uint32_t)mb->mbxCommand,
4863                                 mb->un.varWords[0], mb->un.varWords[1]);
4864                 }
4865                 else {
4866                         lpfc_debugfs_disc_trc(phba->pport,
4867                                 LPFC_DISC_TRC_MBOX,
4868                                 "MBOX Bsy:        cmd:x%x mb:x%x x%x",
4869                                 (uint32_t)mb->mbxCommand,
4870                                 mb->un.varWords[0], mb->un.varWords[1]);
4871                 }
4872
4873                 return MBX_BUSY;
4874         }
4875
4876         psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
4877
4878         /* If we are not polling, we MUST be in SLI2 mode */
4879         if (flag != MBX_POLL) {
4880                 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
4881                     (mb->mbxCommand != MBX_KILL_BOARD)) {
4882                         psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
4883                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4884                         /* Mbox command <mbxCommand> cannot issue */
4885                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4886                                         "(%d):2531 Mailbox command x%x "
4887                                         "cannot issue Data: x%x x%x\n",
4888                                         pmbox->vport ? pmbox->vport->vpi : 0,
4889                                         pmbox->u.mb.mbxCommand,
4890                                         psli->sli_flag, flag);
4891                         goto out_not_finished;
4892                 }
4893                 /* timeout active mbox command */
4894                 mod_timer(&psli->mbox_tmo, (jiffies +
4895                                (HZ * lpfc_mbox_tmo_val(phba, mb->mbxCommand))));
4896         }
4897
4898         /* Mailbox cmd <cmd> issue */
4899         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4900                         "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
4901                         "x%x\n",
4902                         pmbox->vport ? pmbox->vport->vpi : 0,
4903                         mb->mbxCommand, phba->pport->port_state,
4904                         psli->sli_flag, flag);
4905
4906         if (mb->mbxCommand != MBX_HEARTBEAT) {
4907                 if (pmbox->vport) {
4908                         lpfc_debugfs_disc_trc(pmbox->vport,
4909                                 LPFC_DISC_TRC_MBOX_VPORT,
4910                                 "MBOX Send vport: cmd:x%x mb:x%x x%x",
4911                                 (uint32_t)mb->mbxCommand,
4912                                 mb->un.varWords[0], mb->un.varWords[1]);
4913                 }
4914                 else {
4915                         lpfc_debugfs_disc_trc(phba->pport,
4916                                 LPFC_DISC_TRC_MBOX,
4917                                 "MBOX Send:       cmd:x%x mb:x%x x%x",
4918                                 (uint32_t)mb->mbxCommand,
4919                                 mb->un.varWords[0], mb->un.varWords[1]);
4920                 }
4921         }
4922
4923         psli->slistat.mbox_cmd++;
4924         evtctr = psli->slistat.mbox_event;
4925
4926         /* next set own bit for the adapter and copy over command word */
4927         mb->mbxOwner = OWN_CHIP;
4928
4929         if (psli->sli_flag & LPFC_SLI_ACTIVE) {
4930                 /* Populate mbox extension offset word. */
4931                 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
4932                         *(((uint32_t *)mb) + pmbox->mbox_offset_word)
4933                                 = (uint8_t *)phba->mbox_ext
4934                                   - (uint8_t *)phba->mbox;
4935                 }
4936
4937                 /* Copy the mailbox extension data */
4938                 if (pmbox->in_ext_byte_len && pmbox->context2) {
4939                         lpfc_sli_pcimem_bcopy(pmbox->context2,
4940                                 (uint8_t *)phba->mbox_ext,
4941                                 pmbox->in_ext_byte_len);
4942                 }
4943                 /* Copy command data to host SLIM area */
4944                 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
4945         } else {
4946                 /* Populate mbox extension offset word. */
4947                 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
4948                         *(((uint32_t *)mb) + pmbox->mbox_offset_word)
4949                                 = MAILBOX_HBA_EXT_OFFSET;
4950
4951                 /* Copy the mailbox extension data */
4952                 if (pmbox->in_ext_byte_len && pmbox->context2) {
4953                         lpfc_memcpy_to_slim(phba->MBslimaddr +
4954                                 MAILBOX_HBA_EXT_OFFSET,
4955                                 pmbox->context2, pmbox->in_ext_byte_len);
4956
4957                 }
4958                 if (mb->mbxCommand == MBX_CONFIG_PORT) {
4959                         /* copy command data into host mbox for cmpl */
4960                         lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
4961                 }
4962
4963                 /* First copy mbox command data to HBA SLIM, skip past first
4964                    word */
4965                 to_slim = phba->MBslimaddr + sizeof (uint32_t);
4966                 lpfc_memcpy_to_slim(to_slim, &mb->un.varWords[0],
4967                             MAILBOX_CMD_SIZE - sizeof (uint32_t));
4968
4969                 /* Next copy over first word, with mbxOwner set */
4970                 ldata = *((uint32_t *)mb);
4971                 to_slim = phba->MBslimaddr;
4972                 writel(ldata, to_slim);
4973                 readl(to_slim); /* flush */
4974
4975                 if (mb->mbxCommand == MBX_CONFIG_PORT) {
4976                         /* switch over to host mailbox */
4977                         psli->sli_flag |= LPFC_SLI_ACTIVE;
4978                 }
4979         }
4980
4981         wmb();
4982
4983         switch (flag) {
4984         case MBX_NOWAIT:
4985                 /* Set up reference to mailbox command */
4986                 psli->mbox_active = pmbox;
4987                 /* Interrupt board to do it */
4988                 writel(CA_MBATT, phba->CAregaddr);
4989                 readl(phba->CAregaddr); /* flush */
4990                 /* Don't wait for it to finish, just return */
4991                 break;
4992
4993         case MBX_POLL:
4994                 /* Set up null reference to mailbox command */
4995                 psli->mbox_active = NULL;
4996                 /* Interrupt board to do it */
4997                 writel(CA_MBATT, phba->CAregaddr);
4998                 readl(phba->CAregaddr); /* flush */
4999
5000                 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
5001                         /* First read mbox status word */
5002                         word0 = *((uint32_t *)phba->mbox);
5003                         word0 = le32_to_cpu(word0);
5004                 } else {
5005                         /* First read mbox status word */
5006                         word0 = readl(phba->MBslimaddr);
5007                 }
5008
5009                 /* Read the HBA Host Attention Register */
5010                 ha_copy = readl(phba->HAregaddr);
5011                 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
5012                                                              mb->mbxCommand) *
5013                                            1000) + jiffies;
5014                 i = 0;
5015                 /* Wait for command to complete */
5016                 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
5017                        (!(ha_copy & HA_MBATT) &&
5018                         (phba->link_state > LPFC_WARM_START))) {
5019                         if (time_after(jiffies, timeout)) {
5020                                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5021                                 spin_unlock_irqrestore(&phba->hbalock,
5022                                                        drvr_flag);
5023                                 goto out_not_finished;
5024                         }
5025
5026                         /* Check if we took a mbox interrupt while we were
5027                            polling */
5028                         if (((word0 & OWN_CHIP) != OWN_CHIP)
5029                             && (evtctr != psli->slistat.mbox_event))
5030                                 break;
5031
5032                         if (i++ > 10) {
5033                                 spin_unlock_irqrestore(&phba->hbalock,
5034                                                        drvr_flag);
5035                                 msleep(1);
5036                                 spin_lock_irqsave(&phba->hbalock, drvr_flag);
5037                         }
5038
5039                         if (psli->sli_flag & LPFC_SLI_ACTIVE) {
5040                                 /* First copy command data */
5041                                 word0 = *((uint32_t *)phba->mbox);
5042                                 word0 = le32_to_cpu(word0);
5043                                 if (mb->mbxCommand == MBX_CONFIG_PORT) {
5044                                         MAILBOX_t *slimmb;
5045                                         uint32_t slimword0;
5046                                         /* Check real SLIM for any errors */
5047                                         slimword0 = readl(phba->MBslimaddr);
5048                                         slimmb = (MAILBOX_t *) & slimword0;
5049                                         if (((slimword0 & OWN_CHIP) != OWN_CHIP)
5050                                             && slimmb->mbxStatus) {
5051                                                 psli->sli_flag &=
5052                                                     ~LPFC_SLI_ACTIVE;
5053                                                 word0 = slimword0;
5054                                         }
5055                                 }
5056                         } else {
5057                                 /* First copy command data */
5058                                 word0 = readl(phba->MBslimaddr);
5059                         }
5060                         /* Read the HBA Host Attention Register */
5061                         ha_copy = readl(phba->HAregaddr);
5062                 }
5063
5064                 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
5065                         /* copy results back to user */
5066                         lpfc_sli_pcimem_bcopy(phba->mbox, mb, MAILBOX_CMD_SIZE);
5067                         /* Copy the mailbox extension data */
5068                         if (pmbox->out_ext_byte_len && pmbox->context2) {
5069                                 lpfc_sli_pcimem_bcopy(phba->mbox_ext,
5070                                                       pmbox->context2,
5071                                                       pmbox->out_ext_byte_len);
5072                         }
5073                 } else {
5074                         /* First copy command data */
5075                         lpfc_memcpy_from_slim(mb, phba->MBslimaddr,
5076                                                         MAILBOX_CMD_SIZE);
5077                         /* Copy the mailbox extension data */
5078                         if (pmbox->out_ext_byte_len && pmbox->context2) {
5079                                 lpfc_memcpy_from_slim(pmbox->context2,
5080                                         phba->MBslimaddr +
5081                                         MAILBOX_HBA_EXT_OFFSET,
5082                                         pmbox->out_ext_byte_len);
5083                         }
5084                 }
5085
5086                 writel(HA_MBATT, phba->HAregaddr);
5087                 readl(phba->HAregaddr); /* flush */
5088
5089                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5090                 status = mb->mbxStatus;
5091         }
5092
5093         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
5094         return status;
5095
5096 out_not_finished:
5097         if (processing_queue) {
5098                 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
5099                 lpfc_mbox_cmpl_put(phba, pmbox);
5100         }
5101         return MBX_NOT_FINISHED;
5102 }
5103
5104 /**
5105  * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
5106  * @phba: Pointer to HBA context object.
5107  *
5108  * The function blocks the posting of SLI4 asynchronous mailbox commands from
5109  * the driver internal pending mailbox queue. It will then try to wait out the
5110  * possible outstanding mailbox command before return.
5111  *
5112  * Returns:
5113  *      0 - the outstanding mailbox command completed; otherwise, the wait for
5114  *      the outstanding mailbox command timed out.
5115  **/
5116 static int
5117 lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
5118 {
5119         struct lpfc_sli *psli = &phba->sli;
5120         uint8_t actcmd = MBX_HEARTBEAT;
5121         int rc = 0;
5122         unsigned long timeout;
5123
5124         /* Mark the asynchronous mailbox command posting as blocked */
5125         spin_lock_irq(&phba->hbalock);
5126         psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
5127         if (phba->sli.mbox_active)
5128                 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
5129         spin_unlock_irq(&phba->hbalock);
5130         /* Determine how long we might wait for the active mailbox
5131          * command to be gracefully completed by firmware.
5132          */
5133         timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, actcmd) * 1000) +
5134                                    jiffies;
5135         /* Wait for the outstnading mailbox command to complete */
5136         while (phba->sli.mbox_active) {
5137                 /* Check active mailbox complete status every 2ms */
5138                 msleep(2);
5139                 if (time_after(jiffies, timeout)) {
5140                         /* Timeout, marked the outstanding cmd not complete */
5141                         rc = 1;
5142                         break;
5143                 }
5144         }
5145
5146         /* Can not cleanly block async mailbox command, fails it */
5147         if (rc) {
5148                 spin_lock_irq(&phba->hbalock);
5149                 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
5150                 spin_unlock_irq(&phba->hbalock);
5151         }
5152         return rc;
5153 }
5154
5155 /**
5156  * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
5157  * @phba: Pointer to HBA context object.
5158  *
5159  * The function unblocks and resume posting of SLI4 asynchronous mailbox
5160  * commands from the driver internal pending mailbox queue. It makes sure
5161  * that there is no outstanding mailbox command before resuming posting
5162  * asynchronous mailbox commands. If, for any reason, there is outstanding
5163  * mailbox command, it will try to wait it out before resuming asynchronous
5164  * mailbox command posting.
5165  **/
5166 static void
5167 lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
5168 {
5169         struct lpfc_sli *psli = &phba->sli;
5170
5171         spin_lock_irq(&phba->hbalock);
5172         if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
5173                 /* Asynchronous mailbox posting is not blocked, do nothing */
5174                 spin_unlock_irq(&phba->hbalock);
5175                 return;
5176         }
5177
5178         /* Outstanding synchronous mailbox command is guaranteed to be done,
5179          * successful or timeout, after timing-out the outstanding mailbox
5180          * command shall always be removed, so just unblock posting async
5181          * mailbox command and resume
5182          */
5183         psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
5184         spin_unlock_irq(&phba->hbalock);
5185
5186         /* wake up worker thread to post asynchronlous mailbox command */
5187         lpfc_worker_wake_up(phba);
5188 }
5189
5190 /**
5191  * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
5192  * @phba: Pointer to HBA context object.
5193  * @mboxq: Pointer to mailbox object.
5194  *
5195  * The function posts a mailbox to the port.  The mailbox is expected
5196  * to be comletely filled in and ready for the port to operate on it.
5197  * This routine executes a synchronous completion operation on the
5198  * mailbox by polling for its completion.
5199  *
5200  * The caller must not be holding any locks when calling this routine.
5201  *
5202  * Returns:
5203  *      MBX_SUCCESS - mailbox posted successfully
5204  *      Any of the MBX error values.
5205  **/
5206 static int
5207 lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
5208 {
5209         int rc = MBX_SUCCESS;
5210         unsigned long iflag;
5211         uint32_t db_ready;
5212         uint32_t mcqe_status;
5213         uint32_t mbx_cmnd;
5214         unsigned long timeout;
5215         struct lpfc_sli *psli = &phba->sli;
5216         struct lpfc_mqe *mb = &mboxq->u.mqe;
5217         struct lpfc_bmbx_create *mbox_rgn;
5218         struct dma_address *dma_address;
5219         struct lpfc_register bmbx_reg;
5220
5221         /*
5222          * Only one mailbox can be active to the bootstrap mailbox region
5223          * at a time and there is no queueing provided.
5224          */
5225         spin_lock_irqsave(&phba->hbalock, iflag);
5226         if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
5227                 spin_unlock_irqrestore(&phba->hbalock, iflag);
5228                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5229                                 "(%d):2532 Mailbox command x%x (x%x) "
5230                                 "cannot issue Data: x%x x%x\n",
5231                                 mboxq->vport ? mboxq->vport->vpi : 0,
5232                                 mboxq->u.mb.mbxCommand,
5233                                 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5234                                 psli->sli_flag, MBX_POLL);
5235                 return MBXERR_ERROR;
5236         }
5237         /* The server grabs the token and owns it until release */
5238         psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
5239         phba->sli.mbox_active = mboxq;
5240         spin_unlock_irqrestore(&phba->hbalock, iflag);
5241
5242         /*
5243          * Initialize the bootstrap memory region to avoid stale data areas
5244          * in the mailbox post.  Then copy the caller's mailbox contents to
5245          * the bmbx mailbox region.
5246          */
5247         mbx_cmnd = bf_get(lpfc_mqe_command, mb);
5248         memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
5249         lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
5250                               sizeof(struct lpfc_mqe));
5251
5252         /* Post the high mailbox dma address to the port and wait for ready. */
5253         dma_address = &phba->sli4_hba.bmbx.dma_address;
5254         writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
5255
5256         timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mbx_cmnd)
5257                                    * 1000) + jiffies;
5258         do {
5259                 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
5260                 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
5261                 if (!db_ready)
5262                         msleep(2);
5263
5264                 if (time_after(jiffies, timeout)) {
5265                         rc = MBXERR_ERROR;
5266                         goto exit;
5267                 }
5268         } while (!db_ready);
5269
5270         /* Post the low mailbox dma address to the port. */
5271         writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
5272         timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mbx_cmnd)
5273                                    * 1000) + jiffies;
5274         do {
5275                 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
5276                 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
5277                 if (!db_ready)
5278                         msleep(2);
5279
5280                 if (time_after(jiffies, timeout)) {
5281                         rc = MBXERR_ERROR;
5282                         goto exit;
5283                 }
5284         } while (!db_ready);
5285
5286         /*
5287          * Read the CQ to ensure the mailbox has completed.
5288          * If so, update the mailbox status so that the upper layers
5289          * can complete the request normally.
5290          */
5291         lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
5292                               sizeof(struct lpfc_mqe));
5293         mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
5294         lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
5295                               sizeof(struct lpfc_mcqe));
5296         mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
5297
5298         /* Prefix the mailbox status with range x4000 to note SLI4 status. */
5299         if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
5300                 bf_set(lpfc_mqe_status, mb, LPFC_MBX_ERROR_RANGE | mcqe_status);
5301                 rc = MBXERR_ERROR;
5302         } else
5303                 lpfc_sli4_swap_str(phba, mboxq);
5304
5305         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5306                         "(%d):0356 Mailbox cmd x%x (x%x) Status x%x "
5307                         "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
5308                         " x%x x%x CQ: x%x x%x x%x x%x\n",
5309                         mboxq->vport ? mboxq->vport->vpi : 0,
5310                         mbx_cmnd, lpfc_sli4_mbox_opcode_get(phba, mboxq),
5311                         bf_get(lpfc_mqe_status, mb),
5312                         mb->un.mb_words[0], mb->un.mb_words[1],
5313                         mb->un.mb_words[2], mb->un.mb_words[3],
5314                         mb->un.mb_words[4], mb->un.mb_words[5],
5315                         mb->un.mb_words[6], mb->un.mb_words[7],
5316                         mb->un.mb_words[8], mb->un.mb_words[9],
5317                         mb->un.mb_words[10], mb->un.mb_words[11],
5318                         mb->un.mb_words[12], mboxq->mcqe.word0,
5319                         mboxq->mcqe.mcqe_tag0,  mboxq->mcqe.mcqe_tag1,
5320                         mboxq->mcqe.trailer);
5321 exit:
5322         /* We are holding the token, no needed for lock when release */
5323         spin_lock_irqsave(&phba->hbalock, iflag);
5324         psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5325         phba->sli.mbox_active = NULL;
5326         spin_unlock_irqrestore(&phba->hbalock, iflag);
5327         return rc;
5328 }
5329
5330 /**
5331  * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
5332  * @phba: Pointer to HBA context object.
5333  * @pmbox: Pointer to mailbox object.
5334  * @flag: Flag indicating how the mailbox need to be processed.
5335  *
5336  * This function is called by discovery code and HBA management code to submit
5337  * a mailbox command to firmware with SLI-4 interface spec.
5338  *
5339  * Return codes the caller owns the mailbox command after the return of the
5340  * function.
5341  **/
5342 static int
5343 lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
5344                        uint32_t flag)
5345 {
5346         struct lpfc_sli *psli = &phba->sli;
5347         unsigned long iflags;
5348         int rc;
5349
5350         rc = lpfc_mbox_dev_check(phba);
5351         if (unlikely(rc)) {
5352                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5353                                 "(%d):2544 Mailbox command x%x (x%x) "
5354                                 "cannot issue Data: x%x x%x\n",
5355                                 mboxq->vport ? mboxq->vport->vpi : 0,
5356                                 mboxq->u.mb.mbxCommand,
5357                                 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5358                                 psli->sli_flag, flag);
5359                 goto out_not_finished;
5360         }
5361
5362         /* Detect polling mode and jump to a handler */
5363         if (!phba->sli4_hba.intr_enable) {
5364                 if (flag == MBX_POLL)
5365                         rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
5366                 else
5367                         rc = -EIO;
5368                 if (rc != MBX_SUCCESS)
5369                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5370                                         "(%d):2541 Mailbox command x%x "
5371                                         "(x%x) cannot issue Data: x%x x%x\n",
5372                                         mboxq->vport ? mboxq->vport->vpi : 0,
5373                                         mboxq->u.mb.mbxCommand,
5374                                         lpfc_sli4_mbox_opcode_get(phba, mboxq),
5375                                         psli->sli_flag, flag);
5376                 return rc;
5377         } else if (flag == MBX_POLL) {
5378                 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
5379                                 "(%d):2542 Try to issue mailbox command "
5380                                 "x%x (x%x) synchronously ahead of async"
5381                                 "mailbox command queue: x%x x%x\n",
5382                                 mboxq->vport ? mboxq->vport->vpi : 0,
5383                                 mboxq->u.mb.mbxCommand,
5384                                 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5385                                 psli->sli_flag, flag);
5386                 /* Try to block the asynchronous mailbox posting */
5387                 rc = lpfc_sli4_async_mbox_block(phba);
5388                 if (!rc) {
5389                         /* Successfully blocked, now issue sync mbox cmd */
5390                         rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
5391                         if (rc != MBX_SUCCESS)
5392                                 lpfc_printf_log(phba, KERN_ERR,
5393                                                 LOG_MBOX | LOG_SLI,
5394                                                 "(%d):2597 Mailbox command "
5395                                                 "x%x (x%x) cannot issue "
5396                                                 "Data: x%x x%x\n",
5397                                                 mboxq->vport ?
5398                                                 mboxq->vport->vpi : 0,
5399                                                 mboxq->u.mb.mbxCommand,
5400                                                 lpfc_sli4_mbox_opcode_get(phba,
5401                                                                 mboxq),
5402                                                 psli->sli_flag, flag);
5403                         /* Unblock the async mailbox posting afterward */
5404                         lpfc_sli4_async_mbox_unblock(phba);
5405                 }
5406                 return rc;
5407         }
5408
5409         /* Now, interrupt mode asynchrous mailbox command */
5410         rc = lpfc_mbox_cmd_check(phba, mboxq);
5411         if (rc) {
5412                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5413                                 "(%d):2543 Mailbox command x%x (x%x) "
5414                                 "cannot issue Data: x%x x%x\n",
5415                                 mboxq->vport ? mboxq->vport->vpi : 0,
5416                                 mboxq->u.mb.mbxCommand,
5417                                 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5418                                 psli->sli_flag, flag);
5419                 goto out_not_finished;
5420         }
5421
5422         /* Put the mailbox command to the driver internal FIFO */
5423         psli->slistat.mbox_busy++;
5424         spin_lock_irqsave(&phba->hbalock, iflags);
5425         lpfc_mbox_put(phba, mboxq);
5426         spin_unlock_irqrestore(&phba->hbalock, iflags);
5427         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5428                         "(%d):0354 Mbox cmd issue - Enqueue Data: "
5429                         "x%x (x%x) x%x x%x x%x\n",
5430                         mboxq->vport ? mboxq->vport->vpi : 0xffffff,
5431                         bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5432                         lpfc_sli4_mbox_opcode_get(phba, mboxq),
5433                         phba->pport->port_state,
5434                         psli->sli_flag, MBX_NOWAIT);
5435         /* Wake up worker thread to transport mailbox command from head */
5436         lpfc_worker_wake_up(phba);
5437
5438         return MBX_BUSY;
5439
5440 out_not_finished:
5441         return MBX_NOT_FINISHED;
5442 }
5443
5444 /**
5445  * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
5446  * @phba: Pointer to HBA context object.
5447  *
5448  * This function is called by worker thread to send a mailbox command to
5449  * SLI4 HBA firmware.
5450  *
5451  **/
5452 int
5453 lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
5454 {
5455         struct lpfc_sli *psli = &phba->sli;
5456         LPFC_MBOXQ_t *mboxq;
5457         int rc = MBX_SUCCESS;
5458         unsigned long iflags;
5459         struct lpfc_mqe *mqe;
5460         uint32_t mbx_cmnd;
5461
5462         /* Check interrupt mode before post async mailbox command */
5463         if (unlikely(!phba->sli4_hba.intr_enable))
5464                 return MBX_NOT_FINISHED;
5465
5466         /* Check for mailbox command service token */
5467         spin_lock_irqsave(&phba->hbalock, iflags);
5468         if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
5469                 spin_unlock_irqrestore(&phba->hbalock, iflags);
5470                 return MBX_NOT_FINISHED;
5471         }
5472         if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
5473                 spin_unlock_irqrestore(&phba->hbalock, iflags);
5474                 return MBX_NOT_FINISHED;
5475         }
5476         if (unlikely(phba->sli.mbox_active)) {
5477                 spin_unlock_irqrestore(&phba->hbalock, iflags);
5478                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5479                                 "0384 There is pending active mailbox cmd\n");
5480                 return MBX_NOT_FINISHED;
5481         }
5482         /* Take the mailbox command service token */
5483         psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
5484
5485         /* Get the next mailbox command from head of queue */
5486         mboxq = lpfc_mbox_get(phba);
5487
5488         /* If no more mailbox command waiting for post, we're done */
5489         if (!mboxq) {
5490                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5491                 spin_unlock_irqrestore(&phba->hbalock, iflags);
5492                 return MBX_SUCCESS;
5493         }
5494         phba->sli.mbox_active = mboxq;
5495         spin_unlock_irqrestore(&phba->hbalock, iflags);
5496
5497         /* Check device readiness for posting mailbox command */
5498         rc = lpfc_mbox_dev_check(phba);
5499         if (unlikely(rc))
5500                 /* Driver clean routine will clean up pending mailbox */
5501                 goto out_not_finished;
5502
5503         /* Prepare the mbox command to be posted */
5504         mqe = &mboxq->u.mqe;
5505         mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
5506
5507         /* Start timer for the mbox_tmo and log some mailbox post messages */
5508         mod_timer(&psli->mbox_tmo, (jiffies +
5509                   (HZ * lpfc_mbox_tmo_val(phba, mbx_cmnd))));
5510
5511         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5512                         "(%d):0355 Mailbox cmd x%x (x%x) issue Data: "
5513                         "x%x x%x\n",
5514                         mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
5515                         lpfc_sli4_mbox_opcode_get(phba, mboxq),
5516                         phba->pport->port_state, psli->sli_flag);
5517
5518         if (mbx_cmnd != MBX_HEARTBEAT) {
5519                 if (mboxq->vport) {
5520                         lpfc_debugfs_disc_trc(mboxq->vport,
5521                                 LPFC_DISC_TRC_MBOX_VPORT,
5522                                 "MBOX Send vport: cmd:x%x mb:x%x x%x",
5523                                 mbx_cmnd, mqe->un.mb_words[0],
5524                                 mqe->un.mb_words[1]);
5525                 } else {
5526                         lpfc_debugfs_disc_trc(phba->pport,
5527                                 LPFC_DISC_TRC_MBOX,
5528                                 "MBOX Send: cmd:x%x mb:x%x x%x",
5529                                 mbx_cmnd, mqe->un.mb_words[0],
5530                                 mqe->un.mb_words[1]);
5531                 }
5532         }
5533         psli->slistat.mbox_cmd++;
5534
5535         /* Post the mailbox command to the port */
5536         rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
5537         if (rc != MBX_SUCCESS) {
5538                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5539                                 "(%d):2533 Mailbox command x%x (x%x) "
5540                                 "cannot issue Data: x%x x%x\n",
5541                                 mboxq->vport ? mboxq->vport->vpi : 0,
5542                                 mboxq->u.mb.mbxCommand,
5543                                 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5544                                 psli->sli_flag, MBX_NOWAIT);
5545                 goto out_not_finished;
5546         }
5547
5548         return rc;
5549
5550 out_not_finished:
5551         spin_lock_irqsave(&phba->hbalock, iflags);
5552         mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
5553         __lpfc_mbox_cmpl_put(phba, mboxq);
5554         /* Release the token */
5555         psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5556         phba->sli.mbox_active = NULL;
5557         spin_unlock_irqrestore(&phba->hbalock, iflags);
5558
5559         return MBX_NOT_FINISHED;
5560 }
5561
5562 /**
5563  * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
5564  * @phba: Pointer to HBA context object.
5565  * @pmbox: Pointer to mailbox object.
5566  * @flag: Flag indicating how the mailbox need to be processed.
5567  *
5568  * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
5569  * the API jump table function pointer from the lpfc_hba struct.
5570  *
5571  * Return codes the caller owns the mailbox command after the return of the
5572  * function.
5573  **/
5574 int
5575 lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
5576 {
5577         return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
5578 }
5579
5580 /**
5581  * lpfc_mbox_api_table_setup - Set up mbox api fucntion jump table
5582  * @phba: The hba struct for which this call is being executed.
5583  * @dev_grp: The HBA PCI-Device group number.
5584  *
5585  * This routine sets up the mbox interface API function jump table in @phba
5586  * struct.
5587  * Returns: 0 - success, -ENODEV - failure.
5588  **/
5589 int
5590 lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
5591 {
5592
5593         switch (dev_grp) {
5594         case LPFC_PCI_DEV_LP:
5595                 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
5596                 phba->lpfc_sli_handle_slow_ring_event =
5597                                 lpfc_sli_handle_slow_ring_event_s3;
5598                 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
5599                 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
5600                 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
5601                 break;
5602         case LPFC_PCI_DEV_OC:
5603                 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
5604                 phba->lpfc_sli_handle_slow_ring_event =
5605                                 lpfc_sli_handle_slow_ring_event_s4;
5606                 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
5607                 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
5608                 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
5609                 break;
5610         default:
5611                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5612                                 "1420 Invalid HBA PCI-device group: 0x%x\n",
5613                                 dev_grp);
5614                 return -ENODEV;
5615                 break;
5616         }
5617         return 0;
5618 }
5619
5620 /**
5621  * __lpfc_sli_ringtx_put - Add an iocb to the txq
5622  * @phba: Pointer to HBA context object.
5623  * @pring: Pointer to driver SLI ring object.
5624  * @piocb: Pointer to address of newly added command iocb.
5625  *
5626  * This function is called with hbalock held to add a command
5627  * iocb to the txq when SLI layer cannot submit the command iocb
5628  * to the ring.
5629  **/
5630 void
5631 __lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
5632                     struct lpfc_iocbq *piocb)
5633 {
5634         /* Insert the caller's iocb in the txq tail for later processing. */
5635         list_add_tail(&piocb->list, &pring->txq);
5636         pring->txq_cnt++;
5637 }
5638
5639 /**
5640  * lpfc_sli_next_iocb - Get the next iocb in the txq
5641  * @phba: Pointer to HBA context object.
5642  * @pring: Pointer to driver SLI ring object.
5643  * @piocb: Pointer to address of newly added command iocb.
5644  *
5645  * This function is called with hbalock held before a new
5646  * iocb is submitted to the firmware. This function checks
5647  * txq to flush the iocbs in txq to Firmware before
5648  * submitting new iocbs to the Firmware.
5649  * If there are iocbs in the txq which need to be submitted
5650  * to firmware, lpfc_sli_next_iocb returns the first element
5651  * of the txq after dequeuing it from txq.
5652  * If there is no iocb in the txq then the function will return
5653  * *piocb and *piocb is set to NULL. Caller needs to check
5654  * *piocb to find if there are more commands in the txq.
5655  **/
5656 static struct lpfc_iocbq *
5657 lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
5658                    struct lpfc_iocbq **piocb)
5659 {
5660         struct lpfc_iocbq * nextiocb;
5661
5662         nextiocb = lpfc_sli_ringtx_get(phba, pring);
5663         if (!nextiocb) {
5664                 nextiocb = *piocb;
5665                 *piocb = NULL;
5666         }
5667
5668         return nextiocb;
5669 }
5670
5671 /**
5672  * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
5673  * @phba: Pointer to HBA context object.
5674  * @ring_number: SLI ring number to issue iocb on.
5675  * @piocb: Pointer to command iocb.
5676  * @flag: Flag indicating if this command can be put into txq.
5677  *
5678  * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
5679  * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
5680  * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
5681  * flag is turned on, the function returns IOCB_ERROR. When the link is down,
5682  * this function allows only iocbs for posting buffers. This function finds
5683  * next available slot in the command ring and posts the command to the
5684  * available slot and writes the port attention register to request HBA start
5685  * processing new iocb. If there is no slot available in the ring and
5686  * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
5687  * the function returns IOCB_BUSY.
5688  *
5689  * This function is called with hbalock held. The function will return success
5690  * after it successfully submit the iocb to firmware or after adding to the
5691  * txq.
5692  **/
5693 static int
5694 __lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
5695                     struct lpfc_iocbq *piocb, uint32_t flag)
5696 {
5697         struct lpfc_iocbq *nextiocb;
5698         IOCB_t *iocb;
5699         struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
5700
5701         if (piocb->iocb_cmpl && (!piocb->vport) &&
5702            (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
5703            (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
5704                 lpfc_printf_log(phba, KERN_ERR,
5705                                 LOG_SLI | LOG_VPORT,
5706                                 "1807 IOCB x%x failed. No vport\n",
5707                                 piocb->iocb.ulpCommand);
5708                 dump_stack();
5709                 return IOCB_ERROR;
5710         }
5711
5712
5713         /* If the PCI channel is in offline state, do not post iocbs. */
5714         if (unlikely(pci_channel_offline(phba->pcidev)))
5715                 return IOCB_ERROR;
5716
5717         /* If HBA has a deferred error attention, fail the iocb. */
5718         if (unlikely(phba->hba_flag & DEFER_ERATT))
5719                 return IOCB_ERROR;
5720
5721         /*
5722          * We should never get an IOCB if we are in a < LINK_DOWN state
5723          */
5724         if (unlikely(phba->link_state < LPFC_LINK_DOWN))
5725                 return IOCB_ERROR;
5726
5727         /*
5728          * Check to see if we are blocking IOCB processing because of a
5729          * outstanding event.
5730          */
5731         if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
5732                 goto iocb_busy;
5733
5734         if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
5735                 /*
5736                  * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
5737                  * can be issued if the link is not up.
5738                  */
5739                 switch (piocb->iocb.ulpCommand) {
5740                 case CMD_GEN_REQUEST64_CR:
5741                 case CMD_GEN_REQUEST64_CX:
5742                         if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
5743                                 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
5744                                         FC_RCTL_DD_UNSOL_CMD) ||
5745                                 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
5746                                         MENLO_TRANSPORT_TYPE))
5747
5748                                 goto iocb_busy;
5749                         break;
5750                 case CMD_QUE_RING_BUF_CN:
5751                 case CMD_QUE_RING_BUF64_CN:
5752                         /*
5753                          * For IOCBs, like QUE_RING_BUF, that have no rsp ring
5754                          * completion, iocb_cmpl MUST be 0.
5755                          */
5756                         if (piocb->iocb_cmpl)
5757                                 piocb->iocb_cmpl = NULL;
5758                         /*FALLTHROUGH*/
5759                 case CMD_CREATE_XRI_CR:
5760                 case CMD_CLOSE_XRI_CN:
5761                 case CMD_CLOSE_XRI_CX:
5762                         break;
5763                 default:
5764                         goto iocb_busy;
5765                 }
5766
5767         /*
5768          * For FCP commands, we must be in a state where we can process link
5769          * attention events.
5770          */
5771         } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
5772                             !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
5773                 goto iocb_busy;
5774         }
5775
5776         while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
5777                (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
5778                 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
5779
5780         if (iocb)
5781                 lpfc_sli_update_ring(phba, pring);
5782         else
5783                 lpfc_sli_update_full_ring(phba, pring);
5784
5785         if (!piocb)
5786                 return IOCB_SUCCESS;
5787
5788         goto out_busy;
5789
5790  iocb_busy:
5791         pring->stats.iocb_cmd_delay++;
5792
5793  out_busy:
5794
5795         if (!(flag & SLI_IOCB_RET_IOCB)) {
5796                 __lpfc_sli_ringtx_put(phba, pring, piocb);
5797                 return IOCB_SUCCESS;
5798         }
5799
5800         return IOCB_BUSY;
5801 }
5802
5803 /**
5804  * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
5805  * @phba: Pointer to HBA context object.
5806  * @piocb: Pointer to command iocb.
5807  * @sglq: Pointer to the scatter gather queue object.
5808  *
5809  * This routine converts the bpl or bde that is in the IOCB
5810  * to a sgl list for the sli4 hardware. The physical address
5811  * of the bpl/bde is converted back to a virtual address.
5812  * If the IOCB contains a BPL then the list of BDE's is
5813  * converted to sli4_sge's. If the IOCB contains a single
5814  * BDE then it is converted to a single sli_sge.
5815  * The IOCB is still in cpu endianess so the contents of
5816  * the bpl can be used without byte swapping.
5817  *
5818  * Returns valid XRI = Success, NO_XRI = Failure.
5819 **/
5820 static uint16_t
5821 lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
5822                 struct lpfc_sglq *sglq)
5823 {
5824         uint16_t xritag = NO_XRI;
5825         struct ulp_bde64 *bpl = NULL;
5826         struct ulp_bde64 bde;
5827         struct sli4_sge *sgl  = NULL;
5828         IOCB_t *icmd;
5829         int numBdes = 0;
5830         int i = 0;
5831
5832         if (!piocbq || !sglq)
5833                 return xritag;
5834
5835         sgl  = (struct sli4_sge *)sglq->sgl;
5836         icmd = &piocbq->iocb;
5837         if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
5838                 numBdes = icmd->un.genreq64.bdl.bdeSize /
5839                                 sizeof(struct ulp_bde64);
5840                 /* The addrHigh and addrLow fields within the IOCB
5841                  * have not been byteswapped yet so there is no
5842                  * need to swap them back.
5843                  */
5844                 bpl  = (struct ulp_bde64 *)
5845                         ((struct lpfc_dmabuf *)piocbq->context3)->virt;
5846
5847                 if (!bpl)
5848                         return xritag;
5849
5850                 for (i = 0; i < numBdes; i++) {
5851                         /* Should already be byte swapped. */
5852                         sgl->addr_hi = bpl->addrHigh;
5853                         sgl->addr_lo = bpl->addrLow;
5854
5855                         if ((i+1) == numBdes)
5856                                 bf_set(lpfc_sli4_sge_last, sgl, 1);
5857                         else
5858                                 bf_set(lpfc_sli4_sge_last, sgl, 0);
5859                         sgl->word2 = cpu_to_le32(sgl->word2);
5860                         /* swap the size field back to the cpu so we
5861                          * can assign it to the sgl.
5862                          */
5863                         bde.tus.w = le32_to_cpu(bpl->tus.w);
5864                         sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
5865                         bpl++;
5866                         sgl++;
5867                 }
5868         } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
5869                         /* The addrHigh and addrLow fields of the BDE have not
5870                          * been byteswapped yet so they need to be swapped
5871                          * before putting them in the sgl.
5872                          */
5873                         sgl->addr_hi =
5874                                 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
5875                         sgl->addr_lo =
5876                                 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
5877                         bf_set(lpfc_sli4_sge_last, sgl, 1);
5878                         sgl->word2 = cpu_to_le32(sgl->word2);
5879                         sgl->sge_len =
5880                                 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
5881         }
5882         return sglq->sli4_xritag;
5883 }
5884
5885 /**
5886  * lpfc_sli4_scmd_to_wqidx_distr - scsi command to SLI4 WQ index distribution
5887  * @phba: Pointer to HBA context object.
5888  *
5889  * This routine performs a round robin SCSI command to SLI4 FCP WQ index
5890  * distribution.  This is called by __lpfc_sli_issue_iocb_s4() with the hbalock
5891  * held.
5892  *
5893  * Return: index into SLI4 fast-path FCP queue index.
5894  **/
5895 static uint32_t
5896 lpfc_sli4_scmd_to_wqidx_distr(struct lpfc_hba *phba)
5897 {
5898         ++phba->fcp_qidx;
5899         if (phba->fcp_qidx >= phba->cfg_fcp_wq_count)
5900                 phba->fcp_qidx = 0;
5901
5902         return phba->fcp_qidx;
5903 }
5904
5905 /**
5906  * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
5907  * @phba: Pointer to HBA context object.
5908  * @piocb: Pointer to command iocb.
5909  * @wqe: Pointer to the work queue entry.
5910  *
5911  * This routine converts the iocb command to its Work Queue Entry
5912  * equivalent. The wqe pointer should not have any fields set when
5913  * this routine is called because it will memcpy over them.
5914  * This routine does not set the CQ_ID or the WQEC bits in the
5915  * wqe.
5916  *
5917  * Returns: 0 = Success, IOCB_ERROR = Failure.
5918  **/
5919 static int
5920 lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
5921                 union lpfc_wqe *wqe)
5922 {
5923         uint32_t xmit_len = 0, total_len = 0;
5924         uint8_t ct = 0;
5925         uint32_t fip;
5926         uint32_t abort_tag;
5927         uint8_t command_type = ELS_COMMAND_NON_FIP;
5928         uint8_t cmnd;
5929         uint16_t xritag;
5930         struct ulp_bde64 *bpl = NULL;
5931         uint32_t els_id = ELS_ID_DEFAULT;
5932         int numBdes, i;
5933         struct ulp_bde64 bde;
5934
5935         fip = phba->hba_flag & HBA_FIP_SUPPORT;
5936         /* The fcp commands will set command type */
5937         if (iocbq->iocb_flag &  LPFC_IO_FCP)
5938                 command_type = FCP_COMMAND;
5939         else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
5940                 command_type = ELS_COMMAND_FIP;
5941         else
5942                 command_type = ELS_COMMAND_NON_FIP;
5943
5944         /* Some of the fields are in the right position already */
5945         memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
5946         abort_tag = (uint32_t) iocbq->iotag;
5947         xritag = iocbq->sli4_xritag;
5948         wqe->words[7] = 0; /* The ct field has moved so reset */
5949         /* words0-2 bpl convert bde */
5950         if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
5951                 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
5952                                 sizeof(struct ulp_bde64);
5953                 bpl  = (struct ulp_bde64 *)
5954                         ((struct lpfc_dmabuf *)iocbq->context3)->virt;
5955                 if (!bpl)
5956                         return IOCB_ERROR;
5957
5958                 /* Should already be byte swapped. */
5959                 wqe->generic.bde.addrHigh =  le32_to_cpu(bpl->addrHigh);
5960                 wqe->generic.bde.addrLow =  le32_to_cpu(bpl->addrLow);
5961                 /* swap the size field back to the cpu so we
5962                  * can assign it to the sgl.
5963                  */
5964                 wqe->generic.bde.tus.w  = le32_to_cpu(bpl->tus.w);
5965                 xmit_len = wqe->generic.bde.tus.f.bdeSize;
5966                 total_len = 0;
5967                 for (i = 0; i < numBdes; i++) {
5968                         bde.tus.w  = le32_to_cpu(bpl[i].tus.w);
5969                         total_len += bde.tus.f.bdeSize;
5970                 }
5971         } else
5972                 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
5973
5974         iocbq->iocb.ulpIoTag = iocbq->iotag;
5975         cmnd = iocbq->iocb.ulpCommand;
5976
5977         switch (iocbq->iocb.ulpCommand) {
5978         case CMD_ELS_REQUEST64_CR:
5979                 if (!iocbq->iocb.ulpLe) {
5980                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5981                                 "2007 Only Limited Edition cmd Format"
5982                                 " supported 0x%x\n",
5983                                 iocbq->iocb.ulpCommand);
5984                         return IOCB_ERROR;
5985                 }
5986                 wqe->els_req.payload_len = xmit_len;
5987                 /* Els_reguest64 has a TMO */
5988                 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
5989                         iocbq->iocb.ulpTimeout);
5990                 /* Need a VF for word 4 set the vf bit*/
5991                 bf_set(els_req64_vf, &wqe->els_req, 0);
5992                 /* And a VFID for word 12 */
5993                 bf_set(els_req64_vfid, &wqe->els_req, 0);
5994                 /*
5995                  * Set ct field to 3, indicates that the context_tag field
5996                  * contains the FCFI and remote N_Port_ID is
5997                  * in word 5.
5998                  */
5999
6000                 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
6001                 bf_set(lpfc_wqe_gen_context, &wqe->generic,
6002                                 iocbq->iocb.ulpContext);
6003
6004                 bf_set(lpfc_wqe_gen_ct, &wqe->generic, ct);
6005                 bf_set(lpfc_wqe_gen_pu, &wqe->generic, 0);
6006                 /* CCP CCPE PV PRI in word10 were set in the memcpy */
6007
6008                 if (command_type == ELS_COMMAND_FIP) {
6009                         els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
6010                                         >> LPFC_FIP_ELS_ID_SHIFT);
6011                 }
6012                 bf_set(lpfc_wqe_gen_els_id, &wqe->generic, els_id);
6013
6014         break;
6015         case CMD_XMIT_SEQUENCE64_CX:
6016                 bf_set(lpfc_wqe_gen_context, &wqe->generic,
6017                                         iocbq->iocb.un.ulpWord[3]);
6018                 wqe->generic.word3 = 0;
6019                 bf_set(wqe_rcvoxid, &wqe->generic, iocbq->iocb.ulpContext);
6020                 /* The entire sequence is transmitted for this IOCB */
6021                 xmit_len = total_len;
6022                 cmnd = CMD_XMIT_SEQUENCE64_CR;
6023         case CMD_XMIT_SEQUENCE64_CR:
6024                 /* word3 iocb=io_tag32 wqe=payload_offset */
6025                 /* payload offset used for multilpe outstanding
6026                  * sequences on the same exchange
6027                  */
6028                 wqe->words[3] = 0;
6029                 /* word4 relative_offset memcpy */
6030                 /* word5 r_ctl/df_ctl memcpy */
6031                 bf_set(lpfc_wqe_gen_pu, &wqe->generic, 0);
6032                 wqe->xmit_sequence.xmit_len = xmit_len;
6033                 command_type = OTHER_COMMAND;
6034         break;
6035         case CMD_XMIT_BCAST64_CN:
6036                 /* word3 iocb=iotag32 wqe=payload_len */
6037                 wqe->words[3] = 0; /* no definition for this in wqe */
6038                 /* word4 iocb=rsvd wqe=rsvd */
6039                 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
6040                 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
6041                 bf_set(lpfc_wqe_gen_ct, &wqe->generic,
6042                         ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
6043         break;
6044         case CMD_FCP_IWRITE64_CR:
6045                 command_type = FCP_COMMAND_DATA_OUT;
6046                 /* The struct for wqe fcp_iwrite has 3 fields that are somewhat
6047                  * confusing.
6048                  * word3 is payload_len: byte offset to the sgl entry for the
6049                  * fcp_command.
6050                  * word4 is total xfer len, same as the IOCB->ulpParameter.
6051                  * word5 is initial xfer len 0 = wait for xfer-ready
6052                  */
6053
6054                 /* Always wait for xfer-ready before sending data */
6055                 wqe->fcp_iwrite.initial_xfer_len = 0;
6056                 /* word 4 (xfer length) should have been set on the memcpy */
6057
6058         /* allow write to fall through to read */
6059         case CMD_FCP_IREAD64_CR:
6060                 /* FCP_CMD is always the 1st sgl entry */
6061                 wqe->fcp_iread.payload_len =
6062                         xmit_len + sizeof(struct fcp_rsp);
6063
6064                 /* word 4 (xfer length) should have been set on the memcpy */
6065
6066                 bf_set(lpfc_wqe_gen_erp, &wqe->generic,
6067                         iocbq->iocb.ulpFCP2Rcvy);
6068                 bf_set(lpfc_wqe_gen_lnk, &wqe->generic, iocbq->iocb.ulpXS);
6069                 /* The XC bit and the XS bit are similar. The driver never
6070                  * tracked whether or not the exchange was previouslly open.
6071                  * XC = Exchange create, 0 is create. 1 is already open.
6072                  * XS = link cmd: 1 do not close the exchange after command.
6073                  * XS = 0 close exchange when command completes.
6074                  * The only time we would not set the XC bit is when the XS bit
6075                  * is set and we are sending our 2nd or greater command on
6076                  * this exchange.
6077                  */
6078                 /* Always open the exchange */
6079                 bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
6080
6081                 wqe->words[10] &= 0xffff0000; /* zero out ebde count */
6082                 bf_set(lpfc_wqe_gen_pu, &wqe->generic, iocbq->iocb.ulpPU);
6083                 break;
6084         case CMD_FCP_ICMND64_CR:
6085                 /* Always open the exchange */
6086                 bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
6087
6088                 wqe->words[4] = 0;
6089                 wqe->words[10] &= 0xffff0000; /* zero out ebde count */
6090                 bf_set(lpfc_wqe_gen_pu, &wqe->generic, 0);
6091         break;
6092         case CMD_GEN_REQUEST64_CR:
6093                 /* word3 command length is described as byte offset to the
6094                  * rsp_data. Would always be 16, sizeof(struct sli4_sge)
6095                  * sgl[0] = cmnd
6096                  * sgl[1] = rsp.
6097                  *
6098                  */
6099                 wqe->gen_req.command_len = xmit_len;
6100                 /* Word4 parameter  copied in the memcpy */
6101                 /* Word5 [rctl, type, df_ctl, la] copied in memcpy */
6102                 /* word6 context tag copied in memcpy */
6103                 if (iocbq->iocb.ulpCt_h  || iocbq->iocb.ulpCt_l) {
6104                         ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
6105                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6106                                 "2015 Invalid CT %x command 0x%x\n",
6107                                 ct, iocbq->iocb.ulpCommand);
6108                         return IOCB_ERROR;
6109                 }
6110                 bf_set(lpfc_wqe_gen_ct, &wqe->generic, 0);
6111                 bf_set(wqe_tmo, &wqe->gen_req.wqe_com,
6112                         iocbq->iocb.ulpTimeout);
6113
6114                 bf_set(lpfc_wqe_gen_pu, &wqe->generic, iocbq->iocb.ulpPU);
6115                 command_type = OTHER_COMMAND;
6116         break;
6117         case CMD_XMIT_ELS_RSP64_CX:
6118                 /* words0-2 BDE memcpy */
6119                 /* word3 iocb=iotag32 wqe=rsvd */
6120                 wqe->words[3] = 0;
6121                 /* word4 iocb=did wge=rsvd. */
6122                 wqe->words[4] = 0;
6123                 /* word5 iocb=rsvd wge=did */
6124                 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
6125                          iocbq->iocb.un.elsreq64.remoteID);
6126
6127                 bf_set(lpfc_wqe_gen_ct, &wqe->generic,
6128                         ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
6129
6130                 bf_set(lpfc_wqe_gen_pu, &wqe->generic, iocbq->iocb.ulpPU);
6131                 bf_set(wqe_rcvoxid, &wqe->generic, iocbq->iocb.ulpContext);
6132                 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
6133                         bf_set(lpfc_wqe_gen_context, &wqe->generic,
6134                                iocbq->vport->vpi + phba->vpi_base);
6135                 command_type = OTHER_COMMAND;
6136         break;
6137         case CMD_CLOSE_XRI_CN:
6138         case CMD_ABORT_XRI_CN:
6139         case CMD_ABORT_XRI_CX:
6140                 /* words 0-2 memcpy should be 0 rserved */
6141                 /* port will send abts */
6142                 if (iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
6143                         /*
6144                          * The link is down so the fw does not need to send abts
6145                          * on the wire.
6146                          */
6147                         bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
6148                 else
6149                         bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
6150                 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
6151                 wqe->words[5] = 0;
6152                 bf_set(lpfc_wqe_gen_ct, &wqe->generic,
6153                         ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
6154                 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
6155                 /*
6156                  * The abort handler will send us CMD_ABORT_XRI_CN or
6157                  * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
6158                  */
6159                 bf_set(lpfc_wqe_gen_command, &wqe->generic, CMD_ABORT_XRI_CX);
6160                 cmnd = CMD_ABORT_XRI_CX;
6161                 command_type = OTHER_COMMAND;
6162                 xritag = 0;
6163         break;
6164         case CMD_XMIT_BLS_RSP64_CX:
6165                 /* As BLS ABTS-ACC WQE is very different from other WQEs,
6166                  * we re-construct this WQE here based on information in
6167                  * iocbq from scratch.
6168                  */
6169                 memset(wqe, 0, sizeof(union lpfc_wqe));
6170                 /* OX_ID is invariable to who sent ABTS to CT exchange */
6171                 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
6172                        bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_acc));
6173                 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_acc) ==
6174                     LPFC_ABTS_UNSOL_INT) {
6175                         /* ABTS sent by initiator to CT exchange, the
6176                          * RX_ID field will be filled with the newly
6177                          * allocated responder XRI.
6178                          */
6179                         bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
6180                                iocbq->sli4_xritag);
6181                 } else {
6182                         /* ABTS sent by responder to CT exchange, the
6183                          * RX_ID field will be filled with the responder
6184                          * RX_ID from ABTS.
6185                          */
6186                         bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
6187                                bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_acc));
6188                 }
6189                 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
6190                 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
6191                 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
6192                        iocbq->iocb.ulpContext);
6193                 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
6194                 command_type = OTHER_COMMAND;
6195         break;
6196         case CMD_XRI_ABORTED_CX:
6197         case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
6198                 /* words0-2 are all 0's no bde */
6199                 /* word3 and word4 are rsvrd */
6200                 wqe->words[3] = 0;
6201                 wqe->words[4] = 0;
6202                 /* word5 iocb=rsvd wge=did */
6203                 /* There is no remote port id in the IOCB? */
6204                 /* Let this fall through and fail */
6205         case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
6206         case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
6207         case CMD_FCP_TRSP64_CX: /* Target mode rcv */
6208         case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
6209         default:
6210                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6211                                 "2014 Invalid command 0x%x\n",
6212                                 iocbq->iocb.ulpCommand);
6213                 return IOCB_ERROR;
6214         break;
6215
6216         }
6217         bf_set(lpfc_wqe_gen_xri, &wqe->generic, xritag);
6218         bf_set(lpfc_wqe_gen_request_tag, &wqe->generic, iocbq->iotag);
6219         wqe->generic.abort_tag = abort_tag;
6220         bf_set(lpfc_wqe_gen_cmd_type, &wqe->generic, command_type);
6221         bf_set(lpfc_wqe_gen_command, &wqe->generic, cmnd);
6222         bf_set(lpfc_wqe_gen_class, &wqe->generic, iocbq->iocb.ulpClass);
6223         bf_set(lpfc_wqe_gen_cq_id, &wqe->generic, LPFC_WQE_CQ_ID_DEFAULT);
6224
6225         return 0;
6226 }
6227
6228 /**
6229  * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
6230  * @phba: Pointer to HBA context object.
6231  * @ring_number: SLI ring number to issue iocb on.
6232  * @piocb: Pointer to command iocb.
6233  * @flag: Flag indicating if this command can be put into txq.
6234  *
6235  * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
6236  * an iocb command to an HBA with SLI-4 interface spec.
6237  *
6238  * This function is called with hbalock held. The function will return success
6239  * after it successfully submit the iocb to firmware or after adding to the
6240  * txq.
6241  **/
6242 static int
6243 __lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
6244                          struct lpfc_iocbq *piocb, uint32_t flag)
6245 {
6246         struct lpfc_sglq *sglq;
6247         union lpfc_wqe wqe;
6248         struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
6249
6250         if (piocb->sli4_xritag == NO_XRI) {
6251                 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
6252                     piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
6253                         sglq = NULL;
6254                 else {
6255                         if (pring->txq_cnt) {
6256                                 if (!(flag & SLI_IOCB_RET_IOCB)) {
6257                                         __lpfc_sli_ringtx_put(phba,
6258                                                 pring, piocb);
6259                                         return IOCB_SUCCESS;
6260                                 } else {
6261                                         return IOCB_BUSY;
6262                                 }
6263                         } else {
6264                         sglq = __lpfc_sli_get_sglq(phba);
6265                                 if (!sglq) {
6266                                         if (!(flag & SLI_IOCB_RET_IOCB)) {
6267                                                 __lpfc_sli_ringtx_put(phba,
6268                                                                 pring,
6269                                                                 piocb);
6270                                                 return IOCB_SUCCESS;
6271                                         } else
6272                                                 return IOCB_BUSY;
6273                                 }
6274                         }
6275                 }
6276         } else if (piocb->iocb_flag &  LPFC_IO_FCP) {
6277                 sglq = NULL; /* These IO's already have an XRI and
6278                               * a mapped sgl.
6279                               */
6280         } else {
6281                 /* This is a continuation of a commandi,(CX) so this
6282                  * sglq is on the active list
6283                  */
6284                 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_xritag);
6285                 if (!sglq)
6286                         return IOCB_ERROR;
6287         }
6288
6289         if (sglq) {
6290                 piocb->sli4_xritag = sglq->sli4_xritag;
6291
6292                 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
6293                         return IOCB_ERROR;
6294         }
6295
6296         if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
6297                 return IOCB_ERROR;
6298
6299         if ((piocb->iocb_flag & LPFC_IO_FCP) ||
6300                 (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
6301                 /*
6302                  * For FCP command IOCB, get a new WQ index to distribute
6303                  * WQE across the WQsr. On the other hand, for abort IOCB,
6304                  * it carries the same WQ index to the original command
6305                  * IOCB.
6306                  */
6307                 if (piocb->iocb_flag & LPFC_IO_FCP)
6308                         piocb->fcp_wqidx = lpfc_sli4_scmd_to_wqidx_distr(phba);
6309                 if (lpfc_sli4_wq_put(phba->sli4_hba.fcp_wq[piocb->fcp_wqidx],
6310                                      &wqe))
6311                         return IOCB_ERROR;
6312         } else {
6313                 if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
6314                         return IOCB_ERROR;
6315         }
6316         lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
6317
6318         return 0;
6319 }
6320
6321 /**
6322  * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
6323  *
6324  * This routine wraps the actual lockless version for issusing IOCB function
6325  * pointer from the lpfc_hba struct.
6326  *
6327  * Return codes:
6328  *      IOCB_ERROR - Error
6329  *      IOCB_SUCCESS - Success
6330  *      IOCB_BUSY - Busy
6331  **/
6332 int
6333 __lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
6334                 struct lpfc_iocbq *piocb, uint32_t flag)
6335 {
6336         return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
6337 }
6338
6339 /**
6340  * lpfc_sli_api_table_setup - Set up sli api fucntion jump table
6341  * @phba: The hba struct for which this call is being executed.
6342  * @dev_grp: The HBA PCI-Device group number.
6343  *
6344  * This routine sets up the SLI interface API function jump table in @phba
6345  * struct.
6346  * Returns: 0 - success, -ENODEV - failure.
6347  **/
6348 int
6349 lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
6350 {
6351
6352         switch (dev_grp) {
6353         case LPFC_PCI_DEV_LP:
6354                 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
6355                 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
6356                 break;
6357         case LPFC_PCI_DEV_OC:
6358                 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
6359                 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
6360                 break;
6361         default:
6362                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6363                                 "1419 Invalid HBA PCI-device group: 0x%x\n",
6364                                 dev_grp);
6365                 return -ENODEV;
6366                 break;
6367         }
6368         phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
6369         return 0;
6370 }
6371
6372 /**
6373  * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
6374  * @phba: Pointer to HBA context object.
6375  * @pring: Pointer to driver SLI ring object.
6376  * @piocb: Pointer to command iocb.
6377  * @flag: Flag indicating if this command can be put into txq.
6378  *
6379  * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
6380  * function. This function gets the hbalock and calls
6381  * __lpfc_sli_issue_iocb function and will return the error returned
6382  * by __lpfc_sli_issue_iocb function. This wrapper is used by
6383  * functions which do not hold hbalock.
6384  **/
6385 int
6386 lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
6387                     struct lpfc_iocbq *piocb, uint32_t flag)
6388 {
6389         unsigned long iflags;
6390         int rc;
6391
6392         spin_lock_irqsave(&phba->hbalock, iflags);
6393         rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
6394         spin_unlock_irqrestore(&phba->hbalock, iflags);
6395
6396         return rc;
6397 }
6398
6399 /**
6400  * lpfc_extra_ring_setup - Extra ring setup function
6401  * @phba: Pointer to HBA context object.
6402  *
6403  * This function is called while driver attaches with the
6404  * HBA to setup the extra ring. The extra ring is used
6405  * only when driver needs to support target mode functionality
6406  * or IP over FC functionalities.
6407  *
6408  * This function is called with no lock held.
6409  **/
6410 static int
6411 lpfc_extra_ring_setup( struct lpfc_hba *phba)
6412 {
6413         struct lpfc_sli *psli;
6414         struct lpfc_sli_ring *pring;
6415
6416         psli = &phba->sli;
6417
6418         /* Adjust cmd/rsp ring iocb entries more evenly */
6419
6420         /* Take some away from the FCP ring */
6421         pring = &psli->ring[psli->fcp_ring];
6422         pring->numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
6423         pring->numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
6424         pring->numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
6425         pring->numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
6426
6427         /* and give them to the extra ring */
6428         pring = &psli->ring[psli->extra_ring];
6429
6430         pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
6431         pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
6432         pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
6433         pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
6434
6435         /* Setup default profile for this ring */
6436         pring->iotag_max = 4096;
6437         pring->num_mask = 1;
6438         pring->prt[0].profile = 0;      /* Mask 0 */
6439         pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
6440         pring->prt[0].type = phba->cfg_multi_ring_type;
6441         pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
6442         return 0;
6443 }
6444
6445 /**
6446  * lpfc_sli_async_event_handler - ASYNC iocb handler function
6447  * @phba: Pointer to HBA context object.
6448  * @pring: Pointer to driver SLI ring object.
6449  * @iocbq: Pointer to iocb object.
6450  *
6451  * This function is called by the slow ring event handler
6452  * function when there is an ASYNC event iocb in the ring.
6453  * This function is called with no lock held.
6454  * Currently this function handles only temperature related
6455  * ASYNC events. The function decodes the temperature sensor
6456  * event message and posts events for the management applications.
6457  **/
6458 static void
6459 lpfc_sli_async_event_handler(struct lpfc_hba * phba,
6460         struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
6461 {
6462         IOCB_t *icmd;
6463         uint16_t evt_code;
6464         uint16_t temp;
6465         struct temp_event temp_event_data;
6466         struct Scsi_Host *shost;
6467         uint32_t *iocb_w;
6468
6469         icmd = &iocbq->iocb;
6470         evt_code = icmd->un.asyncstat.evt_code;
6471         temp = icmd->ulpContext;
6472
6473         if ((evt_code != ASYNC_TEMP_WARN) &&
6474                 (evt_code != ASYNC_TEMP_SAFE)) {
6475                 iocb_w = (uint32_t *) icmd;
6476                 lpfc_printf_log(phba,
6477                         KERN_ERR,
6478                         LOG_SLI,
6479                         "0346 Ring %d handler: unexpected ASYNC_STATUS"
6480                         " evt_code 0x%x\n"
6481                         "W0  0x%08x W1  0x%08x W2  0x%08x W3  0x%08x\n"
6482                         "W4  0x%08x W5  0x%08x W6  0x%08x W7  0x%08x\n"
6483                         "W8  0x%08x W9  0x%08x W10 0x%08x W11 0x%08x\n"
6484                         "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
6485                         pring->ringno,
6486                         icmd->un.asyncstat.evt_code,
6487                         iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
6488                         iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
6489                         iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
6490                         iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
6491
6492                 return;
6493         }
6494         temp_event_data.data = (uint32_t)temp;
6495         temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
6496         if (evt_code == ASYNC_TEMP_WARN) {
6497                 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
6498                 lpfc_printf_log(phba,
6499                                 KERN_ERR,
6500                                 LOG_TEMP,
6501                                 "0347 Adapter is very hot, please take "
6502                                 "corrective action. temperature : %d Celsius\n",
6503                                 temp);
6504         }
6505         if (evt_code == ASYNC_TEMP_SAFE) {
6506                 temp_event_data.event_code = LPFC_NORMAL_TEMP;
6507                 lpfc_printf_log(phba,
6508                                 KERN_ERR,
6509                                 LOG_TEMP,
6510                                 "0340 Adapter temperature is OK now. "
6511                                 "temperature : %d Celsius\n",
6512                                 temp);
6513         }
6514
6515         /* Send temperature change event to applications */
6516         shost = lpfc_shost_from_vport(phba->pport);
6517         fc_host_post_vendor_event(shost, fc_get_event_number(),
6518                 sizeof(temp_event_data), (char *) &temp_event_data,
6519                 LPFC_NL_VENDOR_ID);
6520
6521 }
6522
6523
6524 /**
6525  * lpfc_sli_setup - SLI ring setup function
6526  * @phba: Pointer to HBA context object.
6527  *
6528  * lpfc_sli_setup sets up rings of the SLI interface with
6529  * number of iocbs per ring and iotags. This function is
6530  * called while driver attach to the HBA and before the
6531  * interrupts are enabled. So there is no need for locking.
6532  *
6533  * This function always returns 0.
6534  **/
6535 int
6536 lpfc_sli_setup(struct lpfc_hba *phba)
6537 {
6538         int i, totiocbsize = 0;
6539         struct lpfc_sli *psli = &phba->sli;
6540         struct lpfc_sli_ring *pring;
6541
6542         psli->num_rings = MAX_CONFIGURED_RINGS;
6543         psli->sli_flag = 0;
6544         psli->fcp_ring = LPFC_FCP_RING;
6545         psli->next_ring = LPFC_FCP_NEXT_RING;
6546         psli->extra_ring = LPFC_EXTRA_RING;
6547
6548         psli->iocbq_lookup = NULL;
6549         psli->iocbq_lookup_len = 0;
6550         psli->last_iotag = 0;
6551
6552         for (i = 0; i < psli->num_rings; i++) {
6553                 pring = &psli->ring[i];
6554                 switch (i) {
6555                 case LPFC_FCP_RING:     /* ring 0 - FCP */
6556                         /* numCiocb and numRiocb are used in config_port */
6557                         pring->numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
6558                         pring->numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
6559                         pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
6560                         pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
6561                         pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
6562                         pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
6563                         pring->sizeCiocb = (phba->sli_rev == 3) ?
6564                                                         SLI3_IOCB_CMD_SIZE :
6565                                                         SLI2_IOCB_CMD_SIZE;
6566                         pring->sizeRiocb = (phba->sli_rev == 3) ?
6567                                                         SLI3_IOCB_RSP_SIZE :
6568                                                         SLI2_IOCB_RSP_SIZE;
6569                         pring->iotag_ctr = 0;
6570                         pring->iotag_max =
6571                             (phba->cfg_hba_queue_depth * 2);
6572                         pring->fast_iotag = pring->iotag_max;
6573                         pring->num_mask = 0;
6574                         break;
6575                 case LPFC_EXTRA_RING:   /* ring 1 - EXTRA */
6576                         /* numCiocb and numRiocb are used in config_port */
6577                         pring->numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
6578                         pring->numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
6579                         pring->sizeCiocb = (phba->sli_rev == 3) ?
6580                                                         SLI3_IOCB_CMD_SIZE :
6581                                                         SLI2_IOCB_CMD_SIZE;
6582                         pring->sizeRiocb = (phba->sli_rev == 3) ?
6583                                                         SLI3_IOCB_RSP_SIZE :
6584                                                         SLI2_IOCB_RSP_SIZE;
6585                         pring->iotag_max = phba->cfg_hba_queue_depth;
6586                         pring->num_mask = 0;
6587                         break;
6588                 case LPFC_ELS_RING:     /* ring 2 - ELS / CT */
6589                         /* numCiocb and numRiocb are used in config_port */
6590                         pring->numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
6591                         pring->numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
6592                         pring->sizeCiocb = (phba->sli_rev == 3) ?
6593                                                         SLI3_IOCB_CMD_SIZE :
6594                                                         SLI2_IOCB_CMD_SIZE;
6595                         pring->sizeRiocb = (phba->sli_rev == 3) ?
6596                                                         SLI3_IOCB_RSP_SIZE :
6597                                                         SLI2_IOCB_RSP_SIZE;
6598                         pring->fast_iotag = 0;
6599                         pring->iotag_ctr = 0;
6600                         pring->iotag_max = 4096;
6601                         pring->lpfc_sli_rcv_async_status =
6602                                 lpfc_sli_async_event_handler;
6603                         pring->num_mask = LPFC_MAX_RING_MASK;
6604                         pring->prt[0].profile = 0;      /* Mask 0 */
6605                         pring->prt[0].rctl = FC_RCTL_ELS_REQ;
6606                         pring->prt[0].type = FC_TYPE_ELS;
6607                         pring->prt[0].lpfc_sli_rcv_unsol_event =
6608                             lpfc_els_unsol_event;
6609                         pring->prt[1].profile = 0;      /* Mask 1 */
6610                         pring->prt[1].rctl = FC_RCTL_ELS_REP;
6611                         pring->prt[1].type = FC_TYPE_ELS;
6612                         pring->prt[1].lpfc_sli_rcv_unsol_event =
6613                             lpfc_els_unsol_event;
6614                         pring->prt[2].profile = 0;      /* Mask 2 */
6615                         /* NameServer Inquiry */
6616                         pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
6617                         /* NameServer */
6618                         pring->prt[2].type = FC_TYPE_CT;
6619                         pring->prt[2].lpfc_sli_rcv_unsol_event =
6620                             lpfc_ct_unsol_event;
6621                         pring->prt[3].profile = 0;      /* Mask 3 */
6622                         /* NameServer response */
6623                         pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
6624                         /* NameServer */
6625                         pring->prt[3].type = FC_TYPE_CT;
6626                         pring->prt[3].lpfc_sli_rcv_unsol_event =
6627                             lpfc_ct_unsol_event;
6628                         /* abort unsolicited sequence */
6629                         pring->prt[4].profile = 0;      /* Mask 4 */
6630                         pring->prt[4].rctl = FC_RCTL_BA_ABTS;
6631                         pring->prt[4].type = FC_TYPE_BLS;
6632                         pring->prt[4].lpfc_sli_rcv_unsol_event =
6633                             lpfc_sli4_ct_abort_unsol_event;
6634                         break;
6635                 }
6636                 totiocbsize += (pring->numCiocb * pring->sizeCiocb) +
6637                                 (pring->numRiocb * pring->sizeRiocb);
6638         }
6639         if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
6640                 /* Too many cmd / rsp ring entries in SLI2 SLIM */
6641                 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
6642                        "SLI2 SLIM Data: x%x x%lx\n",
6643                        phba->brd_no, totiocbsize,
6644                        (unsigned long) MAX_SLIM_IOCB_SIZE);
6645         }
6646         if (phba->cfg_multi_ring_support == 2)
6647                 lpfc_extra_ring_setup(phba);
6648
6649         return 0;
6650 }
6651
6652 /**
6653  * lpfc_sli_queue_setup - Queue initialization function
6654  * @phba: Pointer to HBA context object.
6655  *
6656  * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
6657  * ring. This function also initializes ring indices of each ring.
6658  * This function is called during the initialization of the SLI
6659  * interface of an HBA.
6660  * This function is called with no lock held and always returns
6661  * 1.
6662  **/
6663 int
6664 lpfc_sli_queue_setup(struct lpfc_hba *phba)
6665 {
6666         struct lpfc_sli *psli;
6667         struct lpfc_sli_ring *pring;
6668         int i;
6669
6670         psli = &phba->sli;
6671         spin_lock_irq(&phba->hbalock);
6672         INIT_LIST_HEAD(&psli->mboxq);
6673         INIT_LIST_HEAD(&psli->mboxq_cmpl);
6674         /* Initialize list headers for txq and txcmplq as double linked lists */
6675         for (i = 0; i < psli->num_rings; i++) {
6676                 pring = &psli->ring[i];
6677                 pring->ringno = i;
6678                 pring->next_cmdidx  = 0;
6679                 pring->local_getidx = 0;
6680                 pring->cmdidx = 0;
6681                 INIT_LIST_HEAD(&pring->txq);
6682                 INIT_LIST_HEAD(&pring->txcmplq);
6683                 INIT_LIST_HEAD(&pring->iocb_continueq);
6684                 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
6685                 INIT_LIST_HEAD(&pring->postbufq);
6686         }
6687         spin_unlock_irq(&phba->hbalock);
6688         return 1;
6689 }
6690
6691 /**
6692  * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
6693  * @phba: Pointer to HBA context object.
6694  *
6695  * This routine flushes the mailbox command subsystem. It will unconditionally
6696  * flush all the mailbox commands in the three possible stages in the mailbox
6697  * command sub-system: pending mailbox command queue; the outstanding mailbox
6698  * command; and completed mailbox command queue. It is caller's responsibility
6699  * to make sure that the driver is in the proper state to flush the mailbox
6700  * command sub-system. Namely, the posting of mailbox commands into the
6701  * pending mailbox command queue from the various clients must be stopped;
6702  * either the HBA is in a state that it will never works on the outstanding
6703  * mailbox command (such as in EEH or ERATT conditions) or the outstanding
6704  * mailbox command has been completed.
6705  **/
6706 static void
6707 lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
6708 {
6709         LIST_HEAD(completions);
6710         struct lpfc_sli *psli = &phba->sli;
6711         LPFC_MBOXQ_t *pmb;
6712         unsigned long iflag;
6713
6714         /* Flush all the mailbox commands in the mbox system */
6715         spin_lock_irqsave(&phba->hbalock, iflag);
6716         /* The pending mailbox command queue */
6717         list_splice_init(&phba->sli.mboxq, &completions);
6718         /* The outstanding active mailbox command */
6719         if (psli->mbox_active) {
6720                 list_add_tail(&psli->mbox_active->list, &completions);
6721                 psli->mbox_active = NULL;
6722                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
6723         }
6724         /* The completed mailbox command queue */
6725         list_splice_init(&phba->sli.mboxq_cmpl, &completions);
6726         spin_unlock_irqrestore(&phba->hbalock, iflag);
6727
6728         /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
6729         while (!list_empty(&completions)) {
6730                 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
6731                 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
6732                 if (pmb->mbox_cmpl)
6733                         pmb->mbox_cmpl(phba, pmb);
6734         }
6735 }
6736
6737 /**
6738  * lpfc_sli_host_down - Vport cleanup function
6739  * @vport: Pointer to virtual port object.
6740  *
6741  * lpfc_sli_host_down is called to clean up the resources
6742  * associated with a vport before destroying virtual
6743  * port data structures.
6744  * This function does following operations:
6745  * - Free discovery resources associated with this virtual
6746  *   port.
6747  * - Free iocbs associated with this virtual port in
6748  *   the txq.
6749  * - Send abort for all iocb commands associated with this
6750  *   vport in txcmplq.
6751  *
6752  * This function is called with no lock held and always returns 1.
6753  **/
6754 int
6755 lpfc_sli_host_down(struct lpfc_vport *vport)
6756 {
6757         LIST_HEAD(completions);
6758         struct lpfc_hba *phba = vport->phba;
6759         struct lpfc_sli *psli = &phba->sli;
6760         struct lpfc_sli_ring *pring;
6761         struct lpfc_iocbq *iocb, *next_iocb;
6762         int i;
6763         unsigned long flags = 0;
6764         uint16_t prev_pring_flag;
6765
6766         lpfc_cleanup_discovery_resources(vport);
6767
6768         spin_lock_irqsave(&phba->hbalock, flags);
6769         for (i = 0; i < psli->num_rings; i++) {
6770                 pring = &psli->ring[i];
6771                 prev_pring_flag = pring->flag;
6772                 /* Only slow rings */
6773                 if (pring->ringno == LPFC_ELS_RING) {
6774                         pring->flag |= LPFC_DEFERRED_RING_EVENT;
6775                         /* Set the lpfc data pending flag */
6776                         set_bit(LPFC_DATA_READY, &phba->data_flags);
6777                 }
6778                 /*
6779                  * Error everything on the txq since these iocbs have not been
6780                  * given to the FW yet.
6781                  */
6782                 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
6783                         if (iocb->vport != vport)
6784                                 continue;
6785                         list_move_tail(&iocb->list, &completions);
6786                         pring->txq_cnt--;
6787                 }
6788
6789                 /* Next issue ABTS for everything on the txcmplq */
6790                 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
6791                                                                         list) {
6792                         if (iocb->vport != vport)
6793                                 continue;
6794                         lpfc_sli_issue_abort_iotag(phba, pring, iocb);
6795                 }
6796
6797                 pring->flag = prev_pring_flag;
6798         }
6799
6800         spin_unlock_irqrestore(&phba->hbalock, flags);
6801
6802         /* Cancel all the IOCBs from the completions list */
6803         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
6804                               IOERR_SLI_DOWN);
6805         return 1;
6806 }
6807
6808 /**
6809  * lpfc_sli_hba_down - Resource cleanup function for the HBA
6810  * @phba: Pointer to HBA context object.
6811  *
6812  * This function cleans up all iocb, buffers, mailbox commands
6813  * while shutting down the HBA. This function is called with no
6814  * lock held and always returns 1.
6815  * This function does the following to cleanup driver resources:
6816  * - Free discovery resources for each virtual port
6817  * - Cleanup any pending fabric iocbs
6818  * - Iterate through the iocb txq and free each entry
6819  *   in the list.
6820  * - Free up any buffer posted to the HBA
6821  * - Free mailbox commands in the mailbox queue.
6822  **/
6823 int
6824 lpfc_sli_hba_down(struct lpfc_hba *phba)
6825 {
6826         LIST_HEAD(completions);
6827         struct lpfc_sli *psli = &phba->sli;
6828         struct lpfc_sli_ring *pring;
6829         struct lpfc_dmabuf *buf_ptr;
6830         unsigned long flags = 0;
6831         int i;
6832
6833         /* Shutdown the mailbox command sub-system */
6834         lpfc_sli_mbox_sys_shutdown(phba);
6835
6836         lpfc_hba_down_prep(phba);
6837
6838         lpfc_fabric_abort_hba(phba);
6839
6840         spin_lock_irqsave(&phba->hbalock, flags);
6841         for (i = 0; i < psli->num_rings; i++) {
6842                 pring = &psli->ring[i];
6843                 /* Only slow rings */
6844                 if (pring->ringno == LPFC_ELS_RING) {
6845                         pring->flag |= LPFC_DEFERRED_RING_EVENT;
6846                         /* Set the lpfc data pending flag */
6847                         set_bit(LPFC_DATA_READY, &phba->data_flags);
6848                 }
6849
6850                 /*
6851                  * Error everything on the txq since these iocbs have not been
6852                  * given to the FW yet.
6853                  */
6854                 list_splice_init(&pring->txq, &completions);
6855                 pring->txq_cnt = 0;
6856
6857         }
6858         spin_unlock_irqrestore(&phba->hbalock, flags);
6859
6860         /* Cancel all the IOCBs from the completions list */
6861         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
6862                               IOERR_SLI_DOWN);
6863
6864         spin_lock_irqsave(&phba->hbalock, flags);
6865         list_splice_init(&phba->elsbuf, &completions);
6866         phba->elsbuf_cnt = 0;
6867         phba->elsbuf_prev_cnt = 0;
6868         spin_unlock_irqrestore(&phba->hbalock, flags);
6869
6870         while (!list_empty(&completions)) {
6871                 list_remove_head(&completions, buf_ptr,
6872                         struct lpfc_dmabuf, list);
6873                 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
6874                 kfree(buf_ptr);
6875         }
6876
6877         /* Return any active mbox cmds */
6878         del_timer_sync(&psli->mbox_tmo);
6879
6880         spin_lock_irqsave(&phba->pport->work_port_lock, flags);
6881         phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
6882         spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
6883
6884         return 1;
6885 }
6886
6887 /**
6888  * lpfc_sli4_hba_down - PCI function resource cleanup for the SLI4 HBA
6889  * @phba: Pointer to HBA context object.
6890  *
6891  * This function cleans up all queues, iocb, buffers, mailbox commands while
6892  * shutting down the SLI4 HBA FCoE function. This function is called with no
6893  * lock held and always returns 1.
6894  *
6895  * This function does the following to cleanup driver FCoE function resources:
6896  * - Free discovery resources for each virtual port
6897  * - Cleanup any pending fabric iocbs
6898  * - Iterate through the iocb txq and free each entry in the list.
6899  * - Free up any buffer posted to the HBA.
6900  * - Clean up all the queue entries: WQ, RQ, MQ, EQ, CQ, etc.
6901  * - Free mailbox commands in the mailbox queue.
6902  **/
6903 int
6904 lpfc_sli4_hba_down(struct lpfc_hba *phba)
6905 {
6906         /* Stop the SLI4 device port */
6907         lpfc_stop_port(phba);
6908
6909         /* Tear down the queues in the HBA */
6910         lpfc_sli4_queue_unset(phba);
6911
6912         /* unregister default FCFI from the HBA */
6913         lpfc_sli4_fcfi_unreg(phba, phba->fcf.fcfi);
6914
6915         return 1;
6916 }
6917
6918 /**
6919  * lpfc_sli_pcimem_bcopy - SLI memory copy function
6920  * @srcp: Source memory pointer.
6921  * @destp: Destination memory pointer.
6922  * @cnt: Number of words required to be copied.
6923  *
6924  * This function is used for copying data between driver memory
6925  * and the SLI memory. This function also changes the endianness
6926  * of each word if native endianness is different from SLI
6927  * endianness. This function can be called with or without
6928  * lock.
6929  **/
6930 void
6931 lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
6932 {
6933         uint32_t *src = srcp;
6934         uint32_t *dest = destp;
6935         uint32_t ldata;
6936         int i;
6937
6938         for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
6939                 ldata = *src;
6940                 ldata = le32_to_cpu(ldata);
6941                 *dest = ldata;
6942                 src++;
6943                 dest++;
6944         }
6945 }
6946
6947
6948 /**
6949  * lpfc_sli_bemem_bcopy - SLI memory copy function
6950  * @srcp: Source memory pointer.
6951  * @destp: Destination memory pointer.
6952  * @cnt: Number of words required to be copied.
6953  *
6954  * This function is used for copying data between a data structure
6955  * with big endian representation to local endianness.
6956  * This function can be called with or without lock.
6957  **/
6958 void
6959 lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
6960 {
6961         uint32_t *src = srcp;
6962         uint32_t *dest = destp;
6963         uint32_t ldata;
6964         int i;
6965
6966         for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
6967                 ldata = *src;
6968                 ldata = be32_to_cpu(ldata);
6969                 *dest = ldata;
6970                 src++;
6971                 dest++;
6972         }
6973 }
6974
6975 /**
6976  * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
6977  * @phba: Pointer to HBA context object.
6978  * @pring: Pointer to driver SLI ring object.
6979  * @mp: Pointer to driver buffer object.
6980  *
6981  * This function is called with no lock held.
6982  * It always return zero after adding the buffer to the postbufq
6983  * buffer list.
6984  **/
6985 int
6986 lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
6987                          struct lpfc_dmabuf *mp)
6988 {
6989         /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
6990            later */
6991         spin_lock_irq(&phba->hbalock);
6992         list_add_tail(&mp->list, &pring->postbufq);
6993         pring->postbufq_cnt++;
6994         spin_unlock_irq(&phba->hbalock);
6995         return 0;
6996 }
6997
6998 /**
6999  * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
7000  * @phba: Pointer to HBA context object.
7001  *
7002  * When HBQ is enabled, buffers are searched based on tags. This function
7003  * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
7004  * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
7005  * does not conflict with tags of buffer posted for unsolicited events.
7006  * The function returns the allocated tag. The function is called with
7007  * no locks held.
7008  **/
7009 uint32_t
7010 lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
7011 {
7012         spin_lock_irq(&phba->hbalock);
7013         phba->buffer_tag_count++;
7014         /*
7015          * Always set the QUE_BUFTAG_BIT to distiguish between
7016          * a tag assigned by HBQ.
7017          */
7018         phba->buffer_tag_count |= QUE_BUFTAG_BIT;
7019         spin_unlock_irq(&phba->hbalock);
7020         return phba->buffer_tag_count;
7021 }
7022
7023 /**
7024  * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
7025  * @phba: Pointer to HBA context object.
7026  * @pring: Pointer to driver SLI ring object.
7027  * @tag: Buffer tag.
7028  *
7029  * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
7030  * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
7031  * iocb is posted to the response ring with the tag of the buffer.
7032  * This function searches the pring->postbufq list using the tag
7033  * to find buffer associated with CMD_IOCB_RET_XRI64_CX
7034  * iocb. If the buffer is found then lpfc_dmabuf object of the
7035  * buffer is returned to the caller else NULL is returned.
7036  * This function is called with no lock held.
7037  **/
7038 struct lpfc_dmabuf *
7039 lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7040                         uint32_t tag)
7041 {
7042         struct lpfc_dmabuf *mp, *next_mp;
7043         struct list_head *slp = &pring->postbufq;
7044
7045         /* Search postbufq, from the begining, looking for a match on tag */
7046         spin_lock_irq(&phba->hbalock);
7047         list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
7048                 if (mp->buffer_tag == tag) {
7049                         list_del_init(&mp->list);
7050                         pring->postbufq_cnt--;
7051                         spin_unlock_irq(&phba->hbalock);
7052                         return mp;
7053                 }
7054         }
7055
7056         spin_unlock_irq(&phba->hbalock);
7057         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7058                         "0402 Cannot find virtual addr for buffer tag on "
7059                         "ring %d Data x%lx x%p x%p x%x\n",
7060                         pring->ringno, (unsigned long) tag,
7061                         slp->next, slp->prev, pring->postbufq_cnt);
7062
7063         return NULL;
7064 }
7065
7066 /**
7067  * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
7068  * @phba: Pointer to HBA context object.
7069  * @pring: Pointer to driver SLI ring object.
7070  * @phys: DMA address of the buffer.
7071  *
7072  * This function searches the buffer list using the dma_address
7073  * of unsolicited event to find the driver's lpfc_dmabuf object
7074  * corresponding to the dma_address. The function returns the
7075  * lpfc_dmabuf object if a buffer is found else it returns NULL.
7076  * This function is called by the ct and els unsolicited event
7077  * handlers to get the buffer associated with the unsolicited
7078  * event.
7079  *
7080  * This function is called with no lock held.
7081  **/
7082 struct lpfc_dmabuf *
7083 lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7084                          dma_addr_t phys)
7085 {
7086         struct lpfc_dmabuf *mp, *next_mp;
7087         struct list_head *slp = &pring->postbufq;
7088
7089         /* Search postbufq, from the begining, looking for a match on phys */
7090         spin_lock_irq(&phba->hbalock);
7091         list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
7092                 if (mp->phys == phys) {
7093                         list_del_init(&mp->list);
7094                         pring->postbufq_cnt--;
7095                         spin_unlock_irq(&phba->hbalock);
7096                         return mp;
7097                 }
7098         }
7099
7100         spin_unlock_irq(&phba->hbalock);
7101         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7102                         "0410 Cannot find virtual addr for mapped buf on "
7103                         "ring %d Data x%llx x%p x%p x%x\n",
7104                         pring->ringno, (unsigned long long)phys,
7105                         slp->next, slp->prev, pring->postbufq_cnt);
7106         return NULL;
7107 }
7108
7109 /**
7110  * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
7111  * @phba: Pointer to HBA context object.
7112  * @cmdiocb: Pointer to driver command iocb object.
7113  * @rspiocb: Pointer to driver response iocb object.
7114  *
7115  * This function is the completion handler for the abort iocbs for
7116  * ELS commands. This function is called from the ELS ring event
7117  * handler with no lock held. This function frees memory resources
7118  * associated with the abort iocb.
7119  **/
7120 static void
7121 lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7122                         struct lpfc_iocbq *rspiocb)
7123 {
7124         IOCB_t *irsp = &rspiocb->iocb;
7125         uint16_t abort_iotag, abort_context;
7126         struct lpfc_iocbq *abort_iocb;
7127         struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
7128
7129         abort_iocb = NULL;
7130
7131         if (irsp->ulpStatus) {
7132                 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
7133                 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
7134
7135                 spin_lock_irq(&phba->hbalock);
7136                 if (phba->sli_rev < LPFC_SLI_REV4) {
7137                         if (abort_iotag != 0 &&
7138                                 abort_iotag <= phba->sli.last_iotag)
7139                                 abort_iocb =
7140                                         phba->sli.iocbq_lookup[abort_iotag];
7141                 } else
7142                         /* For sli4 the abort_tag is the XRI,
7143                          * so the abort routine puts the iotag  of the iocb
7144                          * being aborted in the context field of the abort
7145                          * IOCB.
7146                          */
7147                         abort_iocb = phba->sli.iocbq_lookup[abort_context];
7148
7149                 /*
7150                  *  If the iocb is not found in Firmware queue the iocb
7151                  *  might have completed already. Do not free it again.
7152                  */
7153                 if (irsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
7154                         if (irsp->un.ulpWord[4] != IOERR_NO_XRI) {
7155                                 spin_unlock_irq(&phba->hbalock);
7156                                 lpfc_sli_release_iocbq(phba, cmdiocb);
7157                                 return;
7158                         }
7159                         /* For SLI4 the ulpContext field for abort IOCB
7160                          * holds the iotag of the IOCB being aborted so
7161                          * the local abort_context needs to be reset to
7162                          * match the aborted IOCBs ulpContext.
7163                          */
7164                         if (abort_iocb && phba->sli_rev == LPFC_SLI_REV4)
7165                                 abort_context = abort_iocb->iocb.ulpContext;
7166                 }
7167
7168                 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
7169                                 "0327 Cannot abort els iocb %p "
7170                                 "with tag %x context %x, abort status %x, "
7171                                 "abort code %x\n",
7172                                 abort_iocb, abort_iotag, abort_context,
7173                                 irsp->ulpStatus, irsp->un.ulpWord[4]);
7174                 /*
7175                  * make sure we have the right iocbq before taking it
7176                  * off the txcmplq and try to call completion routine.
7177                  */
7178                 if (!abort_iocb ||
7179                     abort_iocb->iocb.ulpContext != abort_context ||
7180                     (abort_iocb->iocb_flag & LPFC_DRIVER_ABORTED) == 0)
7181                         spin_unlock_irq(&phba->hbalock);
7182                 else if (phba->sli_rev < LPFC_SLI_REV4) {
7183                         /*
7184                          * leave the SLI4 aborted command on the txcmplq
7185                          * list and the command complete WCQE's XB bit
7186                          * will tell whether the SGL (XRI) can be released
7187                          * immediately or to the aborted SGL list for the
7188                          * following abort XRI from the HBA.
7189                          */
7190                         list_del_init(&abort_iocb->list);
7191                         if (abort_iocb->iocb_flag & LPFC_IO_ON_Q) {
7192                                 abort_iocb->iocb_flag &= ~LPFC_IO_ON_Q;
7193                                 pring->txcmplq_cnt--;
7194                         }
7195
7196                         /* Firmware could still be in progress of DMAing
7197                          * payload, so don't free data buffer till after
7198                          * a hbeat.
7199                          */
7200                         abort_iocb->iocb_flag |= LPFC_DELAY_MEM_FREE;
7201                         abort_iocb->iocb_flag &= ~LPFC_DRIVER_ABORTED;
7202                         spin_unlock_irq(&phba->hbalock);
7203
7204                         abort_iocb->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
7205                         abort_iocb->iocb.un.ulpWord[4] = IOERR_ABORT_REQUESTED;
7206                         (abort_iocb->iocb_cmpl)(phba, abort_iocb, abort_iocb);
7207                 } else
7208                         spin_unlock_irq(&phba->hbalock);
7209         }
7210
7211         lpfc_sli_release_iocbq(phba, cmdiocb);
7212         return;
7213 }
7214
7215 /**
7216  * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
7217  * @phba: Pointer to HBA context object.
7218  * @cmdiocb: Pointer to driver command iocb object.
7219  * @rspiocb: Pointer to driver response iocb object.
7220  *
7221  * The function is called from SLI ring event handler with no
7222  * lock held. This function is the completion handler for ELS commands
7223  * which are aborted. The function frees memory resources used for
7224  * the aborted ELS commands.
7225  **/
7226 static void
7227 lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7228                      struct lpfc_iocbq *rspiocb)
7229 {
7230         IOCB_t *irsp = &rspiocb->iocb;
7231
7232         /* ELS cmd tag <ulpIoTag> completes */
7233         lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
7234                         "0139 Ignoring ELS cmd tag x%x completion Data: "
7235                         "x%x x%x x%x\n",
7236                         irsp->ulpIoTag, irsp->ulpStatus,
7237                         irsp->un.ulpWord[4], irsp->ulpTimeout);
7238         if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
7239                 lpfc_ct_free_iocb(phba, cmdiocb);
7240         else
7241                 lpfc_els_free_iocb(phba, cmdiocb);
7242         return;
7243 }
7244
7245 /**
7246  * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
7247  * @phba: Pointer to HBA context object.
7248  * @pring: Pointer to driver SLI ring object.
7249  * @cmdiocb: Pointer to driver command iocb object.
7250  *
7251  * This function issues an abort iocb for the provided command
7252  * iocb. This function is called with hbalock held.
7253  * The function returns 0 when it fails due to memory allocation
7254  * failure or when the command iocb is an abort request.
7255  **/
7256 int
7257 lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7258                            struct lpfc_iocbq *cmdiocb)
7259 {
7260         struct lpfc_vport *vport = cmdiocb->vport;
7261         struct lpfc_iocbq *abtsiocbp;
7262         IOCB_t *icmd = NULL;
7263         IOCB_t *iabt = NULL;
7264         int retval = IOCB_ERROR;
7265
7266         /*
7267          * There are certain command types we don't want to abort.  And we
7268          * don't want to abort commands that are already in the process of
7269          * being aborted.
7270          */
7271         icmd = &cmdiocb->iocb;
7272         if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
7273             icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
7274             (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
7275                 return 0;
7276
7277         /* If we're unloading, don't abort iocb on the ELS ring, but change the
7278          * callback so that nothing happens when it finishes.
7279          */
7280         if ((vport->load_flag & FC_UNLOADING) &&
7281             (pring->ringno == LPFC_ELS_RING)) {
7282                 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
7283                         cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
7284                 else
7285                         cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
7286                 goto abort_iotag_exit;
7287         }
7288
7289         /* issue ABTS for this IOCB based on iotag */
7290         abtsiocbp = __lpfc_sli_get_iocbq(phba);
7291         if (abtsiocbp == NULL)
7292                 return 0;
7293
7294         /* This signals the response to set the correct status
7295          * before calling the completion handler
7296          */
7297         cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
7298
7299         iabt = &abtsiocbp->iocb;
7300         iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
7301         iabt->un.acxri.abortContextTag = icmd->ulpContext;
7302         if (phba->sli_rev == LPFC_SLI_REV4) {
7303                 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
7304                 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
7305         }
7306         else
7307                 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
7308         iabt->ulpLe = 1;
7309         iabt->ulpClass = icmd->ulpClass;
7310
7311         /* ABTS WQE must go to the same WQ as the WQE to be aborted */
7312         abtsiocbp->fcp_wqidx = cmdiocb->fcp_wqidx;
7313         if (cmdiocb->iocb_flag & LPFC_IO_FCP)
7314                 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
7315
7316         if (phba->link_state >= LPFC_LINK_UP)
7317                 iabt->ulpCommand = CMD_ABORT_XRI_CN;
7318         else
7319                 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
7320
7321         abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
7322
7323         lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
7324                          "0339 Abort xri x%x, original iotag x%x, "
7325                          "abort cmd iotag x%x\n",
7326                          iabt->un.acxri.abortIoTag,
7327                          iabt->un.acxri.abortContextTag,
7328                          abtsiocbp->iotag);
7329         retval = __lpfc_sli_issue_iocb(phba, pring->ringno, abtsiocbp, 0);
7330
7331         if (retval)
7332                 __lpfc_sli_release_iocbq(phba, abtsiocbp);
7333 abort_iotag_exit:
7334         /*
7335          * Caller to this routine should check for IOCB_ERROR
7336          * and handle it properly.  This routine no longer removes
7337          * iocb off txcmplq and call compl in case of IOCB_ERROR.
7338          */
7339         return retval;
7340 }
7341
7342 /**
7343  * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
7344  * @iocbq: Pointer to driver iocb object.
7345  * @vport: Pointer to driver virtual port object.
7346  * @tgt_id: SCSI ID of the target.
7347  * @lun_id: LUN ID of the scsi device.
7348  * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
7349  *
7350  * This function acts as an iocb filter for functions which abort or count
7351  * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
7352  * 0 if the filtering criteria is met for the given iocb and will return
7353  * 1 if the filtering criteria is not met.
7354  * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
7355  * given iocb is for the SCSI device specified by vport, tgt_id and
7356  * lun_id parameter.
7357  * If ctx_cmd == LPFC_CTX_TGT,  the function returns 0 only if the
7358  * given iocb is for the SCSI target specified by vport and tgt_id
7359  * parameters.
7360  * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
7361  * given iocb is for the SCSI host associated with the given vport.
7362  * This function is called with no locks held.
7363  **/
7364 static int
7365 lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
7366                            uint16_t tgt_id, uint64_t lun_id,
7367                            lpfc_ctx_cmd ctx_cmd)
7368 {
7369         struct lpfc_scsi_buf *lpfc_cmd;
7370         int rc = 1;
7371
7372         if (!(iocbq->iocb_flag &  LPFC_IO_FCP))
7373                 return rc;
7374
7375         if (iocbq->vport != vport)
7376                 return rc;
7377
7378         lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
7379
7380         if (lpfc_cmd->pCmd == NULL)
7381                 return rc;
7382
7383         switch (ctx_cmd) {
7384         case LPFC_CTX_LUN:
7385                 if ((lpfc_cmd->rdata->pnode) &&
7386                     (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
7387                     (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
7388                         rc = 0;
7389                 break;
7390         case LPFC_CTX_TGT:
7391                 if ((lpfc_cmd->rdata->pnode) &&
7392                     (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
7393                         rc = 0;
7394                 break;
7395         case LPFC_CTX_HOST:
7396                 rc = 0;
7397                 break;
7398         default:
7399                 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
7400                         __func__, ctx_cmd);
7401                 break;
7402         }
7403
7404         return rc;
7405 }
7406
7407 /**
7408  * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
7409  * @vport: Pointer to virtual port.
7410  * @tgt_id: SCSI ID of the target.
7411  * @lun_id: LUN ID of the scsi device.
7412  * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
7413  *
7414  * This function returns number of FCP commands pending for the vport.
7415  * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
7416  * commands pending on the vport associated with SCSI device specified
7417  * by tgt_id and lun_id parameters.
7418  * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
7419  * commands pending on the vport associated with SCSI target specified
7420  * by tgt_id parameter.
7421  * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
7422  * commands pending on the vport.
7423  * This function returns the number of iocbs which satisfy the filter.
7424  * This function is called without any lock held.
7425  **/
7426 int
7427 lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
7428                   lpfc_ctx_cmd ctx_cmd)
7429 {
7430         struct lpfc_hba *phba = vport->phba;
7431         struct lpfc_iocbq *iocbq;
7432         int sum, i;
7433
7434         for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
7435                 iocbq = phba->sli.iocbq_lookup[i];
7436
7437                 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
7438                                                 ctx_cmd) == 0)
7439                         sum++;
7440         }
7441
7442         return sum;
7443 }
7444
7445 /**
7446  * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
7447  * @phba: Pointer to HBA context object
7448  * @cmdiocb: Pointer to command iocb object.
7449  * @rspiocb: Pointer to response iocb object.
7450  *
7451  * This function is called when an aborted FCP iocb completes. This
7452  * function is called by the ring event handler with no lock held.
7453  * This function frees the iocb.
7454  **/
7455 void
7456 lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7457                         struct lpfc_iocbq *rspiocb)
7458 {
7459         lpfc_sli_release_iocbq(phba, cmdiocb);
7460         return;
7461 }
7462
7463 /**
7464  * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
7465  * @vport: Pointer to virtual port.
7466  * @pring: Pointer to driver SLI ring object.
7467  * @tgt_id: SCSI ID of the target.
7468  * @lun_id: LUN ID of the scsi device.
7469  * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
7470  *
7471  * This function sends an abort command for every SCSI command
7472  * associated with the given virtual port pending on the ring
7473  * filtered by lpfc_sli_validate_fcp_iocb function.
7474  * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
7475  * FCP iocbs associated with lun specified by tgt_id and lun_id
7476  * parameters
7477  * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
7478  * FCP iocbs associated with SCSI target specified by tgt_id parameter.
7479  * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
7480  * FCP iocbs associated with virtual port.
7481  * This function returns number of iocbs it failed to abort.
7482  * This function is called with no locks held.
7483  **/
7484 int
7485 lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
7486                     uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
7487 {
7488         struct lpfc_hba *phba = vport->phba;
7489         struct lpfc_iocbq *iocbq;
7490         struct lpfc_iocbq *abtsiocb;
7491         IOCB_t *cmd = NULL;
7492         int errcnt = 0, ret_val = 0;
7493         int i;
7494
7495         for (i = 1; i <= phba->sli.last_iotag; i++) {
7496                 iocbq = phba->sli.iocbq_lookup[i];
7497
7498                 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
7499                                                abort_cmd) != 0)
7500                         continue;
7501
7502                 /* issue ABTS for this IOCB based on iotag */
7503                 abtsiocb = lpfc_sli_get_iocbq(phba);
7504                 if (abtsiocb == NULL) {
7505                         errcnt++;
7506                         continue;
7507                 }
7508
7509                 cmd = &iocbq->iocb;
7510                 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
7511                 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
7512                 if (phba->sli_rev == LPFC_SLI_REV4)
7513                         abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
7514                 else
7515                         abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
7516                 abtsiocb->iocb.ulpLe = 1;
7517                 abtsiocb->iocb.ulpClass = cmd->ulpClass;
7518                 abtsiocb->vport = phba->pport;
7519
7520                 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
7521                 abtsiocb->fcp_wqidx = iocbq->fcp_wqidx;
7522                 if (iocbq->iocb_flag & LPFC_IO_FCP)
7523                         abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
7524
7525                 if (lpfc_is_link_up(phba))
7526                         abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
7527                 else
7528                         abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
7529
7530                 /* Setup callback routine and issue the command. */
7531                 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
7532                 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
7533                                               abtsiocb, 0);
7534                 if (ret_val == IOCB_ERROR) {
7535                         lpfc_sli_release_iocbq(phba, abtsiocb);
7536                         errcnt++;
7537                         continue;
7538                 }
7539         }
7540
7541         return errcnt;
7542 }
7543
7544 /**
7545  * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
7546  * @phba: Pointer to HBA context object.
7547  * @cmdiocbq: Pointer to command iocb.
7548  * @rspiocbq: Pointer to response iocb.
7549  *
7550  * This function is the completion handler for iocbs issued using
7551  * lpfc_sli_issue_iocb_wait function. This function is called by the
7552  * ring event handler function without any lock held. This function
7553  * can be called from both worker thread context and interrupt
7554  * context. This function also can be called from other thread which
7555  * cleans up the SLI layer objects.
7556  * This function copy the contents of the response iocb to the
7557  * response iocb memory object provided by the caller of
7558  * lpfc_sli_issue_iocb_wait and then wakes up the thread which
7559  * sleeps for the iocb completion.
7560  **/
7561 static void
7562 lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
7563                         struct lpfc_iocbq *cmdiocbq,
7564                         struct lpfc_iocbq *rspiocbq)
7565 {
7566         wait_queue_head_t *pdone_q;
7567         unsigned long iflags;
7568         struct lpfc_scsi_buf *lpfc_cmd;
7569
7570         spin_lock_irqsave(&phba->hbalock, iflags);
7571         cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
7572         if (cmdiocbq->context2 && rspiocbq)
7573                 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
7574                        &rspiocbq->iocb, sizeof(IOCB_t));
7575
7576         /* Set the exchange busy flag for task management commands */
7577         if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
7578                 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
7579                 lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
7580                         cur_iocbq);
7581                 lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
7582         }
7583
7584         pdone_q = cmdiocbq->context_un.wait_queue;
7585         if (pdone_q)
7586                 wake_up(pdone_q);
7587         spin_unlock_irqrestore(&phba->hbalock, iflags);
7588         return;
7589 }
7590
7591 /**
7592  * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
7593  * @phba: Pointer to HBA context object..
7594  * @piocbq: Pointer to command iocb.
7595  * @flag: Flag to test.
7596  *
7597  * This routine grabs the hbalock and then test the iocb_flag to
7598  * see if the passed in flag is set.
7599  * Returns:
7600  * 1 if flag is set.
7601  * 0 if flag is not set.
7602  **/
7603 static int
7604 lpfc_chk_iocb_flg(struct lpfc_hba *phba,
7605                  struct lpfc_iocbq *piocbq, uint32_t flag)
7606 {
7607         unsigned long iflags;
7608         int ret;
7609
7610         spin_lock_irqsave(&phba->hbalock, iflags);
7611         ret = piocbq->iocb_flag & flag;
7612         spin_unlock_irqrestore(&phba->hbalock, iflags);
7613         return ret;
7614
7615 }
7616
7617 /**
7618  * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
7619  * @phba: Pointer to HBA context object..
7620  * @pring: Pointer to sli ring.
7621  * @piocb: Pointer to command iocb.
7622  * @prspiocbq: Pointer to response iocb.
7623  * @timeout: Timeout in number of seconds.
7624  *
7625  * This function issues the iocb to firmware and waits for the
7626  * iocb to complete. If the iocb command is not
7627  * completed within timeout seconds, it returns IOCB_TIMEDOUT.
7628  * Caller should not free the iocb resources if this function
7629  * returns IOCB_TIMEDOUT.
7630  * The function waits for the iocb completion using an
7631  * non-interruptible wait.
7632  * This function will sleep while waiting for iocb completion.
7633  * So, this function should not be called from any context which
7634  * does not allow sleeping. Due to the same reason, this function
7635  * cannot be called with interrupt disabled.
7636  * This function assumes that the iocb completions occur while
7637  * this function sleep. So, this function cannot be called from
7638  * the thread which process iocb completion for this ring.
7639  * This function clears the iocb_flag of the iocb object before
7640  * issuing the iocb and the iocb completion handler sets this
7641  * flag and wakes this thread when the iocb completes.
7642  * The contents of the response iocb will be copied to prspiocbq
7643  * by the completion handler when the command completes.
7644  * This function returns IOCB_SUCCESS when success.
7645  * This function is called with no lock held.
7646  **/
7647 int
7648 lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
7649                          uint32_t ring_number,
7650                          struct lpfc_iocbq *piocb,
7651                          struct lpfc_iocbq *prspiocbq,
7652                          uint32_t timeout)
7653 {
7654         DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
7655         long timeleft, timeout_req = 0;
7656         int retval = IOCB_SUCCESS;
7657         uint32_t creg_val;
7658         struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
7659         /*
7660          * If the caller has provided a response iocbq buffer, then context2
7661          * is NULL or its an error.
7662          */
7663         if (prspiocbq) {
7664                 if (piocb->context2)
7665                         return IOCB_ERROR;
7666                 piocb->context2 = prspiocbq;
7667         }
7668
7669         piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
7670         piocb->context_un.wait_queue = &done_q;
7671         piocb->iocb_flag &= ~LPFC_IO_WAKE;
7672
7673         if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
7674                 creg_val = readl(phba->HCregaddr);
7675                 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
7676                 writel(creg_val, phba->HCregaddr);
7677                 readl(phba->HCregaddr); /* flush */
7678         }
7679
7680         retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
7681                                      SLI_IOCB_RET_IOCB);
7682         if (retval == IOCB_SUCCESS) {
7683                 timeout_req = timeout * HZ;
7684                 timeleft = wait_event_timeout(done_q,
7685                                 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
7686                                 timeout_req);
7687
7688                 if (piocb->iocb_flag & LPFC_IO_WAKE) {
7689                         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
7690                                         "0331 IOCB wake signaled\n");
7691                 } else if (timeleft == 0) {
7692                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7693                                         "0338 IOCB wait timeout error - no "
7694                                         "wake response Data x%x\n", timeout);
7695                         retval = IOCB_TIMEDOUT;
7696                 } else {
7697                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7698                                         "0330 IOCB wake NOT set, "
7699                                         "Data x%x x%lx\n",
7700                                         timeout, (timeleft / jiffies));
7701                         retval = IOCB_TIMEDOUT;
7702                 }
7703         } else if (retval == IOCB_BUSY) {
7704                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
7705                         "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
7706                         phba->iocb_cnt, pring->txq_cnt, pring->txcmplq_cnt);
7707                 return retval;
7708         } else {
7709                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
7710                                 "0332 IOCB wait issue failed, Data x%x\n",
7711                                 retval);
7712                 retval = IOCB_ERROR;
7713         }
7714
7715         if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
7716                 creg_val = readl(phba->HCregaddr);
7717                 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
7718                 writel(creg_val, phba->HCregaddr);
7719                 readl(phba->HCregaddr); /* flush */
7720         }
7721
7722         if (prspiocbq)
7723                 piocb->context2 = NULL;
7724
7725         piocb->context_un.wait_queue = NULL;
7726         piocb->iocb_cmpl = NULL;
7727         return retval;
7728 }
7729
7730 /**
7731  * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
7732  * @phba: Pointer to HBA context object.
7733  * @pmboxq: Pointer to driver mailbox object.
7734  * @timeout: Timeout in number of seconds.
7735  *
7736  * This function issues the mailbox to firmware and waits for the
7737  * mailbox command to complete. If the mailbox command is not
7738  * completed within timeout seconds, it returns MBX_TIMEOUT.
7739  * The function waits for the mailbox completion using an
7740  * interruptible wait. If the thread is woken up due to a
7741  * signal, MBX_TIMEOUT error is returned to the caller. Caller
7742  * should not free the mailbox resources, if this function returns
7743  * MBX_TIMEOUT.
7744  * This function will sleep while waiting for mailbox completion.
7745  * So, this function should not be called from any context which
7746  * does not allow sleeping. Due to the same reason, this function
7747  * cannot be called with interrupt disabled.
7748  * This function assumes that the mailbox completion occurs while
7749  * this function sleep. So, this function cannot be called from
7750  * the worker thread which processes mailbox completion.
7751  * This function is called in the context of HBA management
7752  * applications.
7753  * This function returns MBX_SUCCESS when successful.
7754  * This function is called with no lock held.
7755  **/
7756 int
7757 lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
7758                          uint32_t timeout)
7759 {
7760         DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
7761         int retval;
7762         unsigned long flag;
7763
7764         /* The caller must leave context1 empty. */
7765         if (pmboxq->context1)
7766                 return MBX_NOT_FINISHED;
7767
7768         pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
7769         /* setup wake call as IOCB callback */
7770         pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
7771         /* setup context field to pass wait_queue pointer to wake function  */
7772         pmboxq->context1 = &done_q;
7773
7774         /* now issue the command */
7775         retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
7776
7777         if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
7778                 wait_event_interruptible_timeout(done_q,
7779                                 pmboxq->mbox_flag & LPFC_MBX_WAKE,
7780                                 timeout * HZ);
7781
7782                 spin_lock_irqsave(&phba->hbalock, flag);
7783                 pmboxq->context1 = NULL;
7784                 /*
7785                  * if LPFC_MBX_WAKE flag is set the mailbox is completed
7786                  * else do not free the resources.
7787                  */
7788                 if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
7789                         retval = MBX_SUCCESS;
7790                         lpfc_sli4_swap_str(phba, pmboxq);
7791                 } else {
7792                         retval = MBX_TIMEOUT;
7793                         pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
7794                 }
7795                 spin_unlock_irqrestore(&phba->hbalock, flag);
7796         }
7797
7798         return retval;
7799 }
7800
7801 /**
7802  * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
7803  * @phba: Pointer to HBA context.
7804  *
7805  * This function is called to shutdown the driver's mailbox sub-system.
7806  * It first marks the mailbox sub-system is in a block state to prevent
7807  * the asynchronous mailbox command from issued off the pending mailbox
7808  * command queue. If the mailbox command sub-system shutdown is due to
7809  * HBA error conditions such as EEH or ERATT, this routine shall invoke
7810  * the mailbox sub-system flush routine to forcefully bring down the
7811  * mailbox sub-system. Otherwise, if it is due to normal condition (such
7812  * as with offline or HBA function reset), this routine will wait for the
7813  * outstanding mailbox command to complete before invoking the mailbox
7814  * sub-system flush routine to gracefully bring down mailbox sub-system.
7815  **/
7816 void
7817 lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba)
7818 {
7819         struct lpfc_sli *psli = &phba->sli;
7820         uint8_t actcmd = MBX_HEARTBEAT;
7821         unsigned long timeout;
7822
7823         spin_lock_irq(&phba->hbalock);
7824         psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
7825         spin_unlock_irq(&phba->hbalock);
7826
7827         if (psli->sli_flag & LPFC_SLI_ACTIVE) {
7828                 spin_lock_irq(&phba->hbalock);
7829                 if (phba->sli.mbox_active)
7830                         actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
7831                 spin_unlock_irq(&phba->hbalock);
7832                 /* Determine how long we might wait for the active mailbox
7833                  * command to be gracefully completed by firmware.
7834                  */
7835                 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, actcmd) *
7836                                            1000) + jiffies;
7837                 while (phba->sli.mbox_active) {
7838                         /* Check active mailbox complete status every 2ms */
7839                         msleep(2);
7840                         if (time_after(jiffies, timeout))
7841                                 /* Timeout, let the mailbox flush routine to
7842                                  * forcefully release active mailbox command
7843                                  */
7844                                 break;
7845                 }
7846         }
7847         lpfc_sli_mbox_sys_flush(phba);
7848 }
7849
7850 /**
7851  * lpfc_sli_eratt_read - read sli-3 error attention events
7852  * @phba: Pointer to HBA context.
7853  *
7854  * This function is called to read the SLI3 device error attention registers
7855  * for possible error attention events. The caller must hold the hostlock
7856  * with spin_lock_irq().
7857  *
7858  * This fucntion returns 1 when there is Error Attention in the Host Attention
7859  * Register and returns 0 otherwise.
7860  **/
7861 static int
7862 lpfc_sli_eratt_read(struct lpfc_hba *phba)
7863 {
7864         uint32_t ha_copy;
7865
7866         /* Read chip Host Attention (HA) register */
7867         ha_copy = readl(phba->HAregaddr);
7868         if (ha_copy & HA_ERATT) {
7869                 /* Read host status register to retrieve error event */
7870                 lpfc_sli_read_hs(phba);
7871
7872                 /* Check if there is a deferred error condition is active */
7873                 if ((HS_FFER1 & phba->work_hs) &&
7874                     ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
7875                      HS_FFER6 | HS_FFER7) & phba->work_hs)) {
7876                         phba->hba_flag |= DEFER_ERATT;
7877                         /* Clear all interrupt enable conditions */
7878                         writel(0, phba->HCregaddr);
7879                         readl(phba->HCregaddr);
7880                 }
7881
7882                 /* Set the driver HA work bitmap */
7883                 phba->work_ha |= HA_ERATT;
7884                 /* Indicate polling handles this ERATT */
7885                 phba->hba_flag |= HBA_ERATT_HANDLED;
7886                 return 1;
7887         }
7888         return 0;
7889 }
7890
7891 /**
7892  * lpfc_sli4_eratt_read - read sli-4 error attention events
7893  * @phba: Pointer to HBA context.
7894  *
7895  * This function is called to read the SLI4 device error attention registers
7896  * for possible error attention events. The caller must hold the hostlock
7897  * with spin_lock_irq().
7898  *
7899  * This fucntion returns 1 when there is Error Attention in the Host Attention
7900  * Register and returns 0 otherwise.
7901  **/
7902 static int
7903 lpfc_sli4_eratt_read(struct lpfc_hba *phba)
7904 {
7905         uint32_t uerr_sta_hi, uerr_sta_lo;
7906
7907         /* For now, use the SLI4 device internal unrecoverable error
7908          * registers for error attention. This can be changed later.
7909          */
7910         uerr_sta_lo = readl(phba->sli4_hba.UERRLOregaddr);
7911         uerr_sta_hi = readl(phba->sli4_hba.UERRHIregaddr);
7912         if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
7913             (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
7914                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7915                                 "1423 HBA Unrecoverable error: "
7916                                 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
7917                                 "ue_mask_lo_reg=0x%x, ue_mask_hi_reg=0x%x\n",
7918                                 uerr_sta_lo, uerr_sta_hi,
7919                                 phba->sli4_hba.ue_mask_lo,
7920                                 phba->sli4_hba.ue_mask_hi);
7921                 phba->work_status[0] = uerr_sta_lo;
7922                 phba->work_status[1] = uerr_sta_hi;
7923                 /* Set the driver HA work bitmap */
7924                 phba->work_ha |= HA_ERATT;
7925                 /* Indicate polling handles this ERATT */
7926                 phba->hba_flag |= HBA_ERATT_HANDLED;
7927                 return 1;
7928         }
7929         return 0;
7930 }
7931
7932 /**
7933  * lpfc_sli_check_eratt - check error attention events
7934  * @phba: Pointer to HBA context.
7935  *
7936  * This function is called from timer soft interrupt context to check HBA's
7937  * error attention register bit for error attention events.
7938  *
7939  * This fucntion returns 1 when there is Error Attention in the Host Attention
7940  * Register and returns 0 otherwise.
7941  **/
7942 int
7943 lpfc_sli_check_eratt(struct lpfc_hba *phba)
7944 {
7945         uint32_t ha_copy;
7946
7947         /* If somebody is waiting to handle an eratt, don't process it
7948          * here. The brdkill function will do this.
7949          */
7950         if (phba->link_flag & LS_IGNORE_ERATT)
7951                 return 0;
7952
7953         /* Check if interrupt handler handles this ERATT */
7954         spin_lock_irq(&phba->hbalock);
7955         if (phba->hba_flag & HBA_ERATT_HANDLED) {
7956                 /* Interrupt handler has handled ERATT */
7957                 spin_unlock_irq(&phba->hbalock);
7958                 return 0;
7959         }
7960
7961         /*
7962          * If there is deferred error attention, do not check for error
7963          * attention
7964          */
7965         if (unlikely(phba->hba_flag & DEFER_ERATT)) {
7966                 spin_unlock_irq(&phba->hbalock);
7967                 return 0;
7968         }
7969
7970         /* If PCI channel is offline, don't process it */
7971         if (unlikely(pci_channel_offline(phba->pcidev))) {
7972                 spin_unlock_irq(&phba->hbalock);
7973                 return 0;
7974         }
7975
7976         switch (phba->sli_rev) {
7977         case LPFC_SLI_REV2:
7978         case LPFC_SLI_REV3:
7979                 /* Read chip Host Attention (HA) register */
7980                 ha_copy = lpfc_sli_eratt_read(phba);
7981                 break;
7982         case LPFC_SLI_REV4:
7983                 /* Read devcie Uncoverable Error (UERR) registers */
7984                 ha_copy = lpfc_sli4_eratt_read(phba);
7985                 break;
7986         default:
7987                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7988                                 "0299 Invalid SLI revision (%d)\n",
7989                                 phba->sli_rev);
7990                 ha_copy = 0;
7991                 break;
7992         }
7993         spin_unlock_irq(&phba->hbalock);
7994
7995         return ha_copy;
7996 }
7997
7998 /**
7999  * lpfc_intr_state_check - Check device state for interrupt handling
8000  * @phba: Pointer to HBA context.
8001  *
8002  * This inline routine checks whether a device or its PCI slot is in a state
8003  * that the interrupt should be handled.
8004  *
8005  * This function returns 0 if the device or the PCI slot is in a state that
8006  * interrupt should be handled, otherwise -EIO.
8007  */
8008 static inline int
8009 lpfc_intr_state_check(struct lpfc_hba *phba)
8010 {
8011         /* If the pci channel is offline, ignore all the interrupts */
8012         if (unlikely(pci_channel_offline(phba->pcidev)))
8013                 return -EIO;
8014
8015         /* Update device level interrupt statistics */
8016         phba->sli.slistat.sli_intr++;
8017
8018         /* Ignore all interrupts during initialization. */
8019         if (unlikely(phba->link_state < LPFC_LINK_DOWN))
8020                 return -EIO;
8021
8022         return 0;
8023 }
8024
8025 /**
8026  * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
8027  * @irq: Interrupt number.
8028  * @dev_id: The device context pointer.
8029  *
8030  * This function is directly called from the PCI layer as an interrupt
8031  * service routine when device with SLI-3 interface spec is enabled with
8032  * MSI-X multi-message interrupt mode and there are slow-path events in
8033  * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
8034  * interrupt mode, this function is called as part of the device-level
8035  * interrupt handler. When the PCI slot is in error recovery or the HBA
8036  * is undergoing initialization, the interrupt handler will not process
8037  * the interrupt. The link attention and ELS ring attention events are
8038  * handled by the worker thread. The interrupt handler signals the worker
8039  * thread and returns for these events. This function is called without
8040  * any lock held. It gets the hbalock to access and update SLI data
8041  * structures.
8042  *
8043  * This function returns IRQ_HANDLED when interrupt is handled else it
8044  * returns IRQ_NONE.
8045  **/
8046 irqreturn_t
8047 lpfc_sli_sp_intr_handler(int irq, void *dev_id)
8048 {
8049         struct lpfc_hba  *phba;
8050         uint32_t ha_copy, hc_copy;
8051         uint32_t work_ha_copy;
8052         unsigned long status;
8053         unsigned long iflag;
8054         uint32_t control;
8055
8056         MAILBOX_t *mbox, *pmbox;
8057         struct lpfc_vport *vport;
8058         struct lpfc_nodelist *ndlp;
8059         struct lpfc_dmabuf *mp;
8060         LPFC_MBOXQ_t *pmb;
8061         int rc;
8062
8063         /*
8064          * Get the driver's phba structure from the dev_id and
8065          * assume the HBA is not interrupting.
8066          */
8067         phba = (struct lpfc_hba *)dev_id;
8068
8069         if (unlikely(!phba))
8070                 return IRQ_NONE;
8071
8072         /*
8073          * Stuff needs to be attented to when this function is invoked as an
8074          * individual interrupt handler in MSI-X multi-message interrupt mode
8075          */
8076         if (phba->intr_type == MSIX) {
8077                 /* Check device state for handling interrupt */
8078                 if (lpfc_intr_state_check(phba))
8079                         return IRQ_NONE;
8080                 /* Need to read HA REG for slow-path events */
8081                 spin_lock_irqsave(&phba->hbalock, iflag);
8082                 ha_copy = readl(phba->HAregaddr);
8083                 /* If somebody is waiting to handle an eratt don't process it
8084                  * here. The brdkill function will do this.
8085                  */
8086                 if (phba->link_flag & LS_IGNORE_ERATT)
8087                         ha_copy &= ~HA_ERATT;
8088                 /* Check the need for handling ERATT in interrupt handler */
8089                 if (ha_copy & HA_ERATT) {
8090                         if (phba->hba_flag & HBA_ERATT_HANDLED)
8091                                 /* ERATT polling has handled ERATT */
8092                                 ha_copy &= ~HA_ERATT;
8093                         else
8094                                 /* Indicate interrupt handler handles ERATT */
8095                                 phba->hba_flag |= HBA_ERATT_HANDLED;
8096                 }
8097
8098                 /*
8099                  * If there is deferred error attention, do not check for any
8100                  * interrupt.
8101                  */
8102                 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
8103                         spin_unlock_irqrestore(&phba->hbalock, iflag);
8104                         return IRQ_NONE;
8105                 }
8106
8107                 /* Clear up only attention source related to slow-path */
8108                 hc_copy = readl(phba->HCregaddr);
8109                 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
8110                         HC_LAINT_ENA | HC_ERINT_ENA),
8111                         phba->HCregaddr);
8112                 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
8113                         phba->HAregaddr);
8114                 writel(hc_copy, phba->HCregaddr);
8115                 readl(phba->HAregaddr); /* flush */
8116                 spin_unlock_irqrestore(&phba->hbalock, iflag);
8117         } else
8118                 ha_copy = phba->ha_copy;
8119
8120         work_ha_copy = ha_copy & phba->work_ha_mask;
8121
8122         if (work_ha_copy) {
8123                 if (work_ha_copy & HA_LATT) {
8124                         if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
8125                                 /*
8126                                  * Turn off Link Attention interrupts
8127                                  * until CLEAR_LA done
8128                                  */
8129                                 spin_lock_irqsave(&phba->hbalock, iflag);
8130                                 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
8131                                 control = readl(phba->HCregaddr);
8132                                 control &= ~HC_LAINT_ENA;
8133                                 writel(control, phba->HCregaddr);
8134                                 readl(phba->HCregaddr); /* flush */
8135                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
8136                         }
8137                         else
8138                                 work_ha_copy &= ~HA_LATT;
8139                 }
8140
8141                 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
8142                         /*
8143                          * Turn off Slow Rings interrupts, LPFC_ELS_RING is
8144                          * the only slow ring.
8145                          */
8146                         status = (work_ha_copy &
8147                                 (HA_RXMASK  << (4*LPFC_ELS_RING)));
8148                         status >>= (4*LPFC_ELS_RING);
8149                         if (status & HA_RXMASK) {
8150                                 spin_lock_irqsave(&phba->hbalock, iflag);
8151                                 control = readl(phba->HCregaddr);
8152
8153                                 lpfc_debugfs_slow_ring_trc(phba,
8154                                 "ISR slow ring:   ctl:x%x stat:x%x isrcnt:x%x",
8155                                 control, status,
8156                                 (uint32_t)phba->sli.slistat.sli_intr);
8157
8158                                 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
8159                                         lpfc_debugfs_slow_ring_trc(phba,
8160                                                 "ISR Disable ring:"
8161                                                 "pwork:x%x hawork:x%x wait:x%x",
8162                                                 phba->work_ha, work_ha_copy,
8163                                                 (uint32_t)((unsigned long)
8164                                                 &phba->work_waitq));
8165
8166                                         control &=
8167                                             ~(HC_R0INT_ENA << LPFC_ELS_RING);
8168                                         writel(control, phba->HCregaddr);
8169                                         readl(phba->HCregaddr); /* flush */
8170                                 }
8171                                 else {
8172                                         lpfc_debugfs_slow_ring_trc(phba,
8173                                                 "ISR slow ring:   pwork:"
8174                                                 "x%x hawork:x%x wait:x%x",
8175                                                 phba->work_ha, work_ha_copy,
8176                                                 (uint32_t)((unsigned long)
8177                                                 &phba->work_waitq));
8178                                 }
8179                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
8180                         }
8181                 }
8182                 spin_lock_irqsave(&phba->hbalock, iflag);
8183                 if (work_ha_copy & HA_ERATT) {
8184                         lpfc_sli_read_hs(phba);
8185                         /*
8186                          * Check if there is a deferred error condition
8187                          * is active
8188                          */
8189                         if ((HS_FFER1 & phba->work_hs) &&
8190                                 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
8191                                 HS_FFER6 | HS_FFER7) & phba->work_hs)) {
8192                                 phba->hba_flag |= DEFER_ERATT;
8193                                 /* Clear all interrupt enable conditions */
8194                                 writel(0, phba->HCregaddr);
8195                                 readl(phba->HCregaddr);
8196                         }
8197                 }
8198
8199                 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
8200                         pmb = phba->sli.mbox_active;
8201                         pmbox = &pmb->u.mb;
8202                         mbox = phba->mbox;
8203                         vport = pmb->vport;
8204
8205                         /* First check out the status word */
8206                         lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
8207                         if (pmbox->mbxOwner != OWN_HOST) {
8208                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
8209                                 /*
8210                                  * Stray Mailbox Interrupt, mbxCommand <cmd>
8211                                  * mbxStatus <status>
8212                                  */
8213                                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
8214                                                 LOG_SLI,
8215                                                 "(%d):0304 Stray Mailbox "
8216                                                 "Interrupt mbxCommand x%x "
8217                                                 "mbxStatus x%x\n",
8218                                                 (vport ? vport->vpi : 0),
8219                                                 pmbox->mbxCommand,
8220                                                 pmbox->mbxStatus);
8221                                 /* clear mailbox attention bit */
8222                                 work_ha_copy &= ~HA_MBATT;
8223                         } else {
8224                                 phba->sli.mbox_active = NULL;
8225                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
8226                                 phba->last_completion_time = jiffies;
8227                                 del_timer(&phba->sli.mbox_tmo);
8228                                 if (pmb->mbox_cmpl) {
8229                                         lpfc_sli_pcimem_bcopy(mbox, pmbox,
8230                                                         MAILBOX_CMD_SIZE);
8231                                         if (pmb->out_ext_byte_len &&
8232                                                 pmb->context2)
8233                                                 lpfc_sli_pcimem_bcopy(
8234                                                 phba->mbox_ext,
8235                                                 pmb->context2,
8236                                                 pmb->out_ext_byte_len);
8237                                 }
8238                                 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
8239                                         pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
8240
8241                                         lpfc_debugfs_disc_trc(vport,
8242                                                 LPFC_DISC_TRC_MBOX_VPORT,
8243                                                 "MBOX dflt rpi: : "
8244                                                 "status:x%x rpi:x%x",
8245                                                 (uint32_t)pmbox->mbxStatus,
8246                                                 pmbox->un.varWords[0], 0);
8247
8248                                         if (!pmbox->mbxStatus) {
8249                                                 mp = (struct lpfc_dmabuf *)
8250                                                         (pmb->context1);
8251                                                 ndlp = (struct lpfc_nodelist *)
8252                                                         pmb->context2;
8253
8254                                                 /* Reg_LOGIN of dflt RPI was
8255                                                  * successful. new lets get
8256                                                  * rid of the RPI using the
8257                                                  * same mbox buffer.
8258                                                  */
8259                                                 lpfc_unreg_login(phba,
8260                                                         vport->vpi,
8261                                                         pmbox->un.varWords[0],
8262                                                         pmb);
8263                                                 pmb->mbox_cmpl =
8264                                                         lpfc_mbx_cmpl_dflt_rpi;
8265                                                 pmb->context1 = mp;
8266                                                 pmb->context2 = ndlp;
8267                                                 pmb->vport = vport;
8268                                                 rc = lpfc_sli_issue_mbox(phba,
8269                                                                 pmb,
8270                                                                 MBX_NOWAIT);
8271                                                 if (rc != MBX_BUSY)
8272                                                         lpfc_printf_log(phba,
8273                                                         KERN_ERR,
8274                                                         LOG_MBOX | LOG_SLI,
8275                                                         "0350 rc should have"
8276                                                         "been MBX_BUSY\n");
8277                                                 if (rc != MBX_NOT_FINISHED)
8278                                                         goto send_current_mbox;
8279                                         }
8280                                 }
8281                                 spin_lock_irqsave(
8282                                                 &phba->pport->work_port_lock,
8283                                                 iflag);
8284                                 phba->pport->work_port_events &=
8285                                         ~WORKER_MBOX_TMO;
8286                                 spin_unlock_irqrestore(
8287                                                 &phba->pport->work_port_lock,
8288                                                 iflag);
8289                                 lpfc_mbox_cmpl_put(phba, pmb);
8290                         }
8291                 } else
8292                         spin_unlock_irqrestore(&phba->hbalock, iflag);
8293
8294                 if ((work_ha_copy & HA_MBATT) &&
8295                     (phba->sli.mbox_active == NULL)) {
8296 send_current_mbox:
8297                         /* Process next mailbox command if there is one */
8298                         do {
8299                                 rc = lpfc_sli_issue_mbox(phba, NULL,
8300                                                          MBX_NOWAIT);
8301                         } while (rc == MBX_NOT_FINISHED);
8302                         if (rc != MBX_SUCCESS)
8303                                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
8304                                                 LOG_SLI, "0349 rc should be "
8305                                                 "MBX_SUCCESS\n");
8306                 }
8307
8308                 spin_lock_irqsave(&phba->hbalock, iflag);
8309                 phba->work_ha |= work_ha_copy;
8310                 spin_unlock_irqrestore(&phba->hbalock, iflag);
8311                 lpfc_worker_wake_up(phba);
8312         }
8313         return IRQ_HANDLED;
8314
8315 } /* lpfc_sli_sp_intr_handler */
8316
8317 /**
8318  * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
8319  * @irq: Interrupt number.
8320  * @dev_id: The device context pointer.
8321  *
8322  * This function is directly called from the PCI layer as an interrupt
8323  * service routine when device with SLI-3 interface spec is enabled with
8324  * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
8325  * ring event in the HBA. However, when the device is enabled with either
8326  * MSI or Pin-IRQ interrupt mode, this function is called as part of the
8327  * device-level interrupt handler. When the PCI slot is in error recovery
8328  * or the HBA is undergoing initialization, the interrupt handler will not
8329  * process the interrupt. The SCSI FCP fast-path ring event are handled in
8330  * the intrrupt context. This function is called without any lock held.
8331  * It gets the hbalock to access and update SLI data structures.
8332  *
8333  * This function returns IRQ_HANDLED when interrupt is handled else it
8334  * returns IRQ_NONE.
8335  **/
8336 irqreturn_t
8337 lpfc_sli_fp_intr_handler(int irq, void *dev_id)
8338 {
8339         struct lpfc_hba  *phba;
8340         uint32_t ha_copy;
8341         unsigned long status;
8342         unsigned long iflag;
8343
8344         /* Get the driver's phba structure from the dev_id and
8345          * assume the HBA is not interrupting.
8346          */
8347         phba = (struct lpfc_hba *) dev_id;
8348
8349         if (unlikely(!phba))
8350                 return IRQ_NONE;
8351
8352         /*
8353          * Stuff needs to be attented to when this function is invoked as an
8354          * individual interrupt handler in MSI-X multi-message interrupt mode
8355          */
8356         if (phba->intr_type == MSIX) {
8357                 /* Check device state for handling interrupt */
8358                 if (lpfc_intr_state_check(phba))
8359                         return IRQ_NONE;
8360                 /* Need to read HA REG for FCP ring and other ring events */
8361                 ha_copy = readl(phba->HAregaddr);
8362                 /* Clear up only attention source related to fast-path */
8363                 spin_lock_irqsave(&phba->hbalock, iflag);
8364                 /*
8365                  * If there is deferred error attention, do not check for
8366                  * any interrupt.
8367                  */
8368                 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
8369                         spin_unlock_irqrestore(&phba->hbalock, iflag);
8370                         return IRQ_NONE;
8371                 }
8372                 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
8373                         phba->HAregaddr);
8374                 readl(phba->HAregaddr); /* flush */
8375                 spin_unlock_irqrestore(&phba->hbalock, iflag);
8376         } else
8377                 ha_copy = phba->ha_copy;
8378
8379         /*
8380          * Process all events on FCP ring. Take the optimized path for FCP IO.
8381          */
8382         ha_copy &= ~(phba->work_ha_mask);
8383
8384         status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
8385         status >>= (4*LPFC_FCP_RING);
8386         if (status & HA_RXMASK)
8387                 lpfc_sli_handle_fast_ring_event(phba,
8388                                                 &phba->sli.ring[LPFC_FCP_RING],
8389                                                 status);
8390
8391         if (phba->cfg_multi_ring_support == 2) {
8392                 /*
8393                  * Process all events on extra ring. Take the optimized path
8394                  * for extra ring IO.
8395                  */
8396                 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
8397                 status >>= (4*LPFC_EXTRA_RING);
8398                 if (status & HA_RXMASK) {
8399                         lpfc_sli_handle_fast_ring_event(phba,
8400                                         &phba->sli.ring[LPFC_EXTRA_RING],
8401                                         status);
8402                 }
8403         }
8404         return IRQ_HANDLED;
8405 }  /* lpfc_sli_fp_intr_handler */
8406
8407 /**
8408  * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
8409  * @irq: Interrupt number.
8410  * @dev_id: The device context pointer.
8411  *
8412  * This function is the HBA device-level interrupt handler to device with
8413  * SLI-3 interface spec, called from the PCI layer when either MSI or
8414  * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
8415  * requires driver attention. This function invokes the slow-path interrupt
8416  * attention handling function and fast-path interrupt attention handling
8417  * function in turn to process the relevant HBA attention events. This
8418  * function is called without any lock held. It gets the hbalock to access
8419  * and update SLI data structures.
8420  *
8421  * This function returns IRQ_HANDLED when interrupt is handled, else it
8422  * returns IRQ_NONE.
8423  **/
8424 irqreturn_t
8425 lpfc_sli_intr_handler(int irq, void *dev_id)
8426 {
8427         struct lpfc_hba  *phba;
8428         irqreturn_t sp_irq_rc, fp_irq_rc;
8429         unsigned long status1, status2;
8430         uint32_t hc_copy;
8431
8432         /*
8433          * Get the driver's phba structure from the dev_id and
8434          * assume the HBA is not interrupting.
8435          */
8436         phba = (struct lpfc_hba *) dev_id;
8437
8438         if (unlikely(!phba))
8439                 return IRQ_NONE;
8440
8441         /* Check device state for handling interrupt */
8442         if (lpfc_intr_state_check(phba))
8443                 return IRQ_NONE;
8444
8445         spin_lock(&phba->hbalock);
8446         phba->ha_copy = readl(phba->HAregaddr);
8447         if (unlikely(!phba->ha_copy)) {
8448                 spin_unlock(&phba->hbalock);
8449                 return IRQ_NONE;
8450         } else if (phba->ha_copy & HA_ERATT) {
8451                 if (phba->hba_flag & HBA_ERATT_HANDLED)
8452                         /* ERATT polling has handled ERATT */
8453                         phba->ha_copy &= ~HA_ERATT;
8454                 else
8455                         /* Indicate interrupt handler handles ERATT */
8456                         phba->hba_flag |= HBA_ERATT_HANDLED;
8457         }
8458
8459         /*
8460          * If there is deferred error attention, do not check for any interrupt.
8461          */
8462         if (unlikely(phba->hba_flag & DEFER_ERATT)) {
8463                 spin_unlock_irq(&phba->hbalock);
8464                 return IRQ_NONE;
8465         }
8466
8467         /* Clear attention sources except link and error attentions */
8468         hc_copy = readl(phba->HCregaddr);
8469         writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
8470                 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
8471                 phba->HCregaddr);
8472         writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
8473         writel(hc_copy, phba->HCregaddr);
8474         readl(phba->HAregaddr); /* flush */
8475         spin_unlock(&phba->hbalock);
8476
8477         /*
8478          * Invokes slow-path host attention interrupt handling as appropriate.
8479          */
8480
8481         /* status of events with mailbox and link attention */
8482         status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
8483
8484         /* status of events with ELS ring */
8485         status2 = (phba->ha_copy & (HA_RXMASK  << (4*LPFC_ELS_RING)));
8486         status2 >>= (4*LPFC_ELS_RING);
8487
8488         if (status1 || (status2 & HA_RXMASK))
8489                 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
8490         else
8491                 sp_irq_rc = IRQ_NONE;
8492
8493         /*
8494          * Invoke fast-path host attention interrupt handling as appropriate.
8495          */
8496
8497         /* status of events with FCP ring */
8498         status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
8499         status1 >>= (4*LPFC_FCP_RING);
8500
8501         /* status of events with extra ring */
8502         if (phba->cfg_multi_ring_support == 2) {
8503                 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
8504                 status2 >>= (4*LPFC_EXTRA_RING);
8505         } else
8506                 status2 = 0;
8507
8508         if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
8509                 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
8510         else
8511                 fp_irq_rc = IRQ_NONE;
8512
8513         /* Return device-level interrupt handling status */
8514         return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
8515 }  /* lpfc_sli_intr_handler */
8516
8517 /**
8518  * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
8519  * @phba: pointer to lpfc hba data structure.
8520  *
8521  * This routine is invoked by the worker thread to process all the pending
8522  * SLI4 FCP abort XRI events.
8523  **/
8524 void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
8525 {
8526         struct lpfc_cq_event *cq_event;
8527
8528         /* First, declare the fcp xri abort event has been handled */
8529         spin_lock_irq(&phba->hbalock);
8530         phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
8531         spin_unlock_irq(&phba->hbalock);
8532         /* Now, handle all the fcp xri abort events */
8533         while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
8534                 /* Get the first event from the head of the event queue */
8535                 spin_lock_irq(&phba->hbalock);
8536                 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
8537                                  cq_event, struct lpfc_cq_event, list);
8538                 spin_unlock_irq(&phba->hbalock);
8539                 /* Notify aborted XRI for FCP work queue */
8540                 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
8541                 /* Free the event processed back to the free pool */
8542                 lpfc_sli4_cq_event_release(phba, cq_event);
8543         }
8544 }
8545
8546 /**
8547  * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
8548  * @phba: pointer to lpfc hba data structure.
8549  *
8550  * This routine is invoked by the worker thread to process all the pending
8551  * SLI4 els abort xri events.
8552  **/
8553 void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
8554 {
8555         struct lpfc_cq_event *cq_event;
8556
8557         /* First, declare the els xri abort event has been handled */
8558         spin_lock_irq(&phba->hbalock);
8559         phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
8560         spin_unlock_irq(&phba->hbalock);
8561         /* Now, handle all the els xri abort events */
8562         while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
8563                 /* Get the first event from the head of the event queue */
8564                 spin_lock_irq(&phba->hbalock);
8565                 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
8566                                  cq_event, struct lpfc_cq_event, list);
8567                 spin_unlock_irq(&phba->hbalock);
8568                 /* Notify aborted XRI for ELS work queue */
8569                 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
8570                 /* Free the event processed back to the free pool */
8571                 lpfc_sli4_cq_event_release(phba, cq_event);
8572         }
8573 }
8574
8575 /**
8576  * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
8577  * @phba: pointer to lpfc hba data structure
8578  * @pIocbIn: pointer to the rspiocbq
8579  * @pIocbOut: pointer to the cmdiocbq
8580  * @wcqe: pointer to the complete wcqe
8581  *
8582  * This routine transfers the fields of a command iocbq to a response iocbq
8583  * by copying all the IOCB fields from command iocbq and transferring the
8584  * completion status information from the complete wcqe.
8585  **/
8586 static void
8587 lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
8588                               struct lpfc_iocbq *pIocbIn,
8589                               struct lpfc_iocbq *pIocbOut,
8590                               struct lpfc_wcqe_complete *wcqe)
8591 {
8592         unsigned long iflags;
8593         size_t offset = offsetof(struct lpfc_iocbq, iocb);
8594
8595         memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
8596                sizeof(struct lpfc_iocbq) - offset);
8597         /* Map WCQE parameters into irspiocb parameters */
8598         pIocbIn->iocb.ulpStatus = bf_get(lpfc_wcqe_c_status, wcqe);
8599         if (pIocbOut->iocb_flag & LPFC_IO_FCP)
8600                 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
8601                         pIocbIn->iocb.un.fcpi.fcpi_parm =
8602                                         pIocbOut->iocb.un.fcpi.fcpi_parm -
8603                                         wcqe->total_data_placed;
8604                 else
8605                         pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
8606         else {
8607                 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
8608                 pIocbIn->iocb.un.genreq64.bdl.bdeSize = wcqe->total_data_placed;
8609         }
8610
8611         /* Pick up HBA exchange busy condition */
8612         if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
8613                 spin_lock_irqsave(&phba->hbalock, iflags);
8614                 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
8615                 spin_unlock_irqrestore(&phba->hbalock, iflags);
8616         }
8617 }
8618
8619 /**
8620  * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
8621  * @phba: Pointer to HBA context object.
8622  * @wcqe: Pointer to work-queue completion queue entry.
8623  *
8624  * This routine handles an ELS work-queue completion event and construct
8625  * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
8626  * discovery engine to handle.
8627  *
8628  * Return: Pointer to the receive IOCBQ, NULL otherwise.
8629  **/
8630 static struct lpfc_iocbq *
8631 lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
8632                                struct lpfc_iocbq *irspiocbq)
8633 {
8634         struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
8635         struct lpfc_iocbq *cmdiocbq;
8636         struct lpfc_wcqe_complete *wcqe;
8637         unsigned long iflags;
8638
8639         wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
8640         spin_lock_irqsave(&phba->hbalock, iflags);
8641         pring->stats.iocb_event++;
8642         /* Look up the ELS command IOCB and create pseudo response IOCB */
8643         cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
8644                                 bf_get(lpfc_wcqe_c_request_tag, wcqe));
8645         spin_unlock_irqrestore(&phba->hbalock, iflags);
8646
8647         if (unlikely(!cmdiocbq)) {
8648                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8649                                 "0386 ELS complete with no corresponding "
8650                                 "cmdiocb: iotag (%d)\n",
8651                                 bf_get(lpfc_wcqe_c_request_tag, wcqe));
8652                 lpfc_sli_release_iocbq(phba, irspiocbq);
8653                 return NULL;
8654         }
8655
8656         /* Fake the irspiocbq and copy necessary response information */
8657         lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
8658
8659         return irspiocbq;
8660 }
8661
8662 /**
8663  * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
8664  * @phba: Pointer to HBA context object.
8665  * @cqe: Pointer to mailbox completion queue entry.
8666  *
8667  * This routine process a mailbox completion queue entry with asynchrous
8668  * event.
8669  *
8670  * Return: true if work posted to worker thread, otherwise false.
8671  **/
8672 static bool
8673 lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
8674 {
8675         struct lpfc_cq_event *cq_event;
8676         unsigned long iflags;
8677
8678         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8679                         "0392 Async Event: word0:x%x, word1:x%x, "
8680                         "word2:x%x, word3:x%x\n", mcqe->word0,
8681                         mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
8682
8683         /* Allocate a new internal CQ_EVENT entry */
8684         cq_event = lpfc_sli4_cq_event_alloc(phba);
8685         if (!cq_event) {
8686                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8687                                 "0394 Failed to allocate CQ_EVENT entry\n");
8688                 return false;
8689         }
8690
8691         /* Move the CQE into an asynchronous event entry */
8692         memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
8693         spin_lock_irqsave(&phba->hbalock, iflags);
8694         list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
8695         /* Set the async event flag */
8696         phba->hba_flag |= ASYNC_EVENT;
8697         spin_unlock_irqrestore(&phba->hbalock, iflags);
8698
8699         return true;
8700 }
8701
8702 /**
8703  * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
8704  * @phba: Pointer to HBA context object.
8705  * @cqe: Pointer to mailbox completion queue entry.
8706  *
8707  * This routine process a mailbox completion queue entry with mailbox
8708  * completion event.
8709  *
8710  * Return: true if work posted to worker thread, otherwise false.
8711  **/
8712 static bool
8713 lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
8714 {
8715         uint32_t mcqe_status;
8716         MAILBOX_t *mbox, *pmbox;
8717         struct lpfc_mqe *mqe;
8718         struct lpfc_vport *vport;
8719         struct lpfc_nodelist *ndlp;
8720         struct lpfc_dmabuf *mp;
8721         unsigned long iflags;
8722         LPFC_MBOXQ_t *pmb;
8723         bool workposted = false;
8724         int rc;
8725
8726         /* If not a mailbox complete MCQE, out by checking mailbox consume */
8727         if (!bf_get(lpfc_trailer_completed, mcqe))
8728                 goto out_no_mqe_complete;
8729
8730         /* Get the reference to the active mbox command */
8731         spin_lock_irqsave(&phba->hbalock, iflags);
8732         pmb = phba->sli.mbox_active;
8733         if (unlikely(!pmb)) {
8734                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
8735                                 "1832 No pending MBOX command to handle\n");
8736                 spin_unlock_irqrestore(&phba->hbalock, iflags);
8737                 goto out_no_mqe_complete;
8738         }
8739         spin_unlock_irqrestore(&phba->hbalock, iflags);
8740         mqe = &pmb->u.mqe;
8741         pmbox = (MAILBOX_t *)&pmb->u.mqe;
8742         mbox = phba->mbox;
8743         vport = pmb->vport;
8744
8745         /* Reset heartbeat timer */
8746         phba->last_completion_time = jiffies;
8747         del_timer(&phba->sli.mbox_tmo);
8748
8749         /* Move mbox data to caller's mailbox region, do endian swapping */
8750         if (pmb->mbox_cmpl && mbox)
8751                 lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
8752         /* Set the mailbox status with SLI4 range 0x4000 */
8753         mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
8754         if (mcqe_status != MB_CQE_STATUS_SUCCESS)
8755                 bf_set(lpfc_mqe_status, mqe,
8756                        (LPFC_MBX_ERROR_RANGE | mcqe_status));
8757
8758         if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
8759                 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
8760                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
8761                                       "MBOX dflt rpi: status:x%x rpi:x%x",
8762                                       mcqe_status,
8763                                       pmbox->un.varWords[0], 0);
8764                 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
8765                         mp = (struct lpfc_dmabuf *)(pmb->context1);
8766                         ndlp = (struct lpfc_nodelist *)pmb->context2;
8767                         /* Reg_LOGIN of dflt RPI was successful. Now lets get
8768                          * RID of the PPI using the same mbox buffer.
8769                          */
8770                         lpfc_unreg_login(phba, vport->vpi,
8771                                          pmbox->un.varWords[0], pmb);
8772                         pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
8773                         pmb->context1 = mp;
8774                         pmb->context2 = ndlp;
8775                         pmb->vport = vport;
8776                         rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
8777                         if (rc != MBX_BUSY)
8778                                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
8779                                                 LOG_SLI, "0385 rc should "
8780                                                 "have been MBX_BUSY\n");
8781                         if (rc != MBX_NOT_FINISHED)
8782                                 goto send_current_mbox;
8783                 }
8784         }
8785         spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
8786         phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
8787         spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
8788
8789         /* There is mailbox completion work to do */
8790         spin_lock_irqsave(&phba->hbalock, iflags);
8791         __lpfc_mbox_cmpl_put(phba, pmb);
8792         phba->work_ha |= HA_MBATT;
8793         spin_unlock_irqrestore(&phba->hbalock, iflags);
8794         workposted = true;
8795
8796 send_current_mbox:
8797         spin_lock_irqsave(&phba->hbalock, iflags);
8798         /* Release the mailbox command posting token */
8799         phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8800         /* Setting active mailbox pointer need to be in sync to flag clear */
8801         phba->sli.mbox_active = NULL;
8802         spin_unlock_irqrestore(&phba->hbalock, iflags);
8803         /* Wake up worker thread to post the next pending mailbox command */
8804         lpfc_worker_wake_up(phba);
8805 out_no_mqe_complete:
8806         if (bf_get(lpfc_trailer_consumed, mcqe))
8807                 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
8808         return workposted;
8809 }
8810
8811 /**
8812  * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
8813  * @phba: Pointer to HBA context object.
8814  * @cqe: Pointer to mailbox completion queue entry.
8815  *
8816  * This routine process a mailbox completion queue entry, it invokes the
8817  * proper mailbox complete handling or asynchrous event handling routine
8818  * according to the MCQE's async bit.
8819  *
8820  * Return: true if work posted to worker thread, otherwise false.
8821  **/
8822 static bool
8823 lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
8824 {
8825         struct lpfc_mcqe mcqe;
8826         bool workposted;
8827
8828         /* Copy the mailbox MCQE and convert endian order as needed */
8829         lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
8830
8831         /* Invoke the proper event handling routine */
8832         if (!bf_get(lpfc_trailer_async, &mcqe))
8833                 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
8834         else
8835                 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
8836         return workposted;
8837 }
8838
8839 /**
8840  * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
8841  * @phba: Pointer to HBA context object.
8842  * @wcqe: Pointer to work-queue completion queue entry.
8843  *
8844  * This routine handles an ELS work-queue completion event.
8845  *
8846  * Return: true if work posted to worker thread, otherwise false.
8847  **/
8848 static bool
8849 lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba,
8850                              struct lpfc_wcqe_complete *wcqe)
8851 {
8852         struct lpfc_iocbq *irspiocbq;
8853         unsigned long iflags;
8854         struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
8855
8856         /* Get an irspiocbq for later ELS response processing use */
8857         irspiocbq = lpfc_sli_get_iocbq(phba);
8858         if (!irspiocbq) {
8859                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8860                         "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
8861                         "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
8862                         pring->txq_cnt, phba->iocb_cnt,
8863                         phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt,
8864                         phba->sli.ring[LPFC_ELS_RING].txcmplq_cnt);
8865                 return false;
8866         }
8867
8868         /* Save off the slow-path queue event for work thread to process */
8869         memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
8870         spin_lock_irqsave(&phba->hbalock, iflags);
8871         list_add_tail(&irspiocbq->cq_event.list,
8872                       &phba->sli4_hba.sp_queue_event);
8873         phba->hba_flag |= HBA_SP_QUEUE_EVT;
8874         spin_unlock_irqrestore(&phba->hbalock, iflags);
8875
8876         return true;
8877 }
8878
8879 /**
8880  * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
8881  * @phba: Pointer to HBA context object.
8882  * @wcqe: Pointer to work-queue completion queue entry.
8883  *
8884  * This routine handles slow-path WQ entry comsumed event by invoking the
8885  * proper WQ release routine to the slow-path WQ.
8886  **/
8887 static void
8888 lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
8889                              struct lpfc_wcqe_release *wcqe)
8890 {
8891         /* Check for the slow-path ELS work queue */
8892         if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
8893                 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
8894                                      bf_get(lpfc_wcqe_r_wqe_index, wcqe));
8895         else
8896                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8897                                 "2579 Slow-path wqe consume event carries "
8898                                 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
8899                                 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
8900                                 phba->sli4_hba.els_wq->queue_id);
8901 }
8902
8903 /**
8904  * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
8905  * @phba: Pointer to HBA context object.
8906  * @cq: Pointer to a WQ completion queue.
8907  * @wcqe: Pointer to work-queue completion queue entry.
8908  *
8909  * This routine handles an XRI abort event.
8910  *
8911  * Return: true if work posted to worker thread, otherwise false.
8912  **/
8913 static bool
8914 lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
8915                                    struct lpfc_queue *cq,
8916                                    struct sli4_wcqe_xri_aborted *wcqe)
8917 {
8918         bool workposted = false;
8919         struct lpfc_cq_event *cq_event;
8920         unsigned long iflags;
8921
8922         /* Allocate a new internal CQ_EVENT entry */
8923         cq_event = lpfc_sli4_cq_event_alloc(phba);
8924         if (!cq_event) {
8925                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8926                                 "0602 Failed to allocate CQ_EVENT entry\n");
8927                 return false;
8928         }
8929
8930         /* Move the CQE into the proper xri abort event list */
8931         memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
8932         switch (cq->subtype) {
8933         case LPFC_FCP:
8934                 spin_lock_irqsave(&phba->hbalock, iflags);
8935                 list_add_tail(&cq_event->list,
8936                               &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
8937                 /* Set the fcp xri abort event flag */
8938                 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
8939                 spin_unlock_irqrestore(&phba->hbalock, iflags);
8940                 workposted = true;
8941                 break;
8942         case LPFC_ELS:
8943                 spin_lock_irqsave(&phba->hbalock, iflags);
8944                 list_add_tail(&cq_event->list,
8945                               &phba->sli4_hba.sp_els_xri_aborted_work_queue);
8946                 /* Set the els xri abort event flag */
8947                 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
8948                 spin_unlock_irqrestore(&phba->hbalock, iflags);
8949                 workposted = true;
8950                 break;
8951         default:
8952                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8953                                 "0603 Invalid work queue CQE subtype (x%x)\n",
8954                                 cq->subtype);
8955                 workposted = false;
8956                 break;
8957         }
8958         return workposted;
8959 }
8960
8961 /**
8962  * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
8963  * @phba: Pointer to HBA context object.
8964  * @rcqe: Pointer to receive-queue completion queue entry.
8965  *
8966  * This routine process a receive-queue completion queue entry.
8967  *
8968  * Return: true if work posted to worker thread, otherwise false.
8969  **/
8970 static bool
8971 lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
8972 {
8973         bool workposted = false;
8974         struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
8975         struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
8976         struct hbq_dmabuf *dma_buf;
8977         uint32_t status;
8978         unsigned long iflags;
8979
8980         if (bf_get(lpfc_rcqe_rq_id, rcqe) != hrq->queue_id)
8981                 goto out;
8982
8983         status = bf_get(lpfc_rcqe_status, rcqe);
8984         switch (status) {
8985         case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
8986                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8987                                 "2537 Receive Frame Truncated!!\n");
8988         case FC_STATUS_RQ_SUCCESS:
8989                 lpfc_sli4_rq_release(hrq, drq);
8990                 spin_lock_irqsave(&phba->hbalock, iflags);
8991                 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
8992                 if (!dma_buf) {
8993                         spin_unlock_irqrestore(&phba->hbalock, iflags);
8994                         goto out;
8995                 }
8996                 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
8997                 /* save off the frame for the word thread to process */
8998                 list_add_tail(&dma_buf->cq_event.list,
8999                               &phba->sli4_hba.sp_queue_event);
9000                 /* Frame received */
9001                 phba->hba_flag |= HBA_SP_QUEUE_EVT;
9002                 spin_unlock_irqrestore(&phba->hbalock, iflags);
9003                 workposted = true;
9004                 break;
9005         case FC_STATUS_INSUFF_BUF_NEED_BUF:
9006         case FC_STATUS_INSUFF_BUF_FRM_DISC:
9007                 /* Post more buffers if possible */
9008                 spin_lock_irqsave(&phba->hbalock, iflags);
9009                 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
9010                 spin_unlock_irqrestore(&phba->hbalock, iflags);
9011                 workposted = true;
9012                 break;
9013         }
9014 out:
9015         return workposted;
9016 }
9017
9018 /**
9019  * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
9020  * @phba: Pointer to HBA context object.
9021  * @cq: Pointer to the completion queue.
9022  * @wcqe: Pointer to a completion queue entry.
9023  *
9024  * This routine process a slow-path work-queue or recieve queue completion queue
9025  * entry.
9026  *
9027  * Return: true if work posted to worker thread, otherwise false.
9028  **/
9029 static bool
9030 lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
9031                          struct lpfc_cqe *cqe)
9032 {
9033         struct lpfc_cqe cqevt;
9034         bool workposted = false;
9035
9036         /* Copy the work queue CQE and convert endian order if needed */
9037         lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
9038
9039         /* Check and process for different type of WCQE and dispatch */
9040         switch (bf_get(lpfc_cqe_code, &cqevt)) {
9041         case CQE_CODE_COMPL_WQE:
9042                 /* Process the WQ/RQ complete event */
9043                 workposted = lpfc_sli4_sp_handle_els_wcqe(phba,
9044                                 (struct lpfc_wcqe_complete *)&cqevt);
9045                 break;
9046         case CQE_CODE_RELEASE_WQE:
9047                 /* Process the WQ release event */
9048                 lpfc_sli4_sp_handle_rel_wcqe(phba,
9049                                 (struct lpfc_wcqe_release *)&cqevt);
9050                 break;
9051         case CQE_CODE_XRI_ABORTED:
9052                 /* Process the WQ XRI abort event */
9053                 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
9054                                 (struct sli4_wcqe_xri_aborted *)&cqevt);
9055                 break;
9056         case CQE_CODE_RECEIVE:
9057                 /* Process the RQ event */
9058                 workposted = lpfc_sli4_sp_handle_rcqe(phba,
9059                                 (struct lpfc_rcqe *)&cqevt);
9060                 break;
9061         default:
9062                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9063                                 "0388 Not a valid WCQE code: x%x\n",
9064                                 bf_get(lpfc_cqe_code, &cqevt));
9065                 break;
9066         }
9067         return workposted;
9068 }
9069
9070 /**
9071  * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
9072  * @phba: Pointer to HBA context object.
9073  * @eqe: Pointer to fast-path event queue entry.
9074  *
9075  * This routine process a event queue entry from the slow-path event queue.
9076  * It will check the MajorCode and MinorCode to determine this is for a
9077  * completion event on a completion queue, if not, an error shall be logged
9078  * and just return. Otherwise, it will get to the corresponding completion
9079  * queue and process all the entries on that completion queue, rearm the
9080  * completion queue, and then return.
9081  *
9082  **/
9083 static void
9084 lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
9085 {
9086         struct lpfc_queue *cq = NULL, *childq, *speq;
9087         struct lpfc_cqe *cqe;
9088         bool workposted = false;
9089         int ecount = 0;
9090         uint16_t cqid;
9091
9092         if (bf_get_le32(lpfc_eqe_major_code, eqe) != 0) {
9093                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9094                                 "0359 Not a valid slow-path completion "
9095                                 "event: majorcode=x%x, minorcode=x%x\n",
9096                                 bf_get_le32(lpfc_eqe_major_code, eqe),
9097                                 bf_get_le32(lpfc_eqe_minor_code, eqe));
9098                 return;
9099         }
9100
9101         /* Get the reference to the corresponding CQ */
9102         cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
9103
9104         /* Search for completion queue pointer matching this cqid */
9105         speq = phba->sli4_hba.sp_eq;
9106         list_for_each_entry(childq, &speq->child_list, list) {
9107                 if (childq->queue_id == cqid) {
9108                         cq = childq;
9109                         break;
9110                 }
9111         }
9112         if (unlikely(!cq)) {
9113                 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
9114                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9115                                         "0365 Slow-path CQ identifier "
9116                                         "(%d) does not exist\n", cqid);
9117                 return;
9118         }
9119
9120         /* Process all the entries to the CQ */
9121         switch (cq->type) {
9122         case LPFC_MCQ:
9123                 while ((cqe = lpfc_sli4_cq_get(cq))) {
9124                         workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
9125                         if (!(++ecount % LPFC_GET_QE_REL_INT))
9126                                 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
9127                 }
9128                 break;
9129         case LPFC_WCQ:
9130                 while ((cqe = lpfc_sli4_cq_get(cq))) {
9131                         workposted |= lpfc_sli4_sp_handle_cqe(phba, cq, cqe);
9132                         if (!(++ecount % LPFC_GET_QE_REL_INT))
9133                                 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
9134                 }
9135                 break;
9136         default:
9137                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9138                                 "0370 Invalid completion queue type (%d)\n",
9139                                 cq->type);
9140                 return;
9141         }
9142
9143         /* Catch the no cq entry condition, log an error */
9144         if (unlikely(ecount == 0))
9145                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9146                                 "0371 No entry from the CQ: identifier "
9147                                 "(x%x), type (%d)\n", cq->queue_id, cq->type);
9148
9149         /* In any case, flash and re-arm the RCQ */
9150         lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
9151
9152         /* wake up worker thread if there are works to be done */
9153         if (workposted)
9154                 lpfc_worker_wake_up(phba);
9155 }
9156
9157 /**
9158  * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
9159  * @eqe: Pointer to fast-path completion queue entry.
9160  *
9161  * This routine process a fast-path work queue completion entry from fast-path
9162  * event queue for FCP command response completion.
9163  **/
9164 static void
9165 lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba,
9166                              struct lpfc_wcqe_complete *wcqe)
9167 {
9168         struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
9169         struct lpfc_iocbq *cmdiocbq;
9170         struct lpfc_iocbq irspiocbq;
9171         unsigned long iflags;
9172
9173         spin_lock_irqsave(&phba->hbalock, iflags);
9174         pring->stats.iocb_event++;
9175         spin_unlock_irqrestore(&phba->hbalock, iflags);
9176
9177         /* Check for response status */
9178         if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
9179                 /* If resource errors reported from HBA, reduce queue
9180                  * depth of the SCSI device.
9181                  */
9182                 if ((bf_get(lpfc_wcqe_c_status, wcqe) ==
9183                      IOSTAT_LOCAL_REJECT) &&
9184                     (wcqe->parameter == IOERR_NO_RESOURCES)) {
9185                         phba->lpfc_rampdown_queue_depth(phba);
9186                 }
9187                 /* Log the error status */
9188                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9189                                 "0373 FCP complete error: status=x%x, "
9190                                 "hw_status=x%x, total_data_specified=%d, "
9191                                 "parameter=x%x, word3=x%x\n",
9192                                 bf_get(lpfc_wcqe_c_status, wcqe),
9193                                 bf_get(lpfc_wcqe_c_hw_status, wcqe),
9194                                 wcqe->total_data_placed, wcqe->parameter,
9195                                 wcqe->word3);
9196         }
9197
9198         /* Look up the FCP command IOCB and create pseudo response IOCB */
9199         spin_lock_irqsave(&phba->hbalock, iflags);
9200         cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
9201                                 bf_get(lpfc_wcqe_c_request_tag, wcqe));
9202         spin_unlock_irqrestore(&phba->hbalock, iflags);
9203         if (unlikely(!cmdiocbq)) {
9204                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9205                                 "0374 FCP complete with no corresponding "
9206                                 "cmdiocb: iotag (%d)\n",
9207                                 bf_get(lpfc_wcqe_c_request_tag, wcqe));
9208                 return;
9209         }
9210         if (unlikely(!cmdiocbq->iocb_cmpl)) {
9211                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9212                                 "0375 FCP cmdiocb not callback function "
9213                                 "iotag: (%d)\n",
9214                                 bf_get(lpfc_wcqe_c_request_tag, wcqe));
9215                 return;
9216         }
9217
9218         /* Fake the irspiocb and copy necessary response information */
9219         lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
9220
9221         if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
9222                 spin_lock_irqsave(&phba->hbalock, iflags);
9223                 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
9224                 spin_unlock_irqrestore(&phba->hbalock, iflags);
9225         }
9226
9227         /* Pass the cmd_iocb and the rsp state to the upper layer */
9228         (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
9229 }
9230
9231 /**
9232  * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
9233  * @phba: Pointer to HBA context object.
9234  * @cq: Pointer to completion queue.
9235  * @wcqe: Pointer to work-queue completion queue entry.
9236  *
9237  * This routine handles an fast-path WQ entry comsumed event by invoking the
9238  * proper WQ release routine to the slow-path WQ.
9239  **/
9240 static void
9241 lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
9242                              struct lpfc_wcqe_release *wcqe)
9243 {
9244         struct lpfc_queue *childwq;
9245         bool wqid_matched = false;
9246         uint16_t fcp_wqid;
9247
9248         /* Check for fast-path FCP work queue release */
9249         fcp_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
9250         list_for_each_entry(childwq, &cq->child_list, list) {
9251                 if (childwq->queue_id == fcp_wqid) {
9252                         lpfc_sli4_wq_release(childwq,
9253                                         bf_get(lpfc_wcqe_r_wqe_index, wcqe));
9254                         wqid_matched = true;
9255                         break;
9256                 }
9257         }
9258         /* Report warning log message if no match found */
9259         if (wqid_matched != true)
9260                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9261                                 "2580 Fast-path wqe consume event carries "
9262                                 "miss-matched qid: wcqe-qid=x%x\n", fcp_wqid);
9263 }
9264
9265 /**
9266  * lpfc_sli4_fp_handle_wcqe - Process fast-path work queue completion entry
9267  * @cq: Pointer to the completion queue.
9268  * @eqe: Pointer to fast-path completion queue entry.
9269  *
9270  * This routine process a fast-path work queue completion entry from fast-path
9271  * event queue for FCP command response completion.
9272  **/
9273 static int
9274 lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
9275                          struct lpfc_cqe *cqe)
9276 {
9277         struct lpfc_wcqe_release wcqe;
9278         bool workposted = false;
9279         unsigned long iflag;
9280
9281         /* Copy the work queue CQE and convert endian order if needed */
9282         lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
9283
9284         /* Check and process for different type of WCQE and dispatch */
9285         switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
9286         case CQE_CODE_COMPL_WQE:
9287                 /* Process the WQ complete event */
9288                 spin_lock_irqsave(&phba->hbalock, iflag);
9289                 phba->last_completion_time = jiffies;
9290                 spin_unlock_irqrestore(&phba->hbalock, iflag);
9291                 lpfc_sli4_fp_handle_fcp_wcqe(phba,
9292                                 (struct lpfc_wcqe_complete *)&wcqe);
9293                 break;
9294         case CQE_CODE_RELEASE_WQE:
9295                 /* Process the WQ release event */
9296                 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
9297                                 (struct lpfc_wcqe_release *)&wcqe);
9298                 break;
9299         case CQE_CODE_XRI_ABORTED:
9300                 /* Process the WQ XRI abort event */
9301                 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
9302                                 (struct sli4_wcqe_xri_aborted *)&wcqe);
9303                 break;
9304         default:
9305                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9306                                 "0144 Not a valid WCQE code: x%x\n",
9307                                 bf_get(lpfc_wcqe_c_code, &wcqe));
9308                 break;
9309         }
9310         return workposted;
9311 }
9312
9313 /**
9314  * lpfc_sli4_fp_handle_eqe - Process a fast-path event queue entry
9315  * @phba: Pointer to HBA context object.
9316  * @eqe: Pointer to fast-path event queue entry.
9317  *
9318  * This routine process a event queue entry from the fast-path event queue.
9319  * It will check the MajorCode and MinorCode to determine this is for a
9320  * completion event on a completion queue, if not, an error shall be logged
9321  * and just return. Otherwise, it will get to the corresponding completion
9322  * queue and process all the entries on the completion queue, rearm the
9323  * completion queue, and then return.
9324  **/
9325 static void
9326 lpfc_sli4_fp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
9327                         uint32_t fcp_cqidx)
9328 {
9329         struct lpfc_queue *cq;
9330         struct lpfc_cqe *cqe;
9331         bool workposted = false;
9332         uint16_t cqid;
9333         int ecount = 0;
9334
9335         if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
9336                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9337                                 "0366 Not a valid fast-path completion "
9338                                 "event: majorcode=x%x, minorcode=x%x\n",
9339                                 bf_get_le32(lpfc_eqe_major_code, eqe),
9340                                 bf_get_le32(lpfc_eqe_minor_code, eqe));
9341                 return;
9342         }
9343
9344         cq = phba->sli4_hba.fcp_cq[fcp_cqidx];
9345         if (unlikely(!cq)) {
9346                 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
9347                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9348                                         "0367 Fast-path completion queue "
9349                                         "does not exist\n");
9350                 return;
9351         }
9352
9353         /* Get the reference to the corresponding CQ */
9354         cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
9355         if (unlikely(cqid != cq->queue_id)) {
9356                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9357                                 "0368 Miss-matched fast-path completion "
9358                                 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
9359                                 cqid, cq->queue_id);
9360                 return;
9361         }
9362
9363         /* Process all the entries to the CQ */
9364         while ((cqe = lpfc_sli4_cq_get(cq))) {
9365                 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
9366                 if (!(++ecount % LPFC_GET_QE_REL_INT))
9367                         lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
9368         }
9369
9370         /* Catch the no cq entry condition */
9371         if (unlikely(ecount == 0))
9372                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9373                                 "0369 No entry from fast-path completion "
9374                                 "queue fcpcqid=%d\n", cq->queue_id);
9375
9376         /* In any case, flash and re-arm the CQ */
9377         lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
9378
9379         /* wake up worker thread if there are works to be done */
9380         if (workposted)
9381                 lpfc_worker_wake_up(phba);
9382 }
9383
9384 static void
9385 lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
9386 {
9387         struct lpfc_eqe *eqe;
9388
9389         /* walk all the EQ entries and drop on the floor */
9390         while ((eqe = lpfc_sli4_eq_get(eq)))
9391                 ;
9392
9393         /* Clear and re-arm the EQ */
9394         lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
9395 }
9396
9397 /**
9398  * lpfc_sli4_sp_intr_handler - Slow-path interrupt handler to SLI-4 device
9399  * @irq: Interrupt number.
9400  * @dev_id: The device context pointer.
9401  *
9402  * This function is directly called from the PCI layer as an interrupt
9403  * service routine when device with SLI-4 interface spec is enabled with
9404  * MSI-X multi-message interrupt mode and there are slow-path events in
9405  * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
9406  * interrupt mode, this function is called as part of the device-level
9407  * interrupt handler. When the PCI slot is in error recovery or the HBA is
9408  * undergoing initialization, the interrupt handler will not process the
9409  * interrupt. The link attention and ELS ring attention events are handled
9410  * by the worker thread. The interrupt handler signals the worker thread
9411  * and returns for these events. This function is called without any lock
9412  * held. It gets the hbalock to access and update SLI data structures.
9413  *
9414  * This function returns IRQ_HANDLED when interrupt is handled else it
9415  * returns IRQ_NONE.
9416  **/
9417 irqreturn_t
9418 lpfc_sli4_sp_intr_handler(int irq, void *dev_id)
9419 {
9420         struct lpfc_hba *phba;
9421         struct lpfc_queue *speq;
9422         struct lpfc_eqe *eqe;
9423         unsigned long iflag;
9424         int ecount = 0;
9425
9426         /*
9427          * Get the driver's phba structure from the dev_id
9428          */
9429         phba = (struct lpfc_hba *)dev_id;
9430
9431         if (unlikely(!phba))
9432                 return IRQ_NONE;
9433
9434         /* Get to the EQ struct associated with this vector */
9435         speq = phba->sli4_hba.sp_eq;
9436
9437         /* Check device state for handling interrupt */
9438         if (unlikely(lpfc_intr_state_check(phba))) {
9439                 /* Check again for link_state with lock held */
9440                 spin_lock_irqsave(&phba->hbalock, iflag);
9441                 if (phba->link_state < LPFC_LINK_DOWN)
9442                         /* Flush, clear interrupt, and rearm the EQ */
9443                         lpfc_sli4_eq_flush(phba, speq);
9444                 spin_unlock_irqrestore(&phba->hbalock, iflag);
9445                 return IRQ_NONE;
9446         }
9447
9448         /*
9449          * Process all the event on FCP slow-path EQ
9450          */
9451         while ((eqe = lpfc_sli4_eq_get(speq))) {
9452                 lpfc_sli4_sp_handle_eqe(phba, eqe);
9453                 if (!(++ecount % LPFC_GET_QE_REL_INT))
9454                         lpfc_sli4_eq_release(speq, LPFC_QUEUE_NOARM);
9455         }
9456
9457         /* Always clear and re-arm the slow-path EQ */
9458         lpfc_sli4_eq_release(speq, LPFC_QUEUE_REARM);
9459
9460         /* Catch the no cq entry condition */
9461         if (unlikely(ecount == 0)) {
9462                 if (phba->intr_type == MSIX)
9463                         /* MSI-X treated interrupt served as no EQ share INT */
9464                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9465                                         "0357 MSI-X interrupt with no EQE\n");
9466                 else
9467                         /* Non MSI-X treated on interrupt as EQ share INT */
9468                         return IRQ_NONE;
9469         }
9470
9471         return IRQ_HANDLED;
9472 } /* lpfc_sli4_sp_intr_handler */
9473
9474 /**
9475  * lpfc_sli4_fp_intr_handler - Fast-path interrupt handler to SLI-4 device
9476  * @irq: Interrupt number.
9477  * @dev_id: The device context pointer.
9478  *
9479  * This function is directly called from the PCI layer as an interrupt
9480  * service routine when device with SLI-4 interface spec is enabled with
9481  * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
9482  * ring event in the HBA. However, when the device is enabled with either
9483  * MSI or Pin-IRQ interrupt mode, this function is called as part of the
9484  * device-level interrupt handler. When the PCI slot is in error recovery
9485  * or the HBA is undergoing initialization, the interrupt handler will not
9486  * process the interrupt. The SCSI FCP fast-path ring event are handled in
9487  * the intrrupt context. This function is called without any lock held.
9488  * It gets the hbalock to access and update SLI data structures. Note that,
9489  * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
9490  * equal to that of FCP CQ index.
9491  *
9492  * This function returns IRQ_HANDLED when interrupt is handled else it
9493  * returns IRQ_NONE.
9494  **/
9495 irqreturn_t
9496 lpfc_sli4_fp_intr_handler(int irq, void *dev_id)
9497 {
9498         struct lpfc_hba *phba;
9499         struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
9500         struct lpfc_queue *fpeq;
9501         struct lpfc_eqe *eqe;
9502         unsigned long iflag;
9503         int ecount = 0;
9504         uint32_t fcp_eqidx;
9505
9506         /* Get the driver's phba structure from the dev_id */
9507         fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
9508         phba = fcp_eq_hdl->phba;
9509         fcp_eqidx = fcp_eq_hdl->idx;
9510
9511         if (unlikely(!phba))
9512                 return IRQ_NONE;
9513
9514         /* Get to the EQ struct associated with this vector */
9515         fpeq = phba->sli4_hba.fp_eq[fcp_eqidx];
9516
9517         /* Check device state for handling interrupt */
9518         if (unlikely(lpfc_intr_state_check(phba))) {
9519                 /* Check again for link_state with lock held */
9520                 spin_lock_irqsave(&phba->hbalock, iflag);
9521                 if (phba->link_state < LPFC_LINK_DOWN)
9522                         /* Flush, clear interrupt, and rearm the EQ */
9523                         lpfc_sli4_eq_flush(phba, fpeq);
9524                 spin_unlock_irqrestore(&phba->hbalock, iflag);
9525                 return IRQ_NONE;
9526         }
9527
9528         /*
9529          * Process all the event on FCP fast-path EQ
9530          */
9531         while ((eqe = lpfc_sli4_eq_get(fpeq))) {
9532                 lpfc_sli4_fp_handle_eqe(phba, eqe, fcp_eqidx);
9533                 if (!(++ecount % LPFC_GET_QE_REL_INT))
9534                         lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
9535         }
9536
9537         /* Always clear and re-arm the fast-path EQ */
9538         lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
9539
9540         if (unlikely(ecount == 0)) {
9541                 if (phba->intr_type == MSIX)
9542                         /* MSI-X treated interrupt served as no EQ share INT */
9543                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9544                                         "0358 MSI-X interrupt with no EQE\n");
9545                 else
9546                         /* Non MSI-X treated on interrupt as EQ share INT */
9547                         return IRQ_NONE;
9548         }
9549
9550         return IRQ_HANDLED;
9551 } /* lpfc_sli4_fp_intr_handler */
9552
9553 /**
9554  * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
9555  * @irq: Interrupt number.
9556  * @dev_id: The device context pointer.
9557  *
9558  * This function is the device-level interrupt handler to device with SLI-4
9559  * interface spec, called from the PCI layer when either MSI or Pin-IRQ
9560  * interrupt mode is enabled and there is an event in the HBA which requires
9561  * driver attention. This function invokes the slow-path interrupt attention
9562  * handling function and fast-path interrupt attention handling function in
9563  * turn to process the relevant HBA attention events. This function is called
9564  * without any lock held. It gets the hbalock to access and update SLI data
9565  * structures.
9566  *
9567  * This function returns IRQ_HANDLED when interrupt is handled, else it
9568  * returns IRQ_NONE.
9569  **/
9570 irqreturn_t
9571 lpfc_sli4_intr_handler(int irq, void *dev_id)
9572 {
9573         struct lpfc_hba  *phba;
9574         irqreturn_t sp_irq_rc, fp_irq_rc;
9575         bool fp_handled = false;
9576         uint32_t fcp_eqidx;
9577
9578         /* Get the driver's phba structure from the dev_id */
9579         phba = (struct lpfc_hba *)dev_id;
9580
9581         if (unlikely(!phba))
9582                 return IRQ_NONE;
9583
9584         /*
9585          * Invokes slow-path host attention interrupt handling as appropriate.
9586          */
9587         sp_irq_rc = lpfc_sli4_sp_intr_handler(irq, dev_id);
9588
9589         /*
9590          * Invoke fast-path host attention interrupt handling as appropriate.
9591          */
9592         for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
9593                 fp_irq_rc = lpfc_sli4_fp_intr_handler(irq,
9594                                         &phba->sli4_hba.fcp_eq_hdl[fcp_eqidx]);
9595                 if (fp_irq_rc == IRQ_HANDLED)
9596                         fp_handled |= true;
9597         }
9598
9599         return (fp_handled == true) ? IRQ_HANDLED : sp_irq_rc;
9600 } /* lpfc_sli4_intr_handler */
9601
9602 /**
9603  * lpfc_sli4_queue_free - free a queue structure and associated memory
9604  * @queue: The queue structure to free.
9605  *
9606  * This function frees a queue structure and the DMAable memeory used for
9607  * the host resident queue. This function must be called after destroying the
9608  * queue on the HBA.
9609  **/
9610 void
9611 lpfc_sli4_queue_free(struct lpfc_queue *queue)
9612 {
9613         struct lpfc_dmabuf *dmabuf;
9614
9615         if (!queue)
9616                 return;
9617
9618         while (!list_empty(&queue->page_list)) {
9619                 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
9620                                  list);
9621                 dma_free_coherent(&queue->phba->pcidev->dev, SLI4_PAGE_SIZE,
9622                                   dmabuf->virt, dmabuf->phys);
9623                 kfree(dmabuf);
9624         }
9625         kfree(queue);
9626         return;
9627 }
9628
9629 /**
9630  * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
9631  * @phba: The HBA that this queue is being created on.
9632  * @entry_size: The size of each queue entry for this queue.
9633  * @entry count: The number of entries that this queue will handle.
9634  *
9635  * This function allocates a queue structure and the DMAable memory used for
9636  * the host resident queue. This function must be called before creating the
9637  * queue on the HBA.
9638  **/
9639 struct lpfc_queue *
9640 lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
9641                       uint32_t entry_count)
9642 {
9643         struct lpfc_queue *queue;
9644         struct lpfc_dmabuf *dmabuf;
9645         int x, total_qe_count;
9646         void *dma_pointer;
9647         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
9648
9649         if (!phba->sli4_hba.pc_sli4_params.supported)
9650                 hw_page_size = SLI4_PAGE_SIZE;
9651
9652         queue = kzalloc(sizeof(struct lpfc_queue) +
9653                         (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
9654         if (!queue)
9655                 return NULL;
9656         queue->page_count = (ALIGN(entry_size * entry_count,
9657                         hw_page_size))/hw_page_size;
9658         INIT_LIST_HEAD(&queue->list);
9659         INIT_LIST_HEAD(&queue->page_list);
9660         INIT_LIST_HEAD(&queue->child_list);
9661         for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
9662                 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
9663                 if (!dmabuf)
9664                         goto out_fail;
9665                 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
9666                                                   hw_page_size, &dmabuf->phys,
9667                                                   GFP_KERNEL);
9668                 if (!dmabuf->virt) {
9669                         kfree(dmabuf);
9670                         goto out_fail;
9671                 }
9672                 memset(dmabuf->virt, 0, hw_page_size);
9673                 dmabuf->buffer_tag = x;
9674                 list_add_tail(&dmabuf->list, &queue->page_list);
9675                 /* initialize queue's entry array */
9676                 dma_pointer = dmabuf->virt;
9677                 for (; total_qe_count < entry_count &&
9678                      dma_pointer < (hw_page_size + dmabuf->virt);
9679                      total_qe_count++, dma_pointer += entry_size) {
9680                         queue->qe[total_qe_count].address = dma_pointer;
9681                 }
9682         }
9683         queue->entry_size = entry_size;
9684         queue->entry_count = entry_count;
9685         queue->phba = phba;
9686
9687         return queue;
9688 out_fail:
9689         lpfc_sli4_queue_free(queue);
9690         return NULL;
9691 }
9692
9693 /**
9694  * lpfc_eq_create - Create an Event Queue on the HBA
9695  * @phba: HBA structure that indicates port to create a queue on.
9696  * @eq: The queue structure to use to create the event queue.
9697  * @imax: The maximum interrupt per second limit.
9698  *
9699  * This function creates an event queue, as detailed in @eq, on a port,
9700  * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
9701  *
9702  * The @phba struct is used to send mailbox command to HBA. The @eq struct
9703  * is used to get the entry count and entry size that are necessary to
9704  * determine the number of pages to allocate and use for this queue. This
9705  * function will send the EQ_CREATE mailbox command to the HBA to setup the
9706  * event queue. This function is asynchronous and will wait for the mailbox
9707  * command to finish before continuing.
9708  *
9709  * On success this function will return a zero. If unable to allocate enough
9710  * memory this function will return ENOMEM. If the queue create mailbox command
9711  * fails this function will return ENXIO.
9712  **/
9713 uint32_t
9714 lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint16_t imax)
9715 {
9716         struct lpfc_mbx_eq_create *eq_create;
9717         LPFC_MBOXQ_t *mbox;
9718         int rc, length, status = 0;
9719         struct lpfc_dmabuf *dmabuf;
9720         uint32_t shdr_status, shdr_add_status;
9721         union lpfc_sli4_cfg_shdr *shdr;
9722         uint16_t dmult;
9723         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
9724
9725         if (!phba->sli4_hba.pc_sli4_params.supported)
9726                 hw_page_size = SLI4_PAGE_SIZE;
9727
9728         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9729         if (!mbox)
9730                 return -ENOMEM;
9731         length = (sizeof(struct lpfc_mbx_eq_create) -
9732                   sizeof(struct lpfc_sli4_cfg_mhdr));
9733         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
9734                          LPFC_MBOX_OPCODE_EQ_CREATE,
9735                          length, LPFC_SLI4_MBX_EMBED);
9736         eq_create = &mbox->u.mqe.un.eq_create;
9737         bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
9738                eq->page_count);
9739         bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
9740                LPFC_EQE_SIZE);
9741         bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
9742         /* Calculate delay multiper from maximum interrupt per second */
9743         dmult = LPFC_DMULT_CONST/imax - 1;
9744         bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
9745                dmult);
9746         switch (eq->entry_count) {
9747         default:
9748                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9749                                 "0360 Unsupported EQ count. (%d)\n",
9750                                 eq->entry_count);
9751                 if (eq->entry_count < 256)
9752                         return -EINVAL;
9753                 /* otherwise default to smallest count (drop through) */
9754         case 256:
9755                 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9756                        LPFC_EQ_CNT_256);
9757                 break;
9758         case 512:
9759                 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9760                        LPFC_EQ_CNT_512);
9761                 break;
9762         case 1024:
9763                 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9764                        LPFC_EQ_CNT_1024);
9765                 break;
9766         case 2048:
9767                 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9768                        LPFC_EQ_CNT_2048);
9769                 break;
9770         case 4096:
9771                 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9772                        LPFC_EQ_CNT_4096);
9773                 break;
9774         }
9775         list_for_each_entry(dmabuf, &eq->page_list, list) {
9776                 memset(dmabuf->virt, 0, hw_page_size);
9777                 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9778                                         putPaddrLow(dmabuf->phys);
9779                 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9780                                         putPaddrHigh(dmabuf->phys);
9781         }
9782         mbox->vport = phba->pport;
9783         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
9784         mbox->context1 = NULL;
9785         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9786         shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
9787         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9788         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9789         if (shdr_status || shdr_add_status || rc) {
9790                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9791                                 "2500 EQ_CREATE mailbox failed with "
9792                                 "status x%x add_status x%x, mbx status x%x\n",
9793                                 shdr_status, shdr_add_status, rc);
9794                 status = -ENXIO;
9795         }
9796         eq->type = LPFC_EQ;
9797         eq->subtype = LPFC_NONE;
9798         eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
9799         if (eq->queue_id == 0xFFFF)
9800                 status = -ENXIO;
9801         eq->host_index = 0;
9802         eq->hba_index = 0;
9803
9804         mempool_free(mbox, phba->mbox_mem_pool);
9805         return status;
9806 }
9807
9808 /**
9809  * lpfc_cq_create - Create a Completion Queue on the HBA
9810  * @phba: HBA structure that indicates port to create a queue on.
9811  * @cq: The queue structure to use to create the completion queue.
9812  * @eq: The event queue to bind this completion queue to.
9813  *
9814  * This function creates a completion queue, as detailed in @wq, on a port,
9815  * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
9816  *
9817  * The @phba struct is used to send mailbox command to HBA. The @cq struct
9818  * is used to get the entry count and entry size that are necessary to
9819  * determine the number of pages to allocate and use for this queue. The @eq
9820  * is used to indicate which event queue to bind this completion queue to. This
9821  * function will send the CQ_CREATE mailbox command to the HBA to setup the
9822  * completion queue. This function is asynchronous and will wait for the mailbox
9823  * command to finish before continuing.
9824  *
9825  * On success this function will return a zero. If unable to allocate enough
9826  * memory this function will return ENOMEM. If the queue create mailbox command
9827  * fails this function will return ENXIO.
9828  **/
9829 uint32_t
9830 lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
9831                struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
9832 {
9833         struct lpfc_mbx_cq_create *cq_create;
9834         struct lpfc_dmabuf *dmabuf;
9835         LPFC_MBOXQ_t *mbox;
9836         int rc, length, status = 0;
9837         uint32_t shdr_status, shdr_add_status;
9838         union lpfc_sli4_cfg_shdr *shdr;
9839         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
9840
9841         if (!phba->sli4_hba.pc_sli4_params.supported)
9842                 hw_page_size = SLI4_PAGE_SIZE;
9843
9844
9845         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9846         if (!mbox)
9847                 return -ENOMEM;
9848         length = (sizeof(struct lpfc_mbx_cq_create) -
9849                   sizeof(struct lpfc_sli4_cfg_mhdr));
9850         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
9851                          LPFC_MBOX_OPCODE_CQ_CREATE,
9852                          length, LPFC_SLI4_MBX_EMBED);
9853         cq_create = &mbox->u.mqe.un.cq_create;
9854         bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
9855                     cq->page_count);
9856         bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
9857         bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
9858         bf_set(lpfc_cq_eq_id, &cq_create->u.request.context, eq->queue_id);
9859         switch (cq->entry_count) {
9860         default:
9861                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9862                                 "0361 Unsupported CQ count. (%d)\n",
9863                                 cq->entry_count);
9864                 if (cq->entry_count < 256)
9865                         return -EINVAL;
9866                 /* otherwise default to smallest count (drop through) */
9867         case 256:
9868                 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
9869                        LPFC_CQ_CNT_256);
9870                 break;
9871         case 512:
9872                 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
9873                        LPFC_CQ_CNT_512);
9874                 break;
9875         case 1024:
9876                 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
9877                        LPFC_CQ_CNT_1024);
9878                 break;
9879         }
9880         list_for_each_entry(dmabuf, &cq->page_list, list) {
9881                 memset(dmabuf->virt, 0, hw_page_size);
9882                 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9883                                         putPaddrLow(dmabuf->phys);
9884                 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9885                                         putPaddrHigh(dmabuf->phys);
9886         }
9887         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9888
9889         /* The IOCTL status is embedded in the mailbox subheader. */
9890         shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
9891         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9892         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9893         if (shdr_status || shdr_add_status || rc) {
9894                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9895                                 "2501 CQ_CREATE mailbox failed with "
9896                                 "status x%x add_status x%x, mbx status x%x\n",
9897                                 shdr_status, shdr_add_status, rc);
9898                 status = -ENXIO;
9899                 goto out;
9900         }
9901         cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
9902         if (cq->queue_id == 0xFFFF) {
9903                 status = -ENXIO;
9904                 goto out;
9905         }
9906         /* link the cq onto the parent eq child list */
9907         list_add_tail(&cq->list, &eq->child_list);
9908         /* Set up completion queue's type and subtype */
9909         cq->type = type;
9910         cq->subtype = subtype;
9911         cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
9912         cq->host_index = 0;
9913         cq->hba_index = 0;
9914
9915 out:
9916         mempool_free(mbox, phba->mbox_mem_pool);
9917         return status;
9918 }
9919
9920 /**
9921  * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
9922  * @phba: HBA structure that indicates port to create a queue on.
9923  * @mq: The queue structure to use to create the mailbox queue.
9924  * @mbox: An allocated pointer to type LPFC_MBOXQ_t
9925  * @cq: The completion queue to associate with this cq.
9926  *
9927  * This function provides failback (fb) functionality when the
9928  * mq_create_ext fails on older FW generations.  It's purpose is identical
9929  * to mq_create_ext otherwise.
9930  *
9931  * This routine cannot fail as all attributes were previously accessed and
9932  * initialized in mq_create_ext.
9933  **/
9934 static void
9935 lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
9936                        LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
9937 {
9938         struct lpfc_mbx_mq_create *mq_create;
9939         struct lpfc_dmabuf *dmabuf;
9940         int length;
9941
9942         length = (sizeof(struct lpfc_mbx_mq_create) -
9943                   sizeof(struct lpfc_sli4_cfg_mhdr));
9944         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
9945                          LPFC_MBOX_OPCODE_MQ_CREATE,
9946                          length, LPFC_SLI4_MBX_EMBED);
9947         mq_create = &mbox->u.mqe.un.mq_create;
9948         bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
9949                mq->page_count);
9950         bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
9951                cq->queue_id);
9952         bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
9953         switch (mq->entry_count) {
9954         case 16:
9955                 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
9956                        LPFC_MQ_CNT_16);
9957                 break;
9958         case 32:
9959                 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
9960                        LPFC_MQ_CNT_32);
9961                 break;
9962         case 64:
9963                 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
9964                        LPFC_MQ_CNT_64);
9965                 break;
9966         case 128:
9967                 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
9968                        LPFC_MQ_CNT_128);
9969                 break;
9970         }
9971         list_for_each_entry(dmabuf, &mq->page_list, list) {
9972                 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9973                         putPaddrLow(dmabuf->phys);
9974                 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9975                         putPaddrHigh(dmabuf->phys);
9976         }
9977 }
9978
9979 /**
9980  * lpfc_mq_create - Create a mailbox Queue on the HBA
9981  * @phba: HBA structure that indicates port to create a queue on.
9982  * @mq: The queue structure to use to create the mailbox queue.
9983  * @cq: The completion queue to associate with this cq.
9984  * @subtype: The queue's subtype.
9985  *
9986  * This function creates a mailbox queue, as detailed in @mq, on a port,
9987  * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
9988  *
9989  * The @phba struct is used to send mailbox command to HBA. The @cq struct
9990  * is used to get the entry count and entry size that are necessary to
9991  * determine the number of pages to allocate and use for this queue. This
9992  * function will send the MQ_CREATE mailbox command to the HBA to setup the
9993  * mailbox queue. This function is asynchronous and will wait for the mailbox
9994  * command to finish before continuing.
9995  *
9996  * On success this function will return a zero. If unable to allocate enough
9997  * memory this function will return ENOMEM. If the queue create mailbox command
9998  * fails this function will return ENXIO.
9999  **/
10000 int32_t
10001 lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
10002                struct lpfc_queue *cq, uint32_t subtype)
10003 {
10004         struct lpfc_mbx_mq_create *mq_create;
10005         struct lpfc_mbx_mq_create_ext *mq_create_ext;
10006         struct lpfc_dmabuf *dmabuf;
10007         LPFC_MBOXQ_t *mbox;
10008         int rc, length, status = 0;
10009         uint32_t shdr_status, shdr_add_status;
10010         union lpfc_sli4_cfg_shdr *shdr;
10011         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
10012
10013         if (!phba->sli4_hba.pc_sli4_params.supported)
10014                 hw_page_size = SLI4_PAGE_SIZE;
10015
10016         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10017         if (!mbox)
10018                 return -ENOMEM;
10019         length = (sizeof(struct lpfc_mbx_mq_create_ext) -
10020                   sizeof(struct lpfc_sli4_cfg_mhdr));
10021         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10022                          LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
10023                          length, LPFC_SLI4_MBX_EMBED);
10024
10025         mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
10026         bf_set(lpfc_mbx_mq_create_ext_num_pages, &mq_create_ext->u.request,
10027                     mq->page_count);
10028         bf_set(lpfc_mbx_mq_create_ext_async_evt_link, &mq_create_ext->u.request,
10029                1);
10030         bf_set(lpfc_mbx_mq_create_ext_async_evt_fcfste,
10031                &mq_create_ext->u.request, 1);
10032         bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
10033                &mq_create_ext->u.request, 1);
10034         bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
10035                cq->queue_id);
10036         bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
10037         switch (mq->entry_count) {
10038         default:
10039                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10040                                 "0362 Unsupported MQ count. (%d)\n",
10041                                 mq->entry_count);
10042                 if (mq->entry_count < 16)
10043                         return -EINVAL;
10044                 /* otherwise default to smallest count (drop through) */
10045         case 16:
10046                 bf_set(lpfc_mq_context_count, &mq_create_ext->u.request.context,
10047                        LPFC_MQ_CNT_16);
10048                 break;
10049         case 32:
10050                 bf_set(lpfc_mq_context_count, &mq_create_ext->u.request.context,
10051                        LPFC_MQ_CNT_32);
10052                 break;
10053         case 64:
10054                 bf_set(lpfc_mq_context_count, &mq_create_ext->u.request.context,
10055                        LPFC_MQ_CNT_64);
10056                 break;
10057         case 128:
10058                 bf_set(lpfc_mq_context_count, &mq_create_ext->u.request.context,
10059                        LPFC_MQ_CNT_128);
10060                 break;
10061         }
10062         list_for_each_entry(dmabuf, &mq->page_list, list) {
10063                 memset(dmabuf->virt, 0, hw_page_size);
10064                 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
10065                                         putPaddrLow(dmabuf->phys);
10066                 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
10067                                         putPaddrHigh(dmabuf->phys);
10068         }
10069         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10070         shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
10071         mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
10072                               &mq_create_ext->u.response);
10073         if (rc != MBX_SUCCESS) {
10074                 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10075                                 "2795 MQ_CREATE_EXT failed with "
10076                                 "status x%x. Failback to MQ_CREATE.\n",
10077                                 rc);
10078                 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
10079                 mq_create = &mbox->u.mqe.un.mq_create;
10080                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10081                 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
10082                 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
10083                                       &mq_create->u.response);
10084         }
10085
10086         /* The IOCTL status is embedded in the mailbox subheader. */
10087         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10088         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10089         if (shdr_status || shdr_add_status || rc) {
10090                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10091                                 "2502 MQ_CREATE mailbox failed with "
10092                                 "status x%x add_status x%x, mbx status x%x\n",
10093                                 shdr_status, shdr_add_status, rc);
10094                 status = -ENXIO;
10095                 goto out;
10096         }
10097         if (mq->queue_id == 0xFFFF) {
10098                 status = -ENXIO;
10099                 goto out;
10100         }
10101         mq->type = LPFC_MQ;
10102         mq->subtype = subtype;
10103         mq->host_index = 0;
10104         mq->hba_index = 0;
10105
10106         /* link the mq onto the parent cq child list */
10107         list_add_tail(&mq->list, &cq->child_list);
10108 out:
10109         mempool_free(mbox, phba->mbox_mem_pool);
10110         return status;
10111 }
10112
10113 /**
10114  * lpfc_wq_create - Create a Work Queue on the HBA
10115  * @phba: HBA structure that indicates port to create a queue on.
10116  * @wq: The queue structure to use to create the work queue.
10117  * @cq: The completion queue to bind this work queue to.
10118  * @subtype: The subtype of the work queue indicating its functionality.
10119  *
10120  * This function creates a work queue, as detailed in @wq, on a port, described
10121  * by @phba by sending a WQ_CREATE mailbox command to the HBA.
10122  *
10123  * The @phba struct is used to send mailbox command to HBA. The @wq struct
10124  * is used to get the entry count and entry size that are necessary to
10125  * determine the number of pages to allocate and use for this queue. The @cq
10126  * is used to indicate which completion queue to bind this work queue to. This
10127  * function will send the WQ_CREATE mailbox command to the HBA to setup the
10128  * work queue. This function is asynchronous and will wait for the mailbox
10129  * command to finish before continuing.
10130  *
10131  * On success this function will return a zero. If unable to allocate enough
10132  * memory this function will return ENOMEM. If the queue create mailbox command
10133  * fails this function will return ENXIO.
10134  **/
10135 uint32_t
10136 lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
10137                struct lpfc_queue *cq, uint32_t subtype)
10138 {
10139         struct lpfc_mbx_wq_create *wq_create;
10140         struct lpfc_dmabuf *dmabuf;
10141         LPFC_MBOXQ_t *mbox;
10142         int rc, length, status = 0;
10143         uint32_t shdr_status, shdr_add_status;
10144         union lpfc_sli4_cfg_shdr *shdr;
10145         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
10146
10147         if (!phba->sli4_hba.pc_sli4_params.supported)
10148                 hw_page_size = SLI4_PAGE_SIZE;
10149
10150         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10151         if (!mbox)
10152                 return -ENOMEM;
10153         length = (sizeof(struct lpfc_mbx_wq_create) -
10154                   sizeof(struct lpfc_sli4_cfg_mhdr));
10155         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10156                          LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
10157                          length, LPFC_SLI4_MBX_EMBED);
10158         wq_create = &mbox->u.mqe.un.wq_create;
10159         bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
10160                     wq->page_count);
10161         bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
10162                     cq->queue_id);
10163         list_for_each_entry(dmabuf, &wq->page_list, list) {
10164                 memset(dmabuf->virt, 0, hw_page_size);
10165                 wq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
10166                                         putPaddrLow(dmabuf->phys);
10167                 wq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
10168                                         putPaddrHigh(dmabuf->phys);
10169         }
10170         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10171         /* The IOCTL status is embedded in the mailbox subheader. */
10172         shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
10173         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10174         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10175         if (shdr_status || shdr_add_status || rc) {
10176                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10177                                 "2503 WQ_CREATE mailbox failed with "
10178                                 "status x%x add_status x%x, mbx status x%x\n",
10179                                 shdr_status, shdr_add_status, rc);
10180                 status = -ENXIO;
10181                 goto out;
10182         }
10183         wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
10184         if (wq->queue_id == 0xFFFF) {
10185                 status = -ENXIO;
10186                 goto out;
10187         }
10188         wq->type = LPFC_WQ;
10189         wq->subtype = subtype;
10190         wq->host_index = 0;
10191         wq->hba_index = 0;
10192
10193         /* link the wq onto the parent cq child list */
10194         list_add_tail(&wq->list, &cq->child_list);
10195 out:
10196         mempool_free(mbox, phba->mbox_mem_pool);
10197         return status;
10198 }
10199
10200 /**
10201  * lpfc_rq_create - Create a Receive Queue on the HBA
10202  * @phba: HBA structure that indicates port to create a queue on.
10203  * @hrq: The queue structure to use to create the header receive queue.
10204  * @drq: The queue structure to use to create the data receive queue.
10205  * @cq: The completion queue to bind this work queue to.
10206  *
10207  * This function creates a receive buffer queue pair , as detailed in @hrq and
10208  * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
10209  * to the HBA.
10210  *
10211  * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
10212  * struct is used to get the entry count that is necessary to determine the
10213  * number of pages to use for this queue. The @cq is used to indicate which
10214  * completion queue to bind received buffers that are posted to these queues to.
10215  * This function will send the RQ_CREATE mailbox command to the HBA to setup the
10216  * receive queue pair. This function is asynchronous and will wait for the
10217  * mailbox command to finish before continuing.
10218  *
10219  * On success this function will return a zero. If unable to allocate enough
10220  * memory this function will return ENOMEM. If the queue create mailbox command
10221  * fails this function will return ENXIO.
10222  **/
10223 uint32_t
10224 lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
10225                struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
10226 {
10227         struct lpfc_mbx_rq_create *rq_create;
10228         struct lpfc_dmabuf *dmabuf;
10229         LPFC_MBOXQ_t *mbox;
10230         int rc, length, status = 0;
10231         uint32_t shdr_status, shdr_add_status;
10232         union lpfc_sli4_cfg_shdr *shdr;
10233         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
10234
10235         if (!phba->sli4_hba.pc_sli4_params.supported)
10236                 hw_page_size = SLI4_PAGE_SIZE;
10237
10238         if (hrq->entry_count != drq->entry_count)
10239                 return -EINVAL;
10240         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10241         if (!mbox)
10242                 return -ENOMEM;
10243         length = (sizeof(struct lpfc_mbx_rq_create) -
10244                   sizeof(struct lpfc_sli4_cfg_mhdr));
10245         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10246                          LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
10247                          length, LPFC_SLI4_MBX_EMBED);
10248         rq_create = &mbox->u.mqe.un.rq_create;
10249         switch (hrq->entry_count) {
10250         default:
10251                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10252                                 "2535 Unsupported RQ count. (%d)\n",
10253                                 hrq->entry_count);
10254                 if (hrq->entry_count < 512)
10255                         return -EINVAL;
10256                 /* otherwise default to smallest count (drop through) */
10257         case 512:
10258                 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10259                        LPFC_RQ_RING_SIZE_512);
10260                 break;
10261         case 1024:
10262                 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10263                        LPFC_RQ_RING_SIZE_1024);
10264                 break;
10265         case 2048:
10266                 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10267                        LPFC_RQ_RING_SIZE_2048);
10268                 break;
10269         case 4096:
10270                 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10271                        LPFC_RQ_RING_SIZE_4096);
10272                 break;
10273         }
10274         bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
10275                cq->queue_id);
10276         bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
10277                hrq->page_count);
10278         bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
10279                LPFC_HDR_BUF_SIZE);
10280         list_for_each_entry(dmabuf, &hrq->page_list, list) {
10281                 memset(dmabuf->virt, 0, hw_page_size);
10282                 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
10283                                         putPaddrLow(dmabuf->phys);
10284                 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
10285                                         putPaddrHigh(dmabuf->phys);
10286         }
10287         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10288         /* The IOCTL status is embedded in the mailbox subheader. */
10289         shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
10290         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10291         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10292         if (shdr_status || shdr_add_status || rc) {
10293                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10294                                 "2504 RQ_CREATE mailbox failed with "
10295                                 "status x%x add_status x%x, mbx status x%x\n",
10296                                 shdr_status, shdr_add_status, rc);
10297                 status = -ENXIO;
10298                 goto out;
10299         }
10300         hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
10301         if (hrq->queue_id == 0xFFFF) {
10302                 status = -ENXIO;
10303                 goto out;
10304         }
10305         hrq->type = LPFC_HRQ;
10306         hrq->subtype = subtype;
10307         hrq->host_index = 0;
10308         hrq->hba_index = 0;
10309
10310         /* now create the data queue */
10311         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10312                          LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
10313                          length, LPFC_SLI4_MBX_EMBED);
10314         switch (drq->entry_count) {
10315         default:
10316                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10317                                 "2536 Unsupported RQ count. (%d)\n",
10318                                 drq->entry_count);
10319                 if (drq->entry_count < 512)
10320                         return -EINVAL;
10321                 /* otherwise default to smallest count (drop through) */
10322         case 512:
10323                 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10324                        LPFC_RQ_RING_SIZE_512);
10325                 break;
10326         case 1024:
10327                 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10328                        LPFC_RQ_RING_SIZE_1024);
10329                 break;
10330         case 2048:
10331                 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10332                        LPFC_RQ_RING_SIZE_2048);
10333                 break;
10334         case 4096:
10335                 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10336                        LPFC_RQ_RING_SIZE_4096);
10337                 break;
10338         }
10339         bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
10340                cq->queue_id);
10341         bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
10342                drq->page_count);
10343         bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
10344                LPFC_DATA_BUF_SIZE);
10345         list_for_each_entry(dmabuf, &drq->page_list, list) {
10346                 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
10347                                         putPaddrLow(dmabuf->phys);
10348                 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
10349                                         putPaddrHigh(dmabuf->phys);
10350         }
10351         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10352         /* The IOCTL status is embedded in the mailbox subheader. */
10353         shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
10354         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10355         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10356         if (shdr_status || shdr_add_status || rc) {
10357                 status = -ENXIO;
10358                 goto out;
10359         }
10360         drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
10361         if (drq->queue_id == 0xFFFF) {
10362                 status = -ENXIO;
10363                 goto out;
10364         }
10365         drq->type = LPFC_DRQ;
10366         drq->subtype = subtype;
10367         drq->host_index = 0;
10368         drq->hba_index = 0;
10369
10370         /* link the header and data RQs onto the parent cq child list */
10371         list_add_tail(&hrq->list, &cq->child_list);
10372         list_add_tail(&drq->list, &cq->child_list);
10373
10374 out:
10375         mempool_free(mbox, phba->mbox_mem_pool);
10376         return status;
10377 }
10378
10379 /**
10380  * lpfc_eq_destroy - Destroy an event Queue on the HBA
10381  * @eq: The queue structure associated with the queue to destroy.
10382  *
10383  * This function destroys a queue, as detailed in @eq by sending an mailbox
10384  * command, specific to the type of queue, to the HBA.
10385  *
10386  * The @eq struct is used to get the queue ID of the queue to destroy.
10387  *
10388  * On success this function will return a zero. If the queue destroy mailbox
10389  * command fails this function will return ENXIO.
10390  **/
10391 uint32_t
10392 lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
10393 {
10394         LPFC_MBOXQ_t *mbox;
10395         int rc, length, status = 0;
10396         uint32_t shdr_status, shdr_add_status;
10397         union lpfc_sli4_cfg_shdr *shdr;
10398
10399         if (!eq)
10400                 return -ENODEV;
10401         mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
10402         if (!mbox)
10403                 return -ENOMEM;
10404         length = (sizeof(struct lpfc_mbx_eq_destroy) -
10405                   sizeof(struct lpfc_sli4_cfg_mhdr));
10406         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10407                          LPFC_MBOX_OPCODE_EQ_DESTROY,
10408                          length, LPFC_SLI4_MBX_EMBED);
10409         bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
10410                eq->queue_id);
10411         mbox->vport = eq->phba->pport;
10412         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10413
10414         rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
10415         /* The IOCTL status is embedded in the mailbox subheader. */
10416         shdr = (union lpfc_sli4_cfg_shdr *)
10417                 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
10418         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10419         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10420         if (shdr_status || shdr_add_status || rc) {
10421                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10422                                 "2505 EQ_DESTROY mailbox failed with "
10423                                 "status x%x add_status x%x, mbx status x%x\n",
10424                                 shdr_status, shdr_add_status, rc);
10425                 status = -ENXIO;
10426         }
10427
10428         /* Remove eq from any list */
10429         list_del_init(&eq->list);
10430         mempool_free(mbox, eq->phba->mbox_mem_pool);
10431         return status;
10432 }
10433
10434 /**
10435  * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
10436  * @cq: The queue structure associated with the queue to destroy.
10437  *
10438  * This function destroys a queue, as detailed in @cq by sending an mailbox
10439  * command, specific to the type of queue, to the HBA.
10440  *
10441  * The @cq struct is used to get the queue ID of the queue to destroy.
10442  *
10443  * On success this function will return a zero. If the queue destroy mailbox
10444  * command fails this function will return ENXIO.
10445  **/
10446 uint32_t
10447 lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
10448 {
10449         LPFC_MBOXQ_t *mbox;
10450         int rc, length, status = 0;
10451         uint32_t shdr_status, shdr_add_status;
10452         union lpfc_sli4_cfg_shdr *shdr;
10453
10454         if (!cq)
10455                 return -ENODEV;
10456         mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
10457         if (!mbox)
10458                 return -ENOMEM;
10459         length = (sizeof(struct lpfc_mbx_cq_destroy) -
10460                   sizeof(struct lpfc_sli4_cfg_mhdr));
10461         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10462                          LPFC_MBOX_OPCODE_CQ_DESTROY,
10463                          length, LPFC_SLI4_MBX_EMBED);
10464         bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
10465                cq->queue_id);
10466         mbox->vport = cq->phba->pport;
10467         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10468         rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
10469         /* The IOCTL status is embedded in the mailbox subheader. */
10470         shdr = (union lpfc_sli4_cfg_shdr *)
10471                 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
10472         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10473         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10474         if (shdr_status || shdr_add_status || rc) {
10475                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10476                                 "2506 CQ_DESTROY mailbox failed with "
10477                                 "status x%x add_status x%x, mbx status x%x\n",
10478                                 shdr_status, shdr_add_status, rc);
10479                 status = -ENXIO;
10480         }
10481         /* Remove cq from any list */
10482         list_del_init(&cq->list);
10483         mempool_free(mbox, cq->phba->mbox_mem_pool);
10484         return status;
10485 }
10486
10487 /**
10488  * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
10489  * @qm: The queue structure associated with the queue to destroy.
10490  *
10491  * This function destroys a queue, as detailed in @mq by sending an mailbox
10492  * command, specific to the type of queue, to the HBA.
10493  *
10494  * The @mq struct is used to get the queue ID of the queue to destroy.
10495  *
10496  * On success this function will return a zero. If the queue destroy mailbox
10497  * command fails this function will return ENXIO.
10498  **/
10499 uint32_t
10500 lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
10501 {
10502         LPFC_MBOXQ_t *mbox;
10503         int rc, length, status = 0;
10504         uint32_t shdr_status, shdr_add_status;
10505         union lpfc_sli4_cfg_shdr *shdr;
10506
10507         if (!mq)
10508                 return -ENODEV;
10509         mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
10510         if (!mbox)
10511                 return -ENOMEM;
10512         length = (sizeof(struct lpfc_mbx_mq_destroy) -
10513                   sizeof(struct lpfc_sli4_cfg_mhdr));
10514         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10515                          LPFC_MBOX_OPCODE_MQ_DESTROY,
10516                          length, LPFC_SLI4_MBX_EMBED);
10517         bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
10518                mq->queue_id);
10519         mbox->vport = mq->phba->pport;
10520         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10521         rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
10522         /* The IOCTL status is embedded in the mailbox subheader. */
10523         shdr = (union lpfc_sli4_cfg_shdr *)
10524                 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
10525         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10526         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10527         if (shdr_status || shdr_add_status || rc) {
10528                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10529                                 "2507 MQ_DESTROY mailbox failed with "
10530                                 "status x%x add_status x%x, mbx status x%x\n",
10531                                 shdr_status, shdr_add_status, rc);
10532                 status = -ENXIO;
10533         }
10534         /* Remove mq from any list */
10535         list_del_init(&mq->list);
10536         mempool_free(mbox, mq->phba->mbox_mem_pool);
10537         return status;
10538 }
10539
10540 /**
10541  * lpfc_wq_destroy - Destroy a Work Queue on the HBA
10542  * @wq: The queue structure associated with the queue to destroy.
10543  *
10544  * This function destroys a queue, as detailed in @wq by sending an mailbox
10545  * command, specific to the type of queue, to the HBA.
10546  *
10547  * The @wq struct is used to get the queue ID of the queue to destroy.
10548  *
10549  * On success this function will return a zero. If the queue destroy mailbox
10550  * command fails this function will return ENXIO.
10551  **/
10552 uint32_t
10553 lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
10554 {
10555         LPFC_MBOXQ_t *mbox;
10556         int rc, length, status = 0;
10557         uint32_t shdr_status, shdr_add_status;
10558         union lpfc_sli4_cfg_shdr *shdr;
10559
10560         if (!wq)
10561                 return -ENODEV;
10562         mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
10563         if (!mbox)
10564                 return -ENOMEM;
10565         length = (sizeof(struct lpfc_mbx_wq_destroy) -
10566                   sizeof(struct lpfc_sli4_cfg_mhdr));
10567         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10568                          LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
10569                          length, LPFC_SLI4_MBX_EMBED);
10570         bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
10571                wq->queue_id);
10572         mbox->vport = wq->phba->pport;
10573         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10574         rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
10575         shdr = (union lpfc_sli4_cfg_shdr *)
10576                 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
10577         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10578         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10579         if (shdr_status || shdr_add_status || rc) {
10580                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10581                                 "2508 WQ_DESTROY mailbox failed with "
10582                                 "status x%x add_status x%x, mbx status x%x\n",
10583                                 shdr_status, shdr_add_status, rc);
10584                 status = -ENXIO;
10585         }
10586         /* Remove wq from any list */
10587         list_del_init(&wq->list);
10588         mempool_free(mbox, wq->phba->mbox_mem_pool);
10589         return status;
10590 }
10591
10592 /**
10593  * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
10594  * @rq: The queue structure associated with the queue to destroy.
10595  *
10596  * This function destroys a queue, as detailed in @rq by sending an mailbox
10597  * command, specific to the type of queue, to the HBA.
10598  *
10599  * The @rq struct is used to get the queue ID of the queue to destroy.
10600  *
10601  * On success this function will return a zero. If the queue destroy mailbox
10602  * command fails this function will return ENXIO.
10603  **/
10604 uint32_t
10605 lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
10606                 struct lpfc_queue *drq)
10607 {
10608         LPFC_MBOXQ_t *mbox;
10609         int rc, length, status = 0;
10610         uint32_t shdr_status, shdr_add_status;
10611         union lpfc_sli4_cfg_shdr *shdr;
10612
10613         if (!hrq || !drq)
10614                 return -ENODEV;
10615         mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
10616         if (!mbox)
10617                 return -ENOMEM;
10618         length = (sizeof(struct lpfc_mbx_rq_destroy) -
10619                   sizeof(struct mbox_header));
10620         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10621                          LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
10622                          length, LPFC_SLI4_MBX_EMBED);
10623         bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
10624                hrq->queue_id);
10625         mbox->vport = hrq->phba->pport;
10626         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10627         rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
10628         /* The IOCTL status is embedded in the mailbox subheader. */
10629         shdr = (union lpfc_sli4_cfg_shdr *)
10630                 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
10631         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10632         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10633         if (shdr_status || shdr_add_status || rc) {
10634                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10635                                 "2509 RQ_DESTROY mailbox failed with "
10636                                 "status x%x add_status x%x, mbx status x%x\n",
10637                                 shdr_status, shdr_add_status, rc);
10638                 if (rc != MBX_TIMEOUT)
10639                         mempool_free(mbox, hrq->phba->mbox_mem_pool);
10640                 return -ENXIO;
10641         }
10642         bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
10643                drq->queue_id);
10644         rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
10645         shdr = (union lpfc_sli4_cfg_shdr *)
10646                 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
10647         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10648         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10649         if (shdr_status || shdr_add_status || rc) {
10650                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10651                                 "2510 RQ_DESTROY mailbox failed with "
10652                                 "status x%x add_status x%x, mbx status x%x\n",
10653                                 shdr_status, shdr_add_status, rc);
10654                 status = -ENXIO;
10655         }
10656         list_del_init(&hrq->list);
10657         list_del_init(&drq->list);
10658         mempool_free(mbox, hrq->phba->mbox_mem_pool);
10659         return status;
10660 }
10661
10662 /**
10663  * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
10664  * @phba: The virtual port for which this call being executed.
10665  * @pdma_phys_addr0: Physical address of the 1st SGL page.
10666  * @pdma_phys_addr1: Physical address of the 2nd SGL page.
10667  * @xritag: the xritag that ties this io to the SGL pages.
10668  *
10669  * This routine will post the sgl pages for the IO that has the xritag
10670  * that is in the iocbq structure. The xritag is assigned during iocbq
10671  * creation and persists for as long as the driver is loaded.
10672  * if the caller has fewer than 256 scatter gather segments to map then
10673  * pdma_phys_addr1 should be 0.
10674  * If the caller needs to map more than 256 scatter gather segment then
10675  * pdma_phys_addr1 should be a valid physical address.
10676  * physical address for SGLs must be 64 byte aligned.
10677  * If you are going to map 2 SGL's then the first one must have 256 entries
10678  * the second sgl can have between 1 and 256 entries.
10679  *
10680  * Return codes:
10681  *      0 - Success
10682  *      -ENXIO, -ENOMEM - Failure
10683  **/
10684 int
10685 lpfc_sli4_post_sgl(struct lpfc_hba *phba,
10686                 dma_addr_t pdma_phys_addr0,
10687                 dma_addr_t pdma_phys_addr1,
10688                 uint16_t xritag)
10689 {
10690         struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
10691         LPFC_MBOXQ_t *mbox;
10692         int rc;
10693         uint32_t shdr_status, shdr_add_status;
10694         union lpfc_sli4_cfg_shdr *shdr;
10695
10696         if (xritag == NO_XRI) {
10697                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10698                                 "0364 Invalid param:\n");
10699                 return -EINVAL;
10700         }
10701
10702         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10703         if (!mbox)
10704                 return -ENOMEM;
10705
10706         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10707                         LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
10708                         sizeof(struct lpfc_mbx_post_sgl_pages) -
10709                         sizeof(struct mbox_header), LPFC_SLI4_MBX_EMBED);
10710
10711         post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
10712                                 &mbox->u.mqe.un.post_sgl_pages;
10713         bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
10714         bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
10715
10716         post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
10717                                 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
10718         post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
10719                                 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
10720
10721         post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
10722                                 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
10723         post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
10724                                 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
10725         if (!phba->sli4_hba.intr_enable)
10726                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10727         else
10728                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
10729         /* The IOCTL status is embedded in the mailbox subheader. */
10730         shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
10731         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10732         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10733         if (rc != MBX_TIMEOUT)
10734                 mempool_free(mbox, phba->mbox_mem_pool);
10735         if (shdr_status || shdr_add_status || rc) {
10736                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10737                                 "2511 POST_SGL mailbox failed with "
10738                                 "status x%x add_status x%x, mbx status x%x\n",
10739                                 shdr_status, shdr_add_status, rc);
10740                 rc = -ENXIO;
10741         }
10742         return 0;
10743 }
10744 /**
10745  * lpfc_sli4_remove_all_sgl_pages - Post scatter gather list for an XRI to HBA
10746  * @phba: The virtual port for which this call being executed.
10747  *
10748  * This routine will remove all of the sgl pages registered with the hba.
10749  *
10750  * Return codes:
10751  *      0 - Success
10752  *      -ENXIO, -ENOMEM - Failure
10753  **/
10754 int
10755 lpfc_sli4_remove_all_sgl_pages(struct lpfc_hba *phba)
10756 {
10757         LPFC_MBOXQ_t *mbox;
10758         int rc;
10759         uint32_t shdr_status, shdr_add_status;
10760         union lpfc_sli4_cfg_shdr *shdr;
10761
10762         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10763         if (!mbox)
10764                 return -ENOMEM;
10765
10766         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10767                         LPFC_MBOX_OPCODE_FCOE_REMOVE_SGL_PAGES, 0,
10768                         LPFC_SLI4_MBX_EMBED);
10769         if (!phba->sli4_hba.intr_enable)
10770                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10771         else
10772                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
10773         /* The IOCTL status is embedded in the mailbox subheader. */
10774         shdr = (union lpfc_sli4_cfg_shdr *)
10775                 &mbox->u.mqe.un.sli4_config.header.cfg_shdr;
10776         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10777         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10778         if (rc != MBX_TIMEOUT)
10779                 mempool_free(mbox, phba->mbox_mem_pool);
10780         if (shdr_status || shdr_add_status || rc) {
10781                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10782                                 "2512 REMOVE_ALL_SGL_PAGES mailbox failed with "
10783                                 "status x%x add_status x%x, mbx status x%x\n",
10784                                 shdr_status, shdr_add_status, rc);
10785                 rc = -ENXIO;
10786         }
10787         return rc;
10788 }
10789
10790 /**
10791  * lpfc_sli4_next_xritag - Get an xritag for the io
10792  * @phba: Pointer to HBA context object.
10793  *
10794  * This function gets an xritag for the iocb. If there is no unused xritag
10795  * it will return 0xffff.
10796  * The function returns the allocated xritag if successful, else returns zero.
10797  * Zero is not a valid xritag.
10798  * The caller is not required to hold any lock.
10799  **/
10800 uint16_t
10801 lpfc_sli4_next_xritag(struct lpfc_hba *phba)
10802 {
10803         uint16_t xritag;
10804
10805         spin_lock_irq(&phba->hbalock);
10806         xritag = phba->sli4_hba.next_xri;
10807         if ((xritag != (uint16_t) -1) && xritag <
10808                 (phba->sli4_hba.max_cfg_param.max_xri
10809                         + phba->sli4_hba.max_cfg_param.xri_base)) {
10810                 phba->sli4_hba.next_xri++;
10811                 phba->sli4_hba.max_cfg_param.xri_used++;
10812                 spin_unlock_irq(&phba->hbalock);
10813                 return xritag;
10814         }
10815         spin_unlock_irq(&phba->hbalock);
10816         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
10817                         "2004 Failed to allocate XRI.last XRITAG is %d"
10818                         " Max XRI is %d, Used XRI is %d\n",
10819                         phba->sli4_hba.next_xri,
10820                         phba->sli4_hba.max_cfg_param.max_xri,
10821                         phba->sli4_hba.max_cfg_param.xri_used);
10822         return -1;
10823 }
10824
10825 /**
10826  * lpfc_sli4_post_sgl_list - post a block of sgl list to the firmware.
10827  * @phba: pointer to lpfc hba data structure.
10828  *
10829  * This routine is invoked to post a block of driver's sgl pages to the
10830  * HBA using non-embedded mailbox command. No Lock is held. This routine
10831  * is only called when the driver is loading and after all IO has been
10832  * stopped.
10833  **/
10834 int
10835 lpfc_sli4_post_sgl_list(struct lpfc_hba *phba)
10836 {
10837         struct lpfc_sglq *sglq_entry;
10838         struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
10839         struct sgl_page_pairs *sgl_pg_pairs;
10840         void *viraddr;
10841         LPFC_MBOXQ_t *mbox;
10842         uint32_t reqlen, alloclen, pg_pairs;
10843         uint32_t mbox_tmo;
10844         uint16_t xritag_start = 0;
10845         int els_xri_cnt, rc = 0;
10846         uint32_t shdr_status, shdr_add_status;
10847         union lpfc_sli4_cfg_shdr *shdr;
10848
10849         /* The number of sgls to be posted */
10850         els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
10851
10852         reqlen = els_xri_cnt * sizeof(struct sgl_page_pairs) +
10853                  sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
10854         if (reqlen > SLI4_PAGE_SIZE) {
10855                 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
10856                                 "2559 Block sgl registration required DMA "
10857                                 "size (%d) great than a page\n", reqlen);
10858                 return -ENOMEM;
10859         }
10860         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10861         if (!mbox) {
10862                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10863                                 "2560 Failed to allocate mbox cmd memory\n");
10864                 return -ENOMEM;
10865         }
10866
10867         /* Allocate DMA memory and set up the non-embedded mailbox command */
10868         alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10869                          LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
10870                          LPFC_SLI4_MBX_NEMBED);
10871
10872         if (alloclen < reqlen) {
10873                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10874                                 "0285 Allocated DMA memory size (%d) is "
10875                                 "less than the requested DMA memory "
10876                                 "size (%d)\n", alloclen, reqlen);
10877                 lpfc_sli4_mbox_cmd_free(phba, mbox);
10878                 return -ENOMEM;
10879         }
10880         /* Get the first SGE entry from the non-embedded DMA memory */
10881         viraddr = mbox->sge_array->addr[0];
10882
10883         /* Set up the SGL pages in the non-embedded DMA pages */
10884         sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
10885         sgl_pg_pairs = &sgl->sgl_pg_pairs;
10886
10887         for (pg_pairs = 0; pg_pairs < els_xri_cnt; pg_pairs++) {
10888                 sglq_entry = phba->sli4_hba.lpfc_els_sgl_array[pg_pairs];
10889                 /* Set up the sge entry */
10890                 sgl_pg_pairs->sgl_pg0_addr_lo =
10891                                 cpu_to_le32(putPaddrLow(sglq_entry->phys));
10892                 sgl_pg_pairs->sgl_pg0_addr_hi =
10893                                 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
10894                 sgl_pg_pairs->sgl_pg1_addr_lo =
10895                                 cpu_to_le32(putPaddrLow(0));
10896                 sgl_pg_pairs->sgl_pg1_addr_hi =
10897                                 cpu_to_le32(putPaddrHigh(0));
10898                 /* Keep the first xritag on the list */
10899                 if (pg_pairs == 0)
10900                         xritag_start = sglq_entry->sli4_xritag;
10901                 sgl_pg_pairs++;
10902         }
10903         bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
10904         bf_set(lpfc_post_sgl_pages_xricnt, sgl, els_xri_cnt);
10905         /* Perform endian conversion if necessary */
10906         sgl->word0 = cpu_to_le32(sgl->word0);
10907
10908         if (!phba->sli4_hba.intr_enable)
10909                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10910         else {
10911                 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
10912                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
10913         }
10914         shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
10915         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10916         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10917         if (rc != MBX_TIMEOUT)
10918                 lpfc_sli4_mbox_cmd_free(phba, mbox);
10919         if (shdr_status || shdr_add_status || rc) {
10920                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10921                                 "2513 POST_SGL_BLOCK mailbox command failed "
10922                                 "status x%x add_status x%x mbx status x%x\n",
10923                                 shdr_status, shdr_add_status, rc);
10924                 rc = -ENXIO;
10925         }
10926         return rc;
10927 }
10928
10929 /**
10930  * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
10931  * @phba: pointer to lpfc hba data structure.
10932  * @sblist: pointer to scsi buffer list.
10933  * @count: number of scsi buffers on the list.
10934  *
10935  * This routine is invoked to post a block of @count scsi sgl pages from a
10936  * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
10937  * No Lock is held.
10938  *
10939  **/
10940 int
10941 lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba, struct list_head *sblist,
10942                               int cnt)
10943 {
10944         struct lpfc_scsi_buf *psb;
10945         struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
10946         struct sgl_page_pairs *sgl_pg_pairs;
10947         void *viraddr;
10948         LPFC_MBOXQ_t *mbox;
10949         uint32_t reqlen, alloclen, pg_pairs;
10950         uint32_t mbox_tmo;
10951         uint16_t xritag_start = 0;
10952         int rc = 0;
10953         uint32_t shdr_status, shdr_add_status;
10954         dma_addr_t pdma_phys_bpl1;
10955         union lpfc_sli4_cfg_shdr *shdr;
10956
10957         /* Calculate the requested length of the dma memory */
10958         reqlen = cnt * sizeof(struct sgl_page_pairs) +
10959                  sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
10960         if (reqlen > SLI4_PAGE_SIZE) {
10961                 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
10962                                 "0217 Block sgl registration required DMA "
10963                                 "size (%d) great than a page\n", reqlen);
10964                 return -ENOMEM;
10965         }
10966         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10967         if (!mbox) {
10968                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10969                                 "0283 Failed to allocate mbox cmd memory\n");
10970                 return -ENOMEM;
10971         }
10972
10973         /* Allocate DMA memory and set up the non-embedded mailbox command */
10974         alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10975                                 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
10976                                 LPFC_SLI4_MBX_NEMBED);
10977
10978         if (alloclen < reqlen) {
10979                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10980                                 "2561 Allocated DMA memory size (%d) is "
10981                                 "less than the requested DMA memory "
10982                                 "size (%d)\n", alloclen, reqlen);
10983                 lpfc_sli4_mbox_cmd_free(phba, mbox);
10984                 return -ENOMEM;
10985         }
10986         /* Get the first SGE entry from the non-embedded DMA memory */
10987         viraddr = mbox->sge_array->addr[0];
10988
10989         /* Set up the SGL pages in the non-embedded DMA pages */
10990         sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
10991         sgl_pg_pairs = &sgl->sgl_pg_pairs;
10992
10993         pg_pairs = 0;
10994         list_for_each_entry(psb, sblist, list) {
10995                 /* Set up the sge entry */
10996                 sgl_pg_pairs->sgl_pg0_addr_lo =
10997                         cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
10998                 sgl_pg_pairs->sgl_pg0_addr_hi =
10999                         cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
11000                 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
11001                         pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
11002                 else
11003                         pdma_phys_bpl1 = 0;
11004                 sgl_pg_pairs->sgl_pg1_addr_lo =
11005                         cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
11006                 sgl_pg_pairs->sgl_pg1_addr_hi =
11007                         cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
11008                 /* Keep the first xritag on the list */
11009                 if (pg_pairs == 0)
11010                         xritag_start = psb->cur_iocbq.sli4_xritag;
11011                 sgl_pg_pairs++;
11012                 pg_pairs++;
11013         }
11014         bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
11015         bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
11016         /* Perform endian conversion if necessary */
11017         sgl->word0 = cpu_to_le32(sgl->word0);
11018
11019         if (!phba->sli4_hba.intr_enable)
11020                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
11021         else {
11022                 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
11023                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
11024         }
11025         shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
11026         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11027         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11028         if (rc != MBX_TIMEOUT)
11029                 lpfc_sli4_mbox_cmd_free(phba, mbox);
11030         if (shdr_status || shdr_add_status || rc) {
11031                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11032                                 "2564 POST_SGL_BLOCK mailbox command failed "
11033                                 "status x%x add_status x%x mbx status x%x\n",
11034                                 shdr_status, shdr_add_status, rc);
11035                 rc = -ENXIO;
11036         }
11037         return rc;
11038 }
11039
11040 /**
11041  * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
11042  * @phba: pointer to lpfc_hba struct that the frame was received on
11043  * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
11044  *
11045  * This function checks the fields in the @fc_hdr to see if the FC frame is a
11046  * valid type of frame that the LPFC driver will handle. This function will
11047  * return a zero if the frame is a valid frame or a non zero value when the
11048  * frame does not pass the check.
11049  **/
11050 static int
11051 lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
11052 {
11053         char *rctl_names[] = FC_RCTL_NAMES_INIT;
11054         char *type_names[] = FC_TYPE_NAMES_INIT;
11055         struct fc_vft_header *fc_vft_hdr;
11056
11057         switch (fc_hdr->fh_r_ctl) {
11058         case FC_RCTL_DD_UNCAT:          /* uncategorized information */
11059         case FC_RCTL_DD_SOL_DATA:       /* solicited data */
11060         case FC_RCTL_DD_UNSOL_CTL:      /* unsolicited control */
11061         case FC_RCTL_DD_SOL_CTL:        /* solicited control or reply */
11062         case FC_RCTL_DD_UNSOL_DATA:     /* unsolicited data */
11063         case FC_RCTL_DD_DATA_DESC:      /* data descriptor */
11064         case FC_RCTL_DD_UNSOL_CMD:      /* unsolicited command */
11065         case FC_RCTL_DD_CMD_STATUS:     /* command status */
11066         case FC_RCTL_ELS_REQ:   /* extended link services request */
11067         case FC_RCTL_ELS_REP:   /* extended link services reply */
11068         case FC_RCTL_ELS4_REQ:  /* FC-4 ELS request */
11069         case FC_RCTL_ELS4_REP:  /* FC-4 ELS reply */
11070         case FC_RCTL_BA_NOP:    /* basic link service NOP */
11071         case FC_RCTL_BA_ABTS:   /* basic link service abort */
11072         case FC_RCTL_BA_RMC:    /* remove connection */
11073         case FC_RCTL_BA_ACC:    /* basic accept */
11074         case FC_RCTL_BA_RJT:    /* basic reject */
11075         case FC_RCTL_BA_PRMT:
11076         case FC_RCTL_ACK_1:     /* acknowledge_1 */
11077         case FC_RCTL_ACK_0:     /* acknowledge_0 */
11078         case FC_RCTL_P_RJT:     /* port reject */
11079         case FC_RCTL_F_RJT:     /* fabric reject */
11080         case FC_RCTL_P_BSY:     /* port busy */
11081         case FC_RCTL_F_BSY:     /* fabric busy to data frame */
11082         case FC_RCTL_F_BSYL:    /* fabric busy to link control frame */
11083         case FC_RCTL_LCR:       /* link credit reset */
11084         case FC_RCTL_END:       /* end */
11085                 break;
11086         case FC_RCTL_VFTH:      /* Virtual Fabric tagging Header */
11087                 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
11088                 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
11089                 return lpfc_fc_frame_check(phba, fc_hdr);
11090         default:
11091                 goto drop;
11092         }
11093         switch (fc_hdr->fh_type) {
11094         case FC_TYPE_BLS:
11095         case FC_TYPE_ELS:
11096         case FC_TYPE_FCP:
11097         case FC_TYPE_CT:
11098                 break;
11099         case FC_TYPE_IP:
11100         case FC_TYPE_ILS:
11101         default:
11102                 goto drop;
11103         }
11104         lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
11105                         "2538 Received frame rctl:%s type:%s\n",
11106                         rctl_names[fc_hdr->fh_r_ctl],
11107                         type_names[fc_hdr->fh_type]);
11108         return 0;
11109 drop:
11110         lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
11111                         "2539 Dropped frame rctl:%s type:%s\n",
11112                         rctl_names[fc_hdr->fh_r_ctl],
11113                         type_names[fc_hdr->fh_type]);
11114         return 1;
11115 }
11116
11117 /**
11118  * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
11119  * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
11120  *
11121  * This function processes the FC header to retrieve the VFI from the VF
11122  * header, if one exists. This function will return the VFI if one exists
11123  * or 0 if no VSAN Header exists.
11124  **/
11125 static uint32_t
11126 lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
11127 {
11128         struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
11129
11130         if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
11131                 return 0;
11132         return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
11133 }
11134
11135 /**
11136  * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
11137  * @phba: Pointer to the HBA structure to search for the vport on
11138  * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
11139  * @fcfi: The FC Fabric ID that the frame came from
11140  *
11141  * This function searches the @phba for a vport that matches the content of the
11142  * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
11143  * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
11144  * returns the matching vport pointer or NULL if unable to match frame to a
11145  * vport.
11146  **/
11147 static struct lpfc_vport *
11148 lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
11149                        uint16_t fcfi)
11150 {
11151         struct lpfc_vport **vports;
11152         struct lpfc_vport *vport = NULL;
11153         int i;
11154         uint32_t did = (fc_hdr->fh_d_id[0] << 16 |
11155                         fc_hdr->fh_d_id[1] << 8 |
11156                         fc_hdr->fh_d_id[2]);
11157
11158         vports = lpfc_create_vport_work_array(phba);
11159         if (vports != NULL)
11160                 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
11161                         if (phba->fcf.fcfi == fcfi &&
11162                             vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
11163                             vports[i]->fc_myDID == did) {
11164                                 vport = vports[i];
11165                                 break;
11166                         }
11167                 }
11168         lpfc_destroy_vport_work_array(phba, vports);
11169         return vport;
11170 }
11171
11172 /**
11173  * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
11174  * @vport: The vport to work on.
11175  *
11176  * This function updates the receive sequence time stamp for this vport. The
11177  * receive sequence time stamp indicates the time that the last frame of the
11178  * the sequence that has been idle for the longest amount of time was received.
11179  * the driver uses this time stamp to indicate if any received sequences have
11180  * timed out.
11181  **/
11182 void
11183 lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
11184 {
11185         struct lpfc_dmabuf *h_buf;
11186         struct hbq_dmabuf *dmabuf = NULL;
11187
11188         /* get the oldest sequence on the rcv list */
11189         h_buf = list_get_first(&vport->rcv_buffer_list,
11190                                struct lpfc_dmabuf, list);
11191         if (!h_buf)
11192                 return;
11193         dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11194         vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
11195 }
11196
11197 /**
11198  * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
11199  * @vport: The vport that the received sequences were sent to.
11200  *
11201  * This function cleans up all outstanding received sequences. This is called
11202  * by the driver when a link event or user action invalidates all the received
11203  * sequences.
11204  **/
11205 void
11206 lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
11207 {
11208         struct lpfc_dmabuf *h_buf, *hnext;
11209         struct lpfc_dmabuf *d_buf, *dnext;
11210         struct hbq_dmabuf *dmabuf = NULL;
11211
11212         /* start with the oldest sequence on the rcv list */
11213         list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
11214                 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11215                 list_del_init(&dmabuf->hbuf.list);
11216                 list_for_each_entry_safe(d_buf, dnext,
11217                                          &dmabuf->dbuf.list, list) {
11218                         list_del_init(&d_buf->list);
11219                         lpfc_in_buf_free(vport->phba, d_buf);
11220                 }
11221                 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
11222         }
11223 }
11224
11225 /**
11226  * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
11227  * @vport: The vport that the received sequences were sent to.
11228  *
11229  * This function determines whether any received sequences have timed out by
11230  * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
11231  * indicates that there is at least one timed out sequence this routine will
11232  * go through the received sequences one at a time from most inactive to most
11233  * active to determine which ones need to be cleaned up. Once it has determined
11234  * that a sequence needs to be cleaned up it will simply free up the resources
11235  * without sending an abort.
11236  **/
11237 void
11238 lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
11239 {
11240         struct lpfc_dmabuf *h_buf, *hnext;
11241         struct lpfc_dmabuf *d_buf, *dnext;
11242         struct hbq_dmabuf *dmabuf = NULL;
11243         unsigned long timeout;
11244         int abort_count = 0;
11245
11246         timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
11247                    vport->rcv_buffer_time_stamp);
11248         if (list_empty(&vport->rcv_buffer_list) ||
11249             time_before(jiffies, timeout))
11250                 return;
11251         /* start with the oldest sequence on the rcv list */
11252         list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
11253                 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11254                 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
11255                            dmabuf->time_stamp);
11256                 if (time_before(jiffies, timeout))
11257                         break;
11258                 abort_count++;
11259                 list_del_init(&dmabuf->hbuf.list);
11260                 list_for_each_entry_safe(d_buf, dnext,
11261                                          &dmabuf->dbuf.list, list) {
11262                         list_del_init(&d_buf->list);
11263                         lpfc_in_buf_free(vport->phba, d_buf);
11264                 }
11265                 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
11266         }
11267         if (abort_count)
11268                 lpfc_update_rcv_time_stamp(vport);
11269 }
11270
11271 /**
11272  * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
11273  * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
11274  *
11275  * This function searches through the existing incomplete sequences that have
11276  * been sent to this @vport. If the frame matches one of the incomplete
11277  * sequences then the dbuf in the @dmabuf is added to the list of frames that
11278  * make up that sequence. If no sequence is found that matches this frame then
11279  * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
11280  * This function returns a pointer to the first dmabuf in the sequence list that
11281  * the frame was linked to.
11282  **/
11283 static struct hbq_dmabuf *
11284 lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
11285 {
11286         struct fc_frame_header *new_hdr;
11287         struct fc_frame_header *temp_hdr;
11288         struct lpfc_dmabuf *d_buf;
11289         struct lpfc_dmabuf *h_buf;
11290         struct hbq_dmabuf *seq_dmabuf = NULL;
11291         struct hbq_dmabuf *temp_dmabuf = NULL;
11292
11293         INIT_LIST_HEAD(&dmabuf->dbuf.list);
11294         dmabuf->time_stamp = jiffies;
11295         new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11296         /* Use the hdr_buf to find the sequence that this frame belongs to */
11297         list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
11298                 temp_hdr = (struct fc_frame_header *)h_buf->virt;
11299                 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
11300                     (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
11301                     (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
11302                         continue;
11303                 /* found a pending sequence that matches this frame */
11304                 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11305                 break;
11306         }
11307         if (!seq_dmabuf) {
11308                 /*
11309                  * This indicates first frame received for this sequence.
11310                  * Queue the buffer on the vport's rcv_buffer_list.
11311                  */
11312                 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
11313                 lpfc_update_rcv_time_stamp(vport);
11314                 return dmabuf;
11315         }
11316         temp_hdr = seq_dmabuf->hbuf.virt;
11317         if (be16_to_cpu(new_hdr->fh_seq_cnt) <
11318                 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
11319                 list_del_init(&seq_dmabuf->hbuf.list);
11320                 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
11321                 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
11322                 lpfc_update_rcv_time_stamp(vport);
11323                 return dmabuf;
11324         }
11325         /* move this sequence to the tail to indicate a young sequence */
11326         list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
11327         seq_dmabuf->time_stamp = jiffies;
11328         lpfc_update_rcv_time_stamp(vport);
11329         if (list_empty(&seq_dmabuf->dbuf.list)) {
11330                 temp_hdr = dmabuf->hbuf.virt;
11331                 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
11332                 return seq_dmabuf;
11333         }
11334         /* find the correct place in the sequence to insert this frame */
11335         list_for_each_entry_reverse(d_buf, &seq_dmabuf->dbuf.list, list) {
11336                 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
11337                 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
11338                 /*
11339                  * If the frame's sequence count is greater than the frame on
11340                  * the list then insert the frame right after this frame
11341                  */
11342                 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
11343                         be16_to_cpu(temp_hdr->fh_seq_cnt)) {
11344                         list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
11345                         return seq_dmabuf;
11346                 }
11347         }
11348         return NULL;
11349 }
11350
11351 /**
11352  * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
11353  * @vport: pointer to a vitural port
11354  * @dmabuf: pointer to a dmabuf that describes the FC sequence
11355  *
11356  * This function tries to abort from the partially assembed sequence, described
11357  * by the information from basic abbort @dmabuf. It checks to see whether such
11358  * partially assembled sequence held by the driver. If so, it shall free up all
11359  * the frames from the partially assembled sequence.
11360  *
11361  * Return
11362  * true  -- if there is matching partially assembled sequence present and all
11363  *          the frames freed with the sequence;
11364  * false -- if there is no matching partially assembled sequence present so
11365  *          nothing got aborted in the lower layer driver
11366  **/
11367 static bool
11368 lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
11369                             struct hbq_dmabuf *dmabuf)
11370 {
11371         struct fc_frame_header *new_hdr;
11372         struct fc_frame_header *temp_hdr;
11373         struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
11374         struct hbq_dmabuf *seq_dmabuf = NULL;
11375
11376         /* Use the hdr_buf to find the sequence that matches this frame */
11377         INIT_LIST_HEAD(&dmabuf->dbuf.list);
11378         INIT_LIST_HEAD(&dmabuf->hbuf.list);
11379         new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11380         list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
11381                 temp_hdr = (struct fc_frame_header *)h_buf->virt;
11382                 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
11383                     (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
11384                     (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
11385                         continue;
11386                 /* found a pending sequence that matches this frame */
11387                 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11388                 break;
11389         }
11390
11391         /* Free up all the frames from the partially assembled sequence */
11392         if (seq_dmabuf) {
11393                 list_for_each_entry_safe(d_buf, n_buf,
11394                                          &seq_dmabuf->dbuf.list, list) {
11395                         list_del_init(&d_buf->list);
11396                         lpfc_in_buf_free(vport->phba, d_buf);
11397                 }
11398                 return true;
11399         }
11400         return false;
11401 }
11402
11403 /**
11404  * lpfc_sli4_seq_abort_acc_cmpl - Accept seq abort iocb complete handler
11405  * @phba: Pointer to HBA context object.
11406  * @cmd_iocbq: pointer to the command iocbq structure.
11407  * @rsp_iocbq: pointer to the response iocbq structure.
11408  *
11409  * This function handles the sequence abort accept iocb command complete
11410  * event. It properly releases the memory allocated to the sequence abort
11411  * accept iocb.
11412  **/
11413 static void
11414 lpfc_sli4_seq_abort_acc_cmpl(struct lpfc_hba *phba,
11415                              struct lpfc_iocbq *cmd_iocbq,
11416                              struct lpfc_iocbq *rsp_iocbq)
11417 {
11418         if (cmd_iocbq)
11419                 lpfc_sli_release_iocbq(phba, cmd_iocbq);
11420 }
11421
11422 /**
11423  * lpfc_sli4_seq_abort_acc - Accept sequence abort
11424  * @phba: Pointer to HBA context object.
11425  * @fc_hdr: pointer to a FC frame header.
11426  *
11427  * This function sends a basic accept to a previous unsol sequence abort
11428  * event after aborting the sequence handling.
11429  **/
11430 static void
11431 lpfc_sli4_seq_abort_acc(struct lpfc_hba *phba,
11432                         struct fc_frame_header *fc_hdr)
11433 {
11434         struct lpfc_iocbq *ctiocb = NULL;
11435         struct lpfc_nodelist *ndlp;
11436         uint16_t oxid, rxid;
11437         uint32_t sid, fctl;
11438         IOCB_t *icmd;
11439
11440         if (!lpfc_is_link_up(phba))
11441                 return;
11442
11443         sid = sli4_sid_from_fc_hdr(fc_hdr);
11444         oxid = be16_to_cpu(fc_hdr->fh_ox_id);
11445         rxid = be16_to_cpu(fc_hdr->fh_rx_id);
11446
11447         ndlp = lpfc_findnode_did(phba->pport, sid);
11448         if (!ndlp) {
11449                 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
11450                                 "1268 Find ndlp returned NULL for oxid:x%x "
11451                                 "SID:x%x\n", oxid, sid);
11452                 return;
11453         }
11454
11455         /* Allocate buffer for acc iocb */
11456         ctiocb = lpfc_sli_get_iocbq(phba);
11457         if (!ctiocb)
11458                 return;
11459
11460         /* Extract the F_CTL field from FC_HDR */
11461         fctl = sli4_fctl_from_fc_hdr(fc_hdr);
11462
11463         icmd = &ctiocb->iocb;
11464         icmd->un.xseq64.bdl.bdeSize = 0;
11465         icmd->un.xseq64.bdl.ulpIoTag32 = 0;
11466         icmd->un.xseq64.w5.hcsw.Dfctl = 0;
11467         icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
11468         icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
11469
11470         /* Fill in the rest of iocb fields */
11471         icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
11472         icmd->ulpBdeCount = 0;
11473         icmd->ulpLe = 1;
11474         icmd->ulpClass = CLASS3;
11475         icmd->ulpContext = ndlp->nlp_rpi;
11476
11477         ctiocb->iocb_cmpl = NULL;
11478         ctiocb->vport = phba->pport;
11479         ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_acc_cmpl;
11480
11481         if (fctl & FC_FC_EX_CTX) {
11482                 /* ABTS sent by responder to CT exchange, construction
11483                  * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
11484                  * field and RX_ID from ABTS for RX_ID field.
11485                  */
11486                 bf_set(lpfc_abts_orig, &icmd->un.bls_acc, LPFC_ABTS_UNSOL_RSP);
11487                 bf_set(lpfc_abts_rxid, &icmd->un.bls_acc, rxid);
11488                 ctiocb->sli4_xritag = oxid;
11489         } else {
11490                 /* ABTS sent by initiator to CT exchange, construction
11491                  * of BA_ACC will need to allocate a new XRI as for the
11492                  * XRI_TAG and RX_ID fields.
11493                  */
11494                 bf_set(lpfc_abts_orig, &icmd->un.bls_acc, LPFC_ABTS_UNSOL_INT);
11495                 bf_set(lpfc_abts_rxid, &icmd->un.bls_acc, NO_XRI);
11496                 ctiocb->sli4_xritag = NO_XRI;
11497         }
11498         bf_set(lpfc_abts_oxid, &icmd->un.bls_acc, oxid);
11499
11500         /* Xmit CT abts accept on exchange <xid> */
11501         lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
11502                         "1200 Xmit CT ABTS ACC on exchange x%x Data: x%x\n",
11503                         CMD_XMIT_BLS_RSP64_CX, phba->link_state);
11504         lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
11505 }
11506
11507 /**
11508  * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
11509  * @vport: Pointer to the vport on which this sequence was received
11510  * @dmabuf: pointer to a dmabuf that describes the FC sequence
11511  *
11512  * This function handles an SLI-4 unsolicited abort event. If the unsolicited
11513  * receive sequence is only partially assembed by the driver, it shall abort
11514  * the partially assembled frames for the sequence. Otherwise, if the
11515  * unsolicited receive sequence has been completely assembled and passed to
11516  * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
11517  * unsolicited sequence has been aborted. After that, it will issue a basic
11518  * accept to accept the abort.
11519  **/
11520 void
11521 lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
11522                              struct hbq_dmabuf *dmabuf)
11523 {
11524         struct lpfc_hba *phba = vport->phba;
11525         struct fc_frame_header fc_hdr;
11526         uint32_t fctl;
11527         bool abts_par;
11528
11529         /* Make a copy of fc_hdr before the dmabuf being released */
11530         memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
11531         fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
11532
11533         if (fctl & FC_FC_EX_CTX) {
11534                 /*
11535                  * ABTS sent by responder to exchange, just free the buffer
11536                  */
11537                 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11538         } else {
11539                 /*
11540                  * ABTS sent by initiator to exchange, need to do cleanup
11541                  */
11542                 /* Try to abort partially assembled seq */
11543                 abts_par = lpfc_sli4_abort_partial_seq(vport, dmabuf);
11544
11545                 /* Send abort to ULP if partially seq abort failed */
11546                 if (abts_par == false)
11547                         lpfc_sli4_send_seq_to_ulp(vport, dmabuf);
11548                 else
11549                         lpfc_in_buf_free(phba, &dmabuf->dbuf);
11550         }
11551         /* Send basic accept (BA_ACC) to the abort requester */
11552         lpfc_sli4_seq_abort_acc(phba, &fc_hdr);
11553 }
11554
11555 /**
11556  * lpfc_seq_complete - Indicates if a sequence is complete
11557  * @dmabuf: pointer to a dmabuf that describes the FC sequence
11558  *
11559  * This function checks the sequence, starting with the frame described by
11560  * @dmabuf, to see if all the frames associated with this sequence are present.
11561  * the frames associated with this sequence are linked to the @dmabuf using the
11562  * dbuf list. This function looks for two major things. 1) That the first frame
11563  * has a sequence count of zero. 2) There is a frame with last frame of sequence
11564  * set. 3) That there are no holes in the sequence count. The function will
11565  * return 1 when the sequence is complete, otherwise it will return 0.
11566  **/
11567 static int
11568 lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
11569 {
11570         struct fc_frame_header *hdr;
11571         struct lpfc_dmabuf *d_buf;
11572         struct hbq_dmabuf *seq_dmabuf;
11573         uint32_t fctl;
11574         int seq_count = 0;
11575
11576         hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11577         /* make sure first fame of sequence has a sequence count of zero */
11578         if (hdr->fh_seq_cnt != seq_count)
11579                 return 0;
11580         fctl = (hdr->fh_f_ctl[0] << 16 |
11581                 hdr->fh_f_ctl[1] << 8 |
11582                 hdr->fh_f_ctl[2]);
11583         /* If last frame of sequence we can return success. */
11584         if (fctl & FC_FC_END_SEQ)
11585                 return 1;
11586         list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
11587                 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
11588                 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
11589                 /* If there is a hole in the sequence count then fail. */
11590                 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
11591                         return 0;
11592                 fctl = (hdr->fh_f_ctl[0] << 16 |
11593                         hdr->fh_f_ctl[1] << 8 |
11594                         hdr->fh_f_ctl[2]);
11595                 /* If last frame of sequence we can return success. */
11596                 if (fctl & FC_FC_END_SEQ)
11597                         return 1;
11598         }
11599         return 0;
11600 }
11601
11602 /**
11603  * lpfc_prep_seq - Prep sequence for ULP processing
11604  * @vport: Pointer to the vport on which this sequence was received
11605  * @dmabuf: pointer to a dmabuf that describes the FC sequence
11606  *
11607  * This function takes a sequence, described by a list of frames, and creates
11608  * a list of iocbq structures to describe the sequence. This iocbq list will be
11609  * used to issue to the generic unsolicited sequence handler. This routine
11610  * returns a pointer to the first iocbq in the list. If the function is unable
11611  * to allocate an iocbq then it throw out the received frames that were not
11612  * able to be described and return a pointer to the first iocbq. If unable to
11613  * allocate any iocbqs (including the first) this function will return NULL.
11614  **/
11615 static struct lpfc_iocbq *
11616 lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
11617 {
11618         struct lpfc_dmabuf *d_buf, *n_buf;
11619         struct lpfc_iocbq *first_iocbq, *iocbq;
11620         struct fc_frame_header *fc_hdr;
11621         uint32_t sid;
11622         struct ulp_bde64 *pbde;
11623
11624         fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
11625         /* remove from receive buffer list */
11626         list_del_init(&seq_dmabuf->hbuf.list);
11627         lpfc_update_rcv_time_stamp(vport);
11628         /* get the Remote Port's SID */
11629         sid = sli4_sid_from_fc_hdr(fc_hdr);
11630         /* Get an iocbq struct to fill in. */
11631         first_iocbq = lpfc_sli_get_iocbq(vport->phba);
11632         if (first_iocbq) {
11633                 /* Initialize the first IOCB. */
11634                 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
11635                 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
11636                 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
11637                 first_iocbq->iocb.ulpContext = be16_to_cpu(fc_hdr->fh_ox_id);
11638                 first_iocbq->iocb.unsli3.rcvsli3.vpi =
11639                                         vport->vpi + vport->phba->vpi_base;
11640                 /* put the first buffer into the first IOCBq */
11641                 first_iocbq->context2 = &seq_dmabuf->dbuf;
11642                 first_iocbq->context3 = NULL;
11643                 first_iocbq->iocb.ulpBdeCount = 1;
11644                 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
11645                                                         LPFC_DATA_BUF_SIZE;
11646                 first_iocbq->iocb.un.rcvels.remoteID = sid;
11647                 first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
11648                                 bf_get(lpfc_rcqe_length,
11649                                        &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
11650         }
11651         iocbq = first_iocbq;
11652         /*
11653          * Each IOCBq can have two Buffers assigned, so go through the list
11654          * of buffers for this sequence and save two buffers in each IOCBq
11655          */
11656         list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
11657                 if (!iocbq) {
11658                         lpfc_in_buf_free(vport->phba, d_buf);
11659                         continue;
11660                 }
11661                 if (!iocbq->context3) {
11662                         iocbq->context3 = d_buf;
11663                         iocbq->iocb.ulpBdeCount++;
11664                         pbde = (struct ulp_bde64 *)
11665                                         &iocbq->iocb.unsli3.sli3Words[4];
11666                         pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
11667                         first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
11668                                 bf_get(lpfc_rcqe_length,
11669                                        &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
11670                 } else {
11671                         iocbq = lpfc_sli_get_iocbq(vport->phba);
11672                         if (!iocbq) {
11673                                 if (first_iocbq) {
11674                                         first_iocbq->iocb.ulpStatus =
11675                                                         IOSTAT_FCP_RSP_ERROR;
11676                                         first_iocbq->iocb.un.ulpWord[4] =
11677                                                         IOERR_NO_RESOURCES;
11678                                 }
11679                                 lpfc_in_buf_free(vport->phba, d_buf);
11680                                 continue;
11681                         }
11682                         iocbq->context2 = d_buf;
11683                         iocbq->context3 = NULL;
11684                         iocbq->iocb.ulpBdeCount = 1;
11685                         iocbq->iocb.un.cont64[0].tus.f.bdeSize =
11686                                                         LPFC_DATA_BUF_SIZE;
11687                         first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
11688                                 bf_get(lpfc_rcqe_length,
11689                                        &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
11690                         iocbq->iocb.un.rcvels.remoteID = sid;
11691                         list_add_tail(&iocbq->list, &first_iocbq->list);
11692                 }
11693         }
11694         return first_iocbq;
11695 }
11696
11697 static void
11698 lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
11699                           struct hbq_dmabuf *seq_dmabuf)
11700 {
11701         struct fc_frame_header *fc_hdr;
11702         struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
11703         struct lpfc_hba *phba = vport->phba;
11704
11705         fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
11706         iocbq = lpfc_prep_seq(vport, seq_dmabuf);
11707         if (!iocbq) {
11708                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11709                                 "2707 Ring %d handler: Failed to allocate "
11710                                 "iocb Rctl x%x Type x%x received\n",
11711                                 LPFC_ELS_RING,
11712                                 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
11713                 return;
11714         }
11715         if (!lpfc_complete_unsol_iocb(phba,
11716                                       &phba->sli.ring[LPFC_ELS_RING],
11717                                       iocbq, fc_hdr->fh_r_ctl,
11718                                       fc_hdr->fh_type))
11719                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11720                                 "2540 Ring %d handler: unexpected Rctl "
11721                                 "x%x Type x%x received\n",
11722                                 LPFC_ELS_RING,
11723                                 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
11724
11725         /* Free iocb created in lpfc_prep_seq */
11726         list_for_each_entry_safe(curr_iocb, next_iocb,
11727                 &iocbq->list, list) {
11728                 list_del_init(&curr_iocb->list);
11729                 lpfc_sli_release_iocbq(phba, curr_iocb);
11730         }
11731         lpfc_sli_release_iocbq(phba, iocbq);
11732 }
11733
11734 /**
11735  * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
11736  * @phba: Pointer to HBA context object.
11737  *
11738  * This function is called with no lock held. This function processes all
11739  * the received buffers and gives it to upper layers when a received buffer
11740  * indicates that it is the final frame in the sequence. The interrupt
11741  * service routine processes received buffers at interrupt contexts and adds
11742  * received dma buffers to the rb_pend_list queue and signals the worker thread.
11743  * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
11744  * appropriate receive function when the final frame in a sequence is received.
11745  **/
11746 void
11747 lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
11748                                  struct hbq_dmabuf *dmabuf)
11749 {
11750         struct hbq_dmabuf *seq_dmabuf;
11751         struct fc_frame_header *fc_hdr;
11752         struct lpfc_vport *vport;
11753         uint32_t fcfi;
11754
11755         /* Process each received buffer */
11756         fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11757         /* check to see if this a valid type of frame */
11758         if (lpfc_fc_frame_check(phba, fc_hdr)) {
11759                 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11760                 return;
11761         }
11762         fcfi = bf_get(lpfc_rcqe_fcf_id, &dmabuf->cq_event.cqe.rcqe_cmpl);
11763         vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi);
11764         if (!vport || !(vport->vpi_state & LPFC_VPI_REGISTERED)) {
11765                 /* throw out the frame */
11766                 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11767                 return;
11768         }
11769         /* Handle the basic abort sequence (BA_ABTS) event */
11770         if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
11771                 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
11772                 return;
11773         }
11774
11775         /* Link this frame */
11776         seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
11777         if (!seq_dmabuf) {
11778                 /* unable to add frame to vport - throw it out */
11779                 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11780                 return;
11781         }
11782         /* If not last frame in sequence continue processing frames. */
11783         if (!lpfc_seq_complete(seq_dmabuf))
11784                 return;
11785
11786         /* Send the complete sequence to the upper layer protocol */
11787         lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
11788 }
11789
11790 /**
11791  * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
11792  * @phba: pointer to lpfc hba data structure.
11793  *
11794  * This routine is invoked to post rpi header templates to the
11795  * HBA consistent with the SLI-4 interface spec.  This routine
11796  * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
11797  * SLI4_PAGE_SIZE modulo 64 rpi context headers.
11798  *
11799  * This routine does not require any locks.  It's usage is expected
11800  * to be driver load or reset recovery when the driver is
11801  * sequential.
11802  *
11803  * Return codes
11804  *      0 - successful
11805  *      EIO - The mailbox failed to complete successfully.
11806  *      When this error occurs, the driver is not guaranteed
11807  *      to have any rpi regions posted to the device and
11808  *      must either attempt to repost the regions or take a
11809  *      fatal error.
11810  **/
11811 int
11812 lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
11813 {
11814         struct lpfc_rpi_hdr *rpi_page;
11815         uint32_t rc = 0;
11816
11817         /* Post all rpi memory regions to the port. */
11818         list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
11819                 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
11820                 if (rc != MBX_SUCCESS) {
11821                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11822                                         "2008 Error %d posting all rpi "
11823                                         "headers\n", rc);
11824                         rc = -EIO;
11825                         break;
11826                 }
11827         }
11828
11829         return rc;
11830 }
11831
11832 /**
11833  * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
11834  * @phba: pointer to lpfc hba data structure.
11835  * @rpi_page:  pointer to the rpi memory region.
11836  *
11837  * This routine is invoked to post a single rpi header to the
11838  * HBA consistent with the SLI-4 interface spec.  This memory region
11839  * maps up to 64 rpi context regions.
11840  *
11841  * Return codes
11842  *      0 - successful
11843  *      ENOMEM - No available memory
11844  *      EIO - The mailbox failed to complete successfully.
11845  **/
11846 int
11847 lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
11848 {
11849         LPFC_MBOXQ_t *mboxq;
11850         struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
11851         uint32_t rc = 0;
11852         uint32_t mbox_tmo;
11853         uint32_t shdr_status, shdr_add_status;
11854         union lpfc_sli4_cfg_shdr *shdr;
11855
11856         /* The port is notified of the header region via a mailbox command. */
11857         mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11858         if (!mboxq) {
11859                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11860                                 "2001 Unable to allocate memory for issuing "
11861                                 "SLI_CONFIG_SPECIAL mailbox command\n");
11862                 return -ENOMEM;
11863         }
11864
11865         /* Post all rpi memory regions to the port. */
11866         hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
11867         mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
11868         lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
11869                          LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
11870                          sizeof(struct lpfc_mbx_post_hdr_tmpl) -
11871                          sizeof(struct mbox_header), LPFC_SLI4_MBX_EMBED);
11872         bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
11873                hdr_tmpl, rpi_page->page_count);
11874         bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
11875                rpi_page->start_rpi);
11876         hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
11877         hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
11878         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
11879         shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
11880         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11881         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11882         if (rc != MBX_TIMEOUT)
11883                 mempool_free(mboxq, phba->mbox_mem_pool);
11884         if (shdr_status || shdr_add_status || rc) {
11885                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11886                                 "2514 POST_RPI_HDR mailbox failed with "
11887                                 "status x%x add_status x%x, mbx status x%x\n",
11888                                 shdr_status, shdr_add_status, rc);
11889                 rc = -ENXIO;
11890         }
11891         return rc;
11892 }
11893
11894 /**
11895  * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
11896  * @phba: pointer to lpfc hba data structure.
11897  *
11898  * This routine is invoked to post rpi header templates to the
11899  * HBA consistent with the SLI-4 interface spec.  This routine
11900  * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
11901  * SLI4_PAGE_SIZE modulo 64 rpi context headers.
11902  *
11903  * Returns
11904  *      A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
11905  *      LPFC_RPI_ALLOC_ERROR if no rpis are available.
11906  **/
11907 int
11908 lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
11909 {
11910         int rpi;
11911         uint16_t max_rpi, rpi_base, rpi_limit;
11912         uint16_t rpi_remaining;
11913         struct lpfc_rpi_hdr *rpi_hdr;
11914
11915         max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
11916         rpi_base = phba->sli4_hba.max_cfg_param.rpi_base;
11917         rpi_limit = phba->sli4_hba.next_rpi;
11918
11919         /*
11920          * The valid rpi range is not guaranteed to be zero-based.  Start
11921          * the search at the rpi_base as reported by the port.
11922          */
11923         spin_lock_irq(&phba->hbalock);
11924         rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, rpi_base);
11925         if (rpi >= rpi_limit || rpi < rpi_base)
11926                 rpi = LPFC_RPI_ALLOC_ERROR;
11927         else {
11928                 set_bit(rpi, phba->sli4_hba.rpi_bmask);
11929                 phba->sli4_hba.max_cfg_param.rpi_used++;
11930                 phba->sli4_hba.rpi_count++;
11931         }
11932
11933         /*
11934          * Don't try to allocate more rpi header regions if the device limit
11935          * on available rpis max has been exhausted.
11936          */
11937         if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
11938             (phba->sli4_hba.rpi_count >= max_rpi)) {
11939                 spin_unlock_irq(&phba->hbalock);
11940                 return rpi;
11941         }
11942
11943         /*
11944          * If the driver is running low on rpi resources, allocate another
11945          * page now.  Note that the next_rpi value is used because
11946          * it represents how many are actually in use whereas max_rpi notes
11947          * how many are supported max by the device.
11948          */
11949         rpi_remaining = phba->sli4_hba.next_rpi - rpi_base -
11950                 phba->sli4_hba.rpi_count;
11951         spin_unlock_irq(&phba->hbalock);
11952         if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
11953                 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
11954                 if (!rpi_hdr) {
11955                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11956                                         "2002 Error Could not grow rpi "
11957                                         "count\n");
11958                 } else {
11959                         lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
11960                 }
11961         }
11962
11963         return rpi;
11964 }
11965
11966 /**
11967  * lpfc_sli4_free_rpi - Release an rpi for reuse.
11968  * @phba: pointer to lpfc hba data structure.
11969  *
11970  * This routine is invoked to release an rpi to the pool of
11971  * available rpis maintained by the driver.
11972  **/
11973 void
11974 __lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
11975 {
11976         if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
11977                 phba->sli4_hba.rpi_count--;
11978                 phba->sli4_hba.max_cfg_param.rpi_used--;
11979         }
11980 }
11981
11982 /**
11983  * lpfc_sli4_free_rpi - Release an rpi for reuse.
11984  * @phba: pointer to lpfc hba data structure.
11985  *
11986  * This routine is invoked to release an rpi to the pool of
11987  * available rpis maintained by the driver.
11988  **/
11989 void
11990 lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
11991 {
11992         spin_lock_irq(&phba->hbalock);
11993         __lpfc_sli4_free_rpi(phba, rpi);
11994         spin_unlock_irq(&phba->hbalock);
11995 }
11996
11997 /**
11998  * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
11999  * @phba: pointer to lpfc hba data structure.
12000  *
12001  * This routine is invoked to remove the memory region that
12002  * provided rpi via a bitmask.
12003  **/
12004 void
12005 lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
12006 {
12007         kfree(phba->sli4_hba.rpi_bmask);
12008 }
12009
12010 /**
12011  * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
12012  * @phba: pointer to lpfc hba data structure.
12013  *
12014  * This routine is invoked to remove the memory region that
12015  * provided rpi via a bitmask.
12016  **/
12017 int
12018 lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp)
12019 {
12020         LPFC_MBOXQ_t *mboxq;
12021         struct lpfc_hba *phba = ndlp->phba;
12022         int rc;
12023
12024         /* The port is notified of the header region via a mailbox command. */
12025         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12026         if (!mboxq)
12027                 return -ENOMEM;
12028
12029         /* Post all rpi memory regions to the port. */
12030         lpfc_resume_rpi(mboxq, ndlp);
12031         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
12032         if (rc == MBX_NOT_FINISHED) {
12033                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12034                                 "2010 Resume RPI Mailbox failed "
12035                                 "status %d, mbxStatus x%x\n", rc,
12036                                 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
12037                 mempool_free(mboxq, phba->mbox_mem_pool);
12038                 return -EIO;
12039         }
12040         return 0;
12041 }
12042
12043 /**
12044  * lpfc_sli4_init_vpi - Initialize a vpi with the port
12045  * @phba: pointer to lpfc hba data structure.
12046  * @vpi: vpi value to activate with the port.
12047  *
12048  * This routine is invoked to activate a vpi with the
12049  * port when the host intends to use vports with a
12050  * nonzero vpi.
12051  *
12052  * Returns:
12053  *    0 success
12054  *    -Evalue otherwise
12055  **/
12056 int
12057 lpfc_sli4_init_vpi(struct lpfc_hba *phba, uint16_t vpi)
12058 {
12059         LPFC_MBOXQ_t *mboxq;
12060         int rc = 0;
12061         int retval = MBX_SUCCESS;
12062         uint32_t mbox_tmo;
12063
12064         if (vpi == 0)
12065                 return -EINVAL;
12066         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12067         if (!mboxq)
12068                 return -ENOMEM;
12069         lpfc_init_vpi(phba, mboxq, vpi);
12070         mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_INIT_VPI);
12071         rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
12072         if (rc != MBX_SUCCESS) {
12073                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12074                                 "2022 INIT VPI Mailbox failed "
12075                                 "status %d, mbxStatus x%x\n", rc,
12076                                 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
12077                 retval = -EIO;
12078         }
12079         if (rc != MBX_TIMEOUT)
12080                 mempool_free(mboxq, phba->mbox_mem_pool);
12081
12082         return retval;
12083 }
12084
12085 /**
12086  * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
12087  * @phba: pointer to lpfc hba data structure.
12088  * @mboxq: Pointer to mailbox object.
12089  *
12090  * This routine is invoked to manually add a single FCF record. The caller
12091  * must pass a completely initialized FCF_Record.  This routine takes
12092  * care of the nonembedded mailbox operations.
12093  **/
12094 static void
12095 lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
12096 {
12097         void *virt_addr;
12098         union lpfc_sli4_cfg_shdr *shdr;
12099         uint32_t shdr_status, shdr_add_status;
12100
12101         virt_addr = mboxq->sge_array->addr[0];
12102         /* The IOCTL status is embedded in the mailbox subheader. */
12103         shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
12104         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12105         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12106
12107         if ((shdr_status || shdr_add_status) &&
12108                 (shdr_status != STATUS_FCF_IN_USE))
12109                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12110                         "2558 ADD_FCF_RECORD mailbox failed with "
12111                         "status x%x add_status x%x\n",
12112                         shdr_status, shdr_add_status);
12113
12114         lpfc_sli4_mbox_cmd_free(phba, mboxq);
12115 }
12116
12117 /**
12118  * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
12119  * @phba: pointer to lpfc hba data structure.
12120  * @fcf_record:  pointer to the initialized fcf record to add.
12121  *
12122  * This routine is invoked to manually add a single FCF record. The caller
12123  * must pass a completely initialized FCF_Record.  This routine takes
12124  * care of the nonembedded mailbox operations.
12125  **/
12126 int
12127 lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
12128 {
12129         int rc = 0;
12130         LPFC_MBOXQ_t *mboxq;
12131         uint8_t *bytep;
12132         void *virt_addr;
12133         dma_addr_t phys_addr;
12134         struct lpfc_mbx_sge sge;
12135         uint32_t alloc_len, req_len;
12136         uint32_t fcfindex;
12137
12138         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12139         if (!mboxq) {
12140                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12141                         "2009 Failed to allocate mbox for ADD_FCF cmd\n");
12142                 return -ENOMEM;
12143         }
12144
12145         req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
12146                   sizeof(uint32_t);
12147
12148         /* Allocate DMA memory and set up the non-embedded mailbox command */
12149         alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
12150                                      LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
12151                                      req_len, LPFC_SLI4_MBX_NEMBED);
12152         if (alloc_len < req_len) {
12153                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12154                         "2523 Allocated DMA memory size (x%x) is "
12155                         "less than the requested DMA memory "
12156                         "size (x%x)\n", alloc_len, req_len);
12157                 lpfc_sli4_mbox_cmd_free(phba, mboxq);
12158                 return -ENOMEM;
12159         }
12160
12161         /*
12162          * Get the first SGE entry from the non-embedded DMA memory.  This
12163          * routine only uses a single SGE.
12164          */
12165         lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
12166         phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
12167         virt_addr = mboxq->sge_array->addr[0];
12168         /*
12169          * Configure the FCF record for FCFI 0.  This is the driver's
12170          * hardcoded default and gets used in nonFIP mode.
12171          */
12172         fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
12173         bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
12174         lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
12175
12176         /*
12177          * Copy the fcf_index and the FCF Record Data. The data starts after
12178          * the FCoE header plus word10. The data copy needs to be endian
12179          * correct.
12180          */
12181         bytep += sizeof(uint32_t);
12182         lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
12183         mboxq->vport = phba->pport;
12184         mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
12185         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
12186         if (rc == MBX_NOT_FINISHED) {
12187                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12188                         "2515 ADD_FCF_RECORD mailbox failed with "
12189                         "status 0x%x\n", rc);
12190                 lpfc_sli4_mbox_cmd_free(phba, mboxq);
12191                 rc = -EIO;
12192         } else
12193                 rc = 0;
12194
12195         return rc;
12196 }
12197
12198 /**
12199  * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
12200  * @phba: pointer to lpfc hba data structure.
12201  * @fcf_record:  pointer to the fcf record to write the default data.
12202  * @fcf_index: FCF table entry index.
12203  *
12204  * This routine is invoked to build the driver's default FCF record.  The
12205  * values used are hardcoded.  This routine handles memory initialization.
12206  *
12207  **/
12208 void
12209 lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
12210                                 struct fcf_record *fcf_record,
12211                                 uint16_t fcf_index)
12212 {
12213         memset(fcf_record, 0, sizeof(struct fcf_record));
12214         fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
12215         fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
12216         fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
12217         bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
12218         bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
12219         bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
12220         bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
12221         bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
12222         bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
12223         bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
12224         bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
12225         bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
12226         bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
12227         bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
12228         bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
12229         bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
12230                 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
12231         /* Set the VLAN bit map */
12232         if (phba->valid_vlan) {
12233                 fcf_record->vlan_bitmap[phba->vlan_id / 8]
12234                         = 1 << (phba->vlan_id % 8);
12235         }
12236 }
12237
12238 /**
12239  * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
12240  * @phba: pointer to lpfc hba data structure.
12241  * @fcf_index: FCF table entry offset.
12242  *
12243  * This routine is invoked to scan the entire FCF table by reading FCF
12244  * record and processing it one at a time starting from the @fcf_index
12245  * for initial FCF discovery or fast FCF failover rediscovery.
12246  *
12247  * Return 0 if the mailbox command is submitted sucessfully, none 0
12248  * otherwise.
12249  **/
12250 int
12251 lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
12252 {
12253         int rc = 0, error;
12254         LPFC_MBOXQ_t *mboxq;
12255
12256         phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
12257         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12258         if (!mboxq) {
12259                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12260                                 "2000 Failed to allocate mbox for "
12261                                 "READ_FCF cmd\n");
12262                 error = -ENOMEM;
12263                 goto fail_fcf_scan;
12264         }
12265         /* Construct the read FCF record mailbox command */
12266         rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
12267         if (rc) {
12268                 error = -EINVAL;
12269                 goto fail_fcf_scan;
12270         }
12271         /* Issue the mailbox command asynchronously */
12272         mboxq->vport = phba->pport;
12273         mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
12274         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
12275         if (rc == MBX_NOT_FINISHED)
12276                 error = -EIO;
12277         else {
12278                 spin_lock_irq(&phba->hbalock);
12279                 phba->hba_flag |= FCF_DISC_INPROGRESS;
12280                 spin_unlock_irq(&phba->hbalock);
12281                 /* Reset FCF round robin index bmask for new scan */
12282                 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST) {
12283                         memset(phba->fcf.fcf_rr_bmask, 0,
12284                                sizeof(*phba->fcf.fcf_rr_bmask));
12285                         phba->fcf.eligible_fcf_cnt = 0;
12286                 }
12287                 error = 0;
12288         }
12289 fail_fcf_scan:
12290         if (error) {
12291                 if (mboxq)
12292                         lpfc_sli4_mbox_cmd_free(phba, mboxq);
12293                 /* FCF scan failed, clear FCF_DISC_INPROGRESS flag */
12294                 spin_lock_irq(&phba->hbalock);
12295                 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
12296                 spin_unlock_irq(&phba->hbalock);
12297         }
12298         return error;
12299 }
12300
12301 /**
12302  * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for round robin fcf.
12303  * @phba: pointer to lpfc hba data structure.
12304  * @fcf_index: FCF table entry offset.
12305  *
12306  * This routine is invoked to read an FCF record indicated by @fcf_index
12307  * and to use it for FLOGI round robin FCF failover.
12308  *
12309  * Return 0 if the mailbox command is submitted sucessfully, none 0
12310  * otherwise.
12311  **/
12312 int
12313 lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
12314 {
12315         int rc = 0, error;
12316         LPFC_MBOXQ_t *mboxq;
12317
12318         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12319         if (!mboxq) {
12320                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
12321                                 "2763 Failed to allocate mbox for "
12322                                 "READ_FCF cmd\n");
12323                 error = -ENOMEM;
12324                 goto fail_fcf_read;
12325         }
12326         /* Construct the read FCF record mailbox command */
12327         rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
12328         if (rc) {
12329                 error = -EINVAL;
12330                 goto fail_fcf_read;
12331         }
12332         /* Issue the mailbox command asynchronously */
12333         mboxq->vport = phba->pport;
12334         mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
12335         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
12336         if (rc == MBX_NOT_FINISHED)
12337                 error = -EIO;
12338         else
12339                 error = 0;
12340
12341 fail_fcf_read:
12342         if (error && mboxq)
12343                 lpfc_sli4_mbox_cmd_free(phba, mboxq);
12344         return error;
12345 }
12346
12347 /**
12348  * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
12349  * @phba: pointer to lpfc hba data structure.
12350  * @fcf_index: FCF table entry offset.
12351  *
12352  * This routine is invoked to read an FCF record indicated by @fcf_index to
12353  * determine whether it's eligible for FLOGI round robin failover list.
12354  *
12355  * Return 0 if the mailbox command is submitted sucessfully, none 0
12356  * otherwise.
12357  **/
12358 int
12359 lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
12360 {
12361         int rc = 0, error;
12362         LPFC_MBOXQ_t *mboxq;
12363
12364         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12365         if (!mboxq) {
12366                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
12367                                 "2758 Failed to allocate mbox for "
12368                                 "READ_FCF cmd\n");
12369                                 error = -ENOMEM;
12370                                 goto fail_fcf_read;
12371         }
12372         /* Construct the read FCF record mailbox command */
12373         rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
12374         if (rc) {
12375                 error = -EINVAL;
12376                 goto fail_fcf_read;
12377         }
12378         /* Issue the mailbox command asynchronously */
12379         mboxq->vport = phba->pport;
12380         mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
12381         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
12382         if (rc == MBX_NOT_FINISHED)
12383                 error = -EIO;
12384         else
12385                 error = 0;
12386
12387 fail_fcf_read:
12388         if (error && mboxq)
12389                 lpfc_sli4_mbox_cmd_free(phba, mboxq);
12390         return error;
12391 }
12392
12393 /**
12394  * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
12395  * @phba: pointer to lpfc hba data structure.
12396  *
12397  * This routine is to get the next eligible FCF record index in a round
12398  * robin fashion. If the next eligible FCF record index equals to the
12399  * initial round robin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
12400  * shall be returned, otherwise, the next eligible FCF record's index
12401  * shall be returned.
12402  **/
12403 uint16_t
12404 lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
12405 {
12406         uint16_t next_fcf_index;
12407
12408         /* Search start from next bit of currently registered FCF index */
12409         next_fcf_index = (phba->fcf.current_rec.fcf_indx + 1) %
12410                                         LPFC_SLI4_FCF_TBL_INDX_MAX;
12411         next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
12412                                        LPFC_SLI4_FCF_TBL_INDX_MAX,
12413                                        next_fcf_index);
12414
12415         /* Wrap around condition on phba->fcf.fcf_rr_bmask */
12416         if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
12417                 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
12418                                                LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
12419
12420         /* Check roundrobin failover list empty condition */
12421         if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
12422                 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
12423                                 "2844 No roundrobin failover FCF available\n");
12424                 return LPFC_FCOE_FCF_NEXT_NONE;
12425         }
12426
12427         /* Check roundrobin failover index bmask stop condition */
12428         if (next_fcf_index == phba->fcf.fcf_rr_init_indx) {
12429                 if (!(phba->fcf.fcf_flag & FCF_REDISC_RRU)) {
12430                         lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
12431                                         "2847 Round robin failover FCF index "
12432                                         "search hit stop condition:x%x\n",
12433                                         next_fcf_index);
12434                         return LPFC_FCOE_FCF_NEXT_NONE;
12435                 }
12436                 /* The roundrobin failover index bmask updated, start over */
12437                 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
12438                                 "2848 Round robin failover FCF index bmask "
12439                                 "updated, start over\n");
12440                 spin_lock_irq(&phba->hbalock);
12441                 phba->fcf.fcf_flag &= ~FCF_REDISC_RRU;
12442                 spin_unlock_irq(&phba->hbalock);
12443                 return phba->fcf.fcf_rr_init_indx;
12444         }
12445
12446         lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
12447                         "2845 Get next round robin failover "
12448                         "FCF index x%x\n", next_fcf_index);
12449         return next_fcf_index;
12450 }
12451
12452 /**
12453  * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
12454  * @phba: pointer to lpfc hba data structure.
12455  *
12456  * This routine sets the FCF record index in to the eligible bmask for
12457  * round robin failover search. It checks to make sure that the index
12458  * does not go beyond the range of the driver allocated bmask dimension
12459  * before setting the bit.
12460  *
12461  * Returns 0 if the index bit successfully set, otherwise, it returns
12462  * -EINVAL.
12463  **/
12464 int
12465 lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
12466 {
12467         if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
12468                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
12469                                 "2610 HBA FCF index reached driver's "
12470                                 "book keeping dimension: fcf_index:%d, "
12471                                 "driver_bmask_max:%d\n",
12472                                 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
12473                 return -EINVAL;
12474         }
12475         /* Set the eligible FCF record index bmask */
12476         set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
12477
12478         /* Set the roundrobin index bmask updated */
12479         spin_lock_irq(&phba->hbalock);
12480         phba->fcf.fcf_flag |= FCF_REDISC_RRU;
12481         spin_unlock_irq(&phba->hbalock);
12482
12483         lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
12484                         "2790 Set FCF index x%x to round robin failover "
12485                         "bmask\n", fcf_index);
12486
12487         return 0;
12488 }
12489
12490 /**
12491  * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
12492  * @phba: pointer to lpfc hba data structure.
12493  *
12494  * This routine clears the FCF record index from the eligible bmask for
12495  * round robin failover search. It checks to make sure that the index
12496  * does not go beyond the range of the driver allocated bmask dimension
12497  * before clearing the bit.
12498  **/
12499 void
12500 lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
12501 {
12502         if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
12503                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
12504                                 "2762 HBA FCF index goes beyond driver's "
12505                                 "book keeping dimension: fcf_index:%d, "
12506                                 "driver_bmask_max:%d\n",
12507                                 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
12508                 return;
12509         }
12510         /* Clear the eligible FCF record index bmask */
12511         clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
12512
12513         lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
12514                         "2791 Clear FCF index x%x from round robin failover "
12515                         "bmask\n", fcf_index);
12516 }
12517
12518 /**
12519  * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
12520  * @phba: pointer to lpfc hba data structure.
12521  *
12522  * This routine is the completion routine for the rediscover FCF table mailbox
12523  * command. If the mailbox command returned failure, it will try to stop the
12524  * FCF rediscover wait timer.
12525  **/
12526 void
12527 lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
12528 {
12529         struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
12530         uint32_t shdr_status, shdr_add_status;
12531
12532         redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
12533
12534         shdr_status = bf_get(lpfc_mbox_hdr_status,
12535                              &redisc_fcf->header.cfg_shdr.response);
12536         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
12537                              &redisc_fcf->header.cfg_shdr.response);
12538         if (shdr_status || shdr_add_status) {
12539                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
12540                                 "2746 Requesting for FCF rediscovery failed "
12541                                 "status x%x add_status x%x\n",
12542                                 shdr_status, shdr_add_status);
12543                 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
12544                         spin_lock_irq(&phba->hbalock);
12545                         phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
12546                         spin_unlock_irq(&phba->hbalock);
12547                         /*
12548                          * CVL event triggered FCF rediscover request failed,
12549                          * last resort to re-try current registered FCF entry.
12550                          */
12551                         lpfc_retry_pport_discovery(phba);
12552                 } else {
12553                         spin_lock_irq(&phba->hbalock);
12554                         phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
12555                         spin_unlock_irq(&phba->hbalock);
12556                         /*
12557                          * DEAD FCF event triggered FCF rediscover request
12558                          * failed, last resort to fail over as a link down
12559                          * to FCF registration.
12560                          */
12561                         lpfc_sli4_fcf_dead_failthrough(phba);
12562                 }
12563         } else {
12564                 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
12565                                 "2775 Start FCF rediscovery quiescent period "
12566                                 "wait timer before scaning FCF table\n");
12567                 /*
12568                  * Start FCF rediscovery wait timer for pending FCF
12569                  * before rescan FCF record table.
12570                  */
12571                 lpfc_fcf_redisc_wait_start_timer(phba);
12572         }
12573
12574         mempool_free(mbox, phba->mbox_mem_pool);
12575 }
12576
12577 /**
12578  * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
12579  * @phba: pointer to lpfc hba data structure.
12580  *
12581  * This routine is invoked to request for rediscovery of the entire FCF table
12582  * by the port.
12583  **/
12584 int
12585 lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
12586 {
12587         LPFC_MBOXQ_t *mbox;
12588         struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
12589         int rc, length;
12590
12591         /* Cancel retry delay timers to all vports before FCF rediscover */
12592         lpfc_cancel_all_vport_retry_delay_timer(phba);
12593
12594         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12595         if (!mbox) {
12596                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12597                                 "2745 Failed to allocate mbox for "
12598                                 "requesting FCF rediscover.\n");
12599                 return -ENOMEM;
12600         }
12601
12602         length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
12603                   sizeof(struct lpfc_sli4_cfg_mhdr));
12604         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12605                          LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
12606                          length, LPFC_SLI4_MBX_EMBED);
12607
12608         redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
12609         /* Set count to 0 for invalidating the entire FCF database */
12610         bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
12611
12612         /* Issue the mailbox command asynchronously */
12613         mbox->vport = phba->pport;
12614         mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
12615         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
12616
12617         if (rc == MBX_NOT_FINISHED) {
12618                 mempool_free(mbox, phba->mbox_mem_pool);
12619                 return -EIO;
12620         }
12621         return 0;
12622 }
12623
12624 /**
12625  * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
12626  * @phba: pointer to lpfc hba data structure.
12627  *
12628  * This function is the failover routine as a last resort to the FCF DEAD
12629  * event when driver failed to perform fast FCF failover.
12630  **/
12631 void
12632 lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
12633 {
12634         uint32_t link_state;
12635
12636         /*
12637          * Last resort as FCF DEAD event failover will treat this as
12638          * a link down, but save the link state because we don't want
12639          * it to be changed to Link Down unless it is already down.
12640          */
12641         link_state = phba->link_state;
12642         lpfc_linkdown(phba);
12643         phba->link_state = link_state;
12644
12645         /* Unregister FCF if no devices connected to it */
12646         lpfc_unregister_unused_fcf(phba);
12647 }
12648
12649 /**
12650  * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
12651  * @phba: pointer to lpfc hba data structure.
12652  *
12653  * This function read region 23 and parse TLV for port status to
12654  * decide if the user disaled the port. If the TLV indicates the
12655  * port is disabled, the hba_flag is set accordingly.
12656  **/
12657 void
12658 lpfc_sli_read_link_ste(struct lpfc_hba *phba)
12659 {
12660         LPFC_MBOXQ_t *pmb = NULL;
12661         MAILBOX_t *mb;
12662         uint8_t *rgn23_data = NULL;
12663         uint32_t offset = 0, data_size, sub_tlv_len, tlv_offset;
12664         int rc;
12665
12666         pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12667         if (!pmb) {
12668                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12669                         "2600 lpfc_sli_read_serdes_param failed to"
12670                         " allocate mailbox memory\n");
12671                 goto out;
12672         }
12673         mb = &pmb->u.mb;
12674
12675         /* Get adapter Region 23 data */
12676         rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
12677         if (!rgn23_data)
12678                 goto out;
12679
12680         do {
12681                 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
12682                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
12683
12684                 if (rc != MBX_SUCCESS) {
12685                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
12686                                 "2601 lpfc_sli_read_link_ste failed to"
12687                                 " read config region 23 rc 0x%x Status 0x%x\n",
12688                                 rc, mb->mbxStatus);
12689                         mb->un.varDmp.word_cnt = 0;
12690                 }
12691                 /*
12692                  * dump mem may return a zero when finished or we got a
12693                  * mailbox error, either way we are done.
12694                  */
12695                 if (mb->un.varDmp.word_cnt == 0)
12696                         break;
12697                 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
12698                         mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
12699
12700                 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
12701                         rgn23_data + offset,
12702                         mb->un.varDmp.word_cnt);
12703                 offset += mb->un.varDmp.word_cnt;
12704         } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
12705
12706         data_size = offset;
12707         offset = 0;
12708
12709         if (!data_size)
12710                 goto out;
12711
12712         /* Check the region signature first */
12713         if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
12714                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12715                         "2619 Config region 23 has bad signature\n");
12716                         goto out;
12717         }
12718         offset += 4;
12719
12720         /* Check the data structure version */
12721         if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
12722                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12723                         "2620 Config region 23 has bad version\n");
12724                 goto out;
12725         }
12726         offset += 4;
12727
12728         /* Parse TLV entries in the region */
12729         while (offset < data_size) {
12730                 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
12731                         break;
12732                 /*
12733                  * If the TLV is not driver specific TLV or driver id is
12734                  * not linux driver id, skip the record.
12735                  */
12736                 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
12737                     (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
12738                     (rgn23_data[offset + 3] != 0)) {
12739                         offset += rgn23_data[offset + 1] * 4 + 4;
12740                         continue;
12741                 }
12742
12743                 /* Driver found a driver specific TLV in the config region */
12744                 sub_tlv_len = rgn23_data[offset + 1] * 4;
12745                 offset += 4;
12746                 tlv_offset = 0;
12747
12748                 /*
12749                  * Search for configured port state sub-TLV.
12750                  */
12751                 while ((offset < data_size) &&
12752                         (tlv_offset < sub_tlv_len)) {
12753                         if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
12754                                 offset += 4;
12755                                 tlv_offset += 4;
12756                                 break;
12757                         }
12758                         if (rgn23_data[offset] != PORT_STE_TYPE) {
12759                                 offset += rgn23_data[offset + 1] * 4 + 4;
12760                                 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
12761                                 continue;
12762                         }
12763
12764                         /* This HBA contains PORT_STE configured */
12765                         if (!rgn23_data[offset + 2])
12766                                 phba->hba_flag |= LINK_DISABLED;
12767
12768                         goto out;
12769                 }
12770         }
12771 out:
12772         if (pmb)
12773                 mempool_free(pmb, phba->mbox_mem_pool);
12774         kfree(rgn23_data);
12775         return;
12776 }
12777
12778 /**
12779  * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
12780  * @vport: pointer to vport data structure.
12781  *
12782  * This function iterate through the mailboxq and clean up all REG_LOGIN
12783  * and REG_VPI mailbox commands associated with the vport. This function
12784  * is called when driver want to restart discovery of the vport due to
12785  * a Clear Virtual Link event.
12786  **/
12787 void
12788 lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
12789 {
12790         struct lpfc_hba *phba = vport->phba;
12791         LPFC_MBOXQ_t *mb, *nextmb;
12792         struct lpfc_dmabuf *mp;
12793         struct lpfc_nodelist *ndlp;
12794         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
12795
12796         spin_lock_irq(&phba->hbalock);
12797         list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
12798                 if (mb->vport != vport)
12799                         continue;
12800
12801                 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
12802                         (mb->u.mb.mbxCommand != MBX_REG_VPI))
12803                         continue;
12804
12805                 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
12806                         if (phba->sli_rev == LPFC_SLI_REV4)
12807                                 __lpfc_sli4_free_rpi(phba,
12808                                                 mb->u.mb.un.varRegLogin.rpi);
12809                         mp = (struct lpfc_dmabuf *) (mb->context1);
12810                         if (mp) {
12811                                 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
12812                                 kfree(mp);
12813                         }
12814                         ndlp = (struct lpfc_nodelist *) mb->context2;
12815                         if (ndlp) {
12816                                 spin_lock_irq(shost->host_lock);
12817                                 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
12818                                 spin_unlock_irq(shost->host_lock);
12819                                 lpfc_nlp_put(ndlp);
12820                                 mb->context2 = NULL;
12821                         }
12822                 }
12823                 list_del(&mb->list);
12824                 mempool_free(mb, phba->mbox_mem_pool);
12825         }
12826         mb = phba->sli.mbox_active;
12827         if (mb && (mb->vport == vport)) {
12828                 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
12829                         (mb->u.mb.mbxCommand == MBX_REG_VPI))
12830                         mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12831                 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
12832                         ndlp = (struct lpfc_nodelist *) mb->context2;
12833                         if (ndlp) {
12834                                 spin_lock_irq(shost->host_lock);
12835                                 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
12836                                 spin_unlock_irq(shost->host_lock);
12837                                 lpfc_nlp_put(ndlp);
12838                                 mb->context2 = NULL;
12839                         }
12840                         /* Unregister the RPI when mailbox complete */
12841                         mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
12842                 }
12843         }
12844         spin_unlock_irq(&phba->hbalock);
12845 }
12846
12847 /**
12848  * lpfc_drain_txq - Drain the txq
12849  * @phba: Pointer to HBA context object.
12850  *
12851  * This function attempt to submit IOCBs on the txq
12852  * to the adapter.  For SLI4 adapters, the txq contains
12853  * ELS IOCBs that have been deferred because the there
12854  * are no SGLs.  This congestion can occur with large
12855  * vport counts during node discovery.
12856  **/
12857
12858 uint32_t
12859 lpfc_drain_txq(struct lpfc_hba *phba)
12860 {
12861         LIST_HEAD(completions);
12862         struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
12863         struct lpfc_iocbq *piocbq = 0;
12864         unsigned long iflags = 0;
12865         char *fail_msg = NULL;
12866         struct lpfc_sglq *sglq;
12867         union lpfc_wqe wqe;
12868
12869         spin_lock_irqsave(&phba->hbalock, iflags);
12870         if (pring->txq_cnt > pring->txq_max)
12871                 pring->txq_max = pring->txq_cnt;
12872
12873         spin_unlock_irqrestore(&phba->hbalock, iflags);
12874
12875         while (pring->txq_cnt) {
12876                 spin_lock_irqsave(&phba->hbalock, iflags);
12877
12878                 sglq = __lpfc_sli_get_sglq(phba);
12879                 if (!sglq) {
12880                         spin_unlock_irqrestore(&phba->hbalock, iflags);
12881                         break;
12882                 } else {
12883                         piocbq = lpfc_sli_ringtx_get(phba, pring);
12884                         if (!piocbq) {
12885                                 /* The txq_cnt out of sync. This should
12886                                  * never happen
12887                                  */
12888                                 sglq = __lpfc_clear_active_sglq(phba,
12889                                                  sglq->sli4_xritag);
12890                                 spin_unlock_irqrestore(&phba->hbalock, iflags);
12891                                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12892                                         "2823 txq empty and txq_cnt is %d\n ",
12893                                         pring->txq_cnt);
12894                                 break;
12895                         }
12896                 }
12897
12898                 /* The xri and iocb resources secured,
12899                  * attempt to issue request
12900                  */
12901                 piocbq->sli4_xritag = sglq->sli4_xritag;
12902                 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
12903                         fail_msg = "to convert bpl to sgl";
12904                 else if (lpfc_sli4_iocb2wqe(phba, piocbq, &wqe))
12905                         fail_msg = "to convert iocb to wqe";
12906                 else if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
12907                         fail_msg = " - Wq is full";
12908                 else
12909                         lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
12910
12911                 if (fail_msg) {
12912                         /* Failed means we can't issue and need to cancel */
12913                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12914                                         "2822 IOCB failed %s iotag 0x%x "
12915                                         "xri 0x%x\n",
12916                                         fail_msg,
12917                                         piocbq->iotag, piocbq->sli4_xritag);
12918                         list_add_tail(&piocbq->list, &completions);
12919                 }
12920                 spin_unlock_irqrestore(&phba->hbalock, iflags);
12921         }
12922
12923         /* Cancel all the IOCBs that cannot be issued */
12924         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
12925                                 IOERR_SLI_ABORTED);
12926
12927         return pring->txq_cnt;
12928 }