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