[SCSI] lpfc 8.3.7: Fix FC protocol errors
[safe/jmp/linux-2.6] / 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
27 #include <scsi/scsi.h>
28 #include <scsi/scsi_cmnd.h>
29 #include <scsi/scsi_device.h>
30 #include <scsi/scsi_host.h>
31 #include <scsi/scsi_transport_fc.h>
32 #include <scsi/fc/fc_fs.h>
33 #include <linux/aer.h>
34
35 #include "lpfc_hw4.h"
36 #include "lpfc_hw.h"
37 #include "lpfc_sli.h"
38 #include "lpfc_sli4.h"
39 #include "lpfc_nl.h"
40 #include "lpfc_disc.h"
41 #include "lpfc_scsi.h"
42 #include "lpfc.h"
43 #include "lpfc_crtn.h"
44 #include "lpfc_logmsg.h"
45 #include "lpfc_compat.h"
46 #include "lpfc_debugfs.h"
47 #include "lpfc_vport.h"
48
49 /* There are only four IOCB completion types. */
50 typedef enum _lpfc_iocb_type {
51         LPFC_UNKNOWN_IOCB,
52         LPFC_UNSOL_IOCB,
53         LPFC_SOL_IOCB,
54         LPFC_ABORT_IOCB
55 } lpfc_iocb_type;
56
57
58 /* Provide function prototypes local to this module. */
59 static int lpfc_sli_issue_mbox_s4(struct lpfc_hba *, LPFC_MBOXQ_t *,
60                                   uint32_t);
61 static int lpfc_sli4_read_rev(struct lpfc_hba *, LPFC_MBOXQ_t *,
62                               uint8_t *, uint32_t *);
63 static struct lpfc_iocbq *lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *,
64                                                          struct lpfc_iocbq *);
65 static void lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *,
66                                       struct hbq_dmabuf *);
67 static IOCB_t *
68 lpfc_get_iocb_from_iocbq(struct lpfc_iocbq *iocbq)
69 {
70         return &iocbq->iocb;
71 }
72
73 /**
74  * lpfc_sli4_wq_put - Put a Work Queue Entry on an Work Queue
75  * @q: The Work Queue to operate on.
76  * @wqe: The work Queue Entry to put on the Work queue.
77  *
78  * This routine will copy the contents of @wqe to the next available entry on
79  * the @q. This function will then ring the Work Queue Doorbell to signal the
80  * HBA to start processing the Work Queue Entry. This function returns 0 if
81  * successful. If no entries are available on @q then this function will return
82  * -ENOMEM.
83  * The caller is expected to hold the hbalock when calling this routine.
84  **/
85 static uint32_t
86 lpfc_sli4_wq_put(struct lpfc_queue *q, union lpfc_wqe *wqe)
87 {
88         union lpfc_wqe *temp_wqe = q->qe[q->host_index].wqe;
89         struct lpfc_register doorbell;
90         uint32_t host_index;
91
92         /* If the host has not yet processed the next entry then we are done */
93         if (((q->host_index + 1) % q->entry_count) == q->hba_index)
94                 return -ENOMEM;
95         /* set consumption flag every once in a while */
96         if (!((q->host_index + 1) % LPFC_RELEASE_NOTIFICATION_INTERVAL))
97                 bf_set(lpfc_wqe_gen_wqec, &wqe->generic, 1);
98
99         lpfc_sli_pcimem_bcopy(wqe, temp_wqe, q->entry_size);
100
101         /* Update the host index before invoking device */
102         host_index = q->host_index;
103         q->host_index = ((q->host_index + 1) % q->entry_count);
104
105         /* Ring Doorbell */
106         doorbell.word0 = 0;
107         bf_set(lpfc_wq_doorbell_num_posted, &doorbell, 1);
108         bf_set(lpfc_wq_doorbell_index, &doorbell, host_index);
109         bf_set(lpfc_wq_doorbell_id, &doorbell, q->queue_id);
110         writel(doorbell.word0, q->phba->sli4_hba.WQDBregaddr);
111         readl(q->phba->sli4_hba.WQDBregaddr); /* Flush */
112
113         return 0;
114 }
115
116 /**
117  * lpfc_sli4_wq_release - Updates internal hba index for WQ
118  * @q: The Work Queue to operate on.
119  * @index: The index to advance the hba index to.
120  *
121  * This routine will update the HBA index of a queue to reflect consumption of
122  * Work Queue Entries by the HBA. When the HBA indicates that it has consumed
123  * an entry the host calls this function to update the queue's internal
124  * pointers. This routine returns the number of entries that were consumed by
125  * the HBA.
126  **/
127 static uint32_t
128 lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)
129 {
130         uint32_t released = 0;
131
132         if (q->hba_index == index)
133                 return 0;
134         do {
135                 q->hba_index = ((q->hba_index + 1) % q->entry_count);
136                 released++;
137         } while (q->hba_index != index);
138         return released;
139 }
140
141 /**
142  * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue
143  * @q: The Mailbox Queue to operate on.
144  * @wqe: The Mailbox Queue Entry to put on the Work queue.
145  *
146  * This routine will copy the contents of @mqe to the next available entry on
147  * the @q. This function will then ring the Work Queue Doorbell to signal the
148  * HBA to start processing the Work Queue Entry. This function returns 0 if
149  * successful. If no entries are available on @q then this function will return
150  * -ENOMEM.
151  * The caller is expected to hold the hbalock when calling this routine.
152  **/
153 static uint32_t
154 lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)
155 {
156         struct lpfc_mqe *temp_mqe = q->qe[q->host_index].mqe;
157         struct lpfc_register doorbell;
158         uint32_t host_index;
159
160         /* If the host has not yet processed the next entry then we are done */
161         if (((q->host_index + 1) % q->entry_count) == q->hba_index)
162                 return -ENOMEM;
163         lpfc_sli_pcimem_bcopy(mqe, temp_mqe, q->entry_size);
164         /* Save off the mailbox pointer for completion */
165         q->phba->mbox = (MAILBOX_t *)temp_mqe;
166
167         /* Update the host index before invoking device */
168         host_index = q->host_index;
169         q->host_index = ((q->host_index + 1) % q->entry_count);
170
171         /* Ring Doorbell */
172         doorbell.word0 = 0;
173         bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);
174         bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);
175         writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);
176         readl(q->phba->sli4_hba.MQDBregaddr); /* Flush */
177         return 0;
178 }
179
180 /**
181  * lpfc_sli4_mq_release - Updates internal hba index for MQ
182  * @q: The Mailbox Queue to operate on.
183  *
184  * This routine will update the HBA index of a queue to reflect consumption of
185  * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed
186  * an entry the host calls this function to update the queue's internal
187  * pointers. This routine returns the number of entries that were consumed by
188  * the HBA.
189  **/
190 static uint32_t
191 lpfc_sli4_mq_release(struct lpfc_queue *q)
192 {
193         /* Clear the mailbox pointer for completion */
194         q->phba->mbox = NULL;
195         q->hba_index = ((q->hba_index + 1) % q->entry_count);
196         return 1;
197 }
198
199 /**
200  * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ
201  * @q: The Event Queue to get the first valid EQE from
202  *
203  * This routine will get the first valid Event Queue Entry from @q, update
204  * the queue's internal hba index, and return the EQE. If no valid EQEs are in
205  * the Queue (no more work to do), or the Queue is full of EQEs that have been
206  * processed, but not popped back to the HBA then this routine will return NULL.
207  **/
208 static struct lpfc_eqe *
209 lpfc_sli4_eq_get(struct lpfc_queue *q)
210 {
211         struct lpfc_eqe *eqe = q->qe[q->hba_index].eqe;
212
213         /* If the next EQE is not valid then we are done */
214         if (!bf_get(lpfc_eqe_valid, eqe))
215                 return NULL;
216         /* If the host has not yet processed the next entry then we are done */
217         if (((q->hba_index + 1) % q->entry_count) == q->host_index)
218                 return NULL;
219
220         q->hba_index = ((q->hba_index + 1) % q->entry_count);
221         return eqe;
222 }
223
224 /**
225  * lpfc_sli4_eq_release - Indicates the host has finished processing an EQ
226  * @q: The Event Queue that the host has completed processing for.
227  * @arm: Indicates whether the host wants to arms this CQ.
228  *
229  * This routine will mark all Event Queue Entries on @q, from the last
230  * known completed entry to the last entry that was processed, as completed
231  * by clearing the valid bit for each completion queue entry. Then it will
232  * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
233  * The internal host index in the @q will be updated by this routine to indicate
234  * that the host has finished processing the entries. The @arm parameter
235  * indicates that the queue should be rearmed when ringing the doorbell.
236  *
237  * This function will return the number of EQEs that were popped.
238  **/
239 uint32_t
240 lpfc_sli4_eq_release(struct lpfc_queue *q, bool arm)
241 {
242         uint32_t released = 0;
243         struct lpfc_eqe *temp_eqe;
244         struct lpfc_register doorbell;
245
246         /* while there are valid entries */
247         while (q->hba_index != q->host_index) {
248                 temp_eqe = q->qe[q->host_index].eqe;
249                 bf_set(lpfc_eqe_valid, temp_eqe, 0);
250                 released++;
251                 q->host_index = ((q->host_index + 1) % q->entry_count);
252         }
253         if (unlikely(released == 0 && !arm))
254                 return 0;
255
256         /* ring doorbell for number popped */
257         doorbell.word0 = 0;
258         if (arm) {
259                 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
260                 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
261         }
262         bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
263         bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
264         bf_set(lpfc_eqcq_doorbell_eqid, &doorbell, q->queue_id);
265         writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
266         /* PCI read to flush PCI pipeline on re-arming for INTx mode */
267         if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
268                 readl(q->phba->sli4_hba.EQCQDBregaddr);
269         return released;
270 }
271
272 /**
273  * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
274  * @q: The Completion Queue to get the first valid CQE from
275  *
276  * This routine will get the first valid Completion Queue Entry from @q, update
277  * the queue's internal hba index, and return the CQE. If no valid CQEs are in
278  * the Queue (no more work to do), or the Queue is full of CQEs that have been
279  * processed, but not popped back to the HBA then this routine will return NULL.
280  **/
281 static struct lpfc_cqe *
282 lpfc_sli4_cq_get(struct lpfc_queue *q)
283 {
284         struct lpfc_cqe *cqe;
285
286         /* If the next CQE is not valid then we are done */
287         if (!bf_get(lpfc_cqe_valid, q->qe[q->hba_index].cqe))
288                 return NULL;
289         /* If the host has not yet processed the next entry then we are done */
290         if (((q->hba_index + 1) % q->entry_count) == q->host_index)
291                 return NULL;
292
293         cqe = q->qe[q->hba_index].cqe;
294         q->hba_index = ((q->hba_index + 1) % q->entry_count);
295         return cqe;
296 }
297
298 /**
299  * lpfc_sli4_cq_release - Indicates the host has finished processing a CQ
300  * @q: The Completion Queue that the host has completed processing for.
301  * @arm: Indicates whether the host wants to arms this CQ.
302  *
303  * This routine will mark all Completion queue entries on @q, from the last
304  * known completed entry to the last entry that was processed, as completed
305  * by clearing the valid bit for each completion queue entry. Then it will
306  * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
307  * The internal host index in the @q will be updated by this routine to indicate
308  * that the host has finished processing the entries. The @arm parameter
309  * indicates that the queue should be rearmed when ringing the doorbell.
310  *
311  * This function will return the number of CQEs that were released.
312  **/
313 uint32_t
314 lpfc_sli4_cq_release(struct lpfc_queue *q, bool arm)
315 {
316         uint32_t released = 0;
317         struct lpfc_cqe *temp_qe;
318         struct lpfc_register doorbell;
319
320         /* while there are valid entries */
321         while (q->hba_index != q->host_index) {
322                 temp_qe = q->qe[q->host_index].cqe;
323                 bf_set(lpfc_cqe_valid, temp_qe, 0);
324                 released++;
325                 q->host_index = ((q->host_index + 1) % q->entry_count);
326         }
327         if (unlikely(released == 0 && !arm))
328                 return 0;
329
330         /* ring doorbell for number popped */
331         doorbell.word0 = 0;
332         if (arm)
333                 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
334         bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
335         bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
336         bf_set(lpfc_eqcq_doorbell_cqid, &doorbell, q->queue_id);
337         writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
338         return released;
339 }
340
341 /**
342  * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
343  * @q: The Header Receive Queue to operate on.
344  * @wqe: The Receive Queue Entry to put on the Receive queue.
345  *
346  * This routine will copy the contents of @wqe to the next available entry on
347  * the @q. This function will then ring the Receive Queue Doorbell to signal the
348  * HBA to start processing the Receive Queue Entry. This function returns the
349  * index that the rqe was copied to if successful. If no entries are available
350  * on @q then this function will return -ENOMEM.
351  * The caller is expected to hold the hbalock when calling this routine.
352  **/
353 static int
354 lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
355                  struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
356 {
357         struct lpfc_rqe *temp_hrqe = hq->qe[hq->host_index].rqe;
358         struct lpfc_rqe *temp_drqe = dq->qe[dq->host_index].rqe;
359         struct lpfc_register doorbell;
360         int put_index = hq->host_index;
361
362         if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
363                 return -EINVAL;
364         if (hq->host_index != dq->host_index)
365                 return -EINVAL;
366         /* If the host has not yet processed the next entry then we are done */
367         if (((hq->host_index + 1) % hq->entry_count) == hq->hba_index)
368                 return -EBUSY;
369         lpfc_sli_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
370         lpfc_sli_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
371
372         /* Update the host index to point to the next slot */
373         hq->host_index = ((hq->host_index + 1) % hq->entry_count);
374         dq->host_index = ((dq->host_index + 1) % dq->entry_count);
375
376         /* Ring The Header Receive Queue Doorbell */
377         if (!(hq->host_index % LPFC_RQ_POST_BATCH)) {
378                 doorbell.word0 = 0;
379                 bf_set(lpfc_rq_doorbell_num_posted, &doorbell,
380                        LPFC_RQ_POST_BATCH);
381                 bf_set(lpfc_rq_doorbell_id, &doorbell, hq->queue_id);
382                 writel(doorbell.word0, hq->phba->sli4_hba.RQDBregaddr);
383         }
384         return put_index;
385 }
386
387 /**
388  * lpfc_sli4_rq_release - Updates internal hba index for RQ
389  * @q: The Header Receive Queue to operate on.
390  *
391  * This routine will update the HBA index of a queue to reflect consumption of
392  * one Receive Queue Entry by the HBA. When the HBA indicates that it has
393  * consumed an entry the host calls this function to update the queue's
394  * internal pointers. This routine returns the number of entries that were
395  * consumed by the HBA.
396  **/
397 static uint32_t
398 lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
399 {
400         if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
401                 return 0;
402         hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
403         dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
404         return 1;
405 }
406
407 /**
408  * lpfc_cmd_iocb - Get next command iocb entry in the ring
409  * @phba: Pointer to HBA context object.
410  * @pring: Pointer to driver SLI ring object.
411  *
412  * This function returns pointer to next command iocb entry
413  * in the command ring. The caller must hold hbalock to prevent
414  * other threads consume the next command iocb.
415  * SLI-2/SLI-3 provide different sized iocbs.
416  **/
417 static inline IOCB_t *
418 lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
419 {
420         return (IOCB_t *) (((char *) pring->cmdringaddr) +
421                            pring->cmdidx * phba->iocb_cmd_size);
422 }
423
424 /**
425  * lpfc_resp_iocb - Get next response iocb entry in the ring
426  * @phba: Pointer to HBA context object.
427  * @pring: Pointer to driver SLI ring object.
428  *
429  * This function returns pointer to next response iocb entry
430  * in the response ring. The caller must hold hbalock to make sure
431  * that no other thread consume the next response iocb.
432  * SLI-2/SLI-3 provide different sized iocbs.
433  **/
434 static inline IOCB_t *
435 lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
436 {
437         return (IOCB_t *) (((char *) pring->rspringaddr) +
438                            pring->rspidx * phba->iocb_rsp_size);
439 }
440
441 /**
442  * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
443  * @phba: Pointer to HBA context object.
444  *
445  * This function is called with hbalock held. This function
446  * allocates a new driver iocb object from the iocb pool. If the
447  * allocation is successful, it returns pointer to the newly
448  * allocated iocb object else it returns NULL.
449  **/
450 static struct lpfc_iocbq *
451 __lpfc_sli_get_iocbq(struct lpfc_hba *phba)
452 {
453         struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
454         struct lpfc_iocbq * iocbq = NULL;
455
456         list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
457         return iocbq;
458 }
459
460 /**
461  * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
462  * @phba: Pointer to HBA context object.
463  * @xritag: XRI value.
464  *
465  * This function clears the sglq pointer from the array of acive
466  * sglq's. The xritag that is passed in is used to index into the
467  * array. Before the xritag can be used it needs to be adjusted
468  * by subtracting the xribase.
469  *
470  * Returns sglq ponter = success, NULL = Failure.
471  **/
472 static struct lpfc_sglq *
473 __lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
474 {
475         uint16_t adj_xri;
476         struct lpfc_sglq *sglq;
477         adj_xri = xritag - phba->sli4_hba.max_cfg_param.xri_base;
478         if (adj_xri > phba->sli4_hba.max_cfg_param.max_xri)
479                 return NULL;
480         sglq = phba->sli4_hba.lpfc_sglq_active_list[adj_xri];
481         phba->sli4_hba.lpfc_sglq_active_list[adj_xri] = NULL;
482         return sglq;
483 }
484
485 /**
486  * __lpfc_get_active_sglq - Get the active sglq for this XRI.
487  * @phba: Pointer to HBA context object.
488  * @xritag: XRI value.
489  *
490  * This function returns the sglq pointer from the array of acive
491  * sglq's. The xritag that is passed in is used to index into the
492  * array. Before the xritag can be used it needs to be adjusted
493  * by subtracting the xribase.
494  *
495  * Returns sglq ponter = success, NULL = Failure.
496  **/
497 static struct lpfc_sglq *
498 __lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
499 {
500         uint16_t adj_xri;
501         struct lpfc_sglq *sglq;
502         adj_xri = xritag - phba->sli4_hba.max_cfg_param.xri_base;
503         if (adj_xri > phba->sli4_hba.max_cfg_param.max_xri)
504                 return NULL;
505         sglq =  phba->sli4_hba.lpfc_sglq_active_list[adj_xri];
506         return sglq;
507 }
508
509 /**
510  * __lpfc_sli_get_sglq - Allocates an iocb object from sgl pool
511  * @phba: Pointer to HBA context object.
512  *
513  * This function is called with hbalock held. This function
514  * Gets a new driver sglq object from the sglq list. If the
515  * list is not empty then it is successful, it returns pointer to the newly
516  * allocated sglq object else it returns NULL.
517  **/
518 static struct lpfc_sglq *
519 __lpfc_sli_get_sglq(struct lpfc_hba *phba)
520 {
521         struct list_head *lpfc_sgl_list = &phba->sli4_hba.lpfc_sgl_list;
522         struct lpfc_sglq *sglq = NULL;
523         uint16_t adj_xri;
524         list_remove_head(lpfc_sgl_list, sglq, struct lpfc_sglq, list);
525         if (!sglq)
526                 return NULL;
527         adj_xri = sglq->sli4_xritag - phba->sli4_hba.max_cfg_param.xri_base;
528         phba->sli4_hba.lpfc_sglq_active_list[adj_xri] = sglq;
529         return sglq;
530 }
531
532 /**
533  * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
534  * @phba: Pointer to HBA context object.
535  *
536  * This function is called with no lock held. This function
537  * allocates a new driver iocb object from the iocb pool. If the
538  * allocation is successful, it returns pointer to the newly
539  * allocated iocb object else it returns NULL.
540  **/
541 struct lpfc_iocbq *
542 lpfc_sli_get_iocbq(struct lpfc_hba *phba)
543 {
544         struct lpfc_iocbq * iocbq = NULL;
545         unsigned long iflags;
546
547         spin_lock_irqsave(&phba->hbalock, iflags);
548         iocbq = __lpfc_sli_get_iocbq(phba);
549         spin_unlock_irqrestore(&phba->hbalock, iflags);
550         return iocbq;
551 }
552
553 /**
554  * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
555  * @phba: Pointer to HBA context object.
556  * @iocbq: Pointer to driver iocb object.
557  *
558  * This function is called with hbalock held to release driver
559  * iocb object to the iocb pool. The iotag in the iocb object
560  * does not change for each use of the iocb object. This function
561  * clears all other fields of the iocb object when it is freed.
562  * The sqlq structure that holds the xritag and phys and virtual
563  * mappings for the scatter gather list is retrieved from the
564  * active array of sglq. The get of the sglq pointer also clears
565  * the entry in the array. If the status of the IO indiactes that
566  * this IO was aborted then the sglq entry it put on the
567  * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
568  * IO has good status or fails for any other reason then the sglq
569  * entry is added to the free list (lpfc_sgl_list).
570  **/
571 static void
572 __lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
573 {
574         struct lpfc_sglq *sglq;
575         size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
576         unsigned long iflag;
577
578         if (iocbq->sli4_xritag == NO_XRI)
579                 sglq = NULL;
580         else
581                 sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_xritag);
582         if (sglq)  {
583                 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED
584                         && ((iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
585                         && (iocbq->iocb.un.ulpWord[4]
586                                 == IOERR_ABORT_REQUESTED))) {
587                         spin_lock_irqsave(&phba->sli4_hba.abts_sgl_list_lock,
588                                         iflag);
589                         list_add(&sglq->list,
590                                 &phba->sli4_hba.lpfc_abts_els_sgl_list);
591                         spin_unlock_irqrestore(
592                                 &phba->sli4_hba.abts_sgl_list_lock, iflag);
593                 } else
594                         list_add(&sglq->list, &phba->sli4_hba.lpfc_sgl_list);
595         }
596
597
598         /*
599          * Clean all volatile data fields, preserve iotag and node struct.
600          */
601         memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
602         iocbq->sli4_xritag = NO_XRI;
603         list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
604 }
605
606 /**
607  * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
608  * @phba: Pointer to HBA context object.
609  * @iocbq: Pointer to driver iocb object.
610  *
611  * This function is called with hbalock held to release driver
612  * iocb object to the iocb pool. The iotag in the iocb object
613  * does not change for each use of the iocb object. This function
614  * clears all other fields of the iocb object when it is freed.
615  **/
616 static void
617 __lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
618 {
619         size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
620
621         /*
622          * Clean all volatile data fields, preserve iotag and node struct.
623          */
624         memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
625         iocbq->sli4_xritag = NO_XRI;
626         list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
627 }
628
629 /**
630  * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
631  * @phba: Pointer to HBA context object.
632  * @iocbq: Pointer to driver iocb object.
633  *
634  * This function is called with hbalock held to release driver
635  * iocb object to the iocb pool. The iotag in the iocb object
636  * does not change for each use of the iocb object. This function
637  * clears all other fields of the iocb object when it is freed.
638  **/
639 static void
640 __lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
641 {
642         phba->__lpfc_sli_release_iocbq(phba, iocbq);
643 }
644
645 /**
646  * lpfc_sli_release_iocbq - Release iocb to the iocb pool
647  * @phba: Pointer to HBA context object.
648  * @iocbq: Pointer to driver iocb object.
649  *
650  * This function is called with no lock held to release the iocb to
651  * iocb pool.
652  **/
653 void
654 lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
655 {
656         unsigned long iflags;
657
658         /*
659          * Clean all volatile data fields, preserve iotag and node struct.
660          */
661         spin_lock_irqsave(&phba->hbalock, iflags);
662         __lpfc_sli_release_iocbq(phba, iocbq);
663         spin_unlock_irqrestore(&phba->hbalock, iflags);
664 }
665
666 /**
667  * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
668  * @phba: Pointer to HBA context object.
669  * @iocblist: List of IOCBs.
670  * @ulpstatus: ULP status in IOCB command field.
671  * @ulpWord4: ULP word-4 in IOCB command field.
672  *
673  * This function is called with a list of IOCBs to cancel. It cancels the IOCB
674  * on the list by invoking the complete callback function associated with the
675  * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
676  * fields.
677  **/
678 void
679 lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
680                       uint32_t ulpstatus, uint32_t ulpWord4)
681 {
682         struct lpfc_iocbq *piocb;
683
684         while (!list_empty(iocblist)) {
685                 list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
686
687                 if (!piocb->iocb_cmpl)
688                         lpfc_sli_release_iocbq(phba, piocb);
689                 else {
690                         piocb->iocb.ulpStatus = ulpstatus;
691                         piocb->iocb.un.ulpWord[4] = ulpWord4;
692                         (piocb->iocb_cmpl) (phba, piocb, piocb);
693                 }
694         }
695         return;
696 }
697
698 /**
699  * lpfc_sli_iocb_cmd_type - Get the iocb type
700  * @iocb_cmnd: iocb command code.
701  *
702  * This function is called by ring event handler function to get the iocb type.
703  * This function translates the iocb command to an iocb command type used to
704  * decide the final disposition of each completed IOCB.
705  * The function returns
706  * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
707  * LPFC_SOL_IOCB     if it is a solicited iocb completion
708  * LPFC_ABORT_IOCB   if it is an abort iocb
709  * LPFC_UNSOL_IOCB   if it is an unsolicited iocb
710  *
711  * The caller is not required to hold any lock.
712  **/
713 static lpfc_iocb_type
714 lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
715 {
716         lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
717
718         if (iocb_cmnd > CMD_MAX_IOCB_CMD)
719                 return 0;
720
721         switch (iocb_cmnd) {
722         case CMD_XMIT_SEQUENCE_CR:
723         case CMD_XMIT_SEQUENCE_CX:
724         case CMD_XMIT_BCAST_CN:
725         case CMD_XMIT_BCAST_CX:
726         case CMD_ELS_REQUEST_CR:
727         case CMD_ELS_REQUEST_CX:
728         case CMD_CREATE_XRI_CR:
729         case CMD_CREATE_XRI_CX:
730         case CMD_GET_RPI_CN:
731         case CMD_XMIT_ELS_RSP_CX:
732         case CMD_GET_RPI_CR:
733         case CMD_FCP_IWRITE_CR:
734         case CMD_FCP_IWRITE_CX:
735         case CMD_FCP_IREAD_CR:
736         case CMD_FCP_IREAD_CX:
737         case CMD_FCP_ICMND_CR:
738         case CMD_FCP_ICMND_CX:
739         case CMD_FCP_TSEND_CX:
740         case CMD_FCP_TRSP_CX:
741         case CMD_FCP_TRECEIVE_CX:
742         case CMD_FCP_AUTO_TRSP_CX:
743         case CMD_ADAPTER_MSG:
744         case CMD_ADAPTER_DUMP:
745         case CMD_XMIT_SEQUENCE64_CR:
746         case CMD_XMIT_SEQUENCE64_CX:
747         case CMD_XMIT_BCAST64_CN:
748         case CMD_XMIT_BCAST64_CX:
749         case CMD_ELS_REQUEST64_CR:
750         case CMD_ELS_REQUEST64_CX:
751         case CMD_FCP_IWRITE64_CR:
752         case CMD_FCP_IWRITE64_CX:
753         case CMD_FCP_IREAD64_CR:
754         case CMD_FCP_IREAD64_CX:
755         case CMD_FCP_ICMND64_CR:
756         case CMD_FCP_ICMND64_CX:
757         case CMD_FCP_TSEND64_CX:
758         case CMD_FCP_TRSP64_CX:
759         case CMD_FCP_TRECEIVE64_CX:
760         case CMD_GEN_REQUEST64_CR:
761         case CMD_GEN_REQUEST64_CX:
762         case CMD_XMIT_ELS_RSP64_CX:
763         case DSSCMD_IWRITE64_CR:
764         case DSSCMD_IWRITE64_CX:
765         case DSSCMD_IREAD64_CR:
766         case DSSCMD_IREAD64_CX:
767         case DSSCMD_INVALIDATE_DEK:
768         case DSSCMD_SET_KEK:
769         case DSSCMD_GET_KEK_ID:
770         case DSSCMD_GEN_XFER:
771                 type = LPFC_SOL_IOCB;
772                 break;
773         case CMD_ABORT_XRI_CN:
774         case CMD_ABORT_XRI_CX:
775         case CMD_CLOSE_XRI_CN:
776         case CMD_CLOSE_XRI_CX:
777         case CMD_XRI_ABORTED_CX:
778         case CMD_ABORT_MXRI64_CN:
779         case CMD_XMIT_BLS_RSP64_CX:
780                 type = LPFC_ABORT_IOCB;
781                 break;
782         case CMD_RCV_SEQUENCE_CX:
783         case CMD_RCV_ELS_REQ_CX:
784         case CMD_RCV_SEQUENCE64_CX:
785         case CMD_RCV_ELS_REQ64_CX:
786         case CMD_ASYNC_STATUS:
787         case CMD_IOCB_RCV_SEQ64_CX:
788         case CMD_IOCB_RCV_ELS64_CX:
789         case CMD_IOCB_RCV_CONT64_CX:
790         case CMD_IOCB_RET_XRI64_CX:
791                 type = LPFC_UNSOL_IOCB;
792                 break;
793         case CMD_IOCB_XMIT_MSEQ64_CR:
794         case CMD_IOCB_XMIT_MSEQ64_CX:
795         case CMD_IOCB_RCV_SEQ_LIST64_CX:
796         case CMD_IOCB_RCV_ELS_LIST64_CX:
797         case CMD_IOCB_CLOSE_EXTENDED_CN:
798         case CMD_IOCB_ABORT_EXTENDED_CN:
799         case CMD_IOCB_RET_HBQE64_CN:
800         case CMD_IOCB_FCP_IBIDIR64_CR:
801         case CMD_IOCB_FCP_IBIDIR64_CX:
802         case CMD_IOCB_FCP_ITASKMGT64_CX:
803         case CMD_IOCB_LOGENTRY_CN:
804         case CMD_IOCB_LOGENTRY_ASYNC_CN:
805                 printk("%s - Unhandled SLI-3 Command x%x\n",
806                                 __func__, iocb_cmnd);
807                 type = LPFC_UNKNOWN_IOCB;
808                 break;
809         default:
810                 type = LPFC_UNKNOWN_IOCB;
811                 break;
812         }
813
814         return type;
815 }
816
817 /**
818  * lpfc_sli_ring_map - Issue config_ring mbox for all rings
819  * @phba: Pointer to HBA context object.
820  *
821  * This function is called from SLI initialization code
822  * to configure every ring of the HBA's SLI interface. The
823  * caller is not required to hold any lock. This function issues
824  * a config_ring mailbox command for each ring.
825  * This function returns zero if successful else returns a negative
826  * error code.
827  **/
828 static int
829 lpfc_sli_ring_map(struct lpfc_hba *phba)
830 {
831         struct lpfc_sli *psli = &phba->sli;
832         LPFC_MBOXQ_t *pmb;
833         MAILBOX_t *pmbox;
834         int i, rc, ret = 0;
835
836         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
837         if (!pmb)
838                 return -ENOMEM;
839         pmbox = &pmb->u.mb;
840         phba->link_state = LPFC_INIT_MBX_CMDS;
841         for (i = 0; i < psli->num_rings; i++) {
842                 lpfc_config_ring(phba, i, pmb);
843                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
844                 if (rc != MBX_SUCCESS) {
845                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
846                                         "0446 Adapter failed to init (%d), "
847                                         "mbxCmd x%x CFG_RING, mbxStatus x%x, "
848                                         "ring %d\n",
849                                         rc, pmbox->mbxCommand,
850                                         pmbox->mbxStatus, i);
851                         phba->link_state = LPFC_HBA_ERROR;
852                         ret = -ENXIO;
853                         break;
854                 }
855         }
856         mempool_free(pmb, phba->mbox_mem_pool);
857         return ret;
858 }
859
860 /**
861  * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
862  * @phba: Pointer to HBA context object.
863  * @pring: Pointer to driver SLI ring object.
864  * @piocb: Pointer to the driver iocb object.
865  *
866  * This function is called with hbalock held. The function adds the
867  * new iocb to txcmplq of the given ring. This function always returns
868  * 0. If this function is called for ELS ring, this function checks if
869  * there is a vport associated with the ELS command. This function also
870  * starts els_tmofunc timer if this is an ELS command.
871  **/
872 static int
873 lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
874                         struct lpfc_iocbq *piocb)
875 {
876         list_add_tail(&piocb->list, &pring->txcmplq);
877         pring->txcmplq_cnt++;
878         if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
879            (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
880            (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
881                 if (!piocb->vport)
882                         BUG();
883                 else
884                         mod_timer(&piocb->vport->els_tmofunc,
885                                   jiffies + HZ * (phba->fc_ratov << 1));
886         }
887
888
889         return 0;
890 }
891
892 /**
893  * lpfc_sli_ringtx_get - Get first element of the txq
894  * @phba: Pointer to HBA context object.
895  * @pring: Pointer to driver SLI ring object.
896  *
897  * This function is called with hbalock held to get next
898  * iocb in txq of the given ring. If there is any iocb in
899  * the txq, the function returns first iocb in the list after
900  * removing the iocb from the list, else it returns NULL.
901  **/
902 static struct lpfc_iocbq *
903 lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
904 {
905         struct lpfc_iocbq *cmd_iocb;
906
907         list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
908         if (cmd_iocb != NULL)
909                 pring->txq_cnt--;
910         return cmd_iocb;
911 }
912
913 /**
914  * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
915  * @phba: Pointer to HBA context object.
916  * @pring: Pointer to driver SLI ring object.
917  *
918  * This function is called with hbalock held and the caller must post the
919  * iocb without releasing the lock. If the caller releases the lock,
920  * iocb slot returned by the function is not guaranteed to be available.
921  * The function returns pointer to the next available iocb slot if there
922  * is available slot in the ring, else it returns NULL.
923  * If the get index of the ring is ahead of the put index, the function
924  * will post an error attention event to the worker thread to take the
925  * HBA to offline state.
926  **/
927 static IOCB_t *
928 lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
929 {
930         struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
931         uint32_t  max_cmd_idx = pring->numCiocb;
932         if ((pring->next_cmdidx == pring->cmdidx) &&
933            (++pring->next_cmdidx >= max_cmd_idx))
934                 pring->next_cmdidx = 0;
935
936         if (unlikely(pring->local_getidx == pring->next_cmdidx)) {
937
938                 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
939
940                 if (unlikely(pring->local_getidx >= max_cmd_idx)) {
941                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
942                                         "0315 Ring %d issue: portCmdGet %d "
943                                         "is bigger than cmd ring %d\n",
944                                         pring->ringno,
945                                         pring->local_getidx, max_cmd_idx);
946
947                         phba->link_state = LPFC_HBA_ERROR;
948                         /*
949                          * All error attention handlers are posted to
950                          * worker thread
951                          */
952                         phba->work_ha |= HA_ERATT;
953                         phba->work_hs = HS_FFER3;
954
955                         lpfc_worker_wake_up(phba);
956
957                         return NULL;
958                 }
959
960                 if (pring->local_getidx == pring->next_cmdidx)
961                         return NULL;
962         }
963
964         return lpfc_cmd_iocb(phba, pring);
965 }
966
967 /**
968  * lpfc_sli_next_iotag - Get an iotag for the iocb
969  * @phba: Pointer to HBA context object.
970  * @iocbq: Pointer to driver iocb object.
971  *
972  * This function gets an iotag for the iocb. If there is no unused iotag and
973  * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
974  * array and assigns a new iotag.
975  * The function returns the allocated iotag if successful, else returns zero.
976  * Zero is not a valid iotag.
977  * The caller is not required to hold any lock.
978  **/
979 uint16_t
980 lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
981 {
982         struct lpfc_iocbq **new_arr;
983         struct lpfc_iocbq **old_arr;
984         size_t new_len;
985         struct lpfc_sli *psli = &phba->sli;
986         uint16_t iotag;
987
988         spin_lock_irq(&phba->hbalock);
989         iotag = psli->last_iotag;
990         if(++iotag < psli->iocbq_lookup_len) {
991                 psli->last_iotag = iotag;
992                 psli->iocbq_lookup[iotag] = iocbq;
993                 spin_unlock_irq(&phba->hbalock);
994                 iocbq->iotag = iotag;
995                 return iotag;
996         } else if (psli->iocbq_lookup_len < (0xffff
997                                            - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
998                 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
999                 spin_unlock_irq(&phba->hbalock);
1000                 new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
1001                                   GFP_KERNEL);
1002                 if (new_arr) {
1003                         spin_lock_irq(&phba->hbalock);
1004                         old_arr = psli->iocbq_lookup;
1005                         if (new_len <= psli->iocbq_lookup_len) {
1006                                 /* highly unprobable case */
1007                                 kfree(new_arr);
1008                                 iotag = psli->last_iotag;
1009                                 if(++iotag < psli->iocbq_lookup_len) {
1010                                         psli->last_iotag = iotag;
1011                                         psli->iocbq_lookup[iotag] = iocbq;
1012                                         spin_unlock_irq(&phba->hbalock);
1013                                         iocbq->iotag = iotag;
1014                                         return iotag;
1015                                 }
1016                                 spin_unlock_irq(&phba->hbalock);
1017                                 return 0;
1018                         }
1019                         if (psli->iocbq_lookup)
1020                                 memcpy(new_arr, old_arr,
1021                                        ((psli->last_iotag  + 1) *
1022                                         sizeof (struct lpfc_iocbq *)));
1023                         psli->iocbq_lookup = new_arr;
1024                         psli->iocbq_lookup_len = new_len;
1025                         psli->last_iotag = iotag;
1026                         psli->iocbq_lookup[iotag] = iocbq;
1027                         spin_unlock_irq(&phba->hbalock);
1028                         iocbq->iotag = iotag;
1029                         kfree(old_arr);
1030                         return iotag;
1031                 }
1032         } else
1033                 spin_unlock_irq(&phba->hbalock);
1034
1035         lpfc_printf_log(phba, KERN_ERR,LOG_SLI,
1036                         "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
1037                         psli->last_iotag);
1038
1039         return 0;
1040 }
1041
1042 /**
1043  * lpfc_sli_submit_iocb - Submit an iocb to the firmware
1044  * @phba: Pointer to HBA context object.
1045  * @pring: Pointer to driver SLI ring object.
1046  * @iocb: Pointer to iocb slot in the ring.
1047  * @nextiocb: Pointer to driver iocb object which need to be
1048  *            posted to firmware.
1049  *
1050  * This function is called with hbalock held to post a new iocb to
1051  * the firmware. This function copies the new iocb to ring iocb slot and
1052  * updates the ring pointers. It adds the new iocb to txcmplq if there is
1053  * a completion call back for this iocb else the function will free the
1054  * iocb object.
1055  **/
1056 static void
1057 lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1058                 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
1059 {
1060         /*
1061          * Set up an iotag
1062          */
1063         nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
1064
1065
1066         if (pring->ringno == LPFC_ELS_RING) {
1067                 lpfc_debugfs_slow_ring_trc(phba,
1068                         "IOCB cmd ring:   wd4:x%08x wd6:x%08x wd7:x%08x",
1069                         *(((uint32_t *) &nextiocb->iocb) + 4),
1070                         *(((uint32_t *) &nextiocb->iocb) + 6),
1071                         *(((uint32_t *) &nextiocb->iocb) + 7));
1072         }
1073
1074         /*
1075          * Issue iocb command to adapter
1076          */
1077         lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
1078         wmb();
1079         pring->stats.iocb_cmd++;
1080
1081         /*
1082          * If there is no completion routine to call, we can release the
1083          * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
1084          * that have no rsp ring completion, iocb_cmpl MUST be NULL.
1085          */
1086         if (nextiocb->iocb_cmpl)
1087                 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
1088         else
1089                 __lpfc_sli_release_iocbq(phba, nextiocb);
1090
1091         /*
1092          * Let the HBA know what IOCB slot will be the next one the
1093          * driver will put a command into.
1094          */
1095         pring->cmdidx = pring->next_cmdidx;
1096         writel(pring->cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
1097 }
1098
1099 /**
1100  * lpfc_sli_update_full_ring - Update the chip attention register
1101  * @phba: Pointer to HBA context object.
1102  * @pring: Pointer to driver SLI ring object.
1103  *
1104  * The caller is not required to hold any lock for calling this function.
1105  * This function updates the chip attention bits for the ring to inform firmware
1106  * that there are pending work to be done for this ring and requests an
1107  * interrupt when there is space available in the ring. This function is
1108  * called when the driver is unable to post more iocbs to the ring due
1109  * to unavailability of space in the ring.
1110  **/
1111 static void
1112 lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1113 {
1114         int ringno = pring->ringno;
1115
1116         pring->flag |= LPFC_CALL_RING_AVAILABLE;
1117
1118         wmb();
1119
1120         /*
1121          * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
1122          * The HBA will tell us when an IOCB entry is available.
1123          */
1124         writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
1125         readl(phba->CAregaddr); /* flush */
1126
1127         pring->stats.iocb_cmd_full++;
1128 }
1129
1130 /**
1131  * lpfc_sli_update_ring - Update chip attention register
1132  * @phba: Pointer to HBA context object.
1133  * @pring: Pointer to driver SLI ring object.
1134  *
1135  * This function updates the chip attention register bit for the
1136  * given ring to inform HBA that there is more work to be done
1137  * in this ring. The caller is not required to hold any lock.
1138  **/
1139 static void
1140 lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1141 {
1142         int ringno = pring->ringno;
1143
1144         /*
1145          * Tell the HBA that there is work to do in this ring.
1146          */
1147         if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
1148                 wmb();
1149                 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
1150                 readl(phba->CAregaddr); /* flush */
1151         }
1152 }
1153
1154 /**
1155  * lpfc_sli_resume_iocb - Process iocbs in the txq
1156  * @phba: Pointer to HBA context object.
1157  * @pring: Pointer to driver SLI ring object.
1158  *
1159  * This function is called with hbalock held to post pending iocbs
1160  * in the txq to the firmware. This function is called when driver
1161  * detects space available in the ring.
1162  **/
1163 static void
1164 lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1165 {
1166         IOCB_t *iocb;
1167         struct lpfc_iocbq *nextiocb;
1168
1169         /*
1170          * Check to see if:
1171          *  (a) there is anything on the txq to send
1172          *  (b) link is up
1173          *  (c) link attention events can be processed (fcp ring only)
1174          *  (d) IOCB processing is not blocked by the outstanding mbox command.
1175          */
1176         if (pring->txq_cnt &&
1177             lpfc_is_link_up(phba) &&
1178             (pring->ringno != phba->sli.fcp_ring ||
1179              phba->sli.sli_flag & LPFC_PROCESS_LA)) {
1180
1181                 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
1182                        (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
1183                         lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
1184
1185                 if (iocb)
1186                         lpfc_sli_update_ring(phba, pring);
1187                 else
1188                         lpfc_sli_update_full_ring(phba, pring);
1189         }
1190
1191         return;
1192 }
1193
1194 /**
1195  * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
1196  * @phba: Pointer to HBA context object.
1197  * @hbqno: HBQ number.
1198  *
1199  * This function is called with hbalock held to get the next
1200  * available slot for the given HBQ. If there is free slot
1201  * available for the HBQ it will return pointer to the next available
1202  * HBQ entry else it will return NULL.
1203  **/
1204 static struct lpfc_hbq_entry *
1205 lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
1206 {
1207         struct hbq_s *hbqp = &phba->hbqs[hbqno];
1208
1209         if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
1210             ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
1211                 hbqp->next_hbqPutIdx = 0;
1212
1213         if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
1214                 uint32_t raw_index = phba->hbq_get[hbqno];
1215                 uint32_t getidx = le32_to_cpu(raw_index);
1216
1217                 hbqp->local_hbqGetIdx = getidx;
1218
1219                 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
1220                         lpfc_printf_log(phba, KERN_ERR,
1221                                         LOG_SLI | LOG_VPORT,
1222                                         "1802 HBQ %d: local_hbqGetIdx "
1223                                         "%u is > than hbqp->entry_count %u\n",
1224                                         hbqno, hbqp->local_hbqGetIdx,
1225                                         hbqp->entry_count);
1226
1227                         phba->link_state = LPFC_HBA_ERROR;
1228                         return NULL;
1229                 }
1230
1231                 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
1232                         return NULL;
1233         }
1234
1235         return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
1236                         hbqp->hbqPutIdx;
1237 }
1238
1239 /**
1240  * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
1241  * @phba: Pointer to HBA context object.
1242  *
1243  * This function is called with no lock held to free all the
1244  * hbq buffers while uninitializing the SLI interface. It also
1245  * frees the HBQ buffers returned by the firmware but not yet
1246  * processed by the upper layers.
1247  **/
1248 void
1249 lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
1250 {
1251         struct lpfc_dmabuf *dmabuf, *next_dmabuf;
1252         struct hbq_dmabuf *hbq_buf;
1253         unsigned long flags;
1254         int i, hbq_count;
1255         uint32_t hbqno;
1256
1257         hbq_count = lpfc_sli_hbq_count();
1258         /* Return all memory used by all HBQs */
1259         spin_lock_irqsave(&phba->hbalock, flags);
1260         for (i = 0; i < hbq_count; ++i) {
1261                 list_for_each_entry_safe(dmabuf, next_dmabuf,
1262                                 &phba->hbqs[i].hbq_buffer_list, list) {
1263                         hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1264                         list_del(&hbq_buf->dbuf.list);
1265                         (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
1266                 }
1267                 phba->hbqs[i].buffer_count = 0;
1268         }
1269         /* Return all HBQ buffer that are in-fly */
1270         list_for_each_entry_safe(dmabuf, next_dmabuf, &phba->rb_pend_list,
1271                                  list) {
1272                 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1273                 list_del(&hbq_buf->dbuf.list);
1274                 if (hbq_buf->tag == -1) {
1275                         (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1276                                 (phba, hbq_buf);
1277                 } else {
1278                         hbqno = hbq_buf->tag >> 16;
1279                         if (hbqno >= LPFC_MAX_HBQS)
1280                                 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1281                                         (phba, hbq_buf);
1282                         else
1283                                 (phba->hbqs[hbqno].hbq_free_buffer)(phba,
1284                                         hbq_buf);
1285                 }
1286         }
1287
1288         /* Mark the HBQs not in use */
1289         phba->hbq_in_use = 0;
1290         spin_unlock_irqrestore(&phba->hbalock, flags);
1291 }
1292
1293 /**
1294  * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
1295  * @phba: Pointer to HBA context object.
1296  * @hbqno: HBQ number.
1297  * @hbq_buf: Pointer to HBQ buffer.
1298  *
1299  * This function is called with the hbalock held to post a
1300  * hbq buffer to the firmware. If the function finds an empty
1301  * slot in the HBQ, it will post the buffer. The function will return
1302  * pointer to the hbq entry if it successfully post the buffer
1303  * else it will return NULL.
1304  **/
1305 static int
1306 lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
1307                          struct hbq_dmabuf *hbq_buf)
1308 {
1309         return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
1310 }
1311
1312 /**
1313  * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
1314  * @phba: Pointer to HBA context object.
1315  * @hbqno: HBQ number.
1316  * @hbq_buf: Pointer to HBQ buffer.
1317  *
1318  * This function is called with the hbalock held to post a hbq buffer to the
1319  * firmware. If the function finds an empty slot in the HBQ, it will post the
1320  * buffer and place it on the hbq_buffer_list. The function will return zero if
1321  * it successfully post the buffer else it will return an error.
1322  **/
1323 static int
1324 lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
1325                             struct hbq_dmabuf *hbq_buf)
1326 {
1327         struct lpfc_hbq_entry *hbqe;
1328         dma_addr_t physaddr = hbq_buf->dbuf.phys;
1329
1330         /* Get next HBQ entry slot to use */
1331         hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
1332         if (hbqe) {
1333                 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1334
1335                 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1336                 hbqe->bde.addrLow  = le32_to_cpu(putPaddrLow(physaddr));
1337                 hbqe->bde.tus.f.bdeSize = hbq_buf->size;
1338                 hbqe->bde.tus.f.bdeFlags = 0;
1339                 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
1340                 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
1341                                 /* Sync SLIM */
1342                 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
1343                 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
1344                                 /* flush */
1345                 readl(phba->hbq_put + hbqno);
1346                 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
1347                 return 0;
1348         } else
1349                 return -ENOMEM;
1350 }
1351
1352 /**
1353  * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
1354  * @phba: Pointer to HBA context object.
1355  * @hbqno: HBQ number.
1356  * @hbq_buf: Pointer to HBQ buffer.
1357  *
1358  * This function is called with the hbalock held to post an RQE to the SLI4
1359  * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
1360  * the hbq_buffer_list and return zero, otherwise it will return an error.
1361  **/
1362 static int
1363 lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
1364                             struct hbq_dmabuf *hbq_buf)
1365 {
1366         int rc;
1367         struct lpfc_rqe hrqe;
1368         struct lpfc_rqe drqe;
1369
1370         hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
1371         hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
1372         drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
1373         drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
1374         rc = lpfc_sli4_rq_put(phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
1375                               &hrqe, &drqe);
1376         if (rc < 0)
1377                 return rc;
1378         hbq_buf->tag = rc;
1379         list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
1380         return 0;
1381 }
1382
1383 /* HBQ for ELS and CT traffic. */
1384 static struct lpfc_hbq_init lpfc_els_hbq = {
1385         .rn = 1,
1386         .entry_count = 200,
1387         .mask_count = 0,
1388         .profile = 0,
1389         .ring_mask = (1 << LPFC_ELS_RING),
1390         .buffer_count = 0,
1391         .init_count = 40,
1392         .add_count = 40,
1393 };
1394
1395 /* HBQ for the extra ring if needed */
1396 static struct lpfc_hbq_init lpfc_extra_hbq = {
1397         .rn = 1,
1398         .entry_count = 200,
1399         .mask_count = 0,
1400         .profile = 0,
1401         .ring_mask = (1 << LPFC_EXTRA_RING),
1402         .buffer_count = 0,
1403         .init_count = 0,
1404         .add_count = 5,
1405 };
1406
1407 /* Array of HBQs */
1408 struct lpfc_hbq_init *lpfc_hbq_defs[] = {
1409         &lpfc_els_hbq,
1410         &lpfc_extra_hbq,
1411 };
1412
1413 /**
1414  * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
1415  * @phba: Pointer to HBA context object.
1416  * @hbqno: HBQ number.
1417  * @count: Number of HBQ buffers to be posted.
1418  *
1419  * This function is called with no lock held to post more hbq buffers to the
1420  * given HBQ. The function returns the number of HBQ buffers successfully
1421  * posted.
1422  **/
1423 static int
1424 lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
1425 {
1426         uint32_t i, posted = 0;
1427         unsigned long flags;
1428         struct hbq_dmabuf *hbq_buffer;
1429         LIST_HEAD(hbq_buf_list);
1430         if (!phba->hbqs[hbqno].hbq_alloc_buffer)
1431                 return 0;
1432
1433         if ((phba->hbqs[hbqno].buffer_count + count) >
1434             lpfc_hbq_defs[hbqno]->entry_count)
1435                 count = lpfc_hbq_defs[hbqno]->entry_count -
1436                                         phba->hbqs[hbqno].buffer_count;
1437         if (!count)
1438                 return 0;
1439         /* Allocate HBQ entries */
1440         for (i = 0; i < count; i++) {
1441                 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
1442                 if (!hbq_buffer)
1443                         break;
1444                 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
1445         }
1446         /* Check whether HBQ is still in use */
1447         spin_lock_irqsave(&phba->hbalock, flags);
1448         if (!phba->hbq_in_use)
1449                 goto err;
1450         while (!list_empty(&hbq_buf_list)) {
1451                 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1452                                  dbuf.list);
1453                 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
1454                                       (hbqno << 16));
1455                 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
1456                         phba->hbqs[hbqno].buffer_count++;
1457                         posted++;
1458                 } else
1459                         (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1460         }
1461         spin_unlock_irqrestore(&phba->hbalock, flags);
1462         return posted;
1463 err:
1464         spin_unlock_irqrestore(&phba->hbalock, flags);
1465         while (!list_empty(&hbq_buf_list)) {
1466                 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1467                                  dbuf.list);
1468                 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1469         }
1470         return 0;
1471 }
1472
1473 /**
1474  * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
1475  * @phba: Pointer to HBA context object.
1476  * @qno: HBQ number.
1477  *
1478  * This function posts more buffers to the HBQ. This function
1479  * is called with no lock held. The function returns the number of HBQ entries
1480  * successfully allocated.
1481  **/
1482 int
1483 lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
1484 {
1485         return(lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1486                                          lpfc_hbq_defs[qno]->add_count));
1487 }
1488
1489 /**
1490  * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
1491  * @phba: Pointer to HBA context object.
1492  * @qno:  HBQ queue number.
1493  *
1494  * This function is called from SLI initialization code path with
1495  * no lock held to post initial HBQ buffers to firmware. The
1496  * function returns the number of HBQ entries successfully allocated.
1497  **/
1498 static int
1499 lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
1500 {
1501         return(lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1502                                          lpfc_hbq_defs[qno]->init_count));
1503 }
1504
1505 /**
1506  * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
1507  * @phba: Pointer to HBA context object.
1508  * @hbqno: HBQ number.
1509  *
1510  * This function removes the first hbq buffer on an hbq list and returns a
1511  * pointer to that buffer. If it finds no buffers on the list it returns NULL.
1512  **/
1513 static struct hbq_dmabuf *
1514 lpfc_sli_hbqbuf_get(struct list_head *rb_list)
1515 {
1516         struct lpfc_dmabuf *d_buf;
1517
1518         list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
1519         if (!d_buf)
1520                 return NULL;
1521         return container_of(d_buf, struct hbq_dmabuf, dbuf);
1522 }
1523
1524 /**
1525  * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
1526  * @phba: Pointer to HBA context object.
1527  * @tag: Tag of the hbq buffer.
1528  *
1529  * This function is called with hbalock held. This function searches
1530  * for the hbq buffer associated with the given tag in the hbq buffer
1531  * list. If it finds the hbq buffer, it returns the hbq_buffer other wise
1532  * it returns NULL.
1533  **/
1534 static struct hbq_dmabuf *
1535 lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
1536 {
1537         struct lpfc_dmabuf *d_buf;
1538         struct hbq_dmabuf *hbq_buf;
1539         uint32_t hbqno;
1540
1541         hbqno = tag >> 16;
1542         if (hbqno >= LPFC_MAX_HBQS)
1543                 return NULL;
1544
1545         spin_lock_irq(&phba->hbalock);
1546         list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
1547                 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
1548                 if (hbq_buf->tag == tag) {
1549                         spin_unlock_irq(&phba->hbalock);
1550                         return hbq_buf;
1551                 }
1552         }
1553         spin_unlock_irq(&phba->hbalock);
1554         lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
1555                         "1803 Bad hbq tag. Data: x%x x%x\n",
1556                         tag, phba->hbqs[tag >> 16].buffer_count);
1557         return NULL;
1558 }
1559
1560 /**
1561  * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
1562  * @phba: Pointer to HBA context object.
1563  * @hbq_buffer: Pointer to HBQ buffer.
1564  *
1565  * This function is called with hbalock. This function gives back
1566  * the hbq buffer to firmware. If the HBQ does not have space to
1567  * post the buffer, it will free the buffer.
1568  **/
1569 void
1570 lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
1571 {
1572         uint32_t hbqno;
1573
1574         if (hbq_buffer) {
1575                 hbqno = hbq_buffer->tag >> 16;
1576                 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
1577                         (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1578         }
1579 }
1580
1581 /**
1582  * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
1583  * @mbxCommand: mailbox command code.
1584  *
1585  * This function is called by the mailbox event handler function to verify
1586  * that the completed mailbox command is a legitimate mailbox command. If the
1587  * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
1588  * and the mailbox event handler will take the HBA offline.
1589  **/
1590 static int
1591 lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
1592 {
1593         uint8_t ret;
1594
1595         switch (mbxCommand) {
1596         case MBX_LOAD_SM:
1597         case MBX_READ_NV:
1598         case MBX_WRITE_NV:
1599         case MBX_WRITE_VPARMS:
1600         case MBX_RUN_BIU_DIAG:
1601         case MBX_INIT_LINK:
1602         case MBX_DOWN_LINK:
1603         case MBX_CONFIG_LINK:
1604         case MBX_CONFIG_RING:
1605         case MBX_RESET_RING:
1606         case MBX_READ_CONFIG:
1607         case MBX_READ_RCONFIG:
1608         case MBX_READ_SPARM:
1609         case MBX_READ_STATUS:
1610         case MBX_READ_RPI:
1611         case MBX_READ_XRI:
1612         case MBX_READ_REV:
1613         case MBX_READ_LNK_STAT:
1614         case MBX_REG_LOGIN:
1615         case MBX_UNREG_LOGIN:
1616         case MBX_READ_LA:
1617         case MBX_CLEAR_LA:
1618         case MBX_DUMP_MEMORY:
1619         case MBX_DUMP_CONTEXT:
1620         case MBX_RUN_DIAGS:
1621         case MBX_RESTART:
1622         case MBX_UPDATE_CFG:
1623         case MBX_DOWN_LOAD:
1624         case MBX_DEL_LD_ENTRY:
1625         case MBX_RUN_PROGRAM:
1626         case MBX_SET_MASK:
1627         case MBX_SET_VARIABLE:
1628         case MBX_UNREG_D_ID:
1629         case MBX_KILL_BOARD:
1630         case MBX_CONFIG_FARP:
1631         case MBX_BEACON:
1632         case MBX_LOAD_AREA:
1633         case MBX_RUN_BIU_DIAG64:
1634         case MBX_CONFIG_PORT:
1635         case MBX_READ_SPARM64:
1636         case MBX_READ_RPI64:
1637         case MBX_REG_LOGIN64:
1638         case MBX_READ_LA64:
1639         case MBX_WRITE_WWN:
1640         case MBX_SET_DEBUG:
1641         case MBX_LOAD_EXP_ROM:
1642         case MBX_ASYNCEVT_ENABLE:
1643         case MBX_REG_VPI:
1644         case MBX_UNREG_VPI:
1645         case MBX_HEARTBEAT:
1646         case MBX_PORT_CAPABILITIES:
1647         case MBX_PORT_IOV_CONTROL:
1648         case MBX_SLI4_CONFIG:
1649         case MBX_SLI4_REQ_FTRS:
1650         case MBX_REG_FCFI:
1651         case MBX_UNREG_FCFI:
1652         case MBX_REG_VFI:
1653         case MBX_UNREG_VFI:
1654         case MBX_INIT_VPI:
1655         case MBX_INIT_VFI:
1656         case MBX_RESUME_RPI:
1657                 ret = mbxCommand;
1658                 break;
1659         default:
1660                 ret = MBX_SHUTDOWN;
1661                 break;
1662         }
1663         return ret;
1664 }
1665
1666 /**
1667  * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
1668  * @phba: Pointer to HBA context object.
1669  * @pmboxq: Pointer to mailbox command.
1670  *
1671  * This is completion handler function for mailbox commands issued from
1672  * lpfc_sli_issue_mbox_wait function. This function is called by the
1673  * mailbox event handler function with no lock held. This function
1674  * will wake up thread waiting on the wait queue pointed by context1
1675  * of the mailbox.
1676  **/
1677 void
1678 lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
1679 {
1680         wait_queue_head_t *pdone_q;
1681         unsigned long drvr_flag;
1682
1683         /*
1684          * If pdone_q is empty, the driver thread gave up waiting and
1685          * continued running.
1686          */
1687         pmboxq->mbox_flag |= LPFC_MBX_WAKE;
1688         spin_lock_irqsave(&phba->hbalock, drvr_flag);
1689         pdone_q = (wait_queue_head_t *) pmboxq->context1;
1690         if (pdone_q)
1691                 wake_up_interruptible(pdone_q);
1692         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
1693         return;
1694 }
1695
1696
1697 /**
1698  * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
1699  * @phba: Pointer to HBA context object.
1700  * @pmb: Pointer to mailbox object.
1701  *
1702  * This function is the default mailbox completion handler. It
1703  * frees the memory resources associated with the completed mailbox
1704  * command. If the completed command is a REG_LOGIN mailbox command,
1705  * this function will issue a UREG_LOGIN to re-claim the RPI.
1706  **/
1707 void
1708 lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
1709 {
1710         struct lpfc_dmabuf *mp;
1711         uint16_t rpi, vpi;
1712         int rc;
1713
1714         mp = (struct lpfc_dmabuf *) (pmb->context1);
1715
1716         if (mp) {
1717                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1718                 kfree(mp);
1719         }
1720
1721         if ((pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) &&
1722             (phba->sli_rev == LPFC_SLI_REV4))
1723                 lpfc_sli4_free_rpi(phba, pmb->u.mb.un.varUnregLogin.rpi);
1724
1725         /*
1726          * If a REG_LOGIN succeeded  after node is destroyed or node
1727          * is in re-discovery driver need to cleanup the RPI.
1728          */
1729         if (!(phba->pport->load_flag & FC_UNLOADING) &&
1730             pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
1731             !pmb->u.mb.mbxStatus) {
1732                 rpi = pmb->u.mb.un.varWords[0];
1733                 vpi = pmb->u.mb.un.varRegLogin.vpi - phba->vpi_base;
1734                 lpfc_unreg_login(phba, vpi, rpi, pmb);
1735                 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
1736                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
1737                 if (rc != MBX_NOT_FINISHED)
1738                         return;
1739         }
1740
1741         if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
1742                 lpfc_sli4_mbox_cmd_free(phba, pmb);
1743         else
1744                 mempool_free(pmb, phba->mbox_mem_pool);
1745 }
1746
1747 /**
1748  * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
1749  * @phba: Pointer to HBA context object.
1750  *
1751  * This function is called with no lock held. This function processes all
1752  * the completed mailbox commands and gives it to upper layers. The interrupt
1753  * service routine processes mailbox completion interrupt and adds completed
1754  * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
1755  * Worker thread call lpfc_sli_handle_mb_event, which will return the
1756  * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
1757  * function returns the mailbox commands to the upper layer by calling the
1758  * completion handler function of each mailbox.
1759  **/
1760 int
1761 lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
1762 {
1763         MAILBOX_t *pmbox;
1764         LPFC_MBOXQ_t *pmb;
1765         int rc;
1766         LIST_HEAD(cmplq);
1767
1768         phba->sli.slistat.mbox_event++;
1769
1770         /* Get all completed mailboxe buffers into the cmplq */
1771         spin_lock_irq(&phba->hbalock);
1772         list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
1773         spin_unlock_irq(&phba->hbalock);
1774
1775         /* Get a Mailbox buffer to setup mailbox commands for callback */
1776         do {
1777                 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
1778                 if (pmb == NULL)
1779                         break;
1780
1781                 pmbox = &pmb->u.mb;
1782
1783                 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
1784                         if (pmb->vport) {
1785                                 lpfc_debugfs_disc_trc(pmb->vport,
1786                                         LPFC_DISC_TRC_MBOX_VPORT,
1787                                         "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
1788                                         (uint32_t)pmbox->mbxCommand,
1789                                         pmbox->un.varWords[0],
1790                                         pmbox->un.varWords[1]);
1791                         }
1792                         else {
1793                                 lpfc_debugfs_disc_trc(phba->pport,
1794                                         LPFC_DISC_TRC_MBOX,
1795                                         "MBOX cmpl:       cmd:x%x mb:x%x x%x",
1796                                         (uint32_t)pmbox->mbxCommand,
1797                                         pmbox->un.varWords[0],
1798                                         pmbox->un.varWords[1]);
1799                         }
1800                 }
1801
1802                 /*
1803                  * It is a fatal error if unknown mbox command completion.
1804                  */
1805                 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
1806                     MBX_SHUTDOWN) {
1807                         /* Unknown mailbox command compl */
1808                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
1809                                         "(%d):0323 Unknown Mailbox command "
1810                                         "x%x (x%x) Cmpl\n",
1811                                         pmb->vport ? pmb->vport->vpi : 0,
1812                                         pmbox->mbxCommand,
1813                                         lpfc_sli4_mbox_opcode_get(phba, pmb));
1814                         phba->link_state = LPFC_HBA_ERROR;
1815                         phba->work_hs = HS_FFER3;
1816                         lpfc_handle_eratt(phba);
1817                         continue;
1818                 }
1819
1820                 if (pmbox->mbxStatus) {
1821                         phba->sli.slistat.mbox_stat_err++;
1822                         if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
1823                                 /* Mbox cmd cmpl error - RETRYing */
1824                                 lpfc_printf_log(phba, KERN_INFO,
1825                                                 LOG_MBOX | LOG_SLI,
1826                                                 "(%d):0305 Mbox cmd cmpl "
1827                                                 "error - RETRYing Data: x%x "
1828                                                 "(x%x) x%x x%x x%x\n",
1829                                                 pmb->vport ? pmb->vport->vpi :0,
1830                                                 pmbox->mbxCommand,
1831                                                 lpfc_sli4_mbox_opcode_get(phba,
1832                                                                           pmb),
1833                                                 pmbox->mbxStatus,
1834                                                 pmbox->un.varWords[0],
1835                                                 pmb->vport->port_state);
1836                                 pmbox->mbxStatus = 0;
1837                                 pmbox->mbxOwner = OWN_HOST;
1838                                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
1839                                 if (rc != MBX_NOT_FINISHED)
1840                                         continue;
1841                         }
1842                 }
1843
1844                 /* Mailbox cmd <cmd> Cmpl <cmpl> */
1845                 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
1846                                 "(%d):0307 Mailbox cmd x%x (x%x) Cmpl x%p "
1847                                 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x\n",
1848                                 pmb->vport ? pmb->vport->vpi : 0,
1849                                 pmbox->mbxCommand,
1850                                 lpfc_sli4_mbox_opcode_get(phba, pmb),
1851                                 pmb->mbox_cmpl,
1852                                 *((uint32_t *) pmbox),
1853                                 pmbox->un.varWords[0],
1854                                 pmbox->un.varWords[1],
1855                                 pmbox->un.varWords[2],
1856                                 pmbox->un.varWords[3],
1857                                 pmbox->un.varWords[4],
1858                                 pmbox->un.varWords[5],
1859                                 pmbox->un.varWords[6],
1860                                 pmbox->un.varWords[7]);
1861
1862                 if (pmb->mbox_cmpl)
1863                         pmb->mbox_cmpl(phba,pmb);
1864         } while (1);
1865         return 0;
1866 }
1867
1868 /**
1869  * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
1870  * @phba: Pointer to HBA context object.
1871  * @pring: Pointer to driver SLI ring object.
1872  * @tag: buffer tag.
1873  *
1874  * This function is called with no lock held. When QUE_BUFTAG_BIT bit
1875  * is set in the tag the buffer is posted for a particular exchange,
1876  * the function will return the buffer without replacing the buffer.
1877  * If the buffer is for unsolicited ELS or CT traffic, this function
1878  * returns the buffer and also posts another buffer to the firmware.
1879  **/
1880 static struct lpfc_dmabuf *
1881 lpfc_sli_get_buff(struct lpfc_hba *phba,
1882                   struct lpfc_sli_ring *pring,
1883                   uint32_t tag)
1884 {
1885         struct hbq_dmabuf *hbq_entry;
1886
1887         if (tag & QUE_BUFTAG_BIT)
1888                 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
1889         hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
1890         if (!hbq_entry)
1891                 return NULL;
1892         return &hbq_entry->dbuf;
1893 }
1894
1895 /**
1896  * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
1897  * @phba: Pointer to HBA context object.
1898  * @pring: Pointer to driver SLI ring object.
1899  * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
1900  * @fch_r_ctl: the r_ctl for the first frame of the sequence.
1901  * @fch_type: the type for the first frame of the sequence.
1902  *
1903  * This function is called with no lock held. This function uses the r_ctl and
1904  * type of the received sequence to find the correct callback function to call
1905  * to process the sequence.
1906  **/
1907 static int
1908 lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1909                          struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
1910                          uint32_t fch_type)
1911 {
1912         int i;
1913
1914         /* unSolicited Responses */
1915         if (pring->prt[0].profile) {
1916                 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
1917                         (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
1918                                                                         saveq);
1919                 return 1;
1920         }
1921         /* We must search, based on rctl / type
1922            for the right routine */
1923         for (i = 0; i < pring->num_mask; i++) {
1924                 if ((pring->prt[i].rctl == fch_r_ctl) &&
1925                     (pring->prt[i].type == fch_type)) {
1926                         if (pring->prt[i].lpfc_sli_rcv_unsol_event)
1927                                 (pring->prt[i].lpfc_sli_rcv_unsol_event)
1928                                                 (phba, pring, saveq);
1929                         return 1;
1930                 }
1931         }
1932         return 0;
1933 }
1934
1935 /**
1936  * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
1937  * @phba: Pointer to HBA context object.
1938  * @pring: Pointer to driver SLI ring object.
1939  * @saveq: Pointer to the unsolicited iocb.
1940  *
1941  * This function is called with no lock held by the ring event handler
1942  * when there is an unsolicited iocb posted to the response ring by the
1943  * firmware. This function gets the buffer associated with the iocbs
1944  * and calls the event handler for the ring. This function handles both
1945  * qring buffers and hbq buffers.
1946  * When the function returns 1 the caller can free the iocb object otherwise
1947  * upper layer functions will free the iocb objects.
1948  **/
1949 static int
1950 lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1951                             struct lpfc_iocbq *saveq)
1952 {
1953         IOCB_t           * irsp;
1954         WORD5            * w5p;
1955         uint32_t           Rctl, Type;
1956         uint32_t           match;
1957         struct lpfc_iocbq *iocbq;
1958         struct lpfc_dmabuf *dmzbuf;
1959
1960         match = 0;
1961         irsp = &(saveq->iocb);
1962
1963         if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
1964                 if (pring->lpfc_sli_rcv_async_status)
1965                         pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
1966                 else
1967                         lpfc_printf_log(phba,
1968                                         KERN_WARNING,
1969                                         LOG_SLI,
1970                                         "0316 Ring %d handler: unexpected "
1971                                         "ASYNC_STATUS iocb received evt_code "
1972                                         "0x%x\n",
1973                                         pring->ringno,
1974                                         irsp->un.asyncstat.evt_code);
1975                 return 1;
1976         }
1977
1978         if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
1979                 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
1980                 if (irsp->ulpBdeCount > 0) {
1981                         dmzbuf = lpfc_sli_get_buff(phba, pring,
1982                                         irsp->un.ulpWord[3]);
1983                         lpfc_in_buf_free(phba, dmzbuf);
1984                 }
1985
1986                 if (irsp->ulpBdeCount > 1) {
1987                         dmzbuf = lpfc_sli_get_buff(phba, pring,
1988                                         irsp->unsli3.sli3Words[3]);
1989                         lpfc_in_buf_free(phba, dmzbuf);
1990                 }
1991
1992                 if (irsp->ulpBdeCount > 2) {
1993                         dmzbuf = lpfc_sli_get_buff(phba, pring,
1994                                 irsp->unsli3.sli3Words[7]);
1995                         lpfc_in_buf_free(phba, dmzbuf);
1996                 }
1997
1998                 return 1;
1999         }
2000
2001         if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
2002                 if (irsp->ulpBdeCount != 0) {
2003                         saveq->context2 = lpfc_sli_get_buff(phba, pring,
2004                                                 irsp->un.ulpWord[3]);
2005                         if (!saveq->context2)
2006                                 lpfc_printf_log(phba,
2007                                         KERN_ERR,
2008                                         LOG_SLI,
2009                                         "0341 Ring %d Cannot find buffer for "
2010                                         "an unsolicited iocb. tag 0x%x\n",
2011                                         pring->ringno,
2012                                         irsp->un.ulpWord[3]);
2013                 }
2014                 if (irsp->ulpBdeCount == 2) {
2015                         saveq->context3 = lpfc_sli_get_buff(phba, pring,
2016                                                 irsp->unsli3.sli3Words[7]);
2017                         if (!saveq->context3)
2018                                 lpfc_printf_log(phba,
2019                                         KERN_ERR,
2020                                         LOG_SLI,
2021                                         "0342 Ring %d Cannot find buffer for an"
2022                                         " unsolicited iocb. tag 0x%x\n",
2023                                         pring->ringno,
2024                                         irsp->unsli3.sli3Words[7]);
2025                 }
2026                 list_for_each_entry(iocbq, &saveq->list, list) {
2027                         irsp = &(iocbq->iocb);
2028                         if (irsp->ulpBdeCount != 0) {
2029                                 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
2030                                                         irsp->un.ulpWord[3]);
2031                                 if (!iocbq->context2)
2032                                         lpfc_printf_log(phba,
2033                                                 KERN_ERR,
2034                                                 LOG_SLI,
2035                                                 "0343 Ring %d Cannot find "
2036                                                 "buffer for an unsolicited iocb"
2037                                                 ". tag 0x%x\n", pring->ringno,
2038                                                 irsp->un.ulpWord[3]);
2039                         }
2040                         if (irsp->ulpBdeCount == 2) {
2041                                 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
2042                                                 irsp->unsli3.sli3Words[7]);
2043                                 if (!iocbq->context3)
2044                                         lpfc_printf_log(phba,
2045                                                 KERN_ERR,
2046                                                 LOG_SLI,
2047                                                 "0344 Ring %d Cannot find "
2048                                                 "buffer for an unsolicited "
2049                                                 "iocb. tag 0x%x\n",
2050                                                 pring->ringno,
2051                                                 irsp->unsli3.sli3Words[7]);
2052                         }
2053                 }
2054         }
2055         if (irsp->ulpBdeCount != 0 &&
2056             (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
2057              irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
2058                 int found = 0;
2059
2060                 /* search continue save q for same XRI */
2061                 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
2062                         if (iocbq->iocb.ulpContext == saveq->iocb.ulpContext) {
2063                                 list_add_tail(&saveq->list, &iocbq->list);
2064                                 found = 1;
2065                                 break;
2066                         }
2067                 }
2068                 if (!found)
2069                         list_add_tail(&saveq->clist,
2070                                       &pring->iocb_continue_saveq);
2071                 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
2072                         list_del_init(&iocbq->clist);
2073                         saveq = iocbq;
2074                         irsp = &(saveq->iocb);
2075                 } else
2076                         return 0;
2077         }
2078         if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
2079             (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
2080             (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
2081                 Rctl = FC_RCTL_ELS_REQ;
2082                 Type = FC_TYPE_ELS;
2083         } else {
2084                 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
2085                 Rctl = w5p->hcsw.Rctl;
2086                 Type = w5p->hcsw.Type;
2087
2088                 /* Firmware Workaround */
2089                 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
2090                         (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
2091                          irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
2092                         Rctl = FC_RCTL_ELS_REQ;
2093                         Type = FC_TYPE_ELS;
2094                         w5p->hcsw.Rctl = Rctl;
2095                         w5p->hcsw.Type = Type;
2096                 }
2097         }
2098
2099         if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
2100                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
2101                                 "0313 Ring %d handler: unexpected Rctl x%x "
2102                                 "Type x%x received\n",
2103                                 pring->ringno, Rctl, Type);
2104
2105         return 1;
2106 }
2107
2108 /**
2109  * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
2110  * @phba: Pointer to HBA context object.
2111  * @pring: Pointer to driver SLI ring object.
2112  * @prspiocb: Pointer to response iocb object.
2113  *
2114  * This function looks up the iocb_lookup table to get the command iocb
2115  * corresponding to the given response iocb using the iotag of the
2116  * response iocb. This function is called with the hbalock held.
2117  * This function returns the command iocb object if it finds the command
2118  * iocb else returns NULL.
2119  **/
2120 static struct lpfc_iocbq *
2121 lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
2122                       struct lpfc_sli_ring *pring,
2123                       struct lpfc_iocbq *prspiocb)
2124 {
2125         struct lpfc_iocbq *cmd_iocb = NULL;
2126         uint16_t iotag;
2127
2128         iotag = prspiocb->iocb.ulpIoTag;
2129
2130         if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2131                 cmd_iocb = phba->sli.iocbq_lookup[iotag];
2132                 list_del_init(&cmd_iocb->list);
2133                 pring->txcmplq_cnt--;
2134                 return cmd_iocb;
2135         }
2136
2137         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2138                         "0317 iotag x%x is out off "
2139                         "range: max iotag x%x wd0 x%x\n",
2140                         iotag, phba->sli.last_iotag,
2141                         *(((uint32_t *) &prspiocb->iocb) + 7));
2142         return NULL;
2143 }
2144
2145 /**
2146  * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
2147  * @phba: Pointer to HBA context object.
2148  * @pring: Pointer to driver SLI ring object.
2149  * @iotag: IOCB tag.
2150  *
2151  * This function looks up the iocb_lookup table to get the command iocb
2152  * corresponding to the given iotag. This function is called with the
2153  * hbalock held.
2154  * This function returns the command iocb object if it finds the command
2155  * iocb else returns NULL.
2156  **/
2157 static struct lpfc_iocbq *
2158 lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
2159                              struct lpfc_sli_ring *pring, uint16_t iotag)
2160 {
2161         struct lpfc_iocbq *cmd_iocb;
2162
2163         if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2164                 cmd_iocb = phba->sli.iocbq_lookup[iotag];
2165                 list_del_init(&cmd_iocb->list);
2166                 pring->txcmplq_cnt--;
2167                 return cmd_iocb;
2168         }
2169
2170         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2171                         "0372 iotag x%x is out off range: max iotag (x%x)\n",
2172                         iotag, phba->sli.last_iotag);
2173         return NULL;
2174 }
2175
2176 /**
2177  * lpfc_sli_process_sol_iocb - process solicited iocb completion
2178  * @phba: Pointer to HBA context object.
2179  * @pring: Pointer to driver SLI ring object.
2180  * @saveq: Pointer to the response iocb to be processed.
2181  *
2182  * This function is called by the ring event handler for non-fcp
2183  * rings when there is a new response iocb in the response ring.
2184  * The caller is not required to hold any locks. This function
2185  * gets the command iocb associated with the response iocb and
2186  * calls the completion handler for the command iocb. If there
2187  * is no completion handler, the function will free the resources
2188  * associated with command iocb. If the response iocb is for
2189  * an already aborted command iocb, the status of the completion
2190  * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
2191  * This function always returns 1.
2192  **/
2193 static int
2194 lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2195                           struct lpfc_iocbq *saveq)
2196 {
2197         struct lpfc_iocbq *cmdiocbp;
2198         int rc = 1;
2199         unsigned long iflag;
2200
2201         /* Based on the iotag field, get the cmd IOCB from the txcmplq */
2202         spin_lock_irqsave(&phba->hbalock, iflag);
2203         cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
2204         spin_unlock_irqrestore(&phba->hbalock, iflag);
2205
2206         if (cmdiocbp) {
2207                 if (cmdiocbp->iocb_cmpl) {
2208                         /*
2209                          * If an ELS command failed send an event to mgmt
2210                          * application.
2211                          */
2212                         if (saveq->iocb.ulpStatus &&
2213                              (pring->ringno == LPFC_ELS_RING) &&
2214                              (cmdiocbp->iocb.ulpCommand ==
2215                                 CMD_ELS_REQUEST64_CR))
2216                                 lpfc_send_els_failure_event(phba,
2217                                         cmdiocbp, saveq);
2218
2219                         /*
2220                          * Post all ELS completions to the worker thread.
2221                          * All other are passed to the completion callback.
2222                          */
2223                         if (pring->ringno == LPFC_ELS_RING) {
2224                                 if (cmdiocbp->iocb_flag & LPFC_DRIVER_ABORTED) {
2225                                         cmdiocbp->iocb_flag &=
2226                                                 ~LPFC_DRIVER_ABORTED;
2227                                         saveq->iocb.ulpStatus =
2228                                                 IOSTAT_LOCAL_REJECT;
2229                                         saveq->iocb.un.ulpWord[4] =
2230                                                 IOERR_SLI_ABORTED;
2231
2232                                         /* Firmware could still be in progress
2233                                          * of DMAing payload, so don't free data
2234                                          * buffer till after a hbeat.
2235                                          */
2236                                         saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
2237                                 }
2238                         }
2239                         (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
2240                 } else
2241                         lpfc_sli_release_iocbq(phba, cmdiocbp);
2242         } else {
2243                 /*
2244                  * Unknown initiating command based on the response iotag.
2245                  * This could be the case on the ELS ring because of
2246                  * lpfc_els_abort().
2247                  */
2248                 if (pring->ringno != LPFC_ELS_RING) {
2249                         /*
2250                          * Ring <ringno> handler: unexpected completion IoTag
2251                          * <IoTag>
2252                          */
2253                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
2254                                          "0322 Ring %d handler: "
2255                                          "unexpected completion IoTag x%x "
2256                                          "Data: x%x x%x x%x x%x\n",
2257                                          pring->ringno,
2258                                          saveq->iocb.ulpIoTag,
2259                                          saveq->iocb.ulpStatus,
2260                                          saveq->iocb.un.ulpWord[4],
2261                                          saveq->iocb.ulpCommand,
2262                                          saveq->iocb.ulpContext);
2263                 }
2264         }
2265
2266         return rc;
2267 }
2268
2269 /**
2270  * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
2271  * @phba: Pointer to HBA context object.
2272  * @pring: Pointer to driver SLI ring object.
2273  *
2274  * This function is called from the iocb ring event handlers when
2275  * put pointer is ahead of the get pointer for a ring. This function signal
2276  * an error attention condition to the worker thread and the worker
2277  * thread will transition the HBA to offline state.
2278  **/
2279 static void
2280 lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
2281 {
2282         struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
2283         /*
2284          * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
2285          * rsp ring <portRspMax>
2286          */
2287         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2288                         "0312 Ring %d handler: portRspPut %d "
2289                         "is bigger than rsp ring %d\n",
2290                         pring->ringno, le32_to_cpu(pgp->rspPutInx),
2291                         pring->numRiocb);
2292
2293         phba->link_state = LPFC_HBA_ERROR;
2294
2295         /*
2296          * All error attention handlers are posted to
2297          * worker thread
2298          */
2299         phba->work_ha |= HA_ERATT;
2300         phba->work_hs = HS_FFER3;
2301
2302         lpfc_worker_wake_up(phba);
2303
2304         return;
2305 }
2306
2307 /**
2308  * lpfc_poll_eratt - Error attention polling timer timeout handler
2309  * @ptr: Pointer to address of HBA context object.
2310  *
2311  * This function is invoked by the Error Attention polling timer when the
2312  * timer times out. It will check the SLI Error Attention register for
2313  * possible attention events. If so, it will post an Error Attention event
2314  * and wake up worker thread to process it. Otherwise, it will set up the
2315  * Error Attention polling timer for the next poll.
2316  **/
2317 void lpfc_poll_eratt(unsigned long ptr)
2318 {
2319         struct lpfc_hba *phba;
2320         uint32_t eratt = 0;
2321
2322         phba = (struct lpfc_hba *)ptr;
2323
2324         /* Check chip HA register for error event */
2325         eratt = lpfc_sli_check_eratt(phba);
2326
2327         if (eratt)
2328                 /* Tell the worker thread there is work to do */
2329                 lpfc_worker_wake_up(phba);
2330         else
2331                 /* Restart the timer for next eratt poll */
2332                 mod_timer(&phba->eratt_poll, jiffies +
2333                                         HZ * LPFC_ERATT_POLL_INTERVAL);
2334         return;
2335 }
2336
2337
2338 /**
2339  * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
2340  * @phba: Pointer to HBA context object.
2341  * @pring: Pointer to driver SLI ring object.
2342  * @mask: Host attention register mask for this ring.
2343  *
2344  * This function is called from the interrupt context when there is a ring
2345  * event for the fcp ring. The caller does not hold any lock.
2346  * The function processes each response iocb in the response ring until it
2347  * finds an iocb with LE bit set and chains all the iocbs upto the iocb with
2348  * LE bit set. The function will call the completion handler of the command iocb
2349  * if the response iocb indicates a completion for a command iocb or it is
2350  * an abort completion. The function will call lpfc_sli_process_unsol_iocb
2351  * function if this is an unsolicited iocb.
2352  * This routine presumes LPFC_FCP_RING handling and doesn't bother
2353  * to check it explicitly.
2354  */
2355 int
2356 lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
2357                                 struct lpfc_sli_ring *pring, uint32_t mask)
2358 {
2359         struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
2360         IOCB_t *irsp = NULL;
2361         IOCB_t *entry = NULL;
2362         struct lpfc_iocbq *cmdiocbq = NULL;
2363         struct lpfc_iocbq rspiocbq;
2364         uint32_t status;
2365         uint32_t portRspPut, portRspMax;
2366         int rc = 1;
2367         lpfc_iocb_type type;
2368         unsigned long iflag;
2369         uint32_t rsp_cmpl = 0;
2370
2371         spin_lock_irqsave(&phba->hbalock, iflag);
2372         pring->stats.iocb_event++;
2373
2374         /*
2375          * The next available response entry should never exceed the maximum
2376          * entries.  If it does, treat it as an adapter hardware error.
2377          */
2378         portRspMax = pring->numRiocb;
2379         portRspPut = le32_to_cpu(pgp->rspPutInx);
2380         if (unlikely(portRspPut >= portRspMax)) {
2381                 lpfc_sli_rsp_pointers_error(phba, pring);
2382                 spin_unlock_irqrestore(&phba->hbalock, iflag);
2383                 return 1;
2384         }
2385         if (phba->fcp_ring_in_use) {
2386                 spin_unlock_irqrestore(&phba->hbalock, iflag);
2387                 return 1;
2388         } else
2389                 phba->fcp_ring_in_use = 1;
2390
2391         rmb();
2392         while (pring->rspidx != portRspPut) {
2393                 /*
2394                  * Fetch an entry off the ring and copy it into a local data
2395                  * structure.  The copy involves a byte-swap since the
2396                  * network byte order and pci byte orders are different.
2397                  */
2398                 entry = lpfc_resp_iocb(phba, pring);
2399                 phba->last_completion_time = jiffies;
2400
2401                 if (++pring->rspidx >= portRspMax)
2402                         pring->rspidx = 0;
2403
2404                 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
2405                                       (uint32_t *) &rspiocbq.iocb,
2406                                       phba->iocb_rsp_size);
2407                 INIT_LIST_HEAD(&(rspiocbq.list));
2408                 irsp = &rspiocbq.iocb;
2409
2410                 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
2411                 pring->stats.iocb_rsp++;
2412                 rsp_cmpl++;
2413
2414                 if (unlikely(irsp->ulpStatus)) {
2415                         /*
2416                          * If resource errors reported from HBA, reduce
2417                          * queuedepths of the SCSI device.
2418                          */
2419                         if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
2420                                 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
2421                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
2422                                 phba->lpfc_rampdown_queue_depth(phba);
2423                                 spin_lock_irqsave(&phba->hbalock, iflag);
2424                         }
2425
2426                         /* Rsp ring <ringno> error: IOCB */
2427                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
2428                                         "0336 Rsp Ring %d error: IOCB Data: "
2429                                         "x%x x%x x%x x%x x%x x%x x%x x%x\n",
2430                                         pring->ringno,
2431                                         irsp->un.ulpWord[0],
2432                                         irsp->un.ulpWord[1],
2433                                         irsp->un.ulpWord[2],
2434                                         irsp->un.ulpWord[3],
2435                                         irsp->un.ulpWord[4],
2436                                         irsp->un.ulpWord[5],
2437                                         *(uint32_t *)&irsp->un1,
2438                                         *((uint32_t *)&irsp->un1 + 1));
2439                 }
2440
2441                 switch (type) {
2442                 case LPFC_ABORT_IOCB:
2443                 case LPFC_SOL_IOCB:
2444                         /*
2445                          * Idle exchange closed via ABTS from port.  No iocb
2446                          * resources need to be recovered.
2447                          */
2448                         if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
2449                                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
2450                                                 "0333 IOCB cmd 0x%x"
2451                                                 " processed. Skipping"
2452                                                 " completion\n",
2453                                                 irsp->ulpCommand);
2454                                 break;
2455                         }
2456
2457                         cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
2458                                                          &rspiocbq);
2459                         if ((cmdiocbq) && (cmdiocbq->iocb_cmpl)) {
2460                                         spin_unlock_irqrestore(&phba->hbalock,
2461                                                                iflag);
2462                                         (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
2463                                                               &rspiocbq);
2464                                         spin_lock_irqsave(&phba->hbalock,
2465                                                           iflag);
2466                                 }
2467                         break;
2468                 case LPFC_UNSOL_IOCB:
2469                         spin_unlock_irqrestore(&phba->hbalock, iflag);
2470                         lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
2471                         spin_lock_irqsave(&phba->hbalock, iflag);
2472                         break;
2473                 default:
2474                         if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
2475                                 char adaptermsg[LPFC_MAX_ADPTMSG];
2476                                 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
2477                                 memcpy(&adaptermsg[0], (uint8_t *) irsp,
2478                                        MAX_MSG_DATA);
2479                                 dev_warn(&((phba->pcidev)->dev),
2480                                          "lpfc%d: %s\n",
2481                                          phba->brd_no, adaptermsg);
2482                         } else {
2483                                 /* Unknown IOCB command */
2484                                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2485                                                 "0334 Unknown IOCB command "
2486                                                 "Data: x%x, x%x x%x x%x x%x\n",
2487                                                 type, irsp->ulpCommand,
2488                                                 irsp->ulpStatus,
2489                                                 irsp->ulpIoTag,
2490                                                 irsp->ulpContext);
2491                         }
2492                         break;
2493                 }
2494
2495                 /*
2496                  * The response IOCB has been processed.  Update the ring
2497                  * pointer in SLIM.  If the port response put pointer has not
2498                  * been updated, sync the pgp->rspPutInx and fetch the new port
2499                  * response put pointer.
2500                  */
2501                 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
2502
2503                 if (pring->rspidx == portRspPut)
2504                         portRspPut = le32_to_cpu(pgp->rspPutInx);
2505         }
2506
2507         if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
2508                 pring->stats.iocb_rsp_full++;
2509                 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
2510                 writel(status, phba->CAregaddr);
2511                 readl(phba->CAregaddr);
2512         }
2513         if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
2514                 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
2515                 pring->stats.iocb_cmd_empty++;
2516
2517                 /* Force update of the local copy of cmdGetInx */
2518                 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
2519                 lpfc_sli_resume_iocb(phba, pring);
2520
2521                 if ((pring->lpfc_sli_cmd_available))
2522                         (pring->lpfc_sli_cmd_available) (phba, pring);
2523
2524         }
2525
2526         phba->fcp_ring_in_use = 0;
2527         spin_unlock_irqrestore(&phba->hbalock, iflag);
2528         return rc;
2529 }
2530
2531 /**
2532  * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
2533  * @phba: Pointer to HBA context object.
2534  * @pring: Pointer to driver SLI ring object.
2535  * @rspiocbp: Pointer to driver response IOCB object.
2536  *
2537  * This function is called from the worker thread when there is a slow-path
2538  * response IOCB to process. This function chains all the response iocbs until
2539  * seeing the iocb with the LE bit set. The function will call
2540  * lpfc_sli_process_sol_iocb function if the response iocb indicates a
2541  * completion of a command iocb. The function will call the
2542  * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
2543  * The function frees the resources or calls the completion handler if this
2544  * iocb is an abort completion. The function returns NULL when the response
2545  * iocb has the LE bit set and all the chained iocbs are processed, otherwise
2546  * this function shall chain the iocb on to the iocb_continueq and return the
2547  * response iocb passed in.
2548  **/
2549 static struct lpfc_iocbq *
2550 lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2551                         struct lpfc_iocbq *rspiocbp)
2552 {
2553         struct lpfc_iocbq *saveq;
2554         struct lpfc_iocbq *cmdiocbp;
2555         struct lpfc_iocbq *next_iocb;
2556         IOCB_t *irsp = NULL;
2557         uint32_t free_saveq;
2558         uint8_t iocb_cmd_type;
2559         lpfc_iocb_type type;
2560         unsigned long iflag;
2561         int rc;
2562
2563         spin_lock_irqsave(&phba->hbalock, iflag);
2564         /* First add the response iocb to the countinueq list */
2565         list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
2566         pring->iocb_continueq_cnt++;
2567
2568         /* Now, determine whetehr the list is completed for processing */
2569         irsp = &rspiocbp->iocb;
2570         if (irsp->ulpLe) {
2571                 /*
2572                  * By default, the driver expects to free all resources
2573                  * associated with this iocb completion.
2574                  */
2575                 free_saveq = 1;
2576                 saveq = list_get_first(&pring->iocb_continueq,
2577                                        struct lpfc_iocbq, list);
2578                 irsp = &(saveq->iocb);
2579                 list_del_init(&pring->iocb_continueq);
2580                 pring->iocb_continueq_cnt = 0;
2581
2582                 pring->stats.iocb_rsp++;
2583
2584                 /*
2585                  * If resource errors reported from HBA, reduce
2586                  * queuedepths of the SCSI device.
2587                  */
2588                 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
2589                     (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
2590                         spin_unlock_irqrestore(&phba->hbalock, iflag);
2591                         phba->lpfc_rampdown_queue_depth(phba);
2592                         spin_lock_irqsave(&phba->hbalock, iflag);
2593                 }
2594
2595                 if (irsp->ulpStatus) {
2596                         /* Rsp ring <ringno> error: IOCB */
2597                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
2598                                         "0328 Rsp Ring %d error: "
2599                                         "IOCB Data: "
2600                                         "x%x x%x x%x x%x "
2601                                         "x%x x%x x%x x%x "
2602                                         "x%x x%x x%x x%x "
2603                                         "x%x x%x x%x x%x\n",
2604                                         pring->ringno,
2605                                         irsp->un.ulpWord[0],
2606                                         irsp->un.ulpWord[1],
2607                                         irsp->un.ulpWord[2],
2608                                         irsp->un.ulpWord[3],
2609                                         irsp->un.ulpWord[4],
2610                                         irsp->un.ulpWord[5],
2611                                         *(((uint32_t *) irsp) + 6),
2612                                         *(((uint32_t *) irsp) + 7),
2613                                         *(((uint32_t *) irsp) + 8),
2614                                         *(((uint32_t *) irsp) + 9),
2615                                         *(((uint32_t *) irsp) + 10),
2616                                         *(((uint32_t *) irsp) + 11),
2617                                         *(((uint32_t *) irsp) + 12),
2618                                         *(((uint32_t *) irsp) + 13),
2619                                         *(((uint32_t *) irsp) + 14),
2620                                         *(((uint32_t *) irsp) + 15));
2621                 }
2622
2623                 /*
2624                  * Fetch the IOCB command type and call the correct completion
2625                  * routine. Solicited and Unsolicited IOCBs on the ELS ring
2626                  * get freed back to the lpfc_iocb_list by the discovery
2627                  * kernel thread.
2628                  */
2629                 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
2630                 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
2631                 switch (type) {
2632                 case LPFC_SOL_IOCB:
2633                         spin_unlock_irqrestore(&phba->hbalock, iflag);
2634                         rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
2635                         spin_lock_irqsave(&phba->hbalock, iflag);
2636                         break;
2637
2638                 case LPFC_UNSOL_IOCB:
2639                         spin_unlock_irqrestore(&phba->hbalock, iflag);
2640                         rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
2641                         spin_lock_irqsave(&phba->hbalock, iflag);
2642                         if (!rc)
2643                                 free_saveq = 0;
2644                         break;
2645
2646                 case LPFC_ABORT_IOCB:
2647                         cmdiocbp = NULL;
2648                         if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
2649                                 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
2650                                                                  saveq);
2651                         if (cmdiocbp) {
2652                                 /* Call the specified completion routine */
2653                                 if (cmdiocbp->iocb_cmpl) {
2654                                         spin_unlock_irqrestore(&phba->hbalock,
2655                                                                iflag);
2656                                         (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
2657                                                               saveq);
2658                                         spin_lock_irqsave(&phba->hbalock,
2659                                                           iflag);
2660                                 } else
2661                                         __lpfc_sli_release_iocbq(phba,
2662                                                                  cmdiocbp);
2663                         }
2664                         break;
2665
2666                 case LPFC_UNKNOWN_IOCB:
2667                         if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
2668                                 char adaptermsg[LPFC_MAX_ADPTMSG];
2669                                 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
2670                                 memcpy(&adaptermsg[0], (uint8_t *)irsp,
2671                                        MAX_MSG_DATA);
2672                                 dev_warn(&((phba->pcidev)->dev),
2673                                          "lpfc%d: %s\n",
2674                                          phba->brd_no, adaptermsg);
2675                         } else {
2676                                 /* Unknown IOCB command */
2677                                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2678                                                 "0335 Unknown IOCB "
2679                                                 "command Data: x%x "
2680                                                 "x%x x%x x%x\n",
2681                                                 irsp->ulpCommand,
2682                                                 irsp->ulpStatus,
2683                                                 irsp->ulpIoTag,
2684                                                 irsp->ulpContext);
2685                         }
2686                         break;
2687                 }
2688
2689                 if (free_saveq) {
2690                         list_for_each_entry_safe(rspiocbp, next_iocb,
2691                                                  &saveq->list, list) {
2692                                 list_del(&rspiocbp->list);
2693                                 __lpfc_sli_release_iocbq(phba, rspiocbp);
2694                         }
2695                         __lpfc_sli_release_iocbq(phba, saveq);
2696                 }
2697                 rspiocbp = NULL;
2698         }
2699         spin_unlock_irqrestore(&phba->hbalock, iflag);
2700         return rspiocbp;
2701 }
2702
2703 /**
2704  * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
2705  * @phba: Pointer to HBA context object.
2706  * @pring: Pointer to driver SLI ring object.
2707  * @mask: Host attention register mask for this ring.
2708  *
2709  * This routine wraps the actual slow_ring event process routine from the
2710  * API jump table function pointer from the lpfc_hba struct.
2711  **/
2712 void
2713 lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
2714                                 struct lpfc_sli_ring *pring, uint32_t mask)
2715 {
2716         phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
2717 }
2718
2719 /**
2720  * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
2721  * @phba: Pointer to HBA context object.
2722  * @pring: Pointer to driver SLI ring object.
2723  * @mask: Host attention register mask for this ring.
2724  *
2725  * This function is called from the worker thread when there is a ring event
2726  * for non-fcp rings. The caller does not hold any lock. The function will
2727  * remove each response iocb in the response ring and calls the handle
2728  * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
2729  **/
2730 static void
2731 lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
2732                                    struct lpfc_sli_ring *pring, uint32_t mask)
2733 {
2734         struct lpfc_pgp *pgp;
2735         IOCB_t *entry;
2736         IOCB_t *irsp = NULL;
2737         struct lpfc_iocbq *rspiocbp = NULL;
2738         uint32_t portRspPut, portRspMax;
2739         unsigned long iflag;
2740         uint32_t status;
2741
2742         pgp = &phba->port_gp[pring->ringno];
2743         spin_lock_irqsave(&phba->hbalock, iflag);
2744         pring->stats.iocb_event++;
2745
2746         /*
2747          * The next available response entry should never exceed the maximum
2748          * entries.  If it does, treat it as an adapter hardware error.
2749          */
2750         portRspMax = pring->numRiocb;
2751         portRspPut = le32_to_cpu(pgp->rspPutInx);
2752         if (portRspPut >= portRspMax) {
2753                 /*
2754                  * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
2755                  * rsp ring <portRspMax>
2756                  */
2757                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2758                                 "0303 Ring %d handler: portRspPut %d "
2759                                 "is bigger than rsp ring %d\n",
2760                                 pring->ringno, portRspPut, portRspMax);
2761
2762                 phba->link_state = LPFC_HBA_ERROR;
2763                 spin_unlock_irqrestore(&phba->hbalock, iflag);
2764
2765                 phba->work_hs = HS_FFER3;
2766                 lpfc_handle_eratt(phba);
2767
2768                 return;
2769         }
2770
2771         rmb();
2772         while (pring->rspidx != portRspPut) {
2773                 /*
2774                  * Build a completion list and call the appropriate handler.
2775                  * The process is to get the next available response iocb, get
2776                  * a free iocb from the list, copy the response data into the
2777                  * free iocb, insert to the continuation list, and update the
2778                  * next response index to slim.  This process makes response
2779                  * iocb's in the ring available to DMA as fast as possible but
2780                  * pays a penalty for a copy operation.  Since the iocb is
2781                  * only 32 bytes, this penalty is considered small relative to
2782                  * the PCI reads for register values and a slim write.  When
2783                  * the ulpLe field is set, the entire Command has been
2784                  * received.
2785                  */
2786                 entry = lpfc_resp_iocb(phba, pring);
2787
2788                 phba->last_completion_time = jiffies;
2789                 rspiocbp = __lpfc_sli_get_iocbq(phba);
2790                 if (rspiocbp == NULL) {
2791                         printk(KERN_ERR "%s: out of buffers! Failing "
2792                                "completion.\n", __func__);
2793                         break;
2794                 }
2795
2796                 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
2797                                       phba->iocb_rsp_size);
2798                 irsp = &rspiocbp->iocb;
2799
2800                 if (++pring->rspidx >= portRspMax)
2801                         pring->rspidx = 0;
2802
2803                 if (pring->ringno == LPFC_ELS_RING) {
2804                         lpfc_debugfs_slow_ring_trc(phba,
2805                         "IOCB rsp ring:   wd4:x%08x wd6:x%08x wd7:x%08x",
2806                                 *(((uint32_t *) irsp) + 4),
2807                                 *(((uint32_t *) irsp) + 6),
2808                                 *(((uint32_t *) irsp) + 7));
2809                 }
2810
2811                 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
2812
2813                 spin_unlock_irqrestore(&phba->hbalock, iflag);
2814                 /* Handle the response IOCB */
2815                 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
2816                 spin_lock_irqsave(&phba->hbalock, iflag);
2817
2818                 /*
2819                  * If the port response put pointer has not been updated, sync
2820                  * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
2821                  * response put pointer.
2822                  */
2823                 if (pring->rspidx == portRspPut) {
2824                         portRspPut = le32_to_cpu(pgp->rspPutInx);
2825                 }
2826         } /* while (pring->rspidx != portRspPut) */
2827
2828         if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
2829                 /* At least one response entry has been freed */
2830                 pring->stats.iocb_rsp_full++;
2831                 /* SET RxRE_RSP in Chip Att register */
2832                 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
2833                 writel(status, phba->CAregaddr);
2834                 readl(phba->CAregaddr); /* flush */
2835         }
2836         if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
2837                 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
2838                 pring->stats.iocb_cmd_empty++;
2839
2840                 /* Force update of the local copy of cmdGetInx */
2841                 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
2842                 lpfc_sli_resume_iocb(phba, pring);
2843
2844                 if ((pring->lpfc_sli_cmd_available))
2845                         (pring->lpfc_sli_cmd_available) (phba, pring);
2846
2847         }
2848
2849         spin_unlock_irqrestore(&phba->hbalock, iflag);
2850         return;
2851 }
2852
2853 /**
2854  * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
2855  * @phba: Pointer to HBA context object.
2856  * @pring: Pointer to driver SLI ring object.
2857  * @mask: Host attention register mask for this ring.
2858  *
2859  * This function is called from the worker thread when there is a pending
2860  * ELS response iocb on the driver internal slow-path response iocb worker
2861  * queue. The caller does not hold any lock. The function will remove each
2862  * response iocb from the response worker queue and calls the handle
2863  * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
2864  **/
2865 static void
2866 lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
2867                                    struct lpfc_sli_ring *pring, uint32_t mask)
2868 {
2869         struct lpfc_iocbq *irspiocbq;
2870         struct hbq_dmabuf *dmabuf;
2871         struct lpfc_cq_event *cq_event;
2872         unsigned long iflag;
2873
2874         spin_lock_irqsave(&phba->hbalock, iflag);
2875         phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
2876         spin_unlock_irqrestore(&phba->hbalock, iflag);
2877         while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
2878                 /* Get the response iocb from the head of work queue */
2879                 spin_lock_irqsave(&phba->hbalock, iflag);
2880                 list_remove_head(&phba->sli4_hba.sp_queue_event,
2881                                  cq_event, struct lpfc_cq_event, list);
2882                 spin_unlock_irqrestore(&phba->hbalock, iflag);
2883
2884                 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
2885                 case CQE_CODE_COMPL_WQE:
2886                         irspiocbq = container_of(cq_event, struct lpfc_iocbq,
2887                                                  cq_event);
2888                         /* Translate ELS WCQE to response IOCBQ */
2889                         irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
2890                                                                    irspiocbq);
2891                         if (irspiocbq)
2892                                 lpfc_sli_sp_handle_rspiocb(phba, pring,
2893                                                            irspiocbq);
2894                         break;
2895                 case CQE_CODE_RECEIVE:
2896                         dmabuf = container_of(cq_event, struct hbq_dmabuf,
2897                                               cq_event);
2898                         lpfc_sli4_handle_received_buffer(phba, dmabuf);
2899                         break;
2900                 default:
2901                         break;
2902                 }
2903         }
2904 }
2905
2906 /**
2907  * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
2908  * @phba: Pointer to HBA context object.
2909  * @pring: Pointer to driver SLI ring object.
2910  *
2911  * This function aborts all iocbs in the given ring and frees all the iocb
2912  * objects in txq. This function issues an abort iocb for all the iocb commands
2913  * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
2914  * the return of this function. The caller is not required to hold any locks.
2915  **/
2916 void
2917 lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
2918 {
2919         LIST_HEAD(completions);
2920         struct lpfc_iocbq *iocb, *next_iocb;
2921
2922         if (pring->ringno == LPFC_ELS_RING) {
2923                 lpfc_fabric_abort_hba(phba);
2924         }
2925
2926         /* Error everything on txq and txcmplq
2927          * First do the txq.
2928          */
2929         spin_lock_irq(&phba->hbalock);
2930         list_splice_init(&pring->txq, &completions);
2931         pring->txq_cnt = 0;
2932
2933         /* Next issue ABTS for everything on the txcmplq */
2934         list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
2935                 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
2936
2937         spin_unlock_irq(&phba->hbalock);
2938
2939         /* Cancel all the IOCBs from the completions list */
2940         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
2941                               IOERR_SLI_ABORTED);
2942 }
2943
2944 /**
2945  * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
2946  * @phba: Pointer to HBA context object.
2947  *
2948  * This function flushes all iocbs in the fcp ring and frees all the iocb
2949  * objects in txq and txcmplq. This function will not issue abort iocbs
2950  * for all the iocb commands in txcmplq, they will just be returned with
2951  * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
2952  * slot has been permanently disabled.
2953  **/
2954 void
2955 lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
2956 {
2957         LIST_HEAD(txq);
2958         LIST_HEAD(txcmplq);
2959         struct lpfc_sli *psli = &phba->sli;
2960         struct lpfc_sli_ring  *pring;
2961
2962         /* Currently, only one fcp ring */
2963         pring = &psli->ring[psli->fcp_ring];
2964
2965         spin_lock_irq(&phba->hbalock);
2966         /* Retrieve everything on txq */
2967         list_splice_init(&pring->txq, &txq);
2968         pring->txq_cnt = 0;
2969
2970         /* Retrieve everything on the txcmplq */
2971         list_splice_init(&pring->txcmplq, &txcmplq);
2972         pring->txcmplq_cnt = 0;
2973         spin_unlock_irq(&phba->hbalock);
2974
2975         /* Flush the txq */
2976         lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
2977                               IOERR_SLI_DOWN);
2978
2979         /* Flush the txcmpq */
2980         lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
2981                               IOERR_SLI_DOWN);
2982 }
2983
2984 /**
2985  * lpfc_sli_brdready_s3 - Check for sli3 host ready status
2986  * @phba: Pointer to HBA context object.
2987  * @mask: Bit mask to be checked.
2988  *
2989  * This function reads the host status register and compares
2990  * with the provided bit mask to check if HBA completed
2991  * the restart. This function will wait in a loop for the
2992  * HBA to complete restart. If the HBA does not restart within
2993  * 15 iterations, the function will reset the HBA again. The
2994  * function returns 1 when HBA fail to restart otherwise returns
2995  * zero.
2996  **/
2997 static int
2998 lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
2999 {
3000         uint32_t status;
3001         int i = 0;
3002         int retval = 0;
3003
3004         /* Read the HBA Host Status Register */
3005         status = readl(phba->HSregaddr);
3006
3007         /*
3008          * Check status register every 100ms for 5 retries, then every
3009          * 500ms for 5, then every 2.5 sec for 5, then reset board and
3010          * every 2.5 sec for 4.
3011          * Break our of the loop if errors occurred during init.
3012          */
3013         while (((status & mask) != mask) &&
3014                !(status & HS_FFERM) &&
3015                i++ < 20) {
3016
3017                 if (i <= 5)
3018                         msleep(10);
3019                 else if (i <= 10)
3020                         msleep(500);
3021                 else
3022                         msleep(2500);
3023
3024                 if (i == 15) {
3025                                 /* Do post */
3026                         phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3027                         lpfc_sli_brdrestart(phba);
3028                 }
3029                 /* Read the HBA Host Status Register */
3030                 status = readl(phba->HSregaddr);
3031         }
3032
3033         /* Check to see if any errors occurred during init */
3034         if ((status & HS_FFERM) || (i >= 20)) {
3035                 phba->link_state = LPFC_HBA_ERROR;
3036                 retval = 1;
3037         }
3038
3039         return retval;
3040 }
3041
3042 /**
3043  * lpfc_sli_brdready_s4 - Check for sli4 host ready status
3044  * @phba: Pointer to HBA context object.
3045  * @mask: Bit mask to be checked.
3046  *
3047  * This function checks the host status register to check if HBA is
3048  * ready. This function will wait in a loop for the HBA to be ready
3049  * If the HBA is not ready , the function will will reset the HBA PCI
3050  * function again. The function returns 1 when HBA fail to be ready
3051  * otherwise returns zero.
3052  **/
3053 static int
3054 lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
3055 {
3056         uint32_t status;
3057         int retval = 0;
3058
3059         /* Read the HBA Host Status Register */
3060         status = lpfc_sli4_post_status_check(phba);
3061
3062         if (status) {
3063                 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3064                 lpfc_sli_brdrestart(phba);
3065                 status = lpfc_sli4_post_status_check(phba);
3066         }
3067
3068         /* Check to see if any errors occurred during init */
3069         if (status) {
3070                 phba->link_state = LPFC_HBA_ERROR;
3071                 retval = 1;
3072         } else
3073                 phba->sli4_hba.intr_enable = 0;
3074
3075         return retval;
3076 }
3077
3078 /**
3079  * lpfc_sli_brdready - Wrapper func for checking the hba readyness
3080  * @phba: Pointer to HBA context object.
3081  * @mask: Bit mask to be checked.
3082  *
3083  * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
3084  * from the API jump table function pointer from the lpfc_hba struct.
3085  **/
3086 int
3087 lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
3088 {
3089         return phba->lpfc_sli_brdready(phba, mask);
3090 }
3091
3092 #define BARRIER_TEST_PATTERN (0xdeadbeef)
3093
3094 /**
3095  * lpfc_reset_barrier - Make HBA ready for HBA reset
3096  * @phba: Pointer to HBA context object.
3097  *
3098  * This function is called before resetting an HBA. This
3099  * function requests HBA to quiesce DMAs before a reset.
3100  **/
3101 void lpfc_reset_barrier(struct lpfc_hba *phba)
3102 {
3103         uint32_t __iomem *resp_buf;
3104         uint32_t __iomem *mbox_buf;
3105         volatile uint32_t mbox;
3106         uint32_t hc_copy;
3107         int  i;
3108         uint8_t hdrtype;
3109
3110         pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
3111         if (hdrtype != 0x80 ||
3112             (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
3113              FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
3114                 return;
3115
3116         /*
3117          * Tell the other part of the chip to suspend temporarily all
3118          * its DMA activity.
3119          */
3120         resp_buf = phba->MBslimaddr;
3121
3122         /* Disable the error attention */
3123         hc_copy = readl(phba->HCregaddr);
3124         writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
3125         readl(phba->HCregaddr); /* flush */
3126         phba->link_flag |= LS_IGNORE_ERATT;
3127
3128         if (readl(phba->HAregaddr) & HA_ERATT) {
3129                 /* Clear Chip error bit */
3130                 writel(HA_ERATT, phba->HAregaddr);
3131                 phba->pport->stopped = 1;
3132         }
3133
3134         mbox = 0;
3135         ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
3136         ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
3137
3138         writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
3139         mbox_buf = phba->MBslimaddr;
3140         writel(mbox, mbox_buf);
3141
3142         for (i = 0;
3143              readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN) && i < 50; i++)
3144                 mdelay(1);
3145
3146         if (readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN)) {
3147                 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
3148                     phba->pport->stopped)
3149                         goto restore_hc;
3150                 else
3151                         goto clear_errat;
3152         }
3153
3154         ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
3155         for (i = 0; readl(resp_buf) != mbox &&  i < 500; i++)
3156                 mdelay(1);
3157
3158 clear_errat:
3159
3160         while (!(readl(phba->HAregaddr) & HA_ERATT) && ++i < 500)
3161                 mdelay(1);
3162
3163         if (readl(phba->HAregaddr) & HA_ERATT) {
3164                 writel(HA_ERATT, phba->HAregaddr);
3165                 phba->pport->stopped = 1;
3166         }
3167
3168 restore_hc:
3169         phba->link_flag &= ~LS_IGNORE_ERATT;
3170         writel(hc_copy, phba->HCregaddr);
3171         readl(phba->HCregaddr); /* flush */
3172 }
3173
3174 /**
3175  * lpfc_sli_brdkill - Issue a kill_board mailbox command
3176  * @phba: Pointer to HBA context object.
3177  *
3178  * This function issues a kill_board mailbox command and waits for
3179  * the error attention interrupt. This function is called for stopping
3180  * the firmware processing. The caller is not required to hold any
3181  * locks. This function calls lpfc_hba_down_post function to free
3182  * any pending commands after the kill. The function will return 1 when it
3183  * fails to kill the board else will return 0.
3184  **/
3185 int
3186 lpfc_sli_brdkill(struct lpfc_hba *phba)
3187 {
3188         struct lpfc_sli *psli;
3189         LPFC_MBOXQ_t *pmb;
3190         uint32_t status;
3191         uint32_t ha_copy;
3192         int retval;
3193         int i = 0;
3194
3195         psli = &phba->sli;
3196
3197         /* Kill HBA */
3198         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3199                         "0329 Kill HBA Data: x%x x%x\n",
3200                         phba->pport->port_state, psli->sli_flag);
3201
3202         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3203         if (!pmb)
3204                 return 1;
3205
3206         /* Disable the error attention */
3207         spin_lock_irq(&phba->hbalock);
3208         status = readl(phba->HCregaddr);
3209         status &= ~HC_ERINT_ENA;
3210         writel(status, phba->HCregaddr);
3211         readl(phba->HCregaddr); /* flush */
3212         phba->link_flag |= LS_IGNORE_ERATT;
3213         spin_unlock_irq(&phba->hbalock);
3214
3215         lpfc_kill_board(phba, pmb);
3216         pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3217         retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3218
3219         if (retval != MBX_SUCCESS) {
3220                 if (retval != MBX_BUSY)
3221                         mempool_free(pmb, phba->mbox_mem_pool);
3222                 spin_lock_irq(&phba->hbalock);
3223                 phba->link_flag &= ~LS_IGNORE_ERATT;
3224                 spin_unlock_irq(&phba->hbalock);
3225                 return 1;
3226         }
3227
3228         spin_lock_irq(&phba->hbalock);
3229         psli->sli_flag &= ~LPFC_SLI_ACTIVE;
3230         spin_unlock_irq(&phba->hbalock);
3231
3232         mempool_free(pmb, phba->mbox_mem_pool);
3233
3234         /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
3235          * attention every 100ms for 3 seconds. If we don't get ERATT after
3236          * 3 seconds we still set HBA_ERROR state because the status of the
3237          * board is now undefined.
3238          */
3239         ha_copy = readl(phba->HAregaddr);
3240
3241         while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
3242                 mdelay(100);
3243                 ha_copy = readl(phba->HAregaddr);
3244         }
3245
3246         del_timer_sync(&psli->mbox_tmo);
3247         if (ha_copy & HA_ERATT) {
3248                 writel(HA_ERATT, phba->HAregaddr);
3249                 phba->pport->stopped = 1;
3250         }
3251         spin_lock_irq(&phba->hbalock);
3252         psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
3253         psli->mbox_active = NULL;
3254         phba->link_flag &= ~LS_IGNORE_ERATT;
3255         spin_unlock_irq(&phba->hbalock);
3256
3257         lpfc_hba_down_post(phba);
3258         phba->link_state = LPFC_HBA_ERROR;
3259
3260         return ha_copy & HA_ERATT ? 0 : 1;
3261 }
3262
3263 /**
3264  * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
3265  * @phba: Pointer to HBA context object.
3266  *
3267  * This function resets the HBA by writing HC_INITFF to the control
3268  * register. After the HBA resets, this function resets all the iocb ring
3269  * indices. This function disables PCI layer parity checking during
3270  * the reset.
3271  * This function returns 0 always.
3272  * The caller is not required to hold any locks.
3273  **/
3274 int
3275 lpfc_sli_brdreset(struct lpfc_hba *phba)
3276 {
3277         struct lpfc_sli *psli;
3278         struct lpfc_sli_ring *pring;
3279         uint16_t cfg_value;
3280         int i;
3281
3282         psli = &phba->sli;
3283
3284         /* Reset HBA */
3285         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3286                         "0325 Reset HBA Data: x%x x%x\n",
3287                         phba->pport->port_state, psli->sli_flag);
3288
3289         /* perform board reset */
3290         phba->fc_eventTag = 0;
3291         phba->link_events = 0;
3292         phba->pport->fc_myDID = 0;
3293         phba->pport->fc_prevDID = 0;
3294
3295         /* Turn off parity checking and serr during the physical reset */
3296         pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3297         pci_write_config_word(phba->pcidev, PCI_COMMAND,
3298                               (cfg_value &
3299                                ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3300
3301         psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
3302
3303         /* Now toggle INITFF bit in the Host Control Register */
3304         writel(HC_INITFF, phba->HCregaddr);
3305         mdelay(1);
3306         readl(phba->HCregaddr); /* flush */
3307         writel(0, phba->HCregaddr);
3308         readl(phba->HCregaddr); /* flush */
3309
3310         /* Restore PCI cmd register */
3311         pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
3312
3313         /* Initialize relevant SLI info */
3314         for (i = 0; i < psli->num_rings; i++) {
3315                 pring = &psli->ring[i];
3316                 pring->flag = 0;
3317                 pring->rspidx = 0;
3318                 pring->next_cmdidx  = 0;
3319                 pring->local_getidx = 0;
3320                 pring->cmdidx = 0;
3321                 pring->missbufcnt = 0;
3322         }
3323
3324         phba->link_state = LPFC_WARM_START;
3325         return 0;
3326 }
3327
3328 /**
3329  * lpfc_sli4_brdreset - Reset a sli-4 HBA
3330  * @phba: Pointer to HBA context object.
3331  *
3332  * This function resets a SLI4 HBA. This function disables PCI layer parity
3333  * checking during resets the device. The caller is not required to hold
3334  * any locks.
3335  *
3336  * This function returns 0 always.
3337  **/
3338 int
3339 lpfc_sli4_brdreset(struct lpfc_hba *phba)
3340 {
3341         struct lpfc_sli *psli = &phba->sli;
3342         uint16_t cfg_value;
3343         uint8_t qindx;
3344
3345         /* Reset HBA */
3346         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3347                         "0295 Reset HBA Data: x%x x%x\n",
3348                         phba->pport->port_state, psli->sli_flag);
3349
3350         /* perform board reset */
3351         phba->fc_eventTag = 0;
3352         phba->link_events = 0;
3353         phba->pport->fc_myDID = 0;
3354         phba->pport->fc_prevDID = 0;
3355
3356         /* Turn off parity checking and serr during the physical reset */
3357         pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3358         pci_write_config_word(phba->pcidev, PCI_COMMAND,
3359                               (cfg_value &
3360                               ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3361
3362         spin_lock_irq(&phba->hbalock);
3363         psli->sli_flag &= ~(LPFC_PROCESS_LA);
3364         phba->fcf.fcf_flag = 0;
3365         /* Clean up the child queue list for the CQs */
3366         list_del_init(&phba->sli4_hba.mbx_wq->list);
3367         list_del_init(&phba->sli4_hba.els_wq->list);
3368         list_del_init(&phba->sli4_hba.hdr_rq->list);
3369         list_del_init(&phba->sli4_hba.dat_rq->list);
3370         list_del_init(&phba->sli4_hba.mbx_cq->list);
3371         list_del_init(&phba->sli4_hba.els_cq->list);
3372         for (qindx = 0; qindx < phba->cfg_fcp_wq_count; qindx++)
3373                 list_del_init(&phba->sli4_hba.fcp_wq[qindx]->list);
3374         for (qindx = 0; qindx < phba->cfg_fcp_eq_count; qindx++)
3375                 list_del_init(&phba->sli4_hba.fcp_cq[qindx]->list);
3376         spin_unlock_irq(&phba->hbalock);
3377
3378         /* Now physically reset the device */
3379         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3380                         "0389 Performing PCI function reset!\n");
3381         /* Perform FCoE PCI function reset */
3382         lpfc_pci_function_reset(phba);
3383
3384         return 0;
3385 }
3386
3387 /**
3388  * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
3389  * @phba: Pointer to HBA context object.
3390  *
3391  * This function is called in the SLI initialization code path to
3392  * restart the HBA. The caller is not required to hold any lock.
3393  * This function writes MBX_RESTART mailbox command to the SLIM and
3394  * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
3395  * function to free any pending commands. The function enables
3396  * POST only during the first initialization. The function returns zero.
3397  * The function does not guarantee completion of MBX_RESTART mailbox
3398  * command before the return of this function.
3399  **/
3400 static int
3401 lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
3402 {
3403         MAILBOX_t *mb;
3404         struct lpfc_sli *psli;
3405         volatile uint32_t word0;
3406         void __iomem *to_slim;
3407         uint32_t hba_aer_enabled;
3408
3409         spin_lock_irq(&phba->hbalock);
3410
3411         /* Take PCIe device Advanced Error Reporting (AER) state */
3412         hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
3413
3414         psli = &phba->sli;
3415
3416         /* Restart HBA */
3417         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3418                         "0337 Restart HBA Data: x%x x%x\n",
3419                         phba->pport->port_state, psli->sli_flag);
3420
3421         word0 = 0;
3422         mb = (MAILBOX_t *) &word0;
3423         mb->mbxCommand = MBX_RESTART;
3424         mb->mbxHc = 1;
3425
3426         lpfc_reset_barrier(phba);
3427
3428         to_slim = phba->MBslimaddr;
3429         writel(*(uint32_t *) mb, to_slim);
3430         readl(to_slim); /* flush */
3431
3432         /* Only skip post after fc_ffinit is completed */
3433         if (phba->pport->port_state)
3434                 word0 = 1;      /* This is really setting up word1 */
3435         else
3436                 word0 = 0;      /* This is really setting up word1 */
3437         to_slim = phba->MBslimaddr + sizeof (uint32_t);
3438         writel(*(uint32_t *) mb, to_slim);
3439         readl(to_slim); /* flush */
3440
3441         lpfc_sli_brdreset(phba);
3442         phba->pport->stopped = 0;
3443         phba->link_state = LPFC_INIT_START;
3444         phba->hba_flag = 0;
3445         spin_unlock_irq(&phba->hbalock);
3446
3447         memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
3448         psli->stats_start = get_seconds();
3449
3450         /* Give the INITFF and Post time to settle. */
3451         mdelay(100);
3452
3453         /* Reset HBA AER if it was enabled, note hba_flag was reset above */
3454         if (hba_aer_enabled)
3455                 pci_disable_pcie_error_reporting(phba->pcidev);
3456
3457         lpfc_hba_down_post(phba);
3458
3459         return 0;
3460 }
3461
3462 /**
3463  * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
3464  * @phba: Pointer to HBA context object.
3465  *
3466  * This function is called in the SLI initialization code path to restart
3467  * a SLI4 HBA. The caller is not required to hold any lock.
3468  * At the end of the function, it calls lpfc_hba_down_post function to
3469  * free any pending commands.
3470  **/
3471 static int
3472 lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
3473 {
3474         struct lpfc_sli *psli = &phba->sli;
3475
3476
3477         /* Restart HBA */
3478         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3479                         "0296 Restart HBA Data: x%x x%x\n",
3480                         phba->pport->port_state, psli->sli_flag);
3481
3482         lpfc_sli4_brdreset(phba);
3483
3484         spin_lock_irq(&phba->hbalock);
3485         phba->pport->stopped = 0;
3486         phba->link_state = LPFC_INIT_START;
3487         phba->hba_flag = 0;
3488         spin_unlock_irq(&phba->hbalock);
3489
3490         memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
3491         psli->stats_start = get_seconds();
3492
3493         lpfc_hba_down_post(phba);
3494
3495         return 0;
3496 }
3497
3498 /**
3499  * lpfc_sli_brdrestart - Wrapper func for restarting hba
3500  * @phba: Pointer to HBA context object.
3501  *
3502  * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
3503  * API jump table function pointer from the lpfc_hba struct.
3504 **/
3505 int
3506 lpfc_sli_brdrestart(struct lpfc_hba *phba)
3507 {
3508         return phba->lpfc_sli_brdrestart(phba);
3509 }
3510
3511 /**
3512  * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
3513  * @phba: Pointer to HBA context object.
3514  *
3515  * This function is called after a HBA restart to wait for successful
3516  * restart of the HBA. Successful restart of the HBA is indicated by
3517  * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
3518  * iteration, the function will restart the HBA again. The function returns
3519  * zero if HBA successfully restarted else returns negative error code.
3520  **/
3521 static int
3522 lpfc_sli_chipset_init(struct lpfc_hba *phba)
3523 {
3524         uint32_t status, i = 0;
3525
3526         /* Read the HBA Host Status Register */
3527         status = readl(phba->HSregaddr);
3528
3529         /* Check status register to see what current state is */
3530         i = 0;
3531         while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
3532
3533                 /* Check every 100ms for 5 retries, then every 500ms for 5, then
3534                  * every 2.5 sec for 5, then reset board and every 2.5 sec for
3535                  * 4.
3536                  */
3537                 if (i++ >= 20) {
3538                         /* Adapter failed to init, timeout, status reg
3539                            <status> */
3540                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3541                                         "0436 Adapter failed to init, "
3542                                         "timeout, status reg x%x, "
3543                                         "FW Data: A8 x%x AC x%x\n", status,
3544                                         readl(phba->MBslimaddr + 0xa8),
3545                                         readl(phba->MBslimaddr + 0xac));
3546                         phba->link_state = LPFC_HBA_ERROR;
3547                         return -ETIMEDOUT;
3548                 }
3549
3550                 /* Check to see if any errors occurred during init */
3551                 if (status & HS_FFERM) {
3552                         /* ERROR: During chipset initialization */
3553                         /* Adapter failed to init, chipset, status reg
3554                            <status> */
3555                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3556                                         "0437 Adapter failed to init, "
3557                                         "chipset, status reg x%x, "
3558                                         "FW Data: A8 x%x AC x%x\n", status,
3559                                         readl(phba->MBslimaddr + 0xa8),
3560                                         readl(phba->MBslimaddr + 0xac));
3561                         phba->link_state = LPFC_HBA_ERROR;
3562                         return -EIO;
3563                 }
3564
3565                 if (i <= 5) {
3566                         msleep(10);
3567                 } else if (i <= 10) {
3568                         msleep(500);
3569                 } else {
3570                         msleep(2500);
3571                 }
3572
3573                 if (i == 15) {
3574                                 /* Do post */
3575                         phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3576                         lpfc_sli_brdrestart(phba);
3577                 }
3578                 /* Read the HBA Host Status Register */
3579                 status = readl(phba->HSregaddr);
3580         }
3581
3582         /* Check to see if any errors occurred during init */
3583         if (status & HS_FFERM) {
3584                 /* ERROR: During chipset initialization */
3585                 /* Adapter failed to init, chipset, status reg <status> */
3586                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3587                                 "0438 Adapter failed to init, chipset, "
3588                                 "status reg x%x, "
3589                                 "FW Data: A8 x%x AC x%x\n", status,
3590                                 readl(phba->MBslimaddr + 0xa8),
3591                                 readl(phba->MBslimaddr + 0xac));
3592                 phba->link_state = LPFC_HBA_ERROR;
3593                 return -EIO;
3594         }
3595
3596         /* Clear all interrupt enable conditions */
3597         writel(0, phba->HCregaddr);
3598         readl(phba->HCregaddr); /* flush */
3599
3600         /* setup host attn register */
3601         writel(0xffffffff, phba->HAregaddr);
3602         readl(phba->HAregaddr); /* flush */
3603         return 0;
3604 }
3605
3606 /**
3607  * lpfc_sli_hbq_count - Get the number of HBQs to be configured
3608  *
3609  * This function calculates and returns the number of HBQs required to be
3610  * configured.
3611  **/
3612 int
3613 lpfc_sli_hbq_count(void)
3614 {
3615         return ARRAY_SIZE(lpfc_hbq_defs);
3616 }
3617
3618 /**
3619  * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
3620  *
3621  * This function adds the number of hbq entries in every HBQ to get
3622  * the total number of hbq entries required for the HBA and returns
3623  * the total count.
3624  **/
3625 static int
3626 lpfc_sli_hbq_entry_count(void)
3627 {
3628         int  hbq_count = lpfc_sli_hbq_count();
3629         int  count = 0;
3630         int  i;
3631
3632         for (i = 0; i < hbq_count; ++i)
3633                 count += lpfc_hbq_defs[i]->entry_count;
3634         return count;
3635 }
3636
3637 /**
3638  * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
3639  *
3640  * This function calculates amount of memory required for all hbq entries
3641  * to be configured and returns the total memory required.
3642  **/
3643 int
3644 lpfc_sli_hbq_size(void)
3645 {
3646         return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
3647 }
3648
3649 /**
3650  * lpfc_sli_hbq_setup - configure and initialize HBQs
3651  * @phba: Pointer to HBA context object.
3652  *
3653  * This function is called during the SLI initialization to configure
3654  * all the HBQs and post buffers to the HBQ. The caller is not
3655  * required to hold any locks. This function will return zero if successful
3656  * else it will return negative error code.
3657  **/
3658 static int
3659 lpfc_sli_hbq_setup(struct lpfc_hba *phba)
3660 {
3661         int  hbq_count = lpfc_sli_hbq_count();
3662         LPFC_MBOXQ_t *pmb;
3663         MAILBOX_t *pmbox;
3664         uint32_t hbqno;
3665         uint32_t hbq_entry_index;
3666
3667                                 /* Get a Mailbox buffer to setup mailbox
3668                                  * commands for HBA initialization
3669                                  */
3670         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3671
3672         if (!pmb)
3673                 return -ENOMEM;
3674
3675         pmbox = &pmb->u.mb;
3676
3677         /* Initialize the struct lpfc_sli_hbq structure for each hbq */
3678         phba->link_state = LPFC_INIT_MBX_CMDS;
3679         phba->hbq_in_use = 1;
3680
3681         hbq_entry_index = 0;
3682         for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
3683                 phba->hbqs[hbqno].next_hbqPutIdx = 0;
3684                 phba->hbqs[hbqno].hbqPutIdx      = 0;
3685                 phba->hbqs[hbqno].local_hbqGetIdx   = 0;
3686                 phba->hbqs[hbqno].entry_count =
3687                         lpfc_hbq_defs[hbqno]->entry_count;
3688                 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
3689                         hbq_entry_index, pmb);
3690                 hbq_entry_index += phba->hbqs[hbqno].entry_count;
3691
3692                 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
3693                         /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
3694                            mbxStatus <status>, ring <num> */
3695
3696                         lpfc_printf_log(phba, KERN_ERR,
3697                                         LOG_SLI | LOG_VPORT,
3698                                         "1805 Adapter failed to init. "
3699                                         "Data: x%x x%x x%x\n",
3700                                         pmbox->mbxCommand,
3701                                         pmbox->mbxStatus, hbqno);
3702
3703                         phba->link_state = LPFC_HBA_ERROR;
3704                         mempool_free(pmb, phba->mbox_mem_pool);
3705                         return ENXIO;
3706                 }
3707         }
3708         phba->hbq_count = hbq_count;
3709
3710         mempool_free(pmb, phba->mbox_mem_pool);
3711
3712         /* Initially populate or replenish the HBQs */
3713         for (hbqno = 0; hbqno < hbq_count; ++hbqno)
3714                 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
3715         return 0;
3716 }
3717
3718 /**
3719  * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
3720  * @phba: Pointer to HBA context object.
3721  *
3722  * This function is called during the SLI initialization to configure
3723  * all the HBQs and post buffers to the HBQ. The caller is not
3724  * required to hold any locks. This function will return zero if successful
3725  * else it will return negative error code.
3726  **/
3727 static int
3728 lpfc_sli4_rb_setup(struct lpfc_hba *phba)
3729 {
3730         phba->hbq_in_use = 1;
3731         phba->hbqs[0].entry_count = lpfc_hbq_defs[0]->entry_count;
3732         phba->hbq_count = 1;
3733         /* Initially populate or replenish the HBQs */
3734         lpfc_sli_hbqbuf_init_hbqs(phba, 0);
3735         return 0;
3736 }
3737
3738 /**
3739  * lpfc_sli_config_port - Issue config port mailbox command
3740  * @phba: Pointer to HBA context object.
3741  * @sli_mode: sli mode - 2/3
3742  *
3743  * This function is called by the sli intialization code path
3744  * to issue config_port mailbox command. This function restarts the
3745  * HBA firmware and issues a config_port mailbox command to configure
3746  * the SLI interface in the sli mode specified by sli_mode
3747  * variable. The caller is not required to hold any locks.
3748  * The function returns 0 if successful, else returns negative error
3749  * code.
3750  **/
3751 int
3752 lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
3753 {
3754         LPFC_MBOXQ_t *pmb;
3755         uint32_t resetcount = 0, rc = 0, done = 0;
3756
3757         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3758         if (!pmb) {
3759                 phba->link_state = LPFC_HBA_ERROR;
3760                 return -ENOMEM;
3761         }
3762
3763         phba->sli_rev = sli_mode;
3764         while (resetcount < 2 && !done) {
3765                 spin_lock_irq(&phba->hbalock);
3766                 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
3767                 spin_unlock_irq(&phba->hbalock);
3768                 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3769                 lpfc_sli_brdrestart(phba);
3770                 rc = lpfc_sli_chipset_init(phba);
3771                 if (rc)
3772                         break;
3773
3774                 spin_lock_irq(&phba->hbalock);
3775                 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
3776                 spin_unlock_irq(&phba->hbalock);
3777                 resetcount++;
3778
3779                 /* Call pre CONFIG_PORT mailbox command initialization.  A
3780                  * value of 0 means the call was successful.  Any other
3781                  * nonzero value is a failure, but if ERESTART is returned,
3782                  * the driver may reset the HBA and try again.
3783                  */
3784                 rc = lpfc_config_port_prep(phba);
3785                 if (rc == -ERESTART) {
3786                         phba->link_state = LPFC_LINK_UNKNOWN;
3787                         continue;
3788                 } else if (rc)
3789                         break;
3790                 phba->link_state = LPFC_INIT_MBX_CMDS;
3791                 lpfc_config_port(phba, pmb);
3792                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
3793                 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
3794                                         LPFC_SLI3_HBQ_ENABLED |
3795                                         LPFC_SLI3_CRP_ENABLED |
3796                                         LPFC_SLI3_INB_ENABLED |
3797                                         LPFC_SLI3_BG_ENABLED);
3798                 if (rc != MBX_SUCCESS) {
3799                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3800                                 "0442 Adapter failed to init, mbxCmd x%x "
3801                                 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
3802                                 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
3803                         spin_lock_irq(&phba->hbalock);
3804                         phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
3805                         spin_unlock_irq(&phba->hbalock);
3806                         rc = -ENXIO;
3807                 } else {
3808                         /* Allow asynchronous mailbox command to go through */
3809                         spin_lock_irq(&phba->hbalock);
3810                         phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
3811                         spin_unlock_irq(&phba->hbalock);
3812                         done = 1;
3813                 }
3814         }
3815         if (!done) {
3816                 rc = -EINVAL;
3817                 goto do_prep_failed;
3818         }
3819         if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
3820                 if (!pmb->u.mb.un.varCfgPort.cMA) {
3821                         rc = -ENXIO;
3822                         goto do_prep_failed;
3823                 }
3824                 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
3825                         phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
3826                         phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
3827                         phba->max_vports = (phba->max_vpi > phba->max_vports) ?
3828                                 phba->max_vpi : phba->max_vports;
3829
3830                 } else
3831                         phba->max_vpi = 0;
3832                 if (pmb->u.mb.un.varCfgPort.gdss)
3833                         phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
3834                 if (pmb->u.mb.un.varCfgPort.gerbm)
3835                         phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
3836                 if (pmb->u.mb.un.varCfgPort.gcrp)
3837                         phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
3838                 if (pmb->u.mb.un.varCfgPort.ginb) {
3839                         phba->sli3_options |= LPFC_SLI3_INB_ENABLED;
3840                         phba->hbq_get = phba->mbox->us.s3_inb_pgp.hbq_get;
3841                         phba->port_gp = phba->mbox->us.s3_inb_pgp.port;
3842                         phba->inb_ha_copy = &phba->mbox->us.s3_inb_pgp.ha_copy;
3843                         phba->inb_counter = &phba->mbox->us.s3_inb_pgp.counter;
3844                         phba->inb_last_counter =
3845                                         phba->mbox->us.s3_inb_pgp.counter;
3846                 } else {
3847                         phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
3848                         phba->port_gp = phba->mbox->us.s3_pgp.port;
3849                         phba->inb_ha_copy = NULL;
3850                         phba->inb_counter = NULL;
3851                 }
3852
3853                 if (phba->cfg_enable_bg) {
3854                         if (pmb->u.mb.un.varCfgPort.gbg)
3855                                 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
3856                         else
3857                                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3858                                                 "0443 Adapter did not grant "
3859                                                 "BlockGuard\n");
3860                 }
3861         } else {
3862                 phba->hbq_get = NULL;
3863                 phba->port_gp = phba->mbox->us.s2.port;
3864                 phba->inb_ha_copy = NULL;
3865                 phba->inb_counter = NULL;
3866                 phba->max_vpi = 0;
3867         }
3868 do_prep_failed:
3869         mempool_free(pmb, phba->mbox_mem_pool);
3870         return rc;
3871 }
3872
3873
3874 /**
3875  * lpfc_sli_hba_setup - SLI intialization function
3876  * @phba: Pointer to HBA context object.
3877  *
3878  * This function is the main SLI intialization function. This function
3879  * is called by the HBA intialization code, HBA reset code and HBA
3880  * error attention handler code. Caller is not required to hold any
3881  * locks. This function issues config_port mailbox command to configure
3882  * the SLI, setup iocb rings and HBQ rings. In the end the function
3883  * calls the config_port_post function to issue init_link mailbox
3884  * command and to start the discovery. The function will return zero
3885  * if successful, else it will return negative error code.
3886  **/
3887 int
3888 lpfc_sli_hba_setup(struct lpfc_hba *phba)
3889 {
3890         uint32_t rc;
3891         int  mode = 3;
3892
3893         switch (lpfc_sli_mode) {
3894         case 2:
3895                 if (phba->cfg_enable_npiv) {
3896                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
3897                                 "1824 NPIV enabled: Override lpfc_sli_mode "
3898                                 "parameter (%d) to auto (0).\n",
3899                                 lpfc_sli_mode);
3900                         break;
3901                 }
3902                 mode = 2;
3903                 break;
3904         case 0:
3905         case 3:
3906                 break;
3907         default:
3908                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
3909                                 "1819 Unrecognized lpfc_sli_mode "
3910                                 "parameter: %d.\n", lpfc_sli_mode);
3911
3912                 break;
3913         }
3914
3915         rc = lpfc_sli_config_port(phba, mode);
3916
3917         if (rc && lpfc_sli_mode == 3)
3918                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
3919                                 "1820 Unable to select SLI-3.  "
3920                                 "Not supported by adapter.\n");
3921         if (rc && mode != 2)
3922                 rc = lpfc_sli_config_port(phba, 2);
3923         if (rc)
3924                 goto lpfc_sli_hba_setup_error;
3925
3926         /* Enable PCIe device Advanced Error Reporting (AER) if configured */
3927         if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
3928                 rc = pci_enable_pcie_error_reporting(phba->pcidev);
3929                 if (!rc) {
3930                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3931                                         "2709 This device supports "
3932                                         "Advanced Error Reporting (AER)\n");
3933                         spin_lock_irq(&phba->hbalock);
3934                         phba->hba_flag |= HBA_AER_ENABLED;
3935                         spin_unlock_irq(&phba->hbalock);
3936                 } else {
3937                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3938                                         "2708 This device does not support "
3939                                         "Advanced Error Reporting (AER)\n");
3940                         phba->cfg_aer_support = 0;
3941                 }
3942         }
3943
3944         if (phba->sli_rev == 3) {
3945                 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
3946                 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
3947         } else {
3948                 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
3949                 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
3950                 phba->sli3_options = 0;
3951         }
3952
3953         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3954                         "0444 Firmware in SLI %x mode. Max_vpi %d\n",
3955                         phba->sli_rev, phba->max_vpi);
3956         rc = lpfc_sli_ring_map(phba);
3957
3958         if (rc)
3959                 goto lpfc_sli_hba_setup_error;
3960
3961         /* Init HBQs */
3962         if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
3963                 rc = lpfc_sli_hbq_setup(phba);
3964                 if (rc)
3965                         goto lpfc_sli_hba_setup_error;
3966         }
3967         spin_lock_irq(&phba->hbalock);
3968         phba->sli.sli_flag |= LPFC_PROCESS_LA;
3969         spin_unlock_irq(&phba->hbalock);
3970
3971         rc = lpfc_config_port_post(phba);
3972         if (rc)
3973                 goto lpfc_sli_hba_setup_error;
3974
3975         return rc;
3976
3977 lpfc_sli_hba_setup_error:
3978         phba->link_state = LPFC_HBA_ERROR;
3979         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3980                         "0445 Firmware initialization failed\n");
3981         return rc;
3982 }
3983
3984 /**
3985  * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
3986  * @phba: Pointer to HBA context object.
3987  * @mboxq: mailbox pointer.
3988  * This function issue a dump mailbox command to read config region
3989  * 23 and parse the records in the region and populate driver
3990  * data structure.
3991  **/
3992 static int
3993 lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba,
3994                 LPFC_MBOXQ_t *mboxq)
3995 {
3996         struct lpfc_dmabuf *mp;
3997         struct lpfc_mqe *mqe;
3998         uint32_t data_length;
3999         int rc;
4000
4001         /* Program the default value of vlan_id and fc_map */
4002         phba->valid_vlan = 0;
4003         phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4004         phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4005         phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
4006
4007         mqe = &mboxq->u.mqe;
4008         if (lpfc_dump_fcoe_param(phba, mboxq))
4009                 return -ENOMEM;
4010
4011         mp = (struct lpfc_dmabuf *) mboxq->context1;
4012         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4013
4014         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4015                         "(%d):2571 Mailbox cmd x%x Status x%x "
4016                         "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4017                         "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4018                         "CQ: x%x x%x x%x x%x\n",
4019                         mboxq->vport ? mboxq->vport->vpi : 0,
4020                         bf_get(lpfc_mqe_command, mqe),
4021                         bf_get(lpfc_mqe_status, mqe),
4022                         mqe->un.mb_words[0], mqe->un.mb_words[1],
4023                         mqe->un.mb_words[2], mqe->un.mb_words[3],
4024                         mqe->un.mb_words[4], mqe->un.mb_words[5],
4025                         mqe->un.mb_words[6], mqe->un.mb_words[7],
4026                         mqe->un.mb_words[8], mqe->un.mb_words[9],
4027                         mqe->un.mb_words[10], mqe->un.mb_words[11],
4028                         mqe->un.mb_words[12], mqe->un.mb_words[13],
4029                         mqe->un.mb_words[14], mqe->un.mb_words[15],
4030                         mqe->un.mb_words[16], mqe->un.mb_words[50],
4031                         mboxq->mcqe.word0,
4032                         mboxq->mcqe.mcqe_tag0,  mboxq->mcqe.mcqe_tag1,
4033                         mboxq->mcqe.trailer);
4034
4035         if (rc) {
4036                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4037                 kfree(mp);
4038                 return -EIO;
4039         }
4040         data_length = mqe->un.mb_words[5];
4041         if (data_length > DMP_RGN23_SIZE) {
4042                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4043                 kfree(mp);
4044                 return -EIO;
4045         }
4046
4047         lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
4048         lpfc_mbuf_free(phba, mp->virt, mp->phys);
4049         kfree(mp);
4050         return 0;
4051 }
4052
4053 /**
4054  * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
4055  * @phba: pointer to lpfc hba data structure.
4056  * @mboxq: pointer to the LPFC_MBOXQ_t structure.
4057  * @vpd: pointer to the memory to hold resulting port vpd data.
4058  * @vpd_size: On input, the number of bytes allocated to @vpd.
4059  *            On output, the number of data bytes in @vpd.
4060  *
4061  * This routine executes a READ_REV SLI4 mailbox command.  In
4062  * addition, this routine gets the port vpd data.
4063  *
4064  * Return codes
4065  *      0 - successful
4066  *      ENOMEM - could not allocated memory.
4067  **/
4068 static int
4069 lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
4070                     uint8_t *vpd, uint32_t *vpd_size)
4071 {
4072         int rc = 0;
4073         uint32_t dma_size;
4074         struct lpfc_dmabuf *dmabuf;
4075         struct lpfc_mqe *mqe;
4076
4077         dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4078         if (!dmabuf)
4079                 return -ENOMEM;
4080
4081         /*
4082          * Get a DMA buffer for the vpd data resulting from the READ_REV
4083          * mailbox command.
4084          */
4085         dma_size = *vpd_size;
4086         dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
4087                                           dma_size,
4088                                           &dmabuf->phys,
4089                                           GFP_KERNEL);
4090         if (!dmabuf->virt) {
4091                 kfree(dmabuf);
4092                 return -ENOMEM;
4093         }
4094         memset(dmabuf->virt, 0, dma_size);
4095
4096         /*
4097          * The SLI4 implementation of READ_REV conflicts at word1,
4098          * bits 31:16 and SLI4 adds vpd functionality not present
4099          * in SLI3.  This code corrects the conflicts.
4100          */
4101         lpfc_read_rev(phba, mboxq);
4102         mqe = &mboxq->u.mqe;
4103         mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
4104         mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
4105         mqe->un.read_rev.word1 &= 0x0000FFFF;
4106         bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
4107         bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
4108
4109         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4110         if (rc) {
4111                 dma_free_coherent(&phba->pcidev->dev, dma_size,
4112                                   dmabuf->virt, dmabuf->phys);
4113                 return -EIO;
4114         }
4115
4116         /*
4117          * The available vpd length cannot be bigger than the
4118          * DMA buffer passed to the port.  Catch the less than
4119          * case and update the caller's size.
4120          */
4121         if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
4122                 *vpd_size = mqe->un.read_rev.avail_vpd_len;
4123
4124         lpfc_sli_pcimem_bcopy(dmabuf->virt, vpd, *vpd_size);
4125         dma_free_coherent(&phba->pcidev->dev, dma_size,
4126                           dmabuf->virt, dmabuf->phys);
4127         kfree(dmabuf);
4128         return 0;
4129 }
4130
4131 /**
4132  * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
4133  * @phba: pointer to lpfc hba data structure.
4134  *
4135  * This routine is called to explicitly arm the SLI4 device's completion and
4136  * event queues
4137  **/
4138 static void
4139 lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
4140 {
4141         uint8_t fcp_eqidx;
4142
4143         lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
4144         lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
4145         for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++)
4146                 lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[fcp_eqidx],
4147                                      LPFC_QUEUE_REARM);
4148         lpfc_sli4_eq_release(phba->sli4_hba.sp_eq, LPFC_QUEUE_REARM);
4149         for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++)
4150                 lpfc_sli4_eq_release(phba->sli4_hba.fp_eq[fcp_eqidx],
4151                                      LPFC_QUEUE_REARM);
4152 }
4153
4154 /**
4155  * lpfc_sli4_hba_setup - SLI4 device intialization PCI function
4156  * @phba: Pointer to HBA context object.
4157  *
4158  * This function is the main SLI4 device intialization PCI function. This
4159  * function is called by the HBA intialization code, HBA reset code and
4160  * HBA error attention handler code. Caller is not required to hold any
4161  * locks.
4162  **/
4163 int
4164 lpfc_sli4_hba_setup(struct lpfc_hba *phba)
4165 {
4166         int rc;
4167         LPFC_MBOXQ_t *mboxq;
4168         struct lpfc_mqe *mqe;
4169         uint8_t *vpd;
4170         uint32_t vpd_size;
4171         uint32_t ftr_rsp = 0;
4172         struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
4173         struct lpfc_vport *vport = phba->pport;
4174         struct lpfc_dmabuf *mp;
4175
4176         /* Perform a PCI function reset to start from clean */
4177         rc = lpfc_pci_function_reset(phba);
4178         if (unlikely(rc))
4179                 return -ENODEV;
4180
4181         /* Check the HBA Host Status Register for readyness */
4182         rc = lpfc_sli4_post_status_check(phba);
4183         if (unlikely(rc))
4184                 return -ENODEV;
4185         else {
4186                 spin_lock_irq(&phba->hbalock);
4187                 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
4188                 spin_unlock_irq(&phba->hbalock);
4189         }
4190
4191         /*
4192          * Allocate a single mailbox container for initializing the
4193          * port.
4194          */
4195         mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4196         if (!mboxq)
4197                 return -ENOMEM;
4198
4199         /*
4200          * Continue initialization with default values even if driver failed
4201          * to read FCoE param config regions
4202          */
4203         if (lpfc_sli4_read_fcoe_params(phba, mboxq))
4204                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
4205                         "2570 Failed to read FCoE parameters\n");
4206
4207         /* Issue READ_REV to collect vpd and FW information. */
4208         vpd_size = PAGE_SIZE;
4209         vpd = kzalloc(vpd_size, GFP_KERNEL);
4210         if (!vpd) {
4211                 rc = -ENOMEM;
4212                 goto out_free_mbox;
4213         }
4214
4215         rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
4216         if (unlikely(rc))
4217                 goto out_free_vpd;
4218
4219         mqe = &mboxq->u.mqe;
4220         phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
4221         if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev))
4222                 phba->hba_flag |= HBA_FCOE_SUPPORT;
4223
4224         if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
4225                 LPFC_DCBX_CEE_MODE)
4226                 phba->hba_flag |= HBA_FIP_SUPPORT;
4227         else
4228                 phba->hba_flag &= ~HBA_FIP_SUPPORT;
4229
4230         if (phba->sli_rev != LPFC_SLI_REV4 ||
4231             !(phba->hba_flag & HBA_FCOE_SUPPORT)) {
4232                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4233                         "0376 READ_REV Error. SLI Level %d "
4234                         "FCoE enabled %d\n",
4235                         phba->sli_rev, phba->hba_flag & HBA_FCOE_SUPPORT);
4236                 rc = -EIO;
4237                 goto out_free_vpd;
4238         }
4239         /*
4240          * Evaluate the read rev and vpd data. Populate the driver
4241          * state with the results. If this routine fails, the failure
4242          * is not fatal as the driver will use generic values.
4243          */
4244         rc = lpfc_parse_vpd(phba, vpd, vpd_size);
4245         if (unlikely(!rc)) {
4246                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4247                                 "0377 Error %d parsing vpd. "
4248                                 "Using defaults.\n", rc);
4249                 rc = 0;
4250         }
4251
4252         /* Save information as VPD data */
4253         phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
4254         phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
4255         phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
4256         phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
4257                                          &mqe->un.read_rev);
4258         phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
4259                                        &mqe->un.read_rev);
4260         phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
4261                                             &mqe->un.read_rev);
4262         phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
4263                                            &mqe->un.read_rev);
4264         phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
4265         memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
4266         phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
4267         memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
4268         phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
4269         memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
4270         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4271                         "(%d):0380 READ_REV Status x%x "
4272                         "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
4273                         mboxq->vport ? mboxq->vport->vpi : 0,
4274                         bf_get(lpfc_mqe_status, mqe),
4275                         phba->vpd.rev.opFwName,
4276                         phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
4277                         phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
4278
4279         /*
4280          * Discover the port's supported feature set and match it against the
4281          * hosts requests.
4282          */
4283         lpfc_request_features(phba, mboxq);
4284         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4285         if (unlikely(rc)) {
4286                 rc = -EIO;
4287                 goto out_free_vpd;
4288         }
4289
4290         /*
4291          * The port must support FCP initiator mode as this is the
4292          * only mode running in the host.
4293          */
4294         if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
4295                 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
4296                                 "0378 No support for fcpi mode.\n");
4297                 ftr_rsp++;
4298         }
4299
4300         /*
4301          * If the port cannot support the host's requested features
4302          * then turn off the global config parameters to disable the
4303          * feature in the driver.  This is not a fatal error.
4304          */
4305         if ((phba->cfg_enable_bg) &&
4306             !(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
4307                 ftr_rsp++;
4308
4309         if (phba->max_vpi && phba->cfg_enable_npiv &&
4310             !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
4311                 ftr_rsp++;
4312
4313         if (ftr_rsp) {
4314                 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
4315                                 "0379 Feature Mismatch Data: x%08x %08x "
4316                                 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
4317                                 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
4318                                 phba->cfg_enable_npiv, phba->max_vpi);
4319                 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
4320                         phba->cfg_enable_bg = 0;
4321                 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
4322                         phba->cfg_enable_npiv = 0;
4323         }
4324
4325         /* These SLI3 features are assumed in SLI4 */
4326         spin_lock_irq(&phba->hbalock);
4327         phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
4328         spin_unlock_irq(&phba->hbalock);
4329
4330         /* Read the port's service parameters. */
4331         lpfc_read_sparam(phba, mboxq, vport->vpi);
4332         mboxq->vport = vport;
4333         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4334         mp = (struct lpfc_dmabuf *) mboxq->context1;
4335         if (rc == MBX_SUCCESS) {
4336                 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
4337                 rc = 0;
4338         }
4339
4340         /*
4341          * This memory was allocated by the lpfc_read_sparam routine. Release
4342          * it to the mbuf pool.
4343          */
4344         lpfc_mbuf_free(phba, mp->virt, mp->phys);
4345         kfree(mp);
4346         mboxq->context1 = NULL;
4347         if (unlikely(rc)) {
4348                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4349                                 "0382 READ_SPARAM command failed "
4350                                 "status %d, mbxStatus x%x\n",
4351                                 rc, bf_get(lpfc_mqe_status, mqe));
4352                 phba->link_state = LPFC_HBA_ERROR;
4353                 rc = -EIO;
4354                 goto out_free_vpd;
4355         }
4356
4357         if (phba->cfg_soft_wwnn)
4358                 u64_to_wwn(phba->cfg_soft_wwnn,
4359                            vport->fc_sparam.nodeName.u.wwn);
4360         if (phba->cfg_soft_wwpn)
4361                 u64_to_wwn(phba->cfg_soft_wwpn,
4362                            vport->fc_sparam.portName.u.wwn);
4363         memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
4364                sizeof(struct lpfc_name));
4365         memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
4366                sizeof(struct lpfc_name));
4367
4368         /* Update the fc_host data structures with new wwn. */
4369         fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
4370         fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
4371
4372         /* Register SGL pool to the device using non-embedded mailbox command */
4373         rc = lpfc_sli4_post_sgl_list(phba);
4374         if (unlikely(rc)) {
4375                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4376                                 "0582 Error %d during sgl post operation\n",
4377                                         rc);
4378                 rc = -ENODEV;
4379                 goto out_free_vpd;
4380         }
4381
4382         /* Register SCSI SGL pool to the device */
4383         rc = lpfc_sli4_repost_scsi_sgl_list(phba);
4384         if (unlikely(rc)) {
4385                 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
4386                                 "0383 Error %d during scsi sgl post "
4387                                 "operation\n", rc);
4388                 /* Some Scsi buffers were moved to the abort scsi list */
4389                 /* A pci function reset will repost them */
4390                 rc = -ENODEV;
4391                 goto out_free_vpd;
4392         }
4393
4394         /* Post the rpi header region to the device. */
4395         rc = lpfc_sli4_post_all_rpi_hdrs(phba);
4396         if (unlikely(rc)) {
4397                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4398                                 "0393 Error %d during rpi post operation\n",
4399                                 rc);
4400                 rc = -ENODEV;
4401                 goto out_free_vpd;
4402         }
4403
4404         /* Set up all the queues to the device */
4405         rc = lpfc_sli4_queue_setup(phba);
4406         if (unlikely(rc)) {
4407                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4408                                 "0381 Error %d during queue setup.\n ", rc);
4409                 goto out_stop_timers;
4410         }
4411
4412         /* Arm the CQs and then EQs on device */
4413         lpfc_sli4_arm_cqeq_intr(phba);
4414
4415         /* Indicate device interrupt mode */
4416         phba->sli4_hba.intr_enable = 1;
4417
4418         /* Allow asynchronous mailbox command to go through */
4419         spin_lock_irq(&phba->hbalock);
4420         phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4421         spin_unlock_irq(&phba->hbalock);
4422
4423         /* Post receive buffers to the device */
4424         lpfc_sli4_rb_setup(phba);
4425
4426         /* Start the ELS watchdog timer */
4427         mod_timer(&vport->els_tmofunc,
4428                   jiffies + HZ * (phba->fc_ratov * 2));
4429
4430         /* Start heart beat timer */
4431         mod_timer(&phba->hb_tmofunc,
4432                   jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
4433         phba->hb_outstanding = 0;
4434         phba->last_completion_time = jiffies;
4435
4436         /* Start error attention (ERATT) polling timer */
4437         mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
4438
4439         /*
4440          * The port is ready, set the host's link state to LINK_DOWN
4441          * in preparation for link interrupts.
4442          */
4443         lpfc_init_link(phba, mboxq, phba->cfg_topology, phba->cfg_link_speed);
4444         mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4445         lpfc_set_loopback_flag(phba);
4446         /* Change driver state to LPFC_LINK_DOWN right before init link */
4447         spin_lock_irq(&phba->hbalock);
4448         phba->link_state = LPFC_LINK_DOWN;
4449         spin_unlock_irq(&phba->hbalock);
4450         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
4451         if (unlikely(rc != MBX_NOT_FINISHED)) {
4452                 kfree(vpd);
4453                 return 0;
4454         } else
4455                 rc = -EIO;
4456
4457         /* Unset all the queues set up in this routine when error out */
4458         if (rc)
4459                 lpfc_sli4_queue_unset(phba);
4460
4461 out_stop_timers:
4462         if (rc)
4463                 lpfc_stop_hba_timers(phba);
4464 out_free_vpd:
4465         kfree(vpd);
4466 out_free_mbox:
4467         mempool_free(mboxq, phba->mbox_mem_pool);
4468         return rc;
4469 }
4470
4471 /**
4472  * lpfc_mbox_timeout - Timeout call back function for mbox timer
4473  * @ptr: context object - pointer to hba structure.
4474  *
4475  * This is the callback function for mailbox timer. The mailbox
4476  * timer is armed when a new mailbox command is issued and the timer
4477  * is deleted when the mailbox complete. The function is called by
4478  * the kernel timer code when a mailbox does not complete within
4479  * expected time. This function wakes up the worker thread to
4480  * process the mailbox timeout and returns. All the processing is
4481  * done by the worker thread function lpfc_mbox_timeout_handler.
4482  **/
4483 void
4484 lpfc_mbox_timeout(unsigned long ptr)
4485 {
4486         struct lpfc_hba  *phba = (struct lpfc_hba *) ptr;
4487         unsigned long iflag;
4488         uint32_t tmo_posted;
4489
4490         spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
4491         tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
4492         if (!tmo_posted)
4493                 phba->pport->work_port_events |= WORKER_MBOX_TMO;
4494         spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
4495
4496         if (!tmo_posted)
4497                 lpfc_worker_wake_up(phba);
4498         return;
4499 }
4500
4501
4502 /**
4503  * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
4504  * @phba: Pointer to HBA context object.
4505  *
4506  * This function is called from worker thread when a mailbox command times out.
4507  * The caller is not required to hold any locks. This function will reset the
4508  * HBA and recover all the pending commands.
4509  **/
4510 void
4511 lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
4512 {
4513         LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
4514         MAILBOX_t *mb = &pmbox->u.mb;
4515         struct lpfc_sli *psli = &phba->sli;
4516         struct lpfc_sli_ring *pring;
4517
4518         /* Check the pmbox pointer first.  There is a race condition
4519          * between the mbox timeout handler getting executed in the
4520          * worklist and the mailbox actually completing. When this
4521          * race condition occurs, the mbox_active will be NULL.
4522          */
4523         spin_lock_irq(&phba->hbalock);
4524         if (pmbox == NULL) {
4525                 lpfc_printf_log(phba, KERN_WARNING,
4526                                 LOG_MBOX | LOG_SLI,
4527                                 "0353 Active Mailbox cleared - mailbox timeout "
4528                                 "exiting\n");
4529                 spin_unlock_irq(&phba->hbalock);
4530                 return;
4531         }
4532
4533         /* Mbox cmd <mbxCommand> timeout */
4534         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4535                         "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
4536                         mb->mbxCommand,
4537                         phba->pport->port_state,
4538                         phba->sli.sli_flag,
4539                         phba->sli.mbox_active);
4540         spin_unlock_irq(&phba->hbalock);
4541
4542         /* Setting state unknown so lpfc_sli_abort_iocb_ring
4543          * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
4544          * it to fail all oustanding SCSI IO.
4545          */
4546         spin_lock_irq(&phba->pport->work_port_lock);
4547         phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
4548         spin_unlock_irq(&phba->pport->work_port_lock);
4549         spin_lock_irq(&phba->hbalock);
4550         phba->link_state = LPFC_LINK_UNKNOWN;
4551         psli->sli_flag &= ~LPFC_SLI_ACTIVE;
4552         spin_unlock_irq(&phba->hbalock);
4553
4554         pring = &psli->ring[psli->fcp_ring];
4555         lpfc_sli_abort_iocb_ring(phba, pring);
4556
4557         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4558                         "0345 Resetting board due to mailbox timeout\n");
4559
4560         /* Reset the HBA device */
4561         lpfc_reset_hba(phba);
4562 }
4563
4564 /**
4565  * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
4566  * @phba: Pointer to HBA context object.
4567  * @pmbox: Pointer to mailbox object.
4568  * @flag: Flag indicating how the mailbox need to be processed.
4569  *
4570  * This function is called by discovery code and HBA management code
4571  * to submit a mailbox command to firmware with SLI-3 interface spec. This
4572  * function gets the hbalock to protect the data structures.
4573  * The mailbox command can be submitted in polling mode, in which case
4574  * this function will wait in a polling loop for the completion of the
4575  * mailbox.
4576  * If the mailbox is submitted in no_wait mode (not polling) the
4577  * function will submit the command and returns immediately without waiting
4578  * for the mailbox completion. The no_wait is supported only when HBA
4579  * is in SLI2/SLI3 mode - interrupts are enabled.
4580  * The SLI interface allows only one mailbox pending at a time. If the
4581  * mailbox is issued in polling mode and there is already a mailbox
4582  * pending, then the function will return an error. If the mailbox is issued
4583  * in NO_WAIT mode and there is a mailbox pending already, the function
4584  * will return MBX_BUSY after queuing the mailbox into mailbox queue.
4585  * The sli layer owns the mailbox object until the completion of mailbox
4586  * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
4587  * return codes the caller owns the mailbox command after the return of
4588  * the function.
4589  **/
4590 static int
4591 lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
4592                        uint32_t flag)
4593 {
4594         MAILBOX_t *mb;
4595         struct lpfc_sli *psli = &phba->sli;
4596         uint32_t status, evtctr;
4597         uint32_t ha_copy;
4598         int i;
4599         unsigned long timeout;
4600         unsigned long drvr_flag = 0;
4601         uint32_t word0, ldata;
4602         void __iomem *to_slim;
4603         int processing_queue = 0;
4604
4605         spin_lock_irqsave(&phba->hbalock, drvr_flag);
4606         if (!pmbox) {
4607                 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
4608                 /* processing mbox queue from intr_handler */
4609                 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
4610                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4611                         return MBX_SUCCESS;
4612                 }
4613                 processing_queue = 1;
4614                 pmbox = lpfc_mbox_get(phba);
4615                 if (!pmbox) {
4616                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4617                         return MBX_SUCCESS;
4618                 }
4619         }
4620
4621         if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
4622                 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
4623                 if(!pmbox->vport) {
4624                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4625                         lpfc_printf_log(phba, KERN_ERR,
4626                                         LOG_MBOX | LOG_VPORT,
4627                                         "1806 Mbox x%x failed. No vport\n",
4628                                         pmbox->u.mb.mbxCommand);
4629                         dump_stack();
4630                         goto out_not_finished;
4631                 }
4632         }
4633
4634         /* If the PCI channel is in offline state, do not post mbox. */
4635         if (unlikely(pci_channel_offline(phba->pcidev))) {
4636                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4637                 goto out_not_finished;
4638         }
4639
4640         /* If HBA has a deferred error attention, fail the iocb. */
4641         if (unlikely(phba->hba_flag & DEFER_ERATT)) {
4642                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4643                 goto out_not_finished;
4644         }
4645
4646         psli = &phba->sli;
4647
4648         mb = &pmbox->u.mb;
4649         status = MBX_SUCCESS;
4650
4651         if (phba->link_state == LPFC_HBA_ERROR) {
4652                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4653
4654                 /* Mbox command <mbxCommand> cannot issue */
4655                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4656                                 "(%d):0311 Mailbox command x%x cannot "
4657                                 "issue Data: x%x x%x\n",
4658                                 pmbox->vport ? pmbox->vport->vpi : 0,
4659                                 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
4660                 goto out_not_finished;
4661         }
4662
4663         if (mb->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT &&
4664             !(readl(phba->HCregaddr) & HC_MBINT_ENA)) {
4665                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4666                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4667                                 "(%d):2528 Mailbox command x%x cannot "
4668                                 "issue Data: x%x x%x\n",
4669                                 pmbox->vport ? pmbox->vport->vpi : 0,
4670                                 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
4671                 goto out_not_finished;
4672         }
4673
4674         if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
4675                 /* Polling for a mbox command when another one is already active
4676                  * is not allowed in SLI. Also, the driver must have established
4677                  * SLI2 mode to queue and process multiple mbox commands.
4678                  */
4679
4680                 if (flag & MBX_POLL) {
4681                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4682
4683                         /* Mbox command <mbxCommand> cannot issue */
4684                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4685                                         "(%d):2529 Mailbox command x%x "
4686                                         "cannot issue Data: x%x x%x\n",
4687                                         pmbox->vport ? pmbox->vport->vpi : 0,
4688                                         pmbox->u.mb.mbxCommand,
4689                                         psli->sli_flag, flag);
4690                         goto out_not_finished;
4691                 }
4692
4693                 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
4694                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4695                         /* Mbox command <mbxCommand> cannot issue */
4696                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4697                                         "(%d):2530 Mailbox command x%x "
4698                                         "cannot issue Data: x%x x%x\n",
4699                                         pmbox->vport ? pmbox->vport->vpi : 0,
4700                                         pmbox->u.mb.mbxCommand,
4701                                         psli->sli_flag, flag);
4702                         goto out_not_finished;
4703                 }
4704
4705                 /* Another mailbox command is still being processed, queue this
4706                  * command to be processed later.
4707                  */
4708                 lpfc_mbox_put(phba, pmbox);
4709
4710                 /* Mbox cmd issue - BUSY */
4711                 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4712                                 "(%d):0308 Mbox cmd issue - BUSY Data: "
4713                                 "x%x x%x x%x x%x\n",
4714                                 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
4715                                 mb->mbxCommand, phba->pport->port_state,
4716                                 psli->sli_flag, flag);
4717
4718                 psli->slistat.mbox_busy++;
4719                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4720
4721                 if (pmbox->vport) {
4722                         lpfc_debugfs_disc_trc(pmbox->vport,
4723                                 LPFC_DISC_TRC_MBOX_VPORT,
4724                                 "MBOX Bsy vport:  cmd:x%x mb:x%x x%x",
4725                                 (uint32_t)mb->mbxCommand,
4726                                 mb->un.varWords[0], mb->un.varWords[1]);
4727                 }
4728                 else {
4729                         lpfc_debugfs_disc_trc(phba->pport,
4730                                 LPFC_DISC_TRC_MBOX,
4731                                 "MBOX Bsy:        cmd:x%x mb:x%x x%x",
4732                                 (uint32_t)mb->mbxCommand,
4733                                 mb->un.varWords[0], mb->un.varWords[1]);
4734                 }
4735
4736                 return MBX_BUSY;
4737         }
4738
4739         psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
4740
4741         /* If we are not polling, we MUST be in SLI2 mode */
4742         if (flag != MBX_POLL) {
4743                 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
4744                     (mb->mbxCommand != MBX_KILL_BOARD)) {
4745                         psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
4746                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4747                         /* Mbox command <mbxCommand> cannot issue */
4748                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4749                                         "(%d):2531 Mailbox command x%x "
4750                                         "cannot issue Data: x%x x%x\n",
4751                                         pmbox->vport ? pmbox->vport->vpi : 0,
4752                                         pmbox->u.mb.mbxCommand,
4753                                         psli->sli_flag, flag);
4754                         goto out_not_finished;
4755                 }
4756                 /* timeout active mbox command */
4757                 mod_timer(&psli->mbox_tmo, (jiffies +
4758                                (HZ * lpfc_mbox_tmo_val(phba, mb->mbxCommand))));
4759         }
4760
4761         /* Mailbox cmd <cmd> issue */
4762         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4763                         "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
4764                         "x%x\n",
4765                         pmbox->vport ? pmbox->vport->vpi : 0,
4766                         mb->mbxCommand, phba->pport->port_state,
4767                         psli->sli_flag, flag);
4768
4769         if (mb->mbxCommand != MBX_HEARTBEAT) {
4770                 if (pmbox->vport) {
4771                         lpfc_debugfs_disc_trc(pmbox->vport,
4772                                 LPFC_DISC_TRC_MBOX_VPORT,
4773                                 "MBOX Send vport: cmd:x%x mb:x%x x%x",
4774                                 (uint32_t)mb->mbxCommand,
4775                                 mb->un.varWords[0], mb->un.varWords[1]);
4776                 }
4777                 else {
4778                         lpfc_debugfs_disc_trc(phba->pport,
4779                                 LPFC_DISC_TRC_MBOX,
4780                                 "MBOX Send:       cmd:x%x mb:x%x x%x",
4781                                 (uint32_t)mb->mbxCommand,
4782                                 mb->un.varWords[0], mb->un.varWords[1]);
4783                 }
4784         }
4785
4786         psli->slistat.mbox_cmd++;
4787         evtctr = psli->slistat.mbox_event;
4788
4789         /* next set own bit for the adapter and copy over command word */
4790         mb->mbxOwner = OWN_CHIP;
4791
4792         if (psli->sli_flag & LPFC_SLI_ACTIVE) {
4793                 /* First copy command data to host SLIM area */
4794                 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
4795         } else {
4796                 if (mb->mbxCommand == MBX_CONFIG_PORT) {
4797                         /* copy command data into host mbox for cmpl */
4798                         lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
4799                 }
4800
4801                 /* First copy mbox command data to HBA SLIM, skip past first
4802                    word */
4803                 to_slim = phba->MBslimaddr + sizeof (uint32_t);
4804                 lpfc_memcpy_to_slim(to_slim, &mb->un.varWords[0],
4805                             MAILBOX_CMD_SIZE - sizeof (uint32_t));
4806
4807                 /* Next copy over first word, with mbxOwner set */
4808                 ldata = *((uint32_t *)mb);
4809                 to_slim = phba->MBslimaddr;
4810                 writel(ldata, to_slim);
4811                 readl(to_slim); /* flush */
4812
4813                 if (mb->mbxCommand == MBX_CONFIG_PORT) {
4814                         /* switch over to host mailbox */
4815                         psli->sli_flag |= LPFC_SLI_ACTIVE;
4816                 }
4817         }
4818
4819         wmb();
4820
4821         switch (flag) {
4822         case MBX_NOWAIT:
4823                 /* Set up reference to mailbox command */
4824                 psli->mbox_active = pmbox;
4825                 /* Interrupt board to do it */
4826                 writel(CA_MBATT, phba->CAregaddr);
4827                 readl(phba->CAregaddr); /* flush */
4828                 /* Don't wait for it to finish, just return */
4829                 break;
4830
4831         case MBX_POLL:
4832                 /* Set up null reference to mailbox command */
4833                 psli->mbox_active = NULL;
4834                 /* Interrupt board to do it */
4835                 writel(CA_MBATT, phba->CAregaddr);
4836                 readl(phba->CAregaddr); /* flush */
4837
4838                 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
4839                         /* First read mbox status word */
4840                         word0 = *((uint32_t *)phba->mbox);
4841                         word0 = le32_to_cpu(word0);
4842                 } else {
4843                         /* First read mbox status word */
4844                         word0 = readl(phba->MBslimaddr);
4845                 }
4846
4847                 /* Read the HBA Host Attention Register */
4848                 ha_copy = readl(phba->HAregaddr);
4849                 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
4850                                                              mb->mbxCommand) *
4851                                            1000) + jiffies;
4852                 i = 0;
4853                 /* Wait for command to complete */
4854                 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
4855                        (!(ha_copy & HA_MBATT) &&
4856                         (phba->link_state > LPFC_WARM_START))) {
4857                         if (time_after(jiffies, timeout)) {
4858                                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
4859                                 spin_unlock_irqrestore(&phba->hbalock,
4860                                                        drvr_flag);
4861                                 goto out_not_finished;
4862                         }
4863
4864                         /* Check if we took a mbox interrupt while we were
4865                            polling */
4866                         if (((word0 & OWN_CHIP) != OWN_CHIP)
4867                             && (evtctr != psli->slistat.mbox_event))
4868                                 break;
4869
4870                         if (i++ > 10) {
4871                                 spin_unlock_irqrestore(&phba->hbalock,
4872                                                        drvr_flag);
4873                                 msleep(1);
4874                                 spin_lock_irqsave(&phba->hbalock, drvr_flag);
4875                         }
4876
4877                         if (psli->sli_flag & LPFC_SLI_ACTIVE) {
4878                                 /* First copy command data */
4879                                 word0 = *((uint32_t *)phba->mbox);
4880                                 word0 = le32_to_cpu(word0);
4881                                 if (mb->mbxCommand == MBX_CONFIG_PORT) {
4882                                         MAILBOX_t *slimmb;
4883                                         uint32_t slimword0;
4884                                         /* Check real SLIM for any errors */
4885                                         slimword0 = readl(phba->MBslimaddr);
4886                                         slimmb = (MAILBOX_t *) & slimword0;
4887                                         if (((slimword0 & OWN_CHIP) != OWN_CHIP)
4888                                             && slimmb->mbxStatus) {
4889                                                 psli->sli_flag &=
4890                                                     ~LPFC_SLI_ACTIVE;
4891                                                 word0 = slimword0;
4892                                         }
4893                                 }
4894                         } else {
4895                                 /* First copy command data */
4896                                 word0 = readl(phba->MBslimaddr);
4897                         }
4898                         /* Read the HBA Host Attention Register */
4899                         ha_copy = readl(phba->HAregaddr);
4900                 }
4901
4902                 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
4903                         /* copy results back to user */
4904                         lpfc_sli_pcimem_bcopy(phba->mbox, mb, MAILBOX_CMD_SIZE);
4905                 } else {
4906                         /* First copy command data */
4907                         lpfc_memcpy_from_slim(mb, phba->MBslimaddr,
4908                                                         MAILBOX_CMD_SIZE);
4909                         if ((mb->mbxCommand == MBX_DUMP_MEMORY) &&
4910                                 pmbox->context2) {
4911                                 lpfc_memcpy_from_slim((void *)pmbox->context2,
4912                                       phba->MBslimaddr + DMP_RSP_OFFSET,
4913                                                       mb->un.varDmp.word_cnt);
4914                         }
4915                 }
4916
4917                 writel(HA_MBATT, phba->HAregaddr);
4918                 readl(phba->HAregaddr); /* flush */
4919
4920                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
4921                 status = mb->mbxStatus;
4922         }
4923
4924         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4925         return status;
4926
4927 out_not_finished:
4928         if (processing_queue) {
4929                 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
4930                 lpfc_mbox_cmpl_put(phba, pmbox);
4931         }
4932         return MBX_NOT_FINISHED;
4933 }
4934
4935 /**
4936  * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
4937  * @phba: Pointer to HBA context object.
4938  *
4939  * The function blocks the posting of SLI4 asynchronous mailbox commands from
4940  * the driver internal pending mailbox queue. It will then try to wait out the
4941  * possible outstanding mailbox command before return.
4942  *
4943  * Returns:
4944  *      0 - the outstanding mailbox command completed; otherwise, the wait for
4945  *      the outstanding mailbox command timed out.
4946  **/
4947 static int
4948 lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
4949 {
4950         struct lpfc_sli *psli = &phba->sli;
4951         uint8_t actcmd = MBX_HEARTBEAT;
4952         int rc = 0;
4953         unsigned long timeout;
4954
4955         /* Mark the asynchronous mailbox command posting as blocked */
4956         spin_lock_irq(&phba->hbalock);
4957         psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
4958         if (phba->sli.mbox_active)
4959                 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
4960         spin_unlock_irq(&phba->hbalock);
4961         /* Determine how long we might wait for the active mailbox
4962          * command to be gracefully completed by firmware.
4963          */
4964         timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, actcmd) * 1000) +
4965                                    jiffies;
4966         /* Wait for the outstnading mailbox command to complete */
4967         while (phba->sli.mbox_active) {
4968                 /* Check active mailbox complete status every 2ms */
4969                 msleep(2);
4970                 if (time_after(jiffies, timeout)) {
4971                         /* Timeout, marked the outstanding cmd not complete */
4972                         rc = 1;
4973                         break;
4974                 }
4975         }
4976
4977         /* Can not cleanly block async mailbox command, fails it */
4978         if (rc) {
4979                 spin_lock_irq(&phba->hbalock);
4980                 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4981                 spin_unlock_irq(&phba->hbalock);
4982         }
4983         return rc;
4984 }
4985
4986 /**
4987  * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
4988  * @phba: Pointer to HBA context object.
4989  *
4990  * The function unblocks and resume posting of SLI4 asynchronous mailbox
4991  * commands from the driver internal pending mailbox queue. It makes sure
4992  * that there is no outstanding mailbox command before resuming posting
4993  * asynchronous mailbox commands. If, for any reason, there is outstanding
4994  * mailbox command, it will try to wait it out before resuming asynchronous
4995  * mailbox command posting.
4996  **/
4997 static void
4998 lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
4999 {
5000         struct lpfc_sli *psli = &phba->sli;
5001
5002         spin_lock_irq(&phba->hbalock);
5003         if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
5004                 /* Asynchronous mailbox posting is not blocked, do nothing */
5005                 spin_unlock_irq(&phba->hbalock);
5006                 return;
5007         }
5008
5009         /* Outstanding synchronous mailbox command is guaranteed to be done,
5010          * successful or timeout, after timing-out the outstanding mailbox
5011          * command shall always be removed, so just unblock posting async
5012          * mailbox command and resume
5013          */
5014         psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
5015         spin_unlock_irq(&phba->hbalock);
5016
5017         /* wake up worker thread to post asynchronlous mailbox command */
5018         lpfc_worker_wake_up(phba);
5019 }
5020
5021 /**
5022  * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
5023  * @phba: Pointer to HBA context object.
5024  * @mboxq: Pointer to mailbox object.
5025  *
5026  * The function posts a mailbox to the port.  The mailbox is expected
5027  * to be comletely filled in and ready for the port to operate on it.
5028  * This routine executes a synchronous completion operation on the
5029  * mailbox by polling for its completion.
5030  *
5031  * The caller must not be holding any locks when calling this routine.
5032  *
5033  * Returns:
5034  *      MBX_SUCCESS - mailbox posted successfully
5035  *      Any of the MBX error values.
5036  **/
5037 static int
5038 lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
5039 {
5040         int rc = MBX_SUCCESS;
5041         unsigned long iflag;
5042         uint32_t db_ready;
5043         uint32_t mcqe_status;
5044         uint32_t mbx_cmnd;
5045         unsigned long timeout;
5046         struct lpfc_sli *psli = &phba->sli;
5047         struct lpfc_mqe *mb = &mboxq->u.mqe;
5048         struct lpfc_bmbx_create *mbox_rgn;
5049         struct dma_address *dma_address;
5050         struct lpfc_register bmbx_reg;
5051
5052         /*
5053          * Only one mailbox can be active to the bootstrap mailbox region
5054          * at a time and there is no queueing provided.
5055          */
5056         spin_lock_irqsave(&phba->hbalock, iflag);
5057         if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
5058                 spin_unlock_irqrestore(&phba->hbalock, iflag);
5059                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5060                                 "(%d):2532 Mailbox command x%x (x%x) "
5061                                 "cannot issue Data: x%x x%x\n",
5062                                 mboxq->vport ? mboxq->vport->vpi : 0,
5063                                 mboxq->u.mb.mbxCommand,
5064                                 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5065                                 psli->sli_flag, MBX_POLL);
5066                 return MBXERR_ERROR;
5067         }
5068         /* The server grabs the token and owns it until release */
5069         psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
5070         phba->sli.mbox_active = mboxq;
5071         spin_unlock_irqrestore(&phba->hbalock, iflag);
5072
5073         /*
5074          * Initialize the bootstrap memory region to avoid stale data areas
5075          * in the mailbox post.  Then copy the caller's mailbox contents to
5076          * the bmbx mailbox region.
5077          */
5078         mbx_cmnd = bf_get(lpfc_mqe_command, mb);
5079         memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
5080         lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
5081                               sizeof(struct lpfc_mqe));
5082
5083         /* Post the high mailbox dma address to the port and wait for ready. */
5084         dma_address = &phba->sli4_hba.bmbx.dma_address;
5085         writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
5086
5087         timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mbx_cmnd)
5088                                    * 1000) + jiffies;
5089         do {
5090                 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
5091                 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
5092                 if (!db_ready)
5093                         msleep(2);
5094
5095                 if (time_after(jiffies, timeout)) {
5096                         rc = MBXERR_ERROR;
5097                         goto exit;
5098                 }
5099         } while (!db_ready);
5100
5101         /* Post the low mailbox dma address to the port. */
5102         writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
5103         timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mbx_cmnd)
5104                                    * 1000) + jiffies;
5105         do {
5106                 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
5107                 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
5108                 if (!db_ready)
5109                         msleep(2);
5110
5111                 if (time_after(jiffies, timeout)) {
5112                         rc = MBXERR_ERROR;
5113                         goto exit;
5114                 }
5115         } while (!db_ready);
5116
5117         /*
5118          * Read the CQ to ensure the mailbox has completed.
5119          * If so, update the mailbox status so that the upper layers
5120          * can complete the request normally.
5121          */
5122         lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
5123                               sizeof(struct lpfc_mqe));
5124         mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
5125         lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
5126                               sizeof(struct lpfc_mcqe));
5127         mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
5128
5129         /* Prefix the mailbox status with range x4000 to note SLI4 status. */
5130         if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
5131                 bf_set(lpfc_mqe_status, mb, LPFC_MBX_ERROR_RANGE | mcqe_status);
5132                 rc = MBXERR_ERROR;
5133         }
5134
5135         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5136                         "(%d):0356 Mailbox cmd x%x (x%x) Status x%x "
5137                         "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
5138                         " x%x x%x CQ: x%x x%x x%x x%x\n",
5139                         mboxq->vport ? mboxq->vport->vpi : 0,
5140                         mbx_cmnd, lpfc_sli4_mbox_opcode_get(phba, mboxq),
5141                         bf_get(lpfc_mqe_status, mb),
5142                         mb->un.mb_words[0], mb->un.mb_words[1],
5143                         mb->un.mb_words[2], mb->un.mb_words[3],
5144                         mb->un.mb_words[4], mb->un.mb_words[5],
5145                         mb->un.mb_words[6], mb->un.mb_words[7],
5146                         mb->un.mb_words[8], mb->un.mb_words[9],
5147                         mb->un.mb_words[10], mb->un.mb_words[11],
5148                         mb->un.mb_words[12], mboxq->mcqe.word0,
5149                         mboxq->mcqe.mcqe_tag0,  mboxq->mcqe.mcqe_tag1,
5150                         mboxq->mcqe.trailer);
5151 exit:
5152         /* We are holding the token, no needed for lock when release */
5153         spin_lock_irqsave(&phba->hbalock, iflag);
5154         psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5155         phba->sli.mbox_active = NULL;
5156         spin_unlock_irqrestore(&phba->hbalock, iflag);
5157         return rc;
5158 }
5159
5160 /**
5161  * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
5162  * @phba: Pointer to HBA context object.
5163  * @pmbox: Pointer to mailbox object.
5164  * @flag: Flag indicating how the mailbox need to be processed.
5165  *
5166  * This function is called by discovery code and HBA management code to submit
5167  * a mailbox command to firmware with SLI-4 interface spec.
5168  *
5169  * Return codes the caller owns the mailbox command after the return of the
5170  * function.
5171  **/
5172 static int
5173 lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
5174                        uint32_t flag)
5175 {
5176         struct lpfc_sli *psli = &phba->sli;
5177         unsigned long iflags;
5178         int rc;
5179
5180         rc = lpfc_mbox_dev_check(phba);
5181         if (unlikely(rc)) {
5182                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5183                                 "(%d):2544 Mailbox command x%x (x%x) "
5184                                 "cannot issue Data: x%x x%x\n",
5185                                 mboxq->vport ? mboxq->vport->vpi : 0,
5186                                 mboxq->u.mb.mbxCommand,
5187                                 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5188                                 psli->sli_flag, flag);
5189                 goto out_not_finished;
5190         }
5191
5192         /* Detect polling mode and jump to a handler */
5193         if (!phba->sli4_hba.intr_enable) {
5194                 if (flag == MBX_POLL)
5195                         rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
5196                 else
5197                         rc = -EIO;
5198                 if (rc != MBX_SUCCESS)
5199                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5200                                         "(%d):2541 Mailbox command x%x "
5201                                         "(x%x) cannot issue Data: x%x x%x\n",
5202                                         mboxq->vport ? mboxq->vport->vpi : 0,
5203                                         mboxq->u.mb.mbxCommand,
5204                                         lpfc_sli4_mbox_opcode_get(phba, mboxq),
5205                                         psli->sli_flag, flag);
5206                 return rc;
5207         } else if (flag == MBX_POLL) {
5208                 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
5209                                 "(%d):2542 Try to issue mailbox command "
5210                                 "x%x (x%x) synchronously ahead of async"
5211                                 "mailbox command queue: x%x x%x\n",
5212                                 mboxq->vport ? mboxq->vport->vpi : 0,
5213                                 mboxq->u.mb.mbxCommand,
5214                                 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5215                                 psli->sli_flag, flag);
5216                 /* Try to block the asynchronous mailbox posting */
5217                 rc = lpfc_sli4_async_mbox_block(phba);
5218                 if (!rc) {
5219                         /* Successfully blocked, now issue sync mbox cmd */
5220                         rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
5221                         if (rc != MBX_SUCCESS)
5222                                 lpfc_printf_log(phba, KERN_ERR,
5223                                                 LOG_MBOX | LOG_SLI,
5224                                                 "(%d):2597 Mailbox command "
5225                                                 "x%x (x%x) cannot issue "
5226                                                 "Data: x%x x%x\n",
5227                                                 mboxq->vport ?
5228                                                 mboxq->vport->vpi : 0,
5229                                                 mboxq->u.mb.mbxCommand,
5230                                                 lpfc_sli4_mbox_opcode_get(phba,
5231                                                                 mboxq),
5232                                                 psli->sli_flag, flag);
5233                         /* Unblock the async mailbox posting afterward */
5234                         lpfc_sli4_async_mbox_unblock(phba);
5235                 }
5236                 return rc;
5237         }
5238
5239         /* Now, interrupt mode asynchrous mailbox command */
5240         rc = lpfc_mbox_cmd_check(phba, mboxq);
5241         if (rc) {
5242                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5243                                 "(%d):2543 Mailbox command x%x (x%x) "
5244                                 "cannot issue Data: x%x x%x\n",
5245                                 mboxq->vport ? mboxq->vport->vpi : 0,
5246                                 mboxq->u.mb.mbxCommand,
5247                                 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5248                                 psli->sli_flag, flag);
5249                 goto out_not_finished;
5250         }
5251
5252         /* Put the mailbox command to the driver internal FIFO */
5253         psli->slistat.mbox_busy++;
5254         spin_lock_irqsave(&phba->hbalock, iflags);
5255         lpfc_mbox_put(phba, mboxq);
5256         spin_unlock_irqrestore(&phba->hbalock, iflags);
5257         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5258                         "(%d):0354 Mbox cmd issue - Enqueue Data: "
5259                         "x%x (x%x) x%x x%x x%x\n",
5260                         mboxq->vport ? mboxq->vport->vpi : 0xffffff,
5261                         bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5262                         lpfc_sli4_mbox_opcode_get(phba, mboxq),
5263                         phba->pport->port_state,
5264                         psli->sli_flag, MBX_NOWAIT);
5265         /* Wake up worker thread to transport mailbox command from head */
5266         lpfc_worker_wake_up(phba);
5267
5268         return MBX_BUSY;
5269
5270 out_not_finished:
5271         return MBX_NOT_FINISHED;
5272 }
5273
5274 /**
5275  * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
5276  * @phba: Pointer to HBA context object.
5277  *
5278  * This function is called by worker thread to send a mailbox command to
5279  * SLI4 HBA firmware.
5280  *
5281  **/
5282 int
5283 lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
5284 {
5285         struct lpfc_sli *psli = &phba->sli;
5286         LPFC_MBOXQ_t *mboxq;
5287         int rc = MBX_SUCCESS;
5288         unsigned long iflags;
5289         struct lpfc_mqe *mqe;
5290         uint32_t mbx_cmnd;
5291
5292         /* Check interrupt mode before post async mailbox command */
5293         if (unlikely(!phba->sli4_hba.intr_enable))
5294                 return MBX_NOT_FINISHED;
5295
5296         /* Check for mailbox command service token */
5297         spin_lock_irqsave(&phba->hbalock, iflags);
5298         if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
5299                 spin_unlock_irqrestore(&phba->hbalock, iflags);
5300                 return MBX_NOT_FINISHED;
5301         }
5302         if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
5303                 spin_unlock_irqrestore(&phba->hbalock, iflags);
5304                 return MBX_NOT_FINISHED;
5305         }
5306         if (unlikely(phba->sli.mbox_active)) {
5307                 spin_unlock_irqrestore(&phba->hbalock, iflags);
5308                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5309                                 "0384 There is pending active mailbox cmd\n");
5310                 return MBX_NOT_FINISHED;
5311         }
5312         /* Take the mailbox command service token */
5313         psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
5314
5315         /* Get the next mailbox command from head of queue */
5316         mboxq = lpfc_mbox_get(phba);
5317
5318         /* If no more mailbox command waiting for post, we're done */
5319         if (!mboxq) {
5320                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5321                 spin_unlock_irqrestore(&phba->hbalock, iflags);
5322                 return MBX_SUCCESS;
5323         }
5324         phba->sli.mbox_active = mboxq;
5325         spin_unlock_irqrestore(&phba->hbalock, iflags);
5326
5327         /* Check device readiness for posting mailbox command */
5328         rc = lpfc_mbox_dev_check(phba);
5329         if (unlikely(rc))
5330                 /* Driver clean routine will clean up pending mailbox */
5331                 goto out_not_finished;
5332
5333         /* Prepare the mbox command to be posted */
5334         mqe = &mboxq->u.mqe;
5335         mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
5336
5337         /* Start timer for the mbox_tmo and log some mailbox post messages */
5338         mod_timer(&psli->mbox_tmo, (jiffies +
5339                   (HZ * lpfc_mbox_tmo_val(phba, mbx_cmnd))));
5340
5341         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5342                         "(%d):0355 Mailbox cmd x%x (x%x) issue Data: "
5343                         "x%x x%x\n",
5344                         mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
5345                         lpfc_sli4_mbox_opcode_get(phba, mboxq),
5346                         phba->pport->port_state, psli->sli_flag);
5347
5348         if (mbx_cmnd != MBX_HEARTBEAT) {
5349                 if (mboxq->vport) {
5350                         lpfc_debugfs_disc_trc(mboxq->vport,
5351                                 LPFC_DISC_TRC_MBOX_VPORT,
5352                                 "MBOX Send vport: cmd:x%x mb:x%x x%x",
5353                                 mbx_cmnd, mqe->un.mb_words[0],
5354                                 mqe->un.mb_words[1]);
5355                 } else {
5356                         lpfc_debugfs_disc_trc(phba->pport,
5357                                 LPFC_DISC_TRC_MBOX,
5358                                 "MBOX Send: cmd:x%x mb:x%x x%x",
5359                                 mbx_cmnd, mqe->un.mb_words[0],
5360                                 mqe->un.mb_words[1]);
5361                 }
5362         }
5363         psli->slistat.mbox_cmd++;
5364
5365         /* Post the mailbox command to the port */
5366         rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
5367         if (rc != MBX_SUCCESS) {
5368                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5369                                 "(%d):2533 Mailbox command x%x (x%x) "
5370                                 "cannot issue Data: x%x x%x\n",
5371                                 mboxq->vport ? mboxq->vport->vpi : 0,
5372                                 mboxq->u.mb.mbxCommand,
5373                                 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5374                                 psli->sli_flag, MBX_NOWAIT);
5375                 goto out_not_finished;
5376         }
5377
5378         return rc;
5379
5380 out_not_finished:
5381         spin_lock_irqsave(&phba->hbalock, iflags);
5382         mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
5383         __lpfc_mbox_cmpl_put(phba, mboxq);
5384         /* Release the token */
5385         psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5386         phba->sli.mbox_active = NULL;
5387         spin_unlock_irqrestore(&phba->hbalock, iflags);
5388
5389         return MBX_NOT_FINISHED;
5390 }
5391
5392 /**
5393  * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
5394  * @phba: Pointer to HBA context object.
5395  * @pmbox: Pointer to mailbox object.
5396  * @flag: Flag indicating how the mailbox need to be processed.
5397  *
5398  * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
5399  * the API jump table function pointer from the lpfc_hba struct.
5400  *
5401  * Return codes the caller owns the mailbox command after the return of the
5402  * function.
5403  **/
5404 int
5405 lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
5406 {
5407         return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
5408 }
5409
5410 /**
5411  * lpfc_mbox_api_table_setup - Set up mbox api fucntion jump table
5412  * @phba: The hba struct for which this call is being executed.
5413  * @dev_grp: The HBA PCI-Device group number.
5414  *
5415  * This routine sets up the mbox interface API function jump table in @phba
5416  * struct.
5417  * Returns: 0 - success, -ENODEV - failure.
5418  **/
5419 int
5420 lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
5421 {
5422
5423         switch (dev_grp) {
5424         case LPFC_PCI_DEV_LP:
5425                 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
5426                 phba->lpfc_sli_handle_slow_ring_event =
5427                                 lpfc_sli_handle_slow_ring_event_s3;
5428                 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
5429                 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
5430                 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
5431                 break;
5432         case LPFC_PCI_DEV_OC:
5433                 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
5434                 phba->lpfc_sli_handle_slow_ring_event =
5435                                 lpfc_sli_handle_slow_ring_event_s4;
5436                 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
5437                 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
5438                 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
5439                 break;
5440         default:
5441                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5442                                 "1420 Invalid HBA PCI-device group: 0x%x\n",
5443                                 dev_grp);
5444                 return -ENODEV;
5445                 break;
5446         }
5447         return 0;
5448 }
5449
5450 /**
5451  * __lpfc_sli_ringtx_put - Add an iocb to the txq
5452  * @phba: Pointer to HBA context object.
5453  * @pring: Pointer to driver SLI ring object.
5454  * @piocb: Pointer to address of newly added command iocb.
5455  *
5456  * This function is called with hbalock held to add a command
5457  * iocb to the txq when SLI layer cannot submit the command iocb
5458  * to the ring.
5459  **/
5460 static void
5461 __lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
5462                     struct lpfc_iocbq *piocb)
5463 {
5464         /* Insert the caller's iocb in the txq tail for later processing. */
5465         list_add_tail(&piocb->list, &pring->txq);
5466         pring->txq_cnt++;
5467 }
5468
5469 /**
5470  * lpfc_sli_next_iocb - Get the next iocb in the txq
5471  * @phba: Pointer to HBA context object.
5472  * @pring: Pointer to driver SLI ring object.
5473  * @piocb: Pointer to address of newly added command iocb.
5474  *
5475  * This function is called with hbalock held before a new
5476  * iocb is submitted to the firmware. This function checks
5477  * txq to flush the iocbs in txq to Firmware before
5478  * submitting new iocbs to the Firmware.
5479  * If there are iocbs in the txq which need to be submitted
5480  * to firmware, lpfc_sli_next_iocb returns the first element
5481  * of the txq after dequeuing it from txq.
5482  * If there is no iocb in the txq then the function will return
5483  * *piocb and *piocb is set to NULL. Caller needs to check
5484  * *piocb to find if there are more commands in the txq.
5485  **/
5486 static struct lpfc_iocbq *
5487 lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
5488                    struct lpfc_iocbq **piocb)
5489 {
5490         struct lpfc_iocbq * nextiocb;
5491
5492         nextiocb = lpfc_sli_ringtx_get(phba, pring);
5493         if (!nextiocb) {
5494                 nextiocb = *piocb;
5495                 *piocb = NULL;
5496         }
5497
5498         return nextiocb;
5499 }
5500
5501 /**
5502  * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
5503  * @phba: Pointer to HBA context object.
5504  * @ring_number: SLI ring number to issue iocb on.
5505  * @piocb: Pointer to command iocb.
5506  * @flag: Flag indicating if this command can be put into txq.
5507  *
5508  * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
5509  * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
5510  * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
5511  * flag is turned on, the function returns IOCB_ERROR. When the link is down,
5512  * this function allows only iocbs for posting buffers. This function finds
5513  * next available slot in the command ring and posts the command to the
5514  * available slot and writes the port attention register to request HBA start
5515  * processing new iocb. If there is no slot available in the ring and
5516  * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
5517  * the function returns IOCB_BUSY.
5518  *
5519  * This function is called with hbalock held. The function will return success
5520  * after it successfully submit the iocb to firmware or after adding to the
5521  * txq.
5522  **/
5523 static int
5524 __lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
5525                     struct lpfc_iocbq *piocb, uint32_t flag)
5526 {
5527         struct lpfc_iocbq *nextiocb;
5528         IOCB_t *iocb;
5529         struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
5530
5531         if (piocb->iocb_cmpl && (!piocb->vport) &&
5532            (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
5533            (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
5534                 lpfc_printf_log(phba, KERN_ERR,
5535                                 LOG_SLI | LOG_VPORT,
5536                                 "1807 IOCB x%x failed. No vport\n",
5537                                 piocb->iocb.ulpCommand);
5538                 dump_stack();
5539                 return IOCB_ERROR;
5540         }
5541
5542
5543         /* If the PCI channel is in offline state, do not post iocbs. */
5544         if (unlikely(pci_channel_offline(phba->pcidev)))
5545                 return IOCB_ERROR;
5546
5547         /* If HBA has a deferred error attention, fail the iocb. */
5548         if (unlikely(phba->hba_flag & DEFER_ERATT))
5549                 return IOCB_ERROR;
5550
5551         /*
5552          * We should never get an IOCB if we are in a < LINK_DOWN state
5553          */
5554         if (unlikely(phba->link_state < LPFC_LINK_DOWN))
5555                 return IOCB_ERROR;
5556
5557         /*
5558          * Check to see if we are blocking IOCB processing because of a
5559          * outstanding event.
5560          */
5561         if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
5562                 goto iocb_busy;
5563
5564         if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
5565                 /*
5566                  * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
5567                  * can be issued if the link is not up.
5568                  */
5569                 switch (piocb->iocb.ulpCommand) {
5570                 case CMD_GEN_REQUEST64_CR:
5571                 case CMD_GEN_REQUEST64_CX:
5572                         if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
5573                                 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
5574                                         FC_RCTL_DD_UNSOL_CMD) ||
5575                                 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
5576                                         MENLO_TRANSPORT_TYPE))
5577
5578                                 goto iocb_busy;
5579                         break;
5580                 case CMD_QUE_RING_BUF_CN:
5581                 case CMD_QUE_RING_BUF64_CN:
5582                         /*
5583                          * For IOCBs, like QUE_RING_BUF, that have no rsp ring
5584                          * completion, iocb_cmpl MUST be 0.
5585                          */
5586                         if (piocb->iocb_cmpl)
5587                                 piocb->iocb_cmpl = NULL;
5588                         /*FALLTHROUGH*/
5589                 case CMD_CREATE_XRI_CR:
5590                 case CMD_CLOSE_XRI_CN:
5591                 case CMD_CLOSE_XRI_CX:
5592                         break;
5593                 default:
5594                         goto iocb_busy;
5595                 }
5596
5597         /*
5598          * For FCP commands, we must be in a state where we can process link
5599          * attention events.
5600          */
5601         } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
5602                             !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
5603                 goto iocb_busy;
5604         }
5605
5606         while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
5607                (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
5608                 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
5609
5610         if (iocb)
5611                 lpfc_sli_update_ring(phba, pring);
5612         else
5613                 lpfc_sli_update_full_ring(phba, pring);
5614
5615         if (!piocb)
5616                 return IOCB_SUCCESS;
5617
5618         goto out_busy;
5619
5620  iocb_busy:
5621         pring->stats.iocb_cmd_delay++;
5622
5623  out_busy:
5624
5625         if (!(flag & SLI_IOCB_RET_IOCB)) {
5626                 __lpfc_sli_ringtx_put(phba, pring, piocb);
5627                 return IOCB_SUCCESS;
5628         }
5629
5630         return IOCB_BUSY;
5631 }
5632
5633 /**
5634  * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
5635  * @phba: Pointer to HBA context object.
5636  * @piocb: Pointer to command iocb.
5637  * @sglq: Pointer to the scatter gather queue object.
5638  *
5639  * This routine converts the bpl or bde that is in the IOCB
5640  * to a sgl list for the sli4 hardware. The physical address
5641  * of the bpl/bde is converted back to a virtual address.
5642  * If the IOCB contains a BPL then the list of BDE's is
5643  * converted to sli4_sge's. If the IOCB contains a single
5644  * BDE then it is converted to a single sli_sge.
5645  * The IOCB is still in cpu endianess so the contents of
5646  * the bpl can be used without byte swapping.
5647  *
5648  * Returns valid XRI = Success, NO_XRI = Failure.
5649 **/
5650 static uint16_t
5651 lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
5652                 struct lpfc_sglq *sglq)
5653 {
5654         uint16_t xritag = NO_XRI;
5655         struct ulp_bde64 *bpl = NULL;
5656         struct ulp_bde64 bde;
5657         struct sli4_sge *sgl  = NULL;
5658         IOCB_t *icmd;
5659         int numBdes = 0;
5660         int i = 0;
5661
5662         if (!piocbq || !sglq)
5663                 return xritag;
5664
5665         sgl  = (struct sli4_sge *)sglq->sgl;
5666         icmd = &piocbq->iocb;
5667         if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
5668                 numBdes = icmd->un.genreq64.bdl.bdeSize /
5669                                 sizeof(struct ulp_bde64);
5670                 /* The addrHigh and addrLow fields within the IOCB
5671                  * have not been byteswapped yet so there is no
5672                  * need to swap them back.
5673                  */
5674                 bpl  = (struct ulp_bde64 *)
5675                         ((struct lpfc_dmabuf *)piocbq->context3)->virt;
5676
5677                 if (!bpl)
5678                         return xritag;
5679
5680                 for (i = 0; i < numBdes; i++) {
5681                         /* Should already be byte swapped. */
5682                         sgl->addr_hi =  bpl->addrHigh;
5683                         sgl->addr_lo =  bpl->addrLow;
5684                         /* swap the size field back to the cpu so we
5685                          * can assign it to the sgl.
5686                          */
5687                         bde.tus.w  = le32_to_cpu(bpl->tus.w);
5688                         bf_set(lpfc_sli4_sge_len, sgl, bde.tus.f.bdeSize);
5689                         if ((i+1) == numBdes)
5690                                 bf_set(lpfc_sli4_sge_last, sgl, 1);
5691                         else
5692                                 bf_set(lpfc_sli4_sge_last, sgl, 0);
5693                         sgl->word2 = cpu_to_le32(sgl->word2);
5694                         sgl->word3 = cpu_to_le32(sgl->word3);
5695                         bpl++;
5696                         sgl++;
5697                 }
5698         } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
5699                         /* The addrHigh and addrLow fields of the BDE have not
5700                          * been byteswapped yet so they need to be swapped
5701                          * before putting them in the sgl.
5702                          */
5703                         sgl->addr_hi =
5704                                 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
5705                         sgl->addr_lo =
5706                                 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
5707                         bf_set(lpfc_sli4_sge_len, sgl,
5708                                 icmd->un.genreq64.bdl.bdeSize);
5709                         bf_set(lpfc_sli4_sge_last, sgl, 1);
5710                         sgl->word2 = cpu_to_le32(sgl->word2);
5711                         sgl->word3 = cpu_to_le32(sgl->word3);
5712         }
5713         return sglq->sli4_xritag;
5714 }
5715
5716 /**
5717  * lpfc_sli4_scmd_to_wqidx_distr - scsi command to SLI4 WQ index distribution
5718  * @phba: Pointer to HBA context object.
5719  *
5720  * This routine performs a round robin SCSI command to SLI4 FCP WQ index
5721  * distribution.  This is called by __lpfc_sli_issue_iocb_s4() with the hbalock
5722  * held.
5723  *
5724  * Return: index into SLI4 fast-path FCP queue index.
5725  **/
5726 static uint32_t
5727 lpfc_sli4_scmd_to_wqidx_distr(struct lpfc_hba *phba)
5728 {
5729         ++phba->fcp_qidx;
5730         if (phba->fcp_qidx >= phba->cfg_fcp_wq_count)
5731                 phba->fcp_qidx = 0;
5732
5733         return phba->fcp_qidx;
5734 }
5735
5736 /**
5737  * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
5738  * @phba: Pointer to HBA context object.
5739  * @piocb: Pointer to command iocb.
5740  * @wqe: Pointer to the work queue entry.
5741  *
5742  * This routine converts the iocb command to its Work Queue Entry
5743  * equivalent. The wqe pointer should not have any fields set when
5744  * this routine is called because it will memcpy over them.
5745  * This routine does not set the CQ_ID or the WQEC bits in the
5746  * wqe.
5747  *
5748  * Returns: 0 = Success, IOCB_ERROR = Failure.
5749  **/
5750 static int
5751 lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
5752                 union lpfc_wqe *wqe)
5753 {
5754         uint32_t xmit_len = 0, total_len = 0;
5755         uint8_t ct = 0;
5756         uint32_t fip;
5757         uint32_t abort_tag;
5758         uint8_t command_type = ELS_COMMAND_NON_FIP;
5759         uint8_t cmnd;
5760         uint16_t xritag;
5761         struct ulp_bde64 *bpl = NULL;
5762         uint32_t els_id = ELS_ID_DEFAULT;
5763         int numBdes, i;
5764         struct ulp_bde64 bde;
5765
5766         fip = phba->hba_flag & HBA_FIP_SUPPORT;
5767         /* The fcp commands will set command type */
5768         if (iocbq->iocb_flag &  LPFC_IO_FCP)
5769                 command_type = FCP_COMMAND;
5770         else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
5771                 command_type = ELS_COMMAND_FIP;
5772         else
5773                 command_type = ELS_COMMAND_NON_FIP;
5774
5775         /* Some of the fields are in the right position already */
5776         memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
5777         abort_tag = (uint32_t) iocbq->iotag;
5778         xritag = iocbq->sli4_xritag;
5779         wqe->words[7] = 0; /* The ct field has moved so reset */
5780         /* words0-2 bpl convert bde */
5781         if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
5782                 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
5783                                 sizeof(struct ulp_bde64);
5784                 bpl  = (struct ulp_bde64 *)
5785                         ((struct lpfc_dmabuf *)iocbq->context3)->virt;
5786                 if (!bpl)
5787                         return IOCB_ERROR;
5788
5789                 /* Should already be byte swapped. */
5790                 wqe->generic.bde.addrHigh =  le32_to_cpu(bpl->addrHigh);
5791                 wqe->generic.bde.addrLow =  le32_to_cpu(bpl->addrLow);
5792                 /* swap the size field back to the cpu so we
5793                  * can assign it to the sgl.
5794                  */
5795                 wqe->generic.bde.tus.w  = le32_to_cpu(bpl->tus.w);
5796                 xmit_len = wqe->generic.bde.tus.f.bdeSize;
5797                 total_len = 0;
5798                 for (i = 0; i < numBdes; i++) {
5799                         bde.tus.w  = le32_to_cpu(bpl[i].tus.w);
5800                         total_len += bde.tus.f.bdeSize;
5801                 }
5802         } else
5803                 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
5804
5805         iocbq->iocb.ulpIoTag = iocbq->iotag;
5806         cmnd = iocbq->iocb.ulpCommand;
5807
5808         switch (iocbq->iocb.ulpCommand) {
5809         case CMD_ELS_REQUEST64_CR:
5810                 if (!iocbq->iocb.ulpLe) {
5811                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5812                                 "2007 Only Limited Edition cmd Format"
5813                                 " supported 0x%x\n",
5814                                 iocbq->iocb.ulpCommand);
5815                         return IOCB_ERROR;
5816                 }
5817                 wqe->els_req.payload_len = xmit_len;
5818                 /* Els_reguest64 has a TMO */
5819                 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
5820                         iocbq->iocb.ulpTimeout);
5821                 /* Need a VF for word 4 set the vf bit*/
5822                 bf_set(els_req64_vf, &wqe->els_req, 0);
5823                 /* And a VFID for word 12 */
5824                 bf_set(els_req64_vfid, &wqe->els_req, 0);
5825                 /*
5826                  * Set ct field to 3, indicates that the context_tag field
5827                  * contains the FCFI and remote N_Port_ID is
5828                  * in word 5.
5829                  */
5830
5831                 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
5832                 bf_set(lpfc_wqe_gen_context, &wqe->generic,
5833                                 iocbq->iocb.ulpContext);
5834
5835                 bf_set(lpfc_wqe_gen_ct, &wqe->generic, ct);
5836                 bf_set(lpfc_wqe_gen_pu, &wqe->generic, 0);
5837                 /* CCP CCPE PV PRI in word10 were set in the memcpy */
5838
5839                 if (command_type == ELS_COMMAND_FIP) {
5840                         els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
5841                                         >> LPFC_FIP_ELS_ID_SHIFT);
5842                 }
5843                 bf_set(lpfc_wqe_gen_els_id, &wqe->generic, els_id);
5844
5845         break;
5846         case CMD_XMIT_SEQUENCE64_CX:
5847                 bf_set(lpfc_wqe_gen_context, &wqe->generic,
5848                                         iocbq->iocb.un.ulpWord[3]);
5849                 wqe->generic.word3 = 0;
5850                 bf_set(wqe_rcvoxid, &wqe->generic, iocbq->iocb.ulpContext);
5851                 /* The entire sequence is transmitted for this IOCB */
5852                 xmit_len = total_len;
5853                 cmnd = CMD_XMIT_SEQUENCE64_CR;
5854         case CMD_XMIT_SEQUENCE64_CR:
5855                 /* word3 iocb=io_tag32 wqe=payload_offset */
5856                 /* payload offset used for multilpe outstanding
5857                  * sequences on the same exchange
5858                  */
5859                 wqe->words[3] = 0;
5860                 /* word4 relative_offset memcpy */
5861                 /* word5 r_ctl/df_ctl memcpy */
5862                 bf_set(lpfc_wqe_gen_pu, &wqe->generic, 0);
5863                 wqe->xmit_sequence.xmit_len = xmit_len;
5864                 command_type = OTHER_COMMAND;
5865         break;
5866         case CMD_XMIT_BCAST64_CN:
5867                 /* word3 iocb=iotag32 wqe=payload_len */
5868                 wqe->words[3] = 0; /* no definition for this in wqe */
5869                 /* word4 iocb=rsvd wqe=rsvd */
5870                 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
5871                 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
5872                 bf_set(lpfc_wqe_gen_ct, &wqe->generic,
5873                         ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
5874         break;
5875         case CMD_FCP_IWRITE64_CR:
5876                 command_type = FCP_COMMAND_DATA_OUT;
5877                 /* The struct for wqe fcp_iwrite has 3 fields that are somewhat
5878                  * confusing.
5879                  * word3 is payload_len: byte offset to the sgl entry for the
5880                  * fcp_command.
5881                  * word4 is total xfer len, same as the IOCB->ulpParameter.
5882                  * word5 is initial xfer len 0 = wait for xfer-ready
5883                  */
5884
5885                 /* Always wait for xfer-ready before sending data */
5886                 wqe->fcp_iwrite.initial_xfer_len = 0;
5887                 /* word 4 (xfer length) should have been set on the memcpy */
5888
5889         /* allow write to fall through to read */
5890         case CMD_FCP_IREAD64_CR:
5891                 /* FCP_CMD is always the 1st sgl entry */
5892                 wqe->fcp_iread.payload_len =
5893                         xmit_len + sizeof(struct fcp_rsp);
5894
5895                 /* word 4 (xfer length) should have been set on the memcpy */
5896
5897                 bf_set(lpfc_wqe_gen_erp, &wqe->generic,
5898                         iocbq->iocb.ulpFCP2Rcvy);
5899                 bf_set(lpfc_wqe_gen_lnk, &wqe->generic, iocbq->iocb.ulpXS);
5900                 /* The XC bit and the XS bit are similar. The driver never
5901                  * tracked whether or not the exchange was previouslly open.
5902                  * XC = Exchange create, 0 is create. 1 is already open.
5903                  * XS = link cmd: 1 do not close the exchange after command.
5904                  * XS = 0 close exchange when command completes.
5905                  * The only time we would not set the XC bit is when the XS bit
5906                  * is set and we are sending our 2nd or greater command on
5907                  * this exchange.
5908                  */
5909                 /* Always open the exchange */
5910                 bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
5911
5912                 wqe->words[10] &= 0xffff0000; /* zero out ebde count */
5913                 bf_set(lpfc_wqe_gen_pu, &wqe->generic, iocbq->iocb.ulpPU);
5914                 break;
5915         case CMD_FCP_ICMND64_CR:
5916                 /* Always open the exchange */
5917                 bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
5918
5919                 wqe->words[4] = 0;
5920                 wqe->words[10] &= 0xffff0000; /* zero out ebde count */
5921                 bf_set(lpfc_wqe_gen_pu, &wqe->generic, 0);
5922         break;
5923         case CMD_GEN_REQUEST64_CR:
5924                 /* word3 command length is described as byte offset to the
5925                  * rsp_data. Would always be 16, sizeof(struct sli4_sge)
5926                  * sgl[0] = cmnd
5927                  * sgl[1] = rsp.
5928                  *
5929                  */
5930                 wqe->gen_req.command_len = xmit_len;
5931                 /* Word4 parameter  copied in the memcpy */
5932                 /* Word5 [rctl, type, df_ctl, la] copied in memcpy */
5933                 /* word6 context tag copied in memcpy */
5934                 if (iocbq->iocb.ulpCt_h  || iocbq->iocb.ulpCt_l) {
5935                         ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
5936                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5937                                 "2015 Invalid CT %x command 0x%x\n",
5938                                 ct, iocbq->iocb.ulpCommand);
5939                         return IOCB_ERROR;
5940                 }
5941                 bf_set(lpfc_wqe_gen_ct, &wqe->generic, 0);
5942                 bf_set(wqe_tmo, &wqe->gen_req.wqe_com,
5943                         iocbq->iocb.ulpTimeout);
5944
5945                 bf_set(lpfc_wqe_gen_pu, &wqe->generic, iocbq->iocb.ulpPU);
5946                 command_type = OTHER_COMMAND;
5947         break;
5948         case CMD_XMIT_ELS_RSP64_CX:
5949                 /* words0-2 BDE memcpy */
5950                 /* word3 iocb=iotag32 wqe=rsvd */
5951                 wqe->words[3] = 0;
5952                 /* word4 iocb=did wge=rsvd. */
5953                 wqe->words[4] = 0;
5954                 /* word5 iocb=rsvd wge=did */
5955                 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
5956                          iocbq->iocb.un.elsreq64.remoteID);
5957
5958                 bf_set(lpfc_wqe_gen_ct, &wqe->generic,
5959                         ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
5960
5961                 bf_set(lpfc_wqe_gen_pu, &wqe->generic, iocbq->iocb.ulpPU);
5962                 bf_set(wqe_rcvoxid, &wqe->generic, iocbq->iocb.ulpContext);
5963                 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
5964                         bf_set(lpfc_wqe_gen_context, &wqe->generic,
5965                                iocbq->vport->vpi + phba->vpi_base);
5966                 command_type = OTHER_COMMAND;
5967         break;
5968         case CMD_CLOSE_XRI_CN:
5969         case CMD_ABORT_XRI_CN:
5970         case CMD_ABORT_XRI_CX:
5971                 /* words 0-2 memcpy should be 0 rserved */
5972                 /* port will send abts */
5973                 if (iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
5974                         /*
5975                          * The link is down so the fw does not need to send abts
5976                          * on the wire.
5977                          */
5978                         bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
5979                 else
5980                         bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
5981                 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
5982                 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
5983                 wqe->words[5] = 0;
5984                 bf_set(lpfc_wqe_gen_ct, &wqe->generic,
5985                         ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
5986                 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
5987                 wqe->generic.abort_tag = abort_tag;
5988                 /*
5989                  * The abort handler will send us CMD_ABORT_XRI_CN or
5990                  * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
5991                  */
5992                 bf_set(lpfc_wqe_gen_command, &wqe->generic, CMD_ABORT_XRI_CX);
5993                 cmnd = CMD_ABORT_XRI_CX;
5994                 command_type = OTHER_COMMAND;
5995                 xritag = 0;
5996         break;
5997         case CMD_XMIT_BLS_RSP64_CX:
5998                 /* As BLS ABTS-ACC WQE is very different from other WQEs,
5999                  * we re-construct this WQE here based on information in
6000                  * iocbq from scratch.
6001                  */
6002                 memset(wqe, 0, sizeof(union lpfc_wqe));
6003                 /* OX_ID is invariable to who sent ABTS to CT exchange */
6004                 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
6005                        bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_acc));
6006                 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_acc) ==
6007                     LPFC_ABTS_UNSOL_INT) {
6008                         /* ABTS sent by initiator to CT exchange, the
6009                          * RX_ID field will be filled with the newly
6010                          * allocated responder XRI.
6011                          */
6012                         bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
6013                                iocbq->sli4_xritag);
6014                 } else {
6015                         /* ABTS sent by responder to CT exchange, the
6016                          * RX_ID field will be filled with the responder
6017                          * RX_ID from ABTS.
6018                          */
6019                         bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
6020                                bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_acc));
6021                 }
6022                 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
6023                 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
6024                 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
6025                        iocbq->iocb.ulpContext);
6026                 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
6027                 command_type = OTHER_COMMAND;
6028         break;
6029         case CMD_XRI_ABORTED_CX:
6030         case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
6031                 /* words0-2 are all 0's no bde */
6032                 /* word3 and word4 are rsvrd */
6033                 wqe->words[3] = 0;
6034                 wqe->words[4] = 0;
6035                 /* word5 iocb=rsvd wge=did */
6036                 /* There is no remote port id in the IOCB? */
6037                 /* Let this fall through and fail */
6038         case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
6039         case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
6040         case CMD_FCP_TRSP64_CX: /* Target mode rcv */
6041         case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
6042         default:
6043                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6044                                 "2014 Invalid command 0x%x\n",
6045                                 iocbq->iocb.ulpCommand);
6046                 return IOCB_ERROR;
6047         break;
6048
6049         }
6050         bf_set(lpfc_wqe_gen_xri, &wqe->generic, xritag);
6051         bf_set(lpfc_wqe_gen_request_tag, &wqe->generic, iocbq->iotag);
6052         wqe->generic.abort_tag = abort_tag;
6053         bf_set(lpfc_wqe_gen_cmd_type, &wqe->generic, command_type);
6054         bf_set(lpfc_wqe_gen_command, &wqe->generic, cmnd);
6055         bf_set(lpfc_wqe_gen_class, &wqe->generic, iocbq->iocb.ulpClass);
6056         bf_set(lpfc_wqe_gen_cq_id, &wqe->generic, LPFC_WQE_CQ_ID_DEFAULT);
6057
6058         return 0;
6059 }
6060
6061 /**
6062  * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
6063  * @phba: Pointer to HBA context object.
6064  * @ring_number: SLI ring number to issue iocb on.
6065  * @piocb: Pointer to command iocb.
6066  * @flag: Flag indicating if this command can be put into txq.
6067  *
6068  * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
6069  * an iocb command to an HBA with SLI-4 interface spec.
6070  *
6071  * This function is called with hbalock held. The function will return success
6072  * after it successfully submit the iocb to firmware or after adding to the
6073  * txq.
6074  **/
6075 static int
6076 __lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
6077                          struct lpfc_iocbq *piocb, uint32_t flag)
6078 {
6079         struct lpfc_sglq *sglq;
6080         uint16_t xritag;
6081         union lpfc_wqe wqe;
6082         struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
6083
6084         if (piocb->sli4_xritag == NO_XRI) {
6085                 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
6086                     piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
6087                         sglq = NULL;
6088                 else {
6089                         sglq = __lpfc_sli_get_sglq(phba);
6090                         if (!sglq)
6091                                 return IOCB_ERROR;
6092                         piocb->sli4_xritag = sglq->sli4_xritag;
6093                 }
6094         } else if (piocb->iocb_flag &  LPFC_IO_FCP) {
6095                 sglq = NULL; /* These IO's already have an XRI and
6096                               * a mapped sgl.
6097                               */
6098         } else {
6099                 /* This is a continuation of a commandi,(CX) so this
6100                  * sglq is on the active list
6101                  */
6102                 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_xritag);
6103                 if (!sglq)
6104                         return IOCB_ERROR;
6105         }
6106
6107         if (sglq) {
6108                 xritag = lpfc_sli4_bpl2sgl(phba, piocb, sglq);
6109                 if (xritag != sglq->sli4_xritag)
6110                         return IOCB_ERROR;
6111         }
6112
6113         if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
6114                 return IOCB_ERROR;
6115
6116         if (piocb->iocb_flag &  LPFC_IO_FCP) {
6117                 /*
6118                  * For FCP command IOCB, get a new WQ index to distribute
6119                  * WQE across the WQsr. On the other hand, for abort IOCB,
6120                  * it carries the same WQ index to the original command
6121                  * IOCB.
6122                  */
6123                 if ((piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
6124                     (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN))
6125                         piocb->fcp_wqidx = lpfc_sli4_scmd_to_wqidx_distr(phba);
6126                 if (lpfc_sli4_wq_put(phba->sli4_hba.fcp_wq[piocb->fcp_wqidx],
6127                                      &wqe))
6128                         return IOCB_ERROR;
6129         } else {
6130                 if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
6131                         return IOCB_ERROR;
6132         }
6133         lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
6134
6135         return 0;
6136 }
6137
6138 /**
6139  * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
6140  *
6141  * This routine wraps the actual lockless version for issusing IOCB function
6142  * pointer from the lpfc_hba struct.
6143  *
6144  * Return codes:
6145  *      IOCB_ERROR - Error
6146  *      IOCB_SUCCESS - Success
6147  *      IOCB_BUSY - Busy
6148  **/
6149 static inline int
6150 __lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
6151                 struct lpfc_iocbq *piocb, uint32_t flag)
6152 {
6153         return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
6154 }
6155
6156 /**
6157  * lpfc_sli_api_table_setup - Set up sli api fucntion jump table
6158  * @phba: The hba struct for which this call is being executed.
6159  * @dev_grp: The HBA PCI-Device group number.
6160  *
6161  * This routine sets up the SLI interface API function jump table in @phba
6162  * struct.
6163  * Returns: 0 - success, -ENODEV - failure.
6164  **/
6165 int
6166 lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
6167 {
6168
6169         switch (dev_grp) {
6170         case LPFC_PCI_DEV_LP:
6171                 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
6172                 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
6173                 break;
6174         case LPFC_PCI_DEV_OC:
6175                 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
6176                 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
6177                 break;
6178         default:
6179                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6180                                 "1419 Invalid HBA PCI-device group: 0x%x\n",
6181                                 dev_grp);
6182                 return -ENODEV;
6183                 break;
6184         }
6185         phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
6186         return 0;
6187 }
6188
6189 /**
6190  * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
6191  * @phba: Pointer to HBA context object.
6192  * @pring: Pointer to driver SLI ring object.
6193  * @piocb: Pointer to command iocb.
6194  * @flag: Flag indicating if this command can be put into txq.
6195  *
6196  * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
6197  * function. This function gets the hbalock and calls
6198  * __lpfc_sli_issue_iocb function and will return the error returned
6199  * by __lpfc_sli_issue_iocb function. This wrapper is used by
6200  * functions which do not hold hbalock.
6201  **/
6202 int
6203 lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
6204                     struct lpfc_iocbq *piocb, uint32_t flag)
6205 {
6206         unsigned long iflags;
6207         int rc;
6208
6209         spin_lock_irqsave(&phba->hbalock, iflags);
6210         rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
6211         spin_unlock_irqrestore(&phba->hbalock, iflags);
6212
6213         return rc;
6214 }
6215
6216 /**
6217  * lpfc_extra_ring_setup - Extra ring setup function
6218  * @phba: Pointer to HBA context object.
6219  *
6220  * This function is called while driver attaches with the
6221  * HBA to setup the extra ring. The extra ring is used
6222  * only when driver needs to support target mode functionality
6223  * or IP over FC functionalities.
6224  *
6225  * This function is called with no lock held.
6226  **/
6227 static int
6228 lpfc_extra_ring_setup( struct lpfc_hba *phba)
6229 {
6230         struct lpfc_sli *psli;
6231         struct lpfc_sli_ring *pring;
6232
6233         psli = &phba->sli;
6234
6235         /* Adjust cmd/rsp ring iocb entries more evenly */
6236
6237         /* Take some away from the FCP ring */
6238         pring = &psli->ring[psli->fcp_ring];
6239         pring->numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
6240         pring->numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
6241         pring->numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
6242         pring->numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
6243
6244         /* and give them to the extra ring */
6245         pring = &psli->ring[psli->extra_ring];
6246
6247         pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
6248         pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
6249         pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
6250         pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
6251
6252         /* Setup default profile for this ring */
6253         pring->iotag_max = 4096;
6254         pring->num_mask = 1;
6255         pring->prt[0].profile = 0;      /* Mask 0 */
6256         pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
6257         pring->prt[0].type = phba->cfg_multi_ring_type;
6258         pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
6259         return 0;
6260 }
6261
6262 /**
6263  * lpfc_sli_async_event_handler - ASYNC iocb handler function
6264  * @phba: Pointer to HBA context object.
6265  * @pring: Pointer to driver SLI ring object.
6266  * @iocbq: Pointer to iocb object.
6267  *
6268  * This function is called by the slow ring event handler
6269  * function when there is an ASYNC event iocb in the ring.
6270  * This function is called with no lock held.
6271  * Currently this function handles only temperature related
6272  * ASYNC events. The function decodes the temperature sensor
6273  * event message and posts events for the management applications.
6274  **/
6275 static void
6276 lpfc_sli_async_event_handler(struct lpfc_hba * phba,
6277         struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
6278 {
6279         IOCB_t *icmd;
6280         uint16_t evt_code;
6281         uint16_t temp;
6282         struct temp_event temp_event_data;
6283         struct Scsi_Host *shost;
6284         uint32_t *iocb_w;
6285
6286         icmd = &iocbq->iocb;
6287         evt_code = icmd->un.asyncstat.evt_code;
6288         temp = icmd->ulpContext;
6289
6290         if ((evt_code != ASYNC_TEMP_WARN) &&
6291                 (evt_code != ASYNC_TEMP_SAFE)) {
6292                 iocb_w = (uint32_t *) icmd;
6293                 lpfc_printf_log(phba,
6294                         KERN_ERR,
6295                         LOG_SLI,
6296                         "0346 Ring %d handler: unexpected ASYNC_STATUS"
6297                         " evt_code 0x%x\n"
6298                         "W0  0x%08x W1  0x%08x W2  0x%08x W3  0x%08x\n"
6299                         "W4  0x%08x W5  0x%08x W6  0x%08x W7  0x%08x\n"
6300                         "W8  0x%08x W9  0x%08x W10 0x%08x W11 0x%08x\n"
6301                         "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
6302                         pring->ringno,
6303                         icmd->un.asyncstat.evt_code,
6304                         iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
6305                         iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
6306                         iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
6307                         iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
6308
6309                 return;
6310         }
6311         temp_event_data.data = (uint32_t)temp;
6312         temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
6313         if (evt_code == ASYNC_TEMP_WARN) {
6314                 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
6315                 lpfc_printf_log(phba,
6316                                 KERN_ERR,
6317                                 LOG_TEMP,
6318                                 "0347 Adapter is very hot, please take "
6319                                 "corrective action. temperature : %d Celsius\n",
6320                                 temp);
6321         }
6322         if (evt_code == ASYNC_TEMP_SAFE) {
6323                 temp_event_data.event_code = LPFC_NORMAL_TEMP;
6324                 lpfc_printf_log(phba,
6325                                 KERN_ERR,
6326                                 LOG_TEMP,
6327                                 "0340 Adapter temperature is OK now. "
6328                                 "temperature : %d Celsius\n",
6329                                 temp);
6330         }
6331
6332         /* Send temperature change event to applications */
6333         shost = lpfc_shost_from_vport(phba->pport);
6334         fc_host_post_vendor_event(shost, fc_get_event_number(),
6335                 sizeof(temp_event_data), (char *) &temp_event_data,
6336                 LPFC_NL_VENDOR_ID);
6337
6338 }
6339
6340
6341 /**
6342  * lpfc_sli_setup - SLI ring setup function
6343  * @phba: Pointer to HBA context object.
6344  *
6345  * lpfc_sli_setup sets up rings of the SLI interface with
6346  * number of iocbs per ring and iotags. This function is
6347  * called while driver attach to the HBA and before the
6348  * interrupts are enabled. So there is no need for locking.
6349  *
6350  * This function always returns 0.
6351  **/
6352 int
6353 lpfc_sli_setup(struct lpfc_hba *phba)
6354 {
6355         int i, totiocbsize = 0;
6356         struct lpfc_sli *psli = &phba->sli;
6357         struct lpfc_sli_ring *pring;
6358
6359         psli->num_rings = MAX_CONFIGURED_RINGS;
6360         psli->sli_flag = 0;
6361         psli->fcp_ring = LPFC_FCP_RING;
6362         psli->next_ring = LPFC_FCP_NEXT_RING;
6363         psli->extra_ring = LPFC_EXTRA_RING;
6364
6365         psli->iocbq_lookup = NULL;
6366         psli->iocbq_lookup_len = 0;
6367         psli->last_iotag = 0;
6368
6369         for (i = 0; i < psli->num_rings; i++) {
6370                 pring = &psli->ring[i];
6371                 switch (i) {
6372                 case LPFC_FCP_RING:     /* ring 0 - FCP */
6373                         /* numCiocb and numRiocb are used in config_port */
6374                         pring->numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
6375                         pring->numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
6376                         pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
6377                         pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
6378                         pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
6379                         pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
6380                         pring->sizeCiocb = (phba->sli_rev == 3) ?
6381                                                         SLI3_IOCB_CMD_SIZE :
6382                                                         SLI2_IOCB_CMD_SIZE;
6383                         pring->sizeRiocb = (phba->sli_rev == 3) ?
6384                                                         SLI3_IOCB_RSP_SIZE :
6385                                                         SLI2_IOCB_RSP_SIZE;
6386                         pring->iotag_ctr = 0;
6387                         pring->iotag_max =
6388                             (phba->cfg_hba_queue_depth * 2);
6389                         pring->fast_iotag = pring->iotag_max;
6390                         pring->num_mask = 0;
6391                         break;
6392                 case LPFC_EXTRA_RING:   /* ring 1 - EXTRA */
6393                         /* numCiocb and numRiocb are used in config_port */
6394                         pring->numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
6395                         pring->numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
6396                         pring->sizeCiocb = (phba->sli_rev == 3) ?
6397                                                         SLI3_IOCB_CMD_SIZE :
6398                                                         SLI2_IOCB_CMD_SIZE;
6399                         pring->sizeRiocb = (phba->sli_rev == 3) ?
6400                                                         SLI3_IOCB_RSP_SIZE :
6401                                                         SLI2_IOCB_RSP_SIZE;
6402                         pring->iotag_max = phba->cfg_hba_queue_depth;
6403                         pring->num_mask = 0;
6404                         break;
6405                 case LPFC_ELS_RING:     /* ring 2 - ELS / CT */
6406                         /* numCiocb and numRiocb are used in config_port */
6407                         pring->numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
6408                         pring->numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
6409                         pring->sizeCiocb = (phba->sli_rev == 3) ?
6410                                                         SLI3_IOCB_CMD_SIZE :
6411                                                         SLI2_IOCB_CMD_SIZE;
6412                         pring->sizeRiocb = (phba->sli_rev == 3) ?
6413                                                         SLI3_IOCB_RSP_SIZE :
6414                                                         SLI2_IOCB_RSP_SIZE;
6415                         pring->fast_iotag = 0;
6416                         pring->iotag_ctr = 0;
6417                         pring->iotag_max = 4096;
6418                         pring->lpfc_sli_rcv_async_status =
6419                                 lpfc_sli_async_event_handler;
6420                         pring->num_mask = LPFC_MAX_RING_MASK;
6421                         pring->prt[0].profile = 0;      /* Mask 0 */
6422                         pring->prt[0].rctl = FC_RCTL_ELS_REQ;
6423                         pring->prt[0].type = FC_TYPE_ELS;
6424                         pring->prt[0].lpfc_sli_rcv_unsol_event =
6425                             lpfc_els_unsol_event;
6426                         pring->prt[1].profile = 0;      /* Mask 1 */
6427                         pring->prt[1].rctl = FC_RCTL_ELS_REP;
6428                         pring->prt[1].type = FC_TYPE_ELS;
6429                         pring->prt[1].lpfc_sli_rcv_unsol_event =
6430                             lpfc_els_unsol_event;
6431                         pring->prt[2].profile = 0;      /* Mask 2 */
6432                         /* NameServer Inquiry */
6433                         pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
6434                         /* NameServer */
6435                         pring->prt[2].type = FC_TYPE_CT;
6436                         pring->prt[2].lpfc_sli_rcv_unsol_event =
6437                             lpfc_ct_unsol_event;
6438                         pring->prt[3].profile = 0;      /* Mask 3 */
6439                         /* NameServer response */
6440                         pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
6441                         /* NameServer */
6442                         pring->prt[3].type = FC_TYPE_CT;
6443                         pring->prt[3].lpfc_sli_rcv_unsol_event =
6444                             lpfc_ct_unsol_event;
6445                         /* abort unsolicited sequence */
6446                         pring->prt[4].profile = 0;      /* Mask 4 */
6447                         pring->prt[4].rctl = FC_RCTL_BA_ABTS;
6448                         pring->prt[4].type = FC_TYPE_BLS;
6449                         pring->prt[4].lpfc_sli_rcv_unsol_event =
6450                             lpfc_sli4_ct_abort_unsol_event;
6451                         break;
6452                 }
6453                 totiocbsize += (pring->numCiocb * pring->sizeCiocb) +
6454                                 (pring->numRiocb * pring->sizeRiocb);
6455         }
6456         if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
6457                 /* Too many cmd / rsp ring entries in SLI2 SLIM */
6458                 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
6459                        "SLI2 SLIM Data: x%x x%lx\n",
6460                        phba->brd_no, totiocbsize,
6461                        (unsigned long) MAX_SLIM_IOCB_SIZE);
6462         }
6463         if (phba->cfg_multi_ring_support == 2)
6464                 lpfc_extra_ring_setup(phba);
6465
6466         return 0;
6467 }
6468
6469 /**
6470  * lpfc_sli_queue_setup - Queue initialization function
6471  * @phba: Pointer to HBA context object.
6472  *
6473  * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
6474  * ring. This function also initializes ring indices of each ring.
6475  * This function is called during the initialization of the SLI
6476  * interface of an HBA.
6477  * This function is called with no lock held and always returns
6478  * 1.
6479  **/
6480 int
6481 lpfc_sli_queue_setup(struct lpfc_hba *phba)
6482 {
6483         struct lpfc_sli *psli;
6484         struct lpfc_sli_ring *pring;
6485         int i;
6486
6487         psli = &phba->sli;
6488         spin_lock_irq(&phba->hbalock);
6489         INIT_LIST_HEAD(&psli->mboxq);
6490         INIT_LIST_HEAD(&psli->mboxq_cmpl);
6491         /* Initialize list headers for txq and txcmplq as double linked lists */
6492         for (i = 0; i < psli->num_rings; i++) {
6493                 pring = &psli->ring[i];
6494                 pring->ringno = i;
6495                 pring->next_cmdidx  = 0;
6496                 pring->local_getidx = 0;
6497                 pring->cmdidx = 0;
6498                 INIT_LIST_HEAD(&pring->txq);
6499                 INIT_LIST_HEAD(&pring->txcmplq);
6500                 INIT_LIST_HEAD(&pring->iocb_continueq);
6501                 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
6502                 INIT_LIST_HEAD(&pring->postbufq);
6503         }
6504         spin_unlock_irq(&phba->hbalock);
6505         return 1;
6506 }
6507
6508 /**
6509  * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
6510  * @phba: Pointer to HBA context object.
6511  *
6512  * This routine flushes the mailbox command subsystem. It will unconditionally
6513  * flush all the mailbox commands in the three possible stages in the mailbox
6514  * command sub-system: pending mailbox command queue; the outstanding mailbox
6515  * command; and completed mailbox command queue. It is caller's responsibility
6516  * to make sure that the driver is in the proper state to flush the mailbox
6517  * command sub-system. Namely, the posting of mailbox commands into the
6518  * pending mailbox command queue from the various clients must be stopped;
6519  * either the HBA is in a state that it will never works on the outstanding
6520  * mailbox command (such as in EEH or ERATT conditions) or the outstanding
6521  * mailbox command has been completed.
6522  **/
6523 static void
6524 lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
6525 {
6526         LIST_HEAD(completions);
6527         struct lpfc_sli *psli = &phba->sli;
6528         LPFC_MBOXQ_t *pmb;
6529         unsigned long iflag;
6530
6531         /* Flush all the mailbox commands in the mbox system */
6532         spin_lock_irqsave(&phba->hbalock, iflag);
6533         /* The pending mailbox command queue */
6534         list_splice_init(&phba->sli.mboxq, &completions);
6535         /* The outstanding active mailbox command */
6536         if (psli->mbox_active) {
6537                 list_add_tail(&psli->mbox_active->list, &completions);
6538                 psli->mbox_active = NULL;
6539                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
6540         }
6541         /* The completed mailbox command queue */
6542         list_splice_init(&phba->sli.mboxq_cmpl, &completions);
6543         spin_unlock_irqrestore(&phba->hbalock, iflag);
6544
6545         /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
6546         while (!list_empty(&completions)) {
6547                 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
6548                 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
6549                 if (pmb->mbox_cmpl)
6550                         pmb->mbox_cmpl(phba, pmb);
6551         }
6552 }
6553
6554 /**
6555  * lpfc_sli_host_down - Vport cleanup function
6556  * @vport: Pointer to virtual port object.
6557  *
6558  * lpfc_sli_host_down is called to clean up the resources
6559  * associated with a vport before destroying virtual
6560  * port data structures.
6561  * This function does following operations:
6562  * - Free discovery resources associated with this virtual
6563  *   port.
6564  * - Free iocbs associated with this virtual port in
6565  *   the txq.
6566  * - Send abort for all iocb commands associated with this
6567  *   vport in txcmplq.
6568  *
6569  * This function is called with no lock held and always returns 1.
6570  **/
6571 int
6572 lpfc_sli_host_down(struct lpfc_vport *vport)
6573 {
6574         LIST_HEAD(completions);
6575         struct lpfc_hba *phba = vport->phba;
6576         struct lpfc_sli *psli = &phba->sli;
6577         struct lpfc_sli_ring *pring;
6578         struct lpfc_iocbq *iocb, *next_iocb;
6579         int i;
6580         unsigned long flags = 0;
6581         uint16_t prev_pring_flag;
6582
6583         lpfc_cleanup_discovery_resources(vport);
6584
6585         spin_lock_irqsave(&phba->hbalock, flags);
6586         for (i = 0; i < psli->num_rings; i++) {
6587                 pring = &psli->ring[i];
6588                 prev_pring_flag = pring->flag;
6589                 /* Only slow rings */
6590                 if (pring->ringno == LPFC_ELS_RING) {
6591                         pring->flag |= LPFC_DEFERRED_RING_EVENT;
6592                         /* Set the lpfc data pending flag */
6593                         set_bit(LPFC_DATA_READY, &phba->data_flags);
6594                 }
6595                 /*
6596                  * Error everything on the txq since these iocbs have not been
6597                  * given to the FW yet.
6598                  */
6599                 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
6600                         if (iocb->vport != vport)
6601                                 continue;
6602                         list_move_tail(&iocb->list, &completions);
6603                         pring->txq_cnt--;
6604                 }
6605
6606                 /* Next issue ABTS for everything on the txcmplq */
6607                 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
6608                                                                         list) {
6609                         if (iocb->vport != vport)
6610                                 continue;
6611                         lpfc_sli_issue_abort_iotag(phba, pring, iocb);
6612                 }
6613
6614                 pring->flag = prev_pring_flag;
6615         }
6616
6617         spin_unlock_irqrestore(&phba->hbalock, flags);
6618
6619         /* Cancel all the IOCBs from the completions list */
6620         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
6621                               IOERR_SLI_DOWN);
6622         return 1;
6623 }
6624
6625 /**
6626  * lpfc_sli_hba_down - Resource cleanup function for the HBA
6627  * @phba: Pointer to HBA context object.
6628  *
6629  * This function cleans up all iocb, buffers, mailbox commands
6630  * while shutting down the HBA. This function is called with no
6631  * lock held and always returns 1.
6632  * This function does the following to cleanup driver resources:
6633  * - Free discovery resources for each virtual port
6634  * - Cleanup any pending fabric iocbs
6635  * - Iterate through the iocb txq and free each entry
6636  *   in the list.
6637  * - Free up any buffer posted to the HBA
6638  * - Free mailbox commands in the mailbox queue.
6639  **/
6640 int
6641 lpfc_sli_hba_down(struct lpfc_hba *phba)
6642 {
6643         LIST_HEAD(completions);
6644         struct lpfc_sli *psli = &phba->sli;
6645         struct lpfc_sli_ring *pring;
6646         struct lpfc_dmabuf *buf_ptr;
6647         unsigned long flags = 0;
6648         int i;
6649
6650         /* Shutdown the mailbox command sub-system */
6651         lpfc_sli_mbox_sys_shutdown(phba);
6652
6653         lpfc_hba_down_prep(phba);
6654
6655         lpfc_fabric_abort_hba(phba);
6656
6657         spin_lock_irqsave(&phba->hbalock, flags);
6658         for (i = 0; i < psli->num_rings; i++) {
6659                 pring = &psli->ring[i];
6660                 /* Only slow rings */
6661                 if (pring->ringno == LPFC_ELS_RING) {
6662                         pring->flag |= LPFC_DEFERRED_RING_EVENT;
6663                         /* Set the lpfc data pending flag */
6664                         set_bit(LPFC_DATA_READY, &phba->data_flags);
6665                 }
6666
6667                 /*
6668                  * Error everything on the txq since these iocbs have not been
6669                  * given to the FW yet.
6670                  */
6671                 list_splice_init(&pring->txq, &completions);
6672                 pring->txq_cnt = 0;
6673
6674         }
6675         spin_unlock_irqrestore(&phba->hbalock, flags);
6676
6677         /* Cancel all the IOCBs from the completions list */
6678         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
6679                               IOERR_SLI_DOWN);
6680
6681         spin_lock_irqsave(&phba->hbalock, flags);
6682         list_splice_init(&phba->elsbuf, &completions);
6683         phba->elsbuf_cnt = 0;
6684         phba->elsbuf_prev_cnt = 0;
6685         spin_unlock_irqrestore(&phba->hbalock, flags);
6686
6687         while (!list_empty(&completions)) {
6688                 list_remove_head(&completions, buf_ptr,
6689                         struct lpfc_dmabuf, list);
6690                 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
6691                 kfree(buf_ptr);
6692         }
6693
6694         /* Return any active mbox cmds */
6695         del_timer_sync(&psli->mbox_tmo);
6696
6697         spin_lock_irqsave(&phba->pport->work_port_lock, flags);
6698         phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
6699         spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
6700
6701         return 1;
6702 }
6703
6704 /**
6705  * lpfc_sli4_hba_down - PCI function resource cleanup for the SLI4 HBA
6706  * @phba: Pointer to HBA context object.
6707  *
6708  * This function cleans up all queues, iocb, buffers, mailbox commands while
6709  * shutting down the SLI4 HBA FCoE function. This function is called with no
6710  * lock held and always returns 1.
6711  *
6712  * This function does the following to cleanup driver FCoE function resources:
6713  * - Free discovery resources for each virtual port
6714  * - Cleanup any pending fabric iocbs
6715  * - Iterate through the iocb txq and free each entry in the list.
6716  * - Free up any buffer posted to the HBA.
6717  * - Clean up all the queue entries: WQ, RQ, MQ, EQ, CQ, etc.
6718  * - Free mailbox commands in the mailbox queue.
6719  **/
6720 int
6721 lpfc_sli4_hba_down(struct lpfc_hba *phba)
6722 {
6723         /* Stop the SLI4 device port */
6724         lpfc_stop_port(phba);
6725
6726         /* Tear down the queues in the HBA */
6727         lpfc_sli4_queue_unset(phba);
6728
6729         /* unregister default FCFI from the HBA */
6730         lpfc_sli4_fcfi_unreg(phba, phba->fcf.fcfi);
6731
6732         return 1;
6733 }
6734
6735 /**
6736  * lpfc_sli_pcimem_bcopy - SLI memory copy function
6737  * @srcp: Source memory pointer.
6738  * @destp: Destination memory pointer.
6739  * @cnt: Number of words required to be copied.
6740  *
6741  * This function is used for copying data between driver memory
6742  * and the SLI memory. This function also changes the endianness
6743  * of each word if native endianness is different from SLI
6744  * endianness. This function can be called with or without
6745  * lock.
6746  **/
6747 void
6748 lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
6749 {
6750         uint32_t *src = srcp;
6751         uint32_t *dest = destp;
6752         uint32_t ldata;
6753         int i;
6754
6755         for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
6756                 ldata = *src;
6757                 ldata = le32_to_cpu(ldata);
6758                 *dest = ldata;
6759                 src++;
6760                 dest++;
6761         }
6762 }
6763
6764
6765 /**
6766  * lpfc_sli_bemem_bcopy - SLI memory copy function
6767  * @srcp: Source memory pointer.
6768  * @destp: Destination memory pointer.
6769  * @cnt: Number of words required to be copied.
6770  *
6771  * This function is used for copying data between a data structure
6772  * with big endian representation to local endianness.
6773  * This function can be called with or without lock.
6774  **/
6775 void
6776 lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
6777 {
6778         uint32_t *src = srcp;
6779         uint32_t *dest = destp;
6780         uint32_t ldata;
6781         int i;
6782
6783         for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
6784                 ldata = *src;
6785                 ldata = be32_to_cpu(ldata);
6786                 *dest = ldata;
6787                 src++;
6788                 dest++;
6789         }
6790 }
6791
6792 /**
6793  * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
6794  * @phba: Pointer to HBA context object.
6795  * @pring: Pointer to driver SLI ring object.
6796  * @mp: Pointer to driver buffer object.
6797  *
6798  * This function is called with no lock held.
6799  * It always return zero after adding the buffer to the postbufq
6800  * buffer list.
6801  **/
6802 int
6803 lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
6804                          struct lpfc_dmabuf *mp)
6805 {
6806         /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
6807            later */
6808         spin_lock_irq(&phba->hbalock);
6809         list_add_tail(&mp->list, &pring->postbufq);
6810         pring->postbufq_cnt++;
6811         spin_unlock_irq(&phba->hbalock);
6812         return 0;
6813 }
6814
6815 /**
6816  * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
6817  * @phba: Pointer to HBA context object.
6818  *
6819  * When HBQ is enabled, buffers are searched based on tags. This function
6820  * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
6821  * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
6822  * does not conflict with tags of buffer posted for unsolicited events.
6823  * The function returns the allocated tag. The function is called with
6824  * no locks held.
6825  **/
6826 uint32_t
6827 lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
6828 {
6829         spin_lock_irq(&phba->hbalock);
6830         phba->buffer_tag_count++;
6831         /*
6832          * Always set the QUE_BUFTAG_BIT to distiguish between
6833          * a tag assigned by HBQ.
6834          */
6835         phba->buffer_tag_count |= QUE_BUFTAG_BIT;
6836         spin_unlock_irq(&phba->hbalock);
6837         return phba->buffer_tag_count;
6838 }
6839
6840 /**
6841  * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
6842  * @phba: Pointer to HBA context object.
6843  * @pring: Pointer to driver SLI ring object.
6844  * @tag: Buffer tag.
6845  *
6846  * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
6847  * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
6848  * iocb is posted to the response ring with the tag of the buffer.
6849  * This function searches the pring->postbufq list using the tag
6850  * to find buffer associated with CMD_IOCB_RET_XRI64_CX
6851  * iocb. If the buffer is found then lpfc_dmabuf object of the
6852  * buffer is returned to the caller else NULL is returned.
6853  * This function is called with no lock held.
6854  **/
6855 struct lpfc_dmabuf *
6856 lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
6857                         uint32_t tag)
6858 {
6859         struct lpfc_dmabuf *mp, *next_mp;
6860         struct list_head *slp = &pring->postbufq;
6861
6862         /* Search postbufq, from the begining, looking for a match on tag */
6863         spin_lock_irq(&phba->hbalock);
6864         list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
6865                 if (mp->buffer_tag == tag) {
6866                         list_del_init(&mp->list);
6867                         pring->postbufq_cnt--;
6868                         spin_unlock_irq(&phba->hbalock);
6869                         return mp;
6870                 }
6871         }
6872
6873         spin_unlock_irq(&phba->hbalock);
6874         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6875                         "0402 Cannot find virtual addr for buffer tag on "
6876                         "ring %d Data x%lx x%p x%p x%x\n",
6877                         pring->ringno, (unsigned long) tag,
6878                         slp->next, slp->prev, pring->postbufq_cnt);
6879
6880         return NULL;
6881 }
6882
6883 /**
6884  * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
6885  * @phba: Pointer to HBA context object.
6886  * @pring: Pointer to driver SLI ring object.
6887  * @phys: DMA address of the buffer.
6888  *
6889  * This function searches the buffer list using the dma_address
6890  * of unsolicited event to find the driver's lpfc_dmabuf object
6891  * corresponding to the dma_address. The function returns the
6892  * lpfc_dmabuf object if a buffer is found else it returns NULL.
6893  * This function is called by the ct and els unsolicited event
6894  * handlers to get the buffer associated with the unsolicited
6895  * event.
6896  *
6897  * This function is called with no lock held.
6898  **/
6899 struct lpfc_dmabuf *
6900 lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
6901                          dma_addr_t phys)
6902 {
6903         struct lpfc_dmabuf *mp, *next_mp;
6904         struct list_head *slp = &pring->postbufq;
6905
6906         /* Search postbufq, from the begining, looking for a match on phys */
6907         spin_lock_irq(&phba->hbalock);
6908         list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
6909                 if (mp->phys == phys) {
6910                         list_del_init(&mp->list);
6911                         pring->postbufq_cnt--;
6912                         spin_unlock_irq(&phba->hbalock);
6913                         return mp;
6914                 }
6915         }
6916
6917         spin_unlock_irq(&phba->hbalock);
6918         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6919                         "0410 Cannot find virtual addr for mapped buf on "
6920                         "ring %d Data x%llx x%p x%p x%x\n",
6921                         pring->ringno, (unsigned long long)phys,
6922                         slp->next, slp->prev, pring->postbufq_cnt);
6923         return NULL;
6924 }
6925
6926 /**
6927  * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
6928  * @phba: Pointer to HBA context object.
6929  * @cmdiocb: Pointer to driver command iocb object.
6930  * @rspiocb: Pointer to driver response iocb object.
6931  *
6932  * This function is the completion handler for the abort iocbs for
6933  * ELS commands. This function is called from the ELS ring event
6934  * handler with no lock held. This function frees memory resources
6935  * associated with the abort iocb.
6936  **/
6937 static void
6938 lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
6939                         struct lpfc_iocbq *rspiocb)
6940 {
6941         IOCB_t *irsp = &rspiocb->iocb;
6942         uint16_t abort_iotag, abort_context;
6943         struct lpfc_iocbq *abort_iocb;
6944         struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
6945
6946         abort_iocb = NULL;
6947
6948         if (irsp->ulpStatus) {
6949                 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
6950                 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
6951
6952                 spin_lock_irq(&phba->hbalock);
6953                 if (phba->sli_rev < LPFC_SLI_REV4) {
6954                         if (abort_iotag != 0 &&
6955                                 abort_iotag <= phba->sli.last_iotag)
6956                                 abort_iocb =
6957                                         phba->sli.iocbq_lookup[abort_iotag];
6958                 } else
6959                         /* For sli4 the abort_tag is the XRI,
6960                          * so the abort routine puts the iotag  of the iocb
6961                          * being aborted in the context field of the abort
6962                          * IOCB.
6963                          */
6964                         abort_iocb = phba->sli.iocbq_lookup[abort_context];
6965
6966                 lpfc_printf_log(phba, KERN_INFO, LOG_ELS | LOG_SLI,
6967                                 "0327 Cannot abort els iocb %p "
6968                                 "with tag %x context %x, abort status %x, "
6969                                 "abort code %x\n",
6970                                 abort_iocb, abort_iotag, abort_context,
6971                                 irsp->ulpStatus, irsp->un.ulpWord[4]);
6972
6973                 /*
6974                  *  If the iocb is not found in Firmware queue the iocb
6975                  *  might have completed already. Do not free it again.
6976                  */
6977                 if (irsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
6978                         if (irsp->un.ulpWord[4] != IOERR_NO_XRI) {
6979                                 spin_unlock_irq(&phba->hbalock);
6980                                 lpfc_sli_release_iocbq(phba, cmdiocb);
6981                                 return;
6982                         }
6983                         /* For SLI4 the ulpContext field for abort IOCB
6984                          * holds the iotag of the IOCB being aborted so
6985                          * the local abort_context needs to be reset to
6986                          * match the aborted IOCBs ulpContext.
6987                          */
6988                         if (abort_iocb && phba->sli_rev == LPFC_SLI_REV4)
6989                                 abort_context = abort_iocb->iocb.ulpContext;
6990                 }
6991                 /*
6992                  * make sure we have the right iocbq before taking it
6993                  * off the txcmplq and try to call completion routine.
6994                  */
6995                 if (!abort_iocb ||
6996                     abort_iocb->iocb.ulpContext != abort_context ||
6997                     (abort_iocb->iocb_flag & LPFC_DRIVER_ABORTED) == 0)
6998                         spin_unlock_irq(&phba->hbalock);
6999                 else {
7000                         list_del_init(&abort_iocb->list);
7001                         pring->txcmplq_cnt--;
7002                         spin_unlock_irq(&phba->hbalock);
7003
7004                         /* Firmware could still be in progress of DMAing
7005                          * payload, so don't free data buffer till after
7006                          * a hbeat.
7007                          */
7008                         abort_iocb->iocb_flag |= LPFC_DELAY_MEM_FREE;
7009
7010                         abort_iocb->iocb_flag &= ~LPFC_DRIVER_ABORTED;
7011                         abort_iocb->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
7012                         abort_iocb->iocb.un.ulpWord[4] = IOERR_SLI_ABORTED;
7013                         (abort_iocb->iocb_cmpl)(phba, abort_iocb, abort_iocb);
7014                 }
7015         }
7016
7017         lpfc_sli_release_iocbq(phba, cmdiocb);
7018         return;
7019 }
7020
7021 /**
7022  * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
7023  * @phba: Pointer to HBA context object.
7024  * @cmdiocb: Pointer to driver command iocb object.
7025  * @rspiocb: Pointer to driver response iocb object.
7026  *
7027  * The function is called from SLI ring event handler with no
7028  * lock held. This function is the completion handler for ELS commands
7029  * which are aborted. The function frees memory resources used for
7030  * the aborted ELS commands.
7031  **/
7032 static void
7033 lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7034                      struct lpfc_iocbq *rspiocb)
7035 {
7036         IOCB_t *irsp = &rspiocb->iocb;
7037
7038         /* ELS cmd tag <ulpIoTag> completes */
7039         lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
7040                         "0139 Ignoring ELS cmd tag x%x completion Data: "
7041                         "x%x x%x x%x\n",
7042                         irsp->ulpIoTag, irsp->ulpStatus,
7043                         irsp->un.ulpWord[4], irsp->ulpTimeout);
7044         if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
7045                 lpfc_ct_free_iocb(phba, cmdiocb);
7046         else
7047                 lpfc_els_free_iocb(phba, cmdiocb);
7048         return;
7049 }
7050
7051 /**
7052  * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
7053  * @phba: Pointer to HBA context object.
7054  * @pring: Pointer to driver SLI ring object.
7055  * @cmdiocb: Pointer to driver command iocb object.
7056  *
7057  * This function issues an abort iocb for the provided command
7058  * iocb. This function is called with hbalock held.
7059  * The function returns 0 when it fails due to memory allocation
7060  * failure or when the command iocb is an abort request.
7061  **/
7062 int
7063 lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7064                            struct lpfc_iocbq *cmdiocb)
7065 {
7066         struct lpfc_vport *vport = cmdiocb->vport;
7067         struct lpfc_iocbq *abtsiocbp;
7068         IOCB_t *icmd = NULL;
7069         IOCB_t *iabt = NULL;
7070         int retval = IOCB_ERROR;
7071
7072         /*
7073          * There are certain command types we don't want to abort.  And we
7074          * don't want to abort commands that are already in the process of
7075          * being aborted.
7076          */
7077         icmd = &cmdiocb->iocb;
7078         if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
7079             icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
7080             (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
7081                 return 0;
7082
7083         /* If we're unloading, don't abort iocb on the ELS ring, but change the
7084          * callback so that nothing happens when it finishes.
7085          */
7086         if ((vport->load_flag & FC_UNLOADING) &&
7087             (pring->ringno == LPFC_ELS_RING)) {
7088                 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
7089                         cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
7090                 else
7091                         cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
7092                 goto abort_iotag_exit;
7093         }
7094
7095         /* issue ABTS for this IOCB based on iotag */
7096         abtsiocbp = __lpfc_sli_get_iocbq(phba);
7097         if (abtsiocbp == NULL)
7098                 return 0;
7099
7100         /* This signals the response to set the correct status
7101          * before calling the completion handler.
7102          */
7103         cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
7104
7105         iabt = &abtsiocbp->iocb;
7106         iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
7107         iabt->un.acxri.abortContextTag = icmd->ulpContext;
7108         if (phba->sli_rev == LPFC_SLI_REV4) {
7109                 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
7110                 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
7111         }
7112         else
7113                 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
7114         iabt->ulpLe = 1;
7115         iabt->ulpClass = icmd->ulpClass;
7116
7117         /* ABTS WQE must go to the same WQ as the WQE to be aborted */
7118         abtsiocbp->fcp_wqidx = cmdiocb->fcp_wqidx;
7119
7120         if (phba->link_state >= LPFC_LINK_UP)
7121                 iabt->ulpCommand = CMD_ABORT_XRI_CN;
7122         else
7123                 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
7124
7125         abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
7126
7127         lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
7128                          "0339 Abort xri x%x, original iotag x%x, "
7129                          "abort cmd iotag x%x\n",
7130                          iabt->un.acxri.abortContextTag,
7131                          iabt->un.acxri.abortIoTag, abtsiocbp->iotag);
7132         retval = __lpfc_sli_issue_iocb(phba, pring->ringno, abtsiocbp, 0);
7133
7134         if (retval)
7135                 __lpfc_sli_release_iocbq(phba, abtsiocbp);
7136 abort_iotag_exit:
7137         /*
7138          * Caller to this routine should check for IOCB_ERROR
7139          * and handle it properly.  This routine no longer removes
7140          * iocb off txcmplq and call compl in case of IOCB_ERROR.
7141          */
7142         return retval;
7143 }
7144
7145 /**
7146  * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
7147  * @iocbq: Pointer to driver iocb object.
7148  * @vport: Pointer to driver virtual port object.
7149  * @tgt_id: SCSI ID of the target.
7150  * @lun_id: LUN ID of the scsi device.
7151  * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
7152  *
7153  * This function acts as an iocb filter for functions which abort or count
7154  * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
7155  * 0 if the filtering criteria is met for the given iocb and will return
7156  * 1 if the filtering criteria is not met.
7157  * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
7158  * given iocb is for the SCSI device specified by vport, tgt_id and
7159  * lun_id parameter.
7160  * If ctx_cmd == LPFC_CTX_TGT,  the function returns 0 only if the
7161  * given iocb is for the SCSI target specified by vport and tgt_id
7162  * parameters.
7163  * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
7164  * given iocb is for the SCSI host associated with the given vport.
7165  * This function is called with no locks held.
7166  **/
7167 static int
7168 lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
7169                            uint16_t tgt_id, uint64_t lun_id,
7170                            lpfc_ctx_cmd ctx_cmd)
7171 {
7172         struct lpfc_scsi_buf *lpfc_cmd;
7173         int rc = 1;
7174
7175         if (!(iocbq->iocb_flag &  LPFC_IO_FCP))
7176                 return rc;
7177
7178         if (iocbq->vport != vport)
7179                 return rc;
7180
7181         lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
7182
7183         if (lpfc_cmd->pCmd == NULL)
7184                 return rc;
7185
7186         switch (ctx_cmd) {
7187         case LPFC_CTX_LUN:
7188                 if ((lpfc_cmd->rdata->pnode) &&
7189                     (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
7190                     (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
7191                         rc = 0;
7192                 break;
7193         case LPFC_CTX_TGT:
7194                 if ((lpfc_cmd->rdata->pnode) &&
7195                     (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
7196                         rc = 0;
7197                 break;
7198         case LPFC_CTX_HOST:
7199                 rc = 0;
7200                 break;
7201         default:
7202                 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
7203                         __func__, ctx_cmd);
7204                 break;
7205         }
7206
7207         return rc;
7208 }
7209
7210 /**
7211  * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
7212  * @vport: Pointer to virtual port.
7213  * @tgt_id: SCSI ID of the target.
7214  * @lun_id: LUN ID of the scsi device.
7215  * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
7216  *
7217  * This function returns number of FCP commands pending for the vport.
7218  * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
7219  * commands pending on the vport associated with SCSI device specified
7220  * by tgt_id and lun_id parameters.
7221  * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
7222  * commands pending on the vport associated with SCSI target specified
7223  * by tgt_id parameter.
7224  * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
7225  * commands pending on the vport.
7226  * This function returns the number of iocbs which satisfy the filter.
7227  * This function is called without any lock held.
7228  **/
7229 int
7230 lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
7231                   lpfc_ctx_cmd ctx_cmd)
7232 {
7233         struct lpfc_hba *phba = vport->phba;
7234         struct lpfc_iocbq *iocbq;
7235         int sum, i;
7236
7237         for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
7238                 iocbq = phba->sli.iocbq_lookup[i];
7239
7240                 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
7241                                                 ctx_cmd) == 0)
7242                         sum++;
7243         }
7244
7245         return sum;
7246 }
7247
7248 /**
7249  * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
7250  * @phba: Pointer to HBA context object
7251  * @cmdiocb: Pointer to command iocb object.
7252  * @rspiocb: Pointer to response iocb object.
7253  *
7254  * This function is called when an aborted FCP iocb completes. This
7255  * function is called by the ring event handler with no lock held.
7256  * This function frees the iocb.
7257  **/
7258 void
7259 lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7260                         struct lpfc_iocbq *rspiocb)
7261 {
7262         lpfc_sli_release_iocbq(phba, cmdiocb);
7263         return;
7264 }
7265
7266 /**
7267  * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
7268  * @vport: Pointer to virtual port.
7269  * @pring: Pointer to driver SLI ring object.
7270  * @tgt_id: SCSI ID of the target.
7271  * @lun_id: LUN ID of the scsi device.
7272  * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
7273  *
7274  * This function sends an abort command for every SCSI command
7275  * associated with the given virtual port pending on the ring
7276  * filtered by lpfc_sli_validate_fcp_iocb function.
7277  * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
7278  * FCP iocbs associated with lun specified by tgt_id and lun_id
7279  * parameters
7280  * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
7281  * FCP iocbs associated with SCSI target specified by tgt_id parameter.
7282  * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
7283  * FCP iocbs associated with virtual port.
7284  * This function returns number of iocbs it failed to abort.
7285  * This function is called with no locks held.
7286  **/
7287 int
7288 lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
7289                     uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
7290 {
7291         struct lpfc_hba *phba = vport->phba;
7292         struct lpfc_iocbq *iocbq;
7293         struct lpfc_iocbq *abtsiocb;
7294         IOCB_t *cmd = NULL;
7295         int errcnt = 0, ret_val = 0;
7296         int i;
7297
7298         for (i = 1; i <= phba->sli.last_iotag; i++) {
7299                 iocbq = phba->sli.iocbq_lookup[i];
7300
7301                 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
7302                                                abort_cmd) != 0)
7303                         continue;
7304
7305                 /* issue ABTS for this IOCB based on iotag */
7306                 abtsiocb = lpfc_sli_get_iocbq(phba);
7307                 if (abtsiocb == NULL) {
7308                         errcnt++;
7309                         continue;
7310                 }
7311
7312                 cmd = &iocbq->iocb;
7313                 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
7314                 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
7315                 if (phba->sli_rev == LPFC_SLI_REV4)
7316                         abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
7317                 else
7318                         abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
7319                 abtsiocb->iocb.ulpLe = 1;
7320                 abtsiocb->iocb.ulpClass = cmd->ulpClass;
7321                 abtsiocb->vport = phba->pport;
7322
7323                 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
7324                 abtsiocb->fcp_wqidx = iocbq->fcp_wqidx;
7325
7326                 if (lpfc_is_link_up(phba))
7327                         abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
7328                 else
7329                         abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
7330
7331                 /* Setup callback routine and issue the command. */
7332                 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
7333                 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
7334                                               abtsiocb, 0);
7335                 if (ret_val == IOCB_ERROR) {
7336                         lpfc_sli_release_iocbq(phba, abtsiocb);
7337                         errcnt++;
7338                         continue;
7339                 }
7340         }
7341
7342         return errcnt;
7343 }
7344
7345 /**
7346  * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
7347  * @phba: Pointer to HBA context object.
7348  * @cmdiocbq: Pointer to command iocb.
7349  * @rspiocbq: Pointer to response iocb.
7350  *
7351  * This function is the completion handler for iocbs issued using
7352  * lpfc_sli_issue_iocb_wait function. This function is called by the
7353  * ring event handler function without any lock held. This function
7354  * can be called from both worker thread context and interrupt
7355  * context. This function also can be called from other thread which
7356  * cleans up the SLI layer objects.
7357  * This function copy the contents of the response iocb to the
7358  * response iocb memory object provided by the caller of
7359  * lpfc_sli_issue_iocb_wait and then wakes up the thread which
7360  * sleeps for the iocb completion.
7361  **/
7362 static void
7363 lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
7364                         struct lpfc_iocbq *cmdiocbq,
7365                         struct lpfc_iocbq *rspiocbq)
7366 {
7367         wait_queue_head_t *pdone_q;
7368         unsigned long iflags;
7369
7370         spin_lock_irqsave(&phba->hbalock, iflags);
7371         cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
7372         if (cmdiocbq->context2 && rspiocbq)
7373                 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
7374                        &rspiocbq->iocb, sizeof(IOCB_t));
7375
7376         pdone_q = cmdiocbq->context_un.wait_queue;
7377         if (pdone_q)
7378                 wake_up(pdone_q);
7379         spin_unlock_irqrestore(&phba->hbalock, iflags);
7380         return;
7381 }
7382
7383 /**
7384  * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
7385  * @phba: Pointer to HBA context object..
7386  * @piocbq: Pointer to command iocb.
7387  * @flag: Flag to test.
7388  *
7389  * This routine grabs the hbalock and then test the iocb_flag to
7390  * see if the passed in flag is set.
7391  * Returns:
7392  * 1 if flag is set.
7393  * 0 if flag is not set.
7394  **/
7395 static int
7396 lpfc_chk_iocb_flg(struct lpfc_hba *phba,
7397                  struct lpfc_iocbq *piocbq, uint32_t flag)
7398 {
7399         unsigned long iflags;
7400         int ret;
7401
7402         spin_lock_irqsave(&phba->hbalock, iflags);
7403         ret = piocbq->iocb_flag & flag;
7404         spin_unlock_irqrestore(&phba->hbalock, iflags);
7405         return ret;
7406
7407 }
7408
7409 /**
7410  * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
7411  * @phba: Pointer to HBA context object..
7412  * @pring: Pointer to sli ring.
7413  * @piocb: Pointer to command iocb.
7414  * @prspiocbq: Pointer to response iocb.
7415  * @timeout: Timeout in number of seconds.
7416  *
7417  * This function issues the iocb to firmware and waits for the
7418  * iocb to complete. If the iocb command is not
7419  * completed within timeout seconds, it returns IOCB_TIMEDOUT.
7420  * Caller should not free the iocb resources if this function
7421  * returns IOCB_TIMEDOUT.
7422  * The function waits for the iocb completion using an
7423  * non-interruptible wait.
7424  * This function will sleep while waiting for iocb completion.
7425  * So, this function should not be called from any context which
7426  * does not allow sleeping. Due to the same reason, this function
7427  * cannot be called with interrupt disabled.
7428  * This function assumes that the iocb completions occur while
7429  * this function sleep. So, this function cannot be called from
7430  * the thread which process iocb completion for this ring.
7431  * This function clears the iocb_flag of the iocb object before
7432  * issuing the iocb and the iocb completion handler sets this
7433  * flag and wakes this thread when the iocb completes.
7434  * The contents of the response iocb will be copied to prspiocbq
7435  * by the completion handler when the command completes.
7436  * This function returns IOCB_SUCCESS when success.
7437  * This function is called with no lock held.
7438  **/
7439 int
7440 lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
7441                          uint32_t ring_number,
7442                          struct lpfc_iocbq *piocb,
7443                          struct lpfc_iocbq *prspiocbq,
7444                          uint32_t timeout)
7445 {
7446         DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
7447         long timeleft, timeout_req = 0;
7448         int retval = IOCB_SUCCESS;
7449         uint32_t creg_val;
7450
7451         /*
7452          * If the caller has provided a response iocbq buffer, then context2
7453          * is NULL or its an error.
7454          */
7455         if (prspiocbq) {
7456                 if (piocb->context2)
7457                         return IOCB_ERROR;
7458                 piocb->context2 = prspiocbq;
7459         }
7460
7461         piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
7462         piocb->context_un.wait_queue = &done_q;
7463         piocb->iocb_flag &= ~LPFC_IO_WAKE;
7464
7465         if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
7466                 creg_val = readl(phba->HCregaddr);
7467                 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
7468                 writel(creg_val, phba->HCregaddr);
7469                 readl(phba->HCregaddr); /* flush */
7470         }
7471
7472         retval = lpfc_sli_issue_iocb(phba, ring_number, piocb, 0);
7473         if (retval == IOCB_SUCCESS) {
7474                 timeout_req = timeout * HZ;
7475                 timeleft = wait_event_timeout(done_q,
7476                                 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
7477                                 timeout_req);
7478
7479                 if (piocb->iocb_flag & LPFC_IO_WAKE) {
7480                         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
7481                                         "0331 IOCB wake signaled\n");
7482                 } else if (timeleft == 0) {
7483                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7484                                         "0338 IOCB wait timeout error - no "
7485                                         "wake response Data x%x\n", timeout);
7486                         retval = IOCB_TIMEDOUT;
7487                 } else {
7488                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7489                                         "0330 IOCB wake NOT set, "
7490                                         "Data x%x x%lx\n",
7491                                         timeout, (timeleft / jiffies));
7492                         retval = IOCB_TIMEDOUT;
7493                 }
7494         } else {
7495                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
7496                                 "0332 IOCB wait issue failed, Data x%x\n",
7497                                 retval);
7498                 retval = IOCB_ERROR;
7499         }
7500
7501         if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
7502                 creg_val = readl(phba->HCregaddr);
7503                 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
7504                 writel(creg_val, phba->HCregaddr);
7505                 readl(phba->HCregaddr); /* flush */
7506         }
7507
7508         if (prspiocbq)
7509                 piocb->context2 = NULL;
7510
7511         piocb->context_un.wait_queue = NULL;
7512         piocb->iocb_cmpl = NULL;
7513         return retval;
7514 }
7515
7516 /**
7517  * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
7518  * @phba: Pointer to HBA context object.
7519  * @pmboxq: Pointer to driver mailbox object.
7520  * @timeout: Timeout in number of seconds.
7521  *
7522  * This function issues the mailbox to firmware and waits for the
7523  * mailbox command to complete. If the mailbox command is not
7524  * completed within timeout seconds, it returns MBX_TIMEOUT.
7525  * The function waits for the mailbox completion using an
7526  * interruptible wait. If the thread is woken up due to a
7527  * signal, MBX_TIMEOUT error is returned to the caller. Caller
7528  * should not free the mailbox resources, if this function returns
7529  * MBX_TIMEOUT.
7530  * This function will sleep while waiting for mailbox completion.
7531  * So, this function should not be called from any context which
7532  * does not allow sleeping. Due to the same reason, this function
7533  * cannot be called with interrupt disabled.
7534  * This function assumes that the mailbox completion occurs while
7535  * this function sleep. So, this function cannot be called from
7536  * the worker thread which processes mailbox completion.
7537  * This function is called in the context of HBA management
7538  * applications.
7539  * This function returns MBX_SUCCESS when successful.
7540  * This function is called with no lock held.
7541  **/
7542 int
7543 lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
7544                          uint32_t timeout)
7545 {
7546         DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
7547         int retval;
7548         unsigned long flag;
7549
7550         /* The caller must leave context1 empty. */
7551         if (pmboxq->context1)
7552                 return MBX_NOT_FINISHED;
7553
7554         pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
7555         /* setup wake call as IOCB callback */
7556         pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
7557         /* setup context field to pass wait_queue pointer to wake function  */
7558         pmboxq->context1 = &done_q;
7559
7560         /* now issue the command */
7561         retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
7562
7563         if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
7564                 wait_event_interruptible_timeout(done_q,
7565                                 pmboxq->mbox_flag & LPFC_MBX_WAKE,
7566                                 timeout * HZ);
7567
7568                 spin_lock_irqsave(&phba->hbalock, flag);
7569                 pmboxq->context1 = NULL;
7570                 /*
7571                  * if LPFC_MBX_WAKE flag is set the mailbox is completed
7572                  * else do not free the resources.
7573                  */
7574                 if (pmboxq->mbox_flag & LPFC_MBX_WAKE)
7575                         retval = MBX_SUCCESS;
7576                 else {
7577                         retval = MBX_TIMEOUT;
7578                         pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
7579                 }
7580                 spin_unlock_irqrestore(&phba->hbalock, flag);
7581         }
7582
7583         return retval;
7584 }
7585
7586 /**
7587  * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
7588  * @phba: Pointer to HBA context.
7589  *
7590  * This function is called to shutdown the driver's mailbox sub-system.
7591  * It first marks the mailbox sub-system is in a block state to prevent
7592  * the asynchronous mailbox command from issued off the pending mailbox
7593  * command queue. If the mailbox command sub-system shutdown is due to
7594  * HBA error conditions such as EEH or ERATT, this routine shall invoke
7595  * the mailbox sub-system flush routine to forcefully bring down the
7596  * mailbox sub-system. Otherwise, if it is due to normal condition (such
7597  * as with offline or HBA function reset), this routine will wait for the
7598  * outstanding mailbox command to complete before invoking the mailbox
7599  * sub-system flush routine to gracefully bring down mailbox sub-system.
7600  **/
7601 void
7602 lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba)
7603 {
7604         struct lpfc_sli *psli = &phba->sli;
7605         uint8_t actcmd = MBX_HEARTBEAT;
7606         unsigned long timeout;
7607
7608         spin_lock_irq(&phba->hbalock);
7609         psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
7610         spin_unlock_irq(&phba->hbalock);
7611
7612         if (psli->sli_flag & LPFC_SLI_ACTIVE) {
7613                 spin_lock_irq(&phba->hbalock);
7614                 if (phba->sli.mbox_active)
7615                         actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
7616                 spin_unlock_irq(&phba->hbalock);
7617                 /* Determine how long we might wait for the active mailbox
7618                  * command to be gracefully completed by firmware.
7619                  */
7620                 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, actcmd) *
7621                                            1000) + jiffies;
7622                 while (phba->sli.mbox_active) {
7623                         /* Check active mailbox complete status every 2ms */
7624                         msleep(2);
7625                         if (time_after(jiffies, timeout))
7626                                 /* Timeout, let the mailbox flush routine to
7627                                  * forcefully release active mailbox command
7628                                  */
7629                                 break;
7630                 }
7631         }
7632         lpfc_sli_mbox_sys_flush(phba);
7633 }
7634
7635 /**
7636  * lpfc_sli_eratt_read - read sli-3 error attention events
7637  * @phba: Pointer to HBA context.
7638  *
7639  * This function is called to read the SLI3 device error attention registers
7640  * for possible error attention events. The caller must hold the hostlock
7641  * with spin_lock_irq().
7642  *
7643  * This fucntion returns 1 when there is Error Attention in the Host Attention
7644  * Register and returns 0 otherwise.
7645  **/
7646 static int
7647 lpfc_sli_eratt_read(struct lpfc_hba *phba)
7648 {
7649         uint32_t ha_copy;
7650
7651         /* Read chip Host Attention (HA) register */
7652         ha_copy = readl(phba->HAregaddr);
7653         if (ha_copy & HA_ERATT) {
7654                 /* Read host status register to retrieve error event */
7655                 lpfc_sli_read_hs(phba);
7656
7657                 /* Check if there is a deferred error condition is active */
7658                 if ((HS_FFER1 & phba->work_hs) &&
7659                     ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
7660                      HS_FFER6 | HS_FFER7) & phba->work_hs)) {
7661                         phba->hba_flag |= DEFER_ERATT;
7662                         /* Clear all interrupt enable conditions */
7663                         writel(0, phba->HCregaddr);
7664                         readl(phba->HCregaddr);
7665                 }
7666
7667                 /* Set the driver HA work bitmap */
7668                 phba->work_ha |= HA_ERATT;
7669                 /* Indicate polling handles this ERATT */
7670                 phba->hba_flag |= HBA_ERATT_HANDLED;
7671                 return 1;
7672         }
7673         return 0;
7674 }
7675
7676 /**
7677  * lpfc_sli4_eratt_read - read sli-4 error attention events
7678  * @phba: Pointer to HBA context.
7679  *
7680  * This function is called to read the SLI4 device error attention registers
7681  * for possible error attention events. The caller must hold the hostlock
7682  * with spin_lock_irq().
7683  *
7684  * This fucntion returns 1 when there is Error Attention in the Host Attention
7685  * Register and returns 0 otherwise.
7686  **/
7687 static int
7688 lpfc_sli4_eratt_read(struct lpfc_hba *phba)
7689 {
7690         uint32_t uerr_sta_hi, uerr_sta_lo;
7691
7692         /* For now, use the SLI4 device internal unrecoverable error
7693          * registers for error attention. This can be changed later.
7694          */
7695         uerr_sta_lo = readl(phba->sli4_hba.UERRLOregaddr);
7696         uerr_sta_hi = readl(phba->sli4_hba.UERRHIregaddr);
7697         if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
7698             (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
7699                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7700                                 "1423 HBA Unrecoverable error: "
7701                                 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
7702                                 "ue_mask_lo_reg=0x%x, ue_mask_hi_reg=0x%x\n",
7703                                 uerr_sta_lo, uerr_sta_hi,
7704                                 phba->sli4_hba.ue_mask_lo,
7705                                 phba->sli4_hba.ue_mask_hi);
7706                 phba->work_status[0] = uerr_sta_lo;
7707                 phba->work_status[1] = uerr_sta_hi;
7708                 /* Set the driver HA work bitmap */
7709                 phba->work_ha |= HA_ERATT;
7710                 /* Indicate polling handles this ERATT */
7711                 phba->hba_flag |= HBA_ERATT_HANDLED;
7712                 return 1;
7713         }
7714         return 0;
7715 }
7716
7717 /**
7718  * lpfc_sli_check_eratt - check error attention events
7719  * @phba: Pointer to HBA context.
7720  *
7721  * This function is called from timer soft interrupt context to check HBA's
7722  * error attention register bit for error attention events.
7723  *
7724  * This fucntion returns 1 when there is Error Attention in the Host Attention
7725  * Register and returns 0 otherwise.
7726  **/
7727 int
7728 lpfc_sli_check_eratt(struct lpfc_hba *phba)
7729 {
7730         uint32_t ha_copy;
7731
7732         /* If somebody is waiting to handle an eratt, don't process it
7733          * here. The brdkill function will do this.
7734          */
7735         if (phba->link_flag & LS_IGNORE_ERATT)
7736                 return 0;
7737
7738         /* Check if interrupt handler handles this ERATT */
7739         spin_lock_irq(&phba->hbalock);
7740         if (phba->hba_flag & HBA_ERATT_HANDLED) {
7741                 /* Interrupt handler has handled ERATT */
7742                 spin_unlock_irq(&phba->hbalock);
7743                 return 0;
7744         }
7745
7746         /*
7747          * If there is deferred error attention, do not check for error
7748          * attention
7749          */
7750         if (unlikely(phba->hba_flag & DEFER_ERATT)) {
7751                 spin_unlock_irq(&phba->hbalock);
7752                 return 0;
7753         }
7754
7755         /* If PCI channel is offline, don't process it */
7756         if (unlikely(pci_channel_offline(phba->pcidev))) {
7757                 spin_unlock_irq(&phba->hbalock);
7758                 return 0;
7759         }
7760
7761         switch (phba->sli_rev) {
7762         case LPFC_SLI_REV2:
7763         case LPFC_SLI_REV3:
7764                 /* Read chip Host Attention (HA) register */
7765                 ha_copy = lpfc_sli_eratt_read(phba);
7766                 break;
7767         case LPFC_SLI_REV4:
7768                 /* Read devcie Uncoverable Error (UERR) registers */
7769                 ha_copy = lpfc_sli4_eratt_read(phba);
7770                 break;
7771         default:
7772                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7773                                 "0299 Invalid SLI revision (%d)\n",
7774                                 phba->sli_rev);
7775                 ha_copy = 0;
7776                 break;
7777         }
7778         spin_unlock_irq(&phba->hbalock);
7779
7780         return ha_copy;
7781 }
7782
7783 /**
7784  * lpfc_intr_state_check - Check device state for interrupt handling
7785  * @phba: Pointer to HBA context.
7786  *
7787  * This inline routine checks whether a device or its PCI slot is in a state
7788  * that the interrupt should be handled.
7789  *
7790  * This function returns 0 if the device or the PCI slot is in a state that
7791  * interrupt should be handled, otherwise -EIO.
7792  */
7793 static inline int
7794 lpfc_intr_state_check(struct lpfc_hba *phba)
7795 {
7796         /* If the pci channel is offline, ignore all the interrupts */
7797         if (unlikely(pci_channel_offline(phba->pcidev)))
7798                 return -EIO;
7799
7800         /* Update device level interrupt statistics */
7801         phba->sli.slistat.sli_intr++;
7802
7803         /* Ignore all interrupts during initialization. */
7804         if (unlikely(phba->link_state < LPFC_LINK_DOWN))
7805                 return -EIO;
7806
7807         return 0;
7808 }
7809
7810 /**
7811  * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
7812  * @irq: Interrupt number.
7813  * @dev_id: The device context pointer.
7814  *
7815  * This function is directly called from the PCI layer as an interrupt
7816  * service routine when device with SLI-3 interface spec is enabled with
7817  * MSI-X multi-message interrupt mode and there are slow-path events in
7818  * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
7819  * interrupt mode, this function is called as part of the device-level
7820  * interrupt handler. When the PCI slot is in error recovery or the HBA
7821  * is undergoing initialization, the interrupt handler will not process
7822  * the interrupt. The link attention and ELS ring attention events are
7823  * handled by the worker thread. The interrupt handler signals the worker
7824  * thread and returns for these events. This function is called without
7825  * any lock held. It gets the hbalock to access and update SLI data
7826  * structures.
7827  *
7828  * This function returns IRQ_HANDLED when interrupt is handled else it
7829  * returns IRQ_NONE.
7830  **/
7831 irqreturn_t
7832 lpfc_sli_sp_intr_handler(int irq, void *dev_id)
7833 {
7834         struct lpfc_hba  *phba;
7835         uint32_t ha_copy, hc_copy;
7836         uint32_t work_ha_copy;
7837         unsigned long status;
7838         unsigned long iflag;
7839         uint32_t control;
7840
7841         MAILBOX_t *mbox, *pmbox;
7842         struct lpfc_vport *vport;
7843         struct lpfc_nodelist *ndlp;
7844         struct lpfc_dmabuf *mp;
7845         LPFC_MBOXQ_t *pmb;
7846         int rc;
7847
7848         /*
7849          * Get the driver's phba structure from the dev_id and
7850          * assume the HBA is not interrupting.
7851          */
7852         phba = (struct lpfc_hba *)dev_id;
7853
7854         if (unlikely(!phba))
7855                 return IRQ_NONE;
7856
7857         /*
7858          * Stuff needs to be attented to when this function is invoked as an
7859          * individual interrupt handler in MSI-X multi-message interrupt mode
7860          */
7861         if (phba->intr_type == MSIX) {
7862                 /* Check device state for handling interrupt */
7863                 if (lpfc_intr_state_check(phba))
7864                         return IRQ_NONE;
7865                 /* Need to read HA REG for slow-path events */
7866                 spin_lock_irqsave(&phba->hbalock, iflag);
7867                 ha_copy = readl(phba->HAregaddr);
7868                 /* If somebody is waiting to handle an eratt don't process it
7869                  * here. The brdkill function will do this.
7870                  */
7871                 if (phba->link_flag & LS_IGNORE_ERATT)
7872                         ha_copy &= ~HA_ERATT;
7873                 /* Check the need for handling ERATT in interrupt handler */
7874                 if (ha_copy & HA_ERATT) {
7875                         if (phba->hba_flag & HBA_ERATT_HANDLED)
7876                                 /* ERATT polling has handled ERATT */
7877                                 ha_copy &= ~HA_ERATT;
7878                         else
7879                                 /* Indicate interrupt handler handles ERATT */
7880                                 phba->hba_flag |= HBA_ERATT_HANDLED;
7881                 }
7882
7883                 /*
7884                  * If there is deferred error attention, do not check for any
7885                  * interrupt.
7886                  */
7887                 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
7888                         spin_unlock_irqrestore(&phba->hbalock, iflag);
7889                         return IRQ_NONE;
7890                 }
7891
7892                 /* Clear up only attention source related to slow-path */
7893                 hc_copy = readl(phba->HCregaddr);
7894                 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
7895                         HC_LAINT_ENA | HC_ERINT_ENA),
7896                         phba->HCregaddr);
7897                 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
7898                         phba->HAregaddr);
7899                 writel(hc_copy, phba->HCregaddr);
7900                 readl(phba->HAregaddr); /* flush */
7901                 spin_unlock_irqrestore(&phba->hbalock, iflag);
7902         } else
7903                 ha_copy = phba->ha_copy;
7904
7905         work_ha_copy = ha_copy & phba->work_ha_mask;
7906
7907         if (work_ha_copy) {
7908                 if (work_ha_copy & HA_LATT) {
7909                         if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
7910                                 /*
7911                                  * Turn off Link Attention interrupts
7912                                  * until CLEAR_LA done
7913                                  */
7914                                 spin_lock_irqsave(&phba->hbalock, iflag);
7915                                 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
7916                                 control = readl(phba->HCregaddr);
7917                                 control &= ~HC_LAINT_ENA;
7918                                 writel(control, phba->HCregaddr);
7919                                 readl(phba->HCregaddr); /* flush */
7920                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
7921                         }
7922                         else
7923                                 work_ha_copy &= ~HA_LATT;
7924                 }
7925
7926                 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
7927                         /*
7928                          * Turn off Slow Rings interrupts, LPFC_ELS_RING is
7929                          * the only slow ring.
7930                          */
7931                         status = (work_ha_copy &
7932                                 (HA_RXMASK  << (4*LPFC_ELS_RING)));
7933                         status >>= (4*LPFC_ELS_RING);
7934                         if (status & HA_RXMASK) {
7935                                 spin_lock_irqsave(&phba->hbalock, iflag);
7936                                 control = readl(phba->HCregaddr);
7937
7938                                 lpfc_debugfs_slow_ring_trc(phba,
7939                                 "ISR slow ring:   ctl:x%x stat:x%x isrcnt:x%x",
7940                                 control, status,
7941                                 (uint32_t)phba->sli.slistat.sli_intr);
7942
7943                                 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
7944                                         lpfc_debugfs_slow_ring_trc(phba,
7945                                                 "ISR Disable ring:"
7946                                                 "pwork:x%x hawork:x%x wait:x%x",
7947                                                 phba->work_ha, work_ha_copy,
7948                                                 (uint32_t)((unsigned long)
7949                                                 &phba->work_waitq));
7950
7951                                         control &=
7952                                             ~(HC_R0INT_ENA << LPFC_ELS_RING);
7953                                         writel(control, phba->HCregaddr);
7954                                         readl(phba->HCregaddr); /* flush */
7955                                 }
7956                                 else {
7957                                         lpfc_debugfs_slow_ring_trc(phba,
7958                                                 "ISR slow ring:   pwork:"
7959                                                 "x%x hawork:x%x wait:x%x",
7960                                                 phba->work_ha, work_ha_copy,
7961                                                 (uint32_t)((unsigned long)
7962                                                 &phba->work_waitq));
7963                                 }
7964                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
7965                         }
7966                 }
7967                 spin_lock_irqsave(&phba->hbalock, iflag);
7968                 if (work_ha_copy & HA_ERATT) {
7969                         lpfc_sli_read_hs(phba);
7970                         /*
7971                          * Check if there is a deferred error condition
7972                          * is active
7973                          */
7974                         if ((HS_FFER1 & phba->work_hs) &&
7975                                 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
7976                                 HS_FFER6 | HS_FFER7) & phba->work_hs)) {
7977                                 phba->hba_flag |= DEFER_ERATT;
7978                                 /* Clear all interrupt enable conditions */
7979                                 writel(0, phba->HCregaddr);
7980                                 readl(phba->HCregaddr);
7981                         }
7982                 }
7983
7984                 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
7985                         pmb = phba->sli.mbox_active;
7986                         pmbox = &pmb->u.mb;
7987                         mbox = phba->mbox;
7988                         vport = pmb->vport;
7989
7990                         /* First check out the status word */
7991                         lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
7992                         if (pmbox->mbxOwner != OWN_HOST) {
7993                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
7994                                 /*
7995                                  * Stray Mailbox Interrupt, mbxCommand <cmd>
7996                                  * mbxStatus <status>
7997                                  */
7998                                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
7999                                                 LOG_SLI,
8000                                                 "(%d):0304 Stray Mailbox "
8001                                                 "Interrupt mbxCommand x%x "
8002                                                 "mbxStatus x%x\n",
8003                                                 (vport ? vport->vpi : 0),
8004                                                 pmbox->mbxCommand,
8005                                                 pmbox->mbxStatus);
8006                                 /* clear mailbox attention bit */
8007                                 work_ha_copy &= ~HA_MBATT;
8008                         } else {
8009                                 phba->sli.mbox_active = NULL;
8010                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
8011                                 phba->last_completion_time = jiffies;
8012                                 del_timer(&phba->sli.mbox_tmo);
8013                                 if (pmb->mbox_cmpl) {
8014                                         lpfc_sli_pcimem_bcopy(mbox, pmbox,
8015                                                         MAILBOX_CMD_SIZE);
8016                                 }
8017                                 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
8018                                         pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
8019
8020                                         lpfc_debugfs_disc_trc(vport,
8021                                                 LPFC_DISC_TRC_MBOX_VPORT,
8022                                                 "MBOX dflt rpi: : "
8023                                                 "status:x%x rpi:x%x",
8024                                                 (uint32_t)pmbox->mbxStatus,
8025                                                 pmbox->un.varWords[0], 0);
8026
8027                                         if (!pmbox->mbxStatus) {
8028                                                 mp = (struct lpfc_dmabuf *)
8029                                                         (pmb->context1);
8030                                                 ndlp = (struct lpfc_nodelist *)
8031                                                         pmb->context2;
8032
8033                                                 /* Reg_LOGIN of dflt RPI was
8034                                                  * successful. new lets get
8035                                                  * rid of the RPI using the
8036                                                  * same mbox buffer.
8037                                                  */
8038                                                 lpfc_unreg_login(phba,
8039                                                         vport->vpi,
8040                                                         pmbox->un.varWords[0],
8041                                                         pmb);
8042                                                 pmb->mbox_cmpl =
8043                                                         lpfc_mbx_cmpl_dflt_rpi;
8044                                                 pmb->context1 = mp;
8045                                                 pmb->context2 = ndlp;
8046                                                 pmb->vport = vport;
8047                                                 rc = lpfc_sli_issue_mbox(phba,
8048                                                                 pmb,
8049                                                                 MBX_NOWAIT);
8050                                                 if (rc != MBX_BUSY)
8051                                                         lpfc_printf_log(phba,
8052                                                         KERN_ERR,
8053                                                         LOG_MBOX | LOG_SLI,
8054                                                         "0350 rc should have"
8055                                                         "been MBX_BUSY\n");
8056                                                 if (rc != MBX_NOT_FINISHED)
8057                                                         goto send_current_mbox;
8058                                         }
8059                                 }
8060                                 spin_lock_irqsave(
8061                                                 &phba->pport->work_port_lock,
8062                                                 iflag);
8063                                 phba->pport->work_port_events &=
8064                                         ~WORKER_MBOX_TMO;
8065                                 spin_unlock_irqrestore(
8066                                                 &phba->pport->work_port_lock,
8067                                                 iflag);
8068                                 lpfc_mbox_cmpl_put(phba, pmb);
8069                         }
8070                 } else
8071                         spin_unlock_irqrestore(&phba->hbalock, iflag);
8072
8073                 if ((work_ha_copy & HA_MBATT) &&
8074                     (phba->sli.mbox_active == NULL)) {
8075 send_current_mbox:
8076                         /* Process next mailbox command if there is one */
8077                         do {
8078                                 rc = lpfc_sli_issue_mbox(phba, NULL,
8079                                                          MBX_NOWAIT);
8080                         } while (rc == MBX_NOT_FINISHED);
8081                         if (rc != MBX_SUCCESS)
8082                                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
8083                                                 LOG_SLI, "0349 rc should be "
8084                                                 "MBX_SUCCESS\n");
8085                 }
8086
8087                 spin_lock_irqsave(&phba->hbalock, iflag);
8088                 phba->work_ha |= work_ha_copy;
8089                 spin_unlock_irqrestore(&phba->hbalock, iflag);
8090                 lpfc_worker_wake_up(phba);
8091         }
8092         return IRQ_HANDLED;
8093
8094 } /* lpfc_sli_sp_intr_handler */
8095
8096 /**
8097  * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
8098  * @irq: Interrupt number.
8099  * @dev_id: The device context pointer.
8100  *
8101  * This function is directly called from the PCI layer as an interrupt
8102  * service routine when device with SLI-3 interface spec is enabled with
8103  * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
8104  * ring event in the HBA. However, when the device is enabled with either
8105  * MSI or Pin-IRQ interrupt mode, this function is called as part of the
8106  * device-level interrupt handler. When the PCI slot is in error recovery
8107  * or the HBA is undergoing initialization, the interrupt handler will not
8108  * process the interrupt. The SCSI FCP fast-path ring event are handled in
8109  * the intrrupt context. This function is called without any lock held.
8110  * It gets the hbalock to access and update SLI data structures.
8111  *
8112  * This function returns IRQ_HANDLED when interrupt is handled else it
8113  * returns IRQ_NONE.
8114  **/
8115 irqreturn_t
8116 lpfc_sli_fp_intr_handler(int irq, void *dev_id)
8117 {
8118         struct lpfc_hba  *phba;
8119         uint32_t ha_copy;
8120         unsigned long status;
8121         unsigned long iflag;
8122
8123         /* Get the driver's phba structure from the dev_id and
8124          * assume the HBA is not interrupting.
8125          */
8126         phba = (struct lpfc_hba *) dev_id;
8127
8128         if (unlikely(!phba))
8129                 return IRQ_NONE;
8130
8131         /*
8132          * Stuff needs to be attented to when this function is invoked as an
8133          * individual interrupt handler in MSI-X multi-message interrupt mode
8134          */
8135         if (phba->intr_type == MSIX) {
8136                 /* Check device state for handling interrupt */
8137                 if (lpfc_intr_state_check(phba))
8138                         return IRQ_NONE;
8139                 /* Need to read HA REG for FCP ring and other ring events */
8140                 ha_copy = readl(phba->HAregaddr);
8141                 /* Clear up only attention source related to fast-path */
8142                 spin_lock_irqsave(&phba->hbalock, iflag);
8143                 /*
8144                  * If there is deferred error attention, do not check for
8145                  * any interrupt.
8146                  */
8147                 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
8148                         spin_unlock_irqrestore(&phba->hbalock, iflag);
8149                         return IRQ_NONE;
8150                 }
8151                 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
8152                         phba->HAregaddr);
8153                 readl(phba->HAregaddr); /* flush */
8154                 spin_unlock_irqrestore(&phba->hbalock, iflag);
8155         } else
8156                 ha_copy = phba->ha_copy;
8157
8158         /*
8159          * Process all events on FCP ring. Take the optimized path for FCP IO.
8160          */
8161         ha_copy &= ~(phba->work_ha_mask);
8162
8163         status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
8164         status >>= (4*LPFC_FCP_RING);
8165         if (status & HA_RXMASK)
8166                 lpfc_sli_handle_fast_ring_event(phba,
8167                                                 &phba->sli.ring[LPFC_FCP_RING],
8168                                                 status);
8169
8170         if (phba->cfg_multi_ring_support == 2) {
8171                 /*
8172                  * Process all events on extra ring. Take the optimized path
8173                  * for extra ring IO.
8174                  */
8175                 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
8176                 status >>= (4*LPFC_EXTRA_RING);
8177                 if (status & HA_RXMASK) {
8178                         lpfc_sli_handle_fast_ring_event(phba,
8179                                         &phba->sli.ring[LPFC_EXTRA_RING],
8180                                         status);
8181                 }
8182         }
8183         return IRQ_HANDLED;
8184 }  /* lpfc_sli_fp_intr_handler */
8185
8186 /**
8187  * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
8188  * @irq: Interrupt number.
8189  * @dev_id: The device context pointer.
8190  *
8191  * This function is the HBA device-level interrupt handler to device with
8192  * SLI-3 interface spec, called from the PCI layer when either MSI or
8193  * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
8194  * requires driver attention. This function invokes the slow-path interrupt
8195  * attention handling function and fast-path interrupt attention handling
8196  * function in turn to process the relevant HBA attention events. This
8197  * function is called without any lock held. It gets the hbalock to access
8198  * and update SLI data structures.
8199  *
8200  * This function returns IRQ_HANDLED when interrupt is handled, else it
8201  * returns IRQ_NONE.
8202  **/
8203 irqreturn_t
8204 lpfc_sli_intr_handler(int irq, void *dev_id)
8205 {
8206         struct lpfc_hba  *phba;
8207         irqreturn_t sp_irq_rc, fp_irq_rc;
8208         unsigned long status1, status2;
8209         uint32_t hc_copy;
8210
8211         /*
8212          * Get the driver's phba structure from the dev_id and
8213          * assume the HBA is not interrupting.
8214          */
8215         phba = (struct lpfc_hba *) dev_id;
8216
8217         if (unlikely(!phba))
8218                 return IRQ_NONE;
8219
8220         /* Check device state for handling interrupt */
8221         if (lpfc_intr_state_check(phba))
8222                 return IRQ_NONE;
8223
8224         spin_lock(&phba->hbalock);
8225         phba->ha_copy = readl(phba->HAregaddr);
8226         if (unlikely(!phba->ha_copy)) {
8227                 spin_unlock(&phba->hbalock);
8228                 return IRQ_NONE;
8229         } else if (phba->ha_copy & HA_ERATT) {
8230                 if (phba->hba_flag & HBA_ERATT_HANDLED)
8231                         /* ERATT polling has handled ERATT */
8232                         phba->ha_copy &= ~HA_ERATT;
8233                 else
8234                         /* Indicate interrupt handler handles ERATT */
8235                         phba->hba_flag |= HBA_ERATT_HANDLED;
8236         }
8237
8238         /*
8239          * If there is deferred error attention, do not check for any interrupt.
8240          */
8241         if (unlikely(phba->hba_flag & DEFER_ERATT)) {
8242                 spin_unlock_irq(&phba->hbalock);
8243                 return IRQ_NONE;
8244         }
8245
8246         /* Clear attention sources except link and error attentions */
8247         hc_copy = readl(phba->HCregaddr);
8248         writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
8249                 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
8250                 phba->HCregaddr);
8251         writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
8252         writel(hc_copy, phba->HCregaddr);
8253         readl(phba->HAregaddr); /* flush */
8254         spin_unlock(&phba->hbalock);
8255
8256         /*
8257          * Invokes slow-path host attention interrupt handling as appropriate.
8258          */
8259
8260         /* status of events with mailbox and link attention */
8261         status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
8262
8263         /* status of events with ELS ring */
8264         status2 = (phba->ha_copy & (HA_RXMASK  << (4*LPFC_ELS_RING)));
8265         status2 >>= (4*LPFC_ELS_RING);
8266
8267         if (status1 || (status2 & HA_RXMASK))
8268                 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
8269         else
8270                 sp_irq_rc = IRQ_NONE;
8271
8272         /*
8273          * Invoke fast-path host attention interrupt handling as appropriate.
8274          */
8275
8276         /* status of events with FCP ring */
8277         status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
8278         status1 >>= (4*LPFC_FCP_RING);
8279
8280         /* status of events with extra ring */
8281         if (phba->cfg_multi_ring_support == 2) {
8282                 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
8283                 status2 >>= (4*LPFC_EXTRA_RING);
8284         } else
8285                 status2 = 0;
8286
8287         if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
8288                 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
8289         else
8290                 fp_irq_rc = IRQ_NONE;
8291
8292         /* Return device-level interrupt handling status */
8293         return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
8294 }  /* lpfc_sli_intr_handler */
8295
8296 /**
8297  * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
8298  * @phba: pointer to lpfc hba data structure.
8299  *
8300  * This routine is invoked by the worker thread to process all the pending
8301  * SLI4 FCP abort XRI events.
8302  **/
8303 void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
8304 {
8305         struct lpfc_cq_event *cq_event;
8306
8307         /* First, declare the fcp xri abort event has been handled */
8308         spin_lock_irq(&phba->hbalock);
8309         phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
8310         spin_unlock_irq(&phba->hbalock);
8311         /* Now, handle all the fcp xri abort events */
8312         while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
8313                 /* Get the first event from the head of the event queue */
8314                 spin_lock_irq(&phba->hbalock);
8315                 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
8316                                  cq_event, struct lpfc_cq_event, list);
8317                 spin_unlock_irq(&phba->hbalock);
8318                 /* Notify aborted XRI for FCP work queue */
8319                 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
8320                 /* Free the event processed back to the free pool */
8321                 lpfc_sli4_cq_event_release(phba, cq_event);
8322         }
8323 }
8324
8325 /**
8326  * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
8327  * @phba: pointer to lpfc hba data structure.
8328  *
8329  * This routine is invoked by the worker thread to process all the pending
8330  * SLI4 els abort xri events.
8331  **/
8332 void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
8333 {
8334         struct lpfc_cq_event *cq_event;
8335
8336         /* First, declare the els xri abort event has been handled */
8337         spin_lock_irq(&phba->hbalock);
8338         phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
8339         spin_unlock_irq(&phba->hbalock);
8340         /* Now, handle all the els xri abort events */
8341         while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
8342                 /* Get the first event from the head of the event queue */
8343                 spin_lock_irq(&phba->hbalock);
8344                 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
8345                                  cq_event, struct lpfc_cq_event, list);
8346                 spin_unlock_irq(&phba->hbalock);
8347                 /* Notify aborted XRI for ELS work queue */
8348                 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
8349                 /* Free the event processed back to the free pool */
8350                 lpfc_sli4_cq_event_release(phba, cq_event);
8351         }
8352 }
8353
8354 static void
8355 lpfc_sli4_iocb_param_transfer(struct lpfc_iocbq *pIocbIn,
8356                               struct lpfc_iocbq *pIocbOut,
8357                               struct lpfc_wcqe_complete *wcqe)
8358 {
8359         size_t offset = offsetof(struct lpfc_iocbq, iocb);
8360
8361         memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
8362                sizeof(struct lpfc_iocbq) - offset);
8363         /* Map WCQE parameters into irspiocb parameters */
8364         pIocbIn->iocb.ulpStatus = bf_get(lpfc_wcqe_c_status, wcqe);
8365         if (pIocbOut->iocb_flag & LPFC_IO_FCP)
8366                 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
8367                         pIocbIn->iocb.un.fcpi.fcpi_parm =
8368                                         pIocbOut->iocb.un.fcpi.fcpi_parm -
8369                                         wcqe->total_data_placed;
8370                 else
8371                         pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
8372         else
8373                 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
8374 }
8375
8376 /**
8377  * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
8378  * @phba: Pointer to HBA context object.
8379  * @wcqe: Pointer to work-queue completion queue entry.
8380  *
8381  * This routine handles an ELS work-queue completion event and construct
8382  * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
8383  * discovery engine to handle.
8384  *
8385  * Return: Pointer to the receive IOCBQ, NULL otherwise.
8386  **/
8387 static struct lpfc_iocbq *
8388 lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
8389                                struct lpfc_iocbq *irspiocbq)
8390 {
8391         struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
8392         struct lpfc_iocbq *cmdiocbq;
8393         struct lpfc_wcqe_complete *wcqe;
8394         unsigned long iflags;
8395
8396         wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
8397         spin_lock_irqsave(&phba->hbalock, iflags);
8398         pring->stats.iocb_event++;
8399         /* Look up the ELS command IOCB and create pseudo response IOCB */
8400         cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
8401                                 bf_get(lpfc_wcqe_c_request_tag, wcqe));
8402         spin_unlock_irqrestore(&phba->hbalock, iflags);
8403
8404         if (unlikely(!cmdiocbq)) {
8405                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8406                                 "0386 ELS complete with no corresponding "
8407                                 "cmdiocb: iotag (%d)\n",
8408                                 bf_get(lpfc_wcqe_c_request_tag, wcqe));
8409                 lpfc_sli_release_iocbq(phba, irspiocbq);
8410                 return NULL;
8411         }
8412
8413         /* Fake the irspiocbq and copy necessary response information */
8414         lpfc_sli4_iocb_param_transfer(irspiocbq, cmdiocbq, wcqe);
8415
8416         return irspiocbq;
8417 }
8418
8419 /**
8420  * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
8421  * @phba: Pointer to HBA context object.
8422  * @cqe: Pointer to mailbox completion queue entry.
8423  *
8424  * This routine process a mailbox completion queue entry with asynchrous
8425  * event.
8426  *
8427  * Return: true if work posted to worker thread, otherwise false.
8428  **/
8429 static bool
8430 lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
8431 {
8432         struct lpfc_cq_event *cq_event;
8433         unsigned long iflags;
8434
8435         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8436                         "0392 Async Event: word0:x%x, word1:x%x, "
8437                         "word2:x%x, word3:x%x\n", mcqe->word0,
8438                         mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
8439
8440         /* Allocate a new internal CQ_EVENT entry */
8441         cq_event = lpfc_sli4_cq_event_alloc(phba);
8442         if (!cq_event) {
8443                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8444                                 "0394 Failed to allocate CQ_EVENT entry\n");
8445                 return false;
8446         }
8447
8448         /* Move the CQE into an asynchronous event entry */
8449         memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
8450         spin_lock_irqsave(&phba->hbalock, iflags);
8451         list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
8452         /* Set the async event flag */
8453         phba->hba_flag |= ASYNC_EVENT;
8454         spin_unlock_irqrestore(&phba->hbalock, iflags);
8455
8456         return true;
8457 }
8458
8459 /**
8460  * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
8461  * @phba: Pointer to HBA context object.
8462  * @cqe: Pointer to mailbox completion queue entry.
8463  *
8464  * This routine process a mailbox completion queue entry with mailbox
8465  * completion event.
8466  *
8467  * Return: true if work posted to worker thread, otherwise false.
8468  **/
8469 static bool
8470 lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
8471 {
8472         uint32_t mcqe_status;
8473         MAILBOX_t *mbox, *pmbox;
8474         struct lpfc_mqe *mqe;
8475         struct lpfc_vport *vport;
8476         struct lpfc_nodelist *ndlp;
8477         struct lpfc_dmabuf *mp;
8478         unsigned long iflags;
8479         LPFC_MBOXQ_t *pmb;
8480         bool workposted = false;
8481         int rc;
8482
8483         /* If not a mailbox complete MCQE, out by checking mailbox consume */
8484         if (!bf_get(lpfc_trailer_completed, mcqe))
8485                 goto out_no_mqe_complete;
8486
8487         /* Get the reference to the active mbox command */
8488         spin_lock_irqsave(&phba->hbalock, iflags);
8489         pmb = phba->sli.mbox_active;
8490         if (unlikely(!pmb)) {
8491                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
8492                                 "1832 No pending MBOX command to handle\n");
8493                 spin_unlock_irqrestore(&phba->hbalock, iflags);
8494                 goto out_no_mqe_complete;
8495         }
8496         spin_unlock_irqrestore(&phba->hbalock, iflags);
8497         mqe = &pmb->u.mqe;
8498         pmbox = (MAILBOX_t *)&pmb->u.mqe;
8499         mbox = phba->mbox;
8500         vport = pmb->vport;
8501
8502         /* Reset heartbeat timer */
8503         phba->last_completion_time = jiffies;
8504         del_timer(&phba->sli.mbox_tmo);
8505
8506         /* Move mbox data to caller's mailbox region, do endian swapping */
8507         if (pmb->mbox_cmpl && mbox)
8508                 lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
8509         /* Set the mailbox status with SLI4 range 0x4000 */
8510         mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
8511         if (mcqe_status != MB_CQE_STATUS_SUCCESS)
8512                 bf_set(lpfc_mqe_status, mqe,
8513                        (LPFC_MBX_ERROR_RANGE | mcqe_status));
8514
8515         if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
8516                 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
8517                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
8518                                       "MBOX dflt rpi: status:x%x rpi:x%x",
8519                                       mcqe_status,
8520                                       pmbox->un.varWords[0], 0);
8521                 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
8522                         mp = (struct lpfc_dmabuf *)(pmb->context1);
8523                         ndlp = (struct lpfc_nodelist *)pmb->context2;
8524                         /* Reg_LOGIN of dflt RPI was successful. Now lets get
8525                          * RID of the PPI using the same mbox buffer.
8526                          */
8527                         lpfc_unreg_login(phba, vport->vpi,
8528                                          pmbox->un.varWords[0], pmb);
8529                         pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
8530                         pmb->context1 = mp;
8531                         pmb->context2 = ndlp;
8532                         pmb->vport = vport;
8533                         rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
8534                         if (rc != MBX_BUSY)
8535                                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
8536                                                 LOG_SLI, "0385 rc should "
8537                                                 "have been MBX_BUSY\n");
8538                         if (rc != MBX_NOT_FINISHED)
8539                                 goto send_current_mbox;
8540                 }
8541         }
8542         spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
8543         phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
8544         spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
8545
8546         /* There is mailbox completion work to do */
8547         spin_lock_irqsave(&phba->hbalock, iflags);
8548         __lpfc_mbox_cmpl_put(phba, pmb);
8549         phba->work_ha |= HA_MBATT;
8550         spin_unlock_irqrestore(&phba->hbalock, iflags);
8551         workposted = true;
8552
8553 send_current_mbox:
8554         spin_lock_irqsave(&phba->hbalock, iflags);
8555         /* Release the mailbox command posting token */
8556         phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8557         /* Setting active mailbox pointer need to be in sync to flag clear */
8558         phba->sli.mbox_active = NULL;
8559         spin_unlock_irqrestore(&phba->hbalock, iflags);
8560         /* Wake up worker thread to post the next pending mailbox command */
8561         lpfc_worker_wake_up(phba);
8562 out_no_mqe_complete:
8563         if (bf_get(lpfc_trailer_consumed, mcqe))
8564                 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
8565         return workposted;
8566 }
8567
8568 /**
8569  * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
8570  * @phba: Pointer to HBA context object.
8571  * @cqe: Pointer to mailbox completion queue entry.
8572  *
8573  * This routine process a mailbox completion queue entry, it invokes the
8574  * proper mailbox complete handling or asynchrous event handling routine
8575  * according to the MCQE's async bit.
8576  *
8577  * Return: true if work posted to worker thread, otherwise false.
8578  **/
8579 static bool
8580 lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
8581 {
8582         struct lpfc_mcqe mcqe;
8583         bool workposted;
8584
8585         /* Copy the mailbox MCQE and convert endian order as needed */
8586         lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
8587
8588         /* Invoke the proper event handling routine */
8589         if (!bf_get(lpfc_trailer_async, &mcqe))
8590                 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
8591         else
8592                 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
8593         return workposted;
8594 }
8595
8596 /**
8597  * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
8598  * @phba: Pointer to HBA context object.
8599  * @wcqe: Pointer to work-queue completion queue entry.
8600  *
8601  * This routine handles an ELS work-queue completion event.
8602  *
8603  * Return: true if work posted to worker thread, otherwise false.
8604  **/
8605 static bool
8606 lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba,
8607                              struct lpfc_wcqe_complete *wcqe)
8608 {
8609         struct lpfc_iocbq *irspiocbq;
8610         unsigned long iflags;
8611
8612         /* Get an irspiocbq for later ELS response processing use */
8613         irspiocbq = lpfc_sli_get_iocbq(phba);
8614         if (!irspiocbq) {
8615                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8616                                 "0387 Failed to allocate an iocbq\n");
8617                 return false;
8618         }
8619
8620         /* Save off the slow-path queue event for work thread to process */
8621         memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
8622         spin_lock_irqsave(&phba->hbalock, iflags);
8623         list_add_tail(&irspiocbq->cq_event.list,
8624                       &phba->sli4_hba.sp_queue_event);
8625         phba->hba_flag |= HBA_SP_QUEUE_EVT;
8626         spin_unlock_irqrestore(&phba->hbalock, iflags);
8627
8628         return true;
8629 }
8630
8631 /**
8632  * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
8633  * @phba: Pointer to HBA context object.
8634  * @wcqe: Pointer to work-queue completion queue entry.
8635  *
8636  * This routine handles slow-path WQ entry comsumed event by invoking the
8637  * proper WQ release routine to the slow-path WQ.
8638  **/
8639 static void
8640 lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
8641                              struct lpfc_wcqe_release *wcqe)
8642 {
8643         /* Check for the slow-path ELS work queue */
8644         if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
8645                 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
8646                                      bf_get(lpfc_wcqe_r_wqe_index, wcqe));
8647         else
8648                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8649                                 "2579 Slow-path wqe consume event carries "
8650                                 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
8651                                 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
8652                                 phba->sli4_hba.els_wq->queue_id);
8653 }
8654
8655 /**
8656  * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
8657  * @phba: Pointer to HBA context object.
8658  * @cq: Pointer to a WQ completion queue.
8659  * @wcqe: Pointer to work-queue completion queue entry.
8660  *
8661  * This routine handles an XRI abort event.
8662  *
8663  * Return: true if work posted to worker thread, otherwise false.
8664  **/
8665 static bool
8666 lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
8667                                    struct lpfc_queue *cq,
8668                                    struct sli4_wcqe_xri_aborted *wcqe)
8669 {
8670         bool workposted = false;
8671         struct lpfc_cq_event *cq_event;
8672         unsigned long iflags;
8673
8674         /* Allocate a new internal CQ_EVENT entry */
8675         cq_event = lpfc_sli4_cq_event_alloc(phba);
8676         if (!cq_event) {
8677                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8678                                 "0602 Failed to allocate CQ_EVENT entry\n");
8679                 return false;
8680         }
8681
8682         /* Move the CQE into the proper xri abort event list */
8683         memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
8684         switch (cq->subtype) {
8685         case LPFC_FCP:
8686                 spin_lock_irqsave(&phba->hbalock, iflags);
8687                 list_add_tail(&cq_event->list,
8688                               &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
8689                 /* Set the fcp xri abort event flag */
8690                 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
8691                 spin_unlock_irqrestore(&phba->hbalock, iflags);
8692                 workposted = true;
8693                 break;
8694         case LPFC_ELS:
8695                 spin_lock_irqsave(&phba->hbalock, iflags);
8696                 list_add_tail(&cq_event->list,
8697                               &phba->sli4_hba.sp_els_xri_aborted_work_queue);
8698                 /* Set the els xri abort event flag */
8699                 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
8700                 spin_unlock_irqrestore(&phba->hbalock, iflags);
8701                 workposted = true;
8702                 break;
8703         default:
8704                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8705                                 "0603 Invalid work queue CQE subtype (x%x)\n",
8706                                 cq->subtype);
8707                 workposted = false;
8708                 break;
8709         }
8710         return workposted;
8711 }
8712
8713 /**
8714  * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
8715  * @phba: Pointer to HBA context object.
8716  * @rcqe: Pointer to receive-queue completion queue entry.
8717  *
8718  * This routine process a receive-queue completion queue entry.
8719  *
8720  * Return: true if work posted to worker thread, otherwise false.
8721  **/
8722 static bool
8723 lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
8724 {
8725         bool workposted = false;
8726         struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
8727         struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
8728         struct hbq_dmabuf *dma_buf;
8729         uint32_t status;
8730         unsigned long iflags;
8731
8732         if (bf_get(lpfc_rcqe_rq_id, rcqe) != hrq->queue_id)
8733                 goto out;
8734
8735         status = bf_get(lpfc_rcqe_status, rcqe);
8736         switch (status) {
8737         case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
8738                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8739                                 "2537 Receive Frame Truncated!!\n");
8740         case FC_STATUS_RQ_SUCCESS:
8741                 lpfc_sli4_rq_release(hrq, drq);
8742                 spin_lock_irqsave(&phba->hbalock, iflags);
8743                 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
8744                 if (!dma_buf) {
8745                         spin_unlock_irqrestore(&phba->hbalock, iflags);
8746                         goto out;
8747                 }
8748                 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
8749                 /* save off the frame for the word thread to process */
8750                 list_add_tail(&dma_buf->cq_event.list,
8751                               &phba->sli4_hba.sp_queue_event);
8752                 /* Frame received */
8753                 phba->hba_flag |= HBA_SP_QUEUE_EVT;
8754                 spin_unlock_irqrestore(&phba->hbalock, iflags);
8755                 workposted = true;
8756                 break;
8757         case FC_STATUS_INSUFF_BUF_NEED_BUF:
8758         case FC_STATUS_INSUFF_BUF_FRM_DISC:
8759                 /* Post more buffers if possible */
8760                 spin_lock_irqsave(&phba->hbalock, iflags);
8761                 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
8762                 spin_unlock_irqrestore(&phba->hbalock, iflags);
8763                 workposted = true;
8764                 break;
8765         }
8766 out:
8767         return workposted;
8768 }
8769
8770 /**
8771  * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
8772  * @phba: Pointer to HBA context object.
8773  * @cq: Pointer to the completion queue.
8774  * @wcqe: Pointer to a completion queue entry.
8775  *
8776  * This routine process a slow-path work-queue or recieve queue completion queue
8777  * entry.
8778  *
8779  * Return: true if work posted to worker thread, otherwise false.
8780  **/
8781 static bool
8782 lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
8783                          struct lpfc_cqe *cqe)
8784 {
8785         struct lpfc_cqe cqevt;
8786         bool workposted = false;
8787
8788         /* Copy the work queue CQE and convert endian order if needed */
8789         lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
8790
8791         /* Check and process for different type of WCQE and dispatch */
8792         switch (bf_get(lpfc_cqe_code, &cqevt)) {
8793         case CQE_CODE_COMPL_WQE:
8794                 /* Process the WQ/RQ complete event */
8795                 workposted = lpfc_sli4_sp_handle_els_wcqe(phba,
8796                                 (struct lpfc_wcqe_complete *)&cqevt);
8797                 break;
8798         case CQE_CODE_RELEASE_WQE:
8799                 /* Process the WQ release event */
8800                 lpfc_sli4_sp_handle_rel_wcqe(phba,
8801                                 (struct lpfc_wcqe_release *)&cqevt);
8802                 break;
8803         case CQE_CODE_XRI_ABORTED:
8804                 /* Process the WQ XRI abort event */
8805                 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
8806                                 (struct sli4_wcqe_xri_aborted *)&cqevt);
8807                 break;
8808         case CQE_CODE_RECEIVE:
8809                 /* Process the RQ event */
8810                 workposted = lpfc_sli4_sp_handle_rcqe(phba,
8811                                 (struct lpfc_rcqe *)&cqevt);
8812                 break;
8813         default:
8814                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8815                                 "0388 Not a valid WCQE code: x%x\n",
8816                                 bf_get(lpfc_cqe_code, &cqevt));
8817                 break;
8818         }
8819         return workposted;
8820 }
8821
8822 /**
8823  * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
8824  * @phba: Pointer to HBA context object.
8825  * @eqe: Pointer to fast-path event queue entry.
8826  *
8827  * This routine process a event queue entry from the slow-path event queue.
8828  * It will check the MajorCode and MinorCode to determine this is for a
8829  * completion event on a completion queue, if not, an error shall be logged
8830  * and just return. Otherwise, it will get to the corresponding completion
8831  * queue and process all the entries on that completion queue, rearm the
8832  * completion queue, and then return.
8833  *
8834  **/
8835 static void
8836 lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
8837 {
8838         struct lpfc_queue *cq = NULL, *childq, *speq;
8839         struct lpfc_cqe *cqe;
8840         bool workposted = false;
8841         int ecount = 0;
8842         uint16_t cqid;
8843
8844         if (bf_get(lpfc_eqe_major_code, eqe) != 0 ||
8845             bf_get(lpfc_eqe_minor_code, eqe) != 0) {
8846                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8847                                 "0359 Not a valid slow-path completion "
8848                                 "event: majorcode=x%x, minorcode=x%x\n",
8849                                 bf_get(lpfc_eqe_major_code, eqe),
8850                                 bf_get(lpfc_eqe_minor_code, eqe));
8851                 return;
8852         }
8853
8854         /* Get the reference to the corresponding CQ */
8855         cqid = bf_get(lpfc_eqe_resource_id, eqe);
8856
8857         /* Search for completion queue pointer matching this cqid */
8858         speq = phba->sli4_hba.sp_eq;
8859         list_for_each_entry(childq, &speq->child_list, list) {
8860                 if (childq->queue_id == cqid) {
8861                         cq = childq;
8862                         break;
8863                 }
8864         }
8865         if (unlikely(!cq)) {
8866                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8867                                 "0365 Slow-path CQ identifier (%d) does "
8868                                 "not exist\n", cqid);
8869                 return;
8870         }
8871
8872         /* Process all the entries to the CQ */
8873         switch (cq->type) {
8874         case LPFC_MCQ:
8875                 while ((cqe = lpfc_sli4_cq_get(cq))) {
8876                         workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
8877                         if (!(++ecount % LPFC_GET_QE_REL_INT))
8878                                 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
8879                 }
8880                 break;
8881         case LPFC_WCQ:
8882                 while ((cqe = lpfc_sli4_cq_get(cq))) {
8883                         workposted |= lpfc_sli4_sp_handle_cqe(phba, cq, cqe);
8884                         if (!(++ecount % LPFC_GET_QE_REL_INT))
8885                                 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
8886                 }
8887                 break;
8888         default:
8889                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8890                                 "0370 Invalid completion queue type (%d)\n",
8891                                 cq->type);
8892                 return;
8893         }
8894
8895         /* Catch the no cq entry condition, log an error */
8896         if (unlikely(ecount == 0))
8897                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8898                                 "0371 No entry from the CQ: identifier "
8899                                 "(x%x), type (%d)\n", cq->queue_id, cq->type);
8900
8901         /* In any case, flash and re-arm the RCQ */
8902         lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
8903
8904         /* wake up worker thread if there are works to be done */
8905         if (workposted)
8906                 lpfc_worker_wake_up(phba);
8907 }
8908
8909 /**
8910  * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
8911  * @eqe: Pointer to fast-path completion queue entry.
8912  *
8913  * This routine process a fast-path work queue completion entry from fast-path
8914  * event queue for FCP command response completion.
8915  **/
8916 static void
8917 lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba,
8918                              struct lpfc_wcqe_complete *wcqe)
8919 {
8920         struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
8921         struct lpfc_iocbq *cmdiocbq;
8922         struct lpfc_iocbq irspiocbq;
8923         unsigned long iflags;
8924
8925         spin_lock_irqsave(&phba->hbalock, iflags);
8926         pring->stats.iocb_event++;
8927         spin_unlock_irqrestore(&phba->hbalock, iflags);
8928
8929         /* Check for response status */
8930         if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
8931                 /* If resource errors reported from HBA, reduce queue
8932                  * depth of the SCSI device.
8933                  */
8934                 if ((bf_get(lpfc_wcqe_c_status, wcqe) ==
8935                      IOSTAT_LOCAL_REJECT) &&
8936                     (wcqe->parameter == IOERR_NO_RESOURCES)) {
8937                         phba->lpfc_rampdown_queue_depth(phba);
8938                 }
8939                 /* Log the error status */
8940                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8941                                 "0373 FCP complete error: status=x%x, "
8942                                 "hw_status=x%x, total_data_specified=%d, "
8943                                 "parameter=x%x, word3=x%x\n",
8944                                 bf_get(lpfc_wcqe_c_status, wcqe),
8945                                 bf_get(lpfc_wcqe_c_hw_status, wcqe),
8946                                 wcqe->total_data_placed, wcqe->parameter,
8947                                 wcqe->word3);
8948         }
8949
8950         /* Look up the FCP command IOCB and create pseudo response IOCB */
8951         spin_lock_irqsave(&phba->hbalock, iflags);
8952         cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
8953                                 bf_get(lpfc_wcqe_c_request_tag, wcqe));
8954         spin_unlock_irqrestore(&phba->hbalock, iflags);
8955         if (unlikely(!cmdiocbq)) {
8956                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8957                                 "0374 FCP complete with no corresponding "
8958                                 "cmdiocb: iotag (%d)\n",
8959                                 bf_get(lpfc_wcqe_c_request_tag, wcqe));
8960                 return;
8961         }
8962         if (unlikely(!cmdiocbq->iocb_cmpl)) {
8963                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8964                                 "0375 FCP cmdiocb not callback function "
8965                                 "iotag: (%d)\n",
8966                                 bf_get(lpfc_wcqe_c_request_tag, wcqe));
8967                 return;
8968         }
8969
8970         /* Fake the irspiocb and copy necessary response information */
8971         lpfc_sli4_iocb_param_transfer(&irspiocbq, cmdiocbq, wcqe);
8972
8973         /* Pass the cmd_iocb and the rsp state to the upper layer */
8974         (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
8975 }
8976
8977 /**
8978  * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
8979  * @phba: Pointer to HBA context object.
8980  * @cq: Pointer to completion queue.
8981  * @wcqe: Pointer to work-queue completion queue entry.
8982  *
8983  * This routine handles an fast-path WQ entry comsumed event by invoking the
8984  * proper WQ release routine to the slow-path WQ.
8985  **/
8986 static void
8987 lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
8988                              struct lpfc_wcqe_release *wcqe)
8989 {
8990         struct lpfc_queue *childwq;
8991         bool wqid_matched = false;
8992         uint16_t fcp_wqid;
8993
8994         /* Check for fast-path FCP work queue release */
8995         fcp_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
8996         list_for_each_entry(childwq, &cq->child_list, list) {
8997                 if (childwq->queue_id == fcp_wqid) {
8998                         lpfc_sli4_wq_release(childwq,
8999                                         bf_get(lpfc_wcqe_r_wqe_index, wcqe));
9000                         wqid_matched = true;
9001                         break;
9002                 }
9003         }
9004         /* Report warning log message if no match found */
9005         if (wqid_matched != true)
9006                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9007                                 "2580 Fast-path wqe consume event carries "
9008                                 "miss-matched qid: wcqe-qid=x%x\n", fcp_wqid);
9009 }
9010
9011 /**
9012  * lpfc_sli4_fp_handle_wcqe - Process fast-path work queue completion entry
9013  * @cq: Pointer to the completion queue.
9014  * @eqe: Pointer to fast-path completion queue entry.
9015  *
9016  * This routine process a fast-path work queue completion entry from fast-path
9017  * event queue for FCP command response completion.
9018  **/
9019 static int
9020 lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
9021                          struct lpfc_cqe *cqe)
9022 {
9023         struct lpfc_wcqe_release wcqe;
9024         bool workposted = false;
9025
9026         /* Copy the work queue CQE and convert endian order if needed */
9027         lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
9028
9029         /* Check and process for different type of WCQE and dispatch */
9030         switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
9031         case CQE_CODE_COMPL_WQE:
9032                 /* Process the WQ complete event */
9033                 lpfc_sli4_fp_handle_fcp_wcqe(phba,
9034                                 (struct lpfc_wcqe_complete *)&wcqe);
9035                 break;
9036         case CQE_CODE_RELEASE_WQE:
9037                 /* Process the WQ release event */
9038                 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
9039                                 (struct lpfc_wcqe_release *)&wcqe);
9040                 break;
9041         case CQE_CODE_XRI_ABORTED:
9042                 /* Process the WQ XRI abort event */
9043                 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
9044                                 (struct sli4_wcqe_xri_aborted *)&wcqe);
9045                 break;
9046         default:
9047                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9048                                 "0144 Not a valid WCQE code: x%x\n",
9049                                 bf_get(lpfc_wcqe_c_code, &wcqe));
9050                 break;
9051         }
9052         return workposted;
9053 }
9054
9055 /**
9056  * lpfc_sli4_fp_handle_eqe - Process a fast-path event queue entry
9057  * @phba: Pointer to HBA context object.
9058  * @eqe: Pointer to fast-path event queue entry.
9059  *
9060  * This routine process a event queue entry from the fast-path event queue.
9061  * It will check the MajorCode and MinorCode to determine this is for a
9062  * completion event on a completion queue, if not, an error shall be logged
9063  * and just return. Otherwise, it will get to the corresponding completion
9064  * queue and process all the entries on the completion queue, rearm the
9065  * completion queue, and then return.
9066  **/
9067 static void
9068 lpfc_sli4_fp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
9069                         uint32_t fcp_cqidx)
9070 {
9071         struct lpfc_queue *cq;
9072         struct lpfc_cqe *cqe;
9073         bool workposted = false;
9074         uint16_t cqid;
9075         int ecount = 0;
9076
9077         if (unlikely(bf_get(lpfc_eqe_major_code, eqe) != 0) ||
9078             unlikely(bf_get(lpfc_eqe_minor_code, eqe) != 0)) {
9079                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9080                                 "0366 Not a valid fast-path completion "
9081                                 "event: majorcode=x%x, minorcode=x%x\n",
9082                                 bf_get(lpfc_eqe_major_code, eqe),
9083                                 bf_get(lpfc_eqe_minor_code, eqe));
9084                 return;
9085         }
9086
9087         cq = phba->sli4_hba.fcp_cq[fcp_cqidx];
9088         if (unlikely(!cq)) {
9089                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9090                                 "0367 Fast-path completion queue does not "
9091                                 "exist\n");
9092                 return;
9093         }
9094
9095         /* Get the reference to the corresponding CQ */
9096         cqid = bf_get(lpfc_eqe_resource_id, eqe);
9097         if (unlikely(cqid != cq->queue_id)) {
9098                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9099                                 "0368 Miss-matched fast-path completion "
9100                                 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
9101                                 cqid, cq->queue_id);
9102                 return;
9103         }
9104
9105         /* Process all the entries to the CQ */
9106         while ((cqe = lpfc_sli4_cq_get(cq))) {
9107                 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
9108                 if (!(++ecount % LPFC_GET_QE_REL_INT))
9109                         lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
9110         }
9111
9112         /* Catch the no cq entry condition */
9113         if (unlikely(ecount == 0))
9114                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9115                                 "0369 No entry from fast-path completion "
9116                                 "queue fcpcqid=%d\n", cq->queue_id);
9117
9118         /* In any case, flash and re-arm the CQ */
9119         lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
9120
9121         /* wake up worker thread if there are works to be done */
9122         if (workposted)
9123                 lpfc_worker_wake_up(phba);
9124 }
9125
9126 static void
9127 lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
9128 {
9129         struct lpfc_eqe *eqe;
9130
9131         /* walk all the EQ entries and drop on the floor */
9132         while ((eqe = lpfc_sli4_eq_get(eq)))
9133                 ;
9134
9135         /* Clear and re-arm the EQ */
9136         lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
9137 }
9138
9139 /**
9140  * lpfc_sli4_sp_intr_handler - Slow-path interrupt handler to SLI-4 device
9141  * @irq: Interrupt number.
9142  * @dev_id: The device context pointer.
9143  *
9144  * This function is directly called from the PCI layer as an interrupt
9145  * service routine when device with SLI-4 interface spec is enabled with
9146  * MSI-X multi-message interrupt mode and there are slow-path events in
9147  * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
9148  * interrupt mode, this function is called as part of the device-level
9149  * interrupt handler. When the PCI slot is in error recovery or the HBA is
9150  * undergoing initialization, the interrupt handler will not process the
9151  * interrupt. The link attention and ELS ring attention events are handled
9152  * by the worker thread. The interrupt handler signals the worker thread
9153  * and returns for these events. This function is called without any lock
9154  * held. It gets the hbalock to access and update SLI data structures.
9155  *
9156  * This function returns IRQ_HANDLED when interrupt is handled else it
9157  * returns IRQ_NONE.
9158  **/
9159 irqreturn_t
9160 lpfc_sli4_sp_intr_handler(int irq, void *dev_id)
9161 {
9162         struct lpfc_hba *phba;
9163         struct lpfc_queue *speq;
9164         struct lpfc_eqe *eqe;
9165         unsigned long iflag;
9166         int ecount = 0;
9167
9168         /*
9169          * Get the driver's phba structure from the dev_id
9170          */
9171         phba = (struct lpfc_hba *)dev_id;
9172
9173         if (unlikely(!phba))
9174                 return IRQ_NONE;
9175
9176         /* Get to the EQ struct associated with this vector */
9177         speq = phba->sli4_hba.sp_eq;
9178
9179         /* Check device state for handling interrupt */
9180         if (unlikely(lpfc_intr_state_check(phba))) {
9181                 /* Check again for link_state with lock held */
9182                 spin_lock_irqsave(&phba->hbalock, iflag);
9183                 if (phba->link_state < LPFC_LINK_DOWN)
9184                         /* Flush, clear interrupt, and rearm the EQ */
9185                         lpfc_sli4_eq_flush(phba, speq);
9186                 spin_unlock_irqrestore(&phba->hbalock, iflag);
9187                 return IRQ_NONE;
9188         }
9189
9190         /*
9191          * Process all the event on FCP slow-path EQ
9192          */
9193         while ((eqe = lpfc_sli4_eq_get(speq))) {
9194                 lpfc_sli4_sp_handle_eqe(phba, eqe);
9195                 if (!(++ecount % LPFC_GET_QE_REL_INT))
9196                         lpfc_sli4_eq_release(speq, LPFC_QUEUE_NOARM);
9197         }
9198
9199         /* Always clear and re-arm the slow-path EQ */
9200         lpfc_sli4_eq_release(speq, LPFC_QUEUE_REARM);
9201
9202         /* Catch the no cq entry condition */
9203         if (unlikely(ecount == 0)) {
9204                 if (phba->intr_type == MSIX)
9205                         /* MSI-X treated interrupt served as no EQ share INT */
9206                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9207                                         "0357 MSI-X interrupt with no EQE\n");
9208                 else
9209                         /* Non MSI-X treated on interrupt as EQ share INT */
9210                         return IRQ_NONE;
9211         }
9212
9213         return IRQ_HANDLED;
9214 } /* lpfc_sli4_sp_intr_handler */
9215
9216 /**
9217  * lpfc_sli4_fp_intr_handler - Fast-path interrupt handler to SLI-4 device
9218  * @irq: Interrupt number.
9219  * @dev_id: The device context pointer.
9220  *
9221  * This function is directly called from the PCI layer as an interrupt
9222  * service routine when device with SLI-4 interface spec is enabled with
9223  * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
9224  * ring event in the HBA. However, when the device is enabled with either
9225  * MSI or Pin-IRQ interrupt mode, this function is called as part of the
9226  * device-level interrupt handler. When the PCI slot is in error recovery
9227  * or the HBA is undergoing initialization, the interrupt handler will not
9228  * process the interrupt. The SCSI FCP fast-path ring event are handled in
9229  * the intrrupt context. This function is called without any lock held.
9230  * It gets the hbalock to access and update SLI data structures. Note that,
9231  * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
9232  * equal to that of FCP CQ index.
9233  *
9234  * This function returns IRQ_HANDLED when interrupt is handled else it
9235  * returns IRQ_NONE.
9236  **/
9237 irqreturn_t
9238 lpfc_sli4_fp_intr_handler(int irq, void *dev_id)
9239 {
9240         struct lpfc_hba *phba;
9241         struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
9242         struct lpfc_queue *fpeq;
9243         struct lpfc_eqe *eqe;
9244         unsigned long iflag;
9245         int ecount = 0;
9246         uint32_t fcp_eqidx;
9247
9248         /* Get the driver's phba structure from the dev_id */
9249         fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
9250         phba = fcp_eq_hdl->phba;
9251         fcp_eqidx = fcp_eq_hdl->idx;
9252
9253         if (unlikely(!phba))
9254                 return IRQ_NONE;
9255
9256         /* Get to the EQ struct associated with this vector */
9257         fpeq = phba->sli4_hba.fp_eq[fcp_eqidx];
9258
9259         /* Check device state for handling interrupt */
9260         if (unlikely(lpfc_intr_state_check(phba))) {
9261                 /* Check again for link_state with lock held */
9262                 spin_lock_irqsave(&phba->hbalock, iflag);
9263                 if (phba->link_state < LPFC_LINK_DOWN)
9264                         /* Flush, clear interrupt, and rearm the EQ */
9265                         lpfc_sli4_eq_flush(phba, fpeq);
9266                 spin_unlock_irqrestore(&phba->hbalock, iflag);
9267                 return IRQ_NONE;
9268         }
9269
9270         /*
9271          * Process all the event on FCP fast-path EQ
9272          */
9273         while ((eqe = lpfc_sli4_eq_get(fpeq))) {
9274                 lpfc_sli4_fp_handle_eqe(phba, eqe, fcp_eqidx);
9275                 if (!(++ecount % LPFC_GET_QE_REL_INT))
9276                         lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
9277         }
9278
9279         /* Always clear and re-arm the fast-path EQ */
9280         lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
9281
9282         if (unlikely(ecount == 0)) {
9283                 if (phba->intr_type == MSIX)
9284                         /* MSI-X treated interrupt served as no EQ share INT */
9285                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9286                                         "0358 MSI-X interrupt with no EQE\n");
9287                 else
9288                         /* Non MSI-X treated on interrupt as EQ share INT */
9289                         return IRQ_NONE;
9290         }
9291
9292         return IRQ_HANDLED;
9293 } /* lpfc_sli4_fp_intr_handler */
9294
9295 /**
9296  * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
9297  * @irq: Interrupt number.
9298  * @dev_id: The device context pointer.
9299  *
9300  * This function is the device-level interrupt handler to device with SLI-4
9301  * interface spec, called from the PCI layer when either MSI or Pin-IRQ
9302  * interrupt mode is enabled and there is an event in the HBA which requires
9303  * driver attention. This function invokes the slow-path interrupt attention
9304  * handling function and fast-path interrupt attention handling function in
9305  * turn to process the relevant HBA attention events. This function is called
9306  * without any lock held. It gets the hbalock to access and update SLI data
9307  * structures.
9308  *
9309  * This function returns IRQ_HANDLED when interrupt is handled, else it
9310  * returns IRQ_NONE.
9311  **/
9312 irqreturn_t
9313 lpfc_sli4_intr_handler(int irq, void *dev_id)
9314 {
9315         struct lpfc_hba  *phba;
9316         irqreturn_t sp_irq_rc, fp_irq_rc;
9317         bool fp_handled = false;
9318         uint32_t fcp_eqidx;
9319
9320         /* Get the driver's phba structure from the dev_id */
9321         phba = (struct lpfc_hba *)dev_id;
9322
9323         if (unlikely(!phba))
9324                 return IRQ_NONE;
9325
9326         /*
9327          * Invokes slow-path host attention interrupt handling as appropriate.
9328          */
9329         sp_irq_rc = lpfc_sli4_sp_intr_handler(irq, dev_id);
9330
9331         /*
9332          * Invoke fast-path host attention interrupt handling as appropriate.
9333          */
9334         for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
9335                 fp_irq_rc = lpfc_sli4_fp_intr_handler(irq,
9336                                         &phba->sli4_hba.fcp_eq_hdl[fcp_eqidx]);
9337                 if (fp_irq_rc == IRQ_HANDLED)
9338                         fp_handled |= true;
9339         }
9340
9341         return (fp_handled == true) ? IRQ_HANDLED : sp_irq_rc;
9342 } /* lpfc_sli4_intr_handler */
9343
9344 /**
9345  * lpfc_sli4_queue_free - free a queue structure and associated memory
9346  * @queue: The queue structure to free.
9347  *
9348  * This function frees a queue structure and the DMAable memeory used for
9349  * the host resident queue. This function must be called after destroying the
9350  * queue on the HBA.
9351  **/
9352 void
9353 lpfc_sli4_queue_free(struct lpfc_queue *queue)
9354 {
9355         struct lpfc_dmabuf *dmabuf;
9356
9357         if (!queue)
9358                 return;
9359
9360         while (!list_empty(&queue->page_list)) {
9361                 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
9362                                  list);
9363                 dma_free_coherent(&queue->phba->pcidev->dev, PAGE_SIZE,
9364                                   dmabuf->virt, dmabuf->phys);
9365                 kfree(dmabuf);
9366         }
9367         kfree(queue);
9368         return;
9369 }
9370
9371 /**
9372  * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
9373  * @phba: The HBA that this queue is being created on.
9374  * @entry_size: The size of each queue entry for this queue.
9375  * @entry count: The number of entries that this queue will handle.
9376  *
9377  * This function allocates a queue structure and the DMAable memory used for
9378  * the host resident queue. This function must be called before creating the
9379  * queue on the HBA.
9380  **/
9381 struct lpfc_queue *
9382 lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
9383                       uint32_t entry_count)
9384 {
9385         struct lpfc_queue *queue;
9386         struct lpfc_dmabuf *dmabuf;
9387         int x, total_qe_count;
9388         void *dma_pointer;
9389
9390
9391         queue = kzalloc(sizeof(struct lpfc_queue) +
9392                         (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
9393         if (!queue)
9394                 return NULL;
9395         queue->page_count = (PAGE_ALIGN(entry_size * entry_count))/PAGE_SIZE;
9396         INIT_LIST_HEAD(&queue->list);
9397         INIT_LIST_HEAD(&queue->page_list);
9398         INIT_LIST_HEAD(&queue->child_list);
9399         for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
9400                 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
9401                 if (!dmabuf)
9402                         goto out_fail;
9403                 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
9404                                                   PAGE_SIZE, &dmabuf->phys,
9405                                                   GFP_KERNEL);
9406                 if (!dmabuf->virt) {
9407                         kfree(dmabuf);
9408                         goto out_fail;
9409                 }
9410                 memset(dmabuf->virt, 0, PAGE_SIZE);
9411                 dmabuf->buffer_tag = x;
9412                 list_add_tail(&dmabuf->list, &queue->page_list);
9413                 /* initialize queue's entry array */
9414                 dma_pointer = dmabuf->virt;
9415                 for (; total_qe_count < entry_count &&
9416                      dma_pointer < (PAGE_SIZE + dmabuf->virt);
9417                      total_qe_count++, dma_pointer += entry_size) {
9418                         queue->qe[total_qe_count].address = dma_pointer;
9419                 }
9420         }
9421         queue->entry_size = entry_size;
9422         queue->entry_count = entry_count;
9423         queue->phba = phba;
9424
9425         return queue;
9426 out_fail:
9427         lpfc_sli4_queue_free(queue);
9428         return NULL;
9429 }
9430
9431 /**
9432  * lpfc_eq_create - Create an Event Queue on the HBA
9433  * @phba: HBA structure that indicates port to create a queue on.
9434  * @eq: The queue structure to use to create the event queue.
9435  * @imax: The maximum interrupt per second limit.
9436  *
9437  * This function creates an event queue, as detailed in @eq, on a port,
9438  * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
9439  *
9440  * The @phba struct is used to send mailbox command to HBA. The @eq struct
9441  * is used to get the entry count and entry size that are necessary to
9442  * determine the number of pages to allocate and use for this queue. This
9443  * function will send the EQ_CREATE mailbox command to the HBA to setup the
9444  * event queue. This function is asynchronous and will wait for the mailbox
9445  * command to finish before continuing.
9446  *
9447  * On success this function will return a zero. If unable to allocate enough
9448  * memory this function will return ENOMEM. If the queue create mailbox command
9449  * fails this function will return ENXIO.
9450  **/
9451 uint32_t
9452 lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint16_t imax)
9453 {
9454         struct lpfc_mbx_eq_create *eq_create;
9455         LPFC_MBOXQ_t *mbox;
9456         int rc, length, status = 0;
9457         struct lpfc_dmabuf *dmabuf;
9458         uint32_t shdr_status, shdr_add_status;
9459         union lpfc_sli4_cfg_shdr *shdr;
9460         uint16_t dmult;
9461
9462         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9463         if (!mbox)
9464                 return -ENOMEM;
9465         length = (sizeof(struct lpfc_mbx_eq_create) -
9466                   sizeof(struct lpfc_sli4_cfg_mhdr));
9467         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
9468                          LPFC_MBOX_OPCODE_EQ_CREATE,
9469                          length, LPFC_SLI4_MBX_EMBED);
9470         eq_create = &mbox->u.mqe.un.eq_create;
9471         bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
9472                eq->page_count);
9473         bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
9474                LPFC_EQE_SIZE);
9475         bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
9476         /* Calculate delay multiper from maximum interrupt per second */
9477         dmult = LPFC_DMULT_CONST/imax - 1;
9478         bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
9479                dmult);
9480         switch (eq->entry_count) {
9481         default:
9482                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9483                                 "0360 Unsupported EQ count. (%d)\n",
9484                                 eq->entry_count);
9485                 if (eq->entry_count < 256)
9486                         return -EINVAL;
9487                 /* otherwise default to smallest count (drop through) */
9488         case 256:
9489                 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9490                        LPFC_EQ_CNT_256);
9491                 break;
9492         case 512:
9493                 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9494                        LPFC_EQ_CNT_512);
9495                 break;
9496         case 1024:
9497                 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9498                        LPFC_EQ_CNT_1024);
9499                 break;
9500         case 2048:
9501                 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9502                        LPFC_EQ_CNT_2048);
9503                 break;
9504         case 4096:
9505                 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9506                        LPFC_EQ_CNT_4096);
9507                 break;
9508         }
9509         list_for_each_entry(dmabuf, &eq->page_list, list) {
9510                 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9511                                         putPaddrLow(dmabuf->phys);
9512                 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9513                                         putPaddrHigh(dmabuf->phys);
9514         }
9515         mbox->vport = phba->pport;
9516         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
9517         mbox->context1 = NULL;
9518         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9519         shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
9520         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9521         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9522         if (shdr_status || shdr_add_status || rc) {
9523                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9524                                 "2500 EQ_CREATE mailbox failed with "
9525                                 "status x%x add_status x%x, mbx status x%x\n",
9526                                 shdr_status, shdr_add_status, rc);
9527                 status = -ENXIO;
9528         }
9529         eq->type = LPFC_EQ;
9530         eq->subtype = LPFC_NONE;
9531         eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
9532         if (eq->queue_id == 0xFFFF)
9533                 status = -ENXIO;
9534         eq->host_index = 0;
9535         eq->hba_index = 0;
9536
9537         mempool_free(mbox, phba->mbox_mem_pool);
9538         return status;
9539 }
9540
9541 /**
9542  * lpfc_cq_create - Create a Completion Queue on the HBA
9543  * @phba: HBA structure that indicates port to create a queue on.
9544  * @cq: The queue structure to use to create the completion queue.
9545  * @eq: The event queue to bind this completion queue to.
9546  *
9547  * This function creates a completion queue, as detailed in @wq, on a port,
9548  * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
9549  *
9550  * The @phba struct is used to send mailbox command to HBA. The @cq struct
9551  * is used to get the entry count and entry size that are necessary to
9552  * determine the number of pages to allocate and use for this queue. The @eq
9553  * is used to indicate which event queue to bind this completion queue to. This
9554  * function will send the CQ_CREATE mailbox command to the HBA to setup the
9555  * completion queue. This function is asynchronous and will wait for the mailbox
9556  * command to finish before continuing.
9557  *
9558  * On success this function will return a zero. If unable to allocate enough
9559  * memory this function will return ENOMEM. If the queue create mailbox command
9560  * fails this function will return ENXIO.
9561  **/
9562 uint32_t
9563 lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
9564                struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
9565 {
9566         struct lpfc_mbx_cq_create *cq_create;
9567         struct lpfc_dmabuf *dmabuf;
9568         LPFC_MBOXQ_t *mbox;
9569         int rc, length, status = 0;
9570         uint32_t shdr_status, shdr_add_status;
9571         union lpfc_sli4_cfg_shdr *shdr;
9572
9573         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9574         if (!mbox)
9575                 return -ENOMEM;
9576         length = (sizeof(struct lpfc_mbx_cq_create) -
9577                   sizeof(struct lpfc_sli4_cfg_mhdr));
9578         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
9579                          LPFC_MBOX_OPCODE_CQ_CREATE,
9580                          length, LPFC_SLI4_MBX_EMBED);
9581         cq_create = &mbox->u.mqe.un.cq_create;
9582         bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
9583                     cq->page_count);
9584         bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
9585         bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
9586         bf_set(lpfc_cq_eq_id, &cq_create->u.request.context, eq->queue_id);
9587         switch (cq->entry_count) {
9588         default:
9589                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9590                                 "0361 Unsupported CQ count. (%d)\n",
9591                                 cq->entry_count);
9592                 if (cq->entry_count < 256)
9593                         return -EINVAL;
9594                 /* otherwise default to smallest count (drop through) */
9595         case 256:
9596                 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
9597                        LPFC_CQ_CNT_256);
9598                 break;
9599         case 512:
9600                 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
9601                        LPFC_CQ_CNT_512);
9602                 break;
9603         case 1024:
9604                 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
9605                        LPFC_CQ_CNT_1024);
9606                 break;
9607         }
9608         list_for_each_entry(dmabuf, &cq->page_list, list) {
9609                 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9610                                         putPaddrLow(dmabuf->phys);
9611                 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9612                                         putPaddrHigh(dmabuf->phys);
9613         }
9614         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9615
9616         /* The IOCTL status is embedded in the mailbox subheader. */
9617         shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
9618         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9619         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9620         if (shdr_status || shdr_add_status || rc) {
9621                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9622                                 "2501 CQ_CREATE mailbox failed with "
9623                                 "status x%x add_status x%x, mbx status x%x\n",
9624                                 shdr_status, shdr_add_status, rc);
9625                 status = -ENXIO;
9626                 goto out;
9627         }
9628         cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
9629         if (cq->queue_id == 0xFFFF) {
9630                 status = -ENXIO;
9631                 goto out;
9632         }
9633         /* link the cq onto the parent eq child list */
9634         list_add_tail(&cq->list, &eq->child_list);
9635         /* Set up completion queue's type and subtype */
9636         cq->type = type;
9637         cq->subtype = subtype;
9638         cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
9639         cq->host_index = 0;
9640         cq->hba_index = 0;
9641
9642 out:
9643         mempool_free(mbox, phba->mbox_mem_pool);
9644         return status;
9645 }
9646
9647 /**
9648  * lpfc_mq_create - Create a mailbox Queue on the HBA
9649  * @phba: HBA structure that indicates port to create a queue on.
9650  * @mq: The queue structure to use to create the mailbox queue.
9651  *
9652  * This function creates a mailbox queue, as detailed in @mq, on a port,
9653  * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
9654  *
9655  * The @phba struct is used to send mailbox command to HBA. The @cq struct
9656  * is used to get the entry count and entry size that are necessary to
9657  * determine the number of pages to allocate and use for this queue. This
9658  * function will send the MQ_CREATE mailbox command to the HBA to setup the
9659  * mailbox queue. This function is asynchronous and will wait for the mailbox
9660  * command to finish before continuing.
9661  *
9662  * On success this function will return a zero. If unable to allocate enough
9663  * memory this function will return ENOMEM. If the queue create mailbox command
9664  * fails this function will return ENXIO.
9665  **/
9666 uint32_t
9667 lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
9668                struct lpfc_queue *cq, uint32_t subtype)
9669 {
9670         struct lpfc_mbx_mq_create *mq_create;
9671         struct lpfc_dmabuf *dmabuf;
9672         LPFC_MBOXQ_t *mbox;
9673         int rc, length, status = 0;
9674         uint32_t shdr_status, shdr_add_status;
9675         union lpfc_sli4_cfg_shdr *shdr;
9676
9677         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9678         if (!mbox)
9679                 return -ENOMEM;
9680         length = (sizeof(struct lpfc_mbx_mq_create) -
9681                   sizeof(struct lpfc_sli4_cfg_mhdr));
9682         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
9683                          LPFC_MBOX_OPCODE_MQ_CREATE,
9684                          length, LPFC_SLI4_MBX_EMBED);
9685         mq_create = &mbox->u.mqe.un.mq_create;
9686         bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
9687                     mq->page_count);
9688         bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
9689                     cq->queue_id);
9690         bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
9691         switch (mq->entry_count) {
9692         default:
9693                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9694                                 "0362 Unsupported MQ count. (%d)\n",
9695                                 mq->entry_count);
9696                 if (mq->entry_count < 16)
9697                         return -EINVAL;
9698                 /* otherwise default to smallest count (drop through) */
9699         case 16:
9700                 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
9701                        LPFC_MQ_CNT_16);
9702                 break;
9703         case 32:
9704                 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
9705                        LPFC_MQ_CNT_32);
9706                 break;
9707         case 64:
9708                 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
9709                        LPFC_MQ_CNT_64);
9710                 break;
9711         case 128:
9712                 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
9713                        LPFC_MQ_CNT_128);
9714                 break;
9715         }
9716         list_for_each_entry(dmabuf, &mq->page_list, list) {
9717                 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9718                                         putPaddrLow(dmabuf->phys);
9719                 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9720                                         putPaddrHigh(dmabuf->phys);
9721         }
9722         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9723         /* The IOCTL status is embedded in the mailbox subheader. */
9724         shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
9725         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9726         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9727         if (shdr_status || shdr_add_status || rc) {
9728                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9729                                 "2502 MQ_CREATE mailbox failed with "
9730                                 "status x%x add_status x%x, mbx status x%x\n",
9731                                 shdr_status, shdr_add_status, rc);
9732                 status = -ENXIO;
9733                 goto out;
9734         }
9735         mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id, &mq_create->u.response);
9736         if (mq->queue_id == 0xFFFF) {
9737                 status = -ENXIO;
9738                 goto out;
9739         }
9740         mq->type = LPFC_MQ;
9741         mq->subtype = subtype;
9742         mq->host_index = 0;
9743         mq->hba_index = 0;
9744
9745         /* link the mq onto the parent cq child list */
9746         list_add_tail(&mq->list, &cq->child_list);
9747 out:
9748         mempool_free(mbox, phba->mbox_mem_pool);
9749         return status;
9750 }
9751
9752 /**
9753  * lpfc_wq_create - Create a Work Queue on the HBA
9754  * @phba: HBA structure that indicates port to create a queue on.
9755  * @wq: The queue structure to use to create the work queue.
9756  * @cq: The completion queue to bind this work queue to.
9757  * @subtype: The subtype of the work queue indicating its functionality.
9758  *
9759  * This function creates a work queue, as detailed in @wq, on a port, described
9760  * by @phba by sending a WQ_CREATE mailbox command to the HBA.
9761  *
9762  * The @phba struct is used to send mailbox command to HBA. The @wq struct
9763  * is used to get the entry count and entry size that are necessary to
9764  * determine the number of pages to allocate and use for this queue. The @cq
9765  * is used to indicate which completion queue to bind this work queue to. This
9766  * function will send the WQ_CREATE mailbox command to the HBA to setup the
9767  * work queue. This function is asynchronous and will wait for the mailbox
9768  * command to finish before continuing.
9769  *
9770  * On success this function will return a zero. If unable to allocate enough
9771  * memory this function will return ENOMEM. If the queue create mailbox command
9772  * fails this function will return ENXIO.
9773  **/
9774 uint32_t
9775 lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
9776                struct lpfc_queue *cq, uint32_t subtype)
9777 {
9778         struct lpfc_mbx_wq_create *wq_create;
9779         struct lpfc_dmabuf *dmabuf;
9780         LPFC_MBOXQ_t *mbox;
9781         int rc, length, status = 0;
9782         uint32_t shdr_status, shdr_add_status;
9783         union lpfc_sli4_cfg_shdr *shdr;
9784
9785         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9786         if (!mbox)
9787                 return -ENOMEM;
9788         length = (sizeof(struct lpfc_mbx_wq_create) -
9789                   sizeof(struct lpfc_sli4_cfg_mhdr));
9790         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
9791                          LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
9792                          length, LPFC_SLI4_MBX_EMBED);
9793         wq_create = &mbox->u.mqe.un.wq_create;
9794         bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
9795                     wq->page_count);
9796         bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
9797                     cq->queue_id);
9798         list_for_each_entry(dmabuf, &wq->page_list, list) {
9799                 wq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9800                                         putPaddrLow(dmabuf->phys);
9801                 wq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9802                                         putPaddrHigh(dmabuf->phys);
9803         }
9804         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9805         /* The IOCTL status is embedded in the mailbox subheader. */
9806         shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
9807         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9808         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9809         if (shdr_status || shdr_add_status || rc) {
9810                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9811                                 "2503 WQ_CREATE mailbox failed with "
9812                                 "status x%x add_status x%x, mbx status x%x\n",
9813                                 shdr_status, shdr_add_status, rc);
9814                 status = -ENXIO;
9815                 goto out;
9816         }
9817         wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
9818         if (wq->queue_id == 0xFFFF) {
9819                 status = -ENXIO;
9820                 goto out;
9821         }
9822         wq->type = LPFC_WQ;
9823         wq->subtype = subtype;
9824         wq->host_index = 0;
9825         wq->hba_index = 0;
9826
9827         /* link the wq onto the parent cq child list */
9828         list_add_tail(&wq->list, &cq->child_list);
9829 out:
9830         mempool_free(mbox, phba->mbox_mem_pool);
9831         return status;
9832 }
9833
9834 /**
9835  * lpfc_rq_create - Create a Receive Queue on the HBA
9836  * @phba: HBA structure that indicates port to create a queue on.
9837  * @hrq: The queue structure to use to create the header receive queue.
9838  * @drq: The queue structure to use to create the data receive queue.
9839  * @cq: The completion queue to bind this work queue to.
9840  *
9841  * This function creates a receive buffer queue pair , as detailed in @hrq and
9842  * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
9843  * to the HBA.
9844  *
9845  * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
9846  * struct is used to get the entry count that is necessary to determine the
9847  * number of pages to use for this queue. The @cq is used to indicate which
9848  * completion queue to bind received buffers that are posted to these queues to.
9849  * This function will send the RQ_CREATE mailbox command to the HBA to setup the
9850  * receive queue pair. This function is asynchronous and will wait for the
9851  * mailbox command to finish before continuing.
9852  *
9853  * On success this function will return a zero. If unable to allocate enough
9854  * memory this function will return ENOMEM. If the queue create mailbox command
9855  * fails this function will return ENXIO.
9856  **/
9857 uint32_t
9858 lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
9859                struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
9860 {
9861         struct lpfc_mbx_rq_create *rq_create;
9862         struct lpfc_dmabuf *dmabuf;
9863         LPFC_MBOXQ_t *mbox;
9864         int rc, length, status = 0;
9865         uint32_t shdr_status, shdr_add_status;
9866         union lpfc_sli4_cfg_shdr *shdr;
9867
9868         if (hrq->entry_count != drq->entry_count)
9869                 return -EINVAL;
9870         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9871         if (!mbox)
9872                 return -ENOMEM;
9873         length = (sizeof(struct lpfc_mbx_rq_create) -
9874                   sizeof(struct lpfc_sli4_cfg_mhdr));
9875         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
9876                          LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
9877                          length, LPFC_SLI4_MBX_EMBED);
9878         rq_create = &mbox->u.mqe.un.rq_create;
9879         switch (hrq->entry_count) {
9880         default:
9881                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9882                                 "2535 Unsupported RQ count. (%d)\n",
9883                                 hrq->entry_count);
9884                 if (hrq->entry_count < 512)
9885                         return -EINVAL;
9886                 /* otherwise default to smallest count (drop through) */
9887         case 512:
9888                 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
9889                        LPFC_RQ_RING_SIZE_512);
9890                 break;
9891         case 1024:
9892                 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
9893                        LPFC_RQ_RING_SIZE_1024);
9894                 break;
9895         case 2048:
9896                 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
9897                        LPFC_RQ_RING_SIZE_2048);
9898                 break;
9899         case 4096:
9900                 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
9901                        LPFC_RQ_RING_SIZE_4096);
9902                 break;
9903         }
9904         bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
9905                cq->queue_id);
9906         bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
9907                hrq->page_count);
9908         bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
9909                LPFC_HDR_BUF_SIZE);
9910         list_for_each_entry(dmabuf, &hrq->page_list, list) {
9911                 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9912                                         putPaddrLow(dmabuf->phys);
9913                 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9914                                         putPaddrHigh(dmabuf->phys);
9915         }
9916         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9917         /* The IOCTL status is embedded in the mailbox subheader. */
9918         shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
9919         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9920         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9921         if (shdr_status || shdr_add_status || rc) {
9922                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9923                                 "2504 RQ_CREATE mailbox failed with "
9924                                 "status x%x add_status x%x, mbx status x%x\n",
9925                                 shdr_status, shdr_add_status, rc);
9926                 status = -ENXIO;
9927                 goto out;
9928         }
9929         hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
9930         if (hrq->queue_id == 0xFFFF) {
9931                 status = -ENXIO;
9932                 goto out;
9933         }
9934         hrq->type = LPFC_HRQ;
9935         hrq->subtype = subtype;
9936         hrq->host_index = 0;
9937         hrq->hba_index = 0;
9938
9939         /* now create the data queue */
9940         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
9941                          LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
9942                          length, LPFC_SLI4_MBX_EMBED);
9943         switch (drq->entry_count) {
9944         default:
9945                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9946                                 "2536 Unsupported RQ count. (%d)\n",
9947                                 drq->entry_count);
9948                 if (drq->entry_count < 512)
9949                         return -EINVAL;
9950                 /* otherwise default to smallest count (drop through) */
9951         case 512:
9952                 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
9953                        LPFC_RQ_RING_SIZE_512);
9954                 break;
9955         case 1024:
9956                 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
9957                        LPFC_RQ_RING_SIZE_1024);
9958                 break;
9959         case 2048:
9960                 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
9961                        LPFC_RQ_RING_SIZE_2048);
9962                 break;
9963         case 4096:
9964                 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
9965                        LPFC_RQ_RING_SIZE_4096);
9966                 break;
9967         }
9968         bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
9969                cq->queue_id);
9970         bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
9971                drq->page_count);
9972         bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
9973                LPFC_DATA_BUF_SIZE);
9974         list_for_each_entry(dmabuf, &drq->page_list, list) {
9975                 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9976                                         putPaddrLow(dmabuf->phys);
9977                 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9978                                         putPaddrHigh(dmabuf->phys);
9979         }
9980         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9981         /* The IOCTL status is embedded in the mailbox subheader. */
9982         shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
9983         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9984         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9985         if (shdr_status || shdr_add_status || rc) {
9986                 status = -ENXIO;
9987                 goto out;
9988         }
9989         drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
9990         if (drq->queue_id == 0xFFFF) {
9991                 status = -ENXIO;
9992                 goto out;
9993         }
9994         drq->type = LPFC_DRQ;
9995         drq->subtype = subtype;
9996         drq->host_index = 0;
9997         drq->hba_index = 0;
9998
9999         /* link the header and data RQs onto the parent cq child list */
10000         list_add_tail(&hrq->list, &cq->child_list);
10001         list_add_tail(&drq->list, &cq->child_list);
10002
10003 out:
10004         mempool_free(mbox, phba->mbox_mem_pool);
10005         return status;
10006 }
10007
10008 /**
10009  * lpfc_eq_destroy - Destroy an event Queue on the HBA
10010  * @eq: The queue structure associated with the queue to destroy.
10011  *
10012  * This function destroys a queue, as detailed in @eq by sending an mailbox
10013  * command, specific to the type of queue, to the HBA.
10014  *
10015  * The @eq struct is used to get the queue ID of the queue to destroy.
10016  *
10017  * On success this function will return a zero. If the queue destroy mailbox
10018  * command fails this function will return ENXIO.
10019  **/
10020 uint32_t
10021 lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
10022 {
10023         LPFC_MBOXQ_t *mbox;
10024         int rc, length, status = 0;
10025         uint32_t shdr_status, shdr_add_status;
10026         union lpfc_sli4_cfg_shdr *shdr;
10027
10028         if (!eq)
10029                 return -ENODEV;
10030         mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
10031         if (!mbox)
10032                 return -ENOMEM;
10033         length = (sizeof(struct lpfc_mbx_eq_destroy) -
10034                   sizeof(struct lpfc_sli4_cfg_mhdr));
10035         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10036                          LPFC_MBOX_OPCODE_EQ_DESTROY,
10037                          length, LPFC_SLI4_MBX_EMBED);
10038         bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
10039                eq->queue_id);
10040         mbox->vport = eq->phba->pport;
10041         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10042
10043         rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
10044         /* The IOCTL status is embedded in the mailbox subheader. */
10045         shdr = (union lpfc_sli4_cfg_shdr *)
10046                 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
10047         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10048         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10049         if (shdr_status || shdr_add_status || rc) {
10050                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10051                                 "2505 EQ_DESTROY mailbox failed with "
10052                                 "status x%x add_status x%x, mbx status x%x\n",
10053                                 shdr_status, shdr_add_status, rc);
10054                 status = -ENXIO;
10055         }
10056
10057         /* Remove eq from any list */
10058         list_del_init(&eq->list);
10059         mempool_free(mbox, eq->phba->mbox_mem_pool);
10060         return status;
10061 }
10062
10063 /**
10064  * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
10065  * @cq: The queue structure associated with the queue to destroy.
10066  *
10067  * This function destroys a queue, as detailed in @cq by sending an mailbox
10068  * command, specific to the type of queue, to the HBA.
10069  *
10070  * The @cq struct is used to get the queue ID of the queue to destroy.
10071  *
10072  * On success this function will return a zero. If the queue destroy mailbox
10073  * command fails this function will return ENXIO.
10074  **/
10075 uint32_t
10076 lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
10077 {
10078         LPFC_MBOXQ_t *mbox;
10079         int rc, length, status = 0;
10080         uint32_t shdr_status, shdr_add_status;
10081         union lpfc_sli4_cfg_shdr *shdr;
10082
10083         if (!cq)
10084                 return -ENODEV;
10085         mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
10086         if (!mbox)
10087                 return -ENOMEM;
10088         length = (sizeof(struct lpfc_mbx_cq_destroy) -
10089                   sizeof(struct lpfc_sli4_cfg_mhdr));
10090         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10091                          LPFC_MBOX_OPCODE_CQ_DESTROY,
10092                          length, LPFC_SLI4_MBX_EMBED);
10093         bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
10094                cq->queue_id);
10095         mbox->vport = cq->phba->pport;
10096         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10097         rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
10098         /* The IOCTL status is embedded in the mailbox subheader. */
10099         shdr = (union lpfc_sli4_cfg_shdr *)
10100                 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
10101         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10102         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10103         if (shdr_status || shdr_add_status || rc) {
10104                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10105                                 "2506 CQ_DESTROY mailbox failed with "
10106                                 "status x%x add_status x%x, mbx status x%x\n",
10107                                 shdr_status, shdr_add_status, rc);
10108                 status = -ENXIO;
10109         }
10110         /* Remove cq from any list */
10111         list_del_init(&cq->list);
10112         mempool_free(mbox, cq->phba->mbox_mem_pool);
10113         return status;
10114 }
10115
10116 /**
10117  * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
10118  * @qm: The queue structure associated with the queue to destroy.
10119  *
10120  * This function destroys a queue, as detailed in @mq by sending an mailbox
10121  * command, specific to the type of queue, to the HBA.
10122  *
10123  * The @mq struct is used to get the queue ID of the queue to destroy.
10124  *
10125  * On success this function will return a zero. If the queue destroy mailbox
10126  * command fails this function will return ENXIO.
10127  **/
10128 uint32_t
10129 lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
10130 {
10131         LPFC_MBOXQ_t *mbox;
10132         int rc, length, status = 0;
10133         uint32_t shdr_status, shdr_add_status;
10134         union lpfc_sli4_cfg_shdr *shdr;
10135
10136         if (!mq)
10137                 return -ENODEV;
10138         mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
10139         if (!mbox)
10140                 return -ENOMEM;
10141         length = (sizeof(struct lpfc_mbx_mq_destroy) -
10142                   sizeof(struct lpfc_sli4_cfg_mhdr));
10143         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10144                          LPFC_MBOX_OPCODE_MQ_DESTROY,
10145                          length, LPFC_SLI4_MBX_EMBED);
10146         bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
10147                mq->queue_id);
10148         mbox->vport = mq->phba->pport;
10149         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10150         rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
10151         /* The IOCTL status is embedded in the mailbox subheader. */
10152         shdr = (union lpfc_sli4_cfg_shdr *)
10153                 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
10154         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10155         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10156         if (shdr_status || shdr_add_status || rc) {
10157                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10158                                 "2507 MQ_DESTROY mailbox failed with "
10159                                 "status x%x add_status x%x, mbx status x%x\n",
10160                                 shdr_status, shdr_add_status, rc);
10161                 status = -ENXIO;
10162         }
10163         /* Remove mq from any list */
10164         list_del_init(&mq->list);
10165         mempool_free(mbox, mq->phba->mbox_mem_pool);
10166         return status;
10167 }
10168
10169 /**
10170  * lpfc_wq_destroy - Destroy a Work Queue on the HBA
10171  * @wq: The queue structure associated with the queue to destroy.
10172  *
10173  * This function destroys a queue, as detailed in @wq by sending an mailbox
10174  * command, specific to the type of queue, to the HBA.
10175  *
10176  * The @wq struct is used to get the queue ID of the queue to destroy.
10177  *
10178  * On success this function will return a zero. If the queue destroy mailbox
10179  * command fails this function will return ENXIO.
10180  **/
10181 uint32_t
10182 lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
10183 {
10184         LPFC_MBOXQ_t *mbox;
10185         int rc, length, status = 0;
10186         uint32_t shdr_status, shdr_add_status;
10187         union lpfc_sli4_cfg_shdr *shdr;
10188
10189         if (!wq)
10190                 return -ENODEV;
10191         mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
10192         if (!mbox)
10193                 return -ENOMEM;
10194         length = (sizeof(struct lpfc_mbx_wq_destroy) -
10195                   sizeof(struct lpfc_sli4_cfg_mhdr));
10196         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10197                          LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
10198                          length, LPFC_SLI4_MBX_EMBED);
10199         bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
10200                wq->queue_id);
10201         mbox->vport = wq->phba->pport;
10202         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10203         rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
10204         shdr = (union lpfc_sli4_cfg_shdr *)
10205                 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
10206         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10207         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10208         if (shdr_status || shdr_add_status || rc) {
10209                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10210                                 "2508 WQ_DESTROY mailbox failed with "
10211                                 "status x%x add_status x%x, mbx status x%x\n",
10212                                 shdr_status, shdr_add_status, rc);
10213                 status = -ENXIO;
10214         }
10215         /* Remove wq from any list */
10216         list_del_init(&wq->list);
10217         mempool_free(mbox, wq->phba->mbox_mem_pool);
10218         return status;
10219 }
10220
10221 /**
10222  * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
10223  * @rq: The queue structure associated with the queue to destroy.
10224  *
10225  * This function destroys a queue, as detailed in @rq by sending an mailbox
10226  * command, specific to the type of queue, to the HBA.
10227  *
10228  * The @rq struct is used to get the queue ID of the queue to destroy.
10229  *
10230  * On success this function will return a zero. If the queue destroy mailbox
10231  * command fails this function will return ENXIO.
10232  **/
10233 uint32_t
10234 lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
10235                 struct lpfc_queue *drq)
10236 {
10237         LPFC_MBOXQ_t *mbox;
10238         int rc, length, status = 0;
10239         uint32_t shdr_status, shdr_add_status;
10240         union lpfc_sli4_cfg_shdr *shdr;
10241
10242         if (!hrq || !drq)
10243                 return -ENODEV;
10244         mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
10245         if (!mbox)
10246                 return -ENOMEM;
10247         length = (sizeof(struct lpfc_mbx_rq_destroy) -
10248                   sizeof(struct mbox_header));
10249         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10250                          LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
10251                          length, LPFC_SLI4_MBX_EMBED);
10252         bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
10253                hrq->queue_id);
10254         mbox->vport = hrq->phba->pport;
10255         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10256         rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
10257         /* The IOCTL status is embedded in the mailbox subheader. */
10258         shdr = (union lpfc_sli4_cfg_shdr *)
10259                 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
10260         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10261         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10262         if (shdr_status || shdr_add_status || rc) {
10263                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10264                                 "2509 RQ_DESTROY mailbox failed with "
10265                                 "status x%x add_status x%x, mbx status x%x\n",
10266                                 shdr_status, shdr_add_status, rc);
10267                 if (rc != MBX_TIMEOUT)
10268                         mempool_free(mbox, hrq->phba->mbox_mem_pool);
10269                 return -ENXIO;
10270         }
10271         bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
10272                drq->queue_id);
10273         rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
10274         shdr = (union lpfc_sli4_cfg_shdr *)
10275                 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
10276         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10277         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10278         if (shdr_status || shdr_add_status || rc) {
10279                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10280                                 "2510 RQ_DESTROY mailbox failed with "
10281                                 "status x%x add_status x%x, mbx status x%x\n",
10282                                 shdr_status, shdr_add_status, rc);
10283                 status = -ENXIO;
10284         }
10285         list_del_init(&hrq->list);
10286         list_del_init(&drq->list);
10287         mempool_free(mbox, hrq->phba->mbox_mem_pool);
10288         return status;
10289 }
10290
10291 /**
10292  * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
10293  * @phba: The virtual port for which this call being executed.
10294  * @pdma_phys_addr0: Physical address of the 1st SGL page.
10295  * @pdma_phys_addr1: Physical address of the 2nd SGL page.
10296  * @xritag: the xritag that ties this io to the SGL pages.
10297  *
10298  * This routine will post the sgl pages for the IO that has the xritag
10299  * that is in the iocbq structure. The xritag is assigned during iocbq
10300  * creation and persists for as long as the driver is loaded.
10301  * if the caller has fewer than 256 scatter gather segments to map then
10302  * pdma_phys_addr1 should be 0.
10303  * If the caller needs to map more than 256 scatter gather segment then
10304  * pdma_phys_addr1 should be a valid physical address.
10305  * physical address for SGLs must be 64 byte aligned.
10306  * If you are going to map 2 SGL's then the first one must have 256 entries
10307  * the second sgl can have between 1 and 256 entries.
10308  *
10309  * Return codes:
10310  *      0 - Success
10311  *      -ENXIO, -ENOMEM - Failure
10312  **/
10313 int
10314 lpfc_sli4_post_sgl(struct lpfc_hba *phba,
10315                 dma_addr_t pdma_phys_addr0,
10316                 dma_addr_t pdma_phys_addr1,
10317                 uint16_t xritag)
10318 {
10319         struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
10320         LPFC_MBOXQ_t *mbox;
10321         int rc;
10322         uint32_t shdr_status, shdr_add_status;
10323         union lpfc_sli4_cfg_shdr *shdr;
10324
10325         if (xritag == NO_XRI) {
10326                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10327                                 "0364 Invalid param:\n");
10328                 return -EINVAL;
10329         }
10330
10331         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10332         if (!mbox)
10333                 return -ENOMEM;
10334
10335         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10336                         LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
10337                         sizeof(struct lpfc_mbx_post_sgl_pages) -
10338                         sizeof(struct mbox_header), LPFC_SLI4_MBX_EMBED);
10339
10340         post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
10341                                 &mbox->u.mqe.un.post_sgl_pages;
10342         bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
10343         bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
10344
10345         post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
10346                                 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
10347         post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
10348                                 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
10349
10350         post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
10351                                 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
10352         post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
10353                                 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
10354         if (!phba->sli4_hba.intr_enable)
10355                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10356         else
10357                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
10358         /* The IOCTL status is embedded in the mailbox subheader. */
10359         shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
10360         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10361         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10362         if (rc != MBX_TIMEOUT)
10363                 mempool_free(mbox, phba->mbox_mem_pool);
10364         if (shdr_status || shdr_add_status || rc) {
10365                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10366                                 "2511 POST_SGL mailbox failed with "
10367                                 "status x%x add_status x%x, mbx status x%x\n",
10368                                 shdr_status, shdr_add_status, rc);
10369                 rc = -ENXIO;
10370         }
10371         return 0;
10372 }
10373 /**
10374  * lpfc_sli4_remove_all_sgl_pages - Post scatter gather list for an XRI to HBA
10375  * @phba: The virtual port for which this call being executed.
10376  *
10377  * This routine will remove all of the sgl pages registered with the hba.
10378  *
10379  * Return codes:
10380  *      0 - Success
10381  *      -ENXIO, -ENOMEM - Failure
10382  **/
10383 int
10384 lpfc_sli4_remove_all_sgl_pages(struct lpfc_hba *phba)
10385 {
10386         LPFC_MBOXQ_t *mbox;
10387         int rc;
10388         uint32_t shdr_status, shdr_add_status;
10389         union lpfc_sli4_cfg_shdr *shdr;
10390
10391         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10392         if (!mbox)
10393                 return -ENOMEM;
10394
10395         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10396                         LPFC_MBOX_OPCODE_FCOE_REMOVE_SGL_PAGES, 0,
10397                         LPFC_SLI4_MBX_EMBED);
10398         if (!phba->sli4_hba.intr_enable)
10399                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10400         else
10401                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
10402         /* The IOCTL status is embedded in the mailbox subheader. */
10403         shdr = (union lpfc_sli4_cfg_shdr *)
10404                 &mbox->u.mqe.un.sli4_config.header.cfg_shdr;
10405         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10406         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10407         if (rc != MBX_TIMEOUT)
10408                 mempool_free(mbox, phba->mbox_mem_pool);
10409         if (shdr_status || shdr_add_status || rc) {
10410                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10411                                 "2512 REMOVE_ALL_SGL_PAGES mailbox failed with "
10412                                 "status x%x add_status x%x, mbx status x%x\n",
10413                                 shdr_status, shdr_add_status, rc);
10414                 rc = -ENXIO;
10415         }
10416         return rc;
10417 }
10418
10419 /**
10420  * lpfc_sli4_next_xritag - Get an xritag for the io
10421  * @phba: Pointer to HBA context object.
10422  *
10423  * This function gets an xritag for the iocb. If there is no unused xritag
10424  * it will return 0xffff.
10425  * The function returns the allocated xritag if successful, else returns zero.
10426  * Zero is not a valid xritag.
10427  * The caller is not required to hold any lock.
10428  **/
10429 uint16_t
10430 lpfc_sli4_next_xritag(struct lpfc_hba *phba)
10431 {
10432         uint16_t xritag;
10433
10434         spin_lock_irq(&phba->hbalock);
10435         xritag = phba->sli4_hba.next_xri;
10436         if ((xritag != (uint16_t) -1) && xritag <
10437                 (phba->sli4_hba.max_cfg_param.max_xri
10438                         + phba->sli4_hba.max_cfg_param.xri_base)) {
10439                 phba->sli4_hba.next_xri++;
10440                 phba->sli4_hba.max_cfg_param.xri_used++;
10441                 spin_unlock_irq(&phba->hbalock);
10442                 return xritag;
10443         }
10444         spin_unlock_irq(&phba->hbalock);
10445         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
10446                         "2004 Failed to allocate XRI.last XRITAG is %d"
10447                         " Max XRI is %d, Used XRI is %d\n",
10448                         phba->sli4_hba.next_xri,
10449                         phba->sli4_hba.max_cfg_param.max_xri,
10450                         phba->sli4_hba.max_cfg_param.xri_used);
10451         return -1;
10452 }
10453
10454 /**
10455  * lpfc_sli4_post_sgl_list - post a block of sgl list to the firmware.
10456  * @phba: pointer to lpfc hba data structure.
10457  *
10458  * This routine is invoked to post a block of driver's sgl pages to the
10459  * HBA using non-embedded mailbox command. No Lock is held. This routine
10460  * is only called when the driver is loading and after all IO has been
10461  * stopped.
10462  **/
10463 int
10464 lpfc_sli4_post_sgl_list(struct lpfc_hba *phba)
10465 {
10466         struct lpfc_sglq *sglq_entry;
10467         struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
10468         struct sgl_page_pairs *sgl_pg_pairs;
10469         void *viraddr;
10470         LPFC_MBOXQ_t *mbox;
10471         uint32_t reqlen, alloclen, pg_pairs;
10472         uint32_t mbox_tmo;
10473         uint16_t xritag_start = 0;
10474         int els_xri_cnt, rc = 0;
10475         uint32_t shdr_status, shdr_add_status;
10476         union lpfc_sli4_cfg_shdr *shdr;
10477
10478         /* The number of sgls to be posted */
10479         els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
10480
10481         reqlen = els_xri_cnt * sizeof(struct sgl_page_pairs) +
10482                  sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
10483         if (reqlen > PAGE_SIZE) {
10484                 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
10485                                 "2559 Block sgl registration required DMA "
10486                                 "size (%d) great than a page\n", reqlen);
10487                 return -ENOMEM;
10488         }
10489         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10490         if (!mbox) {
10491                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10492                                 "2560 Failed to allocate mbox cmd memory\n");
10493                 return -ENOMEM;
10494         }
10495
10496         /* Allocate DMA memory and set up the non-embedded mailbox command */
10497         alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10498                          LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
10499                          LPFC_SLI4_MBX_NEMBED);
10500
10501         if (alloclen < reqlen) {
10502                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10503                                 "0285 Allocated DMA memory size (%d) is "
10504                                 "less than the requested DMA memory "
10505                                 "size (%d)\n", alloclen, reqlen);
10506                 lpfc_sli4_mbox_cmd_free(phba, mbox);
10507                 return -ENOMEM;
10508         }
10509         /* Get the first SGE entry from the non-embedded DMA memory */
10510         viraddr = mbox->sge_array->addr[0];
10511
10512         /* Set up the SGL pages in the non-embedded DMA pages */
10513         sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
10514         sgl_pg_pairs = &sgl->sgl_pg_pairs;
10515
10516         for (pg_pairs = 0; pg_pairs < els_xri_cnt; pg_pairs++) {
10517                 sglq_entry = phba->sli4_hba.lpfc_els_sgl_array[pg_pairs];
10518                 /* Set up the sge entry */
10519                 sgl_pg_pairs->sgl_pg0_addr_lo =
10520                                 cpu_to_le32(putPaddrLow(sglq_entry->phys));
10521                 sgl_pg_pairs->sgl_pg0_addr_hi =
10522                                 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
10523                 sgl_pg_pairs->sgl_pg1_addr_lo =
10524                                 cpu_to_le32(putPaddrLow(0));
10525                 sgl_pg_pairs->sgl_pg1_addr_hi =
10526                                 cpu_to_le32(putPaddrHigh(0));
10527                 /* Keep the first xritag on the list */
10528                 if (pg_pairs == 0)
10529                         xritag_start = sglq_entry->sli4_xritag;
10530                 sgl_pg_pairs++;
10531         }
10532         bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
10533         bf_set(lpfc_post_sgl_pages_xricnt, sgl, els_xri_cnt);
10534         /* Perform endian conversion if necessary */
10535         sgl->word0 = cpu_to_le32(sgl->word0);
10536
10537         if (!phba->sli4_hba.intr_enable)
10538                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10539         else {
10540                 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
10541                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
10542         }
10543         shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
10544         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10545         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10546         if (rc != MBX_TIMEOUT)
10547                 lpfc_sli4_mbox_cmd_free(phba, mbox);
10548         if (shdr_status || shdr_add_status || rc) {
10549                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10550                                 "2513 POST_SGL_BLOCK mailbox command failed "
10551                                 "status x%x add_status x%x mbx status x%x\n",
10552                                 shdr_status, shdr_add_status, rc);
10553                 rc = -ENXIO;
10554         }
10555         return rc;
10556 }
10557
10558 /**
10559  * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
10560  * @phba: pointer to lpfc hba data structure.
10561  * @sblist: pointer to scsi buffer list.
10562  * @count: number of scsi buffers on the list.
10563  *
10564  * This routine is invoked to post a block of @count scsi sgl pages from a
10565  * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
10566  * No Lock is held.
10567  *
10568  **/
10569 int
10570 lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba, struct list_head *sblist,
10571                               int cnt)
10572 {
10573         struct lpfc_scsi_buf *psb;
10574         struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
10575         struct sgl_page_pairs *sgl_pg_pairs;
10576         void *viraddr;
10577         LPFC_MBOXQ_t *mbox;
10578         uint32_t reqlen, alloclen, pg_pairs;
10579         uint32_t mbox_tmo;
10580         uint16_t xritag_start = 0;
10581         int rc = 0;
10582         uint32_t shdr_status, shdr_add_status;
10583         dma_addr_t pdma_phys_bpl1;
10584         union lpfc_sli4_cfg_shdr *shdr;
10585
10586         /* Calculate the requested length of the dma memory */
10587         reqlen = cnt * sizeof(struct sgl_page_pairs) +
10588                  sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
10589         if (reqlen > PAGE_SIZE) {
10590                 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
10591                                 "0217 Block sgl registration required DMA "
10592                                 "size (%d) great than a page\n", reqlen);
10593                 return -ENOMEM;
10594         }
10595         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10596         if (!mbox) {
10597                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10598                                 "0283 Failed to allocate mbox cmd memory\n");
10599                 return -ENOMEM;
10600         }
10601
10602         /* Allocate DMA memory and set up the non-embedded mailbox command */
10603         alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10604                                 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
10605                                 LPFC_SLI4_MBX_NEMBED);
10606
10607         if (alloclen < reqlen) {
10608                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10609                                 "2561 Allocated DMA memory size (%d) is "
10610                                 "less than the requested DMA memory "
10611                                 "size (%d)\n", alloclen, reqlen);
10612                 lpfc_sli4_mbox_cmd_free(phba, mbox);
10613                 return -ENOMEM;
10614         }
10615         /* Get the first SGE entry from the non-embedded DMA memory */
10616         viraddr = mbox->sge_array->addr[0];
10617
10618         /* Set up the SGL pages in the non-embedded DMA pages */
10619         sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
10620         sgl_pg_pairs = &sgl->sgl_pg_pairs;
10621
10622         pg_pairs = 0;
10623         list_for_each_entry(psb, sblist, list) {
10624                 /* Set up the sge entry */
10625                 sgl_pg_pairs->sgl_pg0_addr_lo =
10626                         cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
10627                 sgl_pg_pairs->sgl_pg0_addr_hi =
10628                         cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
10629                 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
10630                         pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
10631                 else
10632                         pdma_phys_bpl1 = 0;
10633                 sgl_pg_pairs->sgl_pg1_addr_lo =
10634                         cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
10635                 sgl_pg_pairs->sgl_pg1_addr_hi =
10636                         cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
10637                 /* Keep the first xritag on the list */
10638                 if (pg_pairs == 0)
10639                         xritag_start = psb->cur_iocbq.sli4_xritag;
10640                 sgl_pg_pairs++;
10641                 pg_pairs++;
10642         }
10643         bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
10644         bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
10645         /* Perform endian conversion if necessary */
10646         sgl->word0 = cpu_to_le32(sgl->word0);
10647
10648         if (!phba->sli4_hba.intr_enable)
10649                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10650         else {
10651                 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
10652                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
10653         }
10654         shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
10655         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10656         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10657         if (rc != MBX_TIMEOUT)
10658                 lpfc_sli4_mbox_cmd_free(phba, mbox);
10659         if (shdr_status || shdr_add_status || rc) {
10660                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10661                                 "2564 POST_SGL_BLOCK mailbox command failed "
10662                                 "status x%x add_status x%x mbx status x%x\n",
10663                                 shdr_status, shdr_add_status, rc);
10664                 rc = -ENXIO;
10665         }
10666         return rc;
10667 }
10668
10669 /**
10670  * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
10671  * @phba: pointer to lpfc_hba struct that the frame was received on
10672  * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
10673  *
10674  * This function checks the fields in the @fc_hdr to see if the FC frame is a
10675  * valid type of frame that the LPFC driver will handle. This function will
10676  * return a zero if the frame is a valid frame or a non zero value when the
10677  * frame does not pass the check.
10678  **/
10679 static int
10680 lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
10681 {
10682         char *rctl_names[] = FC_RCTL_NAMES_INIT;
10683         char *type_names[] = FC_TYPE_NAMES_INIT;
10684         struct fc_vft_header *fc_vft_hdr;
10685
10686         switch (fc_hdr->fh_r_ctl) {
10687         case FC_RCTL_DD_UNCAT:          /* uncategorized information */
10688         case FC_RCTL_DD_SOL_DATA:       /* solicited data */
10689         case FC_RCTL_DD_UNSOL_CTL:      /* unsolicited control */
10690         case FC_RCTL_DD_SOL_CTL:        /* solicited control or reply */
10691         case FC_RCTL_DD_UNSOL_DATA:     /* unsolicited data */
10692         case FC_RCTL_DD_DATA_DESC:      /* data descriptor */
10693         case FC_RCTL_DD_UNSOL_CMD:      /* unsolicited command */
10694         case FC_RCTL_DD_CMD_STATUS:     /* command status */
10695         case FC_RCTL_ELS_REQ:   /* extended link services request */
10696         case FC_RCTL_ELS_REP:   /* extended link services reply */
10697         case FC_RCTL_ELS4_REQ:  /* FC-4 ELS request */
10698         case FC_RCTL_ELS4_REP:  /* FC-4 ELS reply */
10699         case FC_RCTL_BA_NOP:    /* basic link service NOP */
10700         case FC_RCTL_BA_ABTS:   /* basic link service abort */
10701         case FC_RCTL_BA_RMC:    /* remove connection */
10702         case FC_RCTL_BA_ACC:    /* basic accept */
10703         case FC_RCTL_BA_RJT:    /* basic reject */
10704         case FC_RCTL_BA_PRMT:
10705         case FC_RCTL_ACK_1:     /* acknowledge_1 */
10706         case FC_RCTL_ACK_0:     /* acknowledge_0 */
10707         case FC_RCTL_P_RJT:     /* port reject */
10708         case FC_RCTL_F_RJT:     /* fabric reject */
10709         case FC_RCTL_P_BSY:     /* port busy */
10710         case FC_RCTL_F_BSY:     /* fabric busy to data frame */
10711         case FC_RCTL_F_BSYL:    /* fabric busy to link control frame */
10712         case FC_RCTL_LCR:       /* link credit reset */
10713         case FC_RCTL_END:       /* end */
10714                 break;
10715         case FC_RCTL_VFTH:      /* Virtual Fabric tagging Header */
10716                 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
10717                 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
10718                 return lpfc_fc_frame_check(phba, fc_hdr);
10719         default:
10720                 goto drop;
10721         }
10722         switch (fc_hdr->fh_type) {
10723         case FC_TYPE_BLS:
10724         case FC_TYPE_ELS:
10725         case FC_TYPE_FCP:
10726         case FC_TYPE_CT:
10727                 break;
10728         case FC_TYPE_IP:
10729         case FC_TYPE_ILS:
10730         default:
10731                 goto drop;
10732         }
10733         lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
10734                         "2538 Received frame rctl:%s type:%s\n",
10735                         rctl_names[fc_hdr->fh_r_ctl],
10736                         type_names[fc_hdr->fh_type]);
10737         return 0;
10738 drop:
10739         lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
10740                         "2539 Dropped frame rctl:%s type:%s\n",
10741                         rctl_names[fc_hdr->fh_r_ctl],
10742                         type_names[fc_hdr->fh_type]);
10743         return 1;
10744 }
10745
10746 /**
10747  * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
10748  * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
10749  *
10750  * This function processes the FC header to retrieve the VFI from the VF
10751  * header, if one exists. This function will return the VFI if one exists
10752  * or 0 if no VSAN Header exists.
10753  **/
10754 static uint32_t
10755 lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
10756 {
10757         struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
10758
10759         if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
10760                 return 0;
10761         return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
10762 }
10763
10764 /**
10765  * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
10766  * @phba: Pointer to the HBA structure to search for the vport on
10767  * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
10768  * @fcfi: The FC Fabric ID that the frame came from
10769  *
10770  * This function searches the @phba for a vport that matches the content of the
10771  * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
10772  * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
10773  * returns the matching vport pointer or NULL if unable to match frame to a
10774  * vport.
10775  **/
10776 static struct lpfc_vport *
10777 lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
10778                        uint16_t fcfi)
10779 {
10780         struct lpfc_vport **vports;
10781         struct lpfc_vport *vport = NULL;
10782         int i;
10783         uint32_t did = (fc_hdr->fh_d_id[0] << 16 |
10784                         fc_hdr->fh_d_id[1] << 8 |
10785                         fc_hdr->fh_d_id[2]);
10786
10787         vports = lpfc_create_vport_work_array(phba);
10788         if (vports != NULL)
10789                 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
10790                         if (phba->fcf.fcfi == fcfi &&
10791                             vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
10792                             vports[i]->fc_myDID == did) {
10793                                 vport = vports[i];
10794                                 break;
10795                         }
10796                 }
10797         lpfc_destroy_vport_work_array(phba, vports);
10798         return vport;
10799 }
10800
10801 /**
10802  * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
10803  * @vport: The vport to work on.
10804  *
10805  * This function updates the receive sequence time stamp for this vport. The
10806  * receive sequence time stamp indicates the time that the last frame of the
10807  * the sequence that has been idle for the longest amount of time was received.
10808  * the driver uses this time stamp to indicate if any received sequences have
10809  * timed out.
10810  **/
10811 void
10812 lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
10813 {
10814         struct lpfc_dmabuf *h_buf;
10815         struct hbq_dmabuf *dmabuf = NULL;
10816
10817         /* get the oldest sequence on the rcv list */
10818         h_buf = list_get_first(&vport->rcv_buffer_list,
10819                                struct lpfc_dmabuf, list);
10820         if (!h_buf)
10821                 return;
10822         dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
10823         vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
10824 }
10825
10826 /**
10827  * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
10828  * @vport: The vport that the received sequences were sent to.
10829  *
10830  * This function cleans up all outstanding received sequences. This is called
10831  * by the driver when a link event or user action invalidates all the received
10832  * sequences.
10833  **/
10834 void
10835 lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
10836 {
10837         struct lpfc_dmabuf *h_buf, *hnext;
10838         struct lpfc_dmabuf *d_buf, *dnext;
10839         struct hbq_dmabuf *dmabuf = NULL;
10840
10841         /* start with the oldest sequence on the rcv list */
10842         list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
10843                 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
10844                 list_del_init(&dmabuf->hbuf.list);
10845                 list_for_each_entry_safe(d_buf, dnext,
10846                                          &dmabuf->dbuf.list, list) {
10847                         list_del_init(&d_buf->list);
10848                         lpfc_in_buf_free(vport->phba, d_buf);
10849                 }
10850                 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
10851         }
10852 }
10853
10854 /**
10855  * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
10856  * @vport: The vport that the received sequences were sent to.
10857  *
10858  * This function determines whether any received sequences have timed out by
10859  * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
10860  * indicates that there is at least one timed out sequence this routine will
10861  * go through the received sequences one at a time from most inactive to most
10862  * active to determine which ones need to be cleaned up. Once it has determined
10863  * that a sequence needs to be cleaned up it will simply free up the resources
10864  * without sending an abort.
10865  **/
10866 void
10867 lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
10868 {
10869         struct lpfc_dmabuf *h_buf, *hnext;
10870         struct lpfc_dmabuf *d_buf, *dnext;
10871         struct hbq_dmabuf *dmabuf = NULL;
10872         unsigned long timeout;
10873         int abort_count = 0;
10874
10875         timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
10876                    vport->rcv_buffer_time_stamp);
10877         if (list_empty(&vport->rcv_buffer_list) ||
10878             time_before(jiffies, timeout))
10879                 return;
10880         /* start with the oldest sequence on the rcv list */
10881         list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
10882                 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
10883                 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
10884                            dmabuf->time_stamp);
10885                 if (time_before(jiffies, timeout))
10886                         break;
10887                 abort_count++;
10888                 list_del_init(&dmabuf->hbuf.list);
10889                 list_for_each_entry_safe(d_buf, dnext,
10890                                          &dmabuf->dbuf.list, list) {
10891                         list_del_init(&d_buf->list);
10892                         lpfc_in_buf_free(vport->phba, d_buf);
10893                 }
10894                 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
10895         }
10896         if (abort_count)
10897                 lpfc_update_rcv_time_stamp(vport);
10898 }
10899
10900 /**
10901  * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
10902  * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
10903  *
10904  * This function searches through the existing incomplete sequences that have
10905  * been sent to this @vport. If the frame matches one of the incomplete
10906  * sequences then the dbuf in the @dmabuf is added to the list of frames that
10907  * make up that sequence. If no sequence is found that matches this frame then
10908  * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
10909  * This function returns a pointer to the first dmabuf in the sequence list that
10910  * the frame was linked to.
10911  **/
10912 static struct hbq_dmabuf *
10913 lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
10914 {
10915         struct fc_frame_header *new_hdr;
10916         struct fc_frame_header *temp_hdr;
10917         struct lpfc_dmabuf *d_buf;
10918         struct lpfc_dmabuf *h_buf;
10919         struct hbq_dmabuf *seq_dmabuf = NULL;
10920         struct hbq_dmabuf *temp_dmabuf = NULL;
10921
10922         INIT_LIST_HEAD(&dmabuf->dbuf.list);
10923         dmabuf->time_stamp = jiffies;
10924         new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
10925         /* Use the hdr_buf to find the sequence that this frame belongs to */
10926         list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
10927                 temp_hdr = (struct fc_frame_header *)h_buf->virt;
10928                 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
10929                     (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
10930                     (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
10931                         continue;
10932                 /* found a pending sequence that matches this frame */
10933                 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
10934                 break;
10935         }
10936         if (!seq_dmabuf) {
10937                 /*
10938                  * This indicates first frame received for this sequence.
10939                  * Queue the buffer on the vport's rcv_buffer_list.
10940                  */
10941                 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
10942                 lpfc_update_rcv_time_stamp(vport);
10943                 return dmabuf;
10944         }
10945         temp_hdr = seq_dmabuf->hbuf.virt;
10946         if (be16_to_cpu(new_hdr->fh_seq_cnt) <
10947                 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
10948                 list_del_init(&seq_dmabuf->hbuf.list);
10949                 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
10950                 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
10951                 lpfc_update_rcv_time_stamp(vport);
10952                 return dmabuf;
10953         }
10954         /* move this sequence to the tail to indicate a young sequence */
10955         list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
10956         seq_dmabuf->time_stamp = jiffies;
10957         lpfc_update_rcv_time_stamp(vport);
10958         if (list_empty(&seq_dmabuf->dbuf.list)) {
10959                 temp_hdr = dmabuf->hbuf.virt;
10960                 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
10961                 return seq_dmabuf;
10962         }
10963         /* find the correct place in the sequence to insert this frame */
10964         list_for_each_entry_reverse(d_buf, &seq_dmabuf->dbuf.list, list) {
10965                 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
10966                 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
10967                 /*
10968                  * If the frame's sequence count is greater than the frame on
10969                  * the list then insert the frame right after this frame
10970                  */
10971                 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
10972                         be16_to_cpu(temp_hdr->fh_seq_cnt)) {
10973                         list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
10974                         return seq_dmabuf;
10975                 }
10976         }
10977         return NULL;
10978 }
10979
10980 /**
10981  * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
10982  * @vport: pointer to a vitural port
10983  * @dmabuf: pointer to a dmabuf that describes the FC sequence
10984  *
10985  * This function tries to abort from the partially assembed sequence, described
10986  * by the information from basic abbort @dmabuf. It checks to see whether such
10987  * partially assembled sequence held by the driver. If so, it shall free up all
10988  * the frames from the partially assembled sequence.
10989  *
10990  * Return
10991  * true  -- if there is matching partially assembled sequence present and all
10992  *          the frames freed with the sequence;
10993  * false -- if there is no matching partially assembled sequence present so
10994  *          nothing got aborted in the lower layer driver
10995  **/
10996 static bool
10997 lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
10998                             struct hbq_dmabuf *dmabuf)
10999 {
11000         struct fc_frame_header *new_hdr;
11001         struct fc_frame_header *temp_hdr;
11002         struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
11003         struct hbq_dmabuf *seq_dmabuf = NULL;
11004
11005         /* Use the hdr_buf to find the sequence that matches this frame */
11006         INIT_LIST_HEAD(&dmabuf->dbuf.list);
11007         INIT_LIST_HEAD(&dmabuf->hbuf.list);
11008         new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11009         list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
11010                 temp_hdr = (struct fc_frame_header *)h_buf->virt;
11011                 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
11012                     (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
11013                     (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
11014                         continue;
11015                 /* found a pending sequence that matches this frame */
11016                 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11017                 break;
11018         }
11019
11020         /* Free up all the frames from the partially assembled sequence */
11021         if (seq_dmabuf) {
11022                 list_for_each_entry_safe(d_buf, n_buf,
11023                                          &seq_dmabuf->dbuf.list, list) {
11024                         list_del_init(&d_buf->list);
11025                         lpfc_in_buf_free(vport->phba, d_buf);
11026                 }
11027                 return true;
11028         }
11029         return false;
11030 }
11031
11032 /**
11033  * lpfc_sli4_seq_abort_acc_cmpl - Accept seq abort iocb complete handler
11034  * @phba: Pointer to HBA context object.
11035  * @cmd_iocbq: pointer to the command iocbq structure.
11036  * @rsp_iocbq: pointer to the response iocbq structure.
11037  *
11038  * This function handles the sequence abort accept iocb command complete
11039  * event. It properly releases the memory allocated to the sequence abort
11040  * accept iocb.
11041  **/
11042 static void
11043 lpfc_sli4_seq_abort_acc_cmpl(struct lpfc_hba *phba,
11044                              struct lpfc_iocbq *cmd_iocbq,
11045                              struct lpfc_iocbq *rsp_iocbq)
11046 {
11047         if (cmd_iocbq)
11048                 lpfc_sli_release_iocbq(phba, cmd_iocbq);
11049 }
11050
11051 /**
11052  * lpfc_sli4_seq_abort_acc - Accept sequence abort
11053  * @phba: Pointer to HBA context object.
11054  * @fc_hdr: pointer to a FC frame header.
11055  *
11056  * This function sends a basic accept to a previous unsol sequence abort
11057  * event after aborting the sequence handling.
11058  **/
11059 static void
11060 lpfc_sli4_seq_abort_acc(struct lpfc_hba *phba,
11061                         struct fc_frame_header *fc_hdr)
11062 {
11063         struct lpfc_iocbq *ctiocb = NULL;
11064         struct lpfc_nodelist *ndlp;
11065         uint16_t oxid, rxid;
11066         uint32_t sid, fctl;
11067         IOCB_t *icmd;
11068
11069         if (!lpfc_is_link_up(phba))
11070                 return;
11071
11072         sid = sli4_sid_from_fc_hdr(fc_hdr);
11073         oxid = be16_to_cpu(fc_hdr->fh_ox_id);
11074         rxid = be16_to_cpu(fc_hdr->fh_rx_id);
11075
11076         ndlp = lpfc_findnode_did(phba->pport, sid);
11077         if (!ndlp) {
11078                 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
11079                                 "1268 Find ndlp returned NULL for oxid:x%x "
11080                                 "SID:x%x\n", oxid, sid);
11081                 return;
11082         }
11083
11084         /* Allocate buffer for acc iocb */
11085         ctiocb = lpfc_sli_get_iocbq(phba);
11086         if (!ctiocb)
11087                 return;
11088
11089         /* Extract the F_CTL field from FC_HDR */
11090         fctl = sli4_fctl_from_fc_hdr(fc_hdr);
11091
11092         icmd = &ctiocb->iocb;
11093         icmd->un.xseq64.bdl.bdeSize = 0;
11094         icmd->un.xseq64.bdl.ulpIoTag32 = 0;
11095         icmd->un.xseq64.w5.hcsw.Dfctl = 0;
11096         icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
11097         icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
11098
11099         /* Fill in the rest of iocb fields */
11100         icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
11101         icmd->ulpBdeCount = 0;
11102         icmd->ulpLe = 1;
11103         icmd->ulpClass = CLASS3;
11104         icmd->ulpContext = ndlp->nlp_rpi;
11105
11106         ctiocb->iocb_cmpl = NULL;
11107         ctiocb->vport = phba->pport;
11108         ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_acc_cmpl;
11109
11110         if (fctl & FC_FC_EX_CTX) {
11111                 /* ABTS sent by responder to CT exchange, construction
11112                  * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
11113                  * field and RX_ID from ABTS for RX_ID field.
11114                  */
11115                 bf_set(lpfc_abts_orig, &icmd->un.bls_acc, LPFC_ABTS_UNSOL_RSP);
11116                 bf_set(lpfc_abts_rxid, &icmd->un.bls_acc, rxid);
11117                 ctiocb->sli4_xritag = oxid;
11118         } else {
11119                 /* ABTS sent by initiator to CT exchange, construction
11120                  * of BA_ACC will need to allocate a new XRI as for the
11121                  * XRI_TAG and RX_ID fields.
11122                  */
11123                 bf_set(lpfc_abts_orig, &icmd->un.bls_acc, LPFC_ABTS_UNSOL_INT);
11124                 bf_set(lpfc_abts_rxid, &icmd->un.bls_acc, NO_XRI);
11125                 ctiocb->sli4_xritag = NO_XRI;
11126         }
11127         bf_set(lpfc_abts_oxid, &icmd->un.bls_acc, oxid);
11128
11129         /* Xmit CT abts accept on exchange <xid> */
11130         lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
11131                         "1200 Xmit CT ABTS ACC on exchange x%x Data: x%x\n",
11132                         CMD_XMIT_BLS_RSP64_CX, phba->link_state);
11133         lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
11134 }
11135
11136 /**
11137  * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
11138  * @vport: Pointer to the vport on which this sequence was received
11139  * @dmabuf: pointer to a dmabuf that describes the FC sequence
11140  *
11141  * This function handles an SLI-4 unsolicited abort event. If the unsolicited
11142  * receive sequence is only partially assembed by the driver, it shall abort
11143  * the partially assembled frames for the sequence. Otherwise, if the
11144  * unsolicited receive sequence has been completely assembled and passed to
11145  * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
11146  * unsolicited sequence has been aborted. After that, it will issue a basic
11147  * accept to accept the abort.
11148  **/
11149 void
11150 lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
11151                              struct hbq_dmabuf *dmabuf)
11152 {
11153         struct lpfc_hba *phba = vport->phba;
11154         struct fc_frame_header fc_hdr;
11155         uint32_t fctl;
11156         bool abts_par;
11157
11158         /* Make a copy of fc_hdr before the dmabuf being released */
11159         memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
11160         fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
11161
11162         if (fctl & FC_FC_EX_CTX) {
11163                 /*
11164                  * ABTS sent by responder to exchange, just free the buffer
11165                  */
11166                 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11167         } else {
11168                 /*
11169                  * ABTS sent by initiator to exchange, need to do cleanup
11170                  */
11171                 /* Try to abort partially assembled seq */
11172                 abts_par = lpfc_sli4_abort_partial_seq(vport, dmabuf);
11173
11174                 /* Send abort to ULP if partially seq abort failed */
11175                 if (abts_par == false)
11176                         lpfc_sli4_send_seq_to_ulp(vport, dmabuf);
11177                 else
11178                         lpfc_in_buf_free(phba, &dmabuf->dbuf);
11179         }
11180         /* Send basic accept (BA_ACC) to the abort requester */
11181         lpfc_sli4_seq_abort_acc(phba, &fc_hdr);
11182 }
11183
11184 /**
11185  * lpfc_seq_complete - Indicates if a sequence is complete
11186  * @dmabuf: pointer to a dmabuf that describes the FC sequence
11187  *
11188  * This function checks the sequence, starting with the frame described by
11189  * @dmabuf, to see if all the frames associated with this sequence are present.
11190  * the frames associated with this sequence are linked to the @dmabuf using the
11191  * dbuf list. This function looks for two major things. 1) That the first frame
11192  * has a sequence count of zero. 2) There is a frame with last frame of sequence
11193  * set. 3) That there are no holes in the sequence count. The function will
11194  * return 1 when the sequence is complete, otherwise it will return 0.
11195  **/
11196 static int
11197 lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
11198 {
11199         struct fc_frame_header *hdr;
11200         struct lpfc_dmabuf *d_buf;
11201         struct hbq_dmabuf *seq_dmabuf;
11202         uint32_t fctl;
11203         int seq_count = 0;
11204
11205         hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11206         /* make sure first fame of sequence has a sequence count of zero */
11207         if (hdr->fh_seq_cnt != seq_count)
11208                 return 0;
11209         fctl = (hdr->fh_f_ctl[0] << 16 |
11210                 hdr->fh_f_ctl[1] << 8 |
11211                 hdr->fh_f_ctl[2]);
11212         /* If last frame of sequence we can return success. */
11213         if (fctl & FC_FC_END_SEQ)
11214                 return 1;
11215         list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
11216                 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
11217                 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
11218                 /* If there is a hole in the sequence count then fail. */
11219                 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
11220                         return 0;
11221                 fctl = (hdr->fh_f_ctl[0] << 16 |
11222                         hdr->fh_f_ctl[1] << 8 |
11223                         hdr->fh_f_ctl[2]);
11224                 /* If last frame of sequence we can return success. */
11225                 if (fctl & FC_FC_END_SEQ)
11226                         return 1;
11227         }
11228         return 0;
11229 }
11230
11231 /**
11232  * lpfc_prep_seq - Prep sequence for ULP processing
11233  * @vport: Pointer to the vport on which this sequence was received
11234  * @dmabuf: pointer to a dmabuf that describes the FC sequence
11235  *
11236  * This function takes a sequence, described by a list of frames, and creates
11237  * a list of iocbq structures to describe the sequence. This iocbq list will be
11238  * used to issue to the generic unsolicited sequence handler. This routine
11239  * returns a pointer to the first iocbq in the list. If the function is unable
11240  * to allocate an iocbq then it throw out the received frames that were not
11241  * able to be described and return a pointer to the first iocbq. If unable to
11242  * allocate any iocbqs (including the first) this function will return NULL.
11243  **/
11244 static struct lpfc_iocbq *
11245 lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
11246 {
11247         struct lpfc_dmabuf *d_buf, *n_buf;
11248         struct lpfc_iocbq *first_iocbq, *iocbq;
11249         struct fc_frame_header *fc_hdr;
11250         uint32_t sid;
11251         struct ulp_bde64 *pbde;
11252
11253         fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
11254         /* remove from receive buffer list */
11255         list_del_init(&seq_dmabuf->hbuf.list);
11256         lpfc_update_rcv_time_stamp(vport);
11257         /* get the Remote Port's SID */
11258         sid = sli4_sid_from_fc_hdr(fc_hdr);
11259         /* Get an iocbq struct to fill in. */
11260         first_iocbq = lpfc_sli_get_iocbq(vport->phba);
11261         if (first_iocbq) {
11262                 /* Initialize the first IOCB. */
11263                 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
11264                 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
11265                 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
11266                 first_iocbq->iocb.ulpContext = be16_to_cpu(fc_hdr->fh_ox_id);
11267                 first_iocbq->iocb.unsli3.rcvsli3.vpi =
11268                                         vport->vpi + vport->phba->vpi_base;
11269                 /* put the first buffer into the first IOCBq */
11270                 first_iocbq->context2 = &seq_dmabuf->dbuf;
11271                 first_iocbq->context3 = NULL;
11272                 first_iocbq->iocb.ulpBdeCount = 1;
11273                 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
11274                                                         LPFC_DATA_BUF_SIZE;
11275                 first_iocbq->iocb.un.rcvels.remoteID = sid;
11276                 first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
11277                                 bf_get(lpfc_rcqe_length,
11278                                        &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
11279         }
11280         iocbq = first_iocbq;
11281         /*
11282          * Each IOCBq can have two Buffers assigned, so go through the list
11283          * of buffers for this sequence and save two buffers in each IOCBq
11284          */
11285         list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
11286                 if (!iocbq) {
11287                         lpfc_in_buf_free(vport->phba, d_buf);
11288                         continue;
11289                 }
11290                 if (!iocbq->context3) {
11291                         iocbq->context3 = d_buf;
11292                         iocbq->iocb.ulpBdeCount++;
11293                         pbde = (struct ulp_bde64 *)
11294                                         &iocbq->iocb.unsli3.sli3Words[4];
11295                         pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
11296                         first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
11297                                 bf_get(lpfc_rcqe_length,
11298                                        &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
11299                 } else {
11300                         iocbq = lpfc_sli_get_iocbq(vport->phba);
11301                         if (!iocbq) {
11302                                 if (first_iocbq) {
11303                                         first_iocbq->iocb.ulpStatus =
11304                                                         IOSTAT_FCP_RSP_ERROR;
11305                                         first_iocbq->iocb.un.ulpWord[4] =
11306                                                         IOERR_NO_RESOURCES;
11307                                 }
11308                                 lpfc_in_buf_free(vport->phba, d_buf);
11309                                 continue;
11310                         }
11311                         iocbq->context2 = d_buf;
11312                         iocbq->context3 = NULL;
11313                         iocbq->iocb.ulpBdeCount = 1;
11314                         iocbq->iocb.un.cont64[0].tus.f.bdeSize =
11315                                                         LPFC_DATA_BUF_SIZE;
11316                         first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
11317                                 bf_get(lpfc_rcqe_length,
11318                                        &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
11319                         iocbq->iocb.un.rcvels.remoteID = sid;
11320                         list_add_tail(&iocbq->list, &first_iocbq->list);
11321                 }
11322         }
11323         return first_iocbq;
11324 }
11325
11326 static void
11327 lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
11328                           struct hbq_dmabuf *seq_dmabuf)
11329 {
11330         struct fc_frame_header *fc_hdr;
11331         struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
11332         struct lpfc_hba *phba = vport->phba;
11333
11334         fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
11335         iocbq = lpfc_prep_seq(vport, seq_dmabuf);
11336         if (!iocbq) {
11337                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11338                                 "2707 Ring %d handler: Failed to allocate "
11339                                 "iocb Rctl x%x Type x%x received\n",
11340                                 LPFC_ELS_RING,
11341                                 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
11342                 return;
11343         }
11344         if (!lpfc_complete_unsol_iocb(phba,
11345                                       &phba->sli.ring[LPFC_ELS_RING],
11346                                       iocbq, fc_hdr->fh_r_ctl,
11347                                       fc_hdr->fh_type))
11348                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11349                                 "2540 Ring %d handler: unexpected Rctl "
11350                                 "x%x Type x%x received\n",
11351                                 LPFC_ELS_RING,
11352                                 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
11353
11354         /* Free iocb created in lpfc_prep_seq */
11355         list_for_each_entry_safe(curr_iocb, next_iocb,
11356                 &iocbq->list, list) {
11357                 list_del_init(&curr_iocb->list);
11358                 lpfc_sli_release_iocbq(phba, curr_iocb);
11359         }
11360         lpfc_sli_release_iocbq(phba, iocbq);
11361 }
11362
11363 /**
11364  * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
11365  * @phba: Pointer to HBA context object.
11366  *
11367  * This function is called with no lock held. This function processes all
11368  * the received buffers and gives it to upper layers when a received buffer
11369  * indicates that it is the final frame in the sequence. The interrupt
11370  * service routine processes received buffers at interrupt contexts and adds
11371  * received dma buffers to the rb_pend_list queue and signals the worker thread.
11372  * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
11373  * appropriate receive function when the final frame in a sequence is received.
11374  **/
11375 void
11376 lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
11377                                  struct hbq_dmabuf *dmabuf)
11378 {
11379         struct hbq_dmabuf *seq_dmabuf;
11380         struct fc_frame_header *fc_hdr;
11381         struct lpfc_vport *vport;
11382         uint32_t fcfi;
11383
11384         /* Process each received buffer */
11385         fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11386         /* check to see if this a valid type of frame */
11387         if (lpfc_fc_frame_check(phba, fc_hdr)) {
11388                 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11389                 return;
11390         }
11391         fcfi = bf_get(lpfc_rcqe_fcf_id, &dmabuf->cq_event.cqe.rcqe_cmpl);
11392         vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi);
11393         if (!vport || !(vport->vpi_state & LPFC_VPI_REGISTERED)) {
11394                 /* throw out the frame */
11395                 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11396                 return;
11397         }
11398         /* Handle the basic abort sequence (BA_ABTS) event */
11399         if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
11400                 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
11401                 return;
11402         }
11403
11404         /* Link this frame */
11405         seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
11406         if (!seq_dmabuf) {
11407                 /* unable to add frame to vport - throw it out */
11408                 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11409                 return;
11410         }
11411         /* If not last frame in sequence continue processing frames. */
11412         if (!lpfc_seq_complete(seq_dmabuf)) {
11413                 /*
11414                  * When saving off frames post a new one and mark this
11415                  * frame to be freed when it is finished.
11416                  **/
11417                 lpfc_sli_hbqbuf_fill_hbqs(phba, LPFC_ELS_HBQ, 1);
11418                 return;
11419         }
11420         /* Send the complete sequence to the upper layer protocol */
11421         lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
11422 }
11423
11424 /**
11425  * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
11426  * @phba: pointer to lpfc hba data structure.
11427  *
11428  * This routine is invoked to post rpi header templates to the
11429  * HBA consistent with the SLI-4 interface spec.  This routine
11430  * posts a PAGE_SIZE memory region to the port to hold up to
11431  * PAGE_SIZE modulo 64 rpi context headers.
11432  *
11433  * This routine does not require any locks.  It's usage is expected
11434  * to be driver load or reset recovery when the driver is
11435  * sequential.
11436  *
11437  * Return codes
11438  *      0 - successful
11439  *      EIO - The mailbox failed to complete successfully.
11440  *      When this error occurs, the driver is not guaranteed
11441  *      to have any rpi regions posted to the device and
11442  *      must either attempt to repost the regions or take a
11443  *      fatal error.
11444  **/
11445 int
11446 lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
11447 {
11448         struct lpfc_rpi_hdr *rpi_page;
11449         uint32_t rc = 0;
11450
11451         /* Post all rpi memory regions to the port. */
11452         list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
11453                 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
11454                 if (rc != MBX_SUCCESS) {
11455                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11456                                         "2008 Error %d posting all rpi "
11457                                         "headers\n", rc);
11458                         rc = -EIO;
11459                         break;
11460                 }
11461         }
11462
11463         return rc;
11464 }
11465
11466 /**
11467  * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
11468  * @phba: pointer to lpfc hba data structure.
11469  * @rpi_page:  pointer to the rpi memory region.
11470  *
11471  * This routine is invoked to post a single rpi header to the
11472  * HBA consistent with the SLI-4 interface spec.  This memory region
11473  * maps up to 64 rpi context regions.
11474  *
11475  * Return codes
11476  *      0 - successful
11477  *      ENOMEM - No available memory
11478  *      EIO - The mailbox failed to complete successfully.
11479  **/
11480 int
11481 lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
11482 {
11483         LPFC_MBOXQ_t *mboxq;
11484         struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
11485         uint32_t rc = 0;
11486         uint32_t mbox_tmo;
11487         uint32_t shdr_status, shdr_add_status;
11488         union lpfc_sli4_cfg_shdr *shdr;
11489
11490         /* The port is notified of the header region via a mailbox command. */
11491         mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11492         if (!mboxq) {
11493                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11494                                 "2001 Unable to allocate memory for issuing "
11495                                 "SLI_CONFIG_SPECIAL mailbox command\n");
11496                 return -ENOMEM;
11497         }
11498
11499         /* Post all rpi memory regions to the port. */
11500         hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
11501         mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
11502         lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
11503                          LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
11504                          sizeof(struct lpfc_mbx_post_hdr_tmpl) -
11505                          sizeof(struct mbox_header), LPFC_SLI4_MBX_EMBED);
11506         bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
11507                hdr_tmpl, rpi_page->page_count);
11508         bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
11509                rpi_page->start_rpi);
11510         hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
11511         hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
11512         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
11513         shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
11514         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11515         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11516         if (rc != MBX_TIMEOUT)
11517                 mempool_free(mboxq, phba->mbox_mem_pool);
11518         if (shdr_status || shdr_add_status || rc) {
11519                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11520                                 "2514 POST_RPI_HDR mailbox failed with "
11521                                 "status x%x add_status x%x, mbx status x%x\n",
11522                                 shdr_status, shdr_add_status, rc);
11523                 rc = -ENXIO;
11524         }
11525         return rc;
11526 }
11527
11528 /**
11529  * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
11530  * @phba: pointer to lpfc hba data structure.
11531  *
11532  * This routine is invoked to post rpi header templates to the
11533  * HBA consistent with the SLI-4 interface spec.  This routine
11534  * posts a PAGE_SIZE memory region to the port to hold up to
11535  * PAGE_SIZE modulo 64 rpi context headers.
11536  *
11537  * Returns
11538  *      A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
11539  *      LPFC_RPI_ALLOC_ERROR if no rpis are available.
11540  **/
11541 int
11542 lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
11543 {
11544         int rpi;
11545         uint16_t max_rpi, rpi_base, rpi_limit;
11546         uint16_t rpi_remaining;
11547         struct lpfc_rpi_hdr *rpi_hdr;
11548
11549         max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
11550         rpi_base = phba->sli4_hba.max_cfg_param.rpi_base;
11551         rpi_limit = phba->sli4_hba.next_rpi;
11552
11553         /*
11554          * The valid rpi range is not guaranteed to be zero-based.  Start
11555          * the search at the rpi_base as reported by the port.
11556          */
11557         spin_lock_irq(&phba->hbalock);
11558         rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, rpi_base);
11559         if (rpi >= rpi_limit || rpi < rpi_base)
11560                 rpi = LPFC_RPI_ALLOC_ERROR;
11561         else {
11562                 set_bit(rpi, phba->sli4_hba.rpi_bmask);
11563                 phba->sli4_hba.max_cfg_param.rpi_used++;
11564                 phba->sli4_hba.rpi_count++;
11565         }
11566
11567         /*
11568          * Don't try to allocate more rpi header regions if the device limit
11569          * on available rpis max has been exhausted.
11570          */
11571         if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
11572             (phba->sli4_hba.rpi_count >= max_rpi)) {
11573                 spin_unlock_irq(&phba->hbalock);
11574                 return rpi;
11575         }
11576
11577         /*
11578          * If the driver is running low on rpi resources, allocate another
11579          * page now.  Note that the next_rpi value is used because
11580          * it represents how many are actually in use whereas max_rpi notes
11581          * how many are supported max by the device.
11582          */
11583         rpi_remaining = phba->sli4_hba.next_rpi - rpi_base -
11584                 phba->sli4_hba.rpi_count;
11585         spin_unlock_irq(&phba->hbalock);
11586         if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
11587                 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
11588                 if (!rpi_hdr) {
11589                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11590                                         "2002 Error Could not grow rpi "
11591                                         "count\n");
11592                 } else {
11593                         lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
11594                 }
11595         }
11596
11597         return rpi;
11598 }
11599
11600 /**
11601  * lpfc_sli4_free_rpi - Release an rpi for reuse.
11602  * @phba: pointer to lpfc hba data structure.
11603  *
11604  * This routine is invoked to release an rpi to the pool of
11605  * available rpis maintained by the driver.
11606  **/
11607 void
11608 lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
11609 {
11610         spin_lock_irq(&phba->hbalock);
11611         clear_bit(rpi, phba->sli4_hba.rpi_bmask);
11612         phba->sli4_hba.rpi_count--;
11613         phba->sli4_hba.max_cfg_param.rpi_used--;
11614         spin_unlock_irq(&phba->hbalock);
11615 }
11616
11617 /**
11618  * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
11619  * @phba: pointer to lpfc hba data structure.
11620  *
11621  * This routine is invoked to remove the memory region that
11622  * provided rpi via a bitmask.
11623  **/
11624 void
11625 lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
11626 {
11627         kfree(phba->sli4_hba.rpi_bmask);
11628 }
11629
11630 /**
11631  * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
11632  * @phba: pointer to lpfc hba data structure.
11633  *
11634  * This routine is invoked to remove the memory region that
11635  * provided rpi via a bitmask.
11636  **/
11637 int
11638 lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp)
11639 {
11640         LPFC_MBOXQ_t *mboxq;
11641         struct lpfc_hba *phba = ndlp->phba;
11642         int rc;
11643
11644         /* The port is notified of the header region via a mailbox command. */
11645         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11646         if (!mboxq)
11647                 return -ENOMEM;
11648
11649         /* Post all rpi memory regions to the port. */
11650         lpfc_resume_rpi(mboxq, ndlp);
11651         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
11652         if (rc == MBX_NOT_FINISHED) {
11653                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11654                                 "2010 Resume RPI Mailbox failed "
11655                                 "status %d, mbxStatus x%x\n", rc,
11656                                 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
11657                 mempool_free(mboxq, phba->mbox_mem_pool);
11658                 return -EIO;
11659         }
11660         return 0;
11661 }
11662
11663 /**
11664  * lpfc_sli4_init_vpi - Initialize a vpi with the port
11665  * @phba: pointer to lpfc hba data structure.
11666  * @vpi: vpi value to activate with the port.
11667  *
11668  * This routine is invoked to activate a vpi with the
11669  * port when the host intends to use vports with a
11670  * nonzero vpi.
11671  *
11672  * Returns:
11673  *    0 success
11674  *    -Evalue otherwise
11675  **/
11676 int
11677 lpfc_sli4_init_vpi(struct lpfc_hba *phba, uint16_t vpi)
11678 {
11679         LPFC_MBOXQ_t *mboxq;
11680         int rc = 0;
11681         int retval = MBX_SUCCESS;
11682         uint32_t mbox_tmo;
11683
11684         if (vpi == 0)
11685                 return -EINVAL;
11686         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11687         if (!mboxq)
11688                 return -ENOMEM;
11689         lpfc_init_vpi(phba, mboxq, vpi);
11690         mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_INIT_VPI);
11691         rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
11692         if (rc != MBX_SUCCESS) {
11693                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11694                                 "2022 INIT VPI Mailbox failed "
11695                                 "status %d, mbxStatus x%x\n", rc,
11696                                 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
11697                 retval = -EIO;
11698         }
11699         if (rc != MBX_TIMEOUT)
11700                 mempool_free(mboxq, phba->mbox_mem_pool);
11701
11702         return retval;
11703 }
11704
11705 /**
11706  * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
11707  * @phba: pointer to lpfc hba data structure.
11708  * @mboxq: Pointer to mailbox object.
11709  *
11710  * This routine is invoked to manually add a single FCF record. The caller
11711  * must pass a completely initialized FCF_Record.  This routine takes
11712  * care of the nonembedded mailbox operations.
11713  **/
11714 static void
11715 lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
11716 {
11717         void *virt_addr;
11718         union lpfc_sli4_cfg_shdr *shdr;
11719         uint32_t shdr_status, shdr_add_status;
11720
11721         virt_addr = mboxq->sge_array->addr[0];
11722         /* The IOCTL status is embedded in the mailbox subheader. */
11723         shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
11724         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11725         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11726
11727         if ((shdr_status || shdr_add_status) &&
11728                 (shdr_status != STATUS_FCF_IN_USE))
11729                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11730                         "2558 ADD_FCF_RECORD mailbox failed with "
11731                         "status x%x add_status x%x\n",
11732                         shdr_status, shdr_add_status);
11733
11734         lpfc_sli4_mbox_cmd_free(phba, mboxq);
11735 }
11736
11737 /**
11738  * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
11739  * @phba: pointer to lpfc hba data structure.
11740  * @fcf_record:  pointer to the initialized fcf record to add.
11741  *
11742  * This routine is invoked to manually add a single FCF record. The caller
11743  * must pass a completely initialized FCF_Record.  This routine takes
11744  * care of the nonembedded mailbox operations.
11745  **/
11746 int
11747 lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
11748 {
11749         int rc = 0;
11750         LPFC_MBOXQ_t *mboxq;
11751         uint8_t *bytep;
11752         void *virt_addr;
11753         dma_addr_t phys_addr;
11754         struct lpfc_mbx_sge sge;
11755         uint32_t alloc_len, req_len;
11756         uint32_t fcfindex;
11757
11758         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11759         if (!mboxq) {
11760                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11761                         "2009 Failed to allocate mbox for ADD_FCF cmd\n");
11762                 return -ENOMEM;
11763         }
11764
11765         req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
11766                   sizeof(uint32_t);
11767
11768         /* Allocate DMA memory and set up the non-embedded mailbox command */
11769         alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
11770                                      LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
11771                                      req_len, LPFC_SLI4_MBX_NEMBED);
11772         if (alloc_len < req_len) {
11773                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11774                         "2523 Allocated DMA memory size (x%x) is "
11775                         "less than the requested DMA memory "
11776                         "size (x%x)\n", alloc_len, req_len);
11777                 lpfc_sli4_mbox_cmd_free(phba, mboxq);
11778                 return -ENOMEM;
11779         }
11780
11781         /*
11782          * Get the first SGE entry from the non-embedded DMA memory.  This
11783          * routine only uses a single SGE.
11784          */
11785         lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
11786         phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
11787         virt_addr = mboxq->sge_array->addr[0];
11788         /*
11789          * Configure the FCF record for FCFI 0.  This is the driver's
11790          * hardcoded default and gets used in nonFIP mode.
11791          */
11792         fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
11793         bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
11794         lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
11795
11796         /*
11797          * Copy the fcf_index and the FCF Record Data. The data starts after
11798          * the FCoE header plus word10. The data copy needs to be endian
11799          * correct.
11800          */
11801         bytep += sizeof(uint32_t);
11802         lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
11803         mboxq->vport = phba->pport;
11804         mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
11805         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
11806         if (rc == MBX_NOT_FINISHED) {
11807                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11808                         "2515 ADD_FCF_RECORD mailbox failed with "
11809                         "status 0x%x\n", rc);
11810                 lpfc_sli4_mbox_cmd_free(phba, mboxq);
11811                 rc = -EIO;
11812         } else
11813                 rc = 0;
11814
11815         return rc;
11816 }
11817
11818 /**
11819  * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
11820  * @phba: pointer to lpfc hba data structure.
11821  * @fcf_record:  pointer to the fcf record to write the default data.
11822  * @fcf_index: FCF table entry index.
11823  *
11824  * This routine is invoked to build the driver's default FCF record.  The
11825  * values used are hardcoded.  This routine handles memory initialization.
11826  *
11827  **/
11828 void
11829 lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
11830                                 struct fcf_record *fcf_record,
11831                                 uint16_t fcf_index)
11832 {
11833         memset(fcf_record, 0, sizeof(struct fcf_record));
11834         fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
11835         fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
11836         fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
11837         bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
11838         bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
11839         bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
11840         bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
11841         bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
11842         bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
11843         bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
11844         bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
11845         bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
11846         bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
11847         bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
11848         bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
11849         bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
11850                 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
11851         /* Set the VLAN bit map */
11852         if (phba->valid_vlan) {
11853                 fcf_record->vlan_bitmap[phba->vlan_id / 8]
11854                         = 1 << (phba->vlan_id % 8);
11855         }
11856 }
11857
11858 /**
11859  * lpfc_sli4_read_fcf_record - Read the driver's default FCF Record.
11860  * @phba: pointer to lpfc hba data structure.
11861  * @fcf_index: FCF table entry offset.
11862  *
11863  * This routine is invoked to read up to @fcf_num of FCF record from the
11864  * device starting with the given @fcf_index.
11865  **/
11866 int
11867 lpfc_sli4_read_fcf_record(struct lpfc_hba *phba, uint16_t fcf_index)
11868 {
11869         int rc = 0, error;
11870         LPFC_MBOXQ_t *mboxq;
11871         void *virt_addr;
11872         dma_addr_t phys_addr;
11873         uint8_t *bytep;
11874         struct lpfc_mbx_sge sge;
11875         uint32_t alloc_len, req_len;
11876         struct lpfc_mbx_read_fcf_tbl *read_fcf;
11877
11878         phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
11879         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11880         if (!mboxq) {
11881                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11882                                 "2000 Failed to allocate mbox for "
11883                                 "READ_FCF cmd\n");
11884                 error = -ENOMEM;
11885                 goto fail_fcfscan;
11886         }
11887
11888         req_len = sizeof(struct fcf_record) +
11889                   sizeof(union lpfc_sli4_cfg_shdr) + 2 * sizeof(uint32_t);
11890
11891         /* Set up READ_FCF SLI4_CONFIG mailbox-ioctl command */
11892         alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
11893                          LPFC_MBOX_OPCODE_FCOE_READ_FCF_TABLE, req_len,
11894                          LPFC_SLI4_MBX_NEMBED);
11895
11896         if (alloc_len < req_len) {
11897                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11898                                 "0291 Allocated DMA memory size (x%x) is "
11899                                 "less than the requested DMA memory "
11900                                 "size (x%x)\n", alloc_len, req_len);
11901                 error = -ENOMEM;
11902                 goto fail_fcfscan;
11903         }
11904
11905         /* Get the first SGE entry from the non-embedded DMA memory. This
11906          * routine only uses a single SGE.
11907          */
11908         lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
11909         phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
11910         virt_addr = mboxq->sge_array->addr[0];
11911         read_fcf = (struct lpfc_mbx_read_fcf_tbl *)virt_addr;
11912
11913         /* Set up command fields */
11914         bf_set(lpfc_mbx_read_fcf_tbl_indx, &read_fcf->u.request, fcf_index);
11915         /* Perform necessary endian conversion */
11916         bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
11917         lpfc_sli_pcimem_bcopy(bytep, bytep, sizeof(uint32_t));
11918         mboxq->vport = phba->pport;
11919         mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_record;
11920         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
11921         if (rc == MBX_NOT_FINISHED) {
11922                 error = -EIO;
11923         } else {
11924                 spin_lock_irq(&phba->hbalock);
11925                 phba->hba_flag |= FCF_DISC_INPROGRESS;
11926                 spin_unlock_irq(&phba->hbalock);
11927                 error = 0;
11928         }
11929 fail_fcfscan:
11930         if (error) {
11931                 if (mboxq)
11932                         lpfc_sli4_mbox_cmd_free(phba, mboxq);
11933                 /* FCF scan failed, clear FCF_DISC_INPROGRESS flag */
11934                 spin_lock_irq(&phba->hbalock);
11935                 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
11936                 spin_unlock_irq(&phba->hbalock);
11937         }
11938         return error;
11939 }
11940
11941 /**
11942  * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
11943  * @phba: pointer to lpfc hba data structure.
11944  *
11945  * This function read region 23 and parse TLV for port status to
11946  * decide if the user disaled the port. If the TLV indicates the
11947  * port is disabled, the hba_flag is set accordingly.
11948  **/
11949 void
11950 lpfc_sli_read_link_ste(struct lpfc_hba *phba)
11951 {
11952         LPFC_MBOXQ_t *pmb = NULL;
11953         MAILBOX_t *mb;
11954         uint8_t *rgn23_data = NULL;
11955         uint32_t offset = 0, data_size, sub_tlv_len, tlv_offset;
11956         int rc;
11957
11958         pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11959         if (!pmb) {
11960                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11961                         "2600 lpfc_sli_read_serdes_param failed to"
11962                         " allocate mailbox memory\n");
11963                 goto out;
11964         }
11965         mb = &pmb->u.mb;
11966
11967         /* Get adapter Region 23 data */
11968         rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
11969         if (!rgn23_data)
11970                 goto out;
11971
11972         do {
11973                 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
11974                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
11975
11976                 if (rc != MBX_SUCCESS) {
11977                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
11978                                 "2601 lpfc_sli_read_link_ste failed to"
11979                                 " read config region 23 rc 0x%x Status 0x%x\n",
11980                                 rc, mb->mbxStatus);
11981                         mb->un.varDmp.word_cnt = 0;
11982                 }
11983                 /*
11984                  * dump mem may return a zero when finished or we got a
11985                  * mailbox error, either way we are done.
11986                  */
11987                 if (mb->un.varDmp.word_cnt == 0)
11988                         break;
11989                 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
11990                         mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
11991
11992                 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
11993                         rgn23_data + offset,
11994                         mb->un.varDmp.word_cnt);
11995                 offset += mb->un.varDmp.word_cnt;
11996         } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
11997
11998         data_size = offset;
11999         offset = 0;
12000
12001         if (!data_size)
12002                 goto out;
12003
12004         /* Check the region signature first */
12005         if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
12006                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12007                         "2619 Config region 23 has bad signature\n");
12008                         goto out;
12009         }
12010         offset += 4;
12011
12012         /* Check the data structure version */
12013         if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
12014                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12015                         "2620 Config region 23 has bad version\n");
12016                 goto out;
12017         }
12018         offset += 4;
12019
12020         /* Parse TLV entries in the region */
12021         while (offset < data_size) {
12022                 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
12023                         break;
12024                 /*
12025                  * If the TLV is not driver specific TLV or driver id is
12026                  * not linux driver id, skip the record.
12027                  */
12028                 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
12029                     (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
12030                     (rgn23_data[offset + 3] != 0)) {
12031                         offset += rgn23_data[offset + 1] * 4 + 4;
12032                         continue;
12033                 }
12034
12035                 /* Driver found a driver specific TLV in the config region */
12036                 sub_tlv_len = rgn23_data[offset + 1] * 4;
12037                 offset += 4;
12038                 tlv_offset = 0;
12039
12040                 /*
12041                  * Search for configured port state sub-TLV.
12042                  */
12043                 while ((offset < data_size) &&
12044                         (tlv_offset < sub_tlv_len)) {
12045                         if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
12046                                 offset += 4;
12047                                 tlv_offset += 4;
12048                                 break;
12049                         }
12050                         if (rgn23_data[offset] != PORT_STE_TYPE) {
12051                                 offset += rgn23_data[offset + 1] * 4 + 4;
12052                                 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
12053                                 continue;
12054                         }
12055
12056                         /* This HBA contains PORT_STE configured */
12057                         if (!rgn23_data[offset + 2])
12058                                 phba->hba_flag |= LINK_DISABLED;
12059
12060                         goto out;
12061                 }
12062         }
12063 out:
12064         if (pmb)
12065                 mempool_free(pmb, phba->mbox_mem_pool);
12066         kfree(rgn23_data);
12067         return;
12068 }