Merge branch 'drm-ttm-unmappable' into drm-core-next
[pandora-kernel.git] / drivers / scsi / lpfc / lpfc_sli.c
1 /*******************************************************************
2  * This file is part of the Emulex Linux Device Driver for         *
3  * Fibre Channel Host Bus Adapters.                                *
4  * Copyright (C) 2004-2009 Emulex.  All rights reserved.           *
5  * EMULEX and SLI are trademarks of Emulex.                        *
6  * www.emulex.com                                                  *
7  * Portions Copyright (C) 2004-2005 Christoph Hellwig              *
8  *                                                                 *
9  * This program is free software; you can redistribute it and/or   *
10  * modify it under the terms of version 2 of the GNU General       *
11  * Public License as published by the Free Software Foundation.    *
12  * This program is distributed in the hope that it will be useful. *
13  * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND          *
14  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,  *
15  * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE      *
16  * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
17  * TO BE LEGALLY INVALID.  See the GNU General Public License for  *
18  * more details, a copy of which can be found in the file COPYING  *
19  * included with this package.                                     *
20  *******************************************************************/
21
22 #include <linux/blkdev.h>
23 #include <linux/pci.h>
24 #include <linux/interrupt.h>
25 #include <linux/delay.h>
26 #include <linux/slab.h>
27
28 #include <scsi/scsi.h>
29 #include <scsi/scsi_cmnd.h>
30 #include <scsi/scsi_device.h>
31 #include <scsi/scsi_host.h>
32 #include <scsi/scsi_transport_fc.h>
33 #include <scsi/fc/fc_fs.h>
34 #include <linux/aer.h>
35
36 #include "lpfc_hw4.h"
37 #include "lpfc_hw.h"
38 #include "lpfc_sli.h"
39 #include "lpfc_sli4.h"
40 #include "lpfc_nl.h"
41 #include "lpfc_disc.h"
42 #include "lpfc_scsi.h"
43 #include "lpfc.h"
44 #include "lpfc_crtn.h"
45 #include "lpfc_logmsg.h"
46 #include "lpfc_compat.h"
47 #include "lpfc_debugfs.h"
48 #include "lpfc_vport.h"
49
50 /* There are only four IOCB completion types. */
51 typedef enum _lpfc_iocb_type {
52         LPFC_UNKNOWN_IOCB,
53         LPFC_UNSOL_IOCB,
54         LPFC_SOL_IOCB,
55         LPFC_ABORT_IOCB
56 } lpfc_iocb_type;
57
58
59 /* Provide function prototypes local to this module. */
60 static int lpfc_sli_issue_mbox_s4(struct lpfc_hba *, LPFC_MBOXQ_t *,
61                                   uint32_t);
62 static int lpfc_sli4_read_rev(struct lpfc_hba *, LPFC_MBOXQ_t *,
63                               uint8_t *, uint32_t *);
64 static struct lpfc_iocbq *lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *,
65                                                          struct lpfc_iocbq *);
66 static void lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *,
67                                       struct hbq_dmabuf *);
68 static IOCB_t *
69 lpfc_get_iocb_from_iocbq(struct lpfc_iocbq *iocbq)
70 {
71         return &iocbq->iocb;
72 }
73
74 /**
75  * lpfc_sli4_wq_put - Put a Work Queue Entry on an Work Queue
76  * @q: The Work Queue to operate on.
77  * @wqe: The work Queue Entry to put on the Work queue.
78  *
79  * This routine will copy the contents of @wqe to the next available entry on
80  * the @q. This function will then ring the Work Queue Doorbell to signal the
81  * HBA to start processing the Work Queue Entry. This function returns 0 if
82  * successful. If no entries are available on @q then this function will return
83  * -ENOMEM.
84  * The caller is expected to hold the hbalock when calling this routine.
85  **/
86 static uint32_t
87 lpfc_sli4_wq_put(struct lpfc_queue *q, union lpfc_wqe *wqe)
88 {
89         union lpfc_wqe *temp_wqe = q->qe[q->host_index].wqe;
90         struct lpfc_register doorbell;
91         uint32_t host_index;
92
93         /* If the host has not yet processed the next entry then we are done */
94         if (((q->host_index + 1) % q->entry_count) == q->hba_index)
95                 return -ENOMEM;
96         /* set consumption flag every once in a while */
97         if (!((q->host_index + 1) % LPFC_RELEASE_NOTIFICATION_INTERVAL))
98                 bf_set(lpfc_wqe_gen_wqec, &wqe->generic, 1);
99
100         lpfc_sli_pcimem_bcopy(wqe, temp_wqe, q->entry_size);
101
102         /* Update the host index before invoking device */
103         host_index = q->host_index;
104         q->host_index = ((q->host_index + 1) % q->entry_count);
105
106         /* Ring Doorbell */
107         doorbell.word0 = 0;
108         bf_set(lpfc_wq_doorbell_num_posted, &doorbell, 1);
109         bf_set(lpfc_wq_doorbell_index, &doorbell, host_index);
110         bf_set(lpfc_wq_doorbell_id, &doorbell, q->queue_id);
111         writel(doorbell.word0, q->phba->sli4_hba.WQDBregaddr);
112         readl(q->phba->sli4_hba.WQDBregaddr); /* Flush */
113
114         return 0;
115 }
116
117 /**
118  * lpfc_sli4_wq_release - Updates internal hba index for WQ
119  * @q: The Work Queue to operate on.
120  * @index: The index to advance the hba index to.
121  *
122  * This routine will update the HBA index of a queue to reflect consumption of
123  * Work Queue Entries by the HBA. When the HBA indicates that it has consumed
124  * an entry the host calls this function to update the queue's internal
125  * pointers. This routine returns the number of entries that were consumed by
126  * the HBA.
127  **/
128 static uint32_t
129 lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)
130 {
131         uint32_t released = 0;
132
133         if (q->hba_index == index)
134                 return 0;
135         do {
136                 q->hba_index = ((q->hba_index + 1) % q->entry_count);
137                 released++;
138         } while (q->hba_index != index);
139         return released;
140 }
141
142 /**
143  * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue
144  * @q: The Mailbox Queue to operate on.
145  * @wqe: The Mailbox Queue Entry to put on the Work queue.
146  *
147  * This routine will copy the contents of @mqe to the next available entry on
148  * the @q. This function will then ring the Work Queue Doorbell to signal the
149  * HBA to start processing the Work Queue Entry. This function returns 0 if
150  * successful. If no entries are available on @q then this function will return
151  * -ENOMEM.
152  * The caller is expected to hold the hbalock when calling this routine.
153  **/
154 static uint32_t
155 lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)
156 {
157         struct lpfc_mqe *temp_mqe = q->qe[q->host_index].mqe;
158         struct lpfc_register doorbell;
159         uint32_t host_index;
160
161         /* If the host has not yet processed the next entry then we are done */
162         if (((q->host_index + 1) % q->entry_count) == q->hba_index)
163                 return -ENOMEM;
164         lpfc_sli_pcimem_bcopy(mqe, temp_mqe, q->entry_size);
165         /* Save off the mailbox pointer for completion */
166         q->phba->mbox = (MAILBOX_t *)temp_mqe;
167
168         /* Update the host index before invoking device */
169         host_index = q->host_index;
170         q->host_index = ((q->host_index + 1) % q->entry_count);
171
172         /* Ring Doorbell */
173         doorbell.word0 = 0;
174         bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);
175         bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);
176         writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);
177         readl(q->phba->sli4_hba.MQDBregaddr); /* Flush */
178         return 0;
179 }
180
181 /**
182  * lpfc_sli4_mq_release - Updates internal hba index for MQ
183  * @q: The Mailbox Queue to operate on.
184  *
185  * This routine will update the HBA index of a queue to reflect consumption of
186  * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed
187  * an entry the host calls this function to update the queue's internal
188  * pointers. This routine returns the number of entries that were consumed by
189  * the HBA.
190  **/
191 static uint32_t
192 lpfc_sli4_mq_release(struct lpfc_queue *q)
193 {
194         /* Clear the mailbox pointer for completion */
195         q->phba->mbox = NULL;
196         q->hba_index = ((q->hba_index + 1) % q->entry_count);
197         return 1;
198 }
199
200 /**
201  * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ
202  * @q: The Event Queue to get the first valid EQE from
203  *
204  * This routine will get the first valid Event Queue Entry from @q, update
205  * the queue's internal hba index, and return the EQE. If no valid EQEs are in
206  * the Queue (no more work to do), or the Queue is full of EQEs that have been
207  * processed, but not popped back to the HBA then this routine will return NULL.
208  **/
209 static struct lpfc_eqe *
210 lpfc_sli4_eq_get(struct lpfc_queue *q)
211 {
212         struct lpfc_eqe *eqe = q->qe[q->hba_index].eqe;
213
214         /* If the next EQE is not valid then we are done */
215         if (!bf_get(lpfc_eqe_valid, eqe))
216                 return NULL;
217         /* If the host has not yet processed the next entry then we are done */
218         if (((q->hba_index + 1) % q->entry_count) == q->host_index)
219                 return NULL;
220
221         q->hba_index = ((q->hba_index + 1) % q->entry_count);
222         return eqe;
223 }
224
225 /**
226  * lpfc_sli4_eq_release - Indicates the host has finished processing an EQ
227  * @q: The Event Queue that the host has completed processing for.
228  * @arm: Indicates whether the host wants to arms this CQ.
229  *
230  * This routine will mark all Event Queue Entries on @q, from the last
231  * known completed entry to the last entry that was processed, as completed
232  * by clearing the valid bit for each completion queue entry. Then it will
233  * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
234  * The internal host index in the @q will be updated by this routine to indicate
235  * that the host has finished processing the entries. The @arm parameter
236  * indicates that the queue should be rearmed when ringing the doorbell.
237  *
238  * This function will return the number of EQEs that were popped.
239  **/
240 uint32_t
241 lpfc_sli4_eq_release(struct lpfc_queue *q, bool arm)
242 {
243         uint32_t released = 0;
244         struct lpfc_eqe *temp_eqe;
245         struct lpfc_register doorbell;
246
247         /* while there are valid entries */
248         while (q->hba_index != q->host_index) {
249                 temp_eqe = q->qe[q->host_index].eqe;
250                 bf_set(lpfc_eqe_valid, temp_eqe, 0);
251                 released++;
252                 q->host_index = ((q->host_index + 1) % q->entry_count);
253         }
254         if (unlikely(released == 0 && !arm))
255                 return 0;
256
257         /* ring doorbell for number popped */
258         doorbell.word0 = 0;
259         if (arm) {
260                 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
261                 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
262         }
263         bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
264         bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
265         bf_set(lpfc_eqcq_doorbell_eqid, &doorbell, q->queue_id);
266         writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
267         /* PCI read to flush PCI pipeline on re-arming for INTx mode */
268         if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
269                 readl(q->phba->sli4_hba.EQCQDBregaddr);
270         return released;
271 }
272
273 /**
274  * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
275  * @q: The Completion Queue to get the first valid CQE from
276  *
277  * This routine will get the first valid Completion Queue Entry from @q, update
278  * the queue's internal hba index, and return the CQE. If no valid CQEs are in
279  * the Queue (no more work to do), or the Queue is full of CQEs that have been
280  * processed, but not popped back to the HBA then this routine will return NULL.
281  **/
282 static struct lpfc_cqe *
283 lpfc_sli4_cq_get(struct lpfc_queue *q)
284 {
285         struct lpfc_cqe *cqe;
286
287         /* If the next CQE is not valid then we are done */
288         if (!bf_get(lpfc_cqe_valid, q->qe[q->hba_index].cqe))
289                 return NULL;
290         /* If the host has not yet processed the next entry then we are done */
291         if (((q->hba_index + 1) % q->entry_count) == q->host_index)
292                 return NULL;
293
294         cqe = q->qe[q->hba_index].cqe;
295         q->hba_index = ((q->hba_index + 1) % q->entry_count);
296         return cqe;
297 }
298
299 /**
300  * lpfc_sli4_cq_release - Indicates the host has finished processing a CQ
301  * @q: The Completion Queue that the host has completed processing for.
302  * @arm: Indicates whether the host wants to arms this CQ.
303  *
304  * This routine will mark all Completion queue entries on @q, from the last
305  * known completed entry to the last entry that was processed, as completed
306  * by clearing the valid bit for each completion queue entry. Then it will
307  * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
308  * The internal host index in the @q will be updated by this routine to indicate
309  * that the host has finished processing the entries. The @arm parameter
310  * indicates that the queue should be rearmed when ringing the doorbell.
311  *
312  * This function will return the number of CQEs that were released.
313  **/
314 uint32_t
315 lpfc_sli4_cq_release(struct lpfc_queue *q, bool arm)
316 {
317         uint32_t released = 0;
318         struct lpfc_cqe *temp_qe;
319         struct lpfc_register doorbell;
320
321         /* while there are valid entries */
322         while (q->hba_index != q->host_index) {
323                 temp_qe = q->qe[q->host_index].cqe;
324                 bf_set(lpfc_cqe_valid, temp_qe, 0);
325                 released++;
326                 q->host_index = ((q->host_index + 1) % q->entry_count);
327         }
328         if (unlikely(released == 0 && !arm))
329                 return 0;
330
331         /* ring doorbell for number popped */
332         doorbell.word0 = 0;
333         if (arm)
334                 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
335         bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
336         bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
337         bf_set(lpfc_eqcq_doorbell_cqid, &doorbell, q->queue_id);
338         writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
339         return released;
340 }
341
342 /**
343  * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
344  * @q: The Header Receive Queue to operate on.
345  * @wqe: The Receive Queue Entry to put on the Receive queue.
346  *
347  * This routine will copy the contents of @wqe to the next available entry on
348  * the @q. This function will then ring the Receive Queue Doorbell to signal the
349  * HBA to start processing the Receive Queue Entry. This function returns the
350  * index that the rqe was copied to if successful. If no entries are available
351  * on @q then this function will return -ENOMEM.
352  * The caller is expected to hold the hbalock when calling this routine.
353  **/
354 static int
355 lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
356                  struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
357 {
358         struct lpfc_rqe *temp_hrqe = hq->qe[hq->host_index].rqe;
359         struct lpfc_rqe *temp_drqe = dq->qe[dq->host_index].rqe;
360         struct lpfc_register doorbell;
361         int put_index = hq->host_index;
362
363         if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
364                 return -EINVAL;
365         if (hq->host_index != dq->host_index)
366                 return -EINVAL;
367         /* If the host has not yet processed the next entry then we are done */
368         if (((hq->host_index + 1) % hq->entry_count) == hq->hba_index)
369                 return -EBUSY;
370         lpfc_sli_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
371         lpfc_sli_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
372
373         /* Update the host index to point to the next slot */
374         hq->host_index = ((hq->host_index + 1) % hq->entry_count);
375         dq->host_index = ((dq->host_index + 1) % dq->entry_count);
376
377         /* Ring The Header Receive Queue Doorbell */
378         if (!(hq->host_index % LPFC_RQ_POST_BATCH)) {
379                 doorbell.word0 = 0;
380                 bf_set(lpfc_rq_doorbell_num_posted, &doorbell,
381                        LPFC_RQ_POST_BATCH);
382                 bf_set(lpfc_rq_doorbell_id, &doorbell, hq->queue_id);
383                 writel(doorbell.word0, hq->phba->sli4_hba.RQDBregaddr);
384         }
385         return put_index;
386 }
387
388 /**
389  * lpfc_sli4_rq_release - Updates internal hba index for RQ
390  * @q: The Header Receive Queue to operate on.
391  *
392  * This routine will update the HBA index of a queue to reflect consumption of
393  * one Receive Queue Entry by the HBA. When the HBA indicates that it has
394  * consumed an entry the host calls this function to update the queue's
395  * internal pointers. This routine returns the number of entries that were
396  * consumed by the HBA.
397  **/
398 static uint32_t
399 lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
400 {
401         if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
402                 return 0;
403         hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
404         dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
405         return 1;
406 }
407
408 /**
409  * lpfc_cmd_iocb - Get next command iocb entry in the ring
410  * @phba: Pointer to HBA context object.
411  * @pring: Pointer to driver SLI ring object.
412  *
413  * This function returns pointer to next command iocb entry
414  * in the command ring. The caller must hold hbalock to prevent
415  * other threads consume the next command iocb.
416  * SLI-2/SLI-3 provide different sized iocbs.
417  **/
418 static inline IOCB_t *
419 lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
420 {
421         return (IOCB_t *) (((char *) pring->cmdringaddr) +
422                            pring->cmdidx * phba->iocb_cmd_size);
423 }
424
425 /**
426  * lpfc_resp_iocb - Get next response iocb entry in the ring
427  * @phba: Pointer to HBA context object.
428  * @pring: Pointer to driver SLI ring object.
429  *
430  * This function returns pointer to next response iocb entry
431  * in the response ring. The caller must hold hbalock to make sure
432  * that no other thread consume the next response iocb.
433  * SLI-2/SLI-3 provide different sized iocbs.
434  **/
435 static inline IOCB_t *
436 lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
437 {
438         return (IOCB_t *) (((char *) pring->rspringaddr) +
439                            pring->rspidx * phba->iocb_rsp_size);
440 }
441
442 /**
443  * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
444  * @phba: Pointer to HBA context object.
445  *
446  * This function is called with hbalock held. This function
447  * allocates a new driver iocb object from the iocb pool. If the
448  * allocation is successful, it returns pointer to the newly
449  * allocated iocb object else it returns NULL.
450  **/
451 static struct lpfc_iocbq *
452 __lpfc_sli_get_iocbq(struct lpfc_hba *phba)
453 {
454         struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
455         struct lpfc_iocbq * iocbq = NULL;
456
457         list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
458         return iocbq;
459 }
460
461 /**
462  * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
463  * @phba: Pointer to HBA context object.
464  * @xritag: XRI value.
465  *
466  * This function clears the sglq pointer from the array of acive
467  * sglq's. The xritag that is passed in is used to index into the
468  * array. Before the xritag can be used it needs to be adjusted
469  * by subtracting the xribase.
470  *
471  * Returns sglq ponter = success, NULL = Failure.
472  **/
473 static struct lpfc_sglq *
474 __lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
475 {
476         uint16_t adj_xri;
477         struct lpfc_sglq *sglq;
478         adj_xri = xritag - phba->sli4_hba.max_cfg_param.xri_base;
479         if (adj_xri > phba->sli4_hba.max_cfg_param.max_xri)
480                 return NULL;
481         sglq = phba->sli4_hba.lpfc_sglq_active_list[adj_xri];
482         phba->sli4_hba.lpfc_sglq_active_list[adj_xri] = NULL;
483         return sglq;
484 }
485
486 /**
487  * __lpfc_get_active_sglq - Get the active sglq for this XRI.
488  * @phba: Pointer to HBA context object.
489  * @xritag: XRI value.
490  *
491  * This function returns the sglq pointer from the array of acive
492  * sglq's. The xritag that is passed in is used to index into the
493  * array. Before the xritag can be used it needs to be adjusted
494  * by subtracting the xribase.
495  *
496  * Returns sglq ponter = success, NULL = Failure.
497  **/
498 struct lpfc_sglq *
499 __lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
500 {
501         uint16_t adj_xri;
502         struct lpfc_sglq *sglq;
503         adj_xri = xritag - phba->sli4_hba.max_cfg_param.xri_base;
504         if (adj_xri > phba->sli4_hba.max_cfg_param.max_xri)
505                 return NULL;
506         sglq =  phba->sli4_hba.lpfc_sglq_active_list[adj_xri];
507         return sglq;
508 }
509
510 /**
511  * __lpfc_sli_get_sglq - Allocates an iocb object from sgl pool
512  * @phba: Pointer to HBA context object.
513  *
514  * This function is called with hbalock held. This function
515  * Gets a new driver sglq object from the sglq list. If the
516  * list is not empty then it is successful, it returns pointer to the newly
517  * allocated sglq object else it returns NULL.
518  **/
519 static struct lpfc_sglq *
520 __lpfc_sli_get_sglq(struct lpfc_hba *phba)
521 {
522         struct list_head *lpfc_sgl_list = &phba->sli4_hba.lpfc_sgl_list;
523         struct lpfc_sglq *sglq = NULL;
524         uint16_t adj_xri;
525         list_remove_head(lpfc_sgl_list, sglq, struct lpfc_sglq, list);
526         if (!sglq)
527                 return NULL;
528         adj_xri = sglq->sli4_xritag - phba->sli4_hba.max_cfg_param.xri_base;
529         phba->sli4_hba.lpfc_sglq_active_list[adj_xri] = sglq;
530         sglq->state = SGL_ALLOCATED;
531         return sglq;
532 }
533
534 /**
535  * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
536  * @phba: Pointer to HBA context object.
537  *
538  * This function is called with no lock held. This function
539  * allocates a new driver iocb object from the iocb pool. If the
540  * allocation is successful, it returns pointer to the newly
541  * allocated iocb object else it returns NULL.
542  **/
543 struct lpfc_iocbq *
544 lpfc_sli_get_iocbq(struct lpfc_hba *phba)
545 {
546         struct lpfc_iocbq * iocbq = NULL;
547         unsigned long iflags;
548
549         spin_lock_irqsave(&phba->hbalock, iflags);
550         iocbq = __lpfc_sli_get_iocbq(phba);
551         spin_unlock_irqrestore(&phba->hbalock, iflags);
552         return iocbq;
553 }
554
555 /**
556  * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
557  * @phba: Pointer to HBA context object.
558  * @iocbq: Pointer to driver iocb object.
559  *
560  * This function is called with hbalock held to release driver
561  * iocb object to the iocb pool. The iotag in the iocb object
562  * does not change for each use of the iocb object. This function
563  * clears all other fields of the iocb object when it is freed.
564  * The sqlq structure that holds the xritag and phys and virtual
565  * mappings for the scatter gather list is retrieved from the
566  * active array of sglq. The get of the sglq pointer also clears
567  * the entry in the array. If the status of the IO indiactes that
568  * this IO was aborted then the sglq entry it put on the
569  * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
570  * IO has good status or fails for any other reason then the sglq
571  * entry is added to the free list (lpfc_sgl_list).
572  **/
573 static void
574 __lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
575 {
576         struct lpfc_sglq *sglq;
577         size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
578         unsigned long iflag;
579
580         if (iocbq->sli4_xritag == NO_XRI)
581                 sglq = NULL;
582         else
583                 sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_xritag);
584         if (sglq)  {
585                 if ((iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) &&
586                         (sglq->state != SGL_XRI_ABORTED)) {
587                         spin_lock_irqsave(&phba->sli4_hba.abts_sgl_list_lock,
588                                         iflag);
589                         list_add(&sglq->list,
590                                 &phba->sli4_hba.lpfc_abts_els_sgl_list);
591                         spin_unlock_irqrestore(
592                                 &phba->sli4_hba.abts_sgl_list_lock, iflag);
593                 } else {
594                         sglq->state = SGL_FREED;
595                         list_add(&sglq->list, &phba->sli4_hba.lpfc_sgl_list);
596                 }
597         }
598
599
600         /*
601          * Clean all volatile data fields, preserve iotag and node struct.
602          */
603         memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
604         iocbq->sli4_xritag = NO_XRI;
605         list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
606 }
607
608 /**
609  * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
610  * @phba: Pointer to HBA context object.
611  * @iocbq: Pointer to driver iocb object.
612  *
613  * This function is called with hbalock held to release driver
614  * iocb object to the iocb pool. The iotag in the iocb object
615  * does not change for each use of the iocb object. This function
616  * clears all other fields of the iocb object when it is freed.
617  **/
618 static void
619 __lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
620 {
621         size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
622
623         /*
624          * Clean all volatile data fields, preserve iotag and node struct.
625          */
626         memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
627         iocbq->sli4_xritag = NO_XRI;
628         list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
629 }
630
631 /**
632  * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
633  * @phba: Pointer to HBA context object.
634  * @iocbq: Pointer to driver iocb object.
635  *
636  * This function is called with hbalock held to release driver
637  * iocb object to the iocb pool. The iotag in the iocb object
638  * does not change for each use of the iocb object. This function
639  * clears all other fields of the iocb object when it is freed.
640  **/
641 static void
642 __lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
643 {
644         phba->__lpfc_sli_release_iocbq(phba, iocbq);
645 }
646
647 /**
648  * lpfc_sli_release_iocbq - Release iocb to the iocb pool
649  * @phba: Pointer to HBA context object.
650  * @iocbq: Pointer to driver iocb object.
651  *
652  * This function is called with no lock held to release the iocb to
653  * iocb pool.
654  **/
655 void
656 lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
657 {
658         unsigned long iflags;
659
660         /*
661          * Clean all volatile data fields, preserve iotag and node struct.
662          */
663         spin_lock_irqsave(&phba->hbalock, iflags);
664         __lpfc_sli_release_iocbq(phba, iocbq);
665         spin_unlock_irqrestore(&phba->hbalock, iflags);
666 }
667
668 /**
669  * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
670  * @phba: Pointer to HBA context object.
671  * @iocblist: List of IOCBs.
672  * @ulpstatus: ULP status in IOCB command field.
673  * @ulpWord4: ULP word-4 in IOCB command field.
674  *
675  * This function is called with a list of IOCBs to cancel. It cancels the IOCB
676  * on the list by invoking the complete callback function associated with the
677  * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
678  * fields.
679  **/
680 void
681 lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
682                       uint32_t ulpstatus, uint32_t ulpWord4)
683 {
684         struct lpfc_iocbq *piocb;
685
686         while (!list_empty(iocblist)) {
687                 list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
688
689                 if (!piocb->iocb_cmpl)
690                         lpfc_sli_release_iocbq(phba, piocb);
691                 else {
692                         piocb->iocb.ulpStatus = ulpstatus;
693                         piocb->iocb.un.ulpWord[4] = ulpWord4;
694                         (piocb->iocb_cmpl) (phba, piocb, piocb);
695                 }
696         }
697         return;
698 }
699
700 /**
701  * lpfc_sli_iocb_cmd_type - Get the iocb type
702  * @iocb_cmnd: iocb command code.
703  *
704  * This function is called by ring event handler function to get the iocb type.
705  * This function translates the iocb command to an iocb command type used to
706  * decide the final disposition of each completed IOCB.
707  * The function returns
708  * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
709  * LPFC_SOL_IOCB     if it is a solicited iocb completion
710  * LPFC_ABORT_IOCB   if it is an abort iocb
711  * LPFC_UNSOL_IOCB   if it is an unsolicited iocb
712  *
713  * The caller is not required to hold any lock.
714  **/
715 static lpfc_iocb_type
716 lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
717 {
718         lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
719
720         if (iocb_cmnd > CMD_MAX_IOCB_CMD)
721                 return 0;
722
723         switch (iocb_cmnd) {
724         case CMD_XMIT_SEQUENCE_CR:
725         case CMD_XMIT_SEQUENCE_CX:
726         case CMD_XMIT_BCAST_CN:
727         case CMD_XMIT_BCAST_CX:
728         case CMD_ELS_REQUEST_CR:
729         case CMD_ELS_REQUEST_CX:
730         case CMD_CREATE_XRI_CR:
731         case CMD_CREATE_XRI_CX:
732         case CMD_GET_RPI_CN:
733         case CMD_XMIT_ELS_RSP_CX:
734         case CMD_GET_RPI_CR:
735         case CMD_FCP_IWRITE_CR:
736         case CMD_FCP_IWRITE_CX:
737         case CMD_FCP_IREAD_CR:
738         case CMD_FCP_IREAD_CX:
739         case CMD_FCP_ICMND_CR:
740         case CMD_FCP_ICMND_CX:
741         case CMD_FCP_TSEND_CX:
742         case CMD_FCP_TRSP_CX:
743         case CMD_FCP_TRECEIVE_CX:
744         case CMD_FCP_AUTO_TRSP_CX:
745         case CMD_ADAPTER_MSG:
746         case CMD_ADAPTER_DUMP:
747         case CMD_XMIT_SEQUENCE64_CR:
748         case CMD_XMIT_SEQUENCE64_CX:
749         case CMD_XMIT_BCAST64_CN:
750         case CMD_XMIT_BCAST64_CX:
751         case CMD_ELS_REQUEST64_CR:
752         case CMD_ELS_REQUEST64_CX:
753         case CMD_FCP_IWRITE64_CR:
754         case CMD_FCP_IWRITE64_CX:
755         case CMD_FCP_IREAD64_CR:
756         case CMD_FCP_IREAD64_CX:
757         case CMD_FCP_ICMND64_CR:
758         case CMD_FCP_ICMND64_CX:
759         case CMD_FCP_TSEND64_CX:
760         case CMD_FCP_TRSP64_CX:
761         case CMD_FCP_TRECEIVE64_CX:
762         case CMD_GEN_REQUEST64_CR:
763         case CMD_GEN_REQUEST64_CX:
764         case CMD_XMIT_ELS_RSP64_CX:
765         case DSSCMD_IWRITE64_CR:
766         case DSSCMD_IWRITE64_CX:
767         case DSSCMD_IREAD64_CR:
768         case DSSCMD_IREAD64_CX:
769                 type = LPFC_SOL_IOCB;
770                 break;
771         case CMD_ABORT_XRI_CN:
772         case CMD_ABORT_XRI_CX:
773         case CMD_CLOSE_XRI_CN:
774         case CMD_CLOSE_XRI_CX:
775         case CMD_XRI_ABORTED_CX:
776         case CMD_ABORT_MXRI64_CN:
777         case CMD_XMIT_BLS_RSP64_CX:
778                 type = LPFC_ABORT_IOCB;
779                 break;
780         case CMD_RCV_SEQUENCE_CX:
781         case CMD_RCV_ELS_REQ_CX:
782         case CMD_RCV_SEQUENCE64_CX:
783         case CMD_RCV_ELS_REQ64_CX:
784         case CMD_ASYNC_STATUS:
785         case CMD_IOCB_RCV_SEQ64_CX:
786         case CMD_IOCB_RCV_ELS64_CX:
787         case CMD_IOCB_RCV_CONT64_CX:
788         case CMD_IOCB_RET_XRI64_CX:
789                 type = LPFC_UNSOL_IOCB;
790                 break;
791         case CMD_IOCB_XMIT_MSEQ64_CR:
792         case CMD_IOCB_XMIT_MSEQ64_CX:
793         case CMD_IOCB_RCV_SEQ_LIST64_CX:
794         case CMD_IOCB_RCV_ELS_LIST64_CX:
795         case CMD_IOCB_CLOSE_EXTENDED_CN:
796         case CMD_IOCB_ABORT_EXTENDED_CN:
797         case CMD_IOCB_RET_HBQE64_CN:
798         case CMD_IOCB_FCP_IBIDIR64_CR:
799         case CMD_IOCB_FCP_IBIDIR64_CX:
800         case CMD_IOCB_FCP_ITASKMGT64_CX:
801         case CMD_IOCB_LOGENTRY_CN:
802         case CMD_IOCB_LOGENTRY_ASYNC_CN:
803                 printk("%s - Unhandled SLI-3 Command x%x\n",
804                                 __func__, iocb_cmnd);
805                 type = LPFC_UNKNOWN_IOCB;
806                 break;
807         default:
808                 type = LPFC_UNKNOWN_IOCB;
809                 break;
810         }
811
812         return type;
813 }
814
815 /**
816  * lpfc_sli_ring_map - Issue config_ring mbox for all rings
817  * @phba: Pointer to HBA context object.
818  *
819  * This function is called from SLI initialization code
820  * to configure every ring of the HBA's SLI interface. The
821  * caller is not required to hold any lock. This function issues
822  * a config_ring mailbox command for each ring.
823  * This function returns zero if successful else returns a negative
824  * error code.
825  **/
826 static int
827 lpfc_sli_ring_map(struct lpfc_hba *phba)
828 {
829         struct lpfc_sli *psli = &phba->sli;
830         LPFC_MBOXQ_t *pmb;
831         MAILBOX_t *pmbox;
832         int i, rc, ret = 0;
833
834         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
835         if (!pmb)
836                 return -ENOMEM;
837         pmbox = &pmb->u.mb;
838         phba->link_state = LPFC_INIT_MBX_CMDS;
839         for (i = 0; i < psli->num_rings; i++) {
840                 lpfc_config_ring(phba, i, pmb);
841                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
842                 if (rc != MBX_SUCCESS) {
843                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
844                                         "0446 Adapter failed to init (%d), "
845                                         "mbxCmd x%x CFG_RING, mbxStatus x%x, "
846                                         "ring %d\n",
847                                         rc, pmbox->mbxCommand,
848                                         pmbox->mbxStatus, i);
849                         phba->link_state = LPFC_HBA_ERROR;
850                         ret = -ENXIO;
851                         break;
852                 }
853         }
854         mempool_free(pmb, phba->mbox_mem_pool);
855         return ret;
856 }
857
858 /**
859  * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
860  * @phba: Pointer to HBA context object.
861  * @pring: Pointer to driver SLI ring object.
862  * @piocb: Pointer to the driver iocb object.
863  *
864  * This function is called with hbalock held. The function adds the
865  * new iocb to txcmplq of the given ring. This function always returns
866  * 0. If this function is called for ELS ring, this function checks if
867  * there is a vport associated with the ELS command. This function also
868  * starts els_tmofunc timer if this is an ELS command.
869  **/
870 static int
871 lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
872                         struct lpfc_iocbq *piocb)
873 {
874         list_add_tail(&piocb->list, &pring->txcmplq);
875         pring->txcmplq_cnt++;
876         if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
877            (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
878            (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
879                 if (!piocb->vport)
880                         BUG();
881                 else
882                         mod_timer(&piocb->vport->els_tmofunc,
883                                   jiffies + HZ * (phba->fc_ratov << 1));
884         }
885
886
887         return 0;
888 }
889
890 /**
891  * lpfc_sli_ringtx_get - Get first element of the txq
892  * @phba: Pointer to HBA context object.
893  * @pring: Pointer to driver SLI ring object.
894  *
895  * This function is called with hbalock held to get next
896  * iocb in txq of the given ring. If there is any iocb in
897  * the txq, the function returns first iocb in the list after
898  * removing the iocb from the list, else it returns NULL.
899  **/
900 static struct lpfc_iocbq *
901 lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
902 {
903         struct lpfc_iocbq *cmd_iocb;
904
905         list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
906         if (cmd_iocb != NULL)
907                 pring->txq_cnt--;
908         return cmd_iocb;
909 }
910
911 /**
912  * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
913  * @phba: Pointer to HBA context object.
914  * @pring: Pointer to driver SLI ring object.
915  *
916  * This function is called with hbalock held and the caller must post the
917  * iocb without releasing the lock. If the caller releases the lock,
918  * iocb slot returned by the function is not guaranteed to be available.
919  * The function returns pointer to the next available iocb slot if there
920  * is available slot in the ring, else it returns NULL.
921  * If the get index of the ring is ahead of the put index, the function
922  * will post an error attention event to the worker thread to take the
923  * HBA to offline state.
924  **/
925 static IOCB_t *
926 lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
927 {
928         struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
929         uint32_t  max_cmd_idx = pring->numCiocb;
930         if ((pring->next_cmdidx == pring->cmdidx) &&
931            (++pring->next_cmdidx >= max_cmd_idx))
932                 pring->next_cmdidx = 0;
933
934         if (unlikely(pring->local_getidx == pring->next_cmdidx)) {
935
936                 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
937
938                 if (unlikely(pring->local_getidx >= max_cmd_idx)) {
939                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
940                                         "0315 Ring %d issue: portCmdGet %d "
941                                         "is bigger than cmd ring %d\n",
942                                         pring->ringno,
943                                         pring->local_getidx, max_cmd_idx);
944
945                         phba->link_state = LPFC_HBA_ERROR;
946                         /*
947                          * All error attention handlers are posted to
948                          * worker thread
949                          */
950                         phba->work_ha |= HA_ERATT;
951                         phba->work_hs = HS_FFER3;
952
953                         lpfc_worker_wake_up(phba);
954
955                         return NULL;
956                 }
957
958                 if (pring->local_getidx == pring->next_cmdidx)
959                         return NULL;
960         }
961
962         return lpfc_cmd_iocb(phba, pring);
963 }
964
965 /**
966  * lpfc_sli_next_iotag - Get an iotag for the iocb
967  * @phba: Pointer to HBA context object.
968  * @iocbq: Pointer to driver iocb object.
969  *
970  * This function gets an iotag for the iocb. If there is no unused iotag and
971  * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
972  * array and assigns a new iotag.
973  * The function returns the allocated iotag if successful, else returns zero.
974  * Zero is not a valid iotag.
975  * The caller is not required to hold any lock.
976  **/
977 uint16_t
978 lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
979 {
980         struct lpfc_iocbq **new_arr;
981         struct lpfc_iocbq **old_arr;
982         size_t new_len;
983         struct lpfc_sli *psli = &phba->sli;
984         uint16_t iotag;
985
986         spin_lock_irq(&phba->hbalock);
987         iotag = psli->last_iotag;
988         if(++iotag < psli->iocbq_lookup_len) {
989                 psli->last_iotag = iotag;
990                 psli->iocbq_lookup[iotag] = iocbq;
991                 spin_unlock_irq(&phba->hbalock);
992                 iocbq->iotag = iotag;
993                 return iotag;
994         } else if (psli->iocbq_lookup_len < (0xffff
995                                            - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
996                 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
997                 spin_unlock_irq(&phba->hbalock);
998                 new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
999                                   GFP_KERNEL);
1000                 if (new_arr) {
1001                         spin_lock_irq(&phba->hbalock);
1002                         old_arr = psli->iocbq_lookup;
1003                         if (new_len <= psli->iocbq_lookup_len) {
1004                                 /* highly unprobable case */
1005                                 kfree(new_arr);
1006                                 iotag = psli->last_iotag;
1007                                 if(++iotag < psli->iocbq_lookup_len) {
1008                                         psli->last_iotag = iotag;
1009                                         psli->iocbq_lookup[iotag] = iocbq;
1010                                         spin_unlock_irq(&phba->hbalock);
1011                                         iocbq->iotag = iotag;
1012                                         return iotag;
1013                                 }
1014                                 spin_unlock_irq(&phba->hbalock);
1015                                 return 0;
1016                         }
1017                         if (psli->iocbq_lookup)
1018                                 memcpy(new_arr, old_arr,
1019                                        ((psli->last_iotag  + 1) *
1020                                         sizeof (struct lpfc_iocbq *)));
1021                         psli->iocbq_lookup = new_arr;
1022                         psli->iocbq_lookup_len = new_len;
1023                         psli->last_iotag = iotag;
1024                         psli->iocbq_lookup[iotag] = iocbq;
1025                         spin_unlock_irq(&phba->hbalock);
1026                         iocbq->iotag = iotag;
1027                         kfree(old_arr);
1028                         return iotag;
1029                 }
1030         } else
1031                 spin_unlock_irq(&phba->hbalock);
1032
1033         lpfc_printf_log(phba, KERN_ERR,LOG_SLI,
1034                         "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
1035                         psli->last_iotag);
1036
1037         return 0;
1038 }
1039
1040 /**
1041  * lpfc_sli_submit_iocb - Submit an iocb to the firmware
1042  * @phba: Pointer to HBA context object.
1043  * @pring: Pointer to driver SLI ring object.
1044  * @iocb: Pointer to iocb slot in the ring.
1045  * @nextiocb: Pointer to driver iocb object which need to be
1046  *            posted to firmware.
1047  *
1048  * This function is called with hbalock held to post a new iocb to
1049  * the firmware. This function copies the new iocb to ring iocb slot and
1050  * updates the ring pointers. It adds the new iocb to txcmplq if there is
1051  * a completion call back for this iocb else the function will free the
1052  * iocb object.
1053  **/
1054 static void
1055 lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1056                 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
1057 {
1058         /*
1059          * Set up an iotag
1060          */
1061         nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
1062
1063
1064         if (pring->ringno == LPFC_ELS_RING) {
1065                 lpfc_debugfs_slow_ring_trc(phba,
1066                         "IOCB cmd ring:   wd4:x%08x wd6:x%08x wd7:x%08x",
1067                         *(((uint32_t *) &nextiocb->iocb) + 4),
1068                         *(((uint32_t *) &nextiocb->iocb) + 6),
1069                         *(((uint32_t *) &nextiocb->iocb) + 7));
1070         }
1071
1072         /*
1073          * Issue iocb command to adapter
1074          */
1075         lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
1076         wmb();
1077         pring->stats.iocb_cmd++;
1078
1079         /*
1080          * If there is no completion routine to call, we can release the
1081          * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
1082          * that have no rsp ring completion, iocb_cmpl MUST be NULL.
1083          */
1084         if (nextiocb->iocb_cmpl)
1085                 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
1086         else
1087                 __lpfc_sli_release_iocbq(phba, nextiocb);
1088
1089         /*
1090          * Let the HBA know what IOCB slot will be the next one the
1091          * driver will put a command into.
1092          */
1093         pring->cmdidx = pring->next_cmdidx;
1094         writel(pring->cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
1095 }
1096
1097 /**
1098  * lpfc_sli_update_full_ring - Update the chip attention register
1099  * @phba: Pointer to HBA context object.
1100  * @pring: Pointer to driver SLI ring object.
1101  *
1102  * The caller is not required to hold any lock for calling this function.
1103  * This function updates the chip attention bits for the ring to inform firmware
1104  * that there are pending work to be done for this ring and requests an
1105  * interrupt when there is space available in the ring. This function is
1106  * called when the driver is unable to post more iocbs to the ring due
1107  * to unavailability of space in the ring.
1108  **/
1109 static void
1110 lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1111 {
1112         int ringno = pring->ringno;
1113
1114         pring->flag |= LPFC_CALL_RING_AVAILABLE;
1115
1116         wmb();
1117
1118         /*
1119          * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
1120          * The HBA will tell us when an IOCB entry is available.
1121          */
1122         writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
1123         readl(phba->CAregaddr); /* flush */
1124
1125         pring->stats.iocb_cmd_full++;
1126 }
1127
1128 /**
1129  * lpfc_sli_update_ring - Update chip attention register
1130  * @phba: Pointer to HBA context object.
1131  * @pring: Pointer to driver SLI ring object.
1132  *
1133  * This function updates the chip attention register bit for the
1134  * given ring to inform HBA that there is more work to be done
1135  * in this ring. The caller is not required to hold any lock.
1136  **/
1137 static void
1138 lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1139 {
1140         int ringno = pring->ringno;
1141
1142         /*
1143          * Tell the HBA that there is work to do in this ring.
1144          */
1145         if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
1146                 wmb();
1147                 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
1148                 readl(phba->CAregaddr); /* flush */
1149         }
1150 }
1151
1152 /**
1153  * lpfc_sli_resume_iocb - Process iocbs in the txq
1154  * @phba: Pointer to HBA context object.
1155  * @pring: Pointer to driver SLI ring object.
1156  *
1157  * This function is called with hbalock held to post pending iocbs
1158  * in the txq to the firmware. This function is called when driver
1159  * detects space available in the ring.
1160  **/
1161 static void
1162 lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1163 {
1164         IOCB_t *iocb;
1165         struct lpfc_iocbq *nextiocb;
1166
1167         /*
1168          * Check to see if:
1169          *  (a) there is anything on the txq to send
1170          *  (b) link is up
1171          *  (c) link attention events can be processed (fcp ring only)
1172          *  (d) IOCB processing is not blocked by the outstanding mbox command.
1173          */
1174         if (pring->txq_cnt &&
1175             lpfc_is_link_up(phba) &&
1176             (pring->ringno != phba->sli.fcp_ring ||
1177              phba->sli.sli_flag & LPFC_PROCESS_LA)) {
1178
1179                 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
1180                        (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
1181                         lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
1182
1183                 if (iocb)
1184                         lpfc_sli_update_ring(phba, pring);
1185                 else
1186                         lpfc_sli_update_full_ring(phba, pring);
1187         }
1188
1189         return;
1190 }
1191
1192 /**
1193  * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
1194  * @phba: Pointer to HBA context object.
1195  * @hbqno: HBQ number.
1196  *
1197  * This function is called with hbalock held to get the next
1198  * available slot for the given HBQ. If there is free slot
1199  * available for the HBQ it will return pointer to the next available
1200  * HBQ entry else it will return NULL.
1201  **/
1202 static struct lpfc_hbq_entry *
1203 lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
1204 {
1205         struct hbq_s *hbqp = &phba->hbqs[hbqno];
1206
1207         if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
1208             ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
1209                 hbqp->next_hbqPutIdx = 0;
1210
1211         if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
1212                 uint32_t raw_index = phba->hbq_get[hbqno];
1213                 uint32_t getidx = le32_to_cpu(raw_index);
1214
1215                 hbqp->local_hbqGetIdx = getidx;
1216
1217                 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
1218                         lpfc_printf_log(phba, KERN_ERR,
1219                                         LOG_SLI | LOG_VPORT,
1220                                         "1802 HBQ %d: local_hbqGetIdx "
1221                                         "%u is > than hbqp->entry_count %u\n",
1222                                         hbqno, hbqp->local_hbqGetIdx,
1223                                         hbqp->entry_count);
1224
1225                         phba->link_state = LPFC_HBA_ERROR;
1226                         return NULL;
1227                 }
1228
1229                 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
1230                         return NULL;
1231         }
1232
1233         return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
1234                         hbqp->hbqPutIdx;
1235 }
1236
1237 /**
1238  * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
1239  * @phba: Pointer to HBA context object.
1240  *
1241  * This function is called with no lock held to free all the
1242  * hbq buffers while uninitializing the SLI interface. It also
1243  * frees the HBQ buffers returned by the firmware but not yet
1244  * processed by the upper layers.
1245  **/
1246 void
1247 lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
1248 {
1249         struct lpfc_dmabuf *dmabuf, *next_dmabuf;
1250         struct hbq_dmabuf *hbq_buf;
1251         unsigned long flags;
1252         int i, hbq_count;
1253         uint32_t hbqno;
1254
1255         hbq_count = lpfc_sli_hbq_count();
1256         /* Return all memory used by all HBQs */
1257         spin_lock_irqsave(&phba->hbalock, flags);
1258         for (i = 0; i < hbq_count; ++i) {
1259                 list_for_each_entry_safe(dmabuf, next_dmabuf,
1260                                 &phba->hbqs[i].hbq_buffer_list, list) {
1261                         hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1262                         list_del(&hbq_buf->dbuf.list);
1263                         (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
1264                 }
1265                 phba->hbqs[i].buffer_count = 0;
1266         }
1267         /* Return all HBQ buffer that are in-fly */
1268         list_for_each_entry_safe(dmabuf, next_dmabuf, &phba->rb_pend_list,
1269                                  list) {
1270                 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1271                 list_del(&hbq_buf->dbuf.list);
1272                 if (hbq_buf->tag == -1) {
1273                         (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1274                                 (phba, hbq_buf);
1275                 } else {
1276                         hbqno = hbq_buf->tag >> 16;
1277                         if (hbqno >= LPFC_MAX_HBQS)
1278                                 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1279                                         (phba, hbq_buf);
1280                         else
1281                                 (phba->hbqs[hbqno].hbq_free_buffer)(phba,
1282                                         hbq_buf);
1283                 }
1284         }
1285
1286         /* Mark the HBQs not in use */
1287         phba->hbq_in_use = 0;
1288         spin_unlock_irqrestore(&phba->hbalock, flags);
1289 }
1290
1291 /**
1292  * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
1293  * @phba: Pointer to HBA context object.
1294  * @hbqno: HBQ number.
1295  * @hbq_buf: Pointer to HBQ buffer.
1296  *
1297  * This function is called with the hbalock held to post a
1298  * hbq buffer to the firmware. If the function finds an empty
1299  * slot in the HBQ, it will post the buffer. The function will return
1300  * pointer to the hbq entry if it successfully post the buffer
1301  * else it will return NULL.
1302  **/
1303 static int
1304 lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
1305                          struct hbq_dmabuf *hbq_buf)
1306 {
1307         return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
1308 }
1309
1310 /**
1311  * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
1312  * @phba: Pointer to HBA context object.
1313  * @hbqno: HBQ number.
1314  * @hbq_buf: Pointer to HBQ buffer.
1315  *
1316  * This function is called with the hbalock held to post a hbq buffer to the
1317  * firmware. If the function finds an empty slot in the HBQ, it will post the
1318  * buffer and place it on the hbq_buffer_list. The function will return zero if
1319  * it successfully post the buffer else it will return an error.
1320  **/
1321 static int
1322 lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
1323                             struct hbq_dmabuf *hbq_buf)
1324 {
1325         struct lpfc_hbq_entry *hbqe;
1326         dma_addr_t physaddr = hbq_buf->dbuf.phys;
1327
1328         /* Get next HBQ entry slot to use */
1329         hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
1330         if (hbqe) {
1331                 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1332
1333                 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1334                 hbqe->bde.addrLow  = le32_to_cpu(putPaddrLow(physaddr));
1335                 hbqe->bde.tus.f.bdeSize = hbq_buf->size;
1336                 hbqe->bde.tus.f.bdeFlags = 0;
1337                 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
1338                 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
1339                                 /* Sync SLIM */
1340                 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
1341                 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
1342                                 /* flush */
1343                 readl(phba->hbq_put + hbqno);
1344                 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
1345                 return 0;
1346         } else
1347                 return -ENOMEM;
1348 }
1349
1350 /**
1351  * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
1352  * @phba: Pointer to HBA context object.
1353  * @hbqno: HBQ number.
1354  * @hbq_buf: Pointer to HBQ buffer.
1355  *
1356  * This function is called with the hbalock held to post an RQE to the SLI4
1357  * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
1358  * the hbq_buffer_list and return zero, otherwise it will return an error.
1359  **/
1360 static int
1361 lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
1362                             struct hbq_dmabuf *hbq_buf)
1363 {
1364         int rc;
1365         struct lpfc_rqe hrqe;
1366         struct lpfc_rqe drqe;
1367
1368         hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
1369         hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
1370         drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
1371         drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
1372         rc = lpfc_sli4_rq_put(phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
1373                               &hrqe, &drqe);
1374         if (rc < 0)
1375                 return rc;
1376         hbq_buf->tag = rc;
1377         list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
1378         return 0;
1379 }
1380
1381 /* HBQ for ELS and CT traffic. */
1382 static struct lpfc_hbq_init lpfc_els_hbq = {
1383         .rn = 1,
1384         .entry_count = 256,
1385         .mask_count = 0,
1386         .profile = 0,
1387         .ring_mask = (1 << LPFC_ELS_RING),
1388         .buffer_count = 0,
1389         .init_count = 40,
1390         .add_count = 40,
1391 };
1392
1393 /* HBQ for the extra ring if needed */
1394 static struct lpfc_hbq_init lpfc_extra_hbq = {
1395         .rn = 1,
1396         .entry_count = 200,
1397         .mask_count = 0,
1398         .profile = 0,
1399         .ring_mask = (1 << LPFC_EXTRA_RING),
1400         .buffer_count = 0,
1401         .init_count = 0,
1402         .add_count = 5,
1403 };
1404
1405 /* Array of HBQs */
1406 struct lpfc_hbq_init *lpfc_hbq_defs[] = {
1407         &lpfc_els_hbq,
1408         &lpfc_extra_hbq,
1409 };
1410
1411 /**
1412  * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
1413  * @phba: Pointer to HBA context object.
1414  * @hbqno: HBQ number.
1415  * @count: Number of HBQ buffers to be posted.
1416  *
1417  * This function is called with no lock held to post more hbq buffers to the
1418  * given HBQ. The function returns the number of HBQ buffers successfully
1419  * posted.
1420  **/
1421 static int
1422 lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
1423 {
1424         uint32_t i, posted = 0;
1425         unsigned long flags;
1426         struct hbq_dmabuf *hbq_buffer;
1427         LIST_HEAD(hbq_buf_list);
1428         if (!phba->hbqs[hbqno].hbq_alloc_buffer)
1429                 return 0;
1430
1431         if ((phba->hbqs[hbqno].buffer_count + count) >
1432             lpfc_hbq_defs[hbqno]->entry_count)
1433                 count = lpfc_hbq_defs[hbqno]->entry_count -
1434                                         phba->hbqs[hbqno].buffer_count;
1435         if (!count)
1436                 return 0;
1437         /* Allocate HBQ entries */
1438         for (i = 0; i < count; i++) {
1439                 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
1440                 if (!hbq_buffer)
1441                         break;
1442                 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
1443         }
1444         /* Check whether HBQ is still in use */
1445         spin_lock_irqsave(&phba->hbalock, flags);
1446         if (!phba->hbq_in_use)
1447                 goto err;
1448         while (!list_empty(&hbq_buf_list)) {
1449                 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1450                                  dbuf.list);
1451                 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
1452                                       (hbqno << 16));
1453                 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
1454                         phba->hbqs[hbqno].buffer_count++;
1455                         posted++;
1456                 } else
1457                         (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1458         }
1459         spin_unlock_irqrestore(&phba->hbalock, flags);
1460         return posted;
1461 err:
1462         spin_unlock_irqrestore(&phba->hbalock, flags);
1463         while (!list_empty(&hbq_buf_list)) {
1464                 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1465                                  dbuf.list);
1466                 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1467         }
1468         return 0;
1469 }
1470
1471 /**
1472  * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
1473  * @phba: Pointer to HBA context object.
1474  * @qno: HBQ number.
1475  *
1476  * This function posts more buffers to the HBQ. This function
1477  * is called with no lock held. The function returns the number of HBQ entries
1478  * successfully allocated.
1479  **/
1480 int
1481 lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
1482 {
1483         if (phba->sli_rev == LPFC_SLI_REV4)
1484                 return 0;
1485         else
1486                 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1487                                          lpfc_hbq_defs[qno]->add_count);
1488 }
1489
1490 /**
1491  * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
1492  * @phba: Pointer to HBA context object.
1493  * @qno:  HBQ queue number.
1494  *
1495  * This function is called from SLI initialization code path with
1496  * no lock held to post initial HBQ buffers to firmware. The
1497  * function returns the number of HBQ entries successfully allocated.
1498  **/
1499 static int
1500 lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
1501 {
1502         if (phba->sli_rev == LPFC_SLI_REV4)
1503                 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1504                                          lpfc_hbq_defs[qno]->entry_count);
1505         else
1506                 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1507                                          lpfc_hbq_defs[qno]->init_count);
1508 }
1509
1510 /**
1511  * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
1512  * @phba: Pointer to HBA context object.
1513  * @hbqno: HBQ number.
1514  *
1515  * This function removes the first hbq buffer on an hbq list and returns a
1516  * pointer to that buffer. If it finds no buffers on the list it returns NULL.
1517  **/
1518 static struct hbq_dmabuf *
1519 lpfc_sli_hbqbuf_get(struct list_head *rb_list)
1520 {
1521         struct lpfc_dmabuf *d_buf;
1522
1523         list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
1524         if (!d_buf)
1525                 return NULL;
1526         return container_of(d_buf, struct hbq_dmabuf, dbuf);
1527 }
1528
1529 /**
1530  * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
1531  * @phba: Pointer to HBA context object.
1532  * @tag: Tag of the hbq buffer.
1533  *
1534  * This function is called with hbalock held. This function searches
1535  * for the hbq buffer associated with the given tag in the hbq buffer
1536  * list. If it finds the hbq buffer, it returns the hbq_buffer other wise
1537  * it returns NULL.
1538  **/
1539 static struct hbq_dmabuf *
1540 lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
1541 {
1542         struct lpfc_dmabuf *d_buf;
1543         struct hbq_dmabuf *hbq_buf;
1544         uint32_t hbqno;
1545
1546         hbqno = tag >> 16;
1547         if (hbqno >= LPFC_MAX_HBQS)
1548                 return NULL;
1549
1550         spin_lock_irq(&phba->hbalock);
1551         list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
1552                 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
1553                 if (hbq_buf->tag == tag) {
1554                         spin_unlock_irq(&phba->hbalock);
1555                         return hbq_buf;
1556                 }
1557         }
1558         spin_unlock_irq(&phba->hbalock);
1559         lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
1560                         "1803 Bad hbq tag. Data: x%x x%x\n",
1561                         tag, phba->hbqs[tag >> 16].buffer_count);
1562         return NULL;
1563 }
1564
1565 /**
1566  * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
1567  * @phba: Pointer to HBA context object.
1568  * @hbq_buffer: Pointer to HBQ buffer.
1569  *
1570  * This function is called with hbalock. This function gives back
1571  * the hbq buffer to firmware. If the HBQ does not have space to
1572  * post the buffer, it will free the buffer.
1573  **/
1574 void
1575 lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
1576 {
1577         uint32_t hbqno;
1578
1579         if (hbq_buffer) {
1580                 hbqno = hbq_buffer->tag >> 16;
1581                 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
1582                         (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1583         }
1584 }
1585
1586 /**
1587  * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
1588  * @mbxCommand: mailbox command code.
1589  *
1590  * This function is called by the mailbox event handler function to verify
1591  * that the completed mailbox command is a legitimate mailbox command. If the
1592  * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
1593  * and the mailbox event handler will take the HBA offline.
1594  **/
1595 static int
1596 lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
1597 {
1598         uint8_t ret;
1599
1600         switch (mbxCommand) {
1601         case MBX_LOAD_SM:
1602         case MBX_READ_NV:
1603         case MBX_WRITE_NV:
1604         case MBX_WRITE_VPARMS:
1605         case MBX_RUN_BIU_DIAG:
1606         case MBX_INIT_LINK:
1607         case MBX_DOWN_LINK:
1608         case MBX_CONFIG_LINK:
1609         case MBX_CONFIG_RING:
1610         case MBX_RESET_RING:
1611         case MBX_READ_CONFIG:
1612         case MBX_READ_RCONFIG:
1613         case MBX_READ_SPARM:
1614         case MBX_READ_STATUS:
1615         case MBX_READ_RPI:
1616         case MBX_READ_XRI:
1617         case MBX_READ_REV:
1618         case MBX_READ_LNK_STAT:
1619         case MBX_REG_LOGIN:
1620         case MBX_UNREG_LOGIN:
1621         case MBX_READ_LA:
1622         case MBX_CLEAR_LA:
1623         case MBX_DUMP_MEMORY:
1624         case MBX_DUMP_CONTEXT:
1625         case MBX_RUN_DIAGS:
1626         case MBX_RESTART:
1627         case MBX_UPDATE_CFG:
1628         case MBX_DOWN_LOAD:
1629         case MBX_DEL_LD_ENTRY:
1630         case MBX_RUN_PROGRAM:
1631         case MBX_SET_MASK:
1632         case MBX_SET_VARIABLE:
1633         case MBX_UNREG_D_ID:
1634         case MBX_KILL_BOARD:
1635         case MBX_CONFIG_FARP:
1636         case MBX_BEACON:
1637         case MBX_LOAD_AREA:
1638         case MBX_RUN_BIU_DIAG64:
1639         case MBX_CONFIG_PORT:
1640         case MBX_READ_SPARM64:
1641         case MBX_READ_RPI64:
1642         case MBX_REG_LOGIN64:
1643         case MBX_READ_LA64:
1644         case MBX_WRITE_WWN:
1645         case MBX_SET_DEBUG:
1646         case MBX_LOAD_EXP_ROM:
1647         case MBX_ASYNCEVT_ENABLE:
1648         case MBX_REG_VPI:
1649         case MBX_UNREG_VPI:
1650         case MBX_HEARTBEAT:
1651         case MBX_PORT_CAPABILITIES:
1652         case MBX_PORT_IOV_CONTROL:
1653         case MBX_SLI4_CONFIG:
1654         case MBX_SLI4_REQ_FTRS:
1655         case MBX_REG_FCFI:
1656         case MBX_UNREG_FCFI:
1657         case MBX_REG_VFI:
1658         case MBX_UNREG_VFI:
1659         case MBX_INIT_VPI:
1660         case MBX_INIT_VFI:
1661         case MBX_RESUME_RPI:
1662                 ret = mbxCommand;
1663                 break;
1664         default:
1665                 ret = MBX_SHUTDOWN;
1666                 break;
1667         }
1668         return ret;
1669 }
1670
1671 /**
1672  * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
1673  * @phba: Pointer to HBA context object.
1674  * @pmboxq: Pointer to mailbox command.
1675  *
1676  * This is completion handler function for mailbox commands issued from
1677  * lpfc_sli_issue_mbox_wait function. This function is called by the
1678  * mailbox event handler function with no lock held. This function
1679  * will wake up thread waiting on the wait queue pointed by context1
1680  * of the mailbox.
1681  **/
1682 void
1683 lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
1684 {
1685         wait_queue_head_t *pdone_q;
1686         unsigned long drvr_flag;
1687
1688         /*
1689          * If pdone_q is empty, the driver thread gave up waiting and
1690          * continued running.
1691          */
1692         pmboxq->mbox_flag |= LPFC_MBX_WAKE;
1693         spin_lock_irqsave(&phba->hbalock, drvr_flag);
1694         pdone_q = (wait_queue_head_t *) pmboxq->context1;
1695         if (pdone_q)
1696                 wake_up_interruptible(pdone_q);
1697         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
1698         return;
1699 }
1700
1701
1702 /**
1703  * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
1704  * @phba: Pointer to HBA context object.
1705  * @pmb: Pointer to mailbox object.
1706  *
1707  * This function is the default mailbox completion handler. It
1708  * frees the memory resources associated with the completed mailbox
1709  * command. If the completed command is a REG_LOGIN mailbox command,
1710  * this function will issue a UREG_LOGIN to re-claim the RPI.
1711  **/
1712 void
1713 lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
1714 {
1715         struct lpfc_dmabuf *mp;
1716         uint16_t rpi, vpi;
1717         int rc;
1718         struct lpfc_vport  *vport = pmb->vport;
1719
1720         mp = (struct lpfc_dmabuf *) (pmb->context1);
1721
1722         if (mp) {
1723                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1724                 kfree(mp);
1725         }
1726
1727         if ((pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) &&
1728             (phba->sli_rev == LPFC_SLI_REV4))
1729                 lpfc_sli4_free_rpi(phba, pmb->u.mb.un.varUnregLogin.rpi);
1730
1731         /*
1732          * If a REG_LOGIN succeeded  after node is destroyed or node
1733          * is in re-discovery driver need to cleanup the RPI.
1734          */
1735         if (!(phba->pport->load_flag & FC_UNLOADING) &&
1736             pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
1737             !pmb->u.mb.mbxStatus) {
1738                 rpi = pmb->u.mb.un.varWords[0];
1739                 vpi = pmb->u.mb.un.varRegLogin.vpi - phba->vpi_base;
1740                 lpfc_unreg_login(phba, vpi, rpi, pmb);
1741                 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
1742                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
1743                 if (rc != MBX_NOT_FINISHED)
1744                         return;
1745         }
1746
1747         /* Unreg VPI, if the REG_VPI succeed after VLink failure */
1748         if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
1749                 !(phba->pport->load_flag & FC_UNLOADING) &&
1750                 !pmb->u.mb.mbxStatus) {
1751                 lpfc_unreg_vpi(phba, pmb->u.mb.un.varRegVpi.vpi, pmb);
1752                 pmb->vport = vport;
1753                 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
1754                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
1755                 if (rc != MBX_NOT_FINISHED)
1756                         return;
1757         }
1758
1759         if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
1760                 lpfc_sli4_mbox_cmd_free(phba, pmb);
1761         else
1762                 mempool_free(pmb, phba->mbox_mem_pool);
1763 }
1764
1765 /**
1766  * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
1767  * @phba: Pointer to HBA context object.
1768  *
1769  * This function is called with no lock held. This function processes all
1770  * the completed mailbox commands and gives it to upper layers. The interrupt
1771  * service routine processes mailbox completion interrupt and adds completed
1772  * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
1773  * Worker thread call lpfc_sli_handle_mb_event, which will return the
1774  * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
1775  * function returns the mailbox commands to the upper layer by calling the
1776  * completion handler function of each mailbox.
1777  **/
1778 int
1779 lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
1780 {
1781         MAILBOX_t *pmbox;
1782         LPFC_MBOXQ_t *pmb;
1783         int rc;
1784         LIST_HEAD(cmplq);
1785
1786         phba->sli.slistat.mbox_event++;
1787
1788         /* Get all completed mailboxe buffers into the cmplq */
1789         spin_lock_irq(&phba->hbalock);
1790         list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
1791         spin_unlock_irq(&phba->hbalock);
1792
1793         /* Get a Mailbox buffer to setup mailbox commands for callback */
1794         do {
1795                 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
1796                 if (pmb == NULL)
1797                         break;
1798
1799                 pmbox = &pmb->u.mb;
1800
1801                 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
1802                         if (pmb->vport) {
1803                                 lpfc_debugfs_disc_trc(pmb->vport,
1804                                         LPFC_DISC_TRC_MBOX_VPORT,
1805                                         "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
1806                                         (uint32_t)pmbox->mbxCommand,
1807                                         pmbox->un.varWords[0],
1808                                         pmbox->un.varWords[1]);
1809                         }
1810                         else {
1811                                 lpfc_debugfs_disc_trc(phba->pport,
1812                                         LPFC_DISC_TRC_MBOX,
1813                                         "MBOX cmpl:       cmd:x%x mb:x%x x%x",
1814                                         (uint32_t)pmbox->mbxCommand,
1815                                         pmbox->un.varWords[0],
1816                                         pmbox->un.varWords[1]);
1817                         }
1818                 }
1819
1820                 /*
1821                  * It is a fatal error if unknown mbox command completion.
1822                  */
1823                 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
1824                     MBX_SHUTDOWN) {
1825                         /* Unknown mailbox command compl */
1826                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
1827                                         "(%d):0323 Unknown Mailbox command "
1828                                         "x%x (x%x) Cmpl\n",
1829                                         pmb->vport ? pmb->vport->vpi : 0,
1830                                         pmbox->mbxCommand,
1831                                         lpfc_sli4_mbox_opcode_get(phba, pmb));
1832                         phba->link_state = LPFC_HBA_ERROR;
1833                         phba->work_hs = HS_FFER3;
1834                         lpfc_handle_eratt(phba);
1835                         continue;
1836                 }
1837
1838                 if (pmbox->mbxStatus) {
1839                         phba->sli.slistat.mbox_stat_err++;
1840                         if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
1841                                 /* Mbox cmd cmpl error - RETRYing */
1842                                 lpfc_printf_log(phba, KERN_INFO,
1843                                                 LOG_MBOX | LOG_SLI,
1844                                                 "(%d):0305 Mbox cmd cmpl "
1845                                                 "error - RETRYing Data: x%x "
1846                                                 "(x%x) x%x x%x x%x\n",
1847                                                 pmb->vport ? pmb->vport->vpi :0,
1848                                                 pmbox->mbxCommand,
1849                                                 lpfc_sli4_mbox_opcode_get(phba,
1850                                                                           pmb),
1851                                                 pmbox->mbxStatus,
1852                                                 pmbox->un.varWords[0],
1853                                                 pmb->vport->port_state);
1854                                 pmbox->mbxStatus = 0;
1855                                 pmbox->mbxOwner = OWN_HOST;
1856                                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
1857                                 if (rc != MBX_NOT_FINISHED)
1858                                         continue;
1859                         }
1860                 }
1861
1862                 /* Mailbox cmd <cmd> Cmpl <cmpl> */
1863                 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
1864                                 "(%d):0307 Mailbox cmd x%x (x%x) Cmpl x%p "
1865                                 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x\n",
1866                                 pmb->vport ? pmb->vport->vpi : 0,
1867                                 pmbox->mbxCommand,
1868                                 lpfc_sli4_mbox_opcode_get(phba, pmb),
1869                                 pmb->mbox_cmpl,
1870                                 *((uint32_t *) pmbox),
1871                                 pmbox->un.varWords[0],
1872                                 pmbox->un.varWords[1],
1873                                 pmbox->un.varWords[2],
1874                                 pmbox->un.varWords[3],
1875                                 pmbox->un.varWords[4],
1876                                 pmbox->un.varWords[5],
1877                                 pmbox->un.varWords[6],
1878                                 pmbox->un.varWords[7]);
1879
1880                 if (pmb->mbox_cmpl)
1881                         pmb->mbox_cmpl(phba,pmb);
1882         } while (1);
1883         return 0;
1884 }
1885
1886 /**
1887  * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
1888  * @phba: Pointer to HBA context object.
1889  * @pring: Pointer to driver SLI ring object.
1890  * @tag: buffer tag.
1891  *
1892  * This function is called with no lock held. When QUE_BUFTAG_BIT bit
1893  * is set in the tag the buffer is posted for a particular exchange,
1894  * the function will return the buffer without replacing the buffer.
1895  * If the buffer is for unsolicited ELS or CT traffic, this function
1896  * returns the buffer and also posts another buffer to the firmware.
1897  **/
1898 static struct lpfc_dmabuf *
1899 lpfc_sli_get_buff(struct lpfc_hba *phba,
1900                   struct lpfc_sli_ring *pring,
1901                   uint32_t tag)
1902 {
1903         struct hbq_dmabuf *hbq_entry;
1904
1905         if (tag & QUE_BUFTAG_BIT)
1906                 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
1907         hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
1908         if (!hbq_entry)
1909                 return NULL;
1910         return &hbq_entry->dbuf;
1911 }
1912
1913 /**
1914  * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
1915  * @phba: Pointer to HBA context object.
1916  * @pring: Pointer to driver SLI ring object.
1917  * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
1918  * @fch_r_ctl: the r_ctl for the first frame of the sequence.
1919  * @fch_type: the type for the first frame of the sequence.
1920  *
1921  * This function is called with no lock held. This function uses the r_ctl and
1922  * type of the received sequence to find the correct callback function to call
1923  * to process the sequence.
1924  **/
1925 static int
1926 lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1927                          struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
1928                          uint32_t fch_type)
1929 {
1930         int i;
1931
1932         /* unSolicited Responses */
1933         if (pring->prt[0].profile) {
1934                 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
1935                         (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
1936                                                                         saveq);
1937                 return 1;
1938         }
1939         /* We must search, based on rctl / type
1940            for the right routine */
1941         for (i = 0; i < pring->num_mask; i++) {
1942                 if ((pring->prt[i].rctl == fch_r_ctl) &&
1943                     (pring->prt[i].type == fch_type)) {
1944                         if (pring->prt[i].lpfc_sli_rcv_unsol_event)
1945                                 (pring->prt[i].lpfc_sli_rcv_unsol_event)
1946                                                 (phba, pring, saveq);
1947                         return 1;
1948                 }
1949         }
1950         return 0;
1951 }
1952
1953 /**
1954  * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
1955  * @phba: Pointer to HBA context object.
1956  * @pring: Pointer to driver SLI ring object.
1957  * @saveq: Pointer to the unsolicited iocb.
1958  *
1959  * This function is called with no lock held by the ring event handler
1960  * when there is an unsolicited iocb posted to the response ring by the
1961  * firmware. This function gets the buffer associated with the iocbs
1962  * and calls the event handler for the ring. This function handles both
1963  * qring buffers and hbq buffers.
1964  * When the function returns 1 the caller can free the iocb object otherwise
1965  * upper layer functions will free the iocb objects.
1966  **/
1967 static int
1968 lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1969                             struct lpfc_iocbq *saveq)
1970 {
1971         IOCB_t           * irsp;
1972         WORD5            * w5p;
1973         uint32_t           Rctl, Type;
1974         uint32_t           match;
1975         struct lpfc_iocbq *iocbq;
1976         struct lpfc_dmabuf *dmzbuf;
1977
1978         match = 0;
1979         irsp = &(saveq->iocb);
1980
1981         if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
1982                 if (pring->lpfc_sli_rcv_async_status)
1983                         pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
1984                 else
1985                         lpfc_printf_log(phba,
1986                                         KERN_WARNING,
1987                                         LOG_SLI,
1988                                         "0316 Ring %d handler: unexpected "
1989                                         "ASYNC_STATUS iocb received evt_code "
1990                                         "0x%x\n",
1991                                         pring->ringno,
1992                                         irsp->un.asyncstat.evt_code);
1993                 return 1;
1994         }
1995
1996         if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
1997                 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
1998                 if (irsp->ulpBdeCount > 0) {
1999                         dmzbuf = lpfc_sli_get_buff(phba, pring,
2000                                         irsp->un.ulpWord[3]);
2001                         lpfc_in_buf_free(phba, dmzbuf);
2002                 }
2003
2004                 if (irsp->ulpBdeCount > 1) {
2005                         dmzbuf = lpfc_sli_get_buff(phba, pring,
2006                                         irsp->unsli3.sli3Words[3]);
2007                         lpfc_in_buf_free(phba, dmzbuf);
2008                 }
2009
2010                 if (irsp->ulpBdeCount > 2) {
2011                         dmzbuf = lpfc_sli_get_buff(phba, pring,
2012                                 irsp->unsli3.sli3Words[7]);
2013                         lpfc_in_buf_free(phba, dmzbuf);
2014                 }
2015
2016                 return 1;
2017         }
2018
2019         if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
2020                 if (irsp->ulpBdeCount != 0) {
2021                         saveq->context2 = lpfc_sli_get_buff(phba, pring,
2022                                                 irsp->un.ulpWord[3]);
2023                         if (!saveq->context2)
2024                                 lpfc_printf_log(phba,
2025                                         KERN_ERR,
2026                                         LOG_SLI,
2027                                         "0341 Ring %d Cannot find buffer for "
2028                                         "an unsolicited iocb. tag 0x%x\n",
2029                                         pring->ringno,
2030                                         irsp->un.ulpWord[3]);
2031                 }
2032                 if (irsp->ulpBdeCount == 2) {
2033                         saveq->context3 = lpfc_sli_get_buff(phba, pring,
2034                                                 irsp->unsli3.sli3Words[7]);
2035                         if (!saveq->context3)
2036                                 lpfc_printf_log(phba,
2037                                         KERN_ERR,
2038                                         LOG_SLI,
2039                                         "0342 Ring %d Cannot find buffer for an"
2040                                         " unsolicited iocb. tag 0x%x\n",
2041                                         pring->ringno,
2042                                         irsp->unsli3.sli3Words[7]);
2043                 }
2044                 list_for_each_entry(iocbq, &saveq->list, list) {
2045                         irsp = &(iocbq->iocb);
2046                         if (irsp->ulpBdeCount != 0) {
2047                                 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
2048                                                         irsp->un.ulpWord[3]);
2049                                 if (!iocbq->context2)
2050                                         lpfc_printf_log(phba,
2051                                                 KERN_ERR,
2052                                                 LOG_SLI,
2053                                                 "0343 Ring %d Cannot find "
2054                                                 "buffer for an unsolicited iocb"
2055                                                 ". tag 0x%x\n", pring->ringno,
2056                                                 irsp->un.ulpWord[3]);
2057                         }
2058                         if (irsp->ulpBdeCount == 2) {
2059                                 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
2060                                                 irsp->unsli3.sli3Words[7]);
2061                                 if (!iocbq->context3)
2062                                         lpfc_printf_log(phba,
2063                                                 KERN_ERR,
2064                                                 LOG_SLI,
2065                                                 "0344 Ring %d Cannot find "
2066                                                 "buffer for an unsolicited "
2067                                                 "iocb. tag 0x%x\n",
2068                                                 pring->ringno,
2069                                                 irsp->unsli3.sli3Words[7]);
2070                         }
2071                 }
2072         }
2073         if (irsp->ulpBdeCount != 0 &&
2074             (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
2075              irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
2076                 int found = 0;
2077
2078                 /* search continue save q for same XRI */
2079                 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
2080                         if (iocbq->iocb.ulpContext == saveq->iocb.ulpContext) {
2081                                 list_add_tail(&saveq->list, &iocbq->list);
2082                                 found = 1;
2083                                 break;
2084                         }
2085                 }
2086                 if (!found)
2087                         list_add_tail(&saveq->clist,
2088                                       &pring->iocb_continue_saveq);
2089                 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
2090                         list_del_init(&iocbq->clist);
2091                         saveq = iocbq;
2092                         irsp = &(saveq->iocb);
2093                 } else
2094                         return 0;
2095         }
2096         if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
2097             (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
2098             (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
2099                 Rctl = FC_RCTL_ELS_REQ;
2100                 Type = FC_TYPE_ELS;
2101         } else {
2102                 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
2103                 Rctl = w5p->hcsw.Rctl;
2104                 Type = w5p->hcsw.Type;
2105
2106                 /* Firmware Workaround */
2107                 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
2108                         (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
2109                          irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
2110                         Rctl = FC_RCTL_ELS_REQ;
2111                         Type = FC_TYPE_ELS;
2112                         w5p->hcsw.Rctl = Rctl;
2113                         w5p->hcsw.Type = Type;
2114                 }
2115         }
2116
2117         if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
2118                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
2119                                 "0313 Ring %d handler: unexpected Rctl x%x "
2120                                 "Type x%x received\n",
2121                                 pring->ringno, Rctl, Type);
2122
2123         return 1;
2124 }
2125
2126 /**
2127  * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
2128  * @phba: Pointer to HBA context object.
2129  * @pring: Pointer to driver SLI ring object.
2130  * @prspiocb: Pointer to response iocb object.
2131  *
2132  * This function looks up the iocb_lookup table to get the command iocb
2133  * corresponding to the given response iocb using the iotag of the
2134  * response iocb. This function is called with the hbalock held.
2135  * This function returns the command iocb object if it finds the command
2136  * iocb else returns NULL.
2137  **/
2138 static struct lpfc_iocbq *
2139 lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
2140                       struct lpfc_sli_ring *pring,
2141                       struct lpfc_iocbq *prspiocb)
2142 {
2143         struct lpfc_iocbq *cmd_iocb = NULL;
2144         uint16_t iotag;
2145
2146         iotag = prspiocb->iocb.ulpIoTag;
2147
2148         if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2149                 cmd_iocb = phba->sli.iocbq_lookup[iotag];
2150                 list_del_init(&cmd_iocb->list);
2151                 pring->txcmplq_cnt--;
2152                 return cmd_iocb;
2153         }
2154
2155         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2156                         "0317 iotag x%x is out off "
2157                         "range: max iotag x%x wd0 x%x\n",
2158                         iotag, phba->sli.last_iotag,
2159                         *(((uint32_t *) &prspiocb->iocb) + 7));
2160         return NULL;
2161 }
2162
2163 /**
2164  * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
2165  * @phba: Pointer to HBA context object.
2166  * @pring: Pointer to driver SLI ring object.
2167  * @iotag: IOCB tag.
2168  *
2169  * This function looks up the iocb_lookup table to get the command iocb
2170  * corresponding to the given iotag. This function is called with the
2171  * hbalock held.
2172  * This function returns the command iocb object if it finds the command
2173  * iocb else returns NULL.
2174  **/
2175 static struct lpfc_iocbq *
2176 lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
2177                              struct lpfc_sli_ring *pring, uint16_t iotag)
2178 {
2179         struct lpfc_iocbq *cmd_iocb;
2180
2181         if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2182                 cmd_iocb = phba->sli.iocbq_lookup[iotag];
2183                 list_del_init(&cmd_iocb->list);
2184                 pring->txcmplq_cnt--;
2185                 return cmd_iocb;
2186         }
2187
2188         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2189                         "0372 iotag x%x is out off range: max iotag (x%x)\n",
2190                         iotag, phba->sli.last_iotag);
2191         return NULL;
2192 }
2193
2194 /**
2195  * lpfc_sli_process_sol_iocb - process solicited iocb completion
2196  * @phba: Pointer to HBA context object.
2197  * @pring: Pointer to driver SLI ring object.
2198  * @saveq: Pointer to the response iocb to be processed.
2199  *
2200  * This function is called by the ring event handler for non-fcp
2201  * rings when there is a new response iocb in the response ring.
2202  * The caller is not required to hold any locks. This function
2203  * gets the command iocb associated with the response iocb and
2204  * calls the completion handler for the command iocb. If there
2205  * is no completion handler, the function will free the resources
2206  * associated with command iocb. If the response iocb is for
2207  * an already aborted command iocb, the status of the completion
2208  * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
2209  * This function always returns 1.
2210  **/
2211 static int
2212 lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2213                           struct lpfc_iocbq *saveq)
2214 {
2215         struct lpfc_iocbq *cmdiocbp;
2216         int rc = 1;
2217         unsigned long iflag;
2218
2219         /* Based on the iotag field, get the cmd IOCB from the txcmplq */
2220         spin_lock_irqsave(&phba->hbalock, iflag);
2221         cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
2222         spin_unlock_irqrestore(&phba->hbalock, iflag);
2223
2224         if (cmdiocbp) {
2225                 if (cmdiocbp->iocb_cmpl) {
2226                         /*
2227                          * If an ELS command failed send an event to mgmt
2228                          * application.
2229                          */
2230                         if (saveq->iocb.ulpStatus &&
2231                              (pring->ringno == LPFC_ELS_RING) &&
2232                              (cmdiocbp->iocb.ulpCommand ==
2233                                 CMD_ELS_REQUEST64_CR))
2234                                 lpfc_send_els_failure_event(phba,
2235                                         cmdiocbp, saveq);
2236
2237                         /*
2238                          * Post all ELS completions to the worker thread.
2239                          * All other are passed to the completion callback.
2240                          */
2241                         if (pring->ringno == LPFC_ELS_RING) {
2242                                 if ((phba->sli_rev < LPFC_SLI_REV4) &&
2243                                     (cmdiocbp->iocb_flag &
2244                                                         LPFC_DRIVER_ABORTED)) {
2245                                         spin_lock_irqsave(&phba->hbalock,
2246                                                           iflag);
2247                                         cmdiocbp->iocb_flag &=
2248                                                 ~LPFC_DRIVER_ABORTED;
2249                                         spin_unlock_irqrestore(&phba->hbalock,
2250                                                                iflag);
2251                                         saveq->iocb.ulpStatus =
2252                                                 IOSTAT_LOCAL_REJECT;
2253                                         saveq->iocb.un.ulpWord[4] =
2254                                                 IOERR_SLI_ABORTED;
2255
2256                                         /* Firmware could still be in progress
2257                                          * of DMAing payload, so don't free data
2258                                          * buffer till after a hbeat.
2259                                          */
2260                                         spin_lock_irqsave(&phba->hbalock,
2261                                                           iflag);
2262                                         saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
2263                                         spin_unlock_irqrestore(&phba->hbalock,
2264                                                                iflag);
2265                                 }
2266                                 if (phba->sli_rev == LPFC_SLI_REV4) {
2267                                         if (saveq->iocb_flag &
2268                                             LPFC_EXCHANGE_BUSY) {
2269                                                 /* Set cmdiocb flag for the
2270                                                  * exchange busy so sgl (xri)
2271                                                  * will not be released until
2272                                                  * the abort xri is received
2273                                                  * from hba.
2274                                                  */
2275                                                 spin_lock_irqsave(
2276                                                         &phba->hbalock, iflag);
2277                                                 cmdiocbp->iocb_flag |=
2278                                                         LPFC_EXCHANGE_BUSY;
2279                                                 spin_unlock_irqrestore(
2280                                                         &phba->hbalock, iflag);
2281                                         }
2282                                         if (cmdiocbp->iocb_flag &
2283                                             LPFC_DRIVER_ABORTED) {
2284                                                 /*
2285                                                  * Clear LPFC_DRIVER_ABORTED
2286                                                  * bit in case it was driver
2287                                                  * initiated abort.
2288                                                  */
2289                                                 spin_lock_irqsave(
2290                                                         &phba->hbalock, iflag);
2291                                                 cmdiocbp->iocb_flag &=
2292                                                         ~LPFC_DRIVER_ABORTED;
2293                                                 spin_unlock_irqrestore(
2294                                                         &phba->hbalock, iflag);
2295                                                 cmdiocbp->iocb.ulpStatus =
2296                                                         IOSTAT_LOCAL_REJECT;
2297                                                 cmdiocbp->iocb.un.ulpWord[4] =
2298                                                         IOERR_ABORT_REQUESTED;
2299                                                 /*
2300                                                  * For SLI4, irsiocb contains
2301                                                  * NO_XRI in sli_xritag, it
2302                                                  * shall not affect releasing
2303                                                  * sgl (xri) process.
2304                                                  */
2305                                                 saveq->iocb.ulpStatus =
2306                                                         IOSTAT_LOCAL_REJECT;
2307                                                 saveq->iocb.un.ulpWord[4] =
2308                                                         IOERR_SLI_ABORTED;
2309                                                 spin_lock_irqsave(
2310                                                         &phba->hbalock, iflag);
2311                                                 saveq->iocb_flag |=
2312                                                         LPFC_DELAY_MEM_FREE;
2313                                                 spin_unlock_irqrestore(
2314                                                         &phba->hbalock, iflag);
2315                                         }
2316                                 }
2317                         }
2318                         (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
2319                 } else
2320                         lpfc_sli_release_iocbq(phba, cmdiocbp);
2321         } else {
2322                 /*
2323                  * Unknown initiating command based on the response iotag.
2324                  * This could be the case on the ELS ring because of
2325                  * lpfc_els_abort().
2326                  */
2327                 if (pring->ringno != LPFC_ELS_RING) {
2328                         /*
2329                          * Ring <ringno> handler: unexpected completion IoTag
2330                          * <IoTag>
2331                          */
2332                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
2333                                          "0322 Ring %d handler: "
2334                                          "unexpected completion IoTag x%x "
2335                                          "Data: x%x x%x x%x x%x\n",
2336                                          pring->ringno,
2337                                          saveq->iocb.ulpIoTag,
2338                                          saveq->iocb.ulpStatus,
2339                                          saveq->iocb.un.ulpWord[4],
2340                                          saveq->iocb.ulpCommand,
2341                                          saveq->iocb.ulpContext);
2342                 }
2343         }
2344
2345         return rc;
2346 }
2347
2348 /**
2349  * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
2350  * @phba: Pointer to HBA context object.
2351  * @pring: Pointer to driver SLI ring object.
2352  *
2353  * This function is called from the iocb ring event handlers when
2354  * put pointer is ahead of the get pointer for a ring. This function signal
2355  * an error attention condition to the worker thread and the worker
2356  * thread will transition the HBA to offline state.
2357  **/
2358 static void
2359 lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
2360 {
2361         struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
2362         /*
2363          * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
2364          * rsp ring <portRspMax>
2365          */
2366         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2367                         "0312 Ring %d handler: portRspPut %d "
2368                         "is bigger than rsp ring %d\n",
2369                         pring->ringno, le32_to_cpu(pgp->rspPutInx),
2370                         pring->numRiocb);
2371
2372         phba->link_state = LPFC_HBA_ERROR;
2373
2374         /*
2375          * All error attention handlers are posted to
2376          * worker thread
2377          */
2378         phba->work_ha |= HA_ERATT;
2379         phba->work_hs = HS_FFER3;
2380
2381         lpfc_worker_wake_up(phba);
2382
2383         return;
2384 }
2385
2386 /**
2387  * lpfc_poll_eratt - Error attention polling timer timeout handler
2388  * @ptr: Pointer to address of HBA context object.
2389  *
2390  * This function is invoked by the Error Attention polling timer when the
2391  * timer times out. It will check the SLI Error Attention register for
2392  * possible attention events. If so, it will post an Error Attention event
2393  * and wake up worker thread to process it. Otherwise, it will set up the
2394  * Error Attention polling timer for the next poll.
2395  **/
2396 void lpfc_poll_eratt(unsigned long ptr)
2397 {
2398         struct lpfc_hba *phba;
2399         uint32_t eratt = 0;
2400
2401         phba = (struct lpfc_hba *)ptr;
2402
2403         /* Check chip HA register for error event */
2404         eratt = lpfc_sli_check_eratt(phba);
2405
2406         if (eratt)
2407                 /* Tell the worker thread there is work to do */
2408                 lpfc_worker_wake_up(phba);
2409         else
2410                 /* Restart the timer for next eratt poll */
2411                 mod_timer(&phba->eratt_poll, jiffies +
2412                                         HZ * LPFC_ERATT_POLL_INTERVAL);
2413         return;
2414 }
2415
2416
2417 /**
2418  * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
2419  * @phba: Pointer to HBA context object.
2420  * @pring: Pointer to driver SLI ring object.
2421  * @mask: Host attention register mask for this ring.
2422  *
2423  * This function is called from the interrupt context when there is a ring
2424  * event for the fcp ring. The caller does not hold any lock.
2425  * The function processes each response iocb in the response ring until it
2426  * finds an iocb with LE bit set and chains all the iocbs upto the iocb with
2427  * LE bit set. The function will call the completion handler of the command iocb
2428  * if the response iocb indicates a completion for a command iocb or it is
2429  * an abort completion. The function will call lpfc_sli_process_unsol_iocb
2430  * function if this is an unsolicited iocb.
2431  * This routine presumes LPFC_FCP_RING handling and doesn't bother
2432  * to check it explicitly.
2433  */
2434 int
2435 lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
2436                                 struct lpfc_sli_ring *pring, uint32_t mask)
2437 {
2438         struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
2439         IOCB_t *irsp = NULL;
2440         IOCB_t *entry = NULL;
2441         struct lpfc_iocbq *cmdiocbq = NULL;
2442         struct lpfc_iocbq rspiocbq;
2443         uint32_t status;
2444         uint32_t portRspPut, portRspMax;
2445         int rc = 1;
2446         lpfc_iocb_type type;
2447         unsigned long iflag;
2448         uint32_t rsp_cmpl = 0;
2449
2450         spin_lock_irqsave(&phba->hbalock, iflag);
2451         pring->stats.iocb_event++;
2452
2453         /*
2454          * The next available response entry should never exceed the maximum
2455          * entries.  If it does, treat it as an adapter hardware error.
2456          */
2457         portRspMax = pring->numRiocb;
2458         portRspPut = le32_to_cpu(pgp->rspPutInx);
2459         if (unlikely(portRspPut >= portRspMax)) {
2460                 lpfc_sli_rsp_pointers_error(phba, pring);
2461                 spin_unlock_irqrestore(&phba->hbalock, iflag);
2462                 return 1;
2463         }
2464         if (phba->fcp_ring_in_use) {
2465                 spin_unlock_irqrestore(&phba->hbalock, iflag);
2466                 return 1;
2467         } else
2468                 phba->fcp_ring_in_use = 1;
2469
2470         rmb();
2471         while (pring->rspidx != portRspPut) {
2472                 /*
2473                  * Fetch an entry off the ring and copy it into a local data
2474                  * structure.  The copy involves a byte-swap since the
2475                  * network byte order and pci byte orders are different.
2476                  */
2477                 entry = lpfc_resp_iocb(phba, pring);
2478                 phba->last_completion_time = jiffies;
2479
2480                 if (++pring->rspidx >= portRspMax)
2481                         pring->rspidx = 0;
2482
2483                 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
2484                                       (uint32_t *) &rspiocbq.iocb,
2485                                       phba->iocb_rsp_size);
2486                 INIT_LIST_HEAD(&(rspiocbq.list));
2487                 irsp = &rspiocbq.iocb;
2488
2489                 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
2490                 pring->stats.iocb_rsp++;
2491                 rsp_cmpl++;
2492
2493                 if (unlikely(irsp->ulpStatus)) {
2494                         /*
2495                          * If resource errors reported from HBA, reduce
2496                          * queuedepths of the SCSI device.
2497                          */
2498                         if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
2499                                 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
2500                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
2501                                 phba->lpfc_rampdown_queue_depth(phba);
2502                                 spin_lock_irqsave(&phba->hbalock, iflag);
2503                         }
2504
2505                         /* Rsp ring <ringno> error: IOCB */
2506                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
2507                                         "0336 Rsp Ring %d error: IOCB Data: "
2508                                         "x%x x%x x%x x%x x%x x%x x%x x%x\n",
2509                                         pring->ringno,
2510                                         irsp->un.ulpWord[0],
2511                                         irsp->un.ulpWord[1],
2512                                         irsp->un.ulpWord[2],
2513                                         irsp->un.ulpWord[3],
2514                                         irsp->un.ulpWord[4],
2515                                         irsp->un.ulpWord[5],
2516                                         *(uint32_t *)&irsp->un1,
2517                                         *((uint32_t *)&irsp->un1 + 1));
2518                 }
2519
2520                 switch (type) {
2521                 case LPFC_ABORT_IOCB:
2522                 case LPFC_SOL_IOCB:
2523                         /*
2524                          * Idle exchange closed via ABTS from port.  No iocb
2525                          * resources need to be recovered.
2526                          */
2527                         if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
2528                                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
2529                                                 "0333 IOCB cmd 0x%x"
2530                                                 " processed. Skipping"
2531                                                 " completion\n",
2532                                                 irsp->ulpCommand);
2533                                 break;
2534                         }
2535
2536                         cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
2537                                                          &rspiocbq);
2538                         if (unlikely(!cmdiocbq))
2539                                 break;
2540                         if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
2541                                 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
2542                         if (cmdiocbq->iocb_cmpl) {
2543                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
2544                                 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
2545                                                       &rspiocbq);
2546                                 spin_lock_irqsave(&phba->hbalock, iflag);
2547                         }
2548                         break;
2549                 case LPFC_UNSOL_IOCB:
2550                         spin_unlock_irqrestore(&phba->hbalock, iflag);
2551                         lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
2552                         spin_lock_irqsave(&phba->hbalock, iflag);
2553                         break;
2554                 default:
2555                         if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
2556                                 char adaptermsg[LPFC_MAX_ADPTMSG];
2557                                 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
2558                                 memcpy(&adaptermsg[0], (uint8_t *) irsp,
2559                                        MAX_MSG_DATA);
2560                                 dev_warn(&((phba->pcidev)->dev),
2561                                          "lpfc%d: %s\n",
2562                                          phba->brd_no, adaptermsg);
2563                         } else {
2564                                 /* Unknown IOCB command */
2565                                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2566                                                 "0334 Unknown IOCB command "
2567                                                 "Data: x%x, x%x x%x x%x x%x\n",
2568                                                 type, irsp->ulpCommand,
2569                                                 irsp->ulpStatus,
2570                                                 irsp->ulpIoTag,
2571                                                 irsp->ulpContext);
2572                         }
2573                         break;
2574                 }
2575
2576                 /*
2577                  * The response IOCB has been processed.  Update the ring
2578                  * pointer in SLIM.  If the port response put pointer has not
2579                  * been updated, sync the pgp->rspPutInx and fetch the new port
2580                  * response put pointer.
2581                  */
2582                 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
2583
2584                 if (pring->rspidx == portRspPut)
2585                         portRspPut = le32_to_cpu(pgp->rspPutInx);
2586         }
2587
2588         if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
2589                 pring->stats.iocb_rsp_full++;
2590                 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
2591                 writel(status, phba->CAregaddr);
2592                 readl(phba->CAregaddr);
2593         }
2594         if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
2595                 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
2596                 pring->stats.iocb_cmd_empty++;
2597
2598                 /* Force update of the local copy of cmdGetInx */
2599                 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
2600                 lpfc_sli_resume_iocb(phba, pring);
2601
2602                 if ((pring->lpfc_sli_cmd_available))
2603                         (pring->lpfc_sli_cmd_available) (phba, pring);
2604
2605         }
2606
2607         phba->fcp_ring_in_use = 0;
2608         spin_unlock_irqrestore(&phba->hbalock, iflag);
2609         return rc;
2610 }
2611
2612 /**
2613  * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
2614  * @phba: Pointer to HBA context object.
2615  * @pring: Pointer to driver SLI ring object.
2616  * @rspiocbp: Pointer to driver response IOCB object.
2617  *
2618  * This function is called from the worker thread when there is a slow-path
2619  * response IOCB to process. This function chains all the response iocbs until
2620  * seeing the iocb with the LE bit set. The function will call
2621  * lpfc_sli_process_sol_iocb function if the response iocb indicates a
2622  * completion of a command iocb. The function will call the
2623  * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
2624  * The function frees the resources or calls the completion handler if this
2625  * iocb is an abort completion. The function returns NULL when the response
2626  * iocb has the LE bit set and all the chained iocbs are processed, otherwise
2627  * this function shall chain the iocb on to the iocb_continueq and return the
2628  * response iocb passed in.
2629  **/
2630 static struct lpfc_iocbq *
2631 lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2632                         struct lpfc_iocbq *rspiocbp)
2633 {
2634         struct lpfc_iocbq *saveq;
2635         struct lpfc_iocbq *cmdiocbp;
2636         struct lpfc_iocbq *next_iocb;
2637         IOCB_t *irsp = NULL;
2638         uint32_t free_saveq;
2639         uint8_t iocb_cmd_type;
2640         lpfc_iocb_type type;
2641         unsigned long iflag;
2642         int rc;
2643
2644         spin_lock_irqsave(&phba->hbalock, iflag);
2645         /* First add the response iocb to the countinueq list */
2646         list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
2647         pring->iocb_continueq_cnt++;
2648
2649         /* Now, determine whetehr the list is completed for processing */
2650         irsp = &rspiocbp->iocb;
2651         if (irsp->ulpLe) {
2652                 /*
2653                  * By default, the driver expects to free all resources
2654                  * associated with this iocb completion.
2655                  */
2656                 free_saveq = 1;
2657                 saveq = list_get_first(&pring->iocb_continueq,
2658                                        struct lpfc_iocbq, list);
2659                 irsp = &(saveq->iocb);
2660                 list_del_init(&pring->iocb_continueq);
2661                 pring->iocb_continueq_cnt = 0;
2662
2663                 pring->stats.iocb_rsp++;
2664
2665                 /*
2666                  * If resource errors reported from HBA, reduce
2667                  * queuedepths of the SCSI device.
2668                  */
2669                 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
2670                     (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
2671                         spin_unlock_irqrestore(&phba->hbalock, iflag);
2672                         phba->lpfc_rampdown_queue_depth(phba);
2673                         spin_lock_irqsave(&phba->hbalock, iflag);
2674                 }
2675
2676                 if (irsp->ulpStatus) {
2677                         /* Rsp ring <ringno> error: IOCB */
2678                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
2679                                         "0328 Rsp Ring %d error: "
2680                                         "IOCB Data: "
2681                                         "x%x x%x x%x x%x "
2682                                         "x%x x%x x%x x%x "
2683                                         "x%x x%x x%x x%x "
2684                                         "x%x x%x x%x x%x\n",
2685                                         pring->ringno,
2686                                         irsp->un.ulpWord[0],
2687                                         irsp->un.ulpWord[1],
2688                                         irsp->un.ulpWord[2],
2689                                         irsp->un.ulpWord[3],
2690                                         irsp->un.ulpWord[4],
2691                                         irsp->un.ulpWord[5],
2692                                         *(((uint32_t *) irsp) + 6),
2693                                         *(((uint32_t *) irsp) + 7),
2694                                         *(((uint32_t *) irsp) + 8),
2695                                         *(((uint32_t *) irsp) + 9),
2696                                         *(((uint32_t *) irsp) + 10),
2697                                         *(((uint32_t *) irsp) + 11),
2698                                         *(((uint32_t *) irsp) + 12),
2699                                         *(((uint32_t *) irsp) + 13),
2700                                         *(((uint32_t *) irsp) + 14),
2701                                         *(((uint32_t *) irsp) + 15));
2702                 }
2703
2704                 /*
2705                  * Fetch the IOCB command type and call the correct completion
2706                  * routine. Solicited and Unsolicited IOCBs on the ELS ring
2707                  * get freed back to the lpfc_iocb_list by the discovery
2708                  * kernel thread.
2709                  */
2710                 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
2711                 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
2712                 switch (type) {
2713                 case LPFC_SOL_IOCB:
2714                         spin_unlock_irqrestore(&phba->hbalock, iflag);
2715                         rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
2716                         spin_lock_irqsave(&phba->hbalock, iflag);
2717                         break;
2718
2719                 case LPFC_UNSOL_IOCB:
2720                         spin_unlock_irqrestore(&phba->hbalock, iflag);
2721                         rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
2722                         spin_lock_irqsave(&phba->hbalock, iflag);
2723                         if (!rc)
2724                                 free_saveq = 0;
2725                         break;
2726
2727                 case LPFC_ABORT_IOCB:
2728                         cmdiocbp = NULL;
2729                         if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
2730                                 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
2731                                                                  saveq);
2732                         if (cmdiocbp) {
2733                                 /* Call the specified completion routine */
2734                                 if (cmdiocbp->iocb_cmpl) {
2735                                         spin_unlock_irqrestore(&phba->hbalock,
2736                                                                iflag);
2737                                         (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
2738                                                               saveq);
2739                                         spin_lock_irqsave(&phba->hbalock,
2740                                                           iflag);
2741                                 } else
2742                                         __lpfc_sli_release_iocbq(phba,
2743                                                                  cmdiocbp);
2744                         }
2745                         break;
2746
2747                 case LPFC_UNKNOWN_IOCB:
2748                         if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
2749                                 char adaptermsg[LPFC_MAX_ADPTMSG];
2750                                 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
2751                                 memcpy(&adaptermsg[0], (uint8_t *)irsp,
2752                                        MAX_MSG_DATA);
2753                                 dev_warn(&((phba->pcidev)->dev),
2754                                          "lpfc%d: %s\n",
2755                                          phba->brd_no, adaptermsg);
2756                         } else {
2757                                 /* Unknown IOCB command */
2758                                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2759                                                 "0335 Unknown IOCB "
2760                                                 "command Data: x%x "
2761                                                 "x%x x%x x%x\n",
2762                                                 irsp->ulpCommand,
2763                                                 irsp->ulpStatus,
2764                                                 irsp->ulpIoTag,
2765                                                 irsp->ulpContext);
2766                         }
2767                         break;
2768                 }
2769
2770                 if (free_saveq) {
2771                         list_for_each_entry_safe(rspiocbp, next_iocb,
2772                                                  &saveq->list, list) {
2773                                 list_del(&rspiocbp->list);
2774                                 __lpfc_sli_release_iocbq(phba, rspiocbp);
2775                         }
2776                         __lpfc_sli_release_iocbq(phba, saveq);
2777                 }
2778                 rspiocbp = NULL;
2779         }
2780         spin_unlock_irqrestore(&phba->hbalock, iflag);
2781         return rspiocbp;
2782 }
2783
2784 /**
2785  * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
2786  * @phba: Pointer to HBA context object.
2787  * @pring: Pointer to driver SLI ring object.
2788  * @mask: Host attention register mask for this ring.
2789  *
2790  * This routine wraps the actual slow_ring event process routine from the
2791  * API jump table function pointer from the lpfc_hba struct.
2792  **/
2793 void
2794 lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
2795                                 struct lpfc_sli_ring *pring, uint32_t mask)
2796 {
2797         phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
2798 }
2799
2800 /**
2801  * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
2802  * @phba: Pointer to HBA context object.
2803  * @pring: Pointer to driver SLI ring object.
2804  * @mask: Host attention register mask for this ring.
2805  *
2806  * This function is called from the worker thread when there is a ring event
2807  * for non-fcp rings. The caller does not hold any lock. The function will
2808  * remove each response iocb in the response ring and calls the handle
2809  * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
2810  **/
2811 static void
2812 lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
2813                                    struct lpfc_sli_ring *pring, uint32_t mask)
2814 {
2815         struct lpfc_pgp *pgp;
2816         IOCB_t *entry;
2817         IOCB_t *irsp = NULL;
2818         struct lpfc_iocbq *rspiocbp = NULL;
2819         uint32_t portRspPut, portRspMax;
2820         unsigned long iflag;
2821         uint32_t status;
2822
2823         pgp = &phba->port_gp[pring->ringno];
2824         spin_lock_irqsave(&phba->hbalock, iflag);
2825         pring->stats.iocb_event++;
2826
2827         /*
2828          * The next available response entry should never exceed the maximum
2829          * entries.  If it does, treat it as an adapter hardware error.
2830          */
2831         portRspMax = pring->numRiocb;
2832         portRspPut = le32_to_cpu(pgp->rspPutInx);
2833         if (portRspPut >= portRspMax) {
2834                 /*
2835                  * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
2836                  * rsp ring <portRspMax>
2837                  */
2838                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2839                                 "0303 Ring %d handler: portRspPut %d "
2840                                 "is bigger than rsp ring %d\n",
2841                                 pring->ringno, portRspPut, portRspMax);
2842
2843                 phba->link_state = LPFC_HBA_ERROR;
2844                 spin_unlock_irqrestore(&phba->hbalock, iflag);
2845
2846                 phba->work_hs = HS_FFER3;
2847                 lpfc_handle_eratt(phba);
2848
2849                 return;
2850         }
2851
2852         rmb();
2853         while (pring->rspidx != portRspPut) {
2854                 /*
2855                  * Build a completion list and call the appropriate handler.
2856                  * The process is to get the next available response iocb, get
2857                  * a free iocb from the list, copy the response data into the
2858                  * free iocb, insert to the continuation list, and update the
2859                  * next response index to slim.  This process makes response
2860                  * iocb's in the ring available to DMA as fast as possible but
2861                  * pays a penalty for a copy operation.  Since the iocb is
2862                  * only 32 bytes, this penalty is considered small relative to
2863                  * the PCI reads for register values and a slim write.  When
2864                  * the ulpLe field is set, the entire Command has been
2865                  * received.
2866                  */
2867                 entry = lpfc_resp_iocb(phba, pring);
2868
2869                 phba->last_completion_time = jiffies;
2870                 rspiocbp = __lpfc_sli_get_iocbq(phba);
2871                 if (rspiocbp == NULL) {
2872                         printk(KERN_ERR "%s: out of buffers! Failing "
2873                                "completion.\n", __func__);
2874                         break;
2875                 }
2876
2877                 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
2878                                       phba->iocb_rsp_size);
2879                 irsp = &rspiocbp->iocb;
2880
2881                 if (++pring->rspidx >= portRspMax)
2882                         pring->rspidx = 0;
2883
2884                 if (pring->ringno == LPFC_ELS_RING) {
2885                         lpfc_debugfs_slow_ring_trc(phba,
2886                         "IOCB rsp ring:   wd4:x%08x wd6:x%08x wd7:x%08x",
2887                                 *(((uint32_t *) irsp) + 4),
2888                                 *(((uint32_t *) irsp) + 6),
2889                                 *(((uint32_t *) irsp) + 7));
2890                 }
2891
2892                 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
2893
2894                 spin_unlock_irqrestore(&phba->hbalock, iflag);
2895                 /* Handle the response IOCB */
2896                 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
2897                 spin_lock_irqsave(&phba->hbalock, iflag);
2898
2899                 /*
2900                  * If the port response put pointer has not been updated, sync
2901                  * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
2902                  * response put pointer.
2903                  */
2904                 if (pring->rspidx == portRspPut) {
2905                         portRspPut = le32_to_cpu(pgp->rspPutInx);
2906                 }
2907         } /* while (pring->rspidx != portRspPut) */
2908
2909         if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
2910                 /* At least one response entry has been freed */
2911                 pring->stats.iocb_rsp_full++;
2912                 /* SET RxRE_RSP in Chip Att register */
2913                 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
2914                 writel(status, phba->CAregaddr);
2915                 readl(phba->CAregaddr); /* flush */
2916         }
2917         if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
2918                 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
2919                 pring->stats.iocb_cmd_empty++;
2920
2921                 /* Force update of the local copy of cmdGetInx */
2922                 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
2923                 lpfc_sli_resume_iocb(phba, pring);
2924
2925                 if ((pring->lpfc_sli_cmd_available))
2926                         (pring->lpfc_sli_cmd_available) (phba, pring);
2927
2928         }
2929
2930         spin_unlock_irqrestore(&phba->hbalock, iflag);
2931         return;
2932 }
2933
2934 /**
2935  * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
2936  * @phba: Pointer to HBA context object.
2937  * @pring: Pointer to driver SLI ring object.
2938  * @mask: Host attention register mask for this ring.
2939  *
2940  * This function is called from the worker thread when there is a pending
2941  * ELS response iocb on the driver internal slow-path response iocb worker
2942  * queue. The caller does not hold any lock. The function will remove each
2943  * response iocb from the response worker queue and calls the handle
2944  * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
2945  **/
2946 static void
2947 lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
2948                                    struct lpfc_sli_ring *pring, uint32_t mask)
2949 {
2950         struct lpfc_iocbq *irspiocbq;
2951         struct hbq_dmabuf *dmabuf;
2952         struct lpfc_cq_event *cq_event;
2953         unsigned long iflag;
2954
2955         spin_lock_irqsave(&phba->hbalock, iflag);
2956         phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
2957         spin_unlock_irqrestore(&phba->hbalock, iflag);
2958         while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
2959                 /* Get the response iocb from the head of work queue */
2960                 spin_lock_irqsave(&phba->hbalock, iflag);
2961                 list_remove_head(&phba->sli4_hba.sp_queue_event,
2962                                  cq_event, struct lpfc_cq_event, list);
2963                 spin_unlock_irqrestore(&phba->hbalock, iflag);
2964
2965                 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
2966                 case CQE_CODE_COMPL_WQE:
2967                         irspiocbq = container_of(cq_event, struct lpfc_iocbq,
2968                                                  cq_event);
2969                         /* Translate ELS WCQE to response IOCBQ */
2970                         irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
2971                                                                    irspiocbq);
2972                         if (irspiocbq)
2973                                 lpfc_sli_sp_handle_rspiocb(phba, pring,
2974                                                            irspiocbq);
2975                         break;
2976                 case CQE_CODE_RECEIVE:
2977                         dmabuf = container_of(cq_event, struct hbq_dmabuf,
2978                                               cq_event);
2979                         lpfc_sli4_handle_received_buffer(phba, dmabuf);
2980                         break;
2981                 default:
2982                         break;
2983                 }
2984         }
2985 }
2986
2987 /**
2988  * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
2989  * @phba: Pointer to HBA context object.
2990  * @pring: Pointer to driver SLI ring object.
2991  *
2992  * This function aborts all iocbs in the given ring and frees all the iocb
2993  * objects in txq. This function issues an abort iocb for all the iocb commands
2994  * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
2995  * the return of this function. The caller is not required to hold any locks.
2996  **/
2997 void
2998 lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
2999 {
3000         LIST_HEAD(completions);
3001         struct lpfc_iocbq *iocb, *next_iocb;
3002
3003         if (pring->ringno == LPFC_ELS_RING) {
3004                 lpfc_fabric_abort_hba(phba);
3005         }
3006
3007         /* Error everything on txq and txcmplq
3008          * First do the txq.
3009          */
3010         spin_lock_irq(&phba->hbalock);
3011         list_splice_init(&pring->txq, &completions);
3012         pring->txq_cnt = 0;
3013
3014         /* Next issue ABTS for everything on the txcmplq */
3015         list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3016                 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3017
3018         spin_unlock_irq(&phba->hbalock);
3019
3020         /* Cancel all the IOCBs from the completions list */
3021         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3022                               IOERR_SLI_ABORTED);
3023 }
3024
3025 /**
3026  * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
3027  * @phba: Pointer to HBA context object.
3028  *
3029  * This function flushes all iocbs in the fcp ring and frees all the iocb
3030  * objects in txq and txcmplq. This function will not issue abort iocbs
3031  * for all the iocb commands in txcmplq, they will just be returned with
3032  * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3033  * slot has been permanently disabled.
3034  **/
3035 void
3036 lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
3037 {
3038         LIST_HEAD(txq);
3039         LIST_HEAD(txcmplq);
3040         struct lpfc_sli *psli = &phba->sli;
3041         struct lpfc_sli_ring  *pring;
3042
3043         /* Currently, only one fcp ring */
3044         pring = &psli->ring[psli->fcp_ring];
3045
3046         spin_lock_irq(&phba->hbalock);
3047         /* Retrieve everything on txq */
3048         list_splice_init(&pring->txq, &txq);
3049         pring->txq_cnt = 0;
3050
3051         /* Retrieve everything on the txcmplq */
3052         list_splice_init(&pring->txcmplq, &txcmplq);
3053         pring->txcmplq_cnt = 0;
3054         spin_unlock_irq(&phba->hbalock);
3055
3056         /* Flush the txq */
3057         lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
3058                               IOERR_SLI_DOWN);
3059
3060         /* Flush the txcmpq */
3061         lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
3062                               IOERR_SLI_DOWN);
3063 }
3064
3065 /**
3066  * lpfc_sli_brdready_s3 - Check for sli3 host ready status
3067  * @phba: Pointer to HBA context object.
3068  * @mask: Bit mask to be checked.
3069  *
3070  * This function reads the host status register and compares
3071  * with the provided bit mask to check if HBA completed
3072  * the restart. This function will wait in a loop for the
3073  * HBA to complete restart. If the HBA does not restart within
3074  * 15 iterations, the function will reset the HBA again. The
3075  * function returns 1 when HBA fail to restart otherwise returns
3076  * zero.
3077  **/
3078 static int
3079 lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
3080 {
3081         uint32_t status;
3082         int i = 0;
3083         int retval = 0;
3084
3085         /* Read the HBA Host Status Register */
3086         status = readl(phba->HSregaddr);
3087
3088         /*
3089          * Check status register every 100ms for 5 retries, then every
3090          * 500ms for 5, then every 2.5 sec for 5, then reset board and
3091          * every 2.5 sec for 4.
3092          * Break our of the loop if errors occurred during init.
3093          */
3094         while (((status & mask) != mask) &&
3095                !(status & HS_FFERM) &&
3096                i++ < 20) {
3097
3098                 if (i <= 5)
3099                         msleep(10);
3100                 else if (i <= 10)
3101                         msleep(500);
3102                 else
3103                         msleep(2500);
3104
3105                 if (i == 15) {
3106                                 /* Do post */
3107                         phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3108                         lpfc_sli_brdrestart(phba);
3109                 }
3110                 /* Read the HBA Host Status Register */
3111                 status = readl(phba->HSregaddr);
3112         }
3113
3114         /* Check to see if any errors occurred during init */
3115         if ((status & HS_FFERM) || (i >= 20)) {
3116                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3117                                 "2751 Adapter failed to restart, "
3118                                 "status reg x%x, FW Data: A8 x%x AC x%x\n",
3119                                 status,
3120                                 readl(phba->MBslimaddr + 0xa8),
3121                                 readl(phba->MBslimaddr + 0xac));
3122                 phba->link_state = LPFC_HBA_ERROR;
3123                 retval = 1;
3124         }
3125
3126         return retval;
3127 }
3128
3129 /**
3130  * lpfc_sli_brdready_s4 - Check for sli4 host ready status
3131  * @phba: Pointer to HBA context object.
3132  * @mask: Bit mask to be checked.
3133  *
3134  * This function checks the host status register to check if HBA is
3135  * ready. This function will wait in a loop for the HBA to be ready
3136  * If the HBA is not ready , the function will will reset the HBA PCI
3137  * function again. The function returns 1 when HBA fail to be ready
3138  * otherwise returns zero.
3139  **/
3140 static int
3141 lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
3142 {
3143         uint32_t status;
3144         int retval = 0;
3145
3146         /* Read the HBA Host Status Register */
3147         status = lpfc_sli4_post_status_check(phba);
3148
3149         if (status) {
3150                 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3151                 lpfc_sli_brdrestart(phba);
3152                 status = lpfc_sli4_post_status_check(phba);
3153         }
3154
3155         /* Check to see if any errors occurred during init */
3156         if (status) {
3157                 phba->link_state = LPFC_HBA_ERROR;
3158                 retval = 1;
3159         } else
3160                 phba->sli4_hba.intr_enable = 0;
3161
3162         return retval;
3163 }
3164
3165 /**
3166  * lpfc_sli_brdready - Wrapper func for checking the hba readyness
3167  * @phba: Pointer to HBA context object.
3168  * @mask: Bit mask to be checked.
3169  *
3170  * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
3171  * from the API jump table function pointer from the lpfc_hba struct.
3172  **/
3173 int
3174 lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
3175 {
3176         return phba->lpfc_sli_brdready(phba, mask);
3177 }
3178
3179 #define BARRIER_TEST_PATTERN (0xdeadbeef)
3180
3181 /**
3182  * lpfc_reset_barrier - Make HBA ready for HBA reset
3183  * @phba: Pointer to HBA context object.
3184  *
3185  * This function is called before resetting an HBA. This
3186  * function requests HBA to quiesce DMAs before a reset.
3187  **/
3188 void lpfc_reset_barrier(struct lpfc_hba *phba)
3189 {
3190         uint32_t __iomem *resp_buf;
3191         uint32_t __iomem *mbox_buf;
3192         volatile uint32_t mbox;
3193         uint32_t hc_copy;
3194         int  i;
3195         uint8_t hdrtype;
3196
3197         pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
3198         if (hdrtype != 0x80 ||
3199             (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
3200              FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
3201                 return;
3202
3203         /*
3204          * Tell the other part of the chip to suspend temporarily all
3205          * its DMA activity.
3206          */
3207         resp_buf = phba->MBslimaddr;
3208
3209         /* Disable the error attention */
3210         hc_copy = readl(phba->HCregaddr);
3211         writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
3212         readl(phba->HCregaddr); /* flush */
3213         phba->link_flag |= LS_IGNORE_ERATT;
3214
3215         if (readl(phba->HAregaddr) & HA_ERATT) {
3216                 /* Clear Chip error bit */
3217                 writel(HA_ERATT, phba->HAregaddr);
3218                 phba->pport->stopped = 1;
3219         }
3220
3221         mbox = 0;
3222         ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
3223         ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
3224
3225         writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
3226         mbox_buf = phba->MBslimaddr;
3227         writel(mbox, mbox_buf);
3228
3229         for (i = 0;
3230              readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN) && i < 50; i++)
3231                 mdelay(1);
3232
3233         if (readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN)) {
3234                 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
3235                     phba->pport->stopped)
3236                         goto restore_hc;
3237                 else
3238                         goto clear_errat;
3239         }
3240
3241         ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
3242         for (i = 0; readl(resp_buf) != mbox &&  i < 500; i++)
3243                 mdelay(1);
3244
3245 clear_errat:
3246
3247         while (!(readl(phba->HAregaddr) & HA_ERATT) && ++i < 500)
3248                 mdelay(1);
3249
3250         if (readl(phba->HAregaddr) & HA_ERATT) {
3251                 writel(HA_ERATT, phba->HAregaddr);
3252                 phba->pport->stopped = 1;
3253         }
3254
3255 restore_hc:
3256         phba->link_flag &= ~LS_IGNORE_ERATT;
3257         writel(hc_copy, phba->HCregaddr);
3258         readl(phba->HCregaddr); /* flush */
3259 }
3260
3261 /**
3262  * lpfc_sli_brdkill - Issue a kill_board mailbox command
3263  * @phba: Pointer to HBA context object.
3264  *
3265  * This function issues a kill_board mailbox command and waits for
3266  * the error attention interrupt. This function is called for stopping
3267  * the firmware processing. The caller is not required to hold any
3268  * locks. This function calls lpfc_hba_down_post function to free
3269  * any pending commands after the kill. The function will return 1 when it
3270  * fails to kill the board else will return 0.
3271  **/
3272 int
3273 lpfc_sli_brdkill(struct lpfc_hba *phba)
3274 {
3275         struct lpfc_sli *psli;
3276         LPFC_MBOXQ_t *pmb;
3277         uint32_t status;
3278         uint32_t ha_copy;
3279         int retval;
3280         int i = 0;
3281
3282         psli = &phba->sli;
3283
3284         /* Kill HBA */
3285         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3286                         "0329 Kill HBA Data: x%x x%x\n",
3287                         phba->pport->port_state, psli->sli_flag);
3288
3289         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3290         if (!pmb)
3291                 return 1;
3292
3293         /* Disable the error attention */
3294         spin_lock_irq(&phba->hbalock);
3295         status = readl(phba->HCregaddr);
3296         status &= ~HC_ERINT_ENA;
3297         writel(status, phba->HCregaddr);
3298         readl(phba->HCregaddr); /* flush */
3299         phba->link_flag |= LS_IGNORE_ERATT;
3300         spin_unlock_irq(&phba->hbalock);
3301
3302         lpfc_kill_board(phba, pmb);
3303         pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3304         retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3305
3306         if (retval != MBX_SUCCESS) {
3307                 if (retval != MBX_BUSY)
3308                         mempool_free(pmb, phba->mbox_mem_pool);
3309                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3310                                 "2752 KILL_BOARD command failed retval %d\n",
3311                                 retval);
3312                 spin_lock_irq(&phba->hbalock);
3313                 phba->link_flag &= ~LS_IGNORE_ERATT;
3314                 spin_unlock_irq(&phba->hbalock);
3315                 return 1;
3316         }
3317
3318         spin_lock_irq(&phba->hbalock);
3319         psli->sli_flag &= ~LPFC_SLI_ACTIVE;
3320         spin_unlock_irq(&phba->hbalock);
3321
3322         mempool_free(pmb, phba->mbox_mem_pool);
3323
3324         /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
3325          * attention every 100ms for 3 seconds. If we don't get ERATT after
3326          * 3 seconds we still set HBA_ERROR state because the status of the
3327          * board is now undefined.
3328          */
3329         ha_copy = readl(phba->HAregaddr);
3330
3331         while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
3332                 mdelay(100);
3333                 ha_copy = readl(phba->HAregaddr);
3334         }
3335
3336         del_timer_sync(&psli->mbox_tmo);
3337         if (ha_copy & HA_ERATT) {
3338                 writel(HA_ERATT, phba->HAregaddr);
3339                 phba->pport->stopped = 1;
3340         }
3341         spin_lock_irq(&phba->hbalock);
3342         psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
3343         psli->mbox_active = NULL;
3344         phba->link_flag &= ~LS_IGNORE_ERATT;
3345         spin_unlock_irq(&phba->hbalock);
3346
3347         lpfc_hba_down_post(phba);
3348         phba->link_state = LPFC_HBA_ERROR;
3349
3350         return ha_copy & HA_ERATT ? 0 : 1;
3351 }
3352
3353 /**
3354  * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
3355  * @phba: Pointer to HBA context object.
3356  *
3357  * This function resets the HBA by writing HC_INITFF to the control
3358  * register. After the HBA resets, this function resets all the iocb ring
3359  * indices. This function disables PCI layer parity checking during
3360  * the reset.
3361  * This function returns 0 always.
3362  * The caller is not required to hold any locks.
3363  **/
3364 int
3365 lpfc_sli_brdreset(struct lpfc_hba *phba)
3366 {
3367         struct lpfc_sli *psli;
3368         struct lpfc_sli_ring *pring;
3369         uint16_t cfg_value;
3370         int i;
3371
3372         psli = &phba->sli;
3373
3374         /* Reset HBA */
3375         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3376                         "0325 Reset HBA Data: x%x x%x\n",
3377                         phba->pport->port_state, psli->sli_flag);
3378
3379         /* perform board reset */
3380         phba->fc_eventTag = 0;
3381         phba->link_events = 0;
3382         phba->pport->fc_myDID = 0;
3383         phba->pport->fc_prevDID = 0;
3384
3385         /* Turn off parity checking and serr during the physical reset */
3386         pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3387         pci_write_config_word(phba->pcidev, PCI_COMMAND,
3388                               (cfg_value &
3389                                ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3390
3391         psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
3392
3393         /* Now toggle INITFF bit in the Host Control Register */
3394         writel(HC_INITFF, phba->HCregaddr);
3395         mdelay(1);
3396         readl(phba->HCregaddr); /* flush */
3397         writel(0, phba->HCregaddr);
3398         readl(phba->HCregaddr); /* flush */
3399
3400         /* Restore PCI cmd register */
3401         pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
3402
3403         /* Initialize relevant SLI info */
3404         for (i = 0; i < psli->num_rings; i++) {
3405                 pring = &psli->ring[i];
3406                 pring->flag = 0;
3407                 pring->rspidx = 0;
3408                 pring->next_cmdidx  = 0;
3409                 pring->local_getidx = 0;
3410                 pring->cmdidx = 0;
3411                 pring->missbufcnt = 0;
3412         }
3413
3414         phba->link_state = LPFC_WARM_START;
3415         return 0;
3416 }
3417
3418 /**
3419  * lpfc_sli4_brdreset - Reset a sli-4 HBA
3420  * @phba: Pointer to HBA context object.
3421  *
3422  * This function resets a SLI4 HBA. This function disables PCI layer parity
3423  * checking during resets the device. The caller is not required to hold
3424  * any locks.
3425  *
3426  * This function returns 0 always.
3427  **/
3428 int
3429 lpfc_sli4_brdreset(struct lpfc_hba *phba)
3430 {
3431         struct lpfc_sli *psli = &phba->sli;
3432         uint16_t cfg_value;
3433         uint8_t qindx;
3434
3435         /* Reset HBA */
3436         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3437                         "0295 Reset HBA Data: x%x x%x\n",
3438                         phba->pport->port_state, psli->sli_flag);
3439
3440         /* perform board reset */
3441         phba->fc_eventTag = 0;
3442         phba->link_events = 0;
3443         phba->pport->fc_myDID = 0;
3444         phba->pport->fc_prevDID = 0;
3445
3446         /* Turn off parity checking and serr during the physical reset */
3447         pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3448         pci_write_config_word(phba->pcidev, PCI_COMMAND,
3449                               (cfg_value &
3450                               ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3451
3452         spin_lock_irq(&phba->hbalock);
3453         psli->sli_flag &= ~(LPFC_PROCESS_LA);
3454         phba->fcf.fcf_flag = 0;
3455         /* Clean up the child queue list for the CQs */
3456         list_del_init(&phba->sli4_hba.mbx_wq->list);
3457         list_del_init(&phba->sli4_hba.els_wq->list);
3458         list_del_init(&phba->sli4_hba.hdr_rq->list);
3459         list_del_init(&phba->sli4_hba.dat_rq->list);
3460         list_del_init(&phba->sli4_hba.mbx_cq->list);
3461         list_del_init(&phba->sli4_hba.els_cq->list);
3462         for (qindx = 0; qindx < phba->cfg_fcp_wq_count; qindx++)
3463                 list_del_init(&phba->sli4_hba.fcp_wq[qindx]->list);
3464         for (qindx = 0; qindx < phba->cfg_fcp_eq_count; qindx++)
3465                 list_del_init(&phba->sli4_hba.fcp_cq[qindx]->list);
3466         spin_unlock_irq(&phba->hbalock);
3467
3468         /* Now physically reset the device */
3469         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3470                         "0389 Performing PCI function reset!\n");
3471         /* Perform FCoE PCI function reset */
3472         lpfc_pci_function_reset(phba);
3473
3474         return 0;
3475 }
3476
3477 /**
3478  * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
3479  * @phba: Pointer to HBA context object.
3480  *
3481  * This function is called in the SLI initialization code path to
3482  * restart the HBA. The caller is not required to hold any lock.
3483  * This function writes MBX_RESTART mailbox command to the SLIM and
3484  * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
3485  * function to free any pending commands. The function enables
3486  * POST only during the first initialization. The function returns zero.
3487  * The function does not guarantee completion of MBX_RESTART mailbox
3488  * command before the return of this function.
3489  **/
3490 static int
3491 lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
3492 {
3493         MAILBOX_t *mb;
3494         struct lpfc_sli *psli;
3495         volatile uint32_t word0;
3496         void __iomem *to_slim;
3497         uint32_t hba_aer_enabled;
3498
3499         spin_lock_irq(&phba->hbalock);
3500
3501         /* Take PCIe device Advanced Error Reporting (AER) state */
3502         hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
3503
3504         psli = &phba->sli;
3505
3506         /* Restart HBA */
3507         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3508                         "0337 Restart HBA Data: x%x x%x\n",
3509                         phba->pport->port_state, psli->sli_flag);
3510
3511         word0 = 0;
3512         mb = (MAILBOX_t *) &word0;
3513         mb->mbxCommand = MBX_RESTART;
3514         mb->mbxHc = 1;
3515
3516         lpfc_reset_barrier(phba);
3517
3518         to_slim = phba->MBslimaddr;
3519         writel(*(uint32_t *) mb, to_slim);
3520         readl(to_slim); /* flush */
3521
3522         /* Only skip post after fc_ffinit is completed */
3523         if (phba->pport->port_state)
3524                 word0 = 1;      /* This is really setting up word1 */
3525         else
3526                 word0 = 0;      /* This is really setting up word1 */
3527         to_slim = phba->MBslimaddr + sizeof (uint32_t);
3528         writel(*(uint32_t *) mb, to_slim);
3529         readl(to_slim); /* flush */
3530
3531         lpfc_sli_brdreset(phba);
3532         phba->pport->stopped = 0;
3533         phba->link_state = LPFC_INIT_START;
3534         phba->hba_flag = 0;
3535         spin_unlock_irq(&phba->hbalock);
3536
3537         memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
3538         psli->stats_start = get_seconds();
3539
3540         /* Give the INITFF and Post time to settle. */
3541         mdelay(100);
3542
3543         /* Reset HBA AER if it was enabled, note hba_flag was reset above */
3544         if (hba_aer_enabled)
3545                 pci_disable_pcie_error_reporting(phba->pcidev);
3546
3547         lpfc_hba_down_post(phba);
3548
3549         return 0;
3550 }
3551
3552 /**
3553  * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
3554  * @phba: Pointer to HBA context object.
3555  *
3556  * This function is called in the SLI initialization code path to restart
3557  * a SLI4 HBA. The caller is not required to hold any lock.
3558  * At the end of the function, it calls lpfc_hba_down_post function to
3559  * free any pending commands.
3560  **/
3561 static int
3562 lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
3563 {
3564         struct lpfc_sli *psli = &phba->sli;
3565
3566
3567         /* Restart HBA */
3568         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3569                         "0296 Restart HBA Data: x%x x%x\n",
3570                         phba->pport->port_state, psli->sli_flag);
3571
3572         lpfc_sli4_brdreset(phba);
3573
3574         spin_lock_irq(&phba->hbalock);
3575         phba->pport->stopped = 0;
3576         phba->link_state = LPFC_INIT_START;
3577         phba->hba_flag = 0;
3578         spin_unlock_irq(&phba->hbalock);
3579
3580         memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
3581         psli->stats_start = get_seconds();
3582
3583         lpfc_hba_down_post(phba);
3584
3585         return 0;
3586 }
3587
3588 /**
3589  * lpfc_sli_brdrestart - Wrapper func for restarting hba
3590  * @phba: Pointer to HBA context object.
3591  *
3592  * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
3593  * API jump table function pointer from the lpfc_hba struct.
3594 **/
3595 int
3596 lpfc_sli_brdrestart(struct lpfc_hba *phba)
3597 {
3598         return phba->lpfc_sli_brdrestart(phba);
3599 }
3600
3601 /**
3602  * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
3603  * @phba: Pointer to HBA context object.
3604  *
3605  * This function is called after a HBA restart to wait for successful
3606  * restart of the HBA. Successful restart of the HBA is indicated by
3607  * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
3608  * iteration, the function will restart the HBA again. The function returns
3609  * zero if HBA successfully restarted else returns negative error code.
3610  **/
3611 static int
3612 lpfc_sli_chipset_init(struct lpfc_hba *phba)
3613 {
3614         uint32_t status, i = 0;
3615
3616         /* Read the HBA Host Status Register */
3617         status = readl(phba->HSregaddr);
3618
3619         /* Check status register to see what current state is */
3620         i = 0;
3621         while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
3622
3623                 /* Check every 100ms for 5 retries, then every 500ms for 5, then
3624                  * every 2.5 sec for 5, then reset board and every 2.5 sec for
3625                  * 4.
3626                  */
3627                 if (i++ >= 20) {
3628                         /* Adapter failed to init, timeout, status reg
3629                            <status> */
3630                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3631                                         "0436 Adapter failed to init, "
3632                                         "timeout, status reg x%x, "
3633                                         "FW Data: A8 x%x AC x%x\n", status,
3634                                         readl(phba->MBslimaddr + 0xa8),
3635                                         readl(phba->MBslimaddr + 0xac));
3636                         phba->link_state = LPFC_HBA_ERROR;
3637                         return -ETIMEDOUT;
3638                 }
3639
3640                 /* Check to see if any errors occurred during init */
3641                 if (status & HS_FFERM) {
3642                         /* ERROR: During chipset initialization */
3643                         /* Adapter failed to init, chipset, status reg
3644                            <status> */
3645                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3646                                         "0437 Adapter failed to init, "
3647                                         "chipset, status reg x%x, "
3648                                         "FW Data: A8 x%x AC x%x\n", status,
3649                                         readl(phba->MBslimaddr + 0xa8),
3650                                         readl(phba->MBslimaddr + 0xac));
3651                         phba->link_state = LPFC_HBA_ERROR;
3652                         return -EIO;
3653                 }
3654
3655                 if (i <= 5) {
3656                         msleep(10);
3657                 } else if (i <= 10) {
3658                         msleep(500);
3659                 } else {
3660                         msleep(2500);
3661                 }
3662
3663                 if (i == 15) {
3664                                 /* Do post */
3665                         phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3666                         lpfc_sli_brdrestart(phba);
3667                 }
3668                 /* Read the HBA Host Status Register */
3669                 status = readl(phba->HSregaddr);
3670         }
3671
3672         /* Check to see if any errors occurred during init */
3673         if (status & HS_FFERM) {
3674                 /* ERROR: During chipset initialization */
3675                 /* Adapter failed to init, chipset, status reg <status> */
3676                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3677                                 "0438 Adapter failed to init, chipset, "
3678                                 "status reg x%x, "
3679                                 "FW Data: A8 x%x AC x%x\n", status,
3680                                 readl(phba->MBslimaddr + 0xa8),
3681                                 readl(phba->MBslimaddr + 0xac));
3682                 phba->link_state = LPFC_HBA_ERROR;
3683                 return -EIO;
3684         }
3685
3686         /* Clear all interrupt enable conditions */
3687         writel(0, phba->HCregaddr);
3688         readl(phba->HCregaddr); /* flush */
3689
3690         /* setup host attn register */
3691         writel(0xffffffff, phba->HAregaddr);
3692         readl(phba->HAregaddr); /* flush */
3693         return 0;
3694 }
3695
3696 /**
3697  * lpfc_sli_hbq_count - Get the number of HBQs to be configured
3698  *
3699  * This function calculates and returns the number of HBQs required to be
3700  * configured.
3701  **/
3702 int
3703 lpfc_sli_hbq_count(void)
3704 {
3705         return ARRAY_SIZE(lpfc_hbq_defs);
3706 }
3707
3708 /**
3709  * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
3710  *
3711  * This function adds the number of hbq entries in every HBQ to get
3712  * the total number of hbq entries required for the HBA and returns
3713  * the total count.
3714  **/
3715 static int
3716 lpfc_sli_hbq_entry_count(void)
3717 {
3718         int  hbq_count = lpfc_sli_hbq_count();
3719         int  count = 0;
3720         int  i;
3721
3722         for (i = 0; i < hbq_count; ++i)
3723                 count += lpfc_hbq_defs[i]->entry_count;
3724         return count;
3725 }
3726
3727 /**
3728  * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
3729  *
3730  * This function calculates amount of memory required for all hbq entries
3731  * to be configured and returns the total memory required.
3732  **/
3733 int
3734 lpfc_sli_hbq_size(void)
3735 {
3736         return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
3737 }
3738
3739 /**
3740  * lpfc_sli_hbq_setup - configure and initialize HBQs
3741  * @phba: Pointer to HBA context object.
3742  *
3743  * This function is called during the SLI initialization to configure
3744  * all the HBQs and post buffers to the HBQ. The caller is not
3745  * required to hold any locks. This function will return zero if successful
3746  * else it will return negative error code.
3747  **/
3748 static int
3749 lpfc_sli_hbq_setup(struct lpfc_hba *phba)
3750 {
3751         int  hbq_count = lpfc_sli_hbq_count();
3752         LPFC_MBOXQ_t *pmb;
3753         MAILBOX_t *pmbox;
3754         uint32_t hbqno;
3755         uint32_t hbq_entry_index;
3756
3757                                 /* Get a Mailbox buffer to setup mailbox
3758                                  * commands for HBA initialization
3759                                  */
3760         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3761
3762         if (!pmb)
3763                 return -ENOMEM;
3764
3765         pmbox = &pmb->u.mb;
3766
3767         /* Initialize the struct lpfc_sli_hbq structure for each hbq */
3768         phba->link_state = LPFC_INIT_MBX_CMDS;
3769         phba->hbq_in_use = 1;
3770
3771         hbq_entry_index = 0;
3772         for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
3773                 phba->hbqs[hbqno].next_hbqPutIdx = 0;
3774                 phba->hbqs[hbqno].hbqPutIdx      = 0;
3775                 phba->hbqs[hbqno].local_hbqGetIdx   = 0;
3776                 phba->hbqs[hbqno].entry_count =
3777                         lpfc_hbq_defs[hbqno]->entry_count;
3778                 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
3779                         hbq_entry_index, pmb);
3780                 hbq_entry_index += phba->hbqs[hbqno].entry_count;
3781
3782                 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
3783                         /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
3784                            mbxStatus <status>, ring <num> */
3785
3786                         lpfc_printf_log(phba, KERN_ERR,
3787                                         LOG_SLI | LOG_VPORT,
3788                                         "1805 Adapter failed to init. "
3789                                         "Data: x%x x%x x%x\n",
3790                                         pmbox->mbxCommand,
3791                                         pmbox->mbxStatus, hbqno);
3792
3793                         phba->link_state = LPFC_HBA_ERROR;
3794                         mempool_free(pmb, phba->mbox_mem_pool);
3795                         return ENXIO;
3796                 }
3797         }
3798         phba->hbq_count = hbq_count;
3799
3800         mempool_free(pmb, phba->mbox_mem_pool);
3801
3802         /* Initially populate or replenish the HBQs */
3803         for (hbqno = 0; hbqno < hbq_count; ++hbqno)
3804                 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
3805         return 0;
3806 }
3807
3808 /**
3809  * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
3810  * @phba: Pointer to HBA context object.
3811  *
3812  * This function is called during the SLI initialization to configure
3813  * all the HBQs and post buffers to the HBQ. The caller is not
3814  * required to hold any locks. This function will return zero if successful
3815  * else it will return negative error code.
3816  **/
3817 static int
3818 lpfc_sli4_rb_setup(struct lpfc_hba *phba)
3819 {
3820         phba->hbq_in_use = 1;
3821         phba->hbqs[0].entry_count = lpfc_hbq_defs[0]->entry_count;
3822         phba->hbq_count = 1;
3823         /* Initially populate or replenish the HBQs */
3824         lpfc_sli_hbqbuf_init_hbqs(phba, 0);
3825         return 0;
3826 }
3827
3828 /**
3829  * lpfc_sli_config_port - Issue config port mailbox command
3830  * @phba: Pointer to HBA context object.
3831  * @sli_mode: sli mode - 2/3
3832  *
3833  * This function is called by the sli intialization code path
3834  * to issue config_port mailbox command. This function restarts the
3835  * HBA firmware and issues a config_port mailbox command to configure
3836  * the SLI interface in the sli mode specified by sli_mode
3837  * variable. The caller is not required to hold any locks.
3838  * The function returns 0 if successful, else returns negative error
3839  * code.
3840  **/
3841 int
3842 lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
3843 {
3844         LPFC_MBOXQ_t *pmb;
3845         uint32_t resetcount = 0, rc = 0, done = 0;
3846
3847         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3848         if (!pmb) {
3849                 phba->link_state = LPFC_HBA_ERROR;
3850                 return -ENOMEM;
3851         }
3852
3853         phba->sli_rev = sli_mode;
3854         while (resetcount < 2 && !done) {
3855                 spin_lock_irq(&phba->hbalock);
3856                 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
3857                 spin_unlock_irq(&phba->hbalock);
3858                 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3859                 lpfc_sli_brdrestart(phba);
3860                 rc = lpfc_sli_chipset_init(phba);
3861                 if (rc)
3862                         break;
3863
3864                 spin_lock_irq(&phba->hbalock);
3865                 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
3866                 spin_unlock_irq(&phba->hbalock);
3867                 resetcount++;
3868
3869                 /* Call pre CONFIG_PORT mailbox command initialization.  A
3870                  * value of 0 means the call was successful.  Any other
3871                  * nonzero value is a failure, but if ERESTART is returned,
3872                  * the driver may reset the HBA and try again.
3873                  */
3874                 rc = lpfc_config_port_prep(phba);
3875                 if (rc == -ERESTART) {
3876                         phba->link_state = LPFC_LINK_UNKNOWN;
3877                         continue;
3878                 } else if (rc)
3879                         break;
3880                 phba->link_state = LPFC_INIT_MBX_CMDS;
3881                 lpfc_config_port(phba, pmb);
3882                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
3883                 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
3884                                         LPFC_SLI3_HBQ_ENABLED |
3885                                         LPFC_SLI3_CRP_ENABLED |
3886                                         LPFC_SLI3_INB_ENABLED |
3887                                         LPFC_SLI3_BG_ENABLED);
3888                 if (rc != MBX_SUCCESS) {
3889                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3890                                 "0442 Adapter failed to init, mbxCmd x%x "
3891                                 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
3892                                 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
3893                         spin_lock_irq(&phba->hbalock);
3894                         phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
3895                         spin_unlock_irq(&phba->hbalock);
3896                         rc = -ENXIO;
3897                 } else {
3898                         /* Allow asynchronous mailbox command to go through */
3899                         spin_lock_irq(&phba->hbalock);
3900                         phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
3901                         spin_unlock_irq(&phba->hbalock);
3902                         done = 1;
3903                 }
3904         }
3905         if (!done) {
3906                 rc = -EINVAL;
3907                 goto do_prep_failed;
3908         }
3909         if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
3910                 if (!pmb->u.mb.un.varCfgPort.cMA) {
3911                         rc = -ENXIO;
3912                         goto do_prep_failed;
3913                 }
3914                 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
3915                         phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
3916                         phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
3917                         phba->max_vports = (phba->max_vpi > phba->max_vports) ?
3918                                 phba->max_vpi : phba->max_vports;
3919
3920                 } else
3921                         phba->max_vpi = 0;
3922                 if (pmb->u.mb.un.varCfgPort.gdss)
3923                         phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
3924                 if (pmb->u.mb.un.varCfgPort.gerbm)
3925                         phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
3926                 if (pmb->u.mb.un.varCfgPort.gcrp)
3927                         phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
3928                 if (pmb->u.mb.un.varCfgPort.ginb) {
3929                         phba->sli3_options |= LPFC_SLI3_INB_ENABLED;
3930                         phba->hbq_get = phba->mbox->us.s3_inb_pgp.hbq_get;
3931                         phba->port_gp = phba->mbox->us.s3_inb_pgp.port;
3932                         phba->inb_ha_copy = &phba->mbox->us.s3_inb_pgp.ha_copy;
3933                         phba->inb_counter = &phba->mbox->us.s3_inb_pgp.counter;
3934                         phba->inb_last_counter =
3935                                         phba->mbox->us.s3_inb_pgp.counter;
3936                 } else {
3937                         phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
3938                         phba->port_gp = phba->mbox->us.s3_pgp.port;
3939                         phba->inb_ha_copy = NULL;
3940                         phba->inb_counter = NULL;
3941                 }
3942
3943                 if (phba->cfg_enable_bg) {
3944                         if (pmb->u.mb.un.varCfgPort.gbg)
3945                                 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
3946                         else
3947                                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3948                                                 "0443 Adapter did not grant "
3949                                                 "BlockGuard\n");
3950                 }
3951         } else {
3952                 phba->hbq_get = NULL;
3953                 phba->port_gp = phba->mbox->us.s2.port;
3954                 phba->inb_ha_copy = NULL;
3955                 phba->inb_counter = NULL;
3956                 phba->max_vpi = 0;
3957         }
3958 do_prep_failed:
3959         mempool_free(pmb, phba->mbox_mem_pool);
3960         return rc;
3961 }
3962
3963
3964 /**
3965  * lpfc_sli_hba_setup - SLI intialization function
3966  * @phba: Pointer to HBA context object.
3967  *
3968  * This function is the main SLI intialization function. This function
3969  * is called by the HBA intialization code, HBA reset code and HBA
3970  * error attention handler code. Caller is not required to hold any
3971  * locks. This function issues config_port mailbox command to configure
3972  * the SLI, setup iocb rings and HBQ rings. In the end the function
3973  * calls the config_port_post function to issue init_link mailbox
3974  * command and to start the discovery. The function will return zero
3975  * if successful, else it will return negative error code.
3976  **/
3977 int
3978 lpfc_sli_hba_setup(struct lpfc_hba *phba)
3979 {
3980         uint32_t rc;
3981         int  mode = 3;
3982
3983         switch (lpfc_sli_mode) {
3984         case 2:
3985                 if (phba->cfg_enable_npiv) {
3986                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
3987                                 "1824 NPIV enabled: Override lpfc_sli_mode "
3988                                 "parameter (%d) to auto (0).\n",
3989                                 lpfc_sli_mode);
3990                         break;
3991                 }
3992                 mode = 2;
3993                 break;
3994         case 0:
3995         case 3:
3996                 break;
3997         default:
3998                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
3999                                 "1819 Unrecognized lpfc_sli_mode "
4000                                 "parameter: %d.\n", lpfc_sli_mode);
4001
4002                 break;
4003         }
4004
4005         rc = lpfc_sli_config_port(phba, mode);
4006
4007         if (rc && lpfc_sli_mode == 3)
4008                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
4009                                 "1820 Unable to select SLI-3.  "
4010                                 "Not supported by adapter.\n");
4011         if (rc && mode != 2)
4012                 rc = lpfc_sli_config_port(phba, 2);
4013         if (rc)
4014                 goto lpfc_sli_hba_setup_error;
4015
4016         /* Enable PCIe device Advanced Error Reporting (AER) if configured */
4017         if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
4018                 rc = pci_enable_pcie_error_reporting(phba->pcidev);
4019                 if (!rc) {
4020                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4021                                         "2709 This device supports "
4022                                         "Advanced Error Reporting (AER)\n");
4023                         spin_lock_irq(&phba->hbalock);
4024                         phba->hba_flag |= HBA_AER_ENABLED;
4025                         spin_unlock_irq(&phba->hbalock);
4026                 } else {
4027                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4028                                         "2708 This device does not support "
4029                                         "Advanced Error Reporting (AER)\n");
4030                         phba->cfg_aer_support = 0;
4031                 }
4032         }
4033
4034         if (phba->sli_rev == 3) {
4035                 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
4036                 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
4037         } else {
4038                 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
4039                 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
4040                 phba->sli3_options = 0;
4041         }
4042
4043         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4044                         "0444 Firmware in SLI %x mode. Max_vpi %d\n",
4045                         phba->sli_rev, phba->max_vpi);
4046         rc = lpfc_sli_ring_map(phba);
4047
4048         if (rc)
4049                 goto lpfc_sli_hba_setup_error;
4050
4051         /* Init HBQs */
4052         if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
4053                 rc = lpfc_sli_hbq_setup(phba);
4054                 if (rc)
4055                         goto lpfc_sli_hba_setup_error;
4056         }
4057         spin_lock_irq(&phba->hbalock);
4058         phba->sli.sli_flag |= LPFC_PROCESS_LA;
4059         spin_unlock_irq(&phba->hbalock);
4060
4061         rc = lpfc_config_port_post(phba);
4062         if (rc)
4063                 goto lpfc_sli_hba_setup_error;
4064
4065         return rc;
4066
4067 lpfc_sli_hba_setup_error:
4068         phba->link_state = LPFC_HBA_ERROR;
4069         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4070                         "0445 Firmware initialization failed\n");
4071         return rc;
4072 }
4073
4074 /**
4075  * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
4076  * @phba: Pointer to HBA context object.
4077  * @mboxq: mailbox pointer.
4078  * This function issue a dump mailbox command to read config region
4079  * 23 and parse the records in the region and populate driver
4080  * data structure.
4081  **/
4082 static int
4083 lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba,
4084                 LPFC_MBOXQ_t *mboxq)
4085 {
4086         struct lpfc_dmabuf *mp;
4087         struct lpfc_mqe *mqe;
4088         uint32_t data_length;
4089         int rc;
4090
4091         /* Program the default value of vlan_id and fc_map */
4092         phba->valid_vlan = 0;
4093         phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4094         phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4095         phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
4096
4097         mqe = &mboxq->u.mqe;
4098         if (lpfc_dump_fcoe_param(phba, mboxq))
4099                 return -ENOMEM;
4100
4101         mp = (struct lpfc_dmabuf *) mboxq->context1;
4102         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4103
4104         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4105                         "(%d):2571 Mailbox cmd x%x Status x%x "
4106                         "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4107                         "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4108                         "CQ: x%x x%x x%x x%x\n",
4109                         mboxq->vport ? mboxq->vport->vpi : 0,
4110                         bf_get(lpfc_mqe_command, mqe),
4111                         bf_get(lpfc_mqe_status, mqe),
4112                         mqe->un.mb_words[0], mqe->un.mb_words[1],
4113                         mqe->un.mb_words[2], mqe->un.mb_words[3],
4114                         mqe->un.mb_words[4], mqe->un.mb_words[5],
4115                         mqe->un.mb_words[6], mqe->un.mb_words[7],
4116                         mqe->un.mb_words[8], mqe->un.mb_words[9],
4117                         mqe->un.mb_words[10], mqe->un.mb_words[11],
4118                         mqe->un.mb_words[12], mqe->un.mb_words[13],
4119                         mqe->un.mb_words[14], mqe->un.mb_words[15],
4120                         mqe->un.mb_words[16], mqe->un.mb_words[50],
4121                         mboxq->mcqe.word0,
4122                         mboxq->mcqe.mcqe_tag0,  mboxq->mcqe.mcqe_tag1,
4123                         mboxq->mcqe.trailer);
4124
4125         if (rc) {
4126                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4127                 kfree(mp);
4128                 return -EIO;
4129         }
4130         data_length = mqe->un.mb_words[5];
4131         if (data_length > DMP_RGN23_SIZE) {
4132                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4133                 kfree(mp);
4134                 return -EIO;
4135         }
4136
4137         lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
4138         lpfc_mbuf_free(phba, mp->virt, mp->phys);
4139         kfree(mp);
4140         return 0;
4141 }
4142
4143 /**
4144  * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
4145  * @phba: pointer to lpfc hba data structure.
4146  * @mboxq: pointer to the LPFC_MBOXQ_t structure.
4147  * @vpd: pointer to the memory to hold resulting port vpd data.
4148  * @vpd_size: On input, the number of bytes allocated to @vpd.
4149  *            On output, the number of data bytes in @vpd.
4150  *
4151  * This routine executes a READ_REV SLI4 mailbox command.  In
4152  * addition, this routine gets the port vpd data.
4153  *
4154  * Return codes
4155  *      0 - successful
4156  *      ENOMEM - could not allocated memory.
4157  **/
4158 static int
4159 lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
4160                     uint8_t *vpd, uint32_t *vpd_size)
4161 {
4162         int rc = 0;
4163         uint32_t dma_size;
4164         struct lpfc_dmabuf *dmabuf;
4165         struct lpfc_mqe *mqe;
4166
4167         dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4168         if (!dmabuf)
4169                 return -ENOMEM;
4170
4171         /*
4172          * Get a DMA buffer for the vpd data resulting from the READ_REV
4173          * mailbox command.
4174          */
4175         dma_size = *vpd_size;
4176         dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
4177                                           dma_size,
4178                                           &dmabuf->phys,
4179                                           GFP_KERNEL);
4180         if (!dmabuf->virt) {
4181                 kfree(dmabuf);
4182                 return -ENOMEM;
4183         }
4184         memset(dmabuf->virt, 0, dma_size);
4185
4186         /*
4187          * The SLI4 implementation of READ_REV conflicts at word1,
4188          * bits 31:16 and SLI4 adds vpd functionality not present
4189          * in SLI3.  This code corrects the conflicts.
4190          */
4191         lpfc_read_rev(phba, mboxq);
4192         mqe = &mboxq->u.mqe;
4193         mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
4194         mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
4195         mqe->un.read_rev.word1 &= 0x0000FFFF;
4196         bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
4197         bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
4198
4199         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4200         if (rc) {
4201                 dma_free_coherent(&phba->pcidev->dev, dma_size,
4202                                   dmabuf->virt, dmabuf->phys);
4203                 kfree(dmabuf);
4204                 return -EIO;
4205         }
4206
4207         /*
4208          * The available vpd length cannot be bigger than the
4209          * DMA buffer passed to the port.  Catch the less than
4210          * case and update the caller's size.
4211          */
4212         if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
4213                 *vpd_size = mqe->un.read_rev.avail_vpd_len;
4214
4215         lpfc_sli_pcimem_bcopy(dmabuf->virt, vpd, *vpd_size);
4216         dma_free_coherent(&phba->pcidev->dev, dma_size,
4217                           dmabuf->virt, dmabuf->phys);
4218         kfree(dmabuf);
4219         return 0;
4220 }
4221
4222 /**
4223  * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
4224  * @phba: pointer to lpfc hba data structure.
4225  *
4226  * This routine is called to explicitly arm the SLI4 device's completion and
4227  * event queues
4228  **/
4229 static void
4230 lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
4231 {
4232         uint8_t fcp_eqidx;
4233
4234         lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
4235         lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
4236         for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++)
4237                 lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[fcp_eqidx],
4238                                      LPFC_QUEUE_REARM);
4239         lpfc_sli4_eq_release(phba->sli4_hba.sp_eq, LPFC_QUEUE_REARM);
4240         for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++)
4241                 lpfc_sli4_eq_release(phba->sli4_hba.fp_eq[fcp_eqidx],
4242                                      LPFC_QUEUE_REARM);
4243 }
4244
4245 /**
4246  * lpfc_sli4_hba_setup - SLI4 device intialization PCI function
4247  * @phba: Pointer to HBA context object.
4248  *
4249  * This function is the main SLI4 device intialization PCI function. This
4250  * function is called by the HBA intialization code, HBA reset code and
4251  * HBA error attention handler code. Caller is not required to hold any
4252  * locks.
4253  **/
4254 int
4255 lpfc_sli4_hba_setup(struct lpfc_hba *phba)
4256 {
4257         int rc;
4258         LPFC_MBOXQ_t *mboxq;
4259         struct lpfc_mqe *mqe;
4260         uint8_t *vpd;
4261         uint32_t vpd_size;
4262         uint32_t ftr_rsp = 0;
4263         struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
4264         struct lpfc_vport *vport = phba->pport;
4265         struct lpfc_dmabuf *mp;
4266
4267         /* Perform a PCI function reset to start from clean */
4268         rc = lpfc_pci_function_reset(phba);
4269         if (unlikely(rc))
4270                 return -ENODEV;
4271
4272         /* Check the HBA Host Status Register for readyness */
4273         rc = lpfc_sli4_post_status_check(phba);
4274         if (unlikely(rc))
4275                 return -ENODEV;
4276         else {
4277                 spin_lock_irq(&phba->hbalock);
4278                 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
4279                 spin_unlock_irq(&phba->hbalock);
4280         }
4281
4282         /*
4283          * Allocate a single mailbox container for initializing the
4284          * port.
4285          */
4286         mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4287         if (!mboxq)
4288                 return -ENOMEM;
4289
4290         /*
4291          * Continue initialization with default values even if driver failed
4292          * to read FCoE param config regions
4293          */
4294         if (lpfc_sli4_read_fcoe_params(phba, mboxq))
4295                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
4296                         "2570 Failed to read FCoE parameters\n");
4297
4298         /* Issue READ_REV to collect vpd and FW information. */
4299         vpd_size = PAGE_SIZE;
4300         vpd = kzalloc(vpd_size, GFP_KERNEL);
4301         if (!vpd) {
4302                 rc = -ENOMEM;
4303                 goto out_free_mbox;
4304         }
4305
4306         rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
4307         if (unlikely(rc))
4308                 goto out_free_vpd;
4309
4310         mqe = &mboxq->u.mqe;
4311         phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
4312         if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev))
4313                 phba->hba_flag |= HBA_FCOE_SUPPORT;
4314
4315         if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
4316                 LPFC_DCBX_CEE_MODE)
4317                 phba->hba_flag |= HBA_FIP_SUPPORT;
4318         else
4319                 phba->hba_flag &= ~HBA_FIP_SUPPORT;
4320
4321         if (phba->sli_rev != LPFC_SLI_REV4 ||
4322             !(phba->hba_flag & HBA_FCOE_SUPPORT)) {
4323                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4324                         "0376 READ_REV Error. SLI Level %d "
4325                         "FCoE enabled %d\n",
4326                         phba->sli_rev, phba->hba_flag & HBA_FCOE_SUPPORT);
4327                 rc = -EIO;
4328                 goto out_free_vpd;
4329         }
4330         /*
4331          * Evaluate the read rev and vpd data. Populate the driver
4332          * state with the results. If this routine fails, the failure
4333          * is not fatal as the driver will use generic values.
4334          */
4335         rc = lpfc_parse_vpd(phba, vpd, vpd_size);
4336         if (unlikely(!rc)) {
4337                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4338                                 "0377 Error %d parsing vpd. "
4339                                 "Using defaults.\n", rc);
4340                 rc = 0;
4341         }
4342
4343         /* Save information as VPD data */
4344         phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
4345         phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
4346         phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
4347         phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
4348                                          &mqe->un.read_rev);
4349         phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
4350                                        &mqe->un.read_rev);
4351         phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
4352                                             &mqe->un.read_rev);
4353         phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
4354                                            &mqe->un.read_rev);
4355         phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
4356         memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
4357         phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
4358         memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
4359         phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
4360         memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
4361         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4362                         "(%d):0380 READ_REV Status x%x "
4363                         "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
4364                         mboxq->vport ? mboxq->vport->vpi : 0,
4365                         bf_get(lpfc_mqe_status, mqe),
4366                         phba->vpd.rev.opFwName,
4367                         phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
4368                         phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
4369
4370         /*
4371          * Discover the port's supported feature set and match it against the
4372          * hosts requests.
4373          */
4374         lpfc_request_features(phba, mboxq);
4375         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4376         if (unlikely(rc)) {
4377                 rc = -EIO;
4378                 goto out_free_vpd;
4379         }
4380
4381         /*
4382          * The port must support FCP initiator mode as this is the
4383          * only mode running in the host.
4384          */
4385         if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
4386                 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
4387                                 "0378 No support for fcpi mode.\n");
4388                 ftr_rsp++;
4389         }
4390
4391         /*
4392          * If the port cannot support the host's requested features
4393          * then turn off the global config parameters to disable the
4394          * feature in the driver.  This is not a fatal error.
4395          */
4396         if ((phba->cfg_enable_bg) &&
4397             !(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
4398                 ftr_rsp++;
4399
4400         if (phba->max_vpi && phba->cfg_enable_npiv &&
4401             !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
4402                 ftr_rsp++;
4403
4404         if (ftr_rsp) {
4405                 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
4406                                 "0379 Feature Mismatch Data: x%08x %08x "
4407                                 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
4408                                 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
4409                                 phba->cfg_enable_npiv, phba->max_vpi);
4410                 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
4411                         phba->cfg_enable_bg = 0;
4412                 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
4413                         phba->cfg_enable_npiv = 0;
4414         }
4415
4416         /* These SLI3 features are assumed in SLI4 */
4417         spin_lock_irq(&phba->hbalock);
4418         phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
4419         spin_unlock_irq(&phba->hbalock);
4420
4421         /* Read the port's service parameters. */
4422         rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
4423         if (rc) {
4424                 phba->link_state = LPFC_HBA_ERROR;
4425                 rc = -ENOMEM;
4426                 goto out_free_vpd;
4427         }
4428
4429         mboxq->vport = vport;
4430         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4431         mp = (struct lpfc_dmabuf *) mboxq->context1;
4432         if (rc == MBX_SUCCESS) {
4433                 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
4434                 rc = 0;
4435         }
4436
4437         /*
4438          * This memory was allocated by the lpfc_read_sparam routine. Release
4439          * it to the mbuf pool.
4440          */
4441         lpfc_mbuf_free(phba, mp->virt, mp->phys);
4442         kfree(mp);
4443         mboxq->context1 = NULL;
4444         if (unlikely(rc)) {
4445                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4446                                 "0382 READ_SPARAM command failed "
4447                                 "status %d, mbxStatus x%x\n",
4448                                 rc, bf_get(lpfc_mqe_status, mqe));
4449                 phba->link_state = LPFC_HBA_ERROR;
4450                 rc = -EIO;
4451                 goto out_free_vpd;
4452         }
4453
4454         if (phba->cfg_soft_wwnn)
4455                 u64_to_wwn(phba->cfg_soft_wwnn,
4456                            vport->fc_sparam.nodeName.u.wwn);
4457         if (phba->cfg_soft_wwpn)
4458                 u64_to_wwn(phba->cfg_soft_wwpn,
4459                            vport->fc_sparam.portName.u.wwn);
4460         memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
4461                sizeof(struct lpfc_name));
4462         memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
4463                sizeof(struct lpfc_name));
4464
4465         /* Update the fc_host data structures with new wwn. */
4466         fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
4467         fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
4468
4469         /* Register SGL pool to the device using non-embedded mailbox command */
4470         rc = lpfc_sli4_post_sgl_list(phba);
4471         if (unlikely(rc)) {
4472                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4473                                 "0582 Error %d during sgl post operation\n",
4474                                         rc);
4475                 rc = -ENODEV;
4476                 goto out_free_vpd;
4477         }
4478
4479         /* Register SCSI SGL pool to the device */
4480         rc = lpfc_sli4_repost_scsi_sgl_list(phba);
4481         if (unlikely(rc)) {
4482                 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
4483                                 "0383 Error %d during scsi sgl post "
4484                                 "operation\n", rc);
4485                 /* Some Scsi buffers were moved to the abort scsi list */
4486                 /* A pci function reset will repost them */
4487                 rc = -ENODEV;
4488                 goto out_free_vpd;
4489         }
4490
4491         /* Post the rpi header region to the device. */
4492         rc = lpfc_sli4_post_all_rpi_hdrs(phba);
4493         if (unlikely(rc)) {
4494                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4495                                 "0393 Error %d during rpi post operation\n",
4496                                 rc);
4497                 rc = -ENODEV;
4498                 goto out_free_vpd;
4499         }
4500
4501         /* Set up all the queues to the device */
4502         rc = lpfc_sli4_queue_setup(phba);
4503         if (unlikely(rc)) {
4504                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4505                                 "0381 Error %d during queue setup.\n ", rc);
4506                 goto out_stop_timers;
4507         }
4508
4509         /* Arm the CQs and then EQs on device */
4510         lpfc_sli4_arm_cqeq_intr(phba);
4511
4512         /* Indicate device interrupt mode */
4513         phba->sli4_hba.intr_enable = 1;
4514
4515         /* Allow asynchronous mailbox command to go through */
4516         spin_lock_irq(&phba->hbalock);
4517         phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4518         spin_unlock_irq(&phba->hbalock);
4519
4520         /* Post receive buffers to the device */
4521         lpfc_sli4_rb_setup(phba);
4522
4523         /* Reset HBA FCF states after HBA reset */
4524         phba->fcf.fcf_flag = 0;
4525         phba->fcf.current_rec.flag = 0;
4526
4527         /* Start the ELS watchdog timer */
4528         mod_timer(&vport->els_tmofunc,
4529                   jiffies + HZ * (phba->fc_ratov * 2));
4530
4531         /* Start heart beat timer */
4532         mod_timer(&phba->hb_tmofunc,
4533                   jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
4534         phba->hb_outstanding = 0;
4535         phba->last_completion_time = jiffies;
4536
4537         /* Start error attention (ERATT) polling timer */
4538         mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
4539
4540         /*
4541          * The port is ready, set the host's link state to LINK_DOWN
4542          * in preparation for link interrupts.
4543          */
4544         lpfc_init_link(phba, mboxq, phba->cfg_topology, phba->cfg_link_speed);
4545         mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4546         lpfc_set_loopback_flag(phba);
4547         /* Change driver state to LPFC_LINK_DOWN right before init link */
4548         spin_lock_irq(&phba->hbalock);
4549         phba->link_state = LPFC_LINK_DOWN;
4550         spin_unlock_irq(&phba->hbalock);
4551         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
4552         if (unlikely(rc != MBX_NOT_FINISHED)) {
4553                 kfree(vpd);
4554                 return 0;
4555         } else
4556                 rc = -EIO;
4557
4558         /* Unset all the queues set up in this routine when error out */
4559         if (rc)
4560                 lpfc_sli4_queue_unset(phba);
4561
4562 out_stop_timers:
4563         if (rc)
4564                 lpfc_stop_hba_timers(phba);
4565 out_free_vpd:
4566         kfree(vpd);
4567 out_free_mbox:
4568         mempool_free(mboxq, phba->mbox_mem_pool);
4569         return rc;
4570 }
4571
4572 /**
4573  * lpfc_mbox_timeout - Timeout call back function for mbox timer
4574  * @ptr: context object - pointer to hba structure.
4575  *
4576  * This is the callback function for mailbox timer. The mailbox
4577  * timer is armed when a new mailbox command is issued and the timer
4578  * is deleted when the mailbox complete. The function is called by
4579  * the kernel timer code when a mailbox does not complete within
4580  * expected time. This function wakes up the worker thread to
4581  * process the mailbox timeout and returns. All the processing is
4582  * done by the worker thread function lpfc_mbox_timeout_handler.
4583  **/
4584 void
4585 lpfc_mbox_timeout(unsigned long ptr)
4586 {
4587         struct lpfc_hba  *phba = (struct lpfc_hba *) ptr;
4588         unsigned long iflag;
4589         uint32_t tmo_posted;
4590
4591         spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
4592         tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
4593         if (!tmo_posted)
4594                 phba->pport->work_port_events |= WORKER_MBOX_TMO;
4595         spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
4596
4597         if (!tmo_posted)
4598                 lpfc_worker_wake_up(phba);
4599         return;
4600 }
4601
4602
4603 /**
4604  * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
4605  * @phba: Pointer to HBA context object.
4606  *
4607  * This function is called from worker thread when a mailbox command times out.
4608  * The caller is not required to hold any locks. This function will reset the
4609  * HBA and recover all the pending commands.
4610  **/
4611 void
4612 lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
4613 {
4614         LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
4615         MAILBOX_t *mb = &pmbox->u.mb;
4616         struct lpfc_sli *psli = &phba->sli;
4617         struct lpfc_sli_ring *pring;
4618
4619         /* Check the pmbox pointer first.  There is a race condition
4620          * between the mbox timeout handler getting executed in the
4621          * worklist and the mailbox actually completing. When this
4622          * race condition occurs, the mbox_active will be NULL.
4623          */
4624         spin_lock_irq(&phba->hbalock);
4625         if (pmbox == NULL) {
4626                 lpfc_printf_log(phba, KERN_WARNING,
4627                                 LOG_MBOX | LOG_SLI,
4628                                 "0353 Active Mailbox cleared - mailbox timeout "
4629                                 "exiting\n");
4630                 spin_unlock_irq(&phba->hbalock);
4631                 return;
4632         }
4633
4634         /* Mbox cmd <mbxCommand> timeout */
4635         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4636                         "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
4637                         mb->mbxCommand,
4638                         phba->pport->port_state,
4639                         phba->sli.sli_flag,
4640                         phba->sli.mbox_active);
4641         spin_unlock_irq(&phba->hbalock);
4642
4643         /* Setting state unknown so lpfc_sli_abort_iocb_ring
4644          * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
4645          * it to fail all oustanding SCSI IO.
4646          */
4647         spin_lock_irq(&phba->pport->work_port_lock);
4648         phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
4649         spin_unlock_irq(&phba->pport->work_port_lock);
4650         spin_lock_irq(&phba->hbalock);
4651         phba->link_state = LPFC_LINK_UNKNOWN;
4652         psli->sli_flag &= ~LPFC_SLI_ACTIVE;
4653         spin_unlock_irq(&phba->hbalock);
4654
4655         pring = &psli->ring[psli->fcp_ring];
4656         lpfc_sli_abort_iocb_ring(phba, pring);
4657
4658         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4659                         "0345 Resetting board due to mailbox timeout\n");
4660
4661         /* Reset the HBA device */
4662         lpfc_reset_hba(phba);
4663 }
4664
4665 /**
4666  * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
4667  * @phba: Pointer to HBA context object.
4668  * @pmbox: Pointer to mailbox object.
4669  * @flag: Flag indicating how the mailbox need to be processed.
4670  *
4671  * This function is called by discovery code and HBA management code
4672  * to submit a mailbox command to firmware with SLI-3 interface spec. This
4673  * function gets the hbalock to protect the data structures.
4674  * The mailbox command can be submitted in polling mode, in which case
4675  * this function will wait in a polling loop for the completion of the
4676  * mailbox.
4677  * If the mailbox is submitted in no_wait mode (not polling) the
4678  * function will submit the command and returns immediately without waiting
4679  * for the mailbox completion. The no_wait is supported only when HBA
4680  * is in SLI2/SLI3 mode - interrupts are enabled.
4681  * The SLI interface allows only one mailbox pending at a time. If the
4682  * mailbox is issued in polling mode and there is already a mailbox
4683  * pending, then the function will return an error. If the mailbox is issued
4684  * in NO_WAIT mode and there is a mailbox pending already, the function
4685  * will return MBX_BUSY after queuing the mailbox into mailbox queue.
4686  * The sli layer owns the mailbox object until the completion of mailbox
4687  * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
4688  * return codes the caller owns the mailbox command after the return of
4689  * the function.
4690  **/
4691 static int
4692 lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
4693                        uint32_t flag)
4694 {
4695         MAILBOX_t *mb;
4696         struct lpfc_sli *psli = &phba->sli;
4697         uint32_t status, evtctr;
4698         uint32_t ha_copy;
4699         int i;
4700         unsigned long timeout;
4701         unsigned long drvr_flag = 0;
4702         uint32_t word0, ldata;
4703         void __iomem *to_slim;
4704         int processing_queue = 0;
4705
4706         spin_lock_irqsave(&phba->hbalock, drvr_flag);
4707         if (!pmbox) {
4708                 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
4709                 /* processing mbox queue from intr_handler */
4710                 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
4711                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4712                         return MBX_SUCCESS;
4713                 }
4714                 processing_queue = 1;
4715                 pmbox = lpfc_mbox_get(phba);
4716                 if (!pmbox) {
4717                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4718                         return MBX_SUCCESS;
4719                 }
4720         }
4721
4722         if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
4723                 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
4724                 if(!pmbox->vport) {
4725                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4726                         lpfc_printf_log(phba, KERN_ERR,
4727                                         LOG_MBOX | LOG_VPORT,
4728                                         "1806 Mbox x%x failed. No vport\n",
4729                                         pmbox->u.mb.mbxCommand);
4730                         dump_stack();
4731                         goto out_not_finished;
4732                 }
4733         }
4734
4735         /* If the PCI channel is in offline state, do not post mbox. */
4736         if (unlikely(pci_channel_offline(phba->pcidev))) {
4737                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4738                 goto out_not_finished;
4739         }
4740
4741         /* If HBA has a deferred error attention, fail the iocb. */
4742         if (unlikely(phba->hba_flag & DEFER_ERATT)) {
4743                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4744                 goto out_not_finished;
4745         }
4746
4747         psli = &phba->sli;
4748
4749         mb = &pmbox->u.mb;
4750         status = MBX_SUCCESS;
4751
4752         if (phba->link_state == LPFC_HBA_ERROR) {
4753                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4754
4755                 /* Mbox command <mbxCommand> cannot issue */
4756                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4757                                 "(%d):0311 Mailbox command x%x cannot "
4758                                 "issue Data: x%x x%x\n",
4759                                 pmbox->vport ? pmbox->vport->vpi : 0,
4760                                 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
4761                 goto out_not_finished;
4762         }
4763
4764         if (mb->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT &&
4765             !(readl(phba->HCregaddr) & HC_MBINT_ENA)) {
4766                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4767                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4768                                 "(%d):2528 Mailbox command x%x cannot "
4769                                 "issue Data: x%x x%x\n",
4770                                 pmbox->vport ? pmbox->vport->vpi : 0,
4771                                 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
4772                 goto out_not_finished;
4773         }
4774
4775         if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
4776                 /* Polling for a mbox command when another one is already active
4777                  * is not allowed in SLI. Also, the driver must have established
4778                  * SLI2 mode to queue and process multiple mbox commands.
4779                  */
4780
4781                 if (flag & MBX_POLL) {
4782                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4783
4784                         /* Mbox command <mbxCommand> cannot issue */
4785                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4786                                         "(%d):2529 Mailbox command x%x "
4787                                         "cannot issue Data: x%x x%x\n",
4788                                         pmbox->vport ? pmbox->vport->vpi : 0,
4789                                         pmbox->u.mb.mbxCommand,
4790                                         psli->sli_flag, flag);
4791                         goto out_not_finished;
4792                 }
4793
4794                 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
4795                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4796                         /* Mbox command <mbxCommand> cannot issue */
4797                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4798                                         "(%d):2530 Mailbox command x%x "
4799                                         "cannot issue Data: x%x x%x\n",
4800                                         pmbox->vport ? pmbox->vport->vpi : 0,
4801                                         pmbox->u.mb.mbxCommand,
4802                                         psli->sli_flag, flag);
4803                         goto out_not_finished;
4804                 }
4805
4806                 /* Another mailbox command is still being processed, queue this
4807                  * command to be processed later.
4808                  */
4809                 lpfc_mbox_put(phba, pmbox);
4810
4811                 /* Mbox cmd issue - BUSY */
4812                 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4813                                 "(%d):0308 Mbox cmd issue - BUSY Data: "
4814                                 "x%x x%x x%x x%x\n",
4815                                 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
4816                                 mb->mbxCommand, phba->pport->port_state,
4817                                 psli->sli_flag, flag);
4818
4819                 psli->slistat.mbox_busy++;
4820                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4821
4822                 if (pmbox->vport) {
4823                         lpfc_debugfs_disc_trc(pmbox->vport,
4824                                 LPFC_DISC_TRC_MBOX_VPORT,
4825                                 "MBOX Bsy vport:  cmd:x%x mb:x%x x%x",
4826                                 (uint32_t)mb->mbxCommand,
4827                                 mb->un.varWords[0], mb->un.varWords[1]);
4828                 }
4829                 else {
4830                         lpfc_debugfs_disc_trc(phba->pport,
4831                                 LPFC_DISC_TRC_MBOX,
4832                                 "MBOX Bsy:        cmd:x%x mb:x%x x%x",
4833                                 (uint32_t)mb->mbxCommand,
4834                                 mb->un.varWords[0], mb->un.varWords[1]);
4835                 }
4836
4837                 return MBX_BUSY;
4838         }
4839
4840         psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
4841
4842         /* If we are not polling, we MUST be in SLI2 mode */
4843         if (flag != MBX_POLL) {
4844                 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
4845                     (mb->mbxCommand != MBX_KILL_BOARD)) {
4846                         psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
4847                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4848                         /* Mbox command <mbxCommand> cannot issue */
4849                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4850                                         "(%d):2531 Mailbox command x%x "
4851                                         "cannot issue Data: x%x x%x\n",
4852                                         pmbox->vport ? pmbox->vport->vpi : 0,
4853                                         pmbox->u.mb.mbxCommand,
4854                                         psli->sli_flag, flag);
4855                         goto out_not_finished;
4856                 }
4857                 /* timeout active mbox command */
4858                 mod_timer(&psli->mbox_tmo, (jiffies +
4859                                (HZ * lpfc_mbox_tmo_val(phba, mb->mbxCommand))));
4860         }
4861
4862         /* Mailbox cmd <cmd> issue */
4863         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4864                         "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
4865                         "x%x\n",
4866                         pmbox->vport ? pmbox->vport->vpi : 0,
4867                         mb->mbxCommand, phba->pport->port_state,
4868                         psli->sli_flag, flag);
4869
4870         if (mb->mbxCommand != MBX_HEARTBEAT) {
4871                 if (pmbox->vport) {
4872                         lpfc_debugfs_disc_trc(pmbox->vport,
4873                                 LPFC_DISC_TRC_MBOX_VPORT,
4874                                 "MBOX Send vport: cmd:x%x mb:x%x x%x",
4875                                 (uint32_t)mb->mbxCommand,
4876                                 mb->un.varWords[0], mb->un.varWords[1]);
4877                 }
4878                 else {
4879                         lpfc_debugfs_disc_trc(phba->pport,
4880                                 LPFC_DISC_TRC_MBOX,
4881                                 "MBOX Send:       cmd:x%x mb:x%x x%x",
4882                                 (uint32_t)mb->mbxCommand,
4883                                 mb->un.varWords[0], mb->un.varWords[1]);
4884                 }
4885         }
4886
4887         psli->slistat.mbox_cmd++;
4888         evtctr = psli->slistat.mbox_event;
4889
4890         /* next set own bit for the adapter and copy over command word */
4891         mb->mbxOwner = OWN_CHIP;
4892
4893         if (psli->sli_flag & LPFC_SLI_ACTIVE) {
4894                 /* First copy command data to host SLIM area */
4895                 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
4896         } else {
4897                 if (mb->mbxCommand == MBX_CONFIG_PORT) {
4898                         /* copy command data into host mbox for cmpl */
4899                         lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
4900                 }
4901
4902                 /* First copy mbox command data to HBA SLIM, skip past first
4903                    word */
4904                 to_slim = phba->MBslimaddr + sizeof (uint32_t);
4905                 lpfc_memcpy_to_slim(to_slim, &mb->un.varWords[0],
4906                             MAILBOX_CMD_SIZE - sizeof (uint32_t));
4907
4908                 /* Next copy over first word, with mbxOwner set */
4909                 ldata = *((uint32_t *)mb);
4910                 to_slim = phba->MBslimaddr;
4911                 writel(ldata, to_slim);
4912                 readl(to_slim); /* flush */
4913
4914                 if (mb->mbxCommand == MBX_CONFIG_PORT) {
4915                         /* switch over to host mailbox */
4916                         psli->sli_flag |= LPFC_SLI_ACTIVE;
4917                 }
4918         }
4919
4920         wmb();
4921
4922         switch (flag) {
4923         case MBX_NOWAIT:
4924                 /* Set up reference to mailbox command */
4925                 psli->mbox_active = pmbox;
4926                 /* Interrupt board to do it */
4927                 writel(CA_MBATT, phba->CAregaddr);
4928                 readl(phba->CAregaddr); /* flush */
4929                 /* Don't wait for it to finish, just return */
4930                 break;
4931
4932         case MBX_POLL:
4933                 /* Set up null reference to mailbox command */
4934                 psli->mbox_active = NULL;
4935                 /* Interrupt board to do it */
4936                 writel(CA_MBATT, phba->CAregaddr);
4937                 readl(phba->CAregaddr); /* flush */
4938
4939                 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
4940                         /* First read mbox status word */
4941                         word0 = *((uint32_t *)phba->mbox);
4942                         word0 = le32_to_cpu(word0);
4943                 } else {
4944                         /* First read mbox status word */
4945                         word0 = readl(phba->MBslimaddr);
4946                 }
4947
4948                 /* Read the HBA Host Attention Register */
4949                 ha_copy = readl(phba->HAregaddr);
4950                 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
4951                                                              mb->mbxCommand) *
4952                                            1000) + jiffies;
4953                 i = 0;
4954                 /* Wait for command to complete */
4955                 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
4956                        (!(ha_copy & HA_MBATT) &&
4957                         (phba->link_state > LPFC_WARM_START))) {
4958                         if (time_after(jiffies, timeout)) {
4959                                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
4960                                 spin_unlock_irqrestore(&phba->hbalock,
4961                                                        drvr_flag);
4962                                 goto out_not_finished;
4963                         }
4964
4965                         /* Check if we took a mbox interrupt while we were
4966                            polling */
4967                         if (((word0 & OWN_CHIP) != OWN_CHIP)
4968                             && (evtctr != psli->slistat.mbox_event))
4969                                 break;
4970
4971                         if (i++ > 10) {
4972                                 spin_unlock_irqrestore(&phba->hbalock,
4973                                                        drvr_flag);
4974                                 msleep(1);
4975                                 spin_lock_irqsave(&phba->hbalock, drvr_flag);
4976                         }
4977
4978                         if (psli->sli_flag & LPFC_SLI_ACTIVE) {
4979                                 /* First copy command data */
4980                                 word0 = *((uint32_t *)phba->mbox);
4981                                 word0 = le32_to_cpu(word0);
4982                                 if (mb->mbxCommand == MBX_CONFIG_PORT) {
4983                                         MAILBOX_t *slimmb;
4984                                         uint32_t slimword0;
4985                                         /* Check real SLIM for any errors */
4986                                         slimword0 = readl(phba->MBslimaddr);
4987                                         slimmb = (MAILBOX_t *) & slimword0;
4988                                         if (((slimword0 & OWN_CHIP) != OWN_CHIP)
4989                                             && slimmb->mbxStatus) {
4990                                                 psli->sli_flag &=
4991                                                     ~LPFC_SLI_ACTIVE;
4992                                                 word0 = slimword0;
4993                                         }
4994                                 }
4995                         } else {
4996                                 /* First copy command data */
4997                                 word0 = readl(phba->MBslimaddr);
4998                         }
4999                         /* Read the HBA Host Attention Register */
5000                         ha_copy = readl(phba->HAregaddr);
5001                 }
5002
5003                 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
5004                         /* copy results back to user */
5005                         lpfc_sli_pcimem_bcopy(phba->mbox, mb, MAILBOX_CMD_SIZE);
5006                 } else {
5007                         /* First copy command data */
5008                         lpfc_memcpy_from_slim(mb, phba->MBslimaddr,
5009                                                         MAILBOX_CMD_SIZE);
5010                         if ((mb->mbxCommand == MBX_DUMP_MEMORY) &&
5011                                 pmbox->context2) {
5012                                 lpfc_memcpy_from_slim((void *)pmbox->context2,
5013                                       phba->MBslimaddr + DMP_RSP_OFFSET,
5014                                                       mb->un.varDmp.word_cnt);
5015                         }
5016                 }
5017
5018                 writel(HA_MBATT, phba->HAregaddr);
5019                 readl(phba->HAregaddr); /* flush */
5020
5021                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5022                 status = mb->mbxStatus;
5023         }
5024
5025         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
5026         return status;
5027
5028 out_not_finished:
5029         if (processing_queue) {
5030                 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
5031                 lpfc_mbox_cmpl_put(phba, pmbox);
5032         }
5033         return MBX_NOT_FINISHED;
5034 }
5035
5036 /**
5037  * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
5038  * @phba: Pointer to HBA context object.
5039  *
5040  * The function blocks the posting of SLI4 asynchronous mailbox commands from
5041  * the driver internal pending mailbox queue. It will then try to wait out the
5042  * possible outstanding mailbox command before return.
5043  *
5044  * Returns:
5045  *      0 - the outstanding mailbox command completed; otherwise, the wait for
5046  *      the outstanding mailbox command timed out.
5047  **/
5048 static int
5049 lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
5050 {
5051         struct lpfc_sli *psli = &phba->sli;
5052         uint8_t actcmd = MBX_HEARTBEAT;
5053         int rc = 0;
5054         unsigned long timeout;
5055
5056         /* Mark the asynchronous mailbox command posting as blocked */
5057         spin_lock_irq(&phba->hbalock);
5058         psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
5059         if (phba->sli.mbox_active)
5060                 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
5061         spin_unlock_irq(&phba->hbalock);
5062         /* Determine how long we might wait for the active mailbox
5063          * command to be gracefully completed by firmware.
5064          */
5065         timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, actcmd) * 1000) +
5066                                    jiffies;
5067         /* Wait for the outstnading mailbox command to complete */
5068         while (phba->sli.mbox_active) {
5069                 /* Check active mailbox complete status every 2ms */
5070                 msleep(2);
5071                 if (time_after(jiffies, timeout)) {
5072                         /* Timeout, marked the outstanding cmd not complete */
5073                         rc = 1;
5074                         break;
5075                 }
5076         }
5077
5078         /* Can not cleanly block async mailbox command, fails it */
5079         if (rc) {
5080                 spin_lock_irq(&phba->hbalock);
5081                 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
5082                 spin_unlock_irq(&phba->hbalock);
5083         }
5084         return rc;
5085 }
5086
5087 /**
5088  * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
5089  * @phba: Pointer to HBA context object.
5090  *
5091  * The function unblocks and resume posting of SLI4 asynchronous mailbox
5092  * commands from the driver internal pending mailbox queue. It makes sure
5093  * that there is no outstanding mailbox command before resuming posting
5094  * asynchronous mailbox commands. If, for any reason, there is outstanding
5095  * mailbox command, it will try to wait it out before resuming asynchronous
5096  * mailbox command posting.
5097  **/
5098 static void
5099 lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
5100 {
5101         struct lpfc_sli *psli = &phba->sli;
5102
5103         spin_lock_irq(&phba->hbalock);
5104         if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
5105                 /* Asynchronous mailbox posting is not blocked, do nothing */
5106                 spin_unlock_irq(&phba->hbalock);
5107                 return;
5108         }
5109
5110         /* Outstanding synchronous mailbox command is guaranteed to be done,
5111          * successful or timeout, after timing-out the outstanding mailbox
5112          * command shall always be removed, so just unblock posting async
5113          * mailbox command and resume
5114          */
5115         psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
5116         spin_unlock_irq(&phba->hbalock);
5117
5118         /* wake up worker thread to post asynchronlous mailbox command */
5119         lpfc_worker_wake_up(phba);
5120 }
5121
5122 /**
5123  * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
5124  * @phba: Pointer to HBA context object.
5125  * @mboxq: Pointer to mailbox object.
5126  *
5127  * The function posts a mailbox to the port.  The mailbox is expected
5128  * to be comletely filled in and ready for the port to operate on it.
5129  * This routine executes a synchronous completion operation on the
5130  * mailbox by polling for its completion.
5131  *
5132  * The caller must not be holding any locks when calling this routine.
5133  *
5134  * Returns:
5135  *      MBX_SUCCESS - mailbox posted successfully
5136  *      Any of the MBX error values.
5137  **/
5138 static int
5139 lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
5140 {
5141         int rc = MBX_SUCCESS;
5142         unsigned long iflag;
5143         uint32_t db_ready;
5144         uint32_t mcqe_status;
5145         uint32_t mbx_cmnd;
5146         unsigned long timeout;
5147         struct lpfc_sli *psli = &phba->sli;
5148         struct lpfc_mqe *mb = &mboxq->u.mqe;
5149         struct lpfc_bmbx_create *mbox_rgn;
5150         struct dma_address *dma_address;
5151         struct lpfc_register bmbx_reg;
5152
5153         /*
5154          * Only one mailbox can be active to the bootstrap mailbox region
5155          * at a time and there is no queueing provided.
5156          */
5157         spin_lock_irqsave(&phba->hbalock, iflag);
5158         if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
5159                 spin_unlock_irqrestore(&phba->hbalock, iflag);
5160                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5161                                 "(%d):2532 Mailbox command x%x (x%x) "
5162                                 "cannot issue Data: x%x x%x\n",
5163                                 mboxq->vport ? mboxq->vport->vpi : 0,
5164                                 mboxq->u.mb.mbxCommand,
5165                                 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5166                                 psli->sli_flag, MBX_POLL);
5167                 return MBXERR_ERROR;
5168         }
5169         /* The server grabs the token and owns it until release */
5170         psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
5171         phba->sli.mbox_active = mboxq;
5172         spin_unlock_irqrestore(&phba->hbalock, iflag);
5173
5174         /*
5175          * Initialize the bootstrap memory region to avoid stale data areas
5176          * in the mailbox post.  Then copy the caller's mailbox contents to
5177          * the bmbx mailbox region.
5178          */
5179         mbx_cmnd = bf_get(lpfc_mqe_command, mb);
5180         memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
5181         lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
5182                               sizeof(struct lpfc_mqe));
5183
5184         /* Post the high mailbox dma address to the port and wait for ready. */
5185         dma_address = &phba->sli4_hba.bmbx.dma_address;
5186         writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
5187
5188         timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mbx_cmnd)
5189                                    * 1000) + jiffies;
5190         do {
5191                 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
5192                 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
5193                 if (!db_ready)
5194                         msleep(2);
5195
5196                 if (time_after(jiffies, timeout)) {
5197                         rc = MBXERR_ERROR;
5198                         goto exit;
5199                 }
5200         } while (!db_ready);
5201
5202         /* Post the low mailbox dma address to the port. */
5203         writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
5204         timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mbx_cmnd)
5205                                    * 1000) + jiffies;
5206         do {
5207                 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
5208                 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
5209                 if (!db_ready)
5210                         msleep(2);
5211
5212                 if (time_after(jiffies, timeout)) {
5213                         rc = MBXERR_ERROR;
5214                         goto exit;
5215                 }
5216         } while (!db_ready);
5217
5218         /*
5219          * Read the CQ to ensure the mailbox has completed.
5220          * If so, update the mailbox status so that the upper layers
5221          * can complete the request normally.
5222          */
5223         lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
5224                               sizeof(struct lpfc_mqe));
5225         mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
5226         lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
5227                               sizeof(struct lpfc_mcqe));
5228         mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
5229
5230         /* Prefix the mailbox status with range x4000 to note SLI4 status. */
5231         if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
5232                 bf_set(lpfc_mqe_status, mb, LPFC_MBX_ERROR_RANGE | mcqe_status);
5233                 rc = MBXERR_ERROR;
5234         }
5235
5236         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5237                         "(%d):0356 Mailbox cmd x%x (x%x) Status x%x "
5238                         "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
5239                         " x%x x%x CQ: x%x x%x x%x x%x\n",
5240                         mboxq->vport ? mboxq->vport->vpi : 0,
5241                         mbx_cmnd, lpfc_sli4_mbox_opcode_get(phba, mboxq),
5242                         bf_get(lpfc_mqe_status, mb),
5243                         mb->un.mb_words[0], mb->un.mb_words[1],
5244                         mb->un.mb_words[2], mb->un.mb_words[3],
5245                         mb->un.mb_words[4], mb->un.mb_words[5],
5246                         mb->un.mb_words[6], mb->un.mb_words[7],
5247                         mb->un.mb_words[8], mb->un.mb_words[9],
5248                         mb->un.mb_words[10], mb->un.mb_words[11],
5249                         mb->un.mb_words[12], mboxq->mcqe.word0,
5250                         mboxq->mcqe.mcqe_tag0,  mboxq->mcqe.mcqe_tag1,
5251                         mboxq->mcqe.trailer);
5252 exit:
5253         /* We are holding the token, no needed for lock when release */
5254         spin_lock_irqsave(&phba->hbalock, iflag);
5255         psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5256         phba->sli.mbox_active = NULL;
5257         spin_unlock_irqrestore(&phba->hbalock, iflag);
5258         return rc;
5259 }
5260
5261 /**
5262  * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
5263  * @phba: Pointer to HBA context object.
5264  * @pmbox: Pointer to mailbox object.
5265  * @flag: Flag indicating how the mailbox need to be processed.
5266  *
5267  * This function is called by discovery code and HBA management code to submit
5268  * a mailbox command to firmware with SLI-4 interface spec.
5269  *
5270  * Return codes the caller owns the mailbox command after the return of the
5271  * function.
5272  **/
5273 static int
5274 lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
5275                        uint32_t flag)
5276 {
5277         struct lpfc_sli *psli = &phba->sli;
5278         unsigned long iflags;
5279         int rc;
5280
5281         rc = lpfc_mbox_dev_check(phba);
5282         if (unlikely(rc)) {
5283                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5284                                 "(%d):2544 Mailbox command x%x (x%x) "
5285                                 "cannot issue Data: x%x x%x\n",
5286                                 mboxq->vport ? mboxq->vport->vpi : 0,
5287                                 mboxq->u.mb.mbxCommand,
5288                                 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5289                                 psli->sli_flag, flag);
5290                 goto out_not_finished;
5291         }
5292
5293         /* Detect polling mode and jump to a handler */
5294         if (!phba->sli4_hba.intr_enable) {
5295                 if (flag == MBX_POLL)
5296                         rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
5297                 else
5298                         rc = -EIO;
5299                 if (rc != MBX_SUCCESS)
5300                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5301                                         "(%d):2541 Mailbox command x%x "
5302                                         "(x%x) cannot issue Data: x%x x%x\n",
5303                                         mboxq->vport ? mboxq->vport->vpi : 0,
5304                                         mboxq->u.mb.mbxCommand,
5305                                         lpfc_sli4_mbox_opcode_get(phba, mboxq),
5306                                         psli->sli_flag, flag);
5307                 return rc;
5308         } else if (flag == MBX_POLL) {
5309                 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
5310                                 "(%d):2542 Try to issue mailbox command "
5311                                 "x%x (x%x) synchronously ahead of async"
5312                                 "mailbox command queue: x%x x%x\n",
5313                                 mboxq->vport ? mboxq->vport->vpi : 0,
5314                                 mboxq->u.mb.mbxCommand,
5315                                 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5316                                 psli->sli_flag, flag);
5317                 /* Try to block the asynchronous mailbox posting */
5318                 rc = lpfc_sli4_async_mbox_block(phba);
5319                 if (!rc) {
5320                         /* Successfully blocked, now issue sync mbox cmd */
5321                         rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
5322                         if (rc != MBX_SUCCESS)
5323                                 lpfc_printf_log(phba, KERN_ERR,
5324                                                 LOG_MBOX | LOG_SLI,
5325                                                 "(%d):2597 Mailbox command "
5326                                                 "x%x (x%x) cannot issue "
5327                                                 "Data: x%x x%x\n",
5328                                                 mboxq->vport ?
5329                                                 mboxq->vport->vpi : 0,
5330                                                 mboxq->u.mb.mbxCommand,
5331                                                 lpfc_sli4_mbox_opcode_get(phba,
5332                                                                 mboxq),
5333                                                 psli->sli_flag, flag);
5334                         /* Unblock the async mailbox posting afterward */
5335                         lpfc_sli4_async_mbox_unblock(phba);
5336                 }
5337                 return rc;
5338         }
5339
5340         /* Now, interrupt mode asynchrous mailbox command */
5341         rc = lpfc_mbox_cmd_check(phba, mboxq);
5342         if (rc) {
5343                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5344                                 "(%d):2543 Mailbox command x%x (x%x) "
5345                                 "cannot issue Data: x%x x%x\n",
5346                                 mboxq->vport ? mboxq->vport->vpi : 0,
5347                                 mboxq->u.mb.mbxCommand,
5348                                 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5349                                 psli->sli_flag, flag);
5350                 goto out_not_finished;
5351         }
5352
5353         /* Put the mailbox command to the driver internal FIFO */
5354         psli->slistat.mbox_busy++;
5355         spin_lock_irqsave(&phba->hbalock, iflags);
5356         lpfc_mbox_put(phba, mboxq);
5357         spin_unlock_irqrestore(&phba->hbalock, iflags);
5358         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5359                         "(%d):0354 Mbox cmd issue - Enqueue Data: "
5360                         "x%x (x%x) x%x x%x x%x\n",
5361                         mboxq->vport ? mboxq->vport->vpi : 0xffffff,
5362                         bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5363                         lpfc_sli4_mbox_opcode_get(phba, mboxq),
5364                         phba->pport->port_state,
5365                         psli->sli_flag, MBX_NOWAIT);
5366         /* Wake up worker thread to transport mailbox command from head */
5367         lpfc_worker_wake_up(phba);
5368
5369         return MBX_BUSY;
5370
5371 out_not_finished:
5372         return MBX_NOT_FINISHED;
5373 }
5374
5375 /**
5376  * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
5377  * @phba: Pointer to HBA context object.
5378  *
5379  * This function is called by worker thread to send a mailbox command to
5380  * SLI4 HBA firmware.
5381  *
5382  **/
5383 int
5384 lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
5385 {
5386         struct lpfc_sli *psli = &phba->sli;
5387         LPFC_MBOXQ_t *mboxq;
5388         int rc = MBX_SUCCESS;
5389         unsigned long iflags;
5390         struct lpfc_mqe *mqe;
5391         uint32_t mbx_cmnd;
5392
5393         /* Check interrupt mode before post async mailbox command */
5394         if (unlikely(!phba->sli4_hba.intr_enable))
5395                 return MBX_NOT_FINISHED;
5396
5397         /* Check for mailbox command service token */
5398         spin_lock_irqsave(&phba->hbalock, iflags);
5399         if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
5400                 spin_unlock_irqrestore(&phba->hbalock, iflags);
5401                 return MBX_NOT_FINISHED;
5402         }
5403         if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
5404                 spin_unlock_irqrestore(&phba->hbalock, iflags);
5405                 return MBX_NOT_FINISHED;
5406         }
5407         if (unlikely(phba->sli.mbox_active)) {
5408                 spin_unlock_irqrestore(&phba->hbalock, iflags);
5409                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5410                                 "0384 There is pending active mailbox cmd\n");
5411                 return MBX_NOT_FINISHED;
5412         }
5413         /* Take the mailbox command service token */
5414         psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
5415
5416         /* Get the next mailbox command from head of queue */
5417         mboxq = lpfc_mbox_get(phba);
5418
5419         /* If no more mailbox command waiting for post, we're done */
5420         if (!mboxq) {
5421                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5422                 spin_unlock_irqrestore(&phba->hbalock, iflags);
5423                 return MBX_SUCCESS;
5424         }
5425         phba->sli.mbox_active = mboxq;
5426         spin_unlock_irqrestore(&phba->hbalock, iflags);
5427
5428         /* Check device readiness for posting mailbox command */
5429         rc = lpfc_mbox_dev_check(phba);
5430         if (unlikely(rc))
5431                 /* Driver clean routine will clean up pending mailbox */
5432                 goto out_not_finished;
5433
5434         /* Prepare the mbox command to be posted */
5435         mqe = &mboxq->u.mqe;
5436         mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
5437
5438         /* Start timer for the mbox_tmo and log some mailbox post messages */
5439         mod_timer(&psli->mbox_tmo, (jiffies +
5440                   (HZ * lpfc_mbox_tmo_val(phba, mbx_cmnd))));
5441
5442         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5443                         "(%d):0355 Mailbox cmd x%x (x%x) issue Data: "
5444                         "x%x x%x\n",
5445                         mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
5446                         lpfc_sli4_mbox_opcode_get(phba, mboxq),
5447                         phba->pport->port_state, psli->sli_flag);
5448
5449         if (mbx_cmnd != MBX_HEARTBEAT) {
5450                 if (mboxq->vport) {
5451                         lpfc_debugfs_disc_trc(mboxq->vport,
5452                                 LPFC_DISC_TRC_MBOX_VPORT,
5453                                 "MBOX Send vport: cmd:x%x mb:x%x x%x",
5454                                 mbx_cmnd, mqe->un.mb_words[0],
5455                                 mqe->un.mb_words[1]);
5456                 } else {
5457                         lpfc_debugfs_disc_trc(phba->pport,
5458                                 LPFC_DISC_TRC_MBOX,
5459                                 "MBOX Send: cmd:x%x mb:x%x x%x",
5460                                 mbx_cmnd, mqe->un.mb_words[0],
5461                                 mqe->un.mb_words[1]);
5462                 }
5463         }
5464         psli->slistat.mbox_cmd++;
5465
5466         /* Post the mailbox command to the port */
5467         rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
5468         if (rc != MBX_SUCCESS) {
5469                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5470                                 "(%d):2533 Mailbox command x%x (x%x) "
5471                                 "cannot issue Data: x%x x%x\n",
5472                                 mboxq->vport ? mboxq->vport->vpi : 0,
5473                                 mboxq->u.mb.mbxCommand,
5474                                 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5475                                 psli->sli_flag, MBX_NOWAIT);
5476                 goto out_not_finished;
5477         }
5478
5479         return rc;
5480
5481 out_not_finished:
5482         spin_lock_irqsave(&phba->hbalock, iflags);
5483         mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
5484         __lpfc_mbox_cmpl_put(phba, mboxq);
5485         /* Release the token */
5486         psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5487         phba->sli.mbox_active = NULL;
5488         spin_unlock_irqrestore(&phba->hbalock, iflags);
5489
5490         return MBX_NOT_FINISHED;
5491 }
5492
5493 /**
5494  * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
5495  * @phba: Pointer to HBA context object.
5496  * @pmbox: Pointer to mailbox object.
5497  * @flag: Flag indicating how the mailbox need to be processed.
5498  *
5499  * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
5500  * the API jump table function pointer from the lpfc_hba struct.
5501  *
5502  * Return codes the caller owns the mailbox command after the return of the
5503  * function.
5504  **/
5505 int
5506 lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
5507 {
5508         return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
5509 }
5510
5511 /**
5512  * lpfc_mbox_api_table_setup - Set up mbox api fucntion jump table
5513  * @phba: The hba struct for which this call is being executed.
5514  * @dev_grp: The HBA PCI-Device group number.
5515  *
5516  * This routine sets up the mbox interface API function jump table in @phba
5517  * struct.
5518  * Returns: 0 - success, -ENODEV - failure.
5519  **/
5520 int
5521 lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
5522 {
5523
5524         switch (dev_grp) {
5525         case LPFC_PCI_DEV_LP:
5526                 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
5527                 phba->lpfc_sli_handle_slow_ring_event =
5528                                 lpfc_sli_handle_slow_ring_event_s3;
5529                 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
5530                 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
5531                 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
5532                 break;
5533         case LPFC_PCI_DEV_OC:
5534                 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
5535                 phba->lpfc_sli_handle_slow_ring_event =
5536                                 lpfc_sli_handle_slow_ring_event_s4;
5537                 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
5538                 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
5539                 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
5540                 break;
5541         default:
5542                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5543                                 "1420 Invalid HBA PCI-device group: 0x%x\n",
5544                                 dev_grp);
5545                 return -ENODEV;
5546                 break;
5547         }
5548         return 0;
5549 }
5550
5551 /**
5552  * __lpfc_sli_ringtx_put - Add an iocb to the txq
5553  * @phba: Pointer to HBA context object.
5554  * @pring: Pointer to driver SLI ring object.
5555  * @piocb: Pointer to address of newly added command iocb.
5556  *
5557  * This function is called with hbalock held to add a command
5558  * iocb to the txq when SLI layer cannot submit the command iocb
5559  * to the ring.
5560  **/
5561 static void
5562 __lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
5563                     struct lpfc_iocbq *piocb)
5564 {
5565         /* Insert the caller's iocb in the txq tail for later processing. */
5566         list_add_tail(&piocb->list, &pring->txq);
5567         pring->txq_cnt++;
5568 }
5569
5570 /**
5571  * lpfc_sli_next_iocb - Get the next iocb in the txq
5572  * @phba: Pointer to HBA context object.
5573  * @pring: Pointer to driver SLI ring object.
5574  * @piocb: Pointer to address of newly added command iocb.
5575  *
5576  * This function is called with hbalock held before a new
5577  * iocb is submitted to the firmware. This function checks
5578  * txq to flush the iocbs in txq to Firmware before
5579  * submitting new iocbs to the Firmware.
5580  * If there are iocbs in the txq which need to be submitted
5581  * to firmware, lpfc_sli_next_iocb returns the first element
5582  * of the txq after dequeuing it from txq.
5583  * If there is no iocb in the txq then the function will return
5584  * *piocb and *piocb is set to NULL. Caller needs to check
5585  * *piocb to find if there are more commands in the txq.
5586  **/
5587 static struct lpfc_iocbq *
5588 lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
5589                    struct lpfc_iocbq **piocb)
5590 {
5591         struct lpfc_iocbq * nextiocb;
5592
5593         nextiocb = lpfc_sli_ringtx_get(phba, pring);
5594         if (!nextiocb) {
5595                 nextiocb = *piocb;
5596                 *piocb = NULL;
5597         }
5598
5599         return nextiocb;
5600 }
5601
5602 /**
5603  * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
5604  * @phba: Pointer to HBA context object.
5605  * @ring_number: SLI ring number to issue iocb on.
5606  * @piocb: Pointer to command iocb.
5607  * @flag: Flag indicating if this command can be put into txq.
5608  *
5609  * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
5610  * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
5611  * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
5612  * flag is turned on, the function returns IOCB_ERROR. When the link is down,
5613  * this function allows only iocbs for posting buffers. This function finds
5614  * next available slot in the command ring and posts the command to the
5615  * available slot and writes the port attention register to request HBA start
5616  * processing new iocb. If there is no slot available in the ring and
5617  * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
5618  * the function returns IOCB_BUSY.
5619  *
5620  * This function is called with hbalock held. The function will return success
5621  * after it successfully submit the iocb to firmware or after adding to the
5622  * txq.
5623  **/
5624 static int
5625 __lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
5626                     struct lpfc_iocbq *piocb, uint32_t flag)
5627 {
5628         struct lpfc_iocbq *nextiocb;
5629         IOCB_t *iocb;
5630         struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
5631
5632         if (piocb->iocb_cmpl && (!piocb->vport) &&
5633            (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
5634            (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
5635                 lpfc_printf_log(phba, KERN_ERR,
5636                                 LOG_SLI | LOG_VPORT,
5637                                 "1807 IOCB x%x failed. No vport\n",
5638                                 piocb->iocb.ulpCommand);
5639                 dump_stack();
5640                 return IOCB_ERROR;
5641         }
5642
5643
5644         /* If the PCI channel is in offline state, do not post iocbs. */
5645         if (unlikely(pci_channel_offline(phba->pcidev)))
5646                 return IOCB_ERROR;
5647
5648         /* If HBA has a deferred error attention, fail the iocb. */
5649         if (unlikely(phba->hba_flag & DEFER_ERATT))
5650                 return IOCB_ERROR;
5651
5652         /*
5653          * We should never get an IOCB if we are in a < LINK_DOWN state
5654          */
5655         if (unlikely(phba->link_state < LPFC_LINK_DOWN))
5656                 return IOCB_ERROR;
5657
5658         /*
5659          * Check to see if we are blocking IOCB processing because of a
5660          * outstanding event.
5661          */
5662         if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
5663                 goto iocb_busy;
5664
5665         if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
5666                 /*
5667                  * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
5668                  * can be issued if the link is not up.
5669                  */
5670                 switch (piocb->iocb.ulpCommand) {
5671                 case CMD_GEN_REQUEST64_CR:
5672                 case CMD_GEN_REQUEST64_CX:
5673                         if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
5674                                 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
5675                                         FC_RCTL_DD_UNSOL_CMD) ||
5676                                 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
5677                                         MENLO_TRANSPORT_TYPE))
5678
5679                                 goto iocb_busy;
5680                         break;
5681                 case CMD_QUE_RING_BUF_CN:
5682                 case CMD_QUE_RING_BUF64_CN:
5683                         /*
5684                          * For IOCBs, like QUE_RING_BUF, that have no rsp ring
5685                          * completion, iocb_cmpl MUST be 0.
5686                          */
5687                         if (piocb->iocb_cmpl)
5688                                 piocb->iocb_cmpl = NULL;
5689                         /*FALLTHROUGH*/
5690                 case CMD_CREATE_XRI_CR:
5691                 case CMD_CLOSE_XRI_CN:
5692                 case CMD_CLOSE_XRI_CX:
5693                         break;
5694                 default:
5695                         goto iocb_busy;
5696                 }
5697
5698         /*
5699          * For FCP commands, we must be in a state where we can process link
5700          * attention events.
5701          */
5702         } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
5703                             !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
5704                 goto iocb_busy;
5705         }
5706
5707         while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
5708                (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
5709                 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
5710
5711         if (iocb)
5712                 lpfc_sli_update_ring(phba, pring);
5713         else
5714                 lpfc_sli_update_full_ring(phba, pring);
5715
5716         if (!piocb)
5717                 return IOCB_SUCCESS;
5718
5719         goto out_busy;
5720
5721  iocb_busy:
5722         pring->stats.iocb_cmd_delay++;
5723
5724  out_busy:
5725
5726         if (!(flag & SLI_IOCB_RET_IOCB)) {
5727                 __lpfc_sli_ringtx_put(phba, pring, piocb);
5728                 return IOCB_SUCCESS;
5729         }
5730
5731         return IOCB_BUSY;
5732 }
5733
5734 /**
5735  * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
5736  * @phba: Pointer to HBA context object.
5737  * @piocb: Pointer to command iocb.
5738  * @sglq: Pointer to the scatter gather queue object.
5739  *
5740  * This routine converts the bpl or bde that is in the IOCB
5741  * to a sgl list for the sli4 hardware. The physical address
5742  * of the bpl/bde is converted back to a virtual address.
5743  * If the IOCB contains a BPL then the list of BDE's is
5744  * converted to sli4_sge's. If the IOCB contains a single
5745  * BDE then it is converted to a single sli_sge.
5746  * The IOCB is still in cpu endianess so the contents of
5747  * the bpl can be used without byte swapping.
5748  *
5749  * Returns valid XRI = Success, NO_XRI = Failure.
5750 **/
5751 static uint16_t
5752 lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
5753                 struct lpfc_sglq *sglq)
5754 {
5755         uint16_t xritag = NO_XRI;
5756         struct ulp_bde64 *bpl = NULL;
5757         struct ulp_bde64 bde;
5758         struct sli4_sge *sgl  = NULL;
5759         IOCB_t *icmd;
5760         int numBdes = 0;
5761         int i = 0;
5762
5763         if (!piocbq || !sglq)
5764                 return xritag;
5765
5766         sgl  = (struct sli4_sge *)sglq->sgl;
5767         icmd = &piocbq->iocb;
5768         if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
5769                 numBdes = icmd->un.genreq64.bdl.bdeSize /
5770                                 sizeof(struct ulp_bde64);
5771                 /* The addrHigh and addrLow fields within the IOCB
5772                  * have not been byteswapped yet so there is no
5773                  * need to swap them back.
5774                  */
5775                 bpl  = (struct ulp_bde64 *)
5776                         ((struct lpfc_dmabuf *)piocbq->context3)->virt;
5777
5778                 if (!bpl)
5779                         return xritag;
5780
5781                 for (i = 0; i < numBdes; i++) {
5782                         /* Should already be byte swapped. */
5783                         sgl->addr_hi = bpl->addrHigh;
5784                         sgl->addr_lo = bpl->addrLow;
5785
5786                         if ((i+1) == numBdes)
5787                                 bf_set(lpfc_sli4_sge_last, sgl, 1);
5788                         else
5789                                 bf_set(lpfc_sli4_sge_last, sgl, 0);
5790                         sgl->word2 = cpu_to_le32(sgl->word2);
5791                         /* swap the size field back to the cpu so we
5792                          * can assign it to the sgl.
5793                          */
5794                         bde.tus.w = le32_to_cpu(bpl->tus.w);
5795                         sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
5796                         bpl++;
5797                         sgl++;
5798                 }
5799         } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
5800                         /* The addrHigh and addrLow fields of the BDE have not
5801                          * been byteswapped yet so they need to be swapped
5802                          * before putting them in the sgl.
5803                          */
5804                         sgl->addr_hi =
5805                                 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
5806                         sgl->addr_lo =
5807                                 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
5808                         bf_set(lpfc_sli4_sge_last, sgl, 1);
5809                         sgl->word2 = cpu_to_le32(sgl->word2);
5810                         sgl->sge_len =
5811                                 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
5812         }
5813         return sglq->sli4_xritag;
5814 }
5815
5816 /**
5817  * lpfc_sli4_scmd_to_wqidx_distr - scsi command to SLI4 WQ index distribution
5818  * @phba: Pointer to HBA context object.
5819  *
5820  * This routine performs a round robin SCSI command to SLI4 FCP WQ index
5821  * distribution.  This is called by __lpfc_sli_issue_iocb_s4() with the hbalock
5822  * held.
5823  *
5824  * Return: index into SLI4 fast-path FCP queue index.
5825  **/
5826 static uint32_t
5827 lpfc_sli4_scmd_to_wqidx_distr(struct lpfc_hba *phba)
5828 {
5829         ++phba->fcp_qidx;
5830         if (phba->fcp_qidx >= phba->cfg_fcp_wq_count)
5831                 phba->fcp_qidx = 0;
5832
5833         return phba->fcp_qidx;
5834 }
5835
5836 /**
5837  * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
5838  * @phba: Pointer to HBA context object.
5839  * @piocb: Pointer to command iocb.
5840  * @wqe: Pointer to the work queue entry.
5841  *
5842  * This routine converts the iocb command to its Work Queue Entry
5843  * equivalent. The wqe pointer should not have any fields set when
5844  * this routine is called because it will memcpy over them.
5845  * This routine does not set the CQ_ID or the WQEC bits in the
5846  * wqe.
5847  *
5848  * Returns: 0 = Success, IOCB_ERROR = Failure.
5849  **/
5850 static int
5851 lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
5852                 union lpfc_wqe *wqe)
5853 {
5854         uint32_t xmit_len = 0, total_len = 0;
5855         uint8_t ct = 0;
5856         uint32_t fip;
5857         uint32_t abort_tag;
5858         uint8_t command_type = ELS_COMMAND_NON_FIP;
5859         uint8_t cmnd;
5860         uint16_t xritag;
5861         struct ulp_bde64 *bpl = NULL;
5862         uint32_t els_id = ELS_ID_DEFAULT;
5863         int numBdes, i;
5864         struct ulp_bde64 bde;
5865
5866         fip = phba->hba_flag & HBA_FIP_SUPPORT;
5867         /* The fcp commands will set command type */
5868         if (iocbq->iocb_flag &  LPFC_IO_FCP)
5869                 command_type = FCP_COMMAND;
5870         else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
5871                 command_type = ELS_COMMAND_FIP;
5872         else
5873                 command_type = ELS_COMMAND_NON_FIP;
5874
5875         /* Some of the fields are in the right position already */
5876         memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
5877         abort_tag = (uint32_t) iocbq->iotag;
5878         xritag = iocbq->sli4_xritag;
5879         wqe->words[7] = 0; /* The ct field has moved so reset */
5880         /* words0-2 bpl convert bde */
5881         if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
5882                 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
5883                                 sizeof(struct ulp_bde64);
5884                 bpl  = (struct ulp_bde64 *)
5885                         ((struct lpfc_dmabuf *)iocbq->context3)->virt;
5886                 if (!bpl)
5887                         return IOCB_ERROR;
5888
5889                 /* Should already be byte swapped. */
5890                 wqe->generic.bde.addrHigh =  le32_to_cpu(bpl->addrHigh);
5891                 wqe->generic.bde.addrLow =  le32_to_cpu(bpl->addrLow);
5892                 /* swap the size field back to the cpu so we
5893                  * can assign it to the sgl.
5894                  */
5895                 wqe->generic.bde.tus.w  = le32_to_cpu(bpl->tus.w);
5896                 xmit_len = wqe->generic.bde.tus.f.bdeSize;
5897                 total_len = 0;
5898                 for (i = 0; i < numBdes; i++) {
5899                         bde.tus.w  = le32_to_cpu(bpl[i].tus.w);
5900                         total_len += bde.tus.f.bdeSize;
5901                 }
5902         } else
5903                 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
5904
5905         iocbq->iocb.ulpIoTag = iocbq->iotag;
5906         cmnd = iocbq->iocb.ulpCommand;
5907
5908         switch (iocbq->iocb.ulpCommand) {
5909         case CMD_ELS_REQUEST64_CR:
5910                 if (!iocbq->iocb.ulpLe) {
5911                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5912                                 "2007 Only Limited Edition cmd Format"
5913                                 " supported 0x%x\n",
5914                                 iocbq->iocb.ulpCommand);
5915                         return IOCB_ERROR;
5916                 }
5917                 wqe->els_req.payload_len = xmit_len;
5918                 /* Els_reguest64 has a TMO */
5919                 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
5920                         iocbq->iocb.ulpTimeout);
5921                 /* Need a VF for word 4 set the vf bit*/
5922                 bf_set(els_req64_vf, &wqe->els_req, 0);
5923                 /* And a VFID for word 12 */
5924                 bf_set(els_req64_vfid, &wqe->els_req, 0);
5925                 /*
5926                  * Set ct field to 3, indicates that the context_tag field
5927                  * contains the FCFI and remote N_Port_ID is
5928                  * in word 5.
5929                  */
5930
5931                 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
5932                 bf_set(lpfc_wqe_gen_context, &wqe->generic,
5933                                 iocbq->iocb.ulpContext);
5934
5935                 bf_set(lpfc_wqe_gen_ct, &wqe->generic, ct);
5936                 bf_set(lpfc_wqe_gen_pu, &wqe->generic, 0);
5937                 /* CCP CCPE PV PRI in word10 were set in the memcpy */
5938
5939                 if (command_type == ELS_COMMAND_FIP) {
5940                         els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
5941                                         >> LPFC_FIP_ELS_ID_SHIFT);
5942                 }
5943                 bf_set(lpfc_wqe_gen_els_id, &wqe->generic, els_id);
5944
5945         break;
5946         case CMD_XMIT_SEQUENCE64_CX:
5947                 bf_set(lpfc_wqe_gen_context, &wqe->generic,
5948                                         iocbq->iocb.un.ulpWord[3]);
5949                 wqe->generic.word3 = 0;
5950                 bf_set(wqe_rcvoxid, &wqe->generic, iocbq->iocb.ulpContext);
5951                 /* The entire sequence is transmitted for this IOCB */
5952                 xmit_len = total_len;
5953                 cmnd = CMD_XMIT_SEQUENCE64_CR;
5954         case CMD_XMIT_SEQUENCE64_CR:
5955                 /* word3 iocb=io_tag32 wqe=payload_offset */
5956                 /* payload offset used for multilpe outstanding
5957                  * sequences on the same exchange
5958                  */
5959                 wqe->words[3] = 0;
5960                 /* word4 relative_offset memcpy */
5961                 /* word5 r_ctl/df_ctl memcpy */
5962                 bf_set(lpfc_wqe_gen_pu, &wqe->generic, 0);
5963                 wqe->xmit_sequence.xmit_len = xmit_len;
5964                 command_type = OTHER_COMMAND;
5965         break;
5966         case CMD_XMIT_BCAST64_CN:
5967                 /* word3 iocb=iotag32 wqe=payload_len */
5968                 wqe->words[3] = 0; /* no definition for this in wqe */
5969                 /* word4 iocb=rsvd wqe=rsvd */
5970                 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
5971                 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
5972                 bf_set(lpfc_wqe_gen_ct, &wqe->generic,
5973                         ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
5974         break;
5975         case CMD_FCP_IWRITE64_CR:
5976                 command_type = FCP_COMMAND_DATA_OUT;
5977                 /* The struct for wqe fcp_iwrite has 3 fields that are somewhat
5978                  * confusing.
5979                  * word3 is payload_len: byte offset to the sgl entry for the
5980                  * fcp_command.
5981                  * word4 is total xfer len, same as the IOCB->ulpParameter.
5982                  * word5 is initial xfer len 0 = wait for xfer-ready
5983                  */
5984
5985                 /* Always wait for xfer-ready before sending data */
5986                 wqe->fcp_iwrite.initial_xfer_len = 0;
5987                 /* word 4 (xfer length) should have been set on the memcpy */
5988
5989         /* allow write to fall through to read */
5990         case CMD_FCP_IREAD64_CR:
5991                 /* FCP_CMD is always the 1st sgl entry */
5992                 wqe->fcp_iread.payload_len =
5993                         xmit_len + sizeof(struct fcp_rsp);
5994
5995                 /* word 4 (xfer length) should have been set on the memcpy */
5996
5997                 bf_set(lpfc_wqe_gen_erp, &wqe->generic,
5998                         iocbq->iocb.ulpFCP2Rcvy);
5999                 bf_set(lpfc_wqe_gen_lnk, &wqe->generic, iocbq->iocb.ulpXS);
6000                 /* The XC bit and the XS bit are similar. The driver never
6001                  * tracked whether or not the exchange was previouslly open.
6002                  * XC = Exchange create, 0 is create. 1 is already open.
6003                  * XS = link cmd: 1 do not close the exchange after command.
6004                  * XS = 0 close exchange when command completes.
6005                  * The only time we would not set the XC bit is when the XS bit
6006                  * is set and we are sending our 2nd or greater command on
6007                  * this exchange.
6008                  */
6009                 /* Always open the exchange */
6010                 bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
6011
6012                 wqe->words[10] &= 0xffff0000; /* zero out ebde count */
6013                 bf_set(lpfc_wqe_gen_pu, &wqe->generic, iocbq->iocb.ulpPU);
6014                 break;
6015         case CMD_FCP_ICMND64_CR:
6016                 /* Always open the exchange */
6017                 bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
6018
6019                 wqe->words[4] = 0;
6020                 wqe->words[10] &= 0xffff0000; /* zero out ebde count */
6021                 bf_set(lpfc_wqe_gen_pu, &wqe->generic, 0);
6022         break;
6023         case CMD_GEN_REQUEST64_CR:
6024                 /* word3 command length is described as byte offset to the
6025                  * rsp_data. Would always be 16, sizeof(struct sli4_sge)
6026                  * sgl[0] = cmnd
6027                  * sgl[1] = rsp.
6028                  *
6029                  */
6030                 wqe->gen_req.command_len = xmit_len;
6031                 /* Word4 parameter  copied in the memcpy */
6032                 /* Word5 [rctl, type, df_ctl, la] copied in memcpy */
6033                 /* word6 context tag copied in memcpy */
6034                 if (iocbq->iocb.ulpCt_h  || iocbq->iocb.ulpCt_l) {
6035                         ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
6036                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6037                                 "2015 Invalid CT %x command 0x%x\n",
6038                                 ct, iocbq->iocb.ulpCommand);
6039                         return IOCB_ERROR;
6040                 }
6041                 bf_set(lpfc_wqe_gen_ct, &wqe->generic, 0);
6042                 bf_set(wqe_tmo, &wqe->gen_req.wqe_com,
6043                         iocbq->iocb.ulpTimeout);
6044
6045                 bf_set(lpfc_wqe_gen_pu, &wqe->generic, iocbq->iocb.ulpPU);
6046                 command_type = OTHER_COMMAND;
6047         break;
6048         case CMD_XMIT_ELS_RSP64_CX:
6049                 /* words0-2 BDE memcpy */
6050                 /* word3 iocb=iotag32 wqe=rsvd */
6051                 wqe->words[3] = 0;
6052                 /* word4 iocb=did wge=rsvd. */
6053                 wqe->words[4] = 0;
6054                 /* word5 iocb=rsvd wge=did */
6055                 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
6056                          iocbq->iocb.un.elsreq64.remoteID);
6057
6058                 bf_set(lpfc_wqe_gen_ct, &wqe->generic,
6059                         ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
6060
6061                 bf_set(lpfc_wqe_gen_pu, &wqe->generic, iocbq->iocb.ulpPU);
6062                 bf_set(wqe_rcvoxid, &wqe->generic, iocbq->iocb.ulpContext);
6063                 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
6064                         bf_set(lpfc_wqe_gen_context, &wqe->generic,
6065                                iocbq->vport->vpi + phba->vpi_base);
6066                 command_type = OTHER_COMMAND;
6067         break;
6068         case CMD_CLOSE_XRI_CN:
6069         case CMD_ABORT_XRI_CN:
6070         case CMD_ABORT_XRI_CX:
6071                 /* words 0-2 memcpy should be 0 rserved */
6072                 /* port will send abts */
6073                 if (iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
6074                         /*
6075                          * The link is down so the fw does not need to send abts
6076                          * on the wire.
6077                          */
6078                         bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
6079                 else
6080                         bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
6081                 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
6082                 wqe->words[5] = 0;
6083                 bf_set(lpfc_wqe_gen_ct, &wqe->generic,
6084                         ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
6085                 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
6086                 /*
6087                  * The abort handler will send us CMD_ABORT_XRI_CN or
6088                  * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
6089                  */
6090                 bf_set(lpfc_wqe_gen_command, &wqe->generic, CMD_ABORT_XRI_CX);
6091                 cmnd = CMD_ABORT_XRI_CX;
6092                 command_type = OTHER_COMMAND;
6093                 xritag = 0;
6094         break;
6095         case CMD_XMIT_BLS_RSP64_CX:
6096                 /* As BLS ABTS-ACC WQE is very different from other WQEs,
6097                  * we re-construct this WQE here based on information in
6098                  * iocbq from scratch.
6099                  */
6100                 memset(wqe, 0, sizeof(union lpfc_wqe));
6101                 /* OX_ID is invariable to who sent ABTS to CT exchange */
6102                 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
6103                        bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_acc));
6104                 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_acc) ==
6105                     LPFC_ABTS_UNSOL_INT) {
6106                         /* ABTS sent by initiator to CT exchange, the
6107                          * RX_ID field will be filled with the newly
6108                          * allocated responder XRI.
6109                          */
6110                         bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
6111                                iocbq->sli4_xritag);
6112                 } else {
6113                         /* ABTS sent by responder to CT exchange, the
6114                          * RX_ID field will be filled with the responder
6115                          * RX_ID from ABTS.
6116                          */
6117                         bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
6118                                bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_acc));
6119                 }
6120                 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
6121                 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
6122                 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
6123                        iocbq->iocb.ulpContext);
6124                 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
6125                 command_type = OTHER_COMMAND;
6126         break;
6127         case CMD_XRI_ABORTED_CX:
6128         case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
6129                 /* words0-2 are all 0's no bde */
6130                 /* word3 and word4 are rsvrd */
6131                 wqe->words[3] = 0;
6132                 wqe->words[4] = 0;
6133                 /* word5 iocb=rsvd wge=did */
6134                 /* There is no remote port id in the IOCB? */
6135                 /* Let this fall through and fail */
6136         case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
6137         case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
6138         case CMD_FCP_TRSP64_CX: /* Target mode rcv */
6139         case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
6140         default:
6141                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6142                                 "2014 Invalid command 0x%x\n",
6143                                 iocbq->iocb.ulpCommand);
6144                 return IOCB_ERROR;
6145         break;
6146
6147         }
6148         bf_set(lpfc_wqe_gen_xri, &wqe->generic, xritag);
6149         bf_set(lpfc_wqe_gen_request_tag, &wqe->generic, iocbq->iotag);
6150         wqe->generic.abort_tag = abort_tag;
6151         bf_set(lpfc_wqe_gen_cmd_type, &wqe->generic, command_type);
6152         bf_set(lpfc_wqe_gen_command, &wqe->generic, cmnd);
6153         bf_set(lpfc_wqe_gen_class, &wqe->generic, iocbq->iocb.ulpClass);
6154         bf_set(lpfc_wqe_gen_cq_id, &wqe->generic, LPFC_WQE_CQ_ID_DEFAULT);
6155
6156         return 0;
6157 }
6158
6159 /**
6160  * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
6161  * @phba: Pointer to HBA context object.
6162  * @ring_number: SLI ring number to issue iocb on.
6163  * @piocb: Pointer to command iocb.
6164  * @flag: Flag indicating if this command can be put into txq.
6165  *
6166  * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
6167  * an iocb command to an HBA with SLI-4 interface spec.
6168  *
6169  * This function is called with hbalock held. The function will return success
6170  * after it successfully submit the iocb to firmware or after adding to the
6171  * txq.
6172  **/
6173 static int
6174 __lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
6175                          struct lpfc_iocbq *piocb, uint32_t flag)
6176 {
6177         struct lpfc_sglq *sglq;
6178         uint16_t xritag;
6179         union lpfc_wqe wqe;
6180         struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
6181
6182         if (piocb->sli4_xritag == NO_XRI) {
6183                 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
6184                     piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
6185                         sglq = NULL;
6186                 else {
6187                         sglq = __lpfc_sli_get_sglq(phba);
6188                         if (!sglq)
6189                                 return IOCB_ERROR;
6190                         piocb->sli4_xritag = sglq->sli4_xritag;
6191                 }
6192         } else if (piocb->iocb_flag &  LPFC_IO_FCP) {
6193                 sglq = NULL; /* These IO's already have an XRI and
6194                               * a mapped sgl.
6195                               */
6196         } else {
6197                 /* This is a continuation of a commandi,(CX) so this
6198                  * sglq is on the active list
6199                  */
6200                 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_xritag);
6201                 if (!sglq)
6202                         return IOCB_ERROR;
6203         }
6204
6205         if (sglq) {
6206                 xritag = lpfc_sli4_bpl2sgl(phba, piocb, sglq);
6207                 if (xritag != sglq->sli4_xritag)
6208                         return IOCB_ERROR;
6209         }
6210
6211         if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
6212                 return IOCB_ERROR;
6213
6214         if ((piocb->iocb_flag & LPFC_IO_FCP) ||
6215                 (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
6216                 /*
6217                  * For FCP command IOCB, get a new WQ index to distribute
6218                  * WQE across the WQsr. On the other hand, for abort IOCB,
6219                  * it carries the same WQ index to the original command
6220                  * IOCB.
6221                  */
6222                 if (piocb->iocb_flag & LPFC_IO_FCP)
6223                         piocb->fcp_wqidx = lpfc_sli4_scmd_to_wqidx_distr(phba);
6224                 if (lpfc_sli4_wq_put(phba->sli4_hba.fcp_wq[piocb->fcp_wqidx],
6225                                      &wqe))
6226                         return IOCB_ERROR;
6227         } else {
6228                 if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
6229                         return IOCB_ERROR;
6230         }
6231         lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
6232
6233         return 0;
6234 }
6235
6236 /**
6237  * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
6238  *
6239  * This routine wraps the actual lockless version for issusing IOCB function
6240  * pointer from the lpfc_hba struct.
6241  *
6242  * Return codes:
6243  *      IOCB_ERROR - Error
6244  *      IOCB_SUCCESS - Success
6245  *      IOCB_BUSY - Busy
6246  **/
6247 static inline int
6248 __lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
6249                 struct lpfc_iocbq *piocb, uint32_t flag)
6250 {
6251         return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
6252 }
6253
6254 /**
6255  * lpfc_sli_api_table_setup - Set up sli api fucntion jump table
6256  * @phba: The hba struct for which this call is being executed.
6257  * @dev_grp: The HBA PCI-Device group number.
6258  *
6259  * This routine sets up the SLI interface API function jump table in @phba
6260  * struct.
6261  * Returns: 0 - success, -ENODEV - failure.
6262  **/
6263 int
6264 lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
6265 {
6266
6267         switch (dev_grp) {
6268         case LPFC_PCI_DEV_LP:
6269                 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
6270                 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
6271                 break;
6272         case LPFC_PCI_DEV_OC:
6273                 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
6274                 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
6275                 break;
6276         default:
6277                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6278                                 "1419 Invalid HBA PCI-device group: 0x%x\n",
6279                                 dev_grp);
6280                 return -ENODEV;
6281                 break;
6282         }
6283         phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
6284         return 0;
6285 }
6286
6287 /**
6288  * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
6289  * @phba: Pointer to HBA context object.
6290  * @pring: Pointer to driver SLI ring object.
6291  * @piocb: Pointer to command iocb.
6292  * @flag: Flag indicating if this command can be put into txq.
6293  *
6294  * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
6295  * function. This function gets the hbalock and calls
6296  * __lpfc_sli_issue_iocb function and will return the error returned
6297  * by __lpfc_sli_issue_iocb function. This wrapper is used by
6298  * functions which do not hold hbalock.
6299  **/
6300 int
6301 lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
6302                     struct lpfc_iocbq *piocb, uint32_t flag)
6303 {
6304         unsigned long iflags;
6305         int rc;
6306
6307         spin_lock_irqsave(&phba->hbalock, iflags);
6308         rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
6309         spin_unlock_irqrestore(&phba->hbalock, iflags);
6310
6311         return rc;
6312 }
6313
6314 /**
6315  * lpfc_extra_ring_setup - Extra ring setup function
6316  * @phba: Pointer to HBA context object.
6317  *
6318  * This function is called while driver attaches with the
6319  * HBA to setup the extra ring. The extra ring is used
6320  * only when driver needs to support target mode functionality
6321  * or IP over FC functionalities.
6322  *
6323  * This function is called with no lock held.
6324  **/
6325 static int
6326 lpfc_extra_ring_setup( struct lpfc_hba *phba)
6327 {
6328         struct lpfc_sli *psli;
6329         struct lpfc_sli_ring *pring;
6330
6331         psli = &phba->sli;
6332
6333         /* Adjust cmd/rsp ring iocb entries more evenly */
6334
6335         /* Take some away from the FCP ring */
6336         pring = &psli->ring[psli->fcp_ring];
6337         pring->numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
6338         pring->numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
6339         pring->numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
6340         pring->numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
6341
6342         /* and give them to the extra ring */
6343         pring = &psli->ring[psli->extra_ring];
6344
6345         pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
6346         pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
6347         pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
6348         pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
6349
6350         /* Setup default profile for this ring */
6351         pring->iotag_max = 4096;
6352         pring->num_mask = 1;
6353         pring->prt[0].profile = 0;      /* Mask 0 */
6354         pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
6355         pring->prt[0].type = phba->cfg_multi_ring_type;
6356         pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
6357         return 0;
6358 }
6359
6360 /**
6361  * lpfc_sli_async_event_handler - ASYNC iocb handler function
6362  * @phba: Pointer to HBA context object.
6363  * @pring: Pointer to driver SLI ring object.
6364  * @iocbq: Pointer to iocb object.
6365  *
6366  * This function is called by the slow ring event handler
6367  * function when there is an ASYNC event iocb in the ring.
6368  * This function is called with no lock held.
6369  * Currently this function handles only temperature related
6370  * ASYNC events. The function decodes the temperature sensor
6371  * event message and posts events for the management applications.
6372  **/
6373 static void
6374 lpfc_sli_async_event_handler(struct lpfc_hba * phba,
6375         struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
6376 {
6377         IOCB_t *icmd;
6378         uint16_t evt_code;
6379         uint16_t temp;
6380         struct temp_event temp_event_data;
6381         struct Scsi_Host *shost;
6382         uint32_t *iocb_w;
6383
6384         icmd = &iocbq->iocb;
6385         evt_code = icmd->un.asyncstat.evt_code;
6386         temp = icmd->ulpContext;
6387
6388         if ((evt_code != ASYNC_TEMP_WARN) &&
6389                 (evt_code != ASYNC_TEMP_SAFE)) {
6390                 iocb_w = (uint32_t *) icmd;
6391                 lpfc_printf_log(phba,
6392                         KERN_ERR,
6393                         LOG_SLI,
6394                         "0346 Ring %d handler: unexpected ASYNC_STATUS"
6395                         " evt_code 0x%x\n"
6396                         "W0  0x%08x W1  0x%08x W2  0x%08x W3  0x%08x\n"
6397                         "W4  0x%08x W5  0x%08x W6  0x%08x W7  0x%08x\n"
6398                         "W8  0x%08x W9  0x%08x W10 0x%08x W11 0x%08x\n"
6399                         "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
6400                         pring->ringno,
6401                         icmd->un.asyncstat.evt_code,
6402                         iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
6403                         iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
6404                         iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
6405                         iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
6406
6407                 return;
6408         }
6409         temp_event_data.data = (uint32_t)temp;
6410         temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
6411         if (evt_code == ASYNC_TEMP_WARN) {
6412                 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
6413                 lpfc_printf_log(phba,
6414                                 KERN_ERR,
6415                                 LOG_TEMP,
6416                                 "0347 Adapter is very hot, please take "
6417                                 "corrective action. temperature : %d Celsius\n",
6418                                 temp);
6419         }
6420         if (evt_code == ASYNC_TEMP_SAFE) {
6421                 temp_event_data.event_code = LPFC_NORMAL_TEMP;
6422                 lpfc_printf_log(phba,
6423                                 KERN_ERR,
6424                                 LOG_TEMP,
6425                                 "0340 Adapter temperature is OK now. "
6426                                 "temperature : %d Celsius\n",
6427                                 temp);
6428         }
6429
6430         /* Send temperature change event to applications */
6431         shost = lpfc_shost_from_vport(phba->pport);
6432         fc_host_post_vendor_event(shost, fc_get_event_number(),
6433                 sizeof(temp_event_data), (char *) &temp_event_data,
6434                 LPFC_NL_VENDOR_ID);
6435
6436 }
6437
6438
6439 /**
6440  * lpfc_sli_setup - SLI ring setup function
6441  * @phba: Pointer to HBA context object.
6442  *
6443  * lpfc_sli_setup sets up rings of the SLI interface with
6444  * number of iocbs per ring and iotags. This function is
6445  * called while driver attach to the HBA and before the
6446  * interrupts are enabled. So there is no need for locking.
6447  *
6448  * This function always returns 0.
6449  **/
6450 int
6451 lpfc_sli_setup(struct lpfc_hba *phba)
6452 {
6453         int i, totiocbsize = 0;
6454         struct lpfc_sli *psli = &phba->sli;
6455         struct lpfc_sli_ring *pring;
6456
6457         psli->num_rings = MAX_CONFIGURED_RINGS;
6458         psli->sli_flag = 0;
6459         psli->fcp_ring = LPFC_FCP_RING;
6460         psli->next_ring = LPFC_FCP_NEXT_RING;
6461         psli->extra_ring = LPFC_EXTRA_RING;
6462
6463         psli->iocbq_lookup = NULL;
6464         psli->iocbq_lookup_len = 0;
6465         psli->last_iotag = 0;
6466
6467         for (i = 0; i < psli->num_rings; i++) {
6468                 pring = &psli->ring[i];
6469                 switch (i) {
6470                 case LPFC_FCP_RING:     /* ring 0 - FCP */
6471                         /* numCiocb and numRiocb are used in config_port */
6472                         pring->numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
6473                         pring->numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
6474                         pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
6475                         pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
6476                         pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
6477                         pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
6478                         pring->sizeCiocb = (phba->sli_rev == 3) ?
6479                                                         SLI3_IOCB_CMD_SIZE :
6480                                                         SLI2_IOCB_CMD_SIZE;
6481                         pring->sizeRiocb = (phba->sli_rev == 3) ?
6482                                                         SLI3_IOCB_RSP_SIZE :
6483                                                         SLI2_IOCB_RSP_SIZE;
6484                         pring->iotag_ctr = 0;
6485                         pring->iotag_max =
6486                             (phba->cfg_hba_queue_depth * 2);
6487                         pring->fast_iotag = pring->iotag_max;
6488                         pring->num_mask = 0;
6489                         break;
6490                 case LPFC_EXTRA_RING:   /* ring 1 - EXTRA */
6491                         /* numCiocb and numRiocb are used in config_port */
6492                         pring->numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
6493                         pring->numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
6494                         pring->sizeCiocb = (phba->sli_rev == 3) ?
6495                                                         SLI3_IOCB_CMD_SIZE :
6496                                                         SLI2_IOCB_CMD_SIZE;
6497                         pring->sizeRiocb = (phba->sli_rev == 3) ?
6498                                                         SLI3_IOCB_RSP_SIZE :
6499                                                         SLI2_IOCB_RSP_SIZE;
6500                         pring->iotag_max = phba->cfg_hba_queue_depth;
6501                         pring->num_mask = 0;
6502                         break;
6503                 case LPFC_ELS_RING:     /* ring 2 - ELS / CT */
6504                         /* numCiocb and numRiocb are used in config_port */
6505                         pring->numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
6506                         pring->numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
6507                         pring->sizeCiocb = (phba->sli_rev == 3) ?
6508                                                         SLI3_IOCB_CMD_SIZE :
6509                                                         SLI2_IOCB_CMD_SIZE;
6510                         pring->sizeRiocb = (phba->sli_rev == 3) ?
6511                                                         SLI3_IOCB_RSP_SIZE :
6512                                                         SLI2_IOCB_RSP_SIZE;
6513                         pring->fast_iotag = 0;
6514                         pring->iotag_ctr = 0;
6515                         pring->iotag_max = 4096;
6516                         pring->lpfc_sli_rcv_async_status =
6517                                 lpfc_sli_async_event_handler;
6518                         pring->num_mask = LPFC_MAX_RING_MASK;
6519                         pring->prt[0].profile = 0;      /* Mask 0 */
6520                         pring->prt[0].rctl = FC_RCTL_ELS_REQ;
6521                         pring->prt[0].type = FC_TYPE_ELS;
6522                         pring->prt[0].lpfc_sli_rcv_unsol_event =
6523                             lpfc_els_unsol_event;
6524                         pring->prt[1].profile = 0;      /* Mask 1 */
6525                         pring->prt[1].rctl = FC_RCTL_ELS_REP;
6526                         pring->prt[1].type = FC_TYPE_ELS;
6527                         pring->prt[1].lpfc_sli_rcv_unsol_event =
6528                             lpfc_els_unsol_event;
6529                         pring->prt[2].profile = 0;      /* Mask 2 */
6530                         /* NameServer Inquiry */
6531                         pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
6532                         /* NameServer */
6533                         pring->prt[2].type = FC_TYPE_CT;
6534                         pring->prt[2].lpfc_sli_rcv_unsol_event =
6535                             lpfc_ct_unsol_event;
6536                         pring->prt[3].profile = 0;      /* Mask 3 */
6537                         /* NameServer response */
6538                         pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
6539                         /* NameServer */
6540                         pring->prt[3].type = FC_TYPE_CT;
6541                         pring->prt[3].lpfc_sli_rcv_unsol_event =
6542                             lpfc_ct_unsol_event;
6543                         /* abort unsolicited sequence */
6544                         pring->prt[4].profile = 0;      /* Mask 4 */
6545                         pring->prt[4].rctl = FC_RCTL_BA_ABTS;
6546                         pring->prt[4].type = FC_TYPE_BLS;
6547                         pring->prt[4].lpfc_sli_rcv_unsol_event =
6548                             lpfc_sli4_ct_abort_unsol_event;
6549                         break;
6550                 }
6551                 totiocbsize += (pring->numCiocb * pring->sizeCiocb) +
6552                                 (pring->numRiocb * pring->sizeRiocb);
6553         }
6554         if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
6555                 /* Too many cmd / rsp ring entries in SLI2 SLIM */
6556                 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
6557                        "SLI2 SLIM Data: x%x x%lx\n",
6558                        phba->brd_no, totiocbsize,
6559                        (unsigned long) MAX_SLIM_IOCB_SIZE);
6560         }
6561         if (phba->cfg_multi_ring_support == 2)
6562                 lpfc_extra_ring_setup(phba);
6563
6564         return 0;
6565 }
6566
6567 /**
6568  * lpfc_sli_queue_setup - Queue initialization function
6569  * @phba: Pointer to HBA context object.
6570  *
6571  * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
6572  * ring. This function also initializes ring indices of each ring.
6573  * This function is called during the initialization of the SLI
6574  * interface of an HBA.
6575  * This function is called with no lock held and always returns
6576  * 1.
6577  **/
6578 int
6579 lpfc_sli_queue_setup(struct lpfc_hba *phba)
6580 {
6581         struct lpfc_sli *psli;
6582         struct lpfc_sli_ring *pring;
6583         int i;
6584
6585         psli = &phba->sli;
6586         spin_lock_irq(&phba->hbalock);
6587         INIT_LIST_HEAD(&psli->mboxq);
6588         INIT_LIST_HEAD(&psli->mboxq_cmpl);
6589         /* Initialize list headers for txq and txcmplq as double linked lists */
6590         for (i = 0; i < psli->num_rings; i++) {
6591                 pring = &psli->ring[i];
6592                 pring->ringno = i;
6593                 pring->next_cmdidx  = 0;
6594                 pring->local_getidx = 0;
6595                 pring->cmdidx = 0;
6596                 INIT_LIST_HEAD(&pring->txq);
6597                 INIT_LIST_HEAD(&pring->txcmplq);
6598                 INIT_LIST_HEAD(&pring->iocb_continueq);
6599                 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
6600                 INIT_LIST_HEAD(&pring->postbufq);
6601         }
6602         spin_unlock_irq(&phba->hbalock);
6603         return 1;
6604 }
6605
6606 /**
6607  * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
6608  * @phba: Pointer to HBA context object.
6609  *
6610  * This routine flushes the mailbox command subsystem. It will unconditionally
6611  * flush all the mailbox commands in the three possible stages in the mailbox
6612  * command sub-system: pending mailbox command queue; the outstanding mailbox
6613  * command; and completed mailbox command queue. It is caller's responsibility
6614  * to make sure that the driver is in the proper state to flush the mailbox
6615  * command sub-system. Namely, the posting of mailbox commands into the
6616  * pending mailbox command queue from the various clients must be stopped;
6617  * either the HBA is in a state that it will never works on the outstanding
6618  * mailbox command (such as in EEH or ERATT conditions) or the outstanding
6619  * mailbox command has been completed.
6620  **/
6621 static void
6622 lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
6623 {
6624         LIST_HEAD(completions);
6625         struct lpfc_sli *psli = &phba->sli;
6626         LPFC_MBOXQ_t *pmb;
6627         unsigned long iflag;
6628
6629         /* Flush all the mailbox commands in the mbox system */
6630         spin_lock_irqsave(&phba->hbalock, iflag);
6631         /* The pending mailbox command queue */
6632         list_splice_init(&phba->sli.mboxq, &completions);
6633         /* The outstanding active mailbox command */
6634         if (psli->mbox_active) {
6635                 list_add_tail(&psli->mbox_active->list, &completions);
6636                 psli->mbox_active = NULL;
6637                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
6638         }
6639         /* The completed mailbox command queue */
6640         list_splice_init(&phba->sli.mboxq_cmpl, &completions);
6641         spin_unlock_irqrestore(&phba->hbalock, iflag);
6642
6643         /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
6644         while (!list_empty(&completions)) {
6645                 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
6646                 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
6647                 if (pmb->mbox_cmpl)
6648                         pmb->mbox_cmpl(phba, pmb);
6649         }
6650 }
6651
6652 /**
6653  * lpfc_sli_host_down - Vport cleanup function
6654  * @vport: Pointer to virtual port object.
6655  *
6656  * lpfc_sli_host_down is called to clean up the resources
6657  * associated with a vport before destroying virtual
6658  * port data structures.
6659  * This function does following operations:
6660  * - Free discovery resources associated with this virtual
6661  *   port.
6662  * - Free iocbs associated with this virtual port in
6663  *   the txq.
6664  * - Send abort for all iocb commands associated with this
6665  *   vport in txcmplq.
6666  *
6667  * This function is called with no lock held and always returns 1.
6668  **/
6669 int
6670 lpfc_sli_host_down(struct lpfc_vport *vport)
6671 {
6672         LIST_HEAD(completions);
6673         struct lpfc_hba *phba = vport->phba;
6674         struct lpfc_sli *psli = &phba->sli;
6675         struct lpfc_sli_ring *pring;
6676         struct lpfc_iocbq *iocb, *next_iocb;
6677         int i;
6678         unsigned long flags = 0;
6679         uint16_t prev_pring_flag;
6680
6681         lpfc_cleanup_discovery_resources(vport);
6682
6683         spin_lock_irqsave(&phba->hbalock, flags);
6684         for (i = 0; i < psli->num_rings; i++) {
6685                 pring = &psli->ring[i];
6686                 prev_pring_flag = pring->flag;
6687                 /* Only slow rings */
6688                 if (pring->ringno == LPFC_ELS_RING) {
6689                         pring->flag |= LPFC_DEFERRED_RING_EVENT;
6690                         /* Set the lpfc data pending flag */
6691                         set_bit(LPFC_DATA_READY, &phba->data_flags);
6692                 }
6693                 /*
6694                  * Error everything on the txq since these iocbs have not been
6695                  * given to the FW yet.
6696                  */
6697                 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
6698                         if (iocb->vport != vport)
6699                                 continue;
6700                         list_move_tail(&iocb->list, &completions);
6701                         pring->txq_cnt--;
6702                 }
6703
6704                 /* Next issue ABTS for everything on the txcmplq */
6705                 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
6706                                                                         list) {
6707                         if (iocb->vport != vport)
6708                                 continue;
6709                         lpfc_sli_issue_abort_iotag(phba, pring, iocb);
6710                 }
6711
6712                 pring->flag = prev_pring_flag;
6713         }
6714
6715         spin_unlock_irqrestore(&phba->hbalock, flags);
6716
6717         /* Cancel all the IOCBs from the completions list */
6718         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
6719                               IOERR_SLI_DOWN);
6720         return 1;
6721 }
6722
6723 /**
6724  * lpfc_sli_hba_down - Resource cleanup function for the HBA
6725  * @phba: Pointer to HBA context object.
6726  *
6727  * This function cleans up all iocb, buffers, mailbox commands
6728  * while shutting down the HBA. This function is called with no
6729  * lock held and always returns 1.
6730  * This function does the following to cleanup driver resources:
6731  * - Free discovery resources for each virtual port
6732  * - Cleanup any pending fabric iocbs
6733  * - Iterate through the iocb txq and free each entry
6734  *   in the list.
6735  * - Free up any buffer posted to the HBA
6736  * - Free mailbox commands in the mailbox queue.
6737  **/
6738 int
6739 lpfc_sli_hba_down(struct lpfc_hba *phba)
6740 {
6741         LIST_HEAD(completions);
6742         struct lpfc_sli *psli = &phba->sli;
6743         struct lpfc_sli_ring *pring;
6744         struct lpfc_dmabuf *buf_ptr;
6745         unsigned long flags = 0;
6746         int i;
6747
6748         /* Shutdown the mailbox command sub-system */
6749         lpfc_sli_mbox_sys_shutdown(phba);
6750
6751         lpfc_hba_down_prep(phba);
6752
6753         lpfc_fabric_abort_hba(phba);
6754
6755         spin_lock_irqsave(&phba->hbalock, flags);
6756         for (i = 0; i < psli->num_rings; i++) {
6757                 pring = &psli->ring[i];
6758                 /* Only slow rings */
6759                 if (pring->ringno == LPFC_ELS_RING) {
6760                         pring->flag |= LPFC_DEFERRED_RING_EVENT;
6761                         /* Set the lpfc data pending flag */
6762                         set_bit(LPFC_DATA_READY, &phba->data_flags);
6763                 }
6764
6765                 /*
6766                  * Error everything on the txq since these iocbs have not been
6767                  * given to the FW yet.
6768                  */
6769                 list_splice_init(&pring->txq, &completions);
6770                 pring->txq_cnt = 0;
6771
6772         }
6773         spin_unlock_irqrestore(&phba->hbalock, flags);
6774
6775         /* Cancel all the IOCBs from the completions list */
6776         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
6777                               IOERR_SLI_DOWN);
6778
6779         spin_lock_irqsave(&phba->hbalock, flags);
6780         list_splice_init(&phba->elsbuf, &completions);
6781         phba->elsbuf_cnt = 0;
6782         phba->elsbuf_prev_cnt = 0;
6783         spin_unlock_irqrestore(&phba->hbalock, flags);
6784
6785         while (!list_empty(&completions)) {
6786                 list_remove_head(&completions, buf_ptr,
6787                         struct lpfc_dmabuf, list);
6788                 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
6789                 kfree(buf_ptr);
6790         }
6791
6792         /* Return any active mbox cmds */
6793         del_timer_sync(&psli->mbox_tmo);
6794
6795         spin_lock_irqsave(&phba->pport->work_port_lock, flags);
6796         phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
6797         spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
6798
6799         return 1;
6800 }
6801
6802 /**
6803  * lpfc_sli4_hba_down - PCI function resource cleanup for the SLI4 HBA
6804  * @phba: Pointer to HBA context object.
6805  *
6806  * This function cleans up all queues, iocb, buffers, mailbox commands while
6807  * shutting down the SLI4 HBA FCoE function. This function is called with no
6808  * lock held and always returns 1.
6809  *
6810  * This function does the following to cleanup driver FCoE function resources:
6811  * - Free discovery resources for each virtual port
6812  * - Cleanup any pending fabric iocbs
6813  * - Iterate through the iocb txq and free each entry in the list.
6814  * - Free up any buffer posted to the HBA.
6815  * - Clean up all the queue entries: WQ, RQ, MQ, EQ, CQ, etc.
6816  * - Free mailbox commands in the mailbox queue.
6817  **/
6818 int
6819 lpfc_sli4_hba_down(struct lpfc_hba *phba)
6820 {
6821         /* Stop the SLI4 device port */
6822         lpfc_stop_port(phba);
6823
6824         /* Tear down the queues in the HBA */
6825         lpfc_sli4_queue_unset(phba);
6826
6827         /* unregister default FCFI from the HBA */
6828         lpfc_sli4_fcfi_unreg(phba, phba->fcf.fcfi);
6829
6830         return 1;
6831 }
6832
6833 /**
6834  * lpfc_sli_pcimem_bcopy - SLI memory copy function
6835  * @srcp: Source memory pointer.
6836  * @destp: Destination memory pointer.
6837  * @cnt: Number of words required to be copied.
6838  *
6839  * This function is used for copying data between driver memory
6840  * and the SLI memory. This function also changes the endianness
6841  * of each word if native endianness is different from SLI
6842  * endianness. This function can be called with or without
6843  * lock.
6844  **/
6845 void
6846 lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
6847 {
6848         uint32_t *src = srcp;
6849         uint32_t *dest = destp;
6850         uint32_t ldata;
6851         int i;
6852
6853         for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
6854                 ldata = *src;
6855                 ldata = le32_to_cpu(ldata);
6856                 *dest = ldata;
6857                 src++;
6858                 dest++;
6859         }
6860 }
6861
6862
6863 /**
6864  * lpfc_sli_bemem_bcopy - SLI memory copy function
6865  * @srcp: Source memory pointer.
6866  * @destp: Destination memory pointer.
6867  * @cnt: Number of words required to be copied.
6868  *
6869  * This function is used for copying data between a data structure
6870  * with big endian representation to local endianness.
6871  * This function can be called with or without lock.
6872  **/
6873 void
6874 lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
6875 {
6876         uint32_t *src = srcp;
6877         uint32_t *dest = destp;
6878         uint32_t ldata;
6879         int i;
6880
6881         for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
6882                 ldata = *src;
6883                 ldata = be32_to_cpu(ldata);
6884                 *dest = ldata;
6885                 src++;
6886                 dest++;
6887         }
6888 }
6889
6890 /**
6891  * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
6892  * @phba: Pointer to HBA context object.
6893  * @pring: Pointer to driver SLI ring object.
6894  * @mp: Pointer to driver buffer object.
6895  *
6896  * This function is called with no lock held.
6897  * It always return zero after adding the buffer to the postbufq
6898  * buffer list.
6899  **/
6900 int
6901 lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
6902                          struct lpfc_dmabuf *mp)
6903 {
6904         /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
6905            later */
6906         spin_lock_irq(&phba->hbalock);
6907         list_add_tail(&mp->list, &pring->postbufq);
6908         pring->postbufq_cnt++;
6909         spin_unlock_irq(&phba->hbalock);
6910         return 0;
6911 }
6912
6913 /**
6914  * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
6915  * @phba: Pointer to HBA context object.
6916  *
6917  * When HBQ is enabled, buffers are searched based on tags. This function
6918  * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
6919  * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
6920  * does not conflict with tags of buffer posted for unsolicited events.
6921  * The function returns the allocated tag. The function is called with
6922  * no locks held.
6923  **/
6924 uint32_t
6925 lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
6926 {
6927         spin_lock_irq(&phba->hbalock);
6928         phba->buffer_tag_count++;
6929         /*
6930          * Always set the QUE_BUFTAG_BIT to distiguish between
6931          * a tag assigned by HBQ.
6932          */
6933         phba->buffer_tag_count |= QUE_BUFTAG_BIT;
6934         spin_unlock_irq(&phba->hbalock);
6935         return phba->buffer_tag_count;
6936 }
6937
6938 /**
6939  * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
6940  * @phba: Pointer to HBA context object.
6941  * @pring: Pointer to driver SLI ring object.
6942  * @tag: Buffer tag.
6943  *
6944  * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
6945  * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
6946  * iocb is posted to the response ring with the tag of the buffer.
6947  * This function searches the pring->postbufq list using the tag
6948  * to find buffer associated with CMD_IOCB_RET_XRI64_CX
6949  * iocb. If the buffer is found then lpfc_dmabuf object of the
6950  * buffer is returned to the caller else NULL is returned.
6951  * This function is called with no lock held.
6952  **/
6953 struct lpfc_dmabuf *
6954 lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
6955                         uint32_t tag)
6956 {
6957         struct lpfc_dmabuf *mp, *next_mp;
6958         struct list_head *slp = &pring->postbufq;
6959
6960         /* Search postbufq, from the begining, looking for a match on tag */
6961         spin_lock_irq(&phba->hbalock);
6962         list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
6963                 if (mp->buffer_tag == tag) {
6964                         list_del_init(&mp->list);
6965                         pring->postbufq_cnt--;
6966                         spin_unlock_irq(&phba->hbalock);
6967                         return mp;
6968                 }
6969         }
6970
6971         spin_unlock_irq(&phba->hbalock);
6972         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6973                         "0402 Cannot find virtual addr for buffer tag on "
6974                         "ring %d Data x%lx x%p x%p x%x\n",
6975                         pring->ringno, (unsigned long) tag,
6976                         slp->next, slp->prev, pring->postbufq_cnt);
6977
6978         return NULL;
6979 }
6980
6981 /**
6982  * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
6983  * @phba: Pointer to HBA context object.
6984  * @pring: Pointer to driver SLI ring object.
6985  * @phys: DMA address of the buffer.
6986  *
6987  * This function searches the buffer list using the dma_address
6988  * of unsolicited event to find the driver's lpfc_dmabuf object
6989  * corresponding to the dma_address. The function returns the
6990  * lpfc_dmabuf object if a buffer is found else it returns NULL.
6991  * This function is called by the ct and els unsolicited event
6992  * handlers to get the buffer associated with the unsolicited
6993  * event.
6994  *
6995  * This function is called with no lock held.
6996  **/
6997 struct lpfc_dmabuf *
6998 lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
6999                          dma_addr_t phys)
7000 {
7001         struct lpfc_dmabuf *mp, *next_mp;
7002         struct list_head *slp = &pring->postbufq;
7003
7004         /* Search postbufq, from the begining, looking for a match on phys */
7005         spin_lock_irq(&phba->hbalock);
7006         list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
7007                 if (mp->phys == phys) {
7008                         list_del_init(&mp->list);
7009                         pring->postbufq_cnt--;
7010                         spin_unlock_irq(&phba->hbalock);
7011                         return mp;
7012                 }
7013         }
7014
7015         spin_unlock_irq(&phba->hbalock);
7016         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7017                         "0410 Cannot find virtual addr for mapped buf on "
7018                         "ring %d Data x%llx x%p x%p x%x\n",
7019                         pring->ringno, (unsigned long long)phys,
7020                         slp->next, slp->prev, pring->postbufq_cnt);
7021         return NULL;
7022 }
7023
7024 /**
7025  * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
7026  * @phba: Pointer to HBA context object.
7027  * @cmdiocb: Pointer to driver command iocb object.
7028  * @rspiocb: Pointer to driver response iocb object.
7029  *
7030  * This function is the completion handler for the abort iocbs for
7031  * ELS commands. This function is called from the ELS ring event
7032  * handler with no lock held. This function frees memory resources
7033  * associated with the abort iocb.
7034  **/
7035 static void
7036 lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7037                         struct lpfc_iocbq *rspiocb)
7038 {
7039         IOCB_t *irsp = &rspiocb->iocb;
7040         uint16_t abort_iotag, abort_context;
7041         struct lpfc_iocbq *abort_iocb;
7042         struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
7043
7044         abort_iocb = NULL;
7045
7046         if (irsp->ulpStatus) {
7047                 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
7048                 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
7049
7050                 spin_lock_irq(&phba->hbalock);
7051                 if (phba->sli_rev < LPFC_SLI_REV4) {
7052                         if (abort_iotag != 0 &&
7053                                 abort_iotag <= phba->sli.last_iotag)
7054                                 abort_iocb =
7055                                         phba->sli.iocbq_lookup[abort_iotag];
7056                 } else
7057                         /* For sli4 the abort_tag is the XRI,
7058                          * so the abort routine puts the iotag  of the iocb
7059                          * being aborted in the context field of the abort
7060                          * IOCB.
7061                          */
7062                         abort_iocb = phba->sli.iocbq_lookup[abort_context];
7063
7064                 lpfc_printf_log(phba, KERN_INFO, LOG_ELS | LOG_SLI,
7065                                 "0327 Cannot abort els iocb %p "
7066                                 "with tag %x context %x, abort status %x, "
7067                                 "abort code %x\n",
7068                                 abort_iocb, abort_iotag, abort_context,
7069                                 irsp->ulpStatus, irsp->un.ulpWord[4]);
7070
7071                 /*
7072                  *  If the iocb is not found in Firmware queue the iocb
7073                  *  might have completed already. Do not free it again.
7074                  */
7075                 if (irsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
7076                         if (irsp->un.ulpWord[4] != IOERR_NO_XRI) {
7077                                 spin_unlock_irq(&phba->hbalock);
7078                                 lpfc_sli_release_iocbq(phba, cmdiocb);
7079                                 return;
7080                         }
7081                         /* For SLI4 the ulpContext field for abort IOCB
7082                          * holds the iotag of the IOCB being aborted so
7083                          * the local abort_context needs to be reset to
7084                          * match the aborted IOCBs ulpContext.
7085                          */
7086                         if (abort_iocb && phba->sli_rev == LPFC_SLI_REV4)
7087                                 abort_context = abort_iocb->iocb.ulpContext;
7088                 }
7089                 /*
7090                  * make sure we have the right iocbq before taking it
7091                  * off the txcmplq and try to call completion routine.
7092                  */
7093                 if (!abort_iocb ||
7094                     abort_iocb->iocb.ulpContext != abort_context ||
7095                     (abort_iocb->iocb_flag & LPFC_DRIVER_ABORTED) == 0)
7096                         spin_unlock_irq(&phba->hbalock);
7097                 else if (phba->sli_rev < LPFC_SLI_REV4) {
7098                         /*
7099                          * leave the SLI4 aborted command on the txcmplq
7100                          * list and the command complete WCQE's XB bit
7101                          * will tell whether the SGL (XRI) can be released
7102                          * immediately or to the aborted SGL list for the
7103                          * following abort XRI from the HBA.
7104                          */
7105                         list_del_init(&abort_iocb->list);
7106                         pring->txcmplq_cnt--;
7107                         spin_unlock_irq(&phba->hbalock);
7108
7109                         /* Firmware could still be in progress of DMAing
7110                          * payload, so don't free data buffer till after
7111                          * a hbeat.
7112                          */
7113                         spin_lock_irq(&phba->hbalock);
7114                         abort_iocb->iocb_flag |= LPFC_DELAY_MEM_FREE;
7115                         abort_iocb->iocb_flag &= ~LPFC_DRIVER_ABORTED;
7116                         spin_unlock_irq(&phba->hbalock);
7117
7118                         abort_iocb->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
7119                         abort_iocb->iocb.un.ulpWord[4] = IOERR_ABORT_REQUESTED;
7120                         (abort_iocb->iocb_cmpl)(phba, abort_iocb, abort_iocb);
7121                 }
7122         }
7123
7124         lpfc_sli_release_iocbq(phba, cmdiocb);
7125         return;
7126 }
7127
7128 /**
7129  * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
7130  * @phba: Pointer to HBA context object.
7131  * @cmdiocb: Pointer to driver command iocb object.
7132  * @rspiocb: Pointer to driver response iocb object.
7133  *
7134  * The function is called from SLI ring event handler with no
7135  * lock held. This function is the completion handler for ELS commands
7136  * which are aborted. The function frees memory resources used for
7137  * the aborted ELS commands.
7138  **/
7139 static void
7140 lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7141                      struct lpfc_iocbq *rspiocb)
7142 {
7143         IOCB_t *irsp = &rspiocb->iocb;
7144
7145         /* ELS cmd tag <ulpIoTag> completes */
7146         lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
7147                         "0139 Ignoring ELS cmd tag x%x completion Data: "
7148                         "x%x x%x x%x\n",
7149                         irsp->ulpIoTag, irsp->ulpStatus,
7150                         irsp->un.ulpWord[4], irsp->ulpTimeout);
7151         if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
7152                 lpfc_ct_free_iocb(phba, cmdiocb);
7153         else
7154                 lpfc_els_free_iocb(phba, cmdiocb);
7155         return;
7156 }
7157
7158 /**
7159  * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
7160  * @phba: Pointer to HBA context object.
7161  * @pring: Pointer to driver SLI ring object.
7162  * @cmdiocb: Pointer to driver command iocb object.
7163  *
7164  * This function issues an abort iocb for the provided command
7165  * iocb. This function is called with hbalock held.
7166  * The function returns 0 when it fails due to memory allocation
7167  * failure or when the command iocb is an abort request.
7168  **/
7169 int
7170 lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7171                            struct lpfc_iocbq *cmdiocb)
7172 {
7173         struct lpfc_vport *vport = cmdiocb->vport;
7174         struct lpfc_iocbq *abtsiocbp;
7175         IOCB_t *icmd = NULL;
7176         IOCB_t *iabt = NULL;
7177         int retval = IOCB_ERROR;
7178
7179         /*
7180          * There are certain command types we don't want to abort.  And we
7181          * don't want to abort commands that are already in the process of
7182          * being aborted.
7183          */
7184         icmd = &cmdiocb->iocb;
7185         if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
7186             icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
7187             (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
7188                 return 0;
7189
7190         /* If we're unloading, don't abort iocb on the ELS ring, but change the
7191          * callback so that nothing happens when it finishes.
7192          */
7193         if ((vport->load_flag & FC_UNLOADING) &&
7194             (pring->ringno == LPFC_ELS_RING)) {
7195                 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
7196                         cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
7197                 else
7198                         cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
7199                 goto abort_iotag_exit;
7200         }
7201
7202         /* issue ABTS for this IOCB based on iotag */
7203         abtsiocbp = __lpfc_sli_get_iocbq(phba);
7204         if (abtsiocbp == NULL)
7205                 return 0;
7206
7207         /* This signals the response to set the correct status
7208          * before calling the completion handler
7209          */
7210         cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
7211
7212         iabt = &abtsiocbp->iocb;
7213         iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
7214         iabt->un.acxri.abortContextTag = icmd->ulpContext;
7215         if (phba->sli_rev == LPFC_SLI_REV4) {
7216                 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
7217                 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
7218         }
7219         else
7220                 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
7221         iabt->ulpLe = 1;
7222         iabt->ulpClass = icmd->ulpClass;
7223
7224         /* ABTS WQE must go to the same WQ as the WQE to be aborted */
7225         abtsiocbp->fcp_wqidx = cmdiocb->fcp_wqidx;
7226         if (cmdiocb->iocb_flag & LPFC_IO_FCP)
7227                 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
7228
7229         if (phba->link_state >= LPFC_LINK_UP)
7230                 iabt->ulpCommand = CMD_ABORT_XRI_CN;
7231         else
7232                 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
7233
7234         abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
7235
7236         lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
7237                          "0339 Abort xri x%x, original iotag x%x, "
7238                          "abort cmd iotag x%x\n",
7239                          iabt->un.acxri.abortContextTag,
7240                          iabt->un.acxri.abortIoTag, abtsiocbp->iotag);
7241         retval = __lpfc_sli_issue_iocb(phba, pring->ringno, abtsiocbp, 0);
7242
7243         if (retval)
7244                 __lpfc_sli_release_iocbq(phba, abtsiocbp);
7245 abort_iotag_exit:
7246         /*
7247          * Caller to this routine should check for IOCB_ERROR
7248          * and handle it properly.  This routine no longer removes
7249          * iocb off txcmplq and call compl in case of IOCB_ERROR.
7250          */
7251         return retval;
7252 }
7253
7254 /**
7255  * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
7256  * @iocbq: Pointer to driver iocb object.
7257  * @vport: Pointer to driver virtual port object.
7258  * @tgt_id: SCSI ID of the target.
7259  * @lun_id: LUN ID of the scsi device.
7260  * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
7261  *
7262  * This function acts as an iocb filter for functions which abort or count
7263  * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
7264  * 0 if the filtering criteria is met for the given iocb and will return
7265  * 1 if the filtering criteria is not met.
7266  * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
7267  * given iocb is for the SCSI device specified by vport, tgt_id and
7268  * lun_id parameter.
7269  * If ctx_cmd == LPFC_CTX_TGT,  the function returns 0 only if the
7270  * given iocb is for the SCSI target specified by vport and tgt_id
7271  * parameters.
7272  * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
7273  * given iocb is for the SCSI host associated with the given vport.
7274  * This function is called with no locks held.
7275  **/
7276 static int
7277 lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
7278                            uint16_t tgt_id, uint64_t lun_id,
7279                            lpfc_ctx_cmd ctx_cmd)
7280 {
7281         struct lpfc_scsi_buf *lpfc_cmd;
7282         int rc = 1;
7283
7284         if (!(iocbq->iocb_flag &  LPFC_IO_FCP))
7285                 return rc;
7286
7287         if (iocbq->vport != vport)
7288                 return rc;
7289
7290         lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
7291
7292         if (lpfc_cmd->pCmd == NULL)
7293                 return rc;
7294
7295         switch (ctx_cmd) {
7296         case LPFC_CTX_LUN:
7297                 if ((lpfc_cmd->rdata->pnode) &&
7298                     (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
7299                     (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
7300                         rc = 0;
7301                 break;
7302         case LPFC_CTX_TGT:
7303                 if ((lpfc_cmd->rdata->pnode) &&
7304                     (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
7305                         rc = 0;
7306                 break;
7307         case LPFC_CTX_HOST:
7308                 rc = 0;
7309                 break;
7310         default:
7311                 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
7312                         __func__, ctx_cmd);
7313                 break;
7314         }
7315
7316         return rc;
7317 }
7318
7319 /**
7320  * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
7321  * @vport: Pointer to virtual port.
7322  * @tgt_id: SCSI ID of the target.
7323  * @lun_id: LUN ID of the scsi device.
7324  * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
7325  *
7326  * This function returns number of FCP commands pending for the vport.
7327  * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
7328  * commands pending on the vport associated with SCSI device specified
7329  * by tgt_id and lun_id parameters.
7330  * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
7331  * commands pending on the vport associated with SCSI target specified
7332  * by tgt_id parameter.
7333  * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
7334  * commands pending on the vport.
7335  * This function returns the number of iocbs which satisfy the filter.
7336  * This function is called without any lock held.
7337  **/
7338 int
7339 lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
7340                   lpfc_ctx_cmd ctx_cmd)
7341 {
7342         struct lpfc_hba *phba = vport->phba;
7343         struct lpfc_iocbq *iocbq;
7344         int sum, i;
7345
7346         for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
7347                 iocbq = phba->sli.iocbq_lookup[i];
7348
7349                 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
7350                                                 ctx_cmd) == 0)
7351                         sum++;
7352         }
7353
7354         return sum;
7355 }
7356
7357 /**
7358  * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
7359  * @phba: Pointer to HBA context object
7360  * @cmdiocb: Pointer to command iocb object.
7361  * @rspiocb: Pointer to response iocb object.
7362  *
7363  * This function is called when an aborted FCP iocb completes. This
7364  * function is called by the ring event handler with no lock held.
7365  * This function frees the iocb.
7366  **/
7367 void
7368 lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7369                         struct lpfc_iocbq *rspiocb)
7370 {
7371         lpfc_sli_release_iocbq(phba, cmdiocb);
7372         return;
7373 }
7374
7375 /**
7376  * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
7377  * @vport: Pointer to virtual port.
7378  * @pring: Pointer to driver SLI ring object.
7379  * @tgt_id: SCSI ID of the target.
7380  * @lun_id: LUN ID of the scsi device.
7381  * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
7382  *
7383  * This function sends an abort command for every SCSI command
7384  * associated with the given virtual port pending on the ring
7385  * filtered by lpfc_sli_validate_fcp_iocb function.
7386  * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
7387  * FCP iocbs associated with lun specified by tgt_id and lun_id
7388  * parameters
7389  * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
7390  * FCP iocbs associated with SCSI target specified by tgt_id parameter.
7391  * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
7392  * FCP iocbs associated with virtual port.
7393  * This function returns number of iocbs it failed to abort.
7394  * This function is called with no locks held.
7395  **/
7396 int
7397 lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
7398                     uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
7399 {
7400         struct lpfc_hba *phba = vport->phba;
7401         struct lpfc_iocbq *iocbq;
7402         struct lpfc_iocbq *abtsiocb;
7403         IOCB_t *cmd = NULL;
7404         int errcnt = 0, ret_val = 0;
7405         int i;
7406
7407         for (i = 1; i <= phba->sli.last_iotag; i++) {
7408                 iocbq = phba->sli.iocbq_lookup[i];
7409
7410                 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
7411                                                abort_cmd) != 0)
7412                         continue;
7413
7414                 /* issue ABTS for this IOCB based on iotag */
7415                 abtsiocb = lpfc_sli_get_iocbq(phba);
7416                 if (abtsiocb == NULL) {
7417                         errcnt++;
7418                         continue;
7419                 }
7420
7421                 cmd = &iocbq->iocb;
7422                 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
7423                 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
7424                 if (phba->sli_rev == LPFC_SLI_REV4)
7425                         abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
7426                 else
7427                         abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
7428                 abtsiocb->iocb.ulpLe = 1;
7429                 abtsiocb->iocb.ulpClass = cmd->ulpClass;
7430                 abtsiocb->vport = phba->pport;
7431
7432                 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
7433                 abtsiocb->fcp_wqidx = iocbq->fcp_wqidx;
7434                 if (iocbq->iocb_flag & LPFC_IO_FCP)
7435                         abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
7436
7437                 if (lpfc_is_link_up(phba))
7438                         abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
7439                 else
7440                         abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
7441
7442                 /* Setup callback routine and issue the command. */
7443                 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
7444                 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
7445                                               abtsiocb, 0);
7446                 if (ret_val == IOCB_ERROR) {
7447                         lpfc_sli_release_iocbq(phba, abtsiocb);
7448                         errcnt++;
7449                         continue;
7450                 }
7451         }
7452
7453         return errcnt;
7454 }
7455
7456 /**
7457  * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
7458  * @phba: Pointer to HBA context object.
7459  * @cmdiocbq: Pointer to command iocb.
7460  * @rspiocbq: Pointer to response iocb.
7461  *
7462  * This function is the completion handler for iocbs issued using
7463  * lpfc_sli_issue_iocb_wait function. This function is called by the
7464  * ring event handler function without any lock held. This function
7465  * can be called from both worker thread context and interrupt
7466  * context. This function also can be called from other thread which
7467  * cleans up the SLI layer objects.
7468  * This function copy the contents of the response iocb to the
7469  * response iocb memory object provided by the caller of
7470  * lpfc_sli_issue_iocb_wait and then wakes up the thread which
7471  * sleeps for the iocb completion.
7472  **/
7473 static void
7474 lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
7475                         struct lpfc_iocbq *cmdiocbq,
7476                         struct lpfc_iocbq *rspiocbq)
7477 {
7478         wait_queue_head_t *pdone_q;
7479         unsigned long iflags;
7480         struct lpfc_scsi_buf *lpfc_cmd;
7481
7482         spin_lock_irqsave(&phba->hbalock, iflags);
7483         cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
7484         if (cmdiocbq->context2 && rspiocbq)
7485                 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
7486                        &rspiocbq->iocb, sizeof(IOCB_t));
7487
7488         /* Set the exchange busy flag for task management commands */
7489         if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
7490                 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
7491                 lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
7492                         cur_iocbq);
7493                 lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
7494         }
7495
7496         pdone_q = cmdiocbq->context_un.wait_queue;
7497         if (pdone_q)
7498                 wake_up(pdone_q);
7499         spin_unlock_irqrestore(&phba->hbalock, iflags);
7500         return;
7501 }
7502
7503 /**
7504  * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
7505  * @phba: Pointer to HBA context object..
7506  * @piocbq: Pointer to command iocb.
7507  * @flag: Flag to test.
7508  *
7509  * This routine grabs the hbalock and then test the iocb_flag to
7510  * see if the passed in flag is set.
7511  * Returns:
7512  * 1 if flag is set.
7513  * 0 if flag is not set.
7514  **/
7515 static int
7516 lpfc_chk_iocb_flg(struct lpfc_hba *phba,
7517                  struct lpfc_iocbq *piocbq, uint32_t flag)
7518 {
7519         unsigned long iflags;
7520         int ret;
7521
7522         spin_lock_irqsave(&phba->hbalock, iflags);
7523         ret = piocbq->iocb_flag & flag;
7524         spin_unlock_irqrestore(&phba->hbalock, iflags);
7525         return ret;
7526
7527 }
7528
7529 /**
7530  * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
7531  * @phba: Pointer to HBA context object..
7532  * @pring: Pointer to sli ring.
7533  * @piocb: Pointer to command iocb.
7534  * @prspiocbq: Pointer to response iocb.
7535  * @timeout: Timeout in number of seconds.
7536  *
7537  * This function issues the iocb to firmware and waits for the
7538  * iocb to complete. If the iocb command is not
7539  * completed within timeout seconds, it returns IOCB_TIMEDOUT.
7540  * Caller should not free the iocb resources if this function
7541  * returns IOCB_TIMEDOUT.
7542  * The function waits for the iocb completion using an
7543  * non-interruptible wait.
7544  * This function will sleep while waiting for iocb completion.
7545  * So, this function should not be called from any context which
7546  * does not allow sleeping. Due to the same reason, this function
7547  * cannot be called with interrupt disabled.
7548  * This function assumes that the iocb completions occur while
7549  * this function sleep. So, this function cannot be called from
7550  * the thread which process iocb completion for this ring.
7551  * This function clears the iocb_flag of the iocb object before
7552  * issuing the iocb and the iocb completion handler sets this
7553  * flag and wakes this thread when the iocb completes.
7554  * The contents of the response iocb will be copied to prspiocbq
7555  * by the completion handler when the command completes.
7556  * This function returns IOCB_SUCCESS when success.
7557  * This function is called with no lock held.
7558  **/
7559 int
7560 lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
7561                          uint32_t ring_number,
7562                          struct lpfc_iocbq *piocb,
7563                          struct lpfc_iocbq *prspiocbq,
7564                          uint32_t timeout)
7565 {
7566         DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
7567         long timeleft, timeout_req = 0;
7568         int retval = IOCB_SUCCESS;
7569         uint32_t creg_val;
7570
7571         /*
7572          * If the caller has provided a response iocbq buffer, then context2
7573          * is NULL or its an error.
7574          */
7575         if (prspiocbq) {
7576                 if (piocb->context2)
7577                         return IOCB_ERROR;
7578                 piocb->context2 = prspiocbq;
7579         }
7580
7581         piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
7582         piocb->context_un.wait_queue = &done_q;
7583         piocb->iocb_flag &= ~LPFC_IO_WAKE;
7584
7585         if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
7586                 creg_val = readl(phba->HCregaddr);
7587                 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
7588                 writel(creg_val, phba->HCregaddr);
7589                 readl(phba->HCregaddr); /* flush */
7590         }
7591
7592         retval = lpfc_sli_issue_iocb(phba, ring_number, piocb, 0);
7593         if (retval == IOCB_SUCCESS) {
7594                 timeout_req = timeout * HZ;
7595                 timeleft = wait_event_timeout(done_q,
7596                                 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
7597                                 timeout_req);
7598
7599                 if (piocb->iocb_flag & LPFC_IO_WAKE) {
7600                         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
7601                                         "0331 IOCB wake signaled\n");
7602                 } else if (timeleft == 0) {
7603                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7604                                         "0338 IOCB wait timeout error - no "
7605                                         "wake response Data x%x\n", timeout);
7606                         retval = IOCB_TIMEDOUT;
7607                 } else {
7608                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7609                                         "0330 IOCB wake NOT set, "
7610                                         "Data x%x x%lx\n",
7611                                         timeout, (timeleft / jiffies));
7612                         retval = IOCB_TIMEDOUT;
7613                 }
7614         } else {
7615                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
7616                                 "0332 IOCB wait issue failed, Data x%x\n",
7617                                 retval);
7618                 retval = IOCB_ERROR;
7619         }
7620
7621         if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
7622                 creg_val = readl(phba->HCregaddr);
7623                 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
7624                 writel(creg_val, phba->HCregaddr);
7625                 readl(phba->HCregaddr); /* flush */
7626         }
7627
7628         if (prspiocbq)
7629                 piocb->context2 = NULL;
7630
7631         piocb->context_un.wait_queue = NULL;
7632         piocb->iocb_cmpl = NULL;
7633         return retval;
7634 }
7635
7636 /**
7637  * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
7638  * @phba: Pointer to HBA context object.
7639  * @pmboxq: Pointer to driver mailbox object.
7640  * @timeout: Timeout in number of seconds.
7641  *
7642  * This function issues the mailbox to firmware and waits for the
7643  * mailbox command to complete. If the mailbox command is not
7644  * completed within timeout seconds, it returns MBX_TIMEOUT.
7645  * The function waits for the mailbox completion using an
7646  * interruptible wait. If the thread is woken up due to a
7647  * signal, MBX_TIMEOUT error is returned to the caller. Caller
7648  * should not free the mailbox resources, if this function returns
7649  * MBX_TIMEOUT.
7650  * This function will sleep while waiting for mailbox completion.
7651  * So, this function should not be called from any context which
7652  * does not allow sleeping. Due to the same reason, this function
7653  * cannot be called with interrupt disabled.
7654  * This function assumes that the mailbox completion occurs while
7655  * this function sleep. So, this function cannot be called from
7656  * the worker thread which processes mailbox completion.
7657  * This function is called in the context of HBA management
7658  * applications.
7659  * This function returns MBX_SUCCESS when successful.
7660  * This function is called with no lock held.
7661  **/
7662 int
7663 lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
7664                          uint32_t timeout)
7665 {
7666         DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
7667         int retval;
7668         unsigned long flag;
7669
7670         /* The caller must leave context1 empty. */
7671         if (pmboxq->context1)
7672                 return MBX_NOT_FINISHED;
7673
7674         pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
7675         /* setup wake call as IOCB callback */
7676         pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
7677         /* setup context field to pass wait_queue pointer to wake function  */
7678         pmboxq->context1 = &done_q;
7679
7680         /* now issue the command */
7681         retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
7682
7683         if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
7684                 wait_event_interruptible_timeout(done_q,
7685                                 pmboxq->mbox_flag & LPFC_MBX_WAKE,
7686                                 timeout * HZ);
7687
7688                 spin_lock_irqsave(&phba->hbalock, flag);
7689                 pmboxq->context1 = NULL;
7690                 /*
7691                  * if LPFC_MBX_WAKE flag is set the mailbox is completed
7692                  * else do not free the resources.
7693                  */
7694                 if (pmboxq->mbox_flag & LPFC_MBX_WAKE)
7695                         retval = MBX_SUCCESS;
7696                 else {
7697                         retval = MBX_TIMEOUT;
7698                         pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
7699                 }
7700                 spin_unlock_irqrestore(&phba->hbalock, flag);
7701         }
7702
7703         return retval;
7704 }
7705
7706 /**
7707  * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
7708  * @phba: Pointer to HBA context.
7709  *
7710  * This function is called to shutdown the driver's mailbox sub-system.
7711  * It first marks the mailbox sub-system is in a block state to prevent
7712  * the asynchronous mailbox command from issued off the pending mailbox
7713  * command queue. If the mailbox command sub-system shutdown is due to
7714  * HBA error conditions such as EEH or ERATT, this routine shall invoke
7715  * the mailbox sub-system flush routine to forcefully bring down the
7716  * mailbox sub-system. Otherwise, if it is due to normal condition (such
7717  * as with offline or HBA function reset), this routine will wait for the
7718  * outstanding mailbox command to complete before invoking the mailbox
7719  * sub-system flush routine to gracefully bring down mailbox sub-system.
7720  **/
7721 void
7722 lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba)
7723 {
7724         struct lpfc_sli *psli = &phba->sli;
7725         uint8_t actcmd = MBX_HEARTBEAT;
7726         unsigned long timeout;
7727
7728         spin_lock_irq(&phba->hbalock);
7729         psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
7730         spin_unlock_irq(&phba->hbalock);
7731
7732         if (psli->sli_flag & LPFC_SLI_ACTIVE) {
7733                 spin_lock_irq(&phba->hbalock);
7734                 if (phba->sli.mbox_active)
7735                         actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
7736                 spin_unlock_irq(&phba->hbalock);
7737                 /* Determine how long we might wait for the active mailbox
7738                  * command to be gracefully completed by firmware.
7739                  */
7740                 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, actcmd) *
7741                                            1000) + jiffies;
7742                 while (phba->sli.mbox_active) {
7743                         /* Check active mailbox complete status every 2ms */
7744                         msleep(2);
7745                         if (time_after(jiffies, timeout))
7746                                 /* Timeout, let the mailbox flush routine to
7747                                  * forcefully release active mailbox command
7748                                  */
7749                                 break;
7750                 }
7751         }
7752         lpfc_sli_mbox_sys_flush(phba);
7753 }
7754
7755 /**
7756  * lpfc_sli_eratt_read - read sli-3 error attention events
7757  * @phba: Pointer to HBA context.
7758  *
7759  * This function is called to read the SLI3 device error attention registers
7760  * for possible error attention events. The caller must hold the hostlock
7761  * with spin_lock_irq().
7762  *
7763  * This fucntion returns 1 when there is Error Attention in the Host Attention
7764  * Register and returns 0 otherwise.
7765  **/
7766 static int
7767 lpfc_sli_eratt_read(struct lpfc_hba *phba)
7768 {
7769         uint32_t ha_copy;
7770
7771         /* Read chip Host Attention (HA) register */
7772         ha_copy = readl(phba->HAregaddr);
7773         if (ha_copy & HA_ERATT) {
7774                 /* Read host status register to retrieve error event */
7775                 lpfc_sli_read_hs(phba);
7776
7777                 /* Check if there is a deferred error condition is active */
7778                 if ((HS_FFER1 & phba->work_hs) &&
7779                     ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
7780                      HS_FFER6 | HS_FFER7) & phba->work_hs)) {
7781                         phba->hba_flag |= DEFER_ERATT;
7782                         /* Clear all interrupt enable conditions */
7783                         writel(0, phba->HCregaddr);
7784                         readl(phba->HCregaddr);
7785                 }
7786
7787                 /* Set the driver HA work bitmap */
7788                 phba->work_ha |= HA_ERATT;
7789                 /* Indicate polling handles this ERATT */
7790                 phba->hba_flag |= HBA_ERATT_HANDLED;
7791                 return 1;
7792         }
7793         return 0;
7794 }
7795
7796 /**
7797  * lpfc_sli4_eratt_read - read sli-4 error attention events
7798  * @phba: Pointer to HBA context.
7799  *
7800  * This function is called to read the SLI4 device error attention registers
7801  * for possible error attention events. The caller must hold the hostlock
7802  * with spin_lock_irq().
7803  *
7804  * This fucntion returns 1 when there is Error Attention in the Host Attention
7805  * Register and returns 0 otherwise.
7806  **/
7807 static int
7808 lpfc_sli4_eratt_read(struct lpfc_hba *phba)
7809 {
7810         uint32_t uerr_sta_hi, uerr_sta_lo;
7811
7812         /* For now, use the SLI4 device internal unrecoverable error
7813          * registers for error attention. This can be changed later.
7814          */
7815         uerr_sta_lo = readl(phba->sli4_hba.UERRLOregaddr);
7816         uerr_sta_hi = readl(phba->sli4_hba.UERRHIregaddr);
7817         if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
7818             (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
7819                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7820                                 "1423 HBA Unrecoverable error: "
7821                                 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
7822                                 "ue_mask_lo_reg=0x%x, ue_mask_hi_reg=0x%x\n",
7823                                 uerr_sta_lo, uerr_sta_hi,
7824                                 phba->sli4_hba.ue_mask_lo,
7825                                 phba->sli4_hba.ue_mask_hi);
7826                 phba->work_status[0] = uerr_sta_lo;
7827                 phba->work_status[1] = uerr_sta_hi;
7828                 /* Set the driver HA work bitmap */
7829                 phba->work_ha |= HA_ERATT;
7830                 /* Indicate polling handles this ERATT */
7831                 phba->hba_flag |= HBA_ERATT_HANDLED;
7832                 return 1;
7833         }
7834         return 0;
7835 }
7836
7837 /**
7838  * lpfc_sli_check_eratt - check error attention events
7839  * @phba: Pointer to HBA context.
7840  *
7841  * This function is called from timer soft interrupt context to check HBA's
7842  * error attention register bit for error attention events.
7843  *
7844  * This fucntion returns 1 when there is Error Attention in the Host Attention
7845  * Register and returns 0 otherwise.
7846  **/
7847 int
7848 lpfc_sli_check_eratt(struct lpfc_hba *phba)
7849 {
7850         uint32_t ha_copy;
7851
7852         /* If somebody is waiting to handle an eratt, don't process it
7853          * here. The brdkill function will do this.
7854          */
7855         if (phba->link_flag & LS_IGNORE_ERATT)
7856                 return 0;
7857
7858         /* Check if interrupt handler handles this ERATT */
7859         spin_lock_irq(&phba->hbalock);
7860         if (phba->hba_flag & HBA_ERATT_HANDLED) {
7861                 /* Interrupt handler has handled ERATT */
7862                 spin_unlock_irq(&phba->hbalock);
7863                 return 0;
7864         }
7865
7866         /*
7867          * If there is deferred error attention, do not check for error
7868          * attention
7869          */
7870         if (unlikely(phba->hba_flag & DEFER_ERATT)) {
7871                 spin_unlock_irq(&phba->hbalock);
7872                 return 0;
7873         }
7874
7875         /* If PCI channel is offline, don't process it */
7876         if (unlikely(pci_channel_offline(phba->pcidev))) {
7877                 spin_unlock_irq(&phba->hbalock);
7878                 return 0;
7879         }
7880
7881         switch (phba->sli_rev) {
7882         case LPFC_SLI_REV2:
7883         case LPFC_SLI_REV3:
7884                 /* Read chip Host Attention (HA) register */
7885                 ha_copy = lpfc_sli_eratt_read(phba);
7886                 break;
7887         case LPFC_SLI_REV4:
7888                 /* Read devcie Uncoverable Error (UERR) registers */
7889                 ha_copy = lpfc_sli4_eratt_read(phba);
7890                 break;
7891         default:
7892                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7893                                 "0299 Invalid SLI revision (%d)\n",
7894                                 phba->sli_rev);
7895                 ha_copy = 0;
7896                 break;
7897         }
7898         spin_unlock_irq(&phba->hbalock);
7899
7900         return ha_copy;
7901 }
7902
7903 /**
7904  * lpfc_intr_state_check - Check device state for interrupt handling
7905  * @phba: Pointer to HBA context.
7906  *
7907  * This inline routine checks whether a device or its PCI slot is in a state
7908  * that the interrupt should be handled.
7909  *
7910  * This function returns 0 if the device or the PCI slot is in a state that
7911  * interrupt should be handled, otherwise -EIO.
7912  */
7913 static inline int
7914 lpfc_intr_state_check(struct lpfc_hba *phba)
7915 {
7916         /* If the pci channel is offline, ignore all the interrupts */
7917         if (unlikely(pci_channel_offline(phba->pcidev)))
7918                 return -EIO;
7919
7920         /* Update device level interrupt statistics */
7921         phba->sli.slistat.sli_intr++;
7922
7923         /* Ignore all interrupts during initialization. */
7924         if (unlikely(phba->link_state < LPFC_LINK_DOWN))
7925                 return -EIO;
7926
7927         return 0;
7928 }
7929
7930 /**
7931  * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
7932  * @irq: Interrupt number.
7933  * @dev_id: The device context pointer.
7934  *
7935  * This function is directly called from the PCI layer as an interrupt
7936  * service routine when device with SLI-3 interface spec is enabled with
7937  * MSI-X multi-message interrupt mode and there are slow-path events in
7938  * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
7939  * interrupt mode, this function is called as part of the device-level
7940  * interrupt handler. When the PCI slot is in error recovery or the HBA
7941  * is undergoing initialization, the interrupt handler will not process
7942  * the interrupt. The link attention and ELS ring attention events are
7943  * handled by the worker thread. The interrupt handler signals the worker
7944  * thread and returns for these events. This function is called without
7945  * any lock held. It gets the hbalock to access and update SLI data
7946  * structures.
7947  *
7948  * This function returns IRQ_HANDLED when interrupt is handled else it
7949  * returns IRQ_NONE.
7950  **/
7951 irqreturn_t
7952 lpfc_sli_sp_intr_handler(int irq, void *dev_id)
7953 {
7954         struct lpfc_hba  *phba;
7955         uint32_t ha_copy, hc_copy;
7956         uint32_t work_ha_copy;
7957         unsigned long status;
7958         unsigned long iflag;
7959         uint32_t control;
7960
7961         MAILBOX_t *mbox, *pmbox;
7962         struct lpfc_vport *vport;
7963         struct lpfc_nodelist *ndlp;
7964         struct lpfc_dmabuf *mp;
7965         LPFC_MBOXQ_t *pmb;
7966         int rc;
7967
7968         /*
7969          * Get the driver's phba structure from the dev_id and
7970          * assume the HBA is not interrupting.
7971          */
7972         phba = (struct lpfc_hba *)dev_id;
7973
7974         if (unlikely(!phba))
7975                 return IRQ_NONE;
7976
7977         /*
7978          * Stuff needs to be attented to when this function is invoked as an
7979          * individual interrupt handler in MSI-X multi-message interrupt mode
7980          */
7981         if (phba->intr_type == MSIX) {
7982                 /* Check device state for handling interrupt */
7983                 if (lpfc_intr_state_check(phba))
7984                         return IRQ_NONE;
7985                 /* Need to read HA REG for slow-path events */
7986                 spin_lock_irqsave(&phba->hbalock, iflag);
7987                 ha_copy = readl(phba->HAregaddr);
7988                 /* If somebody is waiting to handle an eratt don't process it
7989                  * here. The brdkill function will do this.
7990                  */
7991                 if (phba->link_flag & LS_IGNORE_ERATT)
7992                         ha_copy &= ~HA_ERATT;
7993                 /* Check the need for handling ERATT in interrupt handler */
7994                 if (ha_copy & HA_ERATT) {
7995                         if (phba->hba_flag & HBA_ERATT_HANDLED)
7996                                 /* ERATT polling has handled ERATT */
7997                                 ha_copy &= ~HA_ERATT;
7998                         else
7999                                 /* Indicate interrupt handler handles ERATT */
8000                                 phba->hba_flag |= HBA_ERATT_HANDLED;
8001                 }
8002
8003                 /*
8004                  * If there is deferred error attention, do not check for any
8005                  * interrupt.
8006                  */
8007                 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
8008                         spin_unlock_irqrestore(&phba->hbalock, iflag);
8009                         return IRQ_NONE;
8010                 }
8011
8012                 /* Clear up only attention source related to slow-path */
8013                 hc_copy = readl(phba->HCregaddr);
8014                 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
8015                         HC_LAINT_ENA | HC_ERINT_ENA),
8016                         phba->HCregaddr);
8017                 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
8018                         phba->HAregaddr);
8019                 writel(hc_copy, phba->HCregaddr);
8020                 readl(phba->HAregaddr); /* flush */
8021                 spin_unlock_irqrestore(&phba->hbalock, iflag);
8022         } else
8023                 ha_copy = phba->ha_copy;
8024
8025         work_ha_copy = ha_copy & phba->work_ha_mask;
8026
8027         if (work_ha_copy) {
8028                 if (work_ha_copy & HA_LATT) {
8029                         if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
8030                                 /*
8031                                  * Turn off Link Attention interrupts
8032                                  * until CLEAR_LA done
8033                                  */
8034                                 spin_lock_irqsave(&phba->hbalock, iflag);
8035                                 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
8036                                 control = readl(phba->HCregaddr);
8037                                 control &= ~HC_LAINT_ENA;
8038                                 writel(control, phba->HCregaddr);
8039                                 readl(phba->HCregaddr); /* flush */
8040                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
8041                         }
8042                         else
8043                                 work_ha_copy &= ~HA_LATT;
8044                 }
8045
8046                 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
8047                         /*
8048                          * Turn off Slow Rings interrupts, LPFC_ELS_RING is
8049                          * the only slow ring.
8050                          */
8051                         status = (work_ha_copy &
8052                                 (HA_RXMASK  << (4*LPFC_ELS_RING)));
8053                         status >>= (4*LPFC_ELS_RING);
8054                         if (status & HA_RXMASK) {
8055                                 spin_lock_irqsave(&phba->hbalock, iflag);
8056                                 control = readl(phba->HCregaddr);
8057
8058                                 lpfc_debugfs_slow_ring_trc(phba,
8059                                 "ISR slow ring:   ctl:x%x stat:x%x isrcnt:x%x",
8060                                 control, status,
8061                                 (uint32_t)phba->sli.slistat.sli_intr);
8062
8063                                 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
8064                                         lpfc_debugfs_slow_ring_trc(phba,
8065                                                 "ISR Disable ring:"
8066                                                 "pwork:x%x hawork:x%x wait:x%x",
8067                                                 phba->work_ha, work_ha_copy,
8068                                                 (uint32_t)((unsigned long)
8069                                                 &phba->work_waitq));
8070
8071                                         control &=
8072                                             ~(HC_R0INT_ENA << LPFC_ELS_RING);
8073                                         writel(control, phba->HCregaddr);
8074                                         readl(phba->HCregaddr); /* flush */
8075                                 }
8076                                 else {
8077                                         lpfc_debugfs_slow_ring_trc(phba,
8078                                                 "ISR slow ring:   pwork:"
8079                                                 "x%x hawork:x%x wait:x%x",
8080                                                 phba->work_ha, work_ha_copy,
8081                                                 (uint32_t)((unsigned long)
8082                                                 &phba->work_waitq));
8083                                 }
8084                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
8085                         }
8086                 }
8087                 spin_lock_irqsave(&phba->hbalock, iflag);
8088                 if (work_ha_copy & HA_ERATT) {
8089                         lpfc_sli_read_hs(phba);
8090                         /*
8091                          * Check if there is a deferred error condition
8092                          * is active
8093                          */
8094                         if ((HS_FFER1 & phba->work_hs) &&
8095                                 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
8096                                 HS_FFER6 | HS_FFER7) & phba->work_hs)) {
8097                                 phba->hba_flag |= DEFER_ERATT;
8098                                 /* Clear all interrupt enable conditions */
8099                                 writel(0, phba->HCregaddr);
8100                                 readl(phba->HCregaddr);
8101                         }
8102                 }
8103
8104                 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
8105                         pmb = phba->sli.mbox_active;
8106                         pmbox = &pmb->u.mb;
8107                         mbox = phba->mbox;
8108                         vport = pmb->vport;
8109
8110                         /* First check out the status word */
8111                         lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
8112                         if (pmbox->mbxOwner != OWN_HOST) {
8113                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
8114                                 /*
8115                                  * Stray Mailbox Interrupt, mbxCommand <cmd>
8116                                  * mbxStatus <status>
8117                                  */
8118                                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
8119                                                 LOG_SLI,
8120                                                 "(%d):0304 Stray Mailbox "
8121                                                 "Interrupt mbxCommand x%x "
8122                                                 "mbxStatus x%x\n",
8123                                                 (vport ? vport->vpi : 0),
8124                                                 pmbox->mbxCommand,
8125                                                 pmbox->mbxStatus);
8126                                 /* clear mailbox attention bit */
8127                                 work_ha_copy &= ~HA_MBATT;
8128                         } else {
8129                                 phba->sli.mbox_active = NULL;
8130                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
8131                                 phba->last_completion_time = jiffies;
8132                                 del_timer(&phba->sli.mbox_tmo);
8133                                 if (pmb->mbox_cmpl) {
8134                                         lpfc_sli_pcimem_bcopy(mbox, pmbox,
8135                                                         MAILBOX_CMD_SIZE);
8136                                 }
8137                                 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
8138                                         pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
8139
8140                                         lpfc_debugfs_disc_trc(vport,
8141                                                 LPFC_DISC_TRC_MBOX_VPORT,
8142                                                 "MBOX dflt rpi: : "
8143                                                 "status:x%x rpi:x%x",
8144                                                 (uint32_t)pmbox->mbxStatus,
8145                                                 pmbox->un.varWords[0], 0);
8146
8147                                         if (!pmbox->mbxStatus) {
8148                                                 mp = (struct lpfc_dmabuf *)
8149                                                         (pmb->context1);
8150                                                 ndlp = (struct lpfc_nodelist *)
8151                                                         pmb->context2;
8152
8153                                                 /* Reg_LOGIN of dflt RPI was
8154                                                  * successful. new lets get
8155                                                  * rid of the RPI using the
8156                                                  * same mbox buffer.
8157                                                  */
8158                                                 lpfc_unreg_login(phba,
8159                                                         vport->vpi,
8160                                                         pmbox->un.varWords[0],
8161                                                         pmb);
8162                                                 pmb->mbox_cmpl =
8163                                                         lpfc_mbx_cmpl_dflt_rpi;
8164                                                 pmb->context1 = mp;
8165                                                 pmb->context2 = ndlp;
8166                                                 pmb->vport = vport;
8167                                                 rc = lpfc_sli_issue_mbox(phba,
8168                                                                 pmb,
8169                                                                 MBX_NOWAIT);
8170                                                 if (rc != MBX_BUSY)
8171                                                         lpfc_printf_log(phba,
8172                                                         KERN_ERR,
8173                                                         LOG_MBOX | LOG_SLI,
8174                                                         "0350 rc should have"
8175                                                         "been MBX_BUSY\n");
8176                                                 if (rc != MBX_NOT_FINISHED)
8177                                                         goto send_current_mbox;
8178                                         }
8179                                 }
8180                                 spin_lock_irqsave(
8181                                                 &phba->pport->work_port_lock,
8182                                                 iflag);
8183                                 phba->pport->work_port_events &=
8184                                         ~WORKER_MBOX_TMO;
8185                                 spin_unlock_irqrestore(
8186                                                 &phba->pport->work_port_lock,
8187                                                 iflag);
8188                                 lpfc_mbox_cmpl_put(phba, pmb);
8189                         }
8190                 } else
8191                         spin_unlock_irqrestore(&phba->hbalock, iflag);
8192
8193                 if ((work_ha_copy & HA_MBATT) &&
8194                     (phba->sli.mbox_active == NULL)) {
8195 send_current_mbox:
8196                         /* Process next mailbox command if there is one */
8197                         do {
8198                                 rc = lpfc_sli_issue_mbox(phba, NULL,
8199                                                          MBX_NOWAIT);
8200                         } while (rc == MBX_NOT_FINISHED);
8201                         if (rc != MBX_SUCCESS)
8202                                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
8203                                                 LOG_SLI, "0349 rc should be "
8204                                                 "MBX_SUCCESS\n");
8205                 }
8206
8207                 spin_lock_irqsave(&phba->hbalock, iflag);
8208                 phba->work_ha |= work_ha_copy;
8209                 spin_unlock_irqrestore(&phba->hbalock, iflag);
8210                 lpfc_worker_wake_up(phba);
8211         }
8212         return IRQ_HANDLED;
8213
8214 } /* lpfc_sli_sp_intr_handler */
8215
8216 /**
8217  * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
8218  * @irq: Interrupt number.
8219  * @dev_id: The device context pointer.
8220  *
8221  * This function is directly called from the PCI layer as an interrupt
8222  * service routine when device with SLI-3 interface spec is enabled with
8223  * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
8224  * ring event in the HBA. However, when the device is enabled with either
8225  * MSI or Pin-IRQ interrupt mode, this function is called as part of the
8226  * device-level interrupt handler. When the PCI slot is in error recovery
8227  * or the HBA is undergoing initialization, the interrupt handler will not
8228  * process the interrupt. The SCSI FCP fast-path ring event are handled in
8229  * the intrrupt context. This function is called without any lock held.
8230  * It gets the hbalock to access and update SLI data structures.
8231  *
8232  * This function returns IRQ_HANDLED when interrupt is handled else it
8233  * returns IRQ_NONE.
8234  **/
8235 irqreturn_t
8236 lpfc_sli_fp_intr_handler(int irq, void *dev_id)
8237 {
8238         struct lpfc_hba  *phba;
8239         uint32_t ha_copy;
8240         unsigned long status;
8241         unsigned long iflag;
8242
8243         /* Get the driver's phba structure from the dev_id and
8244          * assume the HBA is not interrupting.
8245          */
8246         phba = (struct lpfc_hba *) dev_id;
8247
8248         if (unlikely(!phba))
8249                 return IRQ_NONE;
8250
8251         /*
8252          * Stuff needs to be attented to when this function is invoked as an
8253          * individual interrupt handler in MSI-X multi-message interrupt mode
8254          */
8255         if (phba->intr_type == MSIX) {
8256                 /* Check device state for handling interrupt */
8257                 if (lpfc_intr_state_check(phba))
8258                         return IRQ_NONE;
8259                 /* Need to read HA REG for FCP ring and other ring events */
8260                 ha_copy = readl(phba->HAregaddr);
8261                 /* Clear up only attention source related to fast-path */
8262                 spin_lock_irqsave(&phba->hbalock, iflag);
8263                 /*
8264                  * If there is deferred error attention, do not check for
8265                  * any interrupt.
8266                  */
8267                 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
8268                         spin_unlock_irqrestore(&phba->hbalock, iflag);
8269                         return IRQ_NONE;
8270                 }
8271                 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
8272                         phba->HAregaddr);
8273                 readl(phba->HAregaddr); /* flush */
8274                 spin_unlock_irqrestore(&phba->hbalock, iflag);
8275         } else
8276                 ha_copy = phba->ha_copy;
8277
8278         /*
8279          * Process all events on FCP ring. Take the optimized path for FCP IO.
8280          */
8281         ha_copy &= ~(phba->work_ha_mask);
8282
8283         status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
8284         status >>= (4*LPFC_FCP_RING);
8285         if (status & HA_RXMASK)
8286                 lpfc_sli_handle_fast_ring_event(phba,
8287                                                 &phba->sli.ring[LPFC_FCP_RING],
8288                                                 status);
8289
8290         if (phba->cfg_multi_ring_support == 2) {
8291                 /*
8292                  * Process all events on extra ring. Take the optimized path
8293                  * for extra ring IO.
8294                  */
8295                 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
8296                 status >>= (4*LPFC_EXTRA_RING);
8297                 if (status & HA_RXMASK) {
8298                         lpfc_sli_handle_fast_ring_event(phba,
8299                                         &phba->sli.ring[LPFC_EXTRA_RING],
8300                                         status);
8301                 }
8302         }
8303         return IRQ_HANDLED;
8304 }  /* lpfc_sli_fp_intr_handler */
8305
8306 /**
8307  * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
8308  * @irq: Interrupt number.
8309  * @dev_id: The device context pointer.
8310  *
8311  * This function is the HBA device-level interrupt handler to device with
8312  * SLI-3 interface spec, called from the PCI layer when either MSI or
8313  * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
8314  * requires driver attention. This function invokes the slow-path interrupt
8315  * attention handling function and fast-path interrupt attention handling
8316  * function in turn to process the relevant HBA attention events. This
8317  * function is called without any lock held. It gets the hbalock to access
8318  * and update SLI data structures.
8319  *
8320  * This function returns IRQ_HANDLED when interrupt is handled, else it
8321  * returns IRQ_NONE.
8322  **/
8323 irqreturn_t
8324 lpfc_sli_intr_handler(int irq, void *dev_id)
8325 {
8326         struct lpfc_hba  *phba;
8327         irqreturn_t sp_irq_rc, fp_irq_rc;
8328         unsigned long status1, status2;
8329         uint32_t hc_copy;
8330
8331         /*
8332          * Get the driver's phba structure from the dev_id and
8333          * assume the HBA is not interrupting.
8334          */
8335         phba = (struct lpfc_hba *) dev_id;
8336
8337         if (unlikely(!phba))
8338                 return IRQ_NONE;
8339
8340         /* Check device state for handling interrupt */
8341         if (lpfc_intr_state_check(phba))
8342                 return IRQ_NONE;
8343
8344         spin_lock(&phba->hbalock);
8345         phba->ha_copy = readl(phba->HAregaddr);
8346         if (unlikely(!phba->ha_copy)) {
8347                 spin_unlock(&phba->hbalock);
8348                 return IRQ_NONE;
8349         } else if (phba->ha_copy & HA_ERATT) {
8350                 if (phba->hba_flag & HBA_ERATT_HANDLED)
8351                         /* ERATT polling has handled ERATT */
8352                         phba->ha_copy &= ~HA_ERATT;
8353                 else
8354                         /* Indicate interrupt handler handles ERATT */
8355                         phba->hba_flag |= HBA_ERATT_HANDLED;
8356         }
8357
8358         /*
8359          * If there is deferred error attention, do not check for any interrupt.
8360          */
8361         if (unlikely(phba->hba_flag & DEFER_ERATT)) {
8362                 spin_unlock_irq(&phba->hbalock);
8363                 return IRQ_NONE;
8364         }
8365
8366         /* Clear attention sources except link and error attentions */
8367         hc_copy = readl(phba->HCregaddr);
8368         writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
8369                 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
8370                 phba->HCregaddr);
8371         writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
8372         writel(hc_copy, phba->HCregaddr);
8373         readl(phba->HAregaddr); /* flush */
8374         spin_unlock(&phba->hbalock);
8375
8376         /*
8377          * Invokes slow-path host attention interrupt handling as appropriate.
8378          */
8379
8380         /* status of events with mailbox and link attention */
8381         status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
8382
8383         /* status of events with ELS ring */
8384         status2 = (phba->ha_copy & (HA_RXMASK  << (4*LPFC_ELS_RING)));
8385         status2 >>= (4*LPFC_ELS_RING);
8386
8387         if (status1 || (status2 & HA_RXMASK))
8388                 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
8389         else
8390                 sp_irq_rc = IRQ_NONE;
8391
8392         /*
8393          * Invoke fast-path host attention interrupt handling as appropriate.
8394          */
8395
8396         /* status of events with FCP ring */
8397         status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
8398         status1 >>= (4*LPFC_FCP_RING);
8399
8400         /* status of events with extra ring */
8401         if (phba->cfg_multi_ring_support == 2) {
8402                 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
8403                 status2 >>= (4*LPFC_EXTRA_RING);
8404         } else
8405                 status2 = 0;
8406
8407         if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
8408                 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
8409         else
8410                 fp_irq_rc = IRQ_NONE;
8411
8412         /* Return device-level interrupt handling status */
8413         return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
8414 }  /* lpfc_sli_intr_handler */
8415
8416 /**
8417  * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
8418  * @phba: pointer to lpfc hba data structure.
8419  *
8420  * This routine is invoked by the worker thread to process all the pending
8421  * SLI4 FCP abort XRI events.
8422  **/
8423 void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
8424 {
8425         struct lpfc_cq_event *cq_event;
8426
8427         /* First, declare the fcp xri abort event has been handled */
8428         spin_lock_irq(&phba->hbalock);
8429         phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
8430         spin_unlock_irq(&phba->hbalock);
8431         /* Now, handle all the fcp xri abort events */
8432         while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
8433                 /* Get the first event from the head of the event queue */
8434                 spin_lock_irq(&phba->hbalock);
8435                 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
8436                                  cq_event, struct lpfc_cq_event, list);
8437                 spin_unlock_irq(&phba->hbalock);
8438                 /* Notify aborted XRI for FCP work queue */
8439                 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
8440                 /* Free the event processed back to the free pool */
8441                 lpfc_sli4_cq_event_release(phba, cq_event);
8442         }
8443 }
8444
8445 /**
8446  * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
8447  * @phba: pointer to lpfc hba data structure.
8448  *
8449  * This routine is invoked by the worker thread to process all the pending
8450  * SLI4 els abort xri events.
8451  **/
8452 void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
8453 {
8454         struct lpfc_cq_event *cq_event;
8455
8456         /* First, declare the els xri abort event has been handled */
8457         spin_lock_irq(&phba->hbalock);
8458         phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
8459         spin_unlock_irq(&phba->hbalock);
8460         /* Now, handle all the els xri abort events */
8461         while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
8462                 /* Get the first event from the head of the event queue */
8463                 spin_lock_irq(&phba->hbalock);
8464                 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
8465                                  cq_event, struct lpfc_cq_event, list);
8466                 spin_unlock_irq(&phba->hbalock);
8467                 /* Notify aborted XRI for ELS work queue */
8468                 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
8469                 /* Free the event processed back to the free pool */
8470                 lpfc_sli4_cq_event_release(phba, cq_event);
8471         }
8472 }
8473
8474 /**
8475  * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
8476  * @phba: pointer to lpfc hba data structure
8477  * @pIocbIn: pointer to the rspiocbq
8478  * @pIocbOut: pointer to the cmdiocbq
8479  * @wcqe: pointer to the complete wcqe
8480  *
8481  * This routine transfers the fields of a command iocbq to a response iocbq
8482  * by copying all the IOCB fields from command iocbq and transferring the
8483  * completion status information from the complete wcqe.
8484  **/
8485 static void
8486 lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
8487                               struct lpfc_iocbq *pIocbIn,
8488                               struct lpfc_iocbq *pIocbOut,
8489                               struct lpfc_wcqe_complete *wcqe)
8490 {
8491         unsigned long iflags;
8492         size_t offset = offsetof(struct lpfc_iocbq, iocb);
8493
8494         memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
8495                sizeof(struct lpfc_iocbq) - offset);
8496         /* Map WCQE parameters into irspiocb parameters */
8497         pIocbIn->iocb.ulpStatus = bf_get(lpfc_wcqe_c_status, wcqe);
8498         if (pIocbOut->iocb_flag & LPFC_IO_FCP)
8499                 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
8500                         pIocbIn->iocb.un.fcpi.fcpi_parm =
8501                                         pIocbOut->iocb.un.fcpi.fcpi_parm -
8502                                         wcqe->total_data_placed;
8503                 else
8504                         pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
8505         else {
8506                 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
8507                 pIocbIn->iocb.un.genreq64.bdl.bdeSize = wcqe->total_data_placed;
8508         }
8509
8510         /* Pick up HBA exchange busy condition */
8511         if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
8512                 spin_lock_irqsave(&phba->hbalock, iflags);
8513                 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
8514                 spin_unlock_irqrestore(&phba->hbalock, iflags);
8515         }
8516 }
8517
8518 /**
8519  * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
8520  * @phba: Pointer to HBA context object.
8521  * @wcqe: Pointer to work-queue completion queue entry.
8522  *
8523  * This routine handles an ELS work-queue completion event and construct
8524  * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
8525  * discovery engine to handle.
8526  *
8527  * Return: Pointer to the receive IOCBQ, NULL otherwise.
8528  **/
8529 static struct lpfc_iocbq *
8530 lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
8531                                struct lpfc_iocbq *irspiocbq)
8532 {
8533         struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
8534         struct lpfc_iocbq *cmdiocbq;
8535         struct lpfc_wcqe_complete *wcqe;
8536         unsigned long iflags;
8537
8538         wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
8539         spin_lock_irqsave(&phba->hbalock, iflags);
8540         pring->stats.iocb_event++;
8541         /* Look up the ELS command IOCB and create pseudo response IOCB */
8542         cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
8543                                 bf_get(lpfc_wcqe_c_request_tag, wcqe));
8544         spin_unlock_irqrestore(&phba->hbalock, iflags);
8545
8546         if (unlikely(!cmdiocbq)) {
8547                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8548                                 "0386 ELS complete with no corresponding "
8549                                 "cmdiocb: iotag (%d)\n",
8550                                 bf_get(lpfc_wcqe_c_request_tag, wcqe));
8551                 lpfc_sli_release_iocbq(phba, irspiocbq);
8552                 return NULL;
8553         }
8554
8555         /* Fake the irspiocbq and copy necessary response information */
8556         lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
8557
8558         return irspiocbq;
8559 }
8560
8561 /**
8562  * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
8563  * @phba: Pointer to HBA context object.
8564  * @cqe: Pointer to mailbox completion queue entry.
8565  *
8566  * This routine process a mailbox completion queue entry with asynchrous
8567  * event.
8568  *
8569  * Return: true if work posted to worker thread, otherwise false.
8570  **/
8571 static bool
8572 lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
8573 {
8574         struct lpfc_cq_event *cq_event;
8575         unsigned long iflags;
8576
8577         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8578                         "0392 Async Event: word0:x%x, word1:x%x, "
8579                         "word2:x%x, word3:x%x\n", mcqe->word0,
8580                         mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
8581
8582         /* Allocate a new internal CQ_EVENT entry */
8583         cq_event = lpfc_sli4_cq_event_alloc(phba);
8584         if (!cq_event) {
8585                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8586                                 "0394 Failed to allocate CQ_EVENT entry\n");
8587                 return false;
8588         }
8589
8590         /* Move the CQE into an asynchronous event entry */
8591         memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
8592         spin_lock_irqsave(&phba->hbalock, iflags);
8593         list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
8594         /* Set the async event flag */
8595         phba->hba_flag |= ASYNC_EVENT;
8596         spin_unlock_irqrestore(&phba->hbalock, iflags);
8597
8598         return true;
8599 }
8600
8601 /**
8602  * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
8603  * @phba: Pointer to HBA context object.
8604  * @cqe: Pointer to mailbox completion queue entry.
8605  *
8606  * This routine process a mailbox completion queue entry with mailbox
8607  * completion event.
8608  *
8609  * Return: true if work posted to worker thread, otherwise false.
8610  **/
8611 static bool
8612 lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
8613 {
8614         uint32_t mcqe_status;
8615         MAILBOX_t *mbox, *pmbox;
8616         struct lpfc_mqe *mqe;
8617         struct lpfc_vport *vport;
8618         struct lpfc_nodelist *ndlp;
8619         struct lpfc_dmabuf *mp;
8620         unsigned long iflags;
8621         LPFC_MBOXQ_t *pmb;
8622         bool workposted = false;
8623         int rc;
8624
8625         /* If not a mailbox complete MCQE, out by checking mailbox consume */
8626         if (!bf_get(lpfc_trailer_completed, mcqe))
8627                 goto out_no_mqe_complete;
8628
8629         /* Get the reference to the active mbox command */
8630         spin_lock_irqsave(&phba->hbalock, iflags);
8631         pmb = phba->sli.mbox_active;
8632         if (unlikely(!pmb)) {
8633                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
8634                                 "1832 No pending MBOX command to handle\n");
8635                 spin_unlock_irqrestore(&phba->hbalock, iflags);
8636                 goto out_no_mqe_complete;
8637         }
8638         spin_unlock_irqrestore(&phba->hbalock, iflags);
8639         mqe = &pmb->u.mqe;
8640         pmbox = (MAILBOX_t *)&pmb->u.mqe;
8641         mbox = phba->mbox;
8642         vport = pmb->vport;
8643
8644         /* Reset heartbeat timer */
8645         phba->last_completion_time = jiffies;
8646         del_timer(&phba->sli.mbox_tmo);
8647
8648         /* Move mbox data to caller's mailbox region, do endian swapping */
8649         if (pmb->mbox_cmpl && mbox)
8650                 lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
8651         /* Set the mailbox status with SLI4 range 0x4000 */
8652         mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
8653         if (mcqe_status != MB_CQE_STATUS_SUCCESS)
8654                 bf_set(lpfc_mqe_status, mqe,
8655                        (LPFC_MBX_ERROR_RANGE | mcqe_status));
8656
8657         if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
8658                 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
8659                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
8660                                       "MBOX dflt rpi: status:x%x rpi:x%x",
8661                                       mcqe_status,
8662                                       pmbox->un.varWords[0], 0);
8663                 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
8664                         mp = (struct lpfc_dmabuf *)(pmb->context1);
8665                         ndlp = (struct lpfc_nodelist *)pmb->context2;
8666                         /* Reg_LOGIN of dflt RPI was successful. Now lets get
8667                          * RID of the PPI using the same mbox buffer.
8668                          */
8669                         lpfc_unreg_login(phba, vport->vpi,
8670                                          pmbox->un.varWords[0], pmb);
8671                         pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
8672                         pmb->context1 = mp;
8673                         pmb->context2 = ndlp;
8674                         pmb->vport = vport;
8675                         rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
8676                         if (rc != MBX_BUSY)
8677                                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
8678                                                 LOG_SLI, "0385 rc should "
8679                                                 "have been MBX_BUSY\n");
8680                         if (rc != MBX_NOT_FINISHED)
8681                                 goto send_current_mbox;
8682                 }
8683         }
8684         spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
8685         phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
8686         spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
8687
8688         /* There is mailbox completion work to do */
8689         spin_lock_irqsave(&phba->hbalock, iflags);
8690         __lpfc_mbox_cmpl_put(phba, pmb);
8691         phba->work_ha |= HA_MBATT;
8692         spin_unlock_irqrestore(&phba->hbalock, iflags);
8693         workposted = true;
8694
8695 send_current_mbox:
8696         spin_lock_irqsave(&phba->hbalock, iflags);
8697         /* Release the mailbox command posting token */
8698         phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8699         /* Setting active mailbox pointer need to be in sync to flag clear */
8700         phba->sli.mbox_active = NULL;
8701         spin_unlock_irqrestore(&phba->hbalock, iflags);
8702         /* Wake up worker thread to post the next pending mailbox command */
8703         lpfc_worker_wake_up(phba);
8704 out_no_mqe_complete:
8705         if (bf_get(lpfc_trailer_consumed, mcqe))
8706                 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
8707         return workposted;
8708 }
8709
8710 /**
8711  * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
8712  * @phba: Pointer to HBA context object.
8713  * @cqe: Pointer to mailbox completion queue entry.
8714  *
8715  * This routine process a mailbox completion queue entry, it invokes the
8716  * proper mailbox complete handling or asynchrous event handling routine
8717  * according to the MCQE's async bit.
8718  *
8719  * Return: true if work posted to worker thread, otherwise false.
8720  **/
8721 static bool
8722 lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
8723 {
8724         struct lpfc_mcqe mcqe;
8725         bool workposted;
8726
8727         /* Copy the mailbox MCQE and convert endian order as needed */
8728         lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
8729
8730         /* Invoke the proper event handling routine */
8731         if (!bf_get(lpfc_trailer_async, &mcqe))
8732                 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
8733         else
8734                 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
8735         return workposted;
8736 }
8737
8738 /**
8739  * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
8740  * @phba: Pointer to HBA context object.
8741  * @wcqe: Pointer to work-queue completion queue entry.
8742  *
8743  * This routine handles an ELS work-queue completion event.
8744  *
8745  * Return: true if work posted to worker thread, otherwise false.
8746  **/
8747 static bool
8748 lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba,
8749                              struct lpfc_wcqe_complete *wcqe)
8750 {
8751         struct lpfc_iocbq *irspiocbq;
8752         unsigned long iflags;
8753
8754         /* Get an irspiocbq for later ELS response processing use */
8755         irspiocbq = lpfc_sli_get_iocbq(phba);
8756         if (!irspiocbq) {
8757                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8758                                 "0387 Failed to allocate an iocbq\n");
8759                 return false;
8760         }
8761
8762         /* Save off the slow-path queue event for work thread to process */
8763         memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
8764         spin_lock_irqsave(&phba->hbalock, iflags);
8765         list_add_tail(&irspiocbq->cq_event.list,
8766                       &phba->sli4_hba.sp_queue_event);
8767         phba->hba_flag |= HBA_SP_QUEUE_EVT;
8768         spin_unlock_irqrestore(&phba->hbalock, iflags);
8769
8770         return true;
8771 }
8772
8773 /**
8774  * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
8775  * @phba: Pointer to HBA context object.
8776  * @wcqe: Pointer to work-queue completion queue entry.
8777  *
8778  * This routine handles slow-path WQ entry comsumed event by invoking the
8779  * proper WQ release routine to the slow-path WQ.
8780  **/
8781 static void
8782 lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
8783                              struct lpfc_wcqe_release *wcqe)
8784 {
8785         /* Check for the slow-path ELS work queue */
8786         if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
8787                 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
8788                                      bf_get(lpfc_wcqe_r_wqe_index, wcqe));
8789         else
8790                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8791                                 "2579 Slow-path wqe consume event carries "
8792                                 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
8793                                 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
8794                                 phba->sli4_hba.els_wq->queue_id);
8795 }
8796
8797 /**
8798  * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
8799  * @phba: Pointer to HBA context object.
8800  * @cq: Pointer to a WQ completion queue.
8801  * @wcqe: Pointer to work-queue completion queue entry.
8802  *
8803  * This routine handles an XRI abort event.
8804  *
8805  * Return: true if work posted to worker thread, otherwise false.
8806  **/
8807 static bool
8808 lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
8809                                    struct lpfc_queue *cq,
8810                                    struct sli4_wcqe_xri_aborted *wcqe)
8811 {
8812         bool workposted = false;
8813         struct lpfc_cq_event *cq_event;
8814         unsigned long iflags;
8815
8816         /* Allocate a new internal CQ_EVENT entry */
8817         cq_event = lpfc_sli4_cq_event_alloc(phba);
8818         if (!cq_event) {
8819                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8820                                 "0602 Failed to allocate CQ_EVENT entry\n");
8821                 return false;
8822         }
8823
8824         /* Move the CQE into the proper xri abort event list */
8825         memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
8826         switch (cq->subtype) {
8827         case LPFC_FCP:
8828                 spin_lock_irqsave(&phba->hbalock, iflags);
8829                 list_add_tail(&cq_event->list,
8830                               &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
8831                 /* Set the fcp xri abort event flag */
8832                 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
8833                 spin_unlock_irqrestore(&phba->hbalock, iflags);
8834                 workposted = true;
8835                 break;
8836         case LPFC_ELS:
8837                 spin_lock_irqsave(&phba->hbalock, iflags);
8838                 list_add_tail(&cq_event->list,
8839                               &phba->sli4_hba.sp_els_xri_aborted_work_queue);
8840                 /* Set the els xri abort event flag */
8841                 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
8842                 spin_unlock_irqrestore(&phba->hbalock, iflags);
8843                 workposted = true;
8844                 break;
8845         default:
8846                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8847                                 "0603 Invalid work queue CQE subtype (x%x)\n",
8848                                 cq->subtype);
8849                 workposted = false;
8850                 break;
8851         }
8852         return workposted;
8853 }
8854
8855 /**
8856  * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
8857  * @phba: Pointer to HBA context object.
8858  * @rcqe: Pointer to receive-queue completion queue entry.
8859  *
8860  * This routine process a receive-queue completion queue entry.
8861  *
8862  * Return: true if work posted to worker thread, otherwise false.
8863  **/
8864 static bool
8865 lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
8866 {
8867         bool workposted = false;
8868         struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
8869         struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
8870         struct hbq_dmabuf *dma_buf;
8871         uint32_t status;
8872         unsigned long iflags;
8873
8874         if (bf_get(lpfc_rcqe_rq_id, rcqe) != hrq->queue_id)
8875                 goto out;
8876
8877         status = bf_get(lpfc_rcqe_status, rcqe);
8878         switch (status) {
8879         case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
8880                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8881                                 "2537 Receive Frame Truncated!!\n");
8882         case FC_STATUS_RQ_SUCCESS:
8883                 lpfc_sli4_rq_release(hrq, drq);
8884                 spin_lock_irqsave(&phba->hbalock, iflags);
8885                 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
8886                 if (!dma_buf) {
8887                         spin_unlock_irqrestore(&phba->hbalock, iflags);
8888                         goto out;
8889                 }
8890                 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
8891                 /* save off the frame for the word thread to process */
8892                 list_add_tail(&dma_buf->cq_event.list,
8893                               &phba->sli4_hba.sp_queue_event);
8894                 /* Frame received */
8895                 phba->hba_flag |= HBA_SP_QUEUE_EVT;
8896                 spin_unlock_irqrestore(&phba->hbalock, iflags);
8897                 workposted = true;
8898                 break;
8899         case FC_STATUS_INSUFF_BUF_NEED_BUF:
8900         case FC_STATUS_INSUFF_BUF_FRM_DISC:
8901                 /* Post more buffers if possible */
8902                 spin_lock_irqsave(&phba->hbalock, iflags);
8903                 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
8904                 spin_unlock_irqrestore(&phba->hbalock, iflags);
8905                 workposted = true;
8906                 break;
8907         }
8908 out:
8909         return workposted;
8910 }
8911
8912 /**
8913  * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
8914  * @phba: Pointer to HBA context object.
8915  * @cq: Pointer to the completion queue.
8916  * @wcqe: Pointer to a completion queue entry.
8917  *
8918  * This routine process a slow-path work-queue or recieve queue completion queue
8919  * entry.
8920  *
8921  * Return: true if work posted to worker thread, otherwise false.
8922  **/
8923 static bool
8924 lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
8925                          struct lpfc_cqe *cqe)
8926 {
8927         struct lpfc_cqe cqevt;
8928         bool workposted = false;
8929
8930         /* Copy the work queue CQE and convert endian order if needed */
8931         lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
8932
8933         /* Check and process for different type of WCQE and dispatch */
8934         switch (bf_get(lpfc_cqe_code, &cqevt)) {
8935         case CQE_CODE_COMPL_WQE:
8936                 /* Process the WQ/RQ complete event */
8937                 workposted = lpfc_sli4_sp_handle_els_wcqe(phba,
8938                                 (struct lpfc_wcqe_complete *)&cqevt);
8939                 break;
8940         case CQE_CODE_RELEASE_WQE:
8941                 /* Process the WQ release event */
8942                 lpfc_sli4_sp_handle_rel_wcqe(phba,
8943                                 (struct lpfc_wcqe_release *)&cqevt);
8944                 break;
8945         case CQE_CODE_XRI_ABORTED:
8946                 /* Process the WQ XRI abort event */
8947                 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
8948                                 (struct sli4_wcqe_xri_aborted *)&cqevt);
8949                 break;
8950         case CQE_CODE_RECEIVE:
8951                 /* Process the RQ event */
8952                 workposted = lpfc_sli4_sp_handle_rcqe(phba,
8953                                 (struct lpfc_rcqe *)&cqevt);
8954                 break;
8955         default:
8956                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8957                                 "0388 Not a valid WCQE code: x%x\n",
8958                                 bf_get(lpfc_cqe_code, &cqevt));
8959                 break;
8960         }
8961         return workposted;
8962 }
8963
8964 /**
8965  * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
8966  * @phba: Pointer to HBA context object.
8967  * @eqe: Pointer to fast-path event queue entry.
8968  *
8969  * This routine process a event queue entry from the slow-path event queue.
8970  * It will check the MajorCode and MinorCode to determine this is for a
8971  * completion event on a completion queue, if not, an error shall be logged
8972  * and just return. Otherwise, it will get to the corresponding completion
8973  * queue and process all the entries on that completion queue, rearm the
8974  * completion queue, and then return.
8975  *
8976  **/
8977 static void
8978 lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
8979 {
8980         struct lpfc_queue *cq = NULL, *childq, *speq;
8981         struct lpfc_cqe *cqe;
8982         bool workposted = false;
8983         int ecount = 0;
8984         uint16_t cqid;
8985
8986         if (bf_get(lpfc_eqe_major_code, eqe) != 0) {
8987                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8988                                 "0359 Not a valid slow-path completion "
8989                                 "event: majorcode=x%x, minorcode=x%x\n",
8990                                 bf_get(lpfc_eqe_major_code, eqe),
8991                                 bf_get(lpfc_eqe_minor_code, eqe));
8992                 return;
8993         }
8994
8995         /* Get the reference to the corresponding CQ */
8996         cqid = bf_get(lpfc_eqe_resource_id, eqe);
8997
8998         /* Search for completion queue pointer matching this cqid */
8999         speq = phba->sli4_hba.sp_eq;
9000         list_for_each_entry(childq, &speq->child_list, list) {
9001                 if (childq->queue_id == cqid) {
9002                         cq = childq;
9003                         break;
9004                 }
9005         }
9006         if (unlikely(!cq)) {
9007                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9008                                 "0365 Slow-path CQ identifier (%d) does "
9009                                 "not exist\n", cqid);
9010                 return;
9011         }
9012
9013         /* Process all the entries to the CQ */
9014         switch (cq->type) {
9015         case LPFC_MCQ:
9016                 while ((cqe = lpfc_sli4_cq_get(cq))) {
9017                         workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
9018                         if (!(++ecount % LPFC_GET_QE_REL_INT))
9019                                 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
9020                 }
9021                 break;
9022         case LPFC_WCQ:
9023                 while ((cqe = lpfc_sli4_cq_get(cq))) {
9024                         workposted |= lpfc_sli4_sp_handle_cqe(phba, cq, cqe);
9025                         if (!(++ecount % LPFC_GET_QE_REL_INT))
9026                                 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
9027                 }
9028                 break;
9029         default:
9030                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9031                                 "0370 Invalid completion queue type (%d)\n",
9032                                 cq->type);
9033                 return;
9034         }
9035
9036         /* Catch the no cq entry condition, log an error */
9037         if (unlikely(ecount == 0))
9038                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9039                                 "0371 No entry from the CQ: identifier "
9040                                 "(x%x), type (%d)\n", cq->queue_id, cq->type);
9041
9042         /* In any case, flash and re-arm the RCQ */
9043         lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
9044
9045         /* wake up worker thread if there are works to be done */
9046         if (workposted)
9047                 lpfc_worker_wake_up(phba);
9048 }
9049
9050 /**
9051  * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
9052  * @eqe: Pointer to fast-path completion queue entry.
9053  *
9054  * This routine process a fast-path work queue completion entry from fast-path
9055  * event queue for FCP command response completion.
9056  **/
9057 static void
9058 lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba,
9059                              struct lpfc_wcqe_complete *wcqe)
9060 {
9061         struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
9062         struct lpfc_iocbq *cmdiocbq;
9063         struct lpfc_iocbq irspiocbq;
9064         unsigned long iflags;
9065
9066         spin_lock_irqsave(&phba->hbalock, iflags);
9067         pring->stats.iocb_event++;
9068         spin_unlock_irqrestore(&phba->hbalock, iflags);
9069
9070         /* Check for response status */
9071         if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
9072                 /* If resource errors reported from HBA, reduce queue
9073                  * depth of the SCSI device.
9074                  */
9075                 if ((bf_get(lpfc_wcqe_c_status, wcqe) ==
9076                      IOSTAT_LOCAL_REJECT) &&
9077                     (wcqe->parameter == IOERR_NO_RESOURCES)) {
9078                         phba->lpfc_rampdown_queue_depth(phba);
9079                 }
9080                 /* Log the error status */
9081                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9082                                 "0373 FCP complete error: status=x%x, "
9083                                 "hw_status=x%x, total_data_specified=%d, "
9084                                 "parameter=x%x, word3=x%x\n",
9085                                 bf_get(lpfc_wcqe_c_status, wcqe),
9086                                 bf_get(lpfc_wcqe_c_hw_status, wcqe),
9087                                 wcqe->total_data_placed, wcqe->parameter,
9088                                 wcqe->word3);
9089         }
9090
9091         /* Look up the FCP command IOCB and create pseudo response IOCB */
9092         spin_lock_irqsave(&phba->hbalock, iflags);
9093         cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
9094                                 bf_get(lpfc_wcqe_c_request_tag, wcqe));
9095         spin_unlock_irqrestore(&phba->hbalock, iflags);
9096         if (unlikely(!cmdiocbq)) {
9097                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9098                                 "0374 FCP complete with no corresponding "
9099                                 "cmdiocb: iotag (%d)\n",
9100                                 bf_get(lpfc_wcqe_c_request_tag, wcqe));
9101                 return;
9102         }
9103         if (unlikely(!cmdiocbq->iocb_cmpl)) {
9104                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9105                                 "0375 FCP cmdiocb not callback function "
9106                                 "iotag: (%d)\n",
9107                                 bf_get(lpfc_wcqe_c_request_tag, wcqe));
9108                 return;
9109         }
9110
9111         /* Fake the irspiocb and copy necessary response information */
9112         lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
9113
9114         if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
9115                 spin_lock_irqsave(&phba->hbalock, iflags);
9116                 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
9117                 spin_unlock_irqrestore(&phba->hbalock, iflags);
9118         }
9119
9120         /* Pass the cmd_iocb and the rsp state to the upper layer */
9121         (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
9122 }
9123
9124 /**
9125  * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
9126  * @phba: Pointer to HBA context object.
9127  * @cq: Pointer to completion queue.
9128  * @wcqe: Pointer to work-queue completion queue entry.
9129  *
9130  * This routine handles an fast-path WQ entry comsumed event by invoking the
9131  * proper WQ release routine to the slow-path WQ.
9132  **/
9133 static void
9134 lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
9135                              struct lpfc_wcqe_release *wcqe)
9136 {
9137         struct lpfc_queue *childwq;
9138         bool wqid_matched = false;
9139         uint16_t fcp_wqid;
9140
9141         /* Check for fast-path FCP work queue release */
9142         fcp_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
9143         list_for_each_entry(childwq, &cq->child_list, list) {
9144                 if (childwq->queue_id == fcp_wqid) {
9145                         lpfc_sli4_wq_release(childwq,
9146                                         bf_get(lpfc_wcqe_r_wqe_index, wcqe));
9147                         wqid_matched = true;
9148                         break;
9149                 }
9150         }
9151         /* Report warning log message if no match found */
9152         if (wqid_matched != true)
9153                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9154                                 "2580 Fast-path wqe consume event carries "
9155                                 "miss-matched qid: wcqe-qid=x%x\n", fcp_wqid);
9156 }
9157
9158 /**
9159  * lpfc_sli4_fp_handle_wcqe - Process fast-path work queue completion entry
9160  * @cq: Pointer to the completion queue.
9161  * @eqe: Pointer to fast-path completion queue entry.
9162  *
9163  * This routine process a fast-path work queue completion entry from fast-path
9164  * event queue for FCP command response completion.
9165  **/
9166 static int
9167 lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
9168                          struct lpfc_cqe *cqe)
9169 {
9170         struct lpfc_wcqe_release wcqe;
9171         bool workposted = false;
9172
9173         /* Copy the work queue CQE and convert endian order if needed */
9174         lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
9175
9176         /* Check and process for different type of WCQE and dispatch */
9177         switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
9178         case CQE_CODE_COMPL_WQE:
9179                 /* Process the WQ complete event */
9180                 lpfc_sli4_fp_handle_fcp_wcqe(phba,
9181                                 (struct lpfc_wcqe_complete *)&wcqe);
9182                 break;
9183         case CQE_CODE_RELEASE_WQE:
9184                 /* Process the WQ release event */
9185                 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
9186                                 (struct lpfc_wcqe_release *)&wcqe);
9187                 break;
9188         case CQE_CODE_XRI_ABORTED:
9189                 /* Process the WQ XRI abort event */
9190                 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
9191                                 (struct sli4_wcqe_xri_aborted *)&wcqe);
9192                 break;
9193         default:
9194                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9195                                 "0144 Not a valid WCQE code: x%x\n",
9196                                 bf_get(lpfc_wcqe_c_code, &wcqe));
9197                 break;
9198         }
9199         return workposted;
9200 }
9201
9202 /**
9203  * lpfc_sli4_fp_handle_eqe - Process a fast-path event queue entry
9204  * @phba: Pointer to HBA context object.
9205  * @eqe: Pointer to fast-path event queue entry.
9206  *
9207  * This routine process a event queue entry from the fast-path event queue.
9208  * It will check the MajorCode and MinorCode to determine this is for a
9209  * completion event on a completion queue, if not, an error shall be logged
9210  * and just return. Otherwise, it will get to the corresponding completion
9211  * queue and process all the entries on the completion queue, rearm the
9212  * completion queue, and then return.
9213  **/
9214 static void
9215 lpfc_sli4_fp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
9216                         uint32_t fcp_cqidx)
9217 {
9218         struct lpfc_queue *cq;
9219         struct lpfc_cqe *cqe;
9220         bool workposted = false;
9221         uint16_t cqid;
9222         int ecount = 0;
9223
9224         if (unlikely(bf_get(lpfc_eqe_major_code, eqe) != 0)) {
9225                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9226                                 "0366 Not a valid fast-path completion "
9227                                 "event: majorcode=x%x, minorcode=x%x\n",
9228                                 bf_get(lpfc_eqe_major_code, eqe),
9229                                 bf_get(lpfc_eqe_minor_code, eqe));
9230                 return;
9231         }
9232
9233         cq = phba->sli4_hba.fcp_cq[fcp_cqidx];
9234         if (unlikely(!cq)) {
9235                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9236                                 "0367 Fast-path completion queue does not "
9237                                 "exist\n");
9238                 return;
9239         }
9240
9241         /* Get the reference to the corresponding CQ */
9242         cqid = bf_get(lpfc_eqe_resource_id, eqe);
9243         if (unlikely(cqid != cq->queue_id)) {
9244                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9245                                 "0368 Miss-matched fast-path completion "
9246                                 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
9247                                 cqid, cq->queue_id);
9248                 return;
9249         }
9250
9251         /* Process all the entries to the CQ */
9252         while ((cqe = lpfc_sli4_cq_get(cq))) {
9253                 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
9254                 if (!(++ecount % LPFC_GET_QE_REL_INT))
9255                         lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
9256         }
9257
9258         /* Catch the no cq entry condition */
9259         if (unlikely(ecount == 0))
9260                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9261                                 "0369 No entry from fast-path completion "
9262                                 "queue fcpcqid=%d\n", cq->queue_id);
9263
9264         /* In any case, flash and re-arm the CQ */
9265         lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
9266
9267         /* wake up worker thread if there are works to be done */
9268         if (workposted)
9269                 lpfc_worker_wake_up(phba);
9270 }
9271
9272 static void
9273 lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
9274 {
9275         struct lpfc_eqe *eqe;
9276
9277         /* walk all the EQ entries and drop on the floor */
9278         while ((eqe = lpfc_sli4_eq_get(eq)))
9279                 ;
9280
9281         /* Clear and re-arm the EQ */
9282         lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
9283 }
9284
9285 /**
9286  * lpfc_sli4_sp_intr_handler - Slow-path interrupt handler to SLI-4 device
9287  * @irq: Interrupt number.
9288  * @dev_id: The device context pointer.
9289  *
9290  * This function is directly called from the PCI layer as an interrupt
9291  * service routine when device with SLI-4 interface spec is enabled with
9292  * MSI-X multi-message interrupt mode and there are slow-path events in
9293  * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
9294  * interrupt mode, this function is called as part of the device-level
9295  * interrupt handler. When the PCI slot is in error recovery or the HBA is
9296  * undergoing initialization, the interrupt handler will not process the
9297  * interrupt. The link attention and ELS ring attention events are handled
9298  * by the worker thread. The interrupt handler signals the worker thread
9299  * and returns for these events. This function is called without any lock
9300  * held. It gets the hbalock to access and update SLI data structures.
9301  *
9302  * This function returns IRQ_HANDLED when interrupt is handled else it
9303  * returns IRQ_NONE.
9304  **/
9305 irqreturn_t
9306 lpfc_sli4_sp_intr_handler(int irq, void *dev_id)
9307 {
9308         struct lpfc_hba *phba;
9309         struct lpfc_queue *speq;
9310         struct lpfc_eqe *eqe;
9311         unsigned long iflag;
9312         int ecount = 0;
9313
9314         /*
9315          * Get the driver's phba structure from the dev_id
9316          */
9317         phba = (struct lpfc_hba *)dev_id;
9318
9319         if (unlikely(!phba))
9320                 return IRQ_NONE;
9321
9322         /* Get to the EQ struct associated with this vector */
9323         speq = phba->sli4_hba.sp_eq;
9324
9325         /* Check device state for handling interrupt */
9326         if (unlikely(lpfc_intr_state_check(phba))) {
9327                 /* Check again for link_state with lock held */
9328                 spin_lock_irqsave(&phba->hbalock, iflag);
9329                 if (phba->link_state < LPFC_LINK_DOWN)
9330                         /* Flush, clear interrupt, and rearm the EQ */
9331                         lpfc_sli4_eq_flush(phba, speq);
9332                 spin_unlock_irqrestore(&phba->hbalock, iflag);
9333                 return IRQ_NONE;
9334         }
9335
9336         /*
9337          * Process all the event on FCP slow-path EQ
9338          */
9339         while ((eqe = lpfc_sli4_eq_get(speq))) {
9340                 lpfc_sli4_sp_handle_eqe(phba, eqe);
9341                 if (!(++ecount % LPFC_GET_QE_REL_INT))
9342                         lpfc_sli4_eq_release(speq, LPFC_QUEUE_NOARM);
9343         }
9344
9345         /* Always clear and re-arm the slow-path EQ */
9346         lpfc_sli4_eq_release(speq, LPFC_QUEUE_REARM);
9347
9348         /* Catch the no cq entry condition */
9349         if (unlikely(ecount == 0)) {
9350                 if (phba->intr_type == MSIX)
9351                         /* MSI-X treated interrupt served as no EQ share INT */
9352                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9353                                         "0357 MSI-X interrupt with no EQE\n");
9354                 else
9355                         /* Non MSI-X treated on interrupt as EQ share INT */
9356                         return IRQ_NONE;
9357         }
9358
9359         return IRQ_HANDLED;
9360 } /* lpfc_sli4_sp_intr_handler */
9361
9362 /**
9363  * lpfc_sli4_fp_intr_handler - Fast-path interrupt handler to SLI-4 device
9364  * @irq: Interrupt number.
9365  * @dev_id: The device context pointer.
9366  *
9367  * This function is directly called from the PCI layer as an interrupt
9368  * service routine when device with SLI-4 interface spec is enabled with
9369  * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
9370  * ring event in the HBA. However, when the device is enabled with either
9371  * MSI or Pin-IRQ interrupt mode, this function is called as part of the
9372  * device-level interrupt handler. When the PCI slot is in error recovery
9373  * or the HBA is undergoing initialization, the interrupt handler will not
9374  * process the interrupt. The SCSI FCP fast-path ring event are handled in
9375  * the intrrupt context. This function is called without any lock held.
9376  * It gets the hbalock to access and update SLI data structures. Note that,
9377  * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
9378  * equal to that of FCP CQ index.
9379  *
9380  * This function returns IRQ_HANDLED when interrupt is handled else it
9381  * returns IRQ_NONE.
9382  **/
9383 irqreturn_t
9384 lpfc_sli4_fp_intr_handler(int irq, void *dev_id)
9385 {
9386         struct lpfc_hba *phba;
9387         struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
9388         struct lpfc_queue *fpeq;
9389         struct lpfc_eqe *eqe;
9390         unsigned long iflag;
9391         int ecount = 0;
9392         uint32_t fcp_eqidx;
9393
9394         /* Get the driver's phba structure from the dev_id */
9395         fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
9396         phba = fcp_eq_hdl->phba;
9397         fcp_eqidx = fcp_eq_hdl->idx;
9398
9399         if (unlikely(!phba))
9400                 return IRQ_NONE;
9401
9402         /* Get to the EQ struct associated with this vector */
9403         fpeq = phba->sli4_hba.fp_eq[fcp_eqidx];
9404
9405         /* Check device state for handling interrupt */
9406         if (unlikely(lpfc_intr_state_check(phba))) {
9407                 /* Check again for link_state with lock held */
9408                 spin_lock_irqsave(&phba->hbalock, iflag);
9409                 if (phba->link_state < LPFC_LINK_DOWN)
9410                         /* Flush, clear interrupt, and rearm the EQ */
9411                         lpfc_sli4_eq_flush(phba, fpeq);
9412                 spin_unlock_irqrestore(&phba->hbalock, iflag);
9413                 return IRQ_NONE;
9414         }
9415
9416         /*
9417          * Process all the event on FCP fast-path EQ
9418          */
9419         while ((eqe = lpfc_sli4_eq_get(fpeq))) {
9420                 lpfc_sli4_fp_handle_eqe(phba, eqe, fcp_eqidx);
9421                 if (!(++ecount % LPFC_GET_QE_REL_INT))
9422                         lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
9423         }
9424
9425         /* Always clear and re-arm the fast-path EQ */
9426         lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
9427
9428         if (unlikely(ecount == 0)) {
9429                 if (phba->intr_type == MSIX)
9430                         /* MSI-X treated interrupt served as no EQ share INT */
9431                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9432                                         "0358 MSI-X interrupt with no EQE\n");
9433                 else
9434                         /* Non MSI-X treated on interrupt as EQ share INT */
9435                         return IRQ_NONE;
9436         }
9437
9438         return IRQ_HANDLED;
9439 } /* lpfc_sli4_fp_intr_handler */
9440
9441 /**
9442  * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
9443  * @irq: Interrupt number.
9444  * @dev_id: The device context pointer.
9445  *
9446  * This function is the device-level interrupt handler to device with SLI-4
9447  * interface spec, called from the PCI layer when either MSI or Pin-IRQ
9448  * interrupt mode is enabled and there is an event in the HBA which requires
9449  * driver attention. This function invokes the slow-path interrupt attention
9450  * handling function and fast-path interrupt attention handling function in
9451  * turn to process the relevant HBA attention events. This function is called
9452  * without any lock held. It gets the hbalock to access and update SLI data
9453  * structures.
9454  *
9455  * This function returns IRQ_HANDLED when interrupt is handled, else it
9456  * returns IRQ_NONE.
9457  **/
9458 irqreturn_t
9459 lpfc_sli4_intr_handler(int irq, void *dev_id)
9460 {
9461         struct lpfc_hba  *phba;
9462         irqreturn_t sp_irq_rc, fp_irq_rc;
9463         bool fp_handled = false;
9464         uint32_t fcp_eqidx;
9465
9466         /* Get the driver's phba structure from the dev_id */
9467         phba = (struct lpfc_hba *)dev_id;
9468
9469         if (unlikely(!phba))
9470                 return IRQ_NONE;
9471
9472         /*
9473          * Invokes slow-path host attention interrupt handling as appropriate.
9474          */
9475         sp_irq_rc = lpfc_sli4_sp_intr_handler(irq, dev_id);
9476
9477         /*
9478          * Invoke fast-path host attention interrupt handling as appropriate.
9479          */
9480         for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
9481                 fp_irq_rc = lpfc_sli4_fp_intr_handler(irq,
9482                                         &phba->sli4_hba.fcp_eq_hdl[fcp_eqidx]);
9483                 if (fp_irq_rc == IRQ_HANDLED)
9484                         fp_handled |= true;
9485         }
9486
9487         return (fp_handled == true) ? IRQ_HANDLED : sp_irq_rc;
9488 } /* lpfc_sli4_intr_handler */
9489
9490 /**
9491  * lpfc_sli4_queue_free - free a queue structure and associated memory
9492  * @queue: The queue structure to free.
9493  *
9494  * This function frees a queue structure and the DMAable memeory used for
9495  * the host resident queue. This function must be called after destroying the
9496  * queue on the HBA.
9497  **/
9498 void
9499 lpfc_sli4_queue_free(struct lpfc_queue *queue)
9500 {
9501         struct lpfc_dmabuf *dmabuf;
9502
9503         if (!queue)
9504                 return;
9505
9506         while (!list_empty(&queue->page_list)) {
9507                 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
9508                                  list);
9509                 dma_free_coherent(&queue->phba->pcidev->dev, PAGE_SIZE,
9510                                   dmabuf->virt, dmabuf->phys);
9511                 kfree(dmabuf);
9512         }
9513         kfree(queue);
9514         return;
9515 }
9516
9517 /**
9518  * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
9519  * @phba: The HBA that this queue is being created on.
9520  * @entry_size: The size of each queue entry for this queue.
9521  * @entry count: The number of entries that this queue will handle.
9522  *
9523  * This function allocates a queue structure and the DMAable memory used for
9524  * the host resident queue. This function must be called before creating the
9525  * queue on the HBA.
9526  **/
9527 struct lpfc_queue *
9528 lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
9529                       uint32_t entry_count)
9530 {
9531         struct lpfc_queue *queue;
9532         struct lpfc_dmabuf *dmabuf;
9533         int x, total_qe_count;
9534         void *dma_pointer;
9535
9536
9537         queue = kzalloc(sizeof(struct lpfc_queue) +
9538                         (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
9539         if (!queue)
9540                 return NULL;
9541         queue->page_count = (PAGE_ALIGN(entry_size * entry_count))/PAGE_SIZE;
9542         INIT_LIST_HEAD(&queue->list);
9543         INIT_LIST_HEAD(&queue->page_list);
9544         INIT_LIST_HEAD(&queue->child_list);
9545         for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
9546                 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
9547                 if (!dmabuf)
9548                         goto out_fail;
9549                 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
9550                                                   PAGE_SIZE, &dmabuf->phys,
9551                                                   GFP_KERNEL);
9552                 if (!dmabuf->virt) {
9553                         kfree(dmabuf);
9554                         goto out_fail;
9555                 }
9556                 memset(dmabuf->virt, 0, PAGE_SIZE);
9557                 dmabuf->buffer_tag = x;
9558                 list_add_tail(&dmabuf->list, &queue->page_list);
9559                 /* initialize queue's entry array */
9560                 dma_pointer = dmabuf->virt;
9561                 for (; total_qe_count < entry_count &&
9562                      dma_pointer < (PAGE_SIZE + dmabuf->virt);
9563                      total_qe_count++, dma_pointer += entry_size) {
9564                         queue->qe[total_qe_count].address = dma_pointer;
9565                 }
9566         }
9567         queue->entry_size = entry_size;
9568         queue->entry_count = entry_count;
9569         queue->phba = phba;
9570
9571         return queue;
9572 out_fail:
9573         lpfc_sli4_queue_free(queue);
9574         return NULL;
9575 }
9576
9577 /**
9578  * lpfc_eq_create - Create an Event Queue on the HBA
9579  * @phba: HBA structure that indicates port to create a queue on.
9580  * @eq: The queue structure to use to create the event queue.
9581  * @imax: The maximum interrupt per second limit.
9582  *
9583  * This function creates an event queue, as detailed in @eq, on a port,
9584  * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
9585  *
9586  * The @phba struct is used to send mailbox command to HBA. The @eq struct
9587  * is used to get the entry count and entry size that are necessary to
9588  * determine the number of pages to allocate and use for this queue. This
9589  * function will send the EQ_CREATE mailbox command to the HBA to setup the
9590  * event queue. This function is asynchronous and will wait for the mailbox
9591  * command to finish before continuing.
9592  *
9593  * On success this function will return a zero. If unable to allocate enough
9594  * memory this function will return ENOMEM. If the queue create mailbox command
9595  * fails this function will return ENXIO.
9596  **/
9597 uint32_t
9598 lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint16_t imax)
9599 {
9600         struct lpfc_mbx_eq_create *eq_create;
9601         LPFC_MBOXQ_t *mbox;
9602         int rc, length, status = 0;
9603         struct lpfc_dmabuf *dmabuf;
9604         uint32_t shdr_status, shdr_add_status;
9605         union lpfc_sli4_cfg_shdr *shdr;
9606         uint16_t dmult;
9607
9608         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9609         if (!mbox)
9610                 return -ENOMEM;
9611         length = (sizeof(struct lpfc_mbx_eq_create) -
9612                   sizeof(struct lpfc_sli4_cfg_mhdr));
9613         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
9614                          LPFC_MBOX_OPCODE_EQ_CREATE,
9615                          length, LPFC_SLI4_MBX_EMBED);
9616         eq_create = &mbox->u.mqe.un.eq_create;
9617         bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
9618                eq->page_count);
9619         bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
9620                LPFC_EQE_SIZE);
9621         bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
9622         /* Calculate delay multiper from maximum interrupt per second */
9623         dmult = LPFC_DMULT_CONST/imax - 1;
9624         bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
9625                dmult);
9626         switch (eq->entry_count) {
9627         default:
9628                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9629                                 "0360 Unsupported EQ count. (%d)\n",
9630                                 eq->entry_count);
9631                 if (eq->entry_count < 256)
9632                         return -EINVAL;
9633                 /* otherwise default to smallest count (drop through) */
9634         case 256:
9635                 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9636                        LPFC_EQ_CNT_256);
9637                 break;
9638         case 512:
9639                 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9640                        LPFC_EQ_CNT_512);
9641                 break;
9642         case 1024:
9643                 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9644                        LPFC_EQ_CNT_1024);
9645                 break;
9646         case 2048:
9647                 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9648                        LPFC_EQ_CNT_2048);
9649                 break;
9650         case 4096:
9651                 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9652                        LPFC_EQ_CNT_4096);
9653                 break;
9654         }
9655         list_for_each_entry(dmabuf, &eq->page_list, list) {
9656                 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9657                                         putPaddrLow(dmabuf->phys);
9658                 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9659                                         putPaddrHigh(dmabuf->phys);
9660         }
9661         mbox->vport = phba->pport;
9662         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
9663         mbox->context1 = NULL;
9664         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9665         shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
9666         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9667         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9668         if (shdr_status || shdr_add_status || rc) {
9669                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9670                                 "2500 EQ_CREATE mailbox failed with "
9671                                 "status x%x add_status x%x, mbx status x%x\n",
9672                                 shdr_status, shdr_add_status, rc);
9673                 status = -ENXIO;
9674         }
9675         eq->type = LPFC_EQ;
9676         eq->subtype = LPFC_NONE;
9677         eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
9678         if (eq->queue_id == 0xFFFF)
9679                 status = -ENXIO;
9680         eq->host_index = 0;
9681         eq->hba_index = 0;
9682
9683         mempool_free(mbox, phba->mbox_mem_pool);
9684         return status;
9685 }
9686
9687 /**
9688  * lpfc_cq_create - Create a Completion Queue on the HBA
9689  * @phba: HBA structure that indicates port to create a queue on.
9690  * @cq: The queue structure to use to create the completion queue.
9691  * @eq: The event queue to bind this completion queue to.
9692  *
9693  * This function creates a completion queue, as detailed in @wq, on a port,
9694  * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
9695  *
9696  * The @phba struct is used to send mailbox command to HBA. The @cq struct
9697  * is used to get the entry count and entry size that are necessary to
9698  * determine the number of pages to allocate and use for this queue. The @eq
9699  * is used to indicate which event queue to bind this completion queue to. This
9700  * function will send the CQ_CREATE mailbox command to the HBA to setup the
9701  * completion queue. This function is asynchronous and will wait for the mailbox
9702  * command to finish before continuing.
9703  *
9704  * On success this function will return a zero. If unable to allocate enough
9705  * memory this function will return ENOMEM. If the queue create mailbox command
9706  * fails this function will return ENXIO.
9707  **/
9708 uint32_t
9709 lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
9710                struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
9711 {
9712         struct lpfc_mbx_cq_create *cq_create;
9713         struct lpfc_dmabuf *dmabuf;
9714         LPFC_MBOXQ_t *mbox;
9715         int rc, length, status = 0;
9716         uint32_t shdr_status, shdr_add_status;
9717         union lpfc_sli4_cfg_shdr *shdr;
9718
9719         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9720         if (!mbox)
9721                 return -ENOMEM;
9722         length = (sizeof(struct lpfc_mbx_cq_create) -
9723                   sizeof(struct lpfc_sli4_cfg_mhdr));
9724         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
9725                          LPFC_MBOX_OPCODE_CQ_CREATE,
9726                          length, LPFC_SLI4_MBX_EMBED);
9727         cq_create = &mbox->u.mqe.un.cq_create;
9728         bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
9729                     cq->page_count);
9730         bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
9731         bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
9732         bf_set(lpfc_cq_eq_id, &cq_create->u.request.context, eq->queue_id);
9733         switch (cq->entry_count) {
9734         default:
9735                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9736                                 "0361 Unsupported CQ count. (%d)\n",
9737                                 cq->entry_count);
9738                 if (cq->entry_count < 256)
9739                         return -EINVAL;
9740                 /* otherwise default to smallest count (drop through) */
9741         case 256:
9742                 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
9743                        LPFC_CQ_CNT_256);
9744                 break;
9745         case 512:
9746                 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
9747                        LPFC_CQ_CNT_512);
9748                 break;
9749         case 1024:
9750                 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
9751                        LPFC_CQ_CNT_1024);
9752                 break;
9753         }
9754         list_for_each_entry(dmabuf, &cq->page_list, list) {
9755                 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9756                                         putPaddrLow(dmabuf->phys);
9757                 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9758                                         putPaddrHigh(dmabuf->phys);
9759         }
9760         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9761
9762         /* The IOCTL status is embedded in the mailbox subheader. */
9763         shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
9764         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9765         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9766         if (shdr_status || shdr_add_status || rc) {
9767                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9768                                 "2501 CQ_CREATE mailbox failed with "
9769                                 "status x%x add_status x%x, mbx status x%x\n",
9770                                 shdr_status, shdr_add_status, rc);
9771                 status = -ENXIO;
9772                 goto out;
9773         }
9774         cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
9775         if (cq->queue_id == 0xFFFF) {
9776                 status = -ENXIO;
9777                 goto out;
9778         }
9779         /* link the cq onto the parent eq child list */
9780         list_add_tail(&cq->list, &eq->child_list);
9781         /* Set up completion queue's type and subtype */
9782         cq->type = type;
9783         cq->subtype = subtype;
9784         cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
9785         cq->host_index = 0;
9786         cq->hba_index = 0;
9787
9788 out:
9789         mempool_free(mbox, phba->mbox_mem_pool);
9790         return status;
9791 }
9792
9793 /**
9794  * lpfc_mq_create - Create a mailbox Queue on the HBA
9795  * @phba: HBA structure that indicates port to create a queue on.
9796  * @mq: The queue structure to use to create the mailbox queue.
9797  *
9798  * This function creates a mailbox queue, as detailed in @mq, on a port,
9799  * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
9800  *
9801  * The @phba struct is used to send mailbox command to HBA. The @cq struct
9802  * is used to get the entry count and entry size that are necessary to
9803  * determine the number of pages to allocate and use for this queue. This
9804  * function will send the MQ_CREATE mailbox command to the HBA to setup the
9805  * mailbox queue. This function is asynchronous and will wait for the mailbox
9806  * command to finish before continuing.
9807  *
9808  * On success this function will return a zero. If unable to allocate enough
9809  * memory this function will return ENOMEM. If the queue create mailbox command
9810  * fails this function will return ENXIO.
9811  **/
9812 uint32_t
9813 lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
9814                struct lpfc_queue *cq, uint32_t subtype)
9815 {
9816         struct lpfc_mbx_mq_create *mq_create;
9817         struct lpfc_dmabuf *dmabuf;
9818         LPFC_MBOXQ_t *mbox;
9819         int rc, length, status = 0;
9820         uint32_t shdr_status, shdr_add_status;
9821         union lpfc_sli4_cfg_shdr *shdr;
9822
9823         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9824         if (!mbox)
9825                 return -ENOMEM;
9826         length = (sizeof(struct lpfc_mbx_mq_create) -
9827                   sizeof(struct lpfc_sli4_cfg_mhdr));
9828         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
9829                          LPFC_MBOX_OPCODE_MQ_CREATE,
9830                          length, LPFC_SLI4_MBX_EMBED);
9831         mq_create = &mbox->u.mqe.un.mq_create;
9832         bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
9833                     mq->page_count);
9834         bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
9835                     cq->queue_id);
9836         bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
9837         switch (mq->entry_count) {
9838         default:
9839                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9840                                 "0362 Unsupported MQ count. (%d)\n",
9841                                 mq->entry_count);
9842                 if (mq->entry_count < 16)
9843                         return -EINVAL;
9844                 /* otherwise default to smallest count (drop through) */
9845         case 16:
9846                 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
9847                        LPFC_MQ_CNT_16);
9848                 break;
9849         case 32:
9850                 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
9851                        LPFC_MQ_CNT_32);
9852                 break;
9853         case 64:
9854                 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
9855                        LPFC_MQ_CNT_64);
9856                 break;
9857         case 128:
9858                 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
9859                        LPFC_MQ_CNT_128);
9860                 break;
9861         }
9862         list_for_each_entry(dmabuf, &mq->page_list, list) {
9863                 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9864                                         putPaddrLow(dmabuf->phys);
9865                 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9866                                         putPaddrHigh(dmabuf->phys);
9867         }
9868         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9869         /* The IOCTL status is embedded in the mailbox subheader. */
9870         shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
9871         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9872         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9873         if (shdr_status || shdr_add_status || rc) {
9874                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9875                                 "2502 MQ_CREATE mailbox failed with "
9876                                 "status x%x add_status x%x, mbx status x%x\n",
9877                                 shdr_status, shdr_add_status, rc);
9878                 status = -ENXIO;
9879                 goto out;
9880         }
9881         mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id, &mq_create->u.response);
9882         if (mq->queue_id == 0xFFFF) {
9883                 status = -ENXIO;
9884                 goto out;
9885         }
9886         mq->type = LPFC_MQ;
9887         mq->subtype = subtype;
9888         mq->host_index = 0;
9889         mq->hba_index = 0;
9890
9891         /* link the mq onto the parent cq child list */
9892         list_add_tail(&mq->list, &cq->child_list);
9893 out:
9894         mempool_free(mbox, phba->mbox_mem_pool);
9895         return status;
9896 }
9897
9898 /**
9899  * lpfc_wq_create - Create a Work Queue on the HBA
9900  * @phba: HBA structure that indicates port to create a queue on.
9901  * @wq: The queue structure to use to create the work queue.
9902  * @cq: The completion queue to bind this work queue to.
9903  * @subtype: The subtype of the work queue indicating its functionality.
9904  *
9905  * This function creates a work queue, as detailed in @wq, on a port, described
9906  * by @phba by sending a WQ_CREATE mailbox command to the HBA.
9907  *
9908  * The @phba struct is used to send mailbox command to HBA. The @wq struct
9909  * is used to get the entry count and entry size that are necessary to
9910  * determine the number of pages to allocate and use for this queue. The @cq
9911  * is used to indicate which completion queue to bind this work queue to. This
9912  * function will send the WQ_CREATE mailbox command to the HBA to setup the
9913  * work queue. This function is asynchronous and will wait for the mailbox
9914  * command to finish before continuing.
9915  *
9916  * On success this function will return a zero. If unable to allocate enough
9917  * memory this function will return ENOMEM. If the queue create mailbox command
9918  * fails this function will return ENXIO.
9919  **/
9920 uint32_t
9921 lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
9922                struct lpfc_queue *cq, uint32_t subtype)
9923 {
9924         struct lpfc_mbx_wq_create *wq_create;
9925         struct lpfc_dmabuf *dmabuf;
9926         LPFC_MBOXQ_t *mbox;
9927         int rc, length, status = 0;
9928         uint32_t shdr_status, shdr_add_status;
9929         union lpfc_sli4_cfg_shdr *shdr;
9930
9931         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9932         if (!mbox)
9933                 return -ENOMEM;
9934         length = (sizeof(struct lpfc_mbx_wq_create) -
9935                   sizeof(struct lpfc_sli4_cfg_mhdr));
9936         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
9937                          LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
9938                          length, LPFC_SLI4_MBX_EMBED);
9939         wq_create = &mbox->u.mqe.un.wq_create;
9940         bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
9941                     wq->page_count);
9942         bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
9943                     cq->queue_id);
9944         list_for_each_entry(dmabuf, &wq->page_list, list) {
9945                 wq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9946                                         putPaddrLow(dmabuf->phys);
9947                 wq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9948                                         putPaddrHigh(dmabuf->phys);
9949         }
9950         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9951         /* The IOCTL status is embedded in the mailbox subheader. */
9952         shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
9953         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9954         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9955         if (shdr_status || shdr_add_status || rc) {
9956                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9957                                 "2503 WQ_CREATE mailbox failed with "
9958                                 "status x%x add_status x%x, mbx status x%x\n",
9959                                 shdr_status, shdr_add_status, rc);
9960                 status = -ENXIO;
9961                 goto out;
9962         }
9963         wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
9964         if (wq->queue_id == 0xFFFF) {
9965                 status = -ENXIO;
9966                 goto out;
9967         }
9968         wq->type = LPFC_WQ;
9969         wq->subtype = subtype;
9970         wq->host_index = 0;
9971         wq->hba_index = 0;
9972
9973         /* link the wq onto the parent cq child list */
9974         list_add_tail(&wq->list, &cq->child_list);
9975 out:
9976         mempool_free(mbox, phba->mbox_mem_pool);
9977         return status;
9978 }
9979
9980 /**
9981  * lpfc_rq_create - Create a Receive Queue on the HBA
9982  * @phba: HBA structure that indicates port to create a queue on.
9983  * @hrq: The queue structure to use to create the header receive queue.
9984  * @drq: The queue structure to use to create the data receive queue.
9985  * @cq: The completion queue to bind this work queue to.
9986  *
9987  * This function creates a receive buffer queue pair , as detailed in @hrq and
9988  * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
9989  * to the HBA.
9990  *
9991  * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
9992  * struct is used to get the entry count that is necessary to determine the
9993  * number of pages to use for this queue. The @cq is used to indicate which
9994  * completion queue to bind received buffers that are posted to these queues to.
9995  * This function will send the RQ_CREATE mailbox command to the HBA to setup the
9996  * receive queue pair. This function is asynchronous and will wait for the
9997  * mailbox command to finish before continuing.
9998  *
9999  * On success this function will return a zero. If unable to allocate enough
10000  * memory this function will return ENOMEM. If the queue create mailbox command
10001  * fails this function will return ENXIO.
10002  **/
10003 uint32_t
10004 lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
10005                struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
10006 {
10007         struct lpfc_mbx_rq_create *rq_create;
10008         struct lpfc_dmabuf *dmabuf;
10009         LPFC_MBOXQ_t *mbox;
10010         int rc, length, status = 0;
10011         uint32_t shdr_status, shdr_add_status;
10012         union lpfc_sli4_cfg_shdr *shdr;
10013
10014         if (hrq->entry_count != drq->entry_count)
10015                 return -EINVAL;
10016         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10017         if (!mbox)
10018                 return -ENOMEM;
10019         length = (sizeof(struct lpfc_mbx_rq_create) -
10020                   sizeof(struct lpfc_sli4_cfg_mhdr));
10021         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10022                          LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
10023                          length, LPFC_SLI4_MBX_EMBED);
10024         rq_create = &mbox->u.mqe.un.rq_create;
10025         switch (hrq->entry_count) {
10026         default:
10027                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10028                                 "2535 Unsupported RQ count. (%d)\n",
10029                                 hrq->entry_count);
10030                 if (hrq->entry_count < 512)
10031                         return -EINVAL;
10032                 /* otherwise default to smallest count (drop through) */
10033         case 512:
10034                 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10035                        LPFC_RQ_RING_SIZE_512);
10036                 break;
10037         case 1024:
10038                 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10039                        LPFC_RQ_RING_SIZE_1024);
10040                 break;
10041         case 2048:
10042                 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10043                        LPFC_RQ_RING_SIZE_2048);
10044                 break;
10045         case 4096:
10046                 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10047                        LPFC_RQ_RING_SIZE_4096);
10048                 break;
10049         }
10050         bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
10051                cq->queue_id);
10052         bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
10053                hrq->page_count);
10054         bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
10055                LPFC_HDR_BUF_SIZE);
10056         list_for_each_entry(dmabuf, &hrq->page_list, list) {
10057                 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
10058                                         putPaddrLow(dmabuf->phys);
10059                 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
10060                                         putPaddrHigh(dmabuf->phys);
10061         }
10062         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10063         /* The IOCTL status is embedded in the mailbox subheader. */
10064         shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
10065         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10066         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10067         if (shdr_status || shdr_add_status || rc) {
10068                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10069                                 "2504 RQ_CREATE mailbox failed with "
10070                                 "status x%x add_status x%x, mbx status x%x\n",
10071                                 shdr_status, shdr_add_status, rc);
10072                 status = -ENXIO;
10073                 goto out;
10074         }
10075         hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
10076         if (hrq->queue_id == 0xFFFF) {
10077                 status = -ENXIO;
10078                 goto out;
10079         }
10080         hrq->type = LPFC_HRQ;
10081         hrq->subtype = subtype;
10082         hrq->host_index = 0;
10083         hrq->hba_index = 0;
10084
10085         /* now create the data queue */
10086         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10087                          LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
10088                          length, LPFC_SLI4_MBX_EMBED);
10089         switch (drq->entry_count) {
10090         default:
10091                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10092                                 "2536 Unsupported RQ count. (%d)\n",
10093                                 drq->entry_count);
10094                 if (drq->entry_count < 512)
10095                         return -EINVAL;
10096                 /* otherwise default to smallest count (drop through) */
10097         case 512:
10098                 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10099                        LPFC_RQ_RING_SIZE_512);
10100                 break;
10101         case 1024:
10102                 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10103                        LPFC_RQ_RING_SIZE_1024);
10104                 break;
10105         case 2048:
10106                 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10107                        LPFC_RQ_RING_SIZE_2048);
10108                 break;
10109         case 4096:
10110                 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10111                        LPFC_RQ_RING_SIZE_4096);
10112                 break;
10113         }
10114         bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
10115                cq->queue_id);
10116         bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
10117                drq->page_count);
10118         bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
10119                LPFC_DATA_BUF_SIZE);
10120         list_for_each_entry(dmabuf, &drq->page_list, list) {
10121                 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
10122                                         putPaddrLow(dmabuf->phys);
10123                 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
10124                                         putPaddrHigh(dmabuf->phys);
10125         }
10126         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10127         /* The IOCTL status is embedded in the mailbox subheader. */
10128         shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
10129         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10130         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10131         if (shdr_status || shdr_add_status || rc) {
10132                 status = -ENXIO;
10133                 goto out;
10134         }
10135         drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
10136         if (drq->queue_id == 0xFFFF) {
10137                 status = -ENXIO;
10138                 goto out;
10139         }
10140         drq->type = LPFC_DRQ;
10141         drq->subtype = subtype;
10142         drq->host_index = 0;
10143         drq->hba_index = 0;
10144
10145         /* link the header and data RQs onto the parent cq child list */
10146         list_add_tail(&hrq->list, &cq->child_list);
10147         list_add_tail(&drq->list, &cq->child_list);
10148
10149 out:
10150         mempool_free(mbox, phba->mbox_mem_pool);
10151         return status;
10152 }
10153
10154 /**
10155  * lpfc_eq_destroy - Destroy an event Queue on the HBA
10156  * @eq: The queue structure associated with the queue to destroy.
10157  *
10158  * This function destroys a queue, as detailed in @eq by sending an mailbox
10159  * command, specific to the type of queue, to the HBA.
10160  *
10161  * The @eq struct is used to get the queue ID of the queue to destroy.
10162  *
10163  * On success this function will return a zero. If the queue destroy mailbox
10164  * command fails this function will return ENXIO.
10165  **/
10166 uint32_t
10167 lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
10168 {
10169         LPFC_MBOXQ_t *mbox;
10170         int rc, length, status = 0;
10171         uint32_t shdr_status, shdr_add_status;
10172         union lpfc_sli4_cfg_shdr *shdr;
10173
10174         if (!eq)
10175                 return -ENODEV;
10176         mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
10177         if (!mbox)
10178                 return -ENOMEM;
10179         length = (sizeof(struct lpfc_mbx_eq_destroy) -
10180                   sizeof(struct lpfc_sli4_cfg_mhdr));
10181         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10182                          LPFC_MBOX_OPCODE_EQ_DESTROY,
10183                          length, LPFC_SLI4_MBX_EMBED);
10184         bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
10185                eq->queue_id);
10186         mbox->vport = eq->phba->pport;
10187         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10188
10189         rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
10190         /* The IOCTL status is embedded in the mailbox subheader. */
10191         shdr = (union lpfc_sli4_cfg_shdr *)
10192                 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
10193         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10194         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10195         if (shdr_status || shdr_add_status || rc) {
10196                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10197                                 "2505 EQ_DESTROY mailbox failed with "
10198                                 "status x%x add_status x%x, mbx status x%x\n",
10199                                 shdr_status, shdr_add_status, rc);
10200                 status = -ENXIO;
10201         }
10202
10203         /* Remove eq from any list */
10204         list_del_init(&eq->list);
10205         mempool_free(mbox, eq->phba->mbox_mem_pool);
10206         return status;
10207 }
10208
10209 /**
10210  * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
10211  * @cq: The queue structure associated with the queue to destroy.
10212  *
10213  * This function destroys a queue, as detailed in @cq by sending an mailbox
10214  * command, specific to the type of queue, to the HBA.
10215  *
10216  * The @cq struct is used to get the queue ID of the queue to destroy.
10217  *
10218  * On success this function will return a zero. If the queue destroy mailbox
10219  * command fails this function will return ENXIO.
10220  **/
10221 uint32_t
10222 lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
10223 {
10224         LPFC_MBOXQ_t *mbox;
10225         int rc, length, status = 0;
10226         uint32_t shdr_status, shdr_add_status;
10227         union lpfc_sli4_cfg_shdr *shdr;
10228
10229         if (!cq)
10230                 return -ENODEV;
10231         mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
10232         if (!mbox)
10233                 return -ENOMEM;
10234         length = (sizeof(struct lpfc_mbx_cq_destroy) -
10235                   sizeof(struct lpfc_sli4_cfg_mhdr));
10236         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10237                          LPFC_MBOX_OPCODE_CQ_DESTROY,
10238                          length, LPFC_SLI4_MBX_EMBED);
10239         bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
10240                cq->queue_id);
10241         mbox->vport = cq->phba->pport;
10242         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10243         rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
10244         /* The IOCTL status is embedded in the mailbox subheader. */
10245         shdr = (union lpfc_sli4_cfg_shdr *)
10246                 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
10247         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10248         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10249         if (shdr_status || shdr_add_status || rc) {
10250                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10251                                 "2506 CQ_DESTROY mailbox failed with "
10252                                 "status x%x add_status x%x, mbx status x%x\n",
10253                                 shdr_status, shdr_add_status, rc);
10254                 status = -ENXIO;
10255         }
10256         /* Remove cq from any list */
10257         list_del_init(&cq->list);
10258         mempool_free(mbox, cq->phba->mbox_mem_pool);
10259         return status;
10260 }
10261
10262 /**
10263  * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
10264  * @qm: The queue structure associated with the queue to destroy.
10265  *
10266  * This function destroys a queue, as detailed in @mq by sending an mailbox
10267  * command, specific to the type of queue, to the HBA.
10268  *
10269  * The @mq struct is used to get the queue ID of the queue to destroy.
10270  *
10271  * On success this function will return a zero. If the queue destroy mailbox
10272  * command fails this function will return ENXIO.
10273  **/
10274 uint32_t
10275 lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
10276 {
10277         LPFC_MBOXQ_t *mbox;
10278         int rc, length, status = 0;
10279         uint32_t shdr_status, shdr_add_status;
10280         union lpfc_sli4_cfg_shdr *shdr;
10281
10282         if (!mq)
10283                 return -ENODEV;
10284         mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
10285         if (!mbox)
10286                 return -ENOMEM;
10287         length = (sizeof(struct lpfc_mbx_mq_destroy) -
10288                   sizeof(struct lpfc_sli4_cfg_mhdr));
10289         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10290                          LPFC_MBOX_OPCODE_MQ_DESTROY,
10291                          length, LPFC_SLI4_MBX_EMBED);
10292         bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
10293                mq->queue_id);
10294         mbox->vport = mq->phba->pport;
10295         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10296         rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
10297         /* The IOCTL status is embedded in the mailbox subheader. */
10298         shdr = (union lpfc_sli4_cfg_shdr *)
10299                 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
10300         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10301         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10302         if (shdr_status || shdr_add_status || rc) {
10303                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10304                                 "2507 MQ_DESTROY mailbox failed with "
10305                                 "status x%x add_status x%x, mbx status x%x\n",
10306                                 shdr_status, shdr_add_status, rc);
10307                 status = -ENXIO;
10308         }
10309         /* Remove mq from any list */
10310         list_del_init(&mq->list);
10311         mempool_free(mbox, mq->phba->mbox_mem_pool);
10312         return status;
10313 }
10314
10315 /**
10316  * lpfc_wq_destroy - Destroy a Work Queue on the HBA
10317  * @wq: The queue structure associated with the queue to destroy.
10318  *
10319  * This function destroys a queue, as detailed in @wq by sending an mailbox
10320  * command, specific to the type of queue, to the HBA.
10321  *
10322  * The @wq struct is used to get the queue ID of the queue to destroy.
10323  *
10324  * On success this function will return a zero. If the queue destroy mailbox
10325  * command fails this function will return ENXIO.
10326  **/
10327 uint32_t
10328 lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
10329 {
10330         LPFC_MBOXQ_t *mbox;
10331         int rc, length, status = 0;
10332         uint32_t shdr_status, shdr_add_status;
10333         union lpfc_sli4_cfg_shdr *shdr;
10334
10335         if (!wq)
10336                 return -ENODEV;
10337         mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
10338         if (!mbox)
10339                 return -ENOMEM;
10340         length = (sizeof(struct lpfc_mbx_wq_destroy) -
10341                   sizeof(struct lpfc_sli4_cfg_mhdr));
10342         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10343                          LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
10344                          length, LPFC_SLI4_MBX_EMBED);
10345         bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
10346                wq->queue_id);
10347         mbox->vport = wq->phba->pport;
10348         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10349         rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
10350         shdr = (union lpfc_sli4_cfg_shdr *)
10351                 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
10352         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10353         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10354         if (shdr_status || shdr_add_status || rc) {
10355                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10356                                 "2508 WQ_DESTROY mailbox failed with "
10357                                 "status x%x add_status x%x, mbx status x%x\n",
10358                                 shdr_status, shdr_add_status, rc);
10359                 status = -ENXIO;
10360         }
10361         /* Remove wq from any list */
10362         list_del_init(&wq->list);
10363         mempool_free(mbox, wq->phba->mbox_mem_pool);
10364         return status;
10365 }
10366
10367 /**
10368  * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
10369  * @rq: The queue structure associated with the queue to destroy.
10370  *
10371  * This function destroys a queue, as detailed in @rq by sending an mailbox
10372  * command, specific to the type of queue, to the HBA.
10373  *
10374  * The @rq struct is used to get the queue ID of the queue to destroy.
10375  *
10376  * On success this function will return a zero. If the queue destroy mailbox
10377  * command fails this function will return ENXIO.
10378  **/
10379 uint32_t
10380 lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
10381                 struct lpfc_queue *drq)
10382 {
10383         LPFC_MBOXQ_t *mbox;
10384         int rc, length, status = 0;
10385         uint32_t shdr_status, shdr_add_status;
10386         union lpfc_sli4_cfg_shdr *shdr;
10387
10388         if (!hrq || !drq)
10389                 return -ENODEV;
10390         mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
10391         if (!mbox)
10392                 return -ENOMEM;
10393         length = (sizeof(struct lpfc_mbx_rq_destroy) -
10394                   sizeof(struct mbox_header));
10395         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10396                          LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
10397                          length, LPFC_SLI4_MBX_EMBED);
10398         bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
10399                hrq->queue_id);
10400         mbox->vport = hrq->phba->pport;
10401         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10402         rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
10403         /* The IOCTL status is embedded in the mailbox subheader. */
10404         shdr = (union lpfc_sli4_cfg_shdr *)
10405                 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
10406         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10407         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10408         if (shdr_status || shdr_add_status || rc) {
10409                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10410                                 "2509 RQ_DESTROY mailbox failed with "
10411                                 "status x%x add_status x%x, mbx status x%x\n",
10412                                 shdr_status, shdr_add_status, rc);
10413                 if (rc != MBX_TIMEOUT)
10414                         mempool_free(mbox, hrq->phba->mbox_mem_pool);
10415                 return -ENXIO;
10416         }
10417         bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
10418                drq->queue_id);
10419         rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
10420         shdr = (union lpfc_sli4_cfg_shdr *)
10421                 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
10422         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10423         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10424         if (shdr_status || shdr_add_status || rc) {
10425                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10426                                 "2510 RQ_DESTROY mailbox failed with "
10427                                 "status x%x add_status x%x, mbx status x%x\n",
10428                                 shdr_status, shdr_add_status, rc);
10429                 status = -ENXIO;
10430         }
10431         list_del_init(&hrq->list);
10432         list_del_init(&drq->list);
10433         mempool_free(mbox, hrq->phba->mbox_mem_pool);
10434         return status;
10435 }
10436
10437 /**
10438  * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
10439  * @phba: The virtual port for which this call being executed.
10440  * @pdma_phys_addr0: Physical address of the 1st SGL page.
10441  * @pdma_phys_addr1: Physical address of the 2nd SGL page.
10442  * @xritag: the xritag that ties this io to the SGL pages.
10443  *
10444  * This routine will post the sgl pages for the IO that has the xritag
10445  * that is in the iocbq structure. The xritag is assigned during iocbq
10446  * creation and persists for as long as the driver is loaded.
10447  * if the caller has fewer than 256 scatter gather segments to map then
10448  * pdma_phys_addr1 should be 0.
10449  * If the caller needs to map more than 256 scatter gather segment then
10450  * pdma_phys_addr1 should be a valid physical address.
10451  * physical address for SGLs must be 64 byte aligned.
10452  * If you are going to map 2 SGL's then the first one must have 256 entries
10453  * the second sgl can have between 1 and 256 entries.
10454  *
10455  * Return codes:
10456  *      0 - Success
10457  *      -ENXIO, -ENOMEM - Failure
10458  **/
10459 int
10460 lpfc_sli4_post_sgl(struct lpfc_hba *phba,
10461                 dma_addr_t pdma_phys_addr0,
10462                 dma_addr_t pdma_phys_addr1,
10463                 uint16_t xritag)
10464 {
10465         struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
10466         LPFC_MBOXQ_t *mbox;
10467         int rc;
10468         uint32_t shdr_status, shdr_add_status;
10469         union lpfc_sli4_cfg_shdr *shdr;
10470
10471         if (xritag == NO_XRI) {
10472                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10473                                 "0364 Invalid param:\n");
10474                 return -EINVAL;
10475         }
10476
10477         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10478         if (!mbox)
10479                 return -ENOMEM;
10480
10481         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10482                         LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
10483                         sizeof(struct lpfc_mbx_post_sgl_pages) -
10484                         sizeof(struct mbox_header), LPFC_SLI4_MBX_EMBED);
10485
10486         post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
10487                                 &mbox->u.mqe.un.post_sgl_pages;
10488         bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
10489         bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
10490
10491         post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
10492                                 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
10493         post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
10494                                 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
10495
10496         post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
10497                                 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
10498         post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
10499                                 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
10500         if (!phba->sli4_hba.intr_enable)
10501                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10502         else
10503                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
10504         /* The IOCTL status is embedded in the mailbox subheader. */
10505         shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
10506         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10507         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10508         if (rc != MBX_TIMEOUT)
10509                 mempool_free(mbox, phba->mbox_mem_pool);
10510         if (shdr_status || shdr_add_status || rc) {
10511                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10512                                 "2511 POST_SGL mailbox failed with "
10513                                 "status x%x add_status x%x, mbx status x%x\n",
10514                                 shdr_status, shdr_add_status, rc);
10515                 rc = -ENXIO;
10516         }
10517         return 0;
10518 }
10519 /**
10520  * lpfc_sli4_remove_all_sgl_pages - Post scatter gather list for an XRI to HBA
10521  * @phba: The virtual port for which this call being executed.
10522  *
10523  * This routine will remove all of the sgl pages registered with the hba.
10524  *
10525  * Return codes:
10526  *      0 - Success
10527  *      -ENXIO, -ENOMEM - Failure
10528  **/
10529 int
10530 lpfc_sli4_remove_all_sgl_pages(struct lpfc_hba *phba)
10531 {
10532         LPFC_MBOXQ_t *mbox;
10533         int rc;
10534         uint32_t shdr_status, shdr_add_status;
10535         union lpfc_sli4_cfg_shdr *shdr;
10536
10537         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10538         if (!mbox)
10539                 return -ENOMEM;
10540
10541         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10542                         LPFC_MBOX_OPCODE_FCOE_REMOVE_SGL_PAGES, 0,
10543                         LPFC_SLI4_MBX_EMBED);
10544         if (!phba->sli4_hba.intr_enable)
10545                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10546         else
10547                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
10548         /* The IOCTL status is embedded in the mailbox subheader. */
10549         shdr = (union lpfc_sli4_cfg_shdr *)
10550                 &mbox->u.mqe.un.sli4_config.header.cfg_shdr;
10551         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10552         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10553         if (rc != MBX_TIMEOUT)
10554                 mempool_free(mbox, phba->mbox_mem_pool);
10555         if (shdr_status || shdr_add_status || rc) {
10556                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10557                                 "2512 REMOVE_ALL_SGL_PAGES mailbox failed with "
10558                                 "status x%x add_status x%x, mbx status x%x\n",
10559                                 shdr_status, shdr_add_status, rc);
10560                 rc = -ENXIO;
10561         }
10562         return rc;
10563 }
10564
10565 /**
10566  * lpfc_sli4_next_xritag - Get an xritag for the io
10567  * @phba: Pointer to HBA context object.
10568  *
10569  * This function gets an xritag for the iocb. If there is no unused xritag
10570  * it will return 0xffff.
10571  * The function returns the allocated xritag if successful, else returns zero.
10572  * Zero is not a valid xritag.
10573  * The caller is not required to hold any lock.
10574  **/
10575 uint16_t
10576 lpfc_sli4_next_xritag(struct lpfc_hba *phba)
10577 {
10578         uint16_t xritag;
10579
10580         spin_lock_irq(&phba->hbalock);
10581         xritag = phba->sli4_hba.next_xri;
10582         if ((xritag != (uint16_t) -1) && xritag <
10583                 (phba->sli4_hba.max_cfg_param.max_xri
10584                         + phba->sli4_hba.max_cfg_param.xri_base)) {
10585                 phba->sli4_hba.next_xri++;
10586                 phba->sli4_hba.max_cfg_param.xri_used++;
10587                 spin_unlock_irq(&phba->hbalock);
10588                 return xritag;
10589         }
10590         spin_unlock_irq(&phba->hbalock);
10591         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
10592                         "2004 Failed to allocate XRI.last XRITAG is %d"
10593                         " Max XRI is %d, Used XRI is %d\n",
10594                         phba->sli4_hba.next_xri,
10595                         phba->sli4_hba.max_cfg_param.max_xri,
10596                         phba->sli4_hba.max_cfg_param.xri_used);
10597         return -1;
10598 }
10599
10600 /**
10601  * lpfc_sli4_post_sgl_list - post a block of sgl list to the firmware.
10602  * @phba: pointer to lpfc hba data structure.
10603  *
10604  * This routine is invoked to post a block of driver's sgl pages to the
10605  * HBA using non-embedded mailbox command. No Lock is held. This routine
10606  * is only called when the driver is loading and after all IO has been
10607  * stopped.
10608  **/
10609 int
10610 lpfc_sli4_post_sgl_list(struct lpfc_hba *phba)
10611 {
10612         struct lpfc_sglq *sglq_entry;
10613         struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
10614         struct sgl_page_pairs *sgl_pg_pairs;
10615         void *viraddr;
10616         LPFC_MBOXQ_t *mbox;
10617         uint32_t reqlen, alloclen, pg_pairs;
10618         uint32_t mbox_tmo;
10619         uint16_t xritag_start = 0;
10620         int els_xri_cnt, rc = 0;
10621         uint32_t shdr_status, shdr_add_status;
10622         union lpfc_sli4_cfg_shdr *shdr;
10623
10624         /* The number of sgls to be posted */
10625         els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
10626
10627         reqlen = els_xri_cnt * sizeof(struct sgl_page_pairs) +
10628                  sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
10629         if (reqlen > PAGE_SIZE) {
10630                 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
10631                                 "2559 Block sgl registration required DMA "
10632                                 "size (%d) great than a page\n", reqlen);
10633                 return -ENOMEM;
10634         }
10635         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10636         if (!mbox) {
10637                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10638                                 "2560 Failed to allocate mbox cmd memory\n");
10639                 return -ENOMEM;
10640         }
10641
10642         /* Allocate DMA memory and set up the non-embedded mailbox command */
10643         alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10644                          LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
10645                          LPFC_SLI4_MBX_NEMBED);
10646
10647         if (alloclen < reqlen) {
10648                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10649                                 "0285 Allocated DMA memory size (%d) is "
10650                                 "less than the requested DMA memory "
10651                                 "size (%d)\n", alloclen, reqlen);
10652                 lpfc_sli4_mbox_cmd_free(phba, mbox);
10653                 return -ENOMEM;
10654         }
10655         /* Get the first SGE entry from the non-embedded DMA memory */
10656         viraddr = mbox->sge_array->addr[0];
10657
10658         /* Set up the SGL pages in the non-embedded DMA pages */
10659         sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
10660         sgl_pg_pairs = &sgl->sgl_pg_pairs;
10661
10662         for (pg_pairs = 0; pg_pairs < els_xri_cnt; pg_pairs++) {
10663                 sglq_entry = phba->sli4_hba.lpfc_els_sgl_array[pg_pairs];
10664                 /* Set up the sge entry */
10665                 sgl_pg_pairs->sgl_pg0_addr_lo =
10666                                 cpu_to_le32(putPaddrLow(sglq_entry->phys));
10667                 sgl_pg_pairs->sgl_pg0_addr_hi =
10668                                 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
10669                 sgl_pg_pairs->sgl_pg1_addr_lo =
10670                                 cpu_to_le32(putPaddrLow(0));
10671                 sgl_pg_pairs->sgl_pg1_addr_hi =
10672                                 cpu_to_le32(putPaddrHigh(0));
10673                 /* Keep the first xritag on the list */
10674                 if (pg_pairs == 0)
10675                         xritag_start = sglq_entry->sli4_xritag;
10676                 sgl_pg_pairs++;
10677         }
10678         bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
10679         bf_set(lpfc_post_sgl_pages_xricnt, sgl, els_xri_cnt);
10680         /* Perform endian conversion if necessary */
10681         sgl->word0 = cpu_to_le32(sgl->word0);
10682
10683         if (!phba->sli4_hba.intr_enable)
10684                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10685         else {
10686                 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
10687                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
10688         }
10689         shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
10690         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10691         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10692         if (rc != MBX_TIMEOUT)
10693                 lpfc_sli4_mbox_cmd_free(phba, mbox);
10694         if (shdr_status || shdr_add_status || rc) {
10695                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10696                                 "2513 POST_SGL_BLOCK mailbox command failed "
10697                                 "status x%x add_status x%x mbx status x%x\n",
10698                                 shdr_status, shdr_add_status, rc);
10699                 rc = -ENXIO;
10700         }
10701         return rc;
10702 }
10703
10704 /**
10705  * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
10706  * @phba: pointer to lpfc hba data structure.
10707  * @sblist: pointer to scsi buffer list.
10708  * @count: number of scsi buffers on the list.
10709  *
10710  * This routine is invoked to post a block of @count scsi sgl pages from a
10711  * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
10712  * No Lock is held.
10713  *
10714  **/
10715 int
10716 lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba, struct list_head *sblist,
10717                               int cnt)
10718 {
10719         struct lpfc_scsi_buf *psb;
10720         struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
10721         struct sgl_page_pairs *sgl_pg_pairs;
10722         void *viraddr;
10723         LPFC_MBOXQ_t *mbox;
10724         uint32_t reqlen, alloclen, pg_pairs;
10725         uint32_t mbox_tmo;
10726         uint16_t xritag_start = 0;
10727         int rc = 0;
10728         uint32_t shdr_status, shdr_add_status;
10729         dma_addr_t pdma_phys_bpl1;
10730         union lpfc_sli4_cfg_shdr *shdr;
10731
10732         /* Calculate the requested length of the dma memory */
10733         reqlen = cnt * sizeof(struct sgl_page_pairs) +
10734                  sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
10735         if (reqlen > PAGE_SIZE) {
10736                 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
10737                                 "0217 Block sgl registration required DMA "
10738                                 "size (%d) great than a page\n", reqlen);
10739                 return -ENOMEM;
10740         }
10741         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10742         if (!mbox) {
10743                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10744                                 "0283 Failed to allocate mbox cmd memory\n");
10745                 return -ENOMEM;
10746         }
10747
10748         /* Allocate DMA memory and set up the non-embedded mailbox command */
10749         alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10750                                 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
10751                                 LPFC_SLI4_MBX_NEMBED);
10752
10753         if (alloclen < reqlen) {
10754                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10755                                 "2561 Allocated DMA memory size (%d) is "
10756                                 "less than the requested DMA memory "
10757                                 "size (%d)\n", alloclen, reqlen);
10758                 lpfc_sli4_mbox_cmd_free(phba, mbox);
10759                 return -ENOMEM;
10760         }
10761         /* Get the first SGE entry from the non-embedded DMA memory */
10762         viraddr = mbox->sge_array->addr[0];
10763
10764         /* Set up the SGL pages in the non-embedded DMA pages */
10765         sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
10766         sgl_pg_pairs = &sgl->sgl_pg_pairs;
10767
10768         pg_pairs = 0;
10769         list_for_each_entry(psb, sblist, list) {
10770                 /* Set up the sge entry */
10771                 sgl_pg_pairs->sgl_pg0_addr_lo =
10772                         cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
10773                 sgl_pg_pairs->sgl_pg0_addr_hi =
10774                         cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
10775                 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
10776                         pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
10777                 else
10778                         pdma_phys_bpl1 = 0;
10779                 sgl_pg_pairs->sgl_pg1_addr_lo =
10780                         cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
10781                 sgl_pg_pairs->sgl_pg1_addr_hi =
10782                         cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
10783                 /* Keep the first xritag on the list */
10784                 if (pg_pairs == 0)
10785                         xritag_start = psb->cur_iocbq.sli4_xritag;
10786                 sgl_pg_pairs++;
10787                 pg_pairs++;
10788         }
10789         bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
10790         bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
10791         /* Perform endian conversion if necessary */
10792         sgl->word0 = cpu_to_le32(sgl->word0);
10793
10794         if (!phba->sli4_hba.intr_enable)
10795                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10796         else {
10797                 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
10798                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
10799         }
10800         shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
10801         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10802         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10803         if (rc != MBX_TIMEOUT)
10804                 lpfc_sli4_mbox_cmd_free(phba, mbox);
10805         if (shdr_status || shdr_add_status || rc) {
10806                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10807                                 "2564 POST_SGL_BLOCK mailbox command failed "
10808                                 "status x%x add_status x%x mbx status x%x\n",
10809                                 shdr_status, shdr_add_status, rc);
10810                 rc = -ENXIO;
10811         }
10812         return rc;
10813 }
10814
10815 /**
10816  * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
10817  * @phba: pointer to lpfc_hba struct that the frame was received on
10818  * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
10819  *
10820  * This function checks the fields in the @fc_hdr to see if the FC frame is a
10821  * valid type of frame that the LPFC driver will handle. This function will
10822  * return a zero if the frame is a valid frame or a non zero value when the
10823  * frame does not pass the check.
10824  **/
10825 static int
10826 lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
10827 {
10828         char *rctl_names[] = FC_RCTL_NAMES_INIT;
10829         char *type_names[] = FC_TYPE_NAMES_INIT;
10830         struct fc_vft_header *fc_vft_hdr;
10831
10832         switch (fc_hdr->fh_r_ctl) {
10833         case FC_RCTL_DD_UNCAT:          /* uncategorized information */
10834         case FC_RCTL_DD_SOL_DATA:       /* solicited data */
10835         case FC_RCTL_DD_UNSOL_CTL:      /* unsolicited control */
10836         case FC_RCTL_DD_SOL_CTL:        /* solicited control or reply */
10837         case FC_RCTL_DD_UNSOL_DATA:     /* unsolicited data */
10838         case FC_RCTL_DD_DATA_DESC:      /* data descriptor */
10839         case FC_RCTL_DD_UNSOL_CMD:      /* unsolicited command */
10840         case FC_RCTL_DD_CMD_STATUS:     /* command status */
10841         case FC_RCTL_ELS_REQ:   /* extended link services request */
10842         case FC_RCTL_ELS_REP:   /* extended link services reply */
10843         case FC_RCTL_ELS4_REQ:  /* FC-4 ELS request */
10844         case FC_RCTL_ELS4_REP:  /* FC-4 ELS reply */
10845         case FC_RCTL_BA_NOP:    /* basic link service NOP */
10846         case FC_RCTL_BA_ABTS:   /* basic link service abort */
10847         case FC_RCTL_BA_RMC:    /* remove connection */
10848         case FC_RCTL_BA_ACC:    /* basic accept */
10849         case FC_RCTL_BA_RJT:    /* basic reject */
10850         case FC_RCTL_BA_PRMT:
10851         case FC_RCTL_ACK_1:     /* acknowledge_1 */
10852         case FC_RCTL_ACK_0:     /* acknowledge_0 */
10853         case FC_RCTL_P_RJT:     /* port reject */
10854         case FC_RCTL_F_RJT:     /* fabric reject */
10855         case FC_RCTL_P_BSY:     /* port busy */
10856         case FC_RCTL_F_BSY:     /* fabric busy to data frame */
10857         case FC_RCTL_F_BSYL:    /* fabric busy to link control frame */
10858         case FC_RCTL_LCR:       /* link credit reset */
10859         case FC_RCTL_END:       /* end */
10860                 break;
10861         case FC_RCTL_VFTH:      /* Virtual Fabric tagging Header */
10862                 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
10863                 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
10864                 return lpfc_fc_frame_check(phba, fc_hdr);
10865         default:
10866                 goto drop;
10867         }
10868         switch (fc_hdr->fh_type) {
10869         case FC_TYPE_BLS:
10870         case FC_TYPE_ELS:
10871         case FC_TYPE_FCP:
10872         case FC_TYPE_CT:
10873                 break;
10874         case FC_TYPE_IP:
10875         case FC_TYPE_ILS:
10876         default:
10877                 goto drop;
10878         }
10879         lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
10880                         "2538 Received frame rctl:%s type:%s\n",
10881                         rctl_names[fc_hdr->fh_r_ctl],
10882                         type_names[fc_hdr->fh_type]);
10883         return 0;
10884 drop:
10885         lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
10886                         "2539 Dropped frame rctl:%s type:%s\n",
10887                         rctl_names[fc_hdr->fh_r_ctl],
10888                         type_names[fc_hdr->fh_type]);
10889         return 1;
10890 }
10891
10892 /**
10893  * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
10894  * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
10895  *
10896  * This function processes the FC header to retrieve the VFI from the VF
10897  * header, if one exists. This function will return the VFI if one exists
10898  * or 0 if no VSAN Header exists.
10899  **/
10900 static uint32_t
10901 lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
10902 {
10903         struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
10904
10905         if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
10906                 return 0;
10907         return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
10908 }
10909
10910 /**
10911  * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
10912  * @phba: Pointer to the HBA structure to search for the vport on
10913  * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
10914  * @fcfi: The FC Fabric ID that the frame came from
10915  *
10916  * This function searches the @phba for a vport that matches the content of the
10917  * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
10918  * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
10919  * returns the matching vport pointer or NULL if unable to match frame to a
10920  * vport.
10921  **/
10922 static struct lpfc_vport *
10923 lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
10924                        uint16_t fcfi)
10925 {
10926         struct lpfc_vport **vports;
10927         struct lpfc_vport *vport = NULL;
10928         int i;
10929         uint32_t did = (fc_hdr->fh_d_id[0] << 16 |
10930                         fc_hdr->fh_d_id[1] << 8 |
10931                         fc_hdr->fh_d_id[2]);
10932
10933         vports = lpfc_create_vport_work_array(phba);
10934         if (vports != NULL)
10935                 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
10936                         if (phba->fcf.fcfi == fcfi &&
10937                             vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
10938                             vports[i]->fc_myDID == did) {
10939                                 vport = vports[i];
10940                                 break;
10941                         }
10942                 }
10943         lpfc_destroy_vport_work_array(phba, vports);
10944         return vport;
10945 }
10946
10947 /**
10948  * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
10949  * @vport: The vport to work on.
10950  *
10951  * This function updates the receive sequence time stamp for this vport. The
10952  * receive sequence time stamp indicates the time that the last frame of the
10953  * the sequence that has been idle for the longest amount of time was received.
10954  * the driver uses this time stamp to indicate if any received sequences have
10955  * timed out.
10956  **/
10957 void
10958 lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
10959 {
10960         struct lpfc_dmabuf *h_buf;
10961         struct hbq_dmabuf *dmabuf = NULL;
10962
10963         /* get the oldest sequence on the rcv list */
10964         h_buf = list_get_first(&vport->rcv_buffer_list,
10965                                struct lpfc_dmabuf, list);
10966         if (!h_buf)
10967                 return;
10968         dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
10969         vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
10970 }
10971
10972 /**
10973  * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
10974  * @vport: The vport that the received sequences were sent to.
10975  *
10976  * This function cleans up all outstanding received sequences. This is called
10977  * by the driver when a link event or user action invalidates all the received
10978  * sequences.
10979  **/
10980 void
10981 lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
10982 {
10983         struct lpfc_dmabuf *h_buf, *hnext;
10984         struct lpfc_dmabuf *d_buf, *dnext;
10985         struct hbq_dmabuf *dmabuf = NULL;
10986
10987         /* start with the oldest sequence on the rcv list */
10988         list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
10989                 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
10990                 list_del_init(&dmabuf->hbuf.list);
10991                 list_for_each_entry_safe(d_buf, dnext,
10992                                          &dmabuf->dbuf.list, list) {
10993                         list_del_init(&d_buf->list);
10994                         lpfc_in_buf_free(vport->phba, d_buf);
10995                 }
10996                 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
10997         }
10998 }
10999
11000 /**
11001  * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
11002  * @vport: The vport that the received sequences were sent to.
11003  *
11004  * This function determines whether any received sequences have timed out by
11005  * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
11006  * indicates that there is at least one timed out sequence this routine will
11007  * go through the received sequences one at a time from most inactive to most
11008  * active to determine which ones need to be cleaned up. Once it has determined
11009  * that a sequence needs to be cleaned up it will simply free up the resources
11010  * without sending an abort.
11011  **/
11012 void
11013 lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
11014 {
11015         struct lpfc_dmabuf *h_buf, *hnext;
11016         struct lpfc_dmabuf *d_buf, *dnext;
11017         struct hbq_dmabuf *dmabuf = NULL;
11018         unsigned long timeout;
11019         int abort_count = 0;
11020
11021         timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
11022                    vport->rcv_buffer_time_stamp);
11023         if (list_empty(&vport->rcv_buffer_list) ||
11024             time_before(jiffies, timeout))
11025                 return;
11026         /* start with the oldest sequence on the rcv list */
11027         list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
11028                 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11029                 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
11030                            dmabuf->time_stamp);
11031                 if (time_before(jiffies, timeout))
11032                         break;
11033                 abort_count++;
11034                 list_del_init(&dmabuf->hbuf.list);
11035                 list_for_each_entry_safe(d_buf, dnext,
11036                                          &dmabuf->dbuf.list, list) {
11037                         list_del_init(&d_buf->list);
11038                         lpfc_in_buf_free(vport->phba, d_buf);
11039                 }
11040                 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
11041         }
11042         if (abort_count)
11043                 lpfc_update_rcv_time_stamp(vport);
11044 }
11045
11046 /**
11047  * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
11048  * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
11049  *
11050  * This function searches through the existing incomplete sequences that have
11051  * been sent to this @vport. If the frame matches one of the incomplete
11052  * sequences then the dbuf in the @dmabuf is added to the list of frames that
11053  * make up that sequence. If no sequence is found that matches this frame then
11054  * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
11055  * This function returns a pointer to the first dmabuf in the sequence list that
11056  * the frame was linked to.
11057  **/
11058 static struct hbq_dmabuf *
11059 lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
11060 {
11061         struct fc_frame_header *new_hdr;
11062         struct fc_frame_header *temp_hdr;
11063         struct lpfc_dmabuf *d_buf;
11064         struct lpfc_dmabuf *h_buf;
11065         struct hbq_dmabuf *seq_dmabuf = NULL;
11066         struct hbq_dmabuf *temp_dmabuf = NULL;
11067
11068         INIT_LIST_HEAD(&dmabuf->dbuf.list);
11069         dmabuf->time_stamp = jiffies;
11070         new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11071         /* Use the hdr_buf to find the sequence that this frame belongs to */
11072         list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
11073                 temp_hdr = (struct fc_frame_header *)h_buf->virt;
11074                 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
11075                     (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
11076                     (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
11077                         continue;
11078                 /* found a pending sequence that matches this frame */
11079                 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11080                 break;
11081         }
11082         if (!seq_dmabuf) {
11083                 /*
11084                  * This indicates first frame received for this sequence.
11085                  * Queue the buffer on the vport's rcv_buffer_list.
11086                  */
11087                 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
11088                 lpfc_update_rcv_time_stamp(vport);
11089                 return dmabuf;
11090         }
11091         temp_hdr = seq_dmabuf->hbuf.virt;
11092         if (be16_to_cpu(new_hdr->fh_seq_cnt) <
11093                 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
11094                 list_del_init(&seq_dmabuf->hbuf.list);
11095                 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
11096                 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
11097                 lpfc_update_rcv_time_stamp(vport);
11098                 return dmabuf;
11099         }
11100         /* move this sequence to the tail to indicate a young sequence */
11101         list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
11102         seq_dmabuf->time_stamp = jiffies;
11103         lpfc_update_rcv_time_stamp(vport);
11104         if (list_empty(&seq_dmabuf->dbuf.list)) {
11105                 temp_hdr = dmabuf->hbuf.virt;
11106                 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
11107                 return seq_dmabuf;
11108         }
11109         /* find the correct place in the sequence to insert this frame */
11110         list_for_each_entry_reverse(d_buf, &seq_dmabuf->dbuf.list, list) {
11111                 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
11112                 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
11113                 /*
11114                  * If the frame's sequence count is greater than the frame on
11115                  * the list then insert the frame right after this frame
11116                  */
11117                 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
11118                         be16_to_cpu(temp_hdr->fh_seq_cnt)) {
11119                         list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
11120                         return seq_dmabuf;
11121                 }
11122         }
11123         return NULL;
11124 }
11125
11126 /**
11127  * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
11128  * @vport: pointer to a vitural port
11129  * @dmabuf: pointer to a dmabuf that describes the FC sequence
11130  *
11131  * This function tries to abort from the partially assembed sequence, described
11132  * by the information from basic abbort @dmabuf. It checks to see whether such
11133  * partially assembled sequence held by the driver. If so, it shall free up all
11134  * the frames from the partially assembled sequence.
11135  *
11136  * Return
11137  * true  -- if there is matching partially assembled sequence present and all
11138  *          the frames freed with the sequence;
11139  * false -- if there is no matching partially assembled sequence present so
11140  *          nothing got aborted in the lower layer driver
11141  **/
11142 static bool
11143 lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
11144                             struct hbq_dmabuf *dmabuf)
11145 {
11146         struct fc_frame_header *new_hdr;
11147         struct fc_frame_header *temp_hdr;
11148         struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
11149         struct hbq_dmabuf *seq_dmabuf = NULL;
11150
11151         /* Use the hdr_buf to find the sequence that matches this frame */
11152         INIT_LIST_HEAD(&dmabuf->dbuf.list);
11153         INIT_LIST_HEAD(&dmabuf->hbuf.list);
11154         new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11155         list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
11156                 temp_hdr = (struct fc_frame_header *)h_buf->virt;
11157                 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
11158                     (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
11159                     (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
11160                         continue;
11161                 /* found a pending sequence that matches this frame */
11162                 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11163                 break;
11164         }
11165
11166         /* Free up all the frames from the partially assembled sequence */
11167         if (seq_dmabuf) {
11168                 list_for_each_entry_safe(d_buf, n_buf,
11169                                          &seq_dmabuf->dbuf.list, list) {
11170                         list_del_init(&d_buf->list);
11171                         lpfc_in_buf_free(vport->phba, d_buf);
11172                 }
11173                 return true;
11174         }
11175         return false;
11176 }
11177
11178 /**
11179  * lpfc_sli4_seq_abort_acc_cmpl - Accept seq abort iocb complete handler
11180  * @phba: Pointer to HBA context object.
11181  * @cmd_iocbq: pointer to the command iocbq structure.
11182  * @rsp_iocbq: pointer to the response iocbq structure.
11183  *
11184  * This function handles the sequence abort accept iocb command complete
11185  * event. It properly releases the memory allocated to the sequence abort
11186  * accept iocb.
11187  **/
11188 static void
11189 lpfc_sli4_seq_abort_acc_cmpl(struct lpfc_hba *phba,
11190                              struct lpfc_iocbq *cmd_iocbq,
11191                              struct lpfc_iocbq *rsp_iocbq)
11192 {
11193         if (cmd_iocbq)
11194                 lpfc_sli_release_iocbq(phba, cmd_iocbq);
11195 }
11196
11197 /**
11198  * lpfc_sli4_seq_abort_acc - Accept sequence abort
11199  * @phba: Pointer to HBA context object.
11200  * @fc_hdr: pointer to a FC frame header.
11201  *
11202  * This function sends a basic accept to a previous unsol sequence abort
11203  * event after aborting the sequence handling.
11204  **/
11205 static void
11206 lpfc_sli4_seq_abort_acc(struct lpfc_hba *phba,
11207                         struct fc_frame_header *fc_hdr)
11208 {
11209         struct lpfc_iocbq *ctiocb = NULL;
11210         struct lpfc_nodelist *ndlp;
11211         uint16_t oxid, rxid;
11212         uint32_t sid, fctl;
11213         IOCB_t *icmd;
11214
11215         if (!lpfc_is_link_up(phba))
11216                 return;
11217
11218         sid = sli4_sid_from_fc_hdr(fc_hdr);
11219         oxid = be16_to_cpu(fc_hdr->fh_ox_id);
11220         rxid = be16_to_cpu(fc_hdr->fh_rx_id);
11221
11222         ndlp = lpfc_findnode_did(phba->pport, sid);
11223         if (!ndlp) {
11224                 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
11225                                 "1268 Find ndlp returned NULL for oxid:x%x "
11226                                 "SID:x%x\n", oxid, sid);
11227                 return;
11228         }
11229
11230         /* Allocate buffer for acc iocb */
11231         ctiocb = lpfc_sli_get_iocbq(phba);
11232         if (!ctiocb)
11233                 return;
11234
11235         /* Extract the F_CTL field from FC_HDR */
11236         fctl = sli4_fctl_from_fc_hdr(fc_hdr);
11237
11238         icmd = &ctiocb->iocb;
11239         icmd->un.xseq64.bdl.bdeSize = 0;
11240         icmd->un.xseq64.bdl.ulpIoTag32 = 0;
11241         icmd->un.xseq64.w5.hcsw.Dfctl = 0;
11242         icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
11243         icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
11244
11245         /* Fill in the rest of iocb fields */
11246         icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
11247         icmd->ulpBdeCount = 0;
11248         icmd->ulpLe = 1;
11249         icmd->ulpClass = CLASS3;
11250         icmd->ulpContext = ndlp->nlp_rpi;
11251
11252         ctiocb->iocb_cmpl = NULL;
11253         ctiocb->vport = phba->pport;
11254         ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_acc_cmpl;
11255
11256         if (fctl & FC_FC_EX_CTX) {
11257                 /* ABTS sent by responder to CT exchange, construction
11258                  * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
11259                  * field and RX_ID from ABTS for RX_ID field.
11260                  */
11261                 bf_set(lpfc_abts_orig, &icmd->un.bls_acc, LPFC_ABTS_UNSOL_RSP);
11262                 bf_set(lpfc_abts_rxid, &icmd->un.bls_acc, rxid);
11263                 ctiocb->sli4_xritag = oxid;
11264         } else {
11265                 /* ABTS sent by initiator to CT exchange, construction
11266                  * of BA_ACC will need to allocate a new XRI as for the
11267                  * XRI_TAG and RX_ID fields.
11268                  */
11269                 bf_set(lpfc_abts_orig, &icmd->un.bls_acc, LPFC_ABTS_UNSOL_INT);
11270                 bf_set(lpfc_abts_rxid, &icmd->un.bls_acc, NO_XRI);
11271                 ctiocb->sli4_xritag = NO_XRI;
11272         }
11273         bf_set(lpfc_abts_oxid, &icmd->un.bls_acc, oxid);
11274
11275         /* Xmit CT abts accept on exchange <xid> */
11276         lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
11277                         "1200 Xmit CT ABTS ACC on exchange x%x Data: x%x\n",
11278                         CMD_XMIT_BLS_RSP64_CX, phba->link_state);
11279         lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
11280 }
11281
11282 /**
11283  * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
11284  * @vport: Pointer to the vport on which this sequence was received
11285  * @dmabuf: pointer to a dmabuf that describes the FC sequence
11286  *
11287  * This function handles an SLI-4 unsolicited abort event. If the unsolicited
11288  * receive sequence is only partially assembed by the driver, it shall abort
11289  * the partially assembled frames for the sequence. Otherwise, if the
11290  * unsolicited receive sequence has been completely assembled and passed to
11291  * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
11292  * unsolicited sequence has been aborted. After that, it will issue a basic
11293  * accept to accept the abort.
11294  **/
11295 void
11296 lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
11297                              struct hbq_dmabuf *dmabuf)
11298 {
11299         struct lpfc_hba *phba = vport->phba;
11300         struct fc_frame_header fc_hdr;
11301         uint32_t fctl;
11302         bool abts_par;
11303
11304         /* Make a copy of fc_hdr before the dmabuf being released */
11305         memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
11306         fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
11307
11308         if (fctl & FC_FC_EX_CTX) {
11309                 /*
11310                  * ABTS sent by responder to exchange, just free the buffer
11311                  */
11312                 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11313         } else {
11314                 /*
11315                  * ABTS sent by initiator to exchange, need to do cleanup
11316                  */
11317                 /* Try to abort partially assembled seq */
11318                 abts_par = lpfc_sli4_abort_partial_seq(vport, dmabuf);
11319
11320                 /* Send abort to ULP if partially seq abort failed */
11321                 if (abts_par == false)
11322                         lpfc_sli4_send_seq_to_ulp(vport, dmabuf);
11323                 else
11324                         lpfc_in_buf_free(phba, &dmabuf->dbuf);
11325         }
11326         /* Send basic accept (BA_ACC) to the abort requester */
11327         lpfc_sli4_seq_abort_acc(phba, &fc_hdr);
11328 }
11329
11330 /**
11331  * lpfc_seq_complete - Indicates if a sequence is complete
11332  * @dmabuf: pointer to a dmabuf that describes the FC sequence
11333  *
11334  * This function checks the sequence, starting with the frame described by
11335  * @dmabuf, to see if all the frames associated with this sequence are present.
11336  * the frames associated with this sequence are linked to the @dmabuf using the
11337  * dbuf list. This function looks for two major things. 1) That the first frame
11338  * has a sequence count of zero. 2) There is a frame with last frame of sequence
11339  * set. 3) That there are no holes in the sequence count. The function will
11340  * return 1 when the sequence is complete, otherwise it will return 0.
11341  **/
11342 static int
11343 lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
11344 {
11345         struct fc_frame_header *hdr;
11346         struct lpfc_dmabuf *d_buf;
11347         struct hbq_dmabuf *seq_dmabuf;
11348         uint32_t fctl;
11349         int seq_count = 0;
11350
11351         hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11352         /* make sure first fame of sequence has a sequence count of zero */
11353         if (hdr->fh_seq_cnt != seq_count)
11354                 return 0;
11355         fctl = (hdr->fh_f_ctl[0] << 16 |
11356                 hdr->fh_f_ctl[1] << 8 |
11357                 hdr->fh_f_ctl[2]);
11358         /* If last frame of sequence we can return success. */
11359         if (fctl & FC_FC_END_SEQ)
11360                 return 1;
11361         list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
11362                 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
11363                 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
11364                 /* If there is a hole in the sequence count then fail. */
11365                 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
11366                         return 0;
11367                 fctl = (hdr->fh_f_ctl[0] << 16 |
11368                         hdr->fh_f_ctl[1] << 8 |
11369                         hdr->fh_f_ctl[2]);
11370                 /* If last frame of sequence we can return success. */
11371                 if (fctl & FC_FC_END_SEQ)
11372                         return 1;
11373         }
11374         return 0;
11375 }
11376
11377 /**
11378  * lpfc_prep_seq - Prep sequence for ULP processing
11379  * @vport: Pointer to the vport on which this sequence was received
11380  * @dmabuf: pointer to a dmabuf that describes the FC sequence
11381  *
11382  * This function takes a sequence, described by a list of frames, and creates
11383  * a list of iocbq structures to describe the sequence. This iocbq list will be
11384  * used to issue to the generic unsolicited sequence handler. This routine
11385  * returns a pointer to the first iocbq in the list. If the function is unable
11386  * to allocate an iocbq then it throw out the received frames that were not
11387  * able to be described and return a pointer to the first iocbq. If unable to
11388  * allocate any iocbqs (including the first) this function will return NULL.
11389  **/
11390 static struct lpfc_iocbq *
11391 lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
11392 {
11393         struct lpfc_dmabuf *d_buf, *n_buf;
11394         struct lpfc_iocbq *first_iocbq, *iocbq;
11395         struct fc_frame_header *fc_hdr;
11396         uint32_t sid;
11397         struct ulp_bde64 *pbde;
11398
11399         fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
11400         /* remove from receive buffer list */
11401         list_del_init(&seq_dmabuf->hbuf.list);
11402         lpfc_update_rcv_time_stamp(vport);
11403         /* get the Remote Port's SID */
11404         sid = sli4_sid_from_fc_hdr(fc_hdr);
11405         /* Get an iocbq struct to fill in. */
11406         first_iocbq = lpfc_sli_get_iocbq(vport->phba);
11407         if (first_iocbq) {
11408                 /* Initialize the first IOCB. */
11409                 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
11410                 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
11411                 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
11412                 first_iocbq->iocb.ulpContext = be16_to_cpu(fc_hdr->fh_ox_id);
11413                 first_iocbq->iocb.unsli3.rcvsli3.vpi =
11414                                         vport->vpi + vport->phba->vpi_base;
11415                 /* put the first buffer into the first IOCBq */
11416                 first_iocbq->context2 = &seq_dmabuf->dbuf;
11417                 first_iocbq->context3 = NULL;
11418                 first_iocbq->iocb.ulpBdeCount = 1;
11419                 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
11420                                                         LPFC_DATA_BUF_SIZE;
11421                 first_iocbq->iocb.un.rcvels.remoteID = sid;
11422                 first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
11423                                 bf_get(lpfc_rcqe_length,
11424                                        &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
11425         }
11426         iocbq = first_iocbq;
11427         /*
11428          * Each IOCBq can have two Buffers assigned, so go through the list
11429          * of buffers for this sequence and save two buffers in each IOCBq
11430          */
11431         list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
11432                 if (!iocbq) {
11433                         lpfc_in_buf_free(vport->phba, d_buf);
11434                         continue;
11435                 }
11436                 if (!iocbq->context3) {
11437                         iocbq->context3 = d_buf;
11438                         iocbq->iocb.ulpBdeCount++;
11439                         pbde = (struct ulp_bde64 *)
11440                                         &iocbq->iocb.unsli3.sli3Words[4];
11441                         pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
11442                         first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
11443                                 bf_get(lpfc_rcqe_length,
11444                                        &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
11445                 } else {
11446                         iocbq = lpfc_sli_get_iocbq(vport->phba);
11447                         if (!iocbq) {
11448                                 if (first_iocbq) {
11449                                         first_iocbq->iocb.ulpStatus =
11450                                                         IOSTAT_FCP_RSP_ERROR;
11451                                         first_iocbq->iocb.un.ulpWord[4] =
11452                                                         IOERR_NO_RESOURCES;
11453                                 }
11454                                 lpfc_in_buf_free(vport->phba, d_buf);
11455                                 continue;
11456                         }
11457                         iocbq->context2 = d_buf;
11458                         iocbq->context3 = NULL;
11459                         iocbq->iocb.ulpBdeCount = 1;
11460                         iocbq->iocb.un.cont64[0].tus.f.bdeSize =
11461                                                         LPFC_DATA_BUF_SIZE;
11462                         first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
11463                                 bf_get(lpfc_rcqe_length,
11464                                        &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
11465                         iocbq->iocb.un.rcvels.remoteID = sid;
11466                         list_add_tail(&iocbq->list, &first_iocbq->list);
11467                 }
11468         }
11469         return first_iocbq;
11470 }
11471
11472 static void
11473 lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
11474                           struct hbq_dmabuf *seq_dmabuf)
11475 {
11476         struct fc_frame_header *fc_hdr;
11477         struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
11478         struct lpfc_hba *phba = vport->phba;
11479
11480         fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
11481         iocbq = lpfc_prep_seq(vport, seq_dmabuf);
11482         if (!iocbq) {
11483                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11484                                 "2707 Ring %d handler: Failed to allocate "
11485                                 "iocb Rctl x%x Type x%x received\n",
11486                                 LPFC_ELS_RING,
11487                                 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
11488                 return;
11489         }
11490         if (!lpfc_complete_unsol_iocb(phba,
11491                                       &phba->sli.ring[LPFC_ELS_RING],
11492                                       iocbq, fc_hdr->fh_r_ctl,
11493                                       fc_hdr->fh_type))
11494                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11495                                 "2540 Ring %d handler: unexpected Rctl "
11496                                 "x%x Type x%x received\n",
11497                                 LPFC_ELS_RING,
11498                                 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
11499
11500         /* Free iocb created in lpfc_prep_seq */
11501         list_for_each_entry_safe(curr_iocb, next_iocb,
11502                 &iocbq->list, list) {
11503                 list_del_init(&curr_iocb->list);
11504                 lpfc_sli_release_iocbq(phba, curr_iocb);
11505         }
11506         lpfc_sli_release_iocbq(phba, iocbq);
11507 }
11508
11509 /**
11510  * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
11511  * @phba: Pointer to HBA context object.
11512  *
11513  * This function is called with no lock held. This function processes all
11514  * the received buffers and gives it to upper layers when a received buffer
11515  * indicates that it is the final frame in the sequence. The interrupt
11516  * service routine processes received buffers at interrupt contexts and adds
11517  * received dma buffers to the rb_pend_list queue and signals the worker thread.
11518  * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
11519  * appropriate receive function when the final frame in a sequence is received.
11520  **/
11521 void
11522 lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
11523                                  struct hbq_dmabuf *dmabuf)
11524 {
11525         struct hbq_dmabuf *seq_dmabuf;
11526         struct fc_frame_header *fc_hdr;
11527         struct lpfc_vport *vport;
11528         uint32_t fcfi;
11529
11530         /* Process each received buffer */
11531         fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11532         /* check to see if this a valid type of frame */
11533         if (lpfc_fc_frame_check(phba, fc_hdr)) {
11534                 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11535                 return;
11536         }
11537         fcfi = bf_get(lpfc_rcqe_fcf_id, &dmabuf->cq_event.cqe.rcqe_cmpl);
11538         vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi);
11539         if (!vport || !(vport->vpi_state & LPFC_VPI_REGISTERED)) {
11540                 /* throw out the frame */
11541                 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11542                 return;
11543         }
11544         /* Handle the basic abort sequence (BA_ABTS) event */
11545         if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
11546                 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
11547                 return;
11548         }
11549
11550         /* Link this frame */
11551         seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
11552         if (!seq_dmabuf) {
11553                 /* unable to add frame to vport - throw it out */
11554                 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11555                 return;
11556         }
11557         /* If not last frame in sequence continue processing frames. */
11558         if (!lpfc_seq_complete(seq_dmabuf))
11559                 return;
11560
11561         /* Send the complete sequence to the upper layer protocol */
11562         lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
11563 }
11564
11565 /**
11566  * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
11567  * @phba: pointer to lpfc hba data structure.
11568  *
11569  * This routine is invoked to post rpi header templates to the
11570  * HBA consistent with the SLI-4 interface spec.  This routine
11571  * posts a PAGE_SIZE memory region to the port to hold up to
11572  * PAGE_SIZE modulo 64 rpi context headers.
11573  *
11574  * This routine does not require any locks.  It's usage is expected
11575  * to be driver load or reset recovery when the driver is
11576  * sequential.
11577  *
11578  * Return codes
11579  *      0 - successful
11580  *      EIO - The mailbox failed to complete successfully.
11581  *      When this error occurs, the driver is not guaranteed
11582  *      to have any rpi regions posted to the device and
11583  *      must either attempt to repost the regions or take a
11584  *      fatal error.
11585  **/
11586 int
11587 lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
11588 {
11589         struct lpfc_rpi_hdr *rpi_page;
11590         uint32_t rc = 0;
11591
11592         /* Post all rpi memory regions to the port. */
11593         list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
11594                 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
11595                 if (rc != MBX_SUCCESS) {
11596                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11597                                         "2008 Error %d posting all rpi "
11598                                         "headers\n", rc);
11599                         rc = -EIO;
11600                         break;
11601                 }
11602         }
11603
11604         return rc;
11605 }
11606
11607 /**
11608  * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
11609  * @phba: pointer to lpfc hba data structure.
11610  * @rpi_page:  pointer to the rpi memory region.
11611  *
11612  * This routine is invoked to post a single rpi header to the
11613  * HBA consistent with the SLI-4 interface spec.  This memory region
11614  * maps up to 64 rpi context regions.
11615  *
11616  * Return codes
11617  *      0 - successful
11618  *      ENOMEM - No available memory
11619  *      EIO - The mailbox failed to complete successfully.
11620  **/
11621 int
11622 lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
11623 {
11624         LPFC_MBOXQ_t *mboxq;
11625         struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
11626         uint32_t rc = 0;
11627         uint32_t mbox_tmo;
11628         uint32_t shdr_status, shdr_add_status;
11629         union lpfc_sli4_cfg_shdr *shdr;
11630
11631         /* The port is notified of the header region via a mailbox command. */
11632         mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11633         if (!mboxq) {
11634                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11635                                 "2001 Unable to allocate memory for issuing "
11636                                 "SLI_CONFIG_SPECIAL mailbox command\n");
11637                 return -ENOMEM;
11638         }
11639
11640         /* Post all rpi memory regions to the port. */
11641         hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
11642         mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
11643         lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
11644                          LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
11645                          sizeof(struct lpfc_mbx_post_hdr_tmpl) -
11646                          sizeof(struct mbox_header), LPFC_SLI4_MBX_EMBED);
11647         bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
11648                hdr_tmpl, rpi_page->page_count);
11649         bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
11650                rpi_page->start_rpi);
11651         hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
11652         hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
11653         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
11654         shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
11655         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11656         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11657         if (rc != MBX_TIMEOUT)
11658                 mempool_free(mboxq, phba->mbox_mem_pool);
11659         if (shdr_status || shdr_add_status || rc) {
11660                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11661                                 "2514 POST_RPI_HDR mailbox failed with "
11662                                 "status x%x add_status x%x, mbx status x%x\n",
11663                                 shdr_status, shdr_add_status, rc);
11664                 rc = -ENXIO;
11665         }
11666         return rc;
11667 }
11668
11669 /**
11670  * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
11671  * @phba: pointer to lpfc hba data structure.
11672  *
11673  * This routine is invoked to post rpi header templates to the
11674  * HBA consistent with the SLI-4 interface spec.  This routine
11675  * posts a PAGE_SIZE memory region to the port to hold up to
11676  * PAGE_SIZE modulo 64 rpi context headers.
11677  *
11678  * Returns
11679  *      A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
11680  *      LPFC_RPI_ALLOC_ERROR if no rpis are available.
11681  **/
11682 int
11683 lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
11684 {
11685         int rpi;
11686         uint16_t max_rpi, rpi_base, rpi_limit;
11687         uint16_t rpi_remaining;
11688         struct lpfc_rpi_hdr *rpi_hdr;
11689
11690         max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
11691         rpi_base = phba->sli4_hba.max_cfg_param.rpi_base;
11692         rpi_limit = phba->sli4_hba.next_rpi;
11693
11694         /*
11695          * The valid rpi range is not guaranteed to be zero-based.  Start
11696          * the search at the rpi_base as reported by the port.
11697          */
11698         spin_lock_irq(&phba->hbalock);
11699         rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, rpi_base);
11700         if (rpi >= rpi_limit || rpi < rpi_base)
11701                 rpi = LPFC_RPI_ALLOC_ERROR;
11702         else {
11703                 set_bit(rpi, phba->sli4_hba.rpi_bmask);
11704                 phba->sli4_hba.max_cfg_param.rpi_used++;
11705                 phba->sli4_hba.rpi_count++;
11706         }
11707
11708         /*
11709          * Don't try to allocate more rpi header regions if the device limit
11710          * on available rpis max has been exhausted.
11711          */
11712         if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
11713             (phba->sli4_hba.rpi_count >= max_rpi)) {
11714                 spin_unlock_irq(&phba->hbalock);
11715                 return rpi;
11716         }
11717
11718         /*
11719          * If the driver is running low on rpi resources, allocate another
11720          * page now.  Note that the next_rpi value is used because
11721          * it represents how many are actually in use whereas max_rpi notes
11722          * how many are supported max by the device.
11723          */
11724         rpi_remaining = phba->sli4_hba.next_rpi - rpi_base -
11725                 phba->sli4_hba.rpi_count;
11726         spin_unlock_irq(&phba->hbalock);
11727         if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
11728                 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
11729                 if (!rpi_hdr) {
11730                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11731                                         "2002 Error Could not grow rpi "
11732                                         "count\n");
11733                 } else {
11734                         lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
11735                 }
11736         }
11737
11738         return rpi;
11739 }
11740
11741 /**
11742  * lpfc_sli4_free_rpi - Release an rpi for reuse.
11743  * @phba: pointer to lpfc hba data structure.
11744  *
11745  * This routine is invoked to release an rpi to the pool of
11746  * available rpis maintained by the driver.
11747  **/
11748 void
11749 lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
11750 {
11751         spin_lock_irq(&phba->hbalock);
11752         clear_bit(rpi, phba->sli4_hba.rpi_bmask);
11753         phba->sli4_hba.rpi_count--;
11754         phba->sli4_hba.max_cfg_param.rpi_used--;
11755         spin_unlock_irq(&phba->hbalock);
11756 }
11757
11758 /**
11759  * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
11760  * @phba: pointer to lpfc hba data structure.
11761  *
11762  * This routine is invoked to remove the memory region that
11763  * provided rpi via a bitmask.
11764  **/
11765 void
11766 lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
11767 {
11768         kfree(phba->sli4_hba.rpi_bmask);
11769 }
11770
11771 /**
11772  * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
11773  * @phba: pointer to lpfc hba data structure.
11774  *
11775  * This routine is invoked to remove the memory region that
11776  * provided rpi via a bitmask.
11777  **/
11778 int
11779 lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp)
11780 {
11781         LPFC_MBOXQ_t *mboxq;
11782         struct lpfc_hba *phba = ndlp->phba;
11783         int rc;
11784
11785         /* The port is notified of the header region via a mailbox command. */
11786         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11787         if (!mboxq)
11788                 return -ENOMEM;
11789
11790         /* Post all rpi memory regions to the port. */
11791         lpfc_resume_rpi(mboxq, ndlp);
11792         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
11793         if (rc == MBX_NOT_FINISHED) {
11794                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11795                                 "2010 Resume RPI Mailbox failed "
11796                                 "status %d, mbxStatus x%x\n", rc,
11797                                 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
11798                 mempool_free(mboxq, phba->mbox_mem_pool);
11799                 return -EIO;
11800         }
11801         return 0;
11802 }
11803
11804 /**
11805  * lpfc_sli4_init_vpi - Initialize a vpi with the port
11806  * @phba: pointer to lpfc hba data structure.
11807  * @vpi: vpi value to activate with the port.
11808  *
11809  * This routine is invoked to activate a vpi with the
11810  * port when the host intends to use vports with a
11811  * nonzero vpi.
11812  *
11813  * Returns:
11814  *    0 success
11815  *    -Evalue otherwise
11816  **/
11817 int
11818 lpfc_sli4_init_vpi(struct lpfc_hba *phba, uint16_t vpi)
11819 {
11820         LPFC_MBOXQ_t *mboxq;
11821         int rc = 0;
11822         int retval = MBX_SUCCESS;
11823         uint32_t mbox_tmo;
11824
11825         if (vpi == 0)
11826                 return -EINVAL;
11827         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11828         if (!mboxq)
11829                 return -ENOMEM;
11830         lpfc_init_vpi(phba, mboxq, vpi);
11831         mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_INIT_VPI);
11832         rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
11833         if (rc != MBX_SUCCESS) {
11834                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11835                                 "2022 INIT VPI Mailbox failed "
11836                                 "status %d, mbxStatus x%x\n", rc,
11837                                 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
11838                 retval = -EIO;
11839         }
11840         if (rc != MBX_TIMEOUT)
11841                 mempool_free(mboxq, phba->mbox_mem_pool);
11842
11843         return retval;
11844 }
11845
11846 /**
11847  * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
11848  * @phba: pointer to lpfc hba data structure.
11849  * @mboxq: Pointer to mailbox object.
11850  *
11851  * This routine is invoked to manually add a single FCF record. The caller
11852  * must pass a completely initialized FCF_Record.  This routine takes
11853  * care of the nonembedded mailbox operations.
11854  **/
11855 static void
11856 lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
11857 {
11858         void *virt_addr;
11859         union lpfc_sli4_cfg_shdr *shdr;
11860         uint32_t shdr_status, shdr_add_status;
11861
11862         virt_addr = mboxq->sge_array->addr[0];
11863         /* The IOCTL status is embedded in the mailbox subheader. */
11864         shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
11865         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11866         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11867
11868         if ((shdr_status || shdr_add_status) &&
11869                 (shdr_status != STATUS_FCF_IN_USE))
11870                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11871                         "2558 ADD_FCF_RECORD mailbox failed with "
11872                         "status x%x add_status x%x\n",
11873                         shdr_status, shdr_add_status);
11874
11875         lpfc_sli4_mbox_cmd_free(phba, mboxq);
11876 }
11877
11878 /**
11879  * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
11880  * @phba: pointer to lpfc hba data structure.
11881  * @fcf_record:  pointer to the initialized fcf record to add.
11882  *
11883  * This routine is invoked to manually add a single FCF record. The caller
11884  * must pass a completely initialized FCF_Record.  This routine takes
11885  * care of the nonembedded mailbox operations.
11886  **/
11887 int
11888 lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
11889 {
11890         int rc = 0;
11891         LPFC_MBOXQ_t *mboxq;
11892         uint8_t *bytep;
11893         void *virt_addr;
11894         dma_addr_t phys_addr;
11895         struct lpfc_mbx_sge sge;
11896         uint32_t alloc_len, req_len;
11897         uint32_t fcfindex;
11898
11899         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11900         if (!mboxq) {
11901                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11902                         "2009 Failed to allocate mbox for ADD_FCF cmd\n");
11903                 return -ENOMEM;
11904         }
11905
11906         req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
11907                   sizeof(uint32_t);
11908
11909         /* Allocate DMA memory and set up the non-embedded mailbox command */
11910         alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
11911                                      LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
11912                                      req_len, LPFC_SLI4_MBX_NEMBED);
11913         if (alloc_len < req_len) {
11914                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11915                         "2523 Allocated DMA memory size (x%x) is "
11916                         "less than the requested DMA memory "
11917                         "size (x%x)\n", alloc_len, req_len);
11918                 lpfc_sli4_mbox_cmd_free(phba, mboxq);
11919                 return -ENOMEM;
11920         }
11921
11922         /*
11923          * Get the first SGE entry from the non-embedded DMA memory.  This
11924          * routine only uses a single SGE.
11925          */
11926         lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
11927         phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
11928         virt_addr = mboxq->sge_array->addr[0];
11929         /*
11930          * Configure the FCF record for FCFI 0.  This is the driver's
11931          * hardcoded default and gets used in nonFIP mode.
11932          */
11933         fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
11934         bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
11935         lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
11936
11937         /*
11938          * Copy the fcf_index and the FCF Record Data. The data starts after
11939          * the FCoE header plus word10. The data copy needs to be endian
11940          * correct.
11941          */
11942         bytep += sizeof(uint32_t);
11943         lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
11944         mboxq->vport = phba->pport;
11945         mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
11946         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
11947         if (rc == MBX_NOT_FINISHED) {
11948                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11949                         "2515 ADD_FCF_RECORD mailbox failed with "
11950                         "status 0x%x\n", rc);
11951                 lpfc_sli4_mbox_cmd_free(phba, mboxq);
11952                 rc = -EIO;
11953         } else
11954                 rc = 0;
11955
11956         return rc;
11957 }
11958
11959 /**
11960  * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
11961  * @phba: pointer to lpfc hba data structure.
11962  * @fcf_record:  pointer to the fcf record to write the default data.
11963  * @fcf_index: FCF table entry index.
11964  *
11965  * This routine is invoked to build the driver's default FCF record.  The
11966  * values used are hardcoded.  This routine handles memory initialization.
11967  *
11968  **/
11969 void
11970 lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
11971                                 struct fcf_record *fcf_record,
11972                                 uint16_t fcf_index)
11973 {
11974         memset(fcf_record, 0, sizeof(struct fcf_record));
11975         fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
11976         fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
11977         fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
11978         bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
11979         bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
11980         bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
11981         bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
11982         bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
11983         bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
11984         bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
11985         bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
11986         bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
11987         bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
11988         bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
11989         bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
11990         bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
11991                 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
11992         /* Set the VLAN bit map */
11993         if (phba->valid_vlan) {
11994                 fcf_record->vlan_bitmap[phba->vlan_id / 8]
11995                         = 1 << (phba->vlan_id % 8);
11996         }
11997 }
11998
11999 /**
12000  * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
12001  * @phba: pointer to lpfc hba data structure.
12002  * @fcf_index: FCF table entry offset.
12003  *
12004  * This routine is invoked to scan the entire FCF table by reading FCF
12005  * record and processing it one at a time starting from the @fcf_index
12006  * for initial FCF discovery or fast FCF failover rediscovery.
12007  *
12008  * Return 0 if the mailbox command is submitted sucessfully, none 0
12009  * otherwise.
12010  **/
12011 int
12012 lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
12013 {
12014         int rc = 0, error;
12015         LPFC_MBOXQ_t *mboxq;
12016
12017         phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
12018         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12019         if (!mboxq) {
12020                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12021                                 "2000 Failed to allocate mbox for "
12022                                 "READ_FCF cmd\n");
12023                 error = -ENOMEM;
12024                 goto fail_fcf_scan;
12025         }
12026         /* Construct the read FCF record mailbox command */
12027         rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
12028         if (rc) {
12029                 error = -EINVAL;
12030                 goto fail_fcf_scan;
12031         }
12032         /* Issue the mailbox command asynchronously */
12033         mboxq->vport = phba->pport;
12034         mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
12035         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
12036         if (rc == MBX_NOT_FINISHED)
12037                 error = -EIO;
12038         else {
12039                 spin_lock_irq(&phba->hbalock);
12040                 phba->hba_flag |= FCF_DISC_INPROGRESS;
12041                 spin_unlock_irq(&phba->hbalock);
12042                 /* Reset FCF round robin index bmask for new scan */
12043                 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
12044                         memset(phba->fcf.fcf_rr_bmask, 0,
12045                                sizeof(*phba->fcf.fcf_rr_bmask));
12046                 error = 0;
12047         }
12048 fail_fcf_scan:
12049         if (error) {
12050                 if (mboxq)
12051                         lpfc_sli4_mbox_cmd_free(phba, mboxq);
12052                 /* FCF scan failed, clear FCF_DISC_INPROGRESS flag */
12053                 spin_lock_irq(&phba->hbalock);
12054                 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
12055                 spin_unlock_irq(&phba->hbalock);
12056         }
12057         return error;
12058 }
12059
12060 /**
12061  * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for round robin fcf.
12062  * @phba: pointer to lpfc hba data structure.
12063  * @fcf_index: FCF table entry offset.
12064  *
12065  * This routine is invoked to read an FCF record indicated by @fcf_index
12066  * and to use it for FLOGI round robin FCF failover.
12067  *
12068  * Return 0 if the mailbox command is submitted sucessfully, none 0
12069  * otherwise.
12070  **/
12071 int
12072 lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
12073 {
12074         int rc = 0, error;
12075         LPFC_MBOXQ_t *mboxq;
12076
12077         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12078         if (!mboxq) {
12079                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
12080                                 "2763 Failed to allocate mbox for "
12081                                 "READ_FCF cmd\n");
12082                 error = -ENOMEM;
12083                 goto fail_fcf_read;
12084         }
12085         /* Construct the read FCF record mailbox command */
12086         rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
12087         if (rc) {
12088                 error = -EINVAL;
12089                 goto fail_fcf_read;
12090         }
12091         /* Issue the mailbox command asynchronously */
12092         mboxq->vport = phba->pport;
12093         mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
12094         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
12095         if (rc == MBX_NOT_FINISHED)
12096                 error = -EIO;
12097         else
12098                 error = 0;
12099
12100 fail_fcf_read:
12101         if (error && mboxq)
12102                 lpfc_sli4_mbox_cmd_free(phba, mboxq);
12103         return error;
12104 }
12105
12106 /**
12107  * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
12108  * @phba: pointer to lpfc hba data structure.
12109  * @fcf_index: FCF table entry offset.
12110  *
12111  * This routine is invoked to read an FCF record indicated by @fcf_index to
12112  * determine whether it's eligible for FLOGI round robin failover list.
12113  *
12114  * Return 0 if the mailbox command is submitted sucessfully, none 0
12115  * otherwise.
12116  **/
12117 int
12118 lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
12119 {
12120         int rc = 0, error;
12121         LPFC_MBOXQ_t *mboxq;
12122
12123         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12124         if (!mboxq) {
12125                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
12126                                 "2758 Failed to allocate mbox for "
12127                                 "READ_FCF cmd\n");
12128                                 error = -ENOMEM;
12129                                 goto fail_fcf_read;
12130         }
12131         /* Construct the read FCF record mailbox command */
12132         rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
12133         if (rc) {
12134                 error = -EINVAL;
12135                 goto fail_fcf_read;
12136         }
12137         /* Issue the mailbox command asynchronously */
12138         mboxq->vport = phba->pport;
12139         mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
12140         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
12141         if (rc == MBX_NOT_FINISHED)
12142                 error = -EIO;
12143         else
12144                 error = 0;
12145
12146 fail_fcf_read:
12147         if (error && mboxq)
12148                 lpfc_sli4_mbox_cmd_free(phba, mboxq);
12149         return error;
12150 }
12151
12152 /**
12153  * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
12154  * @phba: pointer to lpfc hba data structure.
12155  *
12156  * This routine is to get the next eligible FCF record index in a round
12157  * robin fashion. If the next eligible FCF record index equals to the
12158  * initial round robin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
12159  * shall be returned, otherwise, the next eligible FCF record's index
12160  * shall be returned.
12161  **/
12162 uint16_t
12163 lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
12164 {
12165         uint16_t next_fcf_index;
12166
12167         /* Search from the currently registered FCF index */
12168         next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
12169                                        LPFC_SLI4_FCF_TBL_INDX_MAX,
12170                                        phba->fcf.current_rec.fcf_indx);
12171         /* Wrap around condition on phba->fcf.fcf_rr_bmask */
12172         if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
12173                 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
12174                                                LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
12175         /* Round robin failover stop condition */
12176         if (next_fcf_index == phba->fcf.fcf_rr_init_indx)
12177                 return LPFC_FCOE_FCF_NEXT_NONE;
12178
12179         return next_fcf_index;
12180 }
12181
12182 /**
12183  * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
12184  * @phba: pointer to lpfc hba data structure.
12185  *
12186  * This routine sets the FCF record index in to the eligible bmask for
12187  * round robin failover search. It checks to make sure that the index
12188  * does not go beyond the range of the driver allocated bmask dimension
12189  * before setting the bit.
12190  *
12191  * Returns 0 if the index bit successfully set, otherwise, it returns
12192  * -EINVAL.
12193  **/
12194 int
12195 lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
12196 {
12197         if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
12198                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
12199                                 "2610 HBA FCF index reached driver's "
12200                                 "book keeping dimension: fcf_index:%d, "
12201                                 "driver_bmask_max:%d\n",
12202                                 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
12203                 return -EINVAL;
12204         }
12205         /* Set the eligible FCF record index bmask */
12206         set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
12207
12208         return 0;
12209 }
12210
12211 /**
12212  * lpfc_sli4_fcf_rr_index_set - Clear bmask from eligible fcf record index
12213  * @phba: pointer to lpfc hba data structure.
12214  *
12215  * This routine clears the FCF record index from the eligible bmask for
12216  * round robin failover search. It checks to make sure that the index
12217  * does not go beyond the range of the driver allocated bmask dimension
12218  * before clearing the bit.
12219  **/
12220 void
12221 lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
12222 {
12223         if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
12224                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
12225                                 "2762 HBA FCF index goes beyond driver's "
12226                                 "book keeping dimension: fcf_index:%d, "
12227                                 "driver_bmask_max:%d\n",
12228                                 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
12229                 return;
12230         }
12231         /* Clear the eligible FCF record index bmask */
12232         clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
12233 }
12234
12235 /**
12236  * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
12237  * @phba: pointer to lpfc hba data structure.
12238  *
12239  * This routine is the completion routine for the rediscover FCF table mailbox
12240  * command. If the mailbox command returned failure, it will try to stop the
12241  * FCF rediscover wait timer.
12242  **/
12243 void
12244 lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
12245 {
12246         struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
12247         uint32_t shdr_status, shdr_add_status;
12248
12249         redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
12250
12251         shdr_status = bf_get(lpfc_mbox_hdr_status,
12252                              &redisc_fcf->header.cfg_shdr.response);
12253         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
12254                              &redisc_fcf->header.cfg_shdr.response);
12255         if (shdr_status || shdr_add_status) {
12256                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
12257                                 "2746 Requesting for FCF rediscovery failed "
12258                                 "status x%x add_status x%x\n",
12259                                 shdr_status, shdr_add_status);
12260                 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
12261                         spin_lock_irq(&phba->hbalock);
12262                         phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
12263                         spin_unlock_irq(&phba->hbalock);
12264                         /*
12265                          * CVL event triggered FCF rediscover request failed,
12266                          * last resort to re-try current registered FCF entry.
12267                          */
12268                         lpfc_retry_pport_discovery(phba);
12269                 } else {
12270                         spin_lock_irq(&phba->hbalock);
12271                         phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
12272                         spin_unlock_irq(&phba->hbalock);
12273                         /*
12274                          * DEAD FCF event triggered FCF rediscover request
12275                          * failed, last resort to fail over as a link down
12276                          * to FCF registration.
12277                          */
12278                         lpfc_sli4_fcf_dead_failthrough(phba);
12279                 }
12280         } else {
12281                 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
12282                                 "2775 Start FCF rediscovery quiescent period "
12283                                 "wait timer before scaning FCF table\n");
12284                 /*
12285                  * Start FCF rediscovery wait timer for pending FCF
12286                  * before rescan FCF record table.
12287                  */
12288                 lpfc_fcf_redisc_wait_start_timer(phba);
12289         }
12290
12291         mempool_free(mbox, phba->mbox_mem_pool);
12292 }
12293
12294 /**
12295  * lpfc_sli4_redisc_all_fcf - Request to rediscover entire FCF table by port.
12296  * @phba: pointer to lpfc hba data structure.
12297  *
12298  * This routine is invoked to request for rediscovery of the entire FCF table
12299  * by the port.
12300  **/
12301 int
12302 lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
12303 {
12304         LPFC_MBOXQ_t *mbox;
12305         struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
12306         int rc, length;
12307
12308         /* Cancel retry delay timers to all vports before FCF rediscover */
12309         lpfc_cancel_all_vport_retry_delay_timer(phba);
12310
12311         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12312         if (!mbox) {
12313                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12314                                 "2745 Failed to allocate mbox for "
12315                                 "requesting FCF rediscover.\n");
12316                 return -ENOMEM;
12317         }
12318
12319         length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
12320                   sizeof(struct lpfc_sli4_cfg_mhdr));
12321         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12322                          LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
12323                          length, LPFC_SLI4_MBX_EMBED);
12324
12325         redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
12326         /* Set count to 0 for invalidating the entire FCF database */
12327         bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
12328
12329         /* Issue the mailbox command asynchronously */
12330         mbox->vport = phba->pport;
12331         mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
12332         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
12333
12334         if (rc == MBX_NOT_FINISHED) {
12335                 mempool_free(mbox, phba->mbox_mem_pool);
12336                 return -EIO;
12337         }
12338         return 0;
12339 }
12340
12341 /**
12342  * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
12343  * @phba: pointer to lpfc hba data structure.
12344  *
12345  * This function is the failover routine as a last resort to the FCF DEAD
12346  * event when driver failed to perform fast FCF failover.
12347  **/
12348 void
12349 lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
12350 {
12351         uint32_t link_state;
12352
12353         /*
12354          * Last resort as FCF DEAD event failover will treat this as
12355          * a link down, but save the link state because we don't want
12356          * it to be changed to Link Down unless it is already down.
12357          */
12358         link_state = phba->link_state;
12359         lpfc_linkdown(phba);
12360         phba->link_state = link_state;
12361
12362         /* Unregister FCF if no devices connected to it */
12363         lpfc_unregister_unused_fcf(phba);
12364 }
12365
12366 /**
12367  * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
12368  * @phba: pointer to lpfc hba data structure.
12369  *
12370  * This function read region 23 and parse TLV for port status to
12371  * decide if the user disaled the port. If the TLV indicates the
12372  * port is disabled, the hba_flag is set accordingly.
12373  **/
12374 void
12375 lpfc_sli_read_link_ste(struct lpfc_hba *phba)
12376 {
12377         LPFC_MBOXQ_t *pmb = NULL;
12378         MAILBOX_t *mb;
12379         uint8_t *rgn23_data = NULL;
12380         uint32_t offset = 0, data_size, sub_tlv_len, tlv_offset;
12381         int rc;
12382
12383         pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12384         if (!pmb) {
12385                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12386                         "2600 lpfc_sli_read_serdes_param failed to"
12387                         " allocate mailbox memory\n");
12388                 goto out;
12389         }
12390         mb = &pmb->u.mb;
12391
12392         /* Get adapter Region 23 data */
12393         rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
12394         if (!rgn23_data)
12395                 goto out;
12396
12397         do {
12398                 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
12399                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
12400
12401                 if (rc != MBX_SUCCESS) {
12402                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
12403                                 "2601 lpfc_sli_read_link_ste failed to"
12404                                 " read config region 23 rc 0x%x Status 0x%x\n",
12405                                 rc, mb->mbxStatus);
12406                         mb->un.varDmp.word_cnt = 0;
12407                 }
12408                 /*
12409                  * dump mem may return a zero when finished or we got a
12410                  * mailbox error, either way we are done.
12411                  */
12412                 if (mb->un.varDmp.word_cnt == 0)
12413                         break;
12414                 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
12415                         mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
12416
12417                 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
12418                         rgn23_data + offset,
12419                         mb->un.varDmp.word_cnt);
12420                 offset += mb->un.varDmp.word_cnt;
12421         } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
12422
12423         data_size = offset;
12424         offset = 0;
12425
12426         if (!data_size)
12427                 goto out;
12428
12429         /* Check the region signature first */
12430         if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
12431                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12432                         "2619 Config region 23 has bad signature\n");
12433                         goto out;
12434         }
12435         offset += 4;
12436
12437         /* Check the data structure version */
12438         if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
12439                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12440                         "2620 Config region 23 has bad version\n");
12441                 goto out;
12442         }
12443         offset += 4;
12444
12445         /* Parse TLV entries in the region */
12446         while (offset < data_size) {
12447                 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
12448                         break;
12449                 /*
12450                  * If the TLV is not driver specific TLV or driver id is
12451                  * not linux driver id, skip the record.
12452                  */
12453                 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
12454                     (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
12455                     (rgn23_data[offset + 3] != 0)) {
12456                         offset += rgn23_data[offset + 1] * 4 + 4;
12457                         continue;
12458                 }
12459
12460                 /* Driver found a driver specific TLV in the config region */
12461                 sub_tlv_len = rgn23_data[offset + 1] * 4;
12462                 offset += 4;
12463                 tlv_offset = 0;
12464
12465                 /*
12466                  * Search for configured port state sub-TLV.
12467                  */
12468                 while ((offset < data_size) &&
12469                         (tlv_offset < sub_tlv_len)) {
12470                         if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
12471                                 offset += 4;
12472                                 tlv_offset += 4;
12473                                 break;
12474                         }
12475                         if (rgn23_data[offset] != PORT_STE_TYPE) {
12476                                 offset += rgn23_data[offset + 1] * 4 + 4;
12477                                 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
12478                                 continue;
12479                         }
12480
12481                         /* This HBA contains PORT_STE configured */
12482                         if (!rgn23_data[offset + 2])
12483                                 phba->hba_flag |= LINK_DISABLED;
12484
12485                         goto out;
12486                 }
12487         }
12488 out:
12489         if (pmb)
12490                 mempool_free(pmb, phba->mbox_mem_pool);
12491         kfree(rgn23_data);
12492         return;
12493 }
12494
12495 /**
12496  * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
12497  * @vport: pointer to vport data structure.
12498  *
12499  * This function iterate through the mailboxq and clean up all REG_LOGIN
12500  * and REG_VPI mailbox commands associated with the vport. This function
12501  * is called when driver want to restart discovery of the vport due to
12502  * a Clear Virtual Link event.
12503  **/
12504 void
12505 lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
12506 {
12507         struct lpfc_hba *phba = vport->phba;
12508         LPFC_MBOXQ_t *mb, *nextmb;
12509         struct lpfc_dmabuf *mp;
12510
12511         spin_lock_irq(&phba->hbalock);
12512         list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
12513                 if (mb->vport != vport)
12514                         continue;
12515
12516                 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
12517                         (mb->u.mb.mbxCommand != MBX_REG_VPI))
12518                         continue;
12519
12520                 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
12521                         mp = (struct lpfc_dmabuf *) (mb->context1);
12522                         if (mp) {
12523                                 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
12524                                 kfree(mp);
12525                         }
12526                 }
12527                 list_del(&mb->list);
12528                 mempool_free(mb, phba->mbox_mem_pool);
12529         }
12530         mb = phba->sli.mbox_active;
12531         if (mb && (mb->vport == vport)) {
12532                 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
12533                         (mb->u.mb.mbxCommand == MBX_REG_VPI))
12534                         mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12535         }
12536         spin_unlock_irq(&phba->hbalock);
12537 }
12538