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