Merge tag 'mfd_3.4-1' of git://git.kernel.org/pub/scm/linux/kernel/git/sameo/mfd-2.6
[cascardo/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-2011 Emulex.  All rights reserved.           *
5  * EMULEX and SLI are trademarks of Emulex.                        *
6  * www.emulex.com                                                  *
7  * Portions Copyright (C) 2004-2005 Christoph Hellwig              *
8  *                                                                 *
9  * This program is free software; you can redistribute it and/or   *
10  * modify it under the terms of version 2 of the GNU General       *
11  * Public License as published by the Free Software Foundation.    *
12  * This program is distributed in the hope that it will be useful. *
13  * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND          *
14  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,  *
15  * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE      *
16  * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
17  * TO BE LEGALLY INVALID.  See the GNU General Public License for  *
18  * more details, a copy of which can be found in the file COPYING  *
19  * included with this package.                                     *
20  *******************************************************************/
21
22 #include <linux/blkdev.h>
23 #include <linux/pci.h>
24 #include <linux/interrupt.h>
25 #include <linux/delay.h>
26 #include <linux/slab.h>
27
28 #include <scsi/scsi.h>
29 #include <scsi/scsi_cmnd.h>
30 #include <scsi/scsi_device.h>
31 #include <scsi/scsi_host.h>
32 #include <scsi/scsi_transport_fc.h>
33 #include <scsi/fc/fc_fs.h>
34 #include <linux/aer.h>
35
36 #include "lpfc_hw4.h"
37 #include "lpfc_hw.h"
38 #include "lpfc_sli.h"
39 #include "lpfc_sli4.h"
40 #include "lpfc_nl.h"
41 #include "lpfc_disc.h"
42 #include "lpfc_scsi.h"
43 #include "lpfc.h"
44 #include "lpfc_crtn.h"
45 #include "lpfc_logmsg.h"
46 #include "lpfc_compat.h"
47 #include "lpfc_debugfs.h"
48 #include "lpfc_vport.h"
49
50 /* There are only four IOCB completion types. */
51 typedef enum _lpfc_iocb_type {
52         LPFC_UNKNOWN_IOCB,
53         LPFC_UNSOL_IOCB,
54         LPFC_SOL_IOCB,
55         LPFC_ABORT_IOCB
56 } lpfc_iocb_type;
57
58
59 /* Provide function prototypes local to this module. */
60 static int lpfc_sli_issue_mbox_s4(struct lpfc_hba *, LPFC_MBOXQ_t *,
61                                   uint32_t);
62 static int lpfc_sli4_read_rev(struct lpfc_hba *, LPFC_MBOXQ_t *,
63                               uint8_t *, uint32_t *);
64 static struct lpfc_iocbq *lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *,
65                                                          struct lpfc_iocbq *);
66 static void lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *,
67                                       struct hbq_dmabuf *);
68 static int lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *, struct lpfc_queue *,
69                                     struct lpfc_cqe *);
70
71 static IOCB_t *
72 lpfc_get_iocb_from_iocbq(struct lpfc_iocbq *iocbq)
73 {
74         return &iocbq->iocb;
75 }
76
77 /**
78  * lpfc_sli4_wq_put - Put a Work Queue Entry on an Work Queue
79  * @q: The Work Queue to operate on.
80  * @wqe: The work Queue Entry to put on the Work queue.
81  *
82  * This routine will copy the contents of @wqe to the next available entry on
83  * the @q. This function will then ring the Work Queue Doorbell to signal the
84  * HBA to start processing the Work Queue Entry. This function returns 0 if
85  * successful. If no entries are available on @q then this function will return
86  * -ENOMEM.
87  * The caller is expected to hold the hbalock when calling this routine.
88  **/
89 static uint32_t
90 lpfc_sli4_wq_put(struct lpfc_queue *q, union lpfc_wqe *wqe)
91 {
92         union lpfc_wqe *temp_wqe;
93         struct lpfc_register doorbell;
94         uint32_t host_index;
95
96         /* sanity check on queue memory */
97         if (unlikely(!q))
98                 return -ENOMEM;
99         temp_wqe = q->qe[q->host_index].wqe;
100
101         /* If the host has not yet processed the next entry then we are done */
102         if (((q->host_index + 1) % q->entry_count) == q->hba_index)
103                 return -ENOMEM;
104         /* set consumption flag every once in a while */
105         if (!((q->host_index + 1) % q->entry_repost))
106                 bf_set(wqe_wqec, &wqe->generic.wqe_com, 1);
107         if (q->phba->sli3_options & LPFC_SLI4_PHWQ_ENABLED)
108                 bf_set(wqe_wqid, &wqe->generic.wqe_com, q->queue_id);
109         lpfc_sli_pcimem_bcopy(wqe, temp_wqe, q->entry_size);
110
111         /* Update the host index before invoking device */
112         host_index = q->host_index;
113         q->host_index = ((q->host_index + 1) % q->entry_count);
114
115         /* Ring Doorbell */
116         doorbell.word0 = 0;
117         bf_set(lpfc_wq_doorbell_num_posted, &doorbell, 1);
118         bf_set(lpfc_wq_doorbell_index, &doorbell, host_index);
119         bf_set(lpfc_wq_doorbell_id, &doorbell, q->queue_id);
120         writel(doorbell.word0, q->phba->sli4_hba.WQDBregaddr);
121         readl(q->phba->sli4_hba.WQDBregaddr); /* Flush */
122
123         return 0;
124 }
125
126 /**
127  * lpfc_sli4_wq_release - Updates internal hba index for WQ
128  * @q: The Work Queue to operate on.
129  * @index: The index to advance the hba index to.
130  *
131  * This routine will update the HBA index of a queue to reflect consumption of
132  * Work Queue Entries by the HBA. When the HBA indicates that it has consumed
133  * an entry the host calls this function to update the queue's internal
134  * pointers. This routine returns the number of entries that were consumed by
135  * the HBA.
136  **/
137 static uint32_t
138 lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)
139 {
140         uint32_t released = 0;
141
142         /* sanity check on queue memory */
143         if (unlikely(!q))
144                 return 0;
145
146         if (q->hba_index == index)
147                 return 0;
148         do {
149                 q->hba_index = ((q->hba_index + 1) % q->entry_count);
150                 released++;
151         } while (q->hba_index != index);
152         return released;
153 }
154
155 /**
156  * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue
157  * @q: The Mailbox Queue to operate on.
158  * @wqe: The Mailbox Queue Entry to put on the Work queue.
159  *
160  * This routine will copy the contents of @mqe to the next available entry on
161  * the @q. This function will then ring the Work Queue Doorbell to signal the
162  * HBA to start processing the Work Queue Entry. This function returns 0 if
163  * successful. If no entries are available on @q then this function will return
164  * -ENOMEM.
165  * The caller is expected to hold the hbalock when calling this routine.
166  **/
167 static uint32_t
168 lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)
169 {
170         struct lpfc_mqe *temp_mqe;
171         struct lpfc_register doorbell;
172         uint32_t host_index;
173
174         /* sanity check on queue memory */
175         if (unlikely(!q))
176                 return -ENOMEM;
177         temp_mqe = q->qe[q->host_index].mqe;
178
179         /* If the host has not yet processed the next entry then we are done */
180         if (((q->host_index + 1) % q->entry_count) == q->hba_index)
181                 return -ENOMEM;
182         lpfc_sli_pcimem_bcopy(mqe, temp_mqe, q->entry_size);
183         /* Save off the mailbox pointer for completion */
184         q->phba->mbox = (MAILBOX_t *)temp_mqe;
185
186         /* Update the host index before invoking device */
187         host_index = q->host_index;
188         q->host_index = ((q->host_index + 1) % q->entry_count);
189
190         /* Ring Doorbell */
191         doorbell.word0 = 0;
192         bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);
193         bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);
194         writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);
195         readl(q->phba->sli4_hba.MQDBregaddr); /* Flush */
196         return 0;
197 }
198
199 /**
200  * lpfc_sli4_mq_release - Updates internal hba index for MQ
201  * @q: The Mailbox Queue to operate on.
202  *
203  * This routine will update the HBA index of a queue to reflect consumption of
204  * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed
205  * an entry the host calls this function to update the queue's internal
206  * pointers. This routine returns the number of entries that were consumed by
207  * the HBA.
208  **/
209 static uint32_t
210 lpfc_sli4_mq_release(struct lpfc_queue *q)
211 {
212         /* sanity check on queue memory */
213         if (unlikely(!q))
214                 return 0;
215
216         /* Clear the mailbox pointer for completion */
217         q->phba->mbox = NULL;
218         q->hba_index = ((q->hba_index + 1) % q->entry_count);
219         return 1;
220 }
221
222 /**
223  * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ
224  * @q: The Event Queue to get the first valid EQE from
225  *
226  * This routine will get the first valid Event Queue Entry from @q, update
227  * the queue's internal hba index, and return the EQE. If no valid EQEs are in
228  * the Queue (no more work to do), or the Queue is full of EQEs that have been
229  * processed, but not popped back to the HBA then this routine will return NULL.
230  **/
231 static struct lpfc_eqe *
232 lpfc_sli4_eq_get(struct lpfc_queue *q)
233 {
234         struct lpfc_eqe *eqe;
235
236         /* sanity check on queue memory */
237         if (unlikely(!q))
238                 return NULL;
239         eqe = q->qe[q->hba_index].eqe;
240
241         /* If the next EQE is not valid then we are done */
242         if (!bf_get_le32(lpfc_eqe_valid, eqe))
243                 return NULL;
244         /* If the host has not yet processed the next entry then we are done */
245         if (((q->hba_index + 1) % q->entry_count) == q->host_index)
246                 return NULL;
247
248         q->hba_index = ((q->hba_index + 1) % q->entry_count);
249         return eqe;
250 }
251
252 /**
253  * lpfc_sli4_eq_release - Indicates the host has finished processing an EQ
254  * @q: The Event Queue that the host has completed processing for.
255  * @arm: Indicates whether the host wants to arms this CQ.
256  *
257  * This routine will mark all Event Queue Entries on @q, from the last
258  * known completed entry to the last entry that was processed, as completed
259  * by clearing the valid bit for each completion queue entry. Then it will
260  * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
261  * The internal host index in the @q will be updated by this routine to indicate
262  * that the host has finished processing the entries. The @arm parameter
263  * indicates that the queue should be rearmed when ringing the doorbell.
264  *
265  * This function will return the number of EQEs that were popped.
266  **/
267 uint32_t
268 lpfc_sli4_eq_release(struct lpfc_queue *q, bool arm)
269 {
270         uint32_t released = 0;
271         struct lpfc_eqe *temp_eqe;
272         struct lpfc_register doorbell;
273
274         /* sanity check on queue memory */
275         if (unlikely(!q))
276                 return 0;
277
278         /* while there are valid entries */
279         while (q->hba_index != q->host_index) {
280                 temp_eqe = q->qe[q->host_index].eqe;
281                 bf_set_le32(lpfc_eqe_valid, temp_eqe, 0);
282                 released++;
283                 q->host_index = ((q->host_index + 1) % q->entry_count);
284         }
285         if (unlikely(released == 0 && !arm))
286                 return 0;
287
288         /* ring doorbell for number popped */
289         doorbell.word0 = 0;
290         if (arm) {
291                 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
292                 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
293         }
294         bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
295         bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
296         bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
297                         (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
298         bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
299         writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
300         /* PCI read to flush PCI pipeline on re-arming for INTx mode */
301         if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
302                 readl(q->phba->sli4_hba.EQCQDBregaddr);
303         return released;
304 }
305
306 /**
307  * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
308  * @q: The Completion Queue to get the first valid CQE from
309  *
310  * This routine will get the first valid Completion Queue Entry from @q, update
311  * the queue's internal hba index, and return the CQE. If no valid CQEs are in
312  * the Queue (no more work to do), or the Queue is full of CQEs that have been
313  * processed, but not popped back to the HBA then this routine will return NULL.
314  **/
315 static struct lpfc_cqe *
316 lpfc_sli4_cq_get(struct lpfc_queue *q)
317 {
318         struct lpfc_cqe *cqe;
319
320         /* sanity check on queue memory */
321         if (unlikely(!q))
322                 return NULL;
323
324         /* If the next CQE is not valid then we are done */
325         if (!bf_get_le32(lpfc_cqe_valid, q->qe[q->hba_index].cqe))
326                 return NULL;
327         /* If the host has not yet processed the next entry then we are done */
328         if (((q->hba_index + 1) % q->entry_count) == q->host_index)
329                 return NULL;
330
331         cqe = q->qe[q->hba_index].cqe;
332         q->hba_index = ((q->hba_index + 1) % q->entry_count);
333         return cqe;
334 }
335
336 /**
337  * lpfc_sli4_cq_release - Indicates the host has finished processing a CQ
338  * @q: The Completion Queue that the host has completed processing for.
339  * @arm: Indicates whether the host wants to arms this CQ.
340  *
341  * This routine will mark all Completion queue entries on @q, from the last
342  * known completed entry to the last entry that was processed, as completed
343  * by clearing the valid bit for each completion queue entry. Then it will
344  * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
345  * The internal host index in the @q will be updated by this routine to indicate
346  * that the host has finished processing the entries. The @arm parameter
347  * indicates that the queue should be rearmed when ringing the doorbell.
348  *
349  * This function will return the number of CQEs that were released.
350  **/
351 uint32_t
352 lpfc_sli4_cq_release(struct lpfc_queue *q, bool arm)
353 {
354         uint32_t released = 0;
355         struct lpfc_cqe *temp_qe;
356         struct lpfc_register doorbell;
357
358         /* sanity check on queue memory */
359         if (unlikely(!q))
360                 return 0;
361         /* while there are valid entries */
362         while (q->hba_index != q->host_index) {
363                 temp_qe = q->qe[q->host_index].cqe;
364                 bf_set_le32(lpfc_cqe_valid, temp_qe, 0);
365                 released++;
366                 q->host_index = ((q->host_index + 1) % q->entry_count);
367         }
368         if (unlikely(released == 0 && !arm))
369                 return 0;
370
371         /* ring doorbell for number popped */
372         doorbell.word0 = 0;
373         if (arm)
374                 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
375         bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
376         bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
377         bf_set(lpfc_eqcq_doorbell_cqid_hi, &doorbell,
378                         (q->queue_id >> LPFC_CQID_HI_FIELD_SHIFT));
379         bf_set(lpfc_eqcq_doorbell_cqid_lo, &doorbell, q->queue_id);
380         writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
381         return released;
382 }
383
384 /**
385  * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
386  * @q: The Header Receive Queue to operate on.
387  * @wqe: The Receive Queue Entry to put on the Receive queue.
388  *
389  * This routine will copy the contents of @wqe to the next available entry on
390  * the @q. This function will then ring the Receive Queue Doorbell to signal the
391  * HBA to start processing the Receive Queue Entry. This function returns the
392  * index that the rqe was copied to if successful. If no entries are available
393  * on @q then this function will return -ENOMEM.
394  * The caller is expected to hold the hbalock when calling this routine.
395  **/
396 static int
397 lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
398                  struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
399 {
400         struct lpfc_rqe *temp_hrqe;
401         struct lpfc_rqe *temp_drqe;
402         struct lpfc_register doorbell;
403         int put_index = hq->host_index;
404
405         /* sanity check on queue memory */
406         if (unlikely(!hq) || unlikely(!dq))
407                 return -ENOMEM;
408         temp_hrqe = hq->qe[hq->host_index].rqe;
409         temp_drqe = dq->qe[dq->host_index].rqe;
410
411         if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
412                 return -EINVAL;
413         if (hq->host_index != dq->host_index)
414                 return -EINVAL;
415         /* If the host has not yet processed the next entry then we are done */
416         if (((hq->host_index + 1) % hq->entry_count) == hq->hba_index)
417                 return -EBUSY;
418         lpfc_sli_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
419         lpfc_sli_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
420
421         /* Update the host index to point to the next slot */
422         hq->host_index = ((hq->host_index + 1) % hq->entry_count);
423         dq->host_index = ((dq->host_index + 1) % dq->entry_count);
424
425         /* Ring The Header Receive Queue Doorbell */
426         if (!(hq->host_index % hq->entry_repost)) {
427                 doorbell.word0 = 0;
428                 bf_set(lpfc_rq_doorbell_num_posted, &doorbell,
429                        hq->entry_repost);
430                 bf_set(lpfc_rq_doorbell_id, &doorbell, hq->queue_id);
431                 writel(doorbell.word0, hq->phba->sli4_hba.RQDBregaddr);
432         }
433         return put_index;
434 }
435
436 /**
437  * lpfc_sli4_rq_release - Updates internal hba index for RQ
438  * @q: The Header Receive Queue to operate on.
439  *
440  * This routine will update the HBA index of a queue to reflect consumption of
441  * one Receive Queue Entry by the HBA. When the HBA indicates that it has
442  * consumed an entry the host calls this function to update the queue's
443  * internal pointers. This routine returns the number of entries that were
444  * consumed by the HBA.
445  **/
446 static uint32_t
447 lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
448 {
449         /* sanity check on queue memory */
450         if (unlikely(!hq) || unlikely(!dq))
451                 return 0;
452
453         if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
454                 return 0;
455         hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
456         dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
457         return 1;
458 }
459
460 /**
461  * lpfc_cmd_iocb - Get next command iocb entry in the ring
462  * @phba: Pointer to HBA context object.
463  * @pring: Pointer to driver SLI ring object.
464  *
465  * This function returns pointer to next command iocb entry
466  * in the command ring. The caller must hold hbalock to prevent
467  * other threads consume the next command iocb.
468  * SLI-2/SLI-3 provide different sized iocbs.
469  **/
470 static inline IOCB_t *
471 lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
472 {
473         return (IOCB_t *) (((char *) pring->cmdringaddr) +
474                            pring->cmdidx * phba->iocb_cmd_size);
475 }
476
477 /**
478  * lpfc_resp_iocb - Get next response iocb entry in the ring
479  * @phba: Pointer to HBA context object.
480  * @pring: Pointer to driver SLI ring object.
481  *
482  * This function returns pointer to next response iocb entry
483  * in the response ring. The caller must hold hbalock to make sure
484  * that no other thread consume the next response iocb.
485  * SLI-2/SLI-3 provide different sized iocbs.
486  **/
487 static inline IOCB_t *
488 lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
489 {
490         return (IOCB_t *) (((char *) pring->rspringaddr) +
491                            pring->rspidx * phba->iocb_rsp_size);
492 }
493
494 /**
495  * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
496  * @phba: Pointer to HBA context object.
497  *
498  * This function is called with hbalock held. This function
499  * allocates a new driver iocb object from the iocb pool. If the
500  * allocation is successful, it returns pointer to the newly
501  * allocated iocb object else it returns NULL.
502  **/
503 static struct lpfc_iocbq *
504 __lpfc_sli_get_iocbq(struct lpfc_hba *phba)
505 {
506         struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
507         struct lpfc_iocbq * iocbq = NULL;
508
509         list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
510         if (iocbq)
511                 phba->iocb_cnt++;
512         if (phba->iocb_cnt > phba->iocb_max)
513                 phba->iocb_max = phba->iocb_cnt;
514         return iocbq;
515 }
516
517 /**
518  * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
519  * @phba: Pointer to HBA context object.
520  * @xritag: XRI value.
521  *
522  * This function clears the sglq pointer from the array of acive
523  * sglq's. The xritag that is passed in is used to index into the
524  * array. Before the xritag can be used it needs to be adjusted
525  * by subtracting the xribase.
526  *
527  * Returns sglq ponter = success, NULL = Failure.
528  **/
529 static struct lpfc_sglq *
530 __lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
531 {
532         struct lpfc_sglq *sglq;
533
534         sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
535         phba->sli4_hba.lpfc_sglq_active_list[xritag] = NULL;
536         return sglq;
537 }
538
539 /**
540  * __lpfc_get_active_sglq - Get the active sglq for this XRI.
541  * @phba: Pointer to HBA context object.
542  * @xritag: XRI value.
543  *
544  * This function returns the sglq pointer from the array of acive
545  * sglq's. The xritag that is passed in is used to index into the
546  * array. Before the xritag can be used it needs to be adjusted
547  * by subtracting the xribase.
548  *
549  * Returns sglq ponter = success, NULL = Failure.
550  **/
551 struct lpfc_sglq *
552 __lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
553 {
554         struct lpfc_sglq *sglq;
555
556         sglq =  phba->sli4_hba.lpfc_sglq_active_list[xritag];
557         return sglq;
558 }
559
560 /**
561  * lpfc_clr_rrq_active - Clears RRQ active bit in xri_bitmap.
562  * @phba: Pointer to HBA context object.
563  * @xritag: xri used in this exchange.
564  * @rrq: The RRQ to be cleared.
565  *
566  **/
567 void
568 lpfc_clr_rrq_active(struct lpfc_hba *phba,
569                     uint16_t xritag,
570                     struct lpfc_node_rrq *rrq)
571 {
572         struct lpfc_nodelist *ndlp = NULL;
573
574         if ((rrq->vport) && NLP_CHK_NODE_ACT(rrq->ndlp))
575                 ndlp = lpfc_findnode_did(rrq->vport, rrq->nlp_DID);
576
577         /* The target DID could have been swapped (cable swap)
578          * we should use the ndlp from the findnode if it is
579          * available.
580          */
581         if ((!ndlp) && rrq->ndlp)
582                 ndlp = rrq->ndlp;
583
584         if (!ndlp)
585                 goto out;
586
587         if (test_and_clear_bit(xritag, ndlp->active_rrqs.xri_bitmap)) {
588                 rrq->send_rrq = 0;
589                 rrq->xritag = 0;
590                 rrq->rrq_stop_time = 0;
591         }
592 out:
593         mempool_free(rrq, phba->rrq_pool);
594 }
595
596 /**
597  * lpfc_handle_rrq_active - Checks if RRQ has waithed RATOV.
598  * @phba: Pointer to HBA context object.
599  *
600  * This function is called with hbalock held. This function
601  * Checks if stop_time (ratov from setting rrq active) has
602  * been reached, if it has and the send_rrq flag is set then
603  * it will call lpfc_send_rrq. If the send_rrq flag is not set
604  * then it will just call the routine to clear the rrq and
605  * free the rrq resource.
606  * The timer is set to the next rrq that is going to expire before
607  * leaving the routine.
608  *
609  **/
610 void
611 lpfc_handle_rrq_active(struct lpfc_hba *phba)
612 {
613         struct lpfc_node_rrq *rrq;
614         struct lpfc_node_rrq *nextrrq;
615         unsigned long next_time;
616         unsigned long iflags;
617         LIST_HEAD(send_rrq);
618
619         spin_lock_irqsave(&phba->hbalock, iflags);
620         phba->hba_flag &= ~HBA_RRQ_ACTIVE;
621         next_time = jiffies + HZ * (phba->fc_ratov + 1);
622         list_for_each_entry_safe(rrq, nextrrq,
623                                  &phba->active_rrq_list, list) {
624                 if (time_after(jiffies, rrq->rrq_stop_time))
625                         list_move(&rrq->list, &send_rrq);
626                 else if (time_before(rrq->rrq_stop_time, next_time))
627                         next_time = rrq->rrq_stop_time;
628         }
629         spin_unlock_irqrestore(&phba->hbalock, iflags);
630         if (!list_empty(&phba->active_rrq_list))
631                 mod_timer(&phba->rrq_tmr, next_time);
632         list_for_each_entry_safe(rrq, nextrrq, &send_rrq, list) {
633                 list_del(&rrq->list);
634                 if (!rrq->send_rrq)
635                         /* this call will free the rrq */
636                 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
637                 else if (lpfc_send_rrq(phba, rrq)) {
638                         /* if we send the rrq then the completion handler
639                         *  will clear the bit in the xribitmap.
640                         */
641                         lpfc_clr_rrq_active(phba, rrq->xritag,
642                                             rrq);
643                 }
644         }
645 }
646
647 /**
648  * lpfc_get_active_rrq - Get the active RRQ for this exchange.
649  * @vport: Pointer to vport context object.
650  * @xri: The xri used in the exchange.
651  * @did: The targets DID for this exchange.
652  *
653  * returns NULL = rrq not found in the phba->active_rrq_list.
654  *         rrq = rrq for this xri and target.
655  **/
656 struct lpfc_node_rrq *
657 lpfc_get_active_rrq(struct lpfc_vport *vport, uint16_t xri, uint32_t did)
658 {
659         struct lpfc_hba *phba = vport->phba;
660         struct lpfc_node_rrq *rrq;
661         struct lpfc_node_rrq *nextrrq;
662         unsigned long iflags;
663
664         if (phba->sli_rev != LPFC_SLI_REV4)
665                 return NULL;
666         spin_lock_irqsave(&phba->hbalock, iflags);
667         list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {
668                 if (rrq->vport == vport && rrq->xritag == xri &&
669                                 rrq->nlp_DID == did){
670                         list_del(&rrq->list);
671                         spin_unlock_irqrestore(&phba->hbalock, iflags);
672                         return rrq;
673                 }
674         }
675         spin_unlock_irqrestore(&phba->hbalock, iflags);
676         return NULL;
677 }
678
679 /**
680  * lpfc_cleanup_vports_rrqs - Remove and clear the active RRQ for this vport.
681  * @vport: Pointer to vport context object.
682  * @ndlp: Pointer to the lpfc_node_list structure.
683  * If ndlp is NULL Remove all active RRQs for this vport from the
684  * phba->active_rrq_list and clear the rrq.
685  * If ndlp is not NULL then only remove rrqs for this vport & this ndlp.
686  **/
687 void
688 lpfc_cleanup_vports_rrqs(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
689
690 {
691         struct lpfc_hba *phba = vport->phba;
692         struct lpfc_node_rrq *rrq;
693         struct lpfc_node_rrq *nextrrq;
694         unsigned long iflags;
695         LIST_HEAD(rrq_list);
696
697         if (phba->sli_rev != LPFC_SLI_REV4)
698                 return;
699         if (!ndlp) {
700                 lpfc_sli4_vport_delete_els_xri_aborted(vport);
701                 lpfc_sli4_vport_delete_fcp_xri_aborted(vport);
702         }
703         spin_lock_irqsave(&phba->hbalock, iflags);
704         list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list)
705                 if ((rrq->vport == vport) && (!ndlp  || rrq->ndlp == ndlp))
706                         list_move(&rrq->list, &rrq_list);
707         spin_unlock_irqrestore(&phba->hbalock, iflags);
708
709         list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
710                 list_del(&rrq->list);
711                 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
712         }
713 }
714
715 /**
716  * lpfc_cleanup_wt_rrqs - Remove all rrq's from the active list.
717  * @phba: Pointer to HBA context object.
718  *
719  * Remove all rrqs from the phba->active_rrq_list and free them by
720  * calling __lpfc_clr_active_rrq
721  *
722  **/
723 void
724 lpfc_cleanup_wt_rrqs(struct lpfc_hba *phba)
725 {
726         struct lpfc_node_rrq *rrq;
727         struct lpfc_node_rrq *nextrrq;
728         unsigned long next_time;
729         unsigned long iflags;
730         LIST_HEAD(rrq_list);
731
732         if (phba->sli_rev != LPFC_SLI_REV4)
733                 return;
734         spin_lock_irqsave(&phba->hbalock, iflags);
735         phba->hba_flag &= ~HBA_RRQ_ACTIVE;
736         next_time = jiffies + HZ * (phba->fc_ratov * 2);
737         list_splice_init(&phba->active_rrq_list, &rrq_list);
738         spin_unlock_irqrestore(&phba->hbalock, iflags);
739
740         list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
741                 list_del(&rrq->list);
742                 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
743         }
744         if (!list_empty(&phba->active_rrq_list))
745                 mod_timer(&phba->rrq_tmr, next_time);
746 }
747
748
749 /**
750  * lpfc_test_rrq_active - Test RRQ bit in xri_bitmap.
751  * @phba: Pointer to HBA context object.
752  * @ndlp: Targets nodelist pointer for this exchange.
753  * @xritag the xri in the bitmap to test.
754  *
755  * This function is called with hbalock held. This function
756  * returns 0 = rrq not active for this xri
757  *         1 = rrq is valid for this xri.
758  **/
759 int
760 lpfc_test_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
761                         uint16_t  xritag)
762 {
763         if (!ndlp)
764                 return 0;
765         if (test_bit(xritag, ndlp->active_rrqs.xri_bitmap))
766                         return 1;
767         else
768                 return 0;
769 }
770
771 /**
772  * lpfc_set_rrq_active - set RRQ active bit in xri_bitmap.
773  * @phba: Pointer to HBA context object.
774  * @ndlp: nodelist pointer for this target.
775  * @xritag: xri used in this exchange.
776  * @rxid: Remote Exchange ID.
777  * @send_rrq: Flag used to determine if we should send rrq els cmd.
778  *
779  * This function takes the hbalock.
780  * The active bit is always set in the active rrq xri_bitmap even
781  * if there is no slot avaiable for the other rrq information.
782  *
783  * returns 0 rrq actived for this xri
784  *         < 0 No memory or invalid ndlp.
785  **/
786 int
787 lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
788                     uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
789 {
790         unsigned long iflags;
791         struct lpfc_node_rrq *rrq;
792         int empty;
793
794         if (!ndlp)
795                 return -EINVAL;
796
797         if (!phba->cfg_enable_rrq)
798                 return -EINVAL;
799
800         spin_lock_irqsave(&phba->hbalock, iflags);
801         if (phba->pport->load_flag & FC_UNLOADING) {
802                 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
803                 goto out;
804         }
805
806         /*
807          * set the active bit even if there is no mem available.
808          */
809         if (NLP_CHK_FREE_REQ(ndlp))
810                 goto out;
811
812         if (ndlp->vport && (ndlp->vport->load_flag & FC_UNLOADING))
813                 goto out;
814
815         if (test_and_set_bit(xritag, ndlp->active_rrqs.xri_bitmap))
816                 goto out;
817
818         spin_unlock_irqrestore(&phba->hbalock, iflags);
819         rrq = mempool_alloc(phba->rrq_pool, GFP_KERNEL);
820         if (!rrq) {
821                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
822                                 "3155 Unable to allocate RRQ xri:0x%x rxid:0x%x"
823                                 " DID:0x%x Send:%d\n",
824                                 xritag, rxid, ndlp->nlp_DID, send_rrq);
825                 return -EINVAL;
826         }
827         rrq->send_rrq = send_rrq;
828         rrq->xritag = xritag;
829         rrq->rrq_stop_time = jiffies + HZ * (phba->fc_ratov + 1);
830         rrq->ndlp = ndlp;
831         rrq->nlp_DID = ndlp->nlp_DID;
832         rrq->vport = ndlp->vport;
833         rrq->rxid = rxid;
834         rrq->send_rrq = send_rrq;
835         spin_lock_irqsave(&phba->hbalock, iflags);
836         empty = list_empty(&phba->active_rrq_list);
837         list_add_tail(&rrq->list, &phba->active_rrq_list);
838         phba->hba_flag |= HBA_RRQ_ACTIVE;
839         if (empty)
840                 lpfc_worker_wake_up(phba);
841         spin_unlock_irqrestore(&phba->hbalock, iflags);
842         return 0;
843 out:
844         spin_unlock_irqrestore(&phba->hbalock, iflags);
845         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
846                         "2921 Can't set rrq active xri:0x%x rxid:0x%x"
847                         " DID:0x%x Send:%d\n",
848                         xritag, rxid, ndlp->nlp_DID, send_rrq);
849         return -EINVAL;
850 }
851
852 /**
853  * __lpfc_sli_get_sglq - Allocates an iocb object from sgl pool
854  * @phba: Pointer to HBA context object.
855  * @piocb: Pointer to the iocbq.
856  *
857  * This function is called with hbalock held. This function
858  * gets a new driver sglq object from the sglq list. If the
859  * list is not empty then it is successful, it returns pointer to the newly
860  * allocated sglq object else it returns NULL.
861  **/
862 static struct lpfc_sglq *
863 __lpfc_sli_get_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
864 {
865         struct list_head *lpfc_sgl_list = &phba->sli4_hba.lpfc_sgl_list;
866         struct lpfc_sglq *sglq = NULL;
867         struct lpfc_sglq *start_sglq = NULL;
868         struct lpfc_scsi_buf *lpfc_cmd;
869         struct lpfc_nodelist *ndlp;
870         int found = 0;
871
872         if (piocbq->iocb_flag &  LPFC_IO_FCP) {
873                 lpfc_cmd = (struct lpfc_scsi_buf *) piocbq->context1;
874                 ndlp = lpfc_cmd->rdata->pnode;
875         } else  if ((piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) &&
876                         !(piocbq->iocb_flag & LPFC_IO_LIBDFC))
877                 ndlp = piocbq->context_un.ndlp;
878         else
879                 ndlp = piocbq->context1;
880
881         list_remove_head(lpfc_sgl_list, sglq, struct lpfc_sglq, list);
882         start_sglq = sglq;
883         while (!found) {
884                 if (!sglq)
885                         return NULL;
886                 if (lpfc_test_rrq_active(phba, ndlp, sglq->sli4_xritag)) {
887                         /* This xri has an rrq outstanding for this DID.
888                          * put it back in the list and get another xri.
889                          */
890                         list_add_tail(&sglq->list, lpfc_sgl_list);
891                         sglq = NULL;
892                         list_remove_head(lpfc_sgl_list, sglq,
893                                                 struct lpfc_sglq, list);
894                         if (sglq == start_sglq) {
895                                 sglq = NULL;
896                                 break;
897                         } else
898                                 continue;
899                 }
900                 sglq->ndlp = ndlp;
901                 found = 1;
902                 phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
903                 sglq->state = SGL_ALLOCATED;
904         }
905         return sglq;
906 }
907
908 /**
909  * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
910  * @phba: Pointer to HBA context object.
911  *
912  * This function is called with no lock held. This function
913  * allocates a new driver iocb object from the iocb pool. If the
914  * allocation is successful, it returns pointer to the newly
915  * allocated iocb object else it returns NULL.
916  **/
917 struct lpfc_iocbq *
918 lpfc_sli_get_iocbq(struct lpfc_hba *phba)
919 {
920         struct lpfc_iocbq * iocbq = NULL;
921         unsigned long iflags;
922
923         spin_lock_irqsave(&phba->hbalock, iflags);
924         iocbq = __lpfc_sli_get_iocbq(phba);
925         spin_unlock_irqrestore(&phba->hbalock, iflags);
926         return iocbq;
927 }
928
929 /**
930  * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
931  * @phba: Pointer to HBA context object.
932  * @iocbq: Pointer to driver iocb object.
933  *
934  * This function is called with hbalock held to release driver
935  * iocb object to the iocb pool. The iotag in the iocb object
936  * does not change for each use of the iocb object. This function
937  * clears all other fields of the iocb object when it is freed.
938  * The sqlq structure that holds the xritag and phys and virtual
939  * mappings for the scatter gather list is retrieved from the
940  * active array of sglq. The get of the sglq pointer also clears
941  * the entry in the array. If the status of the IO indiactes that
942  * this IO was aborted then the sglq entry it put on the
943  * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
944  * IO has good status or fails for any other reason then the sglq
945  * entry is added to the free list (lpfc_sgl_list).
946  **/
947 static void
948 __lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
949 {
950         struct lpfc_sglq *sglq;
951         size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
952         unsigned long iflag = 0;
953         struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
954
955         if (iocbq->sli4_xritag == NO_XRI)
956                 sglq = NULL;
957         else
958                 sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_lxritag);
959
960         if (sglq)  {
961                 if ((iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) &&
962                         (sglq->state != SGL_XRI_ABORTED)) {
963                         spin_lock_irqsave(&phba->sli4_hba.abts_sgl_list_lock,
964                                         iflag);
965                         list_add(&sglq->list,
966                                 &phba->sli4_hba.lpfc_abts_els_sgl_list);
967                         spin_unlock_irqrestore(
968                                 &phba->sli4_hba.abts_sgl_list_lock, iflag);
969                 } else {
970                         sglq->state = SGL_FREED;
971                         sglq->ndlp = NULL;
972                         list_add_tail(&sglq->list,
973                                 &phba->sli4_hba.lpfc_sgl_list);
974
975                         /* Check if TXQ queue needs to be serviced */
976                         if (pring->txq_cnt)
977                                 lpfc_worker_wake_up(phba);
978                 }
979         }
980
981
982         /*
983          * Clean all volatile data fields, preserve iotag and node struct.
984          */
985         memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
986         iocbq->sli4_lxritag = NO_XRI;
987         iocbq->sli4_xritag = NO_XRI;
988         list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
989 }
990
991
992 /**
993  * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
994  * @phba: Pointer to HBA context object.
995  * @iocbq: Pointer to driver iocb object.
996  *
997  * This function is called with hbalock held to release driver
998  * iocb object to the iocb pool. The iotag in the iocb object
999  * does not change for each use of the iocb object. This function
1000  * clears all other fields of the iocb object when it is freed.
1001  **/
1002 static void
1003 __lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1004 {
1005         size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
1006
1007         /*
1008          * Clean all volatile data fields, preserve iotag and node struct.
1009          */
1010         memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
1011         iocbq->sli4_xritag = NO_XRI;
1012         list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1013 }
1014
1015 /**
1016  * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
1017  * @phba: Pointer to HBA context object.
1018  * @iocbq: Pointer to driver iocb object.
1019  *
1020  * This function is called with hbalock held to release driver
1021  * iocb object to the iocb pool. The iotag in the iocb object
1022  * does not change for each use of the iocb object. This function
1023  * clears all other fields of the iocb object when it is freed.
1024  **/
1025 static void
1026 __lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1027 {
1028         phba->__lpfc_sli_release_iocbq(phba, iocbq);
1029         phba->iocb_cnt--;
1030 }
1031
1032 /**
1033  * lpfc_sli_release_iocbq - Release iocb to the iocb pool
1034  * @phba: Pointer to HBA context object.
1035  * @iocbq: Pointer to driver iocb object.
1036  *
1037  * This function is called with no lock held to release the iocb to
1038  * iocb pool.
1039  **/
1040 void
1041 lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1042 {
1043         unsigned long iflags;
1044
1045         /*
1046          * Clean all volatile data fields, preserve iotag and node struct.
1047          */
1048         spin_lock_irqsave(&phba->hbalock, iflags);
1049         __lpfc_sli_release_iocbq(phba, iocbq);
1050         spin_unlock_irqrestore(&phba->hbalock, iflags);
1051 }
1052
1053 /**
1054  * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
1055  * @phba: Pointer to HBA context object.
1056  * @iocblist: List of IOCBs.
1057  * @ulpstatus: ULP status in IOCB command field.
1058  * @ulpWord4: ULP word-4 in IOCB command field.
1059  *
1060  * This function is called with a list of IOCBs to cancel. It cancels the IOCB
1061  * on the list by invoking the complete callback function associated with the
1062  * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
1063  * fields.
1064  **/
1065 void
1066 lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
1067                       uint32_t ulpstatus, uint32_t ulpWord4)
1068 {
1069         struct lpfc_iocbq *piocb;
1070
1071         while (!list_empty(iocblist)) {
1072                 list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
1073
1074                 if (!piocb->iocb_cmpl)
1075                         lpfc_sli_release_iocbq(phba, piocb);
1076                 else {
1077                         piocb->iocb.ulpStatus = ulpstatus;
1078                         piocb->iocb.un.ulpWord[4] = ulpWord4;
1079                         (piocb->iocb_cmpl) (phba, piocb, piocb);
1080                 }
1081         }
1082         return;
1083 }
1084
1085 /**
1086  * lpfc_sli_iocb_cmd_type - Get the iocb type
1087  * @iocb_cmnd: iocb command code.
1088  *
1089  * This function is called by ring event handler function to get the iocb type.
1090  * This function translates the iocb command to an iocb command type used to
1091  * decide the final disposition of each completed IOCB.
1092  * The function returns
1093  * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
1094  * LPFC_SOL_IOCB     if it is a solicited iocb completion
1095  * LPFC_ABORT_IOCB   if it is an abort iocb
1096  * LPFC_UNSOL_IOCB   if it is an unsolicited iocb
1097  *
1098  * The caller is not required to hold any lock.
1099  **/
1100 static lpfc_iocb_type
1101 lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
1102 {
1103         lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
1104
1105         if (iocb_cmnd > CMD_MAX_IOCB_CMD)
1106                 return 0;
1107
1108         switch (iocb_cmnd) {
1109         case CMD_XMIT_SEQUENCE_CR:
1110         case CMD_XMIT_SEQUENCE_CX:
1111         case CMD_XMIT_BCAST_CN:
1112         case CMD_XMIT_BCAST_CX:
1113         case CMD_ELS_REQUEST_CR:
1114         case CMD_ELS_REQUEST_CX:
1115         case CMD_CREATE_XRI_CR:
1116         case CMD_CREATE_XRI_CX:
1117         case CMD_GET_RPI_CN:
1118         case CMD_XMIT_ELS_RSP_CX:
1119         case CMD_GET_RPI_CR:
1120         case CMD_FCP_IWRITE_CR:
1121         case CMD_FCP_IWRITE_CX:
1122         case CMD_FCP_IREAD_CR:
1123         case CMD_FCP_IREAD_CX:
1124         case CMD_FCP_ICMND_CR:
1125         case CMD_FCP_ICMND_CX:
1126         case CMD_FCP_TSEND_CX:
1127         case CMD_FCP_TRSP_CX:
1128         case CMD_FCP_TRECEIVE_CX:
1129         case CMD_FCP_AUTO_TRSP_CX:
1130         case CMD_ADAPTER_MSG:
1131         case CMD_ADAPTER_DUMP:
1132         case CMD_XMIT_SEQUENCE64_CR:
1133         case CMD_XMIT_SEQUENCE64_CX:
1134         case CMD_XMIT_BCAST64_CN:
1135         case CMD_XMIT_BCAST64_CX:
1136         case CMD_ELS_REQUEST64_CR:
1137         case CMD_ELS_REQUEST64_CX:
1138         case CMD_FCP_IWRITE64_CR:
1139         case CMD_FCP_IWRITE64_CX:
1140         case CMD_FCP_IREAD64_CR:
1141         case CMD_FCP_IREAD64_CX:
1142         case CMD_FCP_ICMND64_CR:
1143         case CMD_FCP_ICMND64_CX:
1144         case CMD_FCP_TSEND64_CX:
1145         case CMD_FCP_TRSP64_CX:
1146         case CMD_FCP_TRECEIVE64_CX:
1147         case CMD_GEN_REQUEST64_CR:
1148         case CMD_GEN_REQUEST64_CX:
1149         case CMD_XMIT_ELS_RSP64_CX:
1150         case DSSCMD_IWRITE64_CR:
1151         case DSSCMD_IWRITE64_CX:
1152         case DSSCMD_IREAD64_CR:
1153         case DSSCMD_IREAD64_CX:
1154                 type = LPFC_SOL_IOCB;
1155                 break;
1156         case CMD_ABORT_XRI_CN:
1157         case CMD_ABORT_XRI_CX:
1158         case CMD_CLOSE_XRI_CN:
1159         case CMD_CLOSE_XRI_CX:
1160         case CMD_XRI_ABORTED_CX:
1161         case CMD_ABORT_MXRI64_CN:
1162         case CMD_XMIT_BLS_RSP64_CX:
1163                 type = LPFC_ABORT_IOCB;
1164                 break;
1165         case CMD_RCV_SEQUENCE_CX:
1166         case CMD_RCV_ELS_REQ_CX:
1167         case CMD_RCV_SEQUENCE64_CX:
1168         case CMD_RCV_ELS_REQ64_CX:
1169         case CMD_ASYNC_STATUS:
1170         case CMD_IOCB_RCV_SEQ64_CX:
1171         case CMD_IOCB_RCV_ELS64_CX:
1172         case CMD_IOCB_RCV_CONT64_CX:
1173         case CMD_IOCB_RET_XRI64_CX:
1174                 type = LPFC_UNSOL_IOCB;
1175                 break;
1176         case CMD_IOCB_XMIT_MSEQ64_CR:
1177         case CMD_IOCB_XMIT_MSEQ64_CX:
1178         case CMD_IOCB_RCV_SEQ_LIST64_CX:
1179         case CMD_IOCB_RCV_ELS_LIST64_CX:
1180         case CMD_IOCB_CLOSE_EXTENDED_CN:
1181         case CMD_IOCB_ABORT_EXTENDED_CN:
1182         case CMD_IOCB_RET_HBQE64_CN:
1183         case CMD_IOCB_FCP_IBIDIR64_CR:
1184         case CMD_IOCB_FCP_IBIDIR64_CX:
1185         case CMD_IOCB_FCP_ITASKMGT64_CX:
1186         case CMD_IOCB_LOGENTRY_CN:
1187         case CMD_IOCB_LOGENTRY_ASYNC_CN:
1188                 printk("%s - Unhandled SLI-3 Command x%x\n",
1189                                 __func__, iocb_cmnd);
1190                 type = LPFC_UNKNOWN_IOCB;
1191                 break;
1192         default:
1193                 type = LPFC_UNKNOWN_IOCB;
1194                 break;
1195         }
1196
1197         return type;
1198 }
1199
1200 /**
1201  * lpfc_sli_ring_map - Issue config_ring mbox for all rings
1202  * @phba: Pointer to HBA context object.
1203  *
1204  * This function is called from SLI initialization code
1205  * to configure every ring of the HBA's SLI interface. The
1206  * caller is not required to hold any lock. This function issues
1207  * a config_ring mailbox command for each ring.
1208  * This function returns zero if successful else returns a negative
1209  * error code.
1210  **/
1211 static int
1212 lpfc_sli_ring_map(struct lpfc_hba *phba)
1213 {
1214         struct lpfc_sli *psli = &phba->sli;
1215         LPFC_MBOXQ_t *pmb;
1216         MAILBOX_t *pmbox;
1217         int i, rc, ret = 0;
1218
1219         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1220         if (!pmb)
1221                 return -ENOMEM;
1222         pmbox = &pmb->u.mb;
1223         phba->link_state = LPFC_INIT_MBX_CMDS;
1224         for (i = 0; i < psli->num_rings; i++) {
1225                 lpfc_config_ring(phba, i, pmb);
1226                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
1227                 if (rc != MBX_SUCCESS) {
1228                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1229                                         "0446 Adapter failed to init (%d), "
1230                                         "mbxCmd x%x CFG_RING, mbxStatus x%x, "
1231                                         "ring %d\n",
1232                                         rc, pmbox->mbxCommand,
1233                                         pmbox->mbxStatus, i);
1234                         phba->link_state = LPFC_HBA_ERROR;
1235                         ret = -ENXIO;
1236                         break;
1237                 }
1238         }
1239         mempool_free(pmb, phba->mbox_mem_pool);
1240         return ret;
1241 }
1242
1243 /**
1244  * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
1245  * @phba: Pointer to HBA context object.
1246  * @pring: Pointer to driver SLI ring object.
1247  * @piocb: Pointer to the driver iocb object.
1248  *
1249  * This function is called with hbalock held. The function adds the
1250  * new iocb to txcmplq of the given ring. This function always returns
1251  * 0. If this function is called for ELS ring, this function checks if
1252  * there is a vport associated with the ELS command. This function also
1253  * starts els_tmofunc timer if this is an ELS command.
1254  **/
1255 static int
1256 lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1257                         struct lpfc_iocbq *piocb)
1258 {
1259         list_add_tail(&piocb->list, &pring->txcmplq);
1260         piocb->iocb_flag |= LPFC_IO_ON_Q;
1261         pring->txcmplq_cnt++;
1262         if (pring->txcmplq_cnt > pring->txcmplq_max)
1263                 pring->txcmplq_max = pring->txcmplq_cnt;
1264
1265         if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
1266            (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
1267            (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
1268                 if (!piocb->vport)
1269                         BUG();
1270                 else
1271                         mod_timer(&piocb->vport->els_tmofunc,
1272                                   jiffies + HZ * (phba->fc_ratov << 1));
1273         }
1274
1275
1276         return 0;
1277 }
1278
1279 /**
1280  * lpfc_sli_ringtx_get - Get first element of the txq
1281  * @phba: Pointer to HBA context object.
1282  * @pring: Pointer to driver SLI ring object.
1283  *
1284  * This function is called with hbalock held to get next
1285  * iocb in txq of the given ring. If there is any iocb in
1286  * the txq, the function returns first iocb in the list after
1287  * removing the iocb from the list, else it returns NULL.
1288  **/
1289 struct lpfc_iocbq *
1290 lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1291 {
1292         struct lpfc_iocbq *cmd_iocb;
1293
1294         list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
1295         if (cmd_iocb != NULL)
1296                 pring->txq_cnt--;
1297         return cmd_iocb;
1298 }
1299
1300 /**
1301  * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
1302  * @phba: Pointer to HBA context object.
1303  * @pring: Pointer to driver SLI ring object.
1304  *
1305  * This function is called with hbalock held and the caller must post the
1306  * iocb without releasing the lock. If the caller releases the lock,
1307  * iocb slot returned by the function is not guaranteed to be available.
1308  * The function returns pointer to the next available iocb slot if there
1309  * is available slot in the ring, else it returns NULL.
1310  * If the get index of the ring is ahead of the put index, the function
1311  * will post an error attention event to the worker thread to take the
1312  * HBA to offline state.
1313  **/
1314 static IOCB_t *
1315 lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1316 {
1317         struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
1318         uint32_t  max_cmd_idx = pring->numCiocb;
1319         if ((pring->next_cmdidx == pring->cmdidx) &&
1320            (++pring->next_cmdidx >= max_cmd_idx))
1321                 pring->next_cmdidx = 0;
1322
1323         if (unlikely(pring->local_getidx == pring->next_cmdidx)) {
1324
1325                 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
1326
1327                 if (unlikely(pring->local_getidx >= max_cmd_idx)) {
1328                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
1329                                         "0315 Ring %d issue: portCmdGet %d "
1330                                         "is bigger than cmd ring %d\n",
1331                                         pring->ringno,
1332                                         pring->local_getidx, max_cmd_idx);
1333
1334                         phba->link_state = LPFC_HBA_ERROR;
1335                         /*
1336                          * All error attention handlers are posted to
1337                          * worker thread
1338                          */
1339                         phba->work_ha |= HA_ERATT;
1340                         phba->work_hs = HS_FFER3;
1341
1342                         lpfc_worker_wake_up(phba);
1343
1344                         return NULL;
1345                 }
1346
1347                 if (pring->local_getidx == pring->next_cmdidx)
1348                         return NULL;
1349         }
1350
1351         return lpfc_cmd_iocb(phba, pring);
1352 }
1353
1354 /**
1355  * lpfc_sli_next_iotag - Get an iotag for the iocb
1356  * @phba: Pointer to HBA context object.
1357  * @iocbq: Pointer to driver iocb object.
1358  *
1359  * This function gets an iotag for the iocb. If there is no unused iotag and
1360  * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
1361  * array and assigns a new iotag.
1362  * The function returns the allocated iotag if successful, else returns zero.
1363  * Zero is not a valid iotag.
1364  * The caller is not required to hold any lock.
1365  **/
1366 uint16_t
1367 lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1368 {
1369         struct lpfc_iocbq **new_arr;
1370         struct lpfc_iocbq **old_arr;
1371         size_t new_len;
1372         struct lpfc_sli *psli = &phba->sli;
1373         uint16_t iotag;
1374
1375         spin_lock_irq(&phba->hbalock);
1376         iotag = psli->last_iotag;
1377         if(++iotag < psli->iocbq_lookup_len) {
1378                 psli->last_iotag = iotag;
1379                 psli->iocbq_lookup[iotag] = iocbq;
1380                 spin_unlock_irq(&phba->hbalock);
1381                 iocbq->iotag = iotag;
1382                 return iotag;
1383         } else if (psli->iocbq_lookup_len < (0xffff
1384                                            - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
1385                 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
1386                 spin_unlock_irq(&phba->hbalock);
1387                 new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
1388                                   GFP_KERNEL);
1389                 if (new_arr) {
1390                         spin_lock_irq(&phba->hbalock);
1391                         old_arr = psli->iocbq_lookup;
1392                         if (new_len <= psli->iocbq_lookup_len) {
1393                                 /* highly unprobable case */
1394                                 kfree(new_arr);
1395                                 iotag = psli->last_iotag;
1396                                 if(++iotag < psli->iocbq_lookup_len) {
1397                                         psli->last_iotag = iotag;
1398                                         psli->iocbq_lookup[iotag] = iocbq;
1399                                         spin_unlock_irq(&phba->hbalock);
1400                                         iocbq->iotag = iotag;
1401                                         return iotag;
1402                                 }
1403                                 spin_unlock_irq(&phba->hbalock);
1404                                 return 0;
1405                         }
1406                         if (psli->iocbq_lookup)
1407                                 memcpy(new_arr, old_arr,
1408                                        ((psli->last_iotag  + 1) *
1409                                         sizeof (struct lpfc_iocbq *)));
1410                         psli->iocbq_lookup = new_arr;
1411                         psli->iocbq_lookup_len = new_len;
1412                         psli->last_iotag = iotag;
1413                         psli->iocbq_lookup[iotag] = iocbq;
1414                         spin_unlock_irq(&phba->hbalock);
1415                         iocbq->iotag = iotag;
1416                         kfree(old_arr);
1417                         return iotag;
1418                 }
1419         } else
1420                 spin_unlock_irq(&phba->hbalock);
1421
1422         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
1423                         "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
1424                         psli->last_iotag);
1425
1426         return 0;
1427 }
1428
1429 /**
1430  * lpfc_sli_submit_iocb - Submit an iocb to the firmware
1431  * @phba: Pointer to HBA context object.
1432  * @pring: Pointer to driver SLI ring object.
1433  * @iocb: Pointer to iocb slot in the ring.
1434  * @nextiocb: Pointer to driver iocb object which need to be
1435  *            posted to firmware.
1436  *
1437  * This function is called with hbalock held to post a new iocb to
1438  * the firmware. This function copies the new iocb to ring iocb slot and
1439  * updates the ring pointers. It adds the new iocb to txcmplq if there is
1440  * a completion call back for this iocb else the function will free the
1441  * iocb object.
1442  **/
1443 static void
1444 lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1445                 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
1446 {
1447         /*
1448          * Set up an iotag
1449          */
1450         nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
1451
1452
1453         if (pring->ringno == LPFC_ELS_RING) {
1454                 lpfc_debugfs_slow_ring_trc(phba,
1455                         "IOCB cmd ring:   wd4:x%08x wd6:x%08x wd7:x%08x",
1456                         *(((uint32_t *) &nextiocb->iocb) + 4),
1457                         *(((uint32_t *) &nextiocb->iocb) + 6),
1458                         *(((uint32_t *) &nextiocb->iocb) + 7));
1459         }
1460
1461         /*
1462          * Issue iocb command to adapter
1463          */
1464         lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
1465         wmb();
1466         pring->stats.iocb_cmd++;
1467
1468         /*
1469          * If there is no completion routine to call, we can release the
1470          * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
1471          * that have no rsp ring completion, iocb_cmpl MUST be NULL.
1472          */
1473         if (nextiocb->iocb_cmpl)
1474                 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
1475         else
1476                 __lpfc_sli_release_iocbq(phba, nextiocb);
1477
1478         /*
1479          * Let the HBA know what IOCB slot will be the next one the
1480          * driver will put a command into.
1481          */
1482         pring->cmdidx = pring->next_cmdidx;
1483         writel(pring->cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
1484 }
1485
1486 /**
1487  * lpfc_sli_update_full_ring - Update the chip attention register
1488  * @phba: Pointer to HBA context object.
1489  * @pring: Pointer to driver SLI ring object.
1490  *
1491  * The caller is not required to hold any lock for calling this function.
1492  * This function updates the chip attention bits for the ring to inform firmware
1493  * that there are pending work to be done for this ring and requests an
1494  * interrupt when there is space available in the ring. This function is
1495  * called when the driver is unable to post more iocbs to the ring due
1496  * to unavailability of space in the ring.
1497  **/
1498 static void
1499 lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1500 {
1501         int ringno = pring->ringno;
1502
1503         pring->flag |= LPFC_CALL_RING_AVAILABLE;
1504
1505         wmb();
1506
1507         /*
1508          * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
1509          * The HBA will tell us when an IOCB entry is available.
1510          */
1511         writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
1512         readl(phba->CAregaddr); /* flush */
1513
1514         pring->stats.iocb_cmd_full++;
1515 }
1516
1517 /**
1518  * lpfc_sli_update_ring - Update chip attention register
1519  * @phba: Pointer to HBA context object.
1520  * @pring: Pointer to driver SLI ring object.
1521  *
1522  * This function updates the chip attention register bit for the
1523  * given ring to inform HBA that there is more work to be done
1524  * in this ring. The caller is not required to hold any lock.
1525  **/
1526 static void
1527 lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1528 {
1529         int ringno = pring->ringno;
1530
1531         /*
1532          * Tell the HBA that there is work to do in this ring.
1533          */
1534         if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
1535                 wmb();
1536                 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
1537                 readl(phba->CAregaddr); /* flush */
1538         }
1539 }
1540
1541 /**
1542  * lpfc_sli_resume_iocb - Process iocbs in the txq
1543  * @phba: Pointer to HBA context object.
1544  * @pring: Pointer to driver SLI ring object.
1545  *
1546  * This function is called with hbalock held to post pending iocbs
1547  * in the txq to the firmware. This function is called when driver
1548  * detects space available in the ring.
1549  **/
1550 static void
1551 lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1552 {
1553         IOCB_t *iocb;
1554         struct lpfc_iocbq *nextiocb;
1555
1556         /*
1557          * Check to see if:
1558          *  (a) there is anything on the txq to send
1559          *  (b) link is up
1560          *  (c) link attention events can be processed (fcp ring only)
1561          *  (d) IOCB processing is not blocked by the outstanding mbox command.
1562          */
1563         if (pring->txq_cnt &&
1564             lpfc_is_link_up(phba) &&
1565             (pring->ringno != phba->sli.fcp_ring ||
1566              phba->sli.sli_flag & LPFC_PROCESS_LA)) {
1567
1568                 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
1569                        (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
1570                         lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
1571
1572                 if (iocb)
1573                         lpfc_sli_update_ring(phba, pring);
1574                 else
1575                         lpfc_sli_update_full_ring(phba, pring);
1576         }
1577
1578         return;
1579 }
1580
1581 /**
1582  * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
1583  * @phba: Pointer to HBA context object.
1584  * @hbqno: HBQ number.
1585  *
1586  * This function is called with hbalock held to get the next
1587  * available slot for the given HBQ. If there is free slot
1588  * available for the HBQ it will return pointer to the next available
1589  * HBQ entry else it will return NULL.
1590  **/
1591 static struct lpfc_hbq_entry *
1592 lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
1593 {
1594         struct hbq_s *hbqp = &phba->hbqs[hbqno];
1595
1596         if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
1597             ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
1598                 hbqp->next_hbqPutIdx = 0;
1599
1600         if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
1601                 uint32_t raw_index = phba->hbq_get[hbqno];
1602                 uint32_t getidx = le32_to_cpu(raw_index);
1603
1604                 hbqp->local_hbqGetIdx = getidx;
1605
1606                 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
1607                         lpfc_printf_log(phba, KERN_ERR,
1608                                         LOG_SLI | LOG_VPORT,
1609                                         "1802 HBQ %d: local_hbqGetIdx "
1610                                         "%u is > than hbqp->entry_count %u\n",
1611                                         hbqno, hbqp->local_hbqGetIdx,
1612                                         hbqp->entry_count);
1613
1614                         phba->link_state = LPFC_HBA_ERROR;
1615                         return NULL;
1616                 }
1617
1618                 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
1619                         return NULL;
1620         }
1621
1622         return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
1623                         hbqp->hbqPutIdx;
1624 }
1625
1626 /**
1627  * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
1628  * @phba: Pointer to HBA context object.
1629  *
1630  * This function is called with no lock held to free all the
1631  * hbq buffers while uninitializing the SLI interface. It also
1632  * frees the HBQ buffers returned by the firmware but not yet
1633  * processed by the upper layers.
1634  **/
1635 void
1636 lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
1637 {
1638         struct lpfc_dmabuf *dmabuf, *next_dmabuf;
1639         struct hbq_dmabuf *hbq_buf;
1640         unsigned long flags;
1641         int i, hbq_count;
1642         uint32_t hbqno;
1643
1644         hbq_count = lpfc_sli_hbq_count();
1645         /* Return all memory used by all HBQs */
1646         spin_lock_irqsave(&phba->hbalock, flags);
1647         for (i = 0; i < hbq_count; ++i) {
1648                 list_for_each_entry_safe(dmabuf, next_dmabuf,
1649                                 &phba->hbqs[i].hbq_buffer_list, list) {
1650                         hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1651                         list_del(&hbq_buf->dbuf.list);
1652                         (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
1653                 }
1654                 phba->hbqs[i].buffer_count = 0;
1655         }
1656         /* Return all HBQ buffer that are in-fly */
1657         list_for_each_entry_safe(dmabuf, next_dmabuf, &phba->rb_pend_list,
1658                                  list) {
1659                 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1660                 list_del(&hbq_buf->dbuf.list);
1661                 if (hbq_buf->tag == -1) {
1662                         (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1663                                 (phba, hbq_buf);
1664                 } else {
1665                         hbqno = hbq_buf->tag >> 16;
1666                         if (hbqno >= LPFC_MAX_HBQS)
1667                                 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1668                                         (phba, hbq_buf);
1669                         else
1670                                 (phba->hbqs[hbqno].hbq_free_buffer)(phba,
1671                                         hbq_buf);
1672                 }
1673         }
1674
1675         /* Mark the HBQs not in use */
1676         phba->hbq_in_use = 0;
1677         spin_unlock_irqrestore(&phba->hbalock, flags);
1678 }
1679
1680 /**
1681  * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
1682  * @phba: Pointer to HBA context object.
1683  * @hbqno: HBQ number.
1684  * @hbq_buf: Pointer to HBQ buffer.
1685  *
1686  * This function is called with the hbalock held to post a
1687  * hbq buffer to the firmware. If the function finds an empty
1688  * slot in the HBQ, it will post the buffer. The function will return
1689  * pointer to the hbq entry if it successfully post the buffer
1690  * else it will return NULL.
1691  **/
1692 static int
1693 lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
1694                          struct hbq_dmabuf *hbq_buf)
1695 {
1696         return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
1697 }
1698
1699 /**
1700  * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
1701  * @phba: Pointer to HBA context object.
1702  * @hbqno: HBQ number.
1703  * @hbq_buf: Pointer to HBQ buffer.
1704  *
1705  * This function is called with the hbalock held to post a hbq buffer to the
1706  * firmware. If the function finds an empty slot in the HBQ, it will post the
1707  * buffer and place it on the hbq_buffer_list. The function will return zero if
1708  * it successfully post the buffer else it will return an error.
1709  **/
1710 static int
1711 lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
1712                             struct hbq_dmabuf *hbq_buf)
1713 {
1714         struct lpfc_hbq_entry *hbqe;
1715         dma_addr_t physaddr = hbq_buf->dbuf.phys;
1716
1717         /* Get next HBQ entry slot to use */
1718         hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
1719         if (hbqe) {
1720                 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1721
1722                 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1723                 hbqe->bde.addrLow  = le32_to_cpu(putPaddrLow(physaddr));
1724                 hbqe->bde.tus.f.bdeSize = hbq_buf->size;
1725                 hbqe->bde.tus.f.bdeFlags = 0;
1726                 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
1727                 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
1728                                 /* Sync SLIM */
1729                 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
1730                 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
1731                                 /* flush */
1732                 readl(phba->hbq_put + hbqno);
1733                 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
1734                 return 0;
1735         } else
1736                 return -ENOMEM;
1737 }
1738
1739 /**
1740  * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
1741  * @phba: Pointer to HBA context object.
1742  * @hbqno: HBQ number.
1743  * @hbq_buf: Pointer to HBQ buffer.
1744  *
1745  * This function is called with the hbalock held to post an RQE to the SLI4
1746  * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
1747  * the hbq_buffer_list and return zero, otherwise it will return an error.
1748  **/
1749 static int
1750 lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
1751                             struct hbq_dmabuf *hbq_buf)
1752 {
1753         int rc;
1754         struct lpfc_rqe hrqe;
1755         struct lpfc_rqe drqe;
1756
1757         hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
1758         hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
1759         drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
1760         drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
1761         rc = lpfc_sli4_rq_put(phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
1762                               &hrqe, &drqe);
1763         if (rc < 0)
1764                 return rc;
1765         hbq_buf->tag = rc;
1766         list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
1767         return 0;
1768 }
1769
1770 /* HBQ for ELS and CT traffic. */
1771 static struct lpfc_hbq_init lpfc_els_hbq = {
1772         .rn = 1,
1773         .entry_count = 256,
1774         .mask_count = 0,
1775         .profile = 0,
1776         .ring_mask = (1 << LPFC_ELS_RING),
1777         .buffer_count = 0,
1778         .init_count = 40,
1779         .add_count = 40,
1780 };
1781
1782 /* HBQ for the extra ring if needed */
1783 static struct lpfc_hbq_init lpfc_extra_hbq = {
1784         .rn = 1,
1785         .entry_count = 200,
1786         .mask_count = 0,
1787         .profile = 0,
1788         .ring_mask = (1 << LPFC_EXTRA_RING),
1789         .buffer_count = 0,
1790         .init_count = 0,
1791         .add_count = 5,
1792 };
1793
1794 /* Array of HBQs */
1795 struct lpfc_hbq_init *lpfc_hbq_defs[] = {
1796         &lpfc_els_hbq,
1797         &lpfc_extra_hbq,
1798 };
1799
1800 /**
1801  * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
1802  * @phba: Pointer to HBA context object.
1803  * @hbqno: HBQ number.
1804  * @count: Number of HBQ buffers to be posted.
1805  *
1806  * This function is called with no lock held to post more hbq buffers to the
1807  * given HBQ. The function returns the number of HBQ buffers successfully
1808  * posted.
1809  **/
1810 static int
1811 lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
1812 {
1813         uint32_t i, posted = 0;
1814         unsigned long flags;
1815         struct hbq_dmabuf *hbq_buffer;
1816         LIST_HEAD(hbq_buf_list);
1817         if (!phba->hbqs[hbqno].hbq_alloc_buffer)
1818                 return 0;
1819
1820         if ((phba->hbqs[hbqno].buffer_count + count) >
1821             lpfc_hbq_defs[hbqno]->entry_count)
1822                 count = lpfc_hbq_defs[hbqno]->entry_count -
1823                                         phba->hbqs[hbqno].buffer_count;
1824         if (!count)
1825                 return 0;
1826         /* Allocate HBQ entries */
1827         for (i = 0; i < count; i++) {
1828                 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
1829                 if (!hbq_buffer)
1830                         break;
1831                 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
1832         }
1833         /* Check whether HBQ is still in use */
1834         spin_lock_irqsave(&phba->hbalock, flags);
1835         if (!phba->hbq_in_use)
1836                 goto err;
1837         while (!list_empty(&hbq_buf_list)) {
1838                 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1839                                  dbuf.list);
1840                 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
1841                                       (hbqno << 16));
1842                 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
1843                         phba->hbqs[hbqno].buffer_count++;
1844                         posted++;
1845                 } else
1846                         (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1847         }
1848         spin_unlock_irqrestore(&phba->hbalock, flags);
1849         return posted;
1850 err:
1851         spin_unlock_irqrestore(&phba->hbalock, flags);
1852         while (!list_empty(&hbq_buf_list)) {
1853                 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1854                                  dbuf.list);
1855                 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1856         }
1857         return 0;
1858 }
1859
1860 /**
1861  * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
1862  * @phba: Pointer to HBA context object.
1863  * @qno: HBQ number.
1864  *
1865  * This function posts more buffers to the HBQ. This function
1866  * is called with no lock held. The function returns the number of HBQ entries
1867  * successfully allocated.
1868  **/
1869 int
1870 lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
1871 {
1872         if (phba->sli_rev == LPFC_SLI_REV4)
1873                 return 0;
1874         else
1875                 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1876                                          lpfc_hbq_defs[qno]->add_count);
1877 }
1878
1879 /**
1880  * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
1881  * @phba: Pointer to HBA context object.
1882  * @qno:  HBQ queue number.
1883  *
1884  * This function is called from SLI initialization code path with
1885  * no lock held to post initial HBQ buffers to firmware. The
1886  * function returns the number of HBQ entries successfully allocated.
1887  **/
1888 static int
1889 lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
1890 {
1891         if (phba->sli_rev == LPFC_SLI_REV4)
1892                 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1893                                         lpfc_hbq_defs[qno]->entry_count);
1894         else
1895                 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1896                                          lpfc_hbq_defs[qno]->init_count);
1897 }
1898
1899 /**
1900  * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
1901  * @phba: Pointer to HBA context object.
1902  * @hbqno: HBQ number.
1903  *
1904  * This function removes the first hbq buffer on an hbq list and returns a
1905  * pointer to that buffer. If it finds no buffers on the list it returns NULL.
1906  **/
1907 static struct hbq_dmabuf *
1908 lpfc_sli_hbqbuf_get(struct list_head *rb_list)
1909 {
1910         struct lpfc_dmabuf *d_buf;
1911
1912         list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
1913         if (!d_buf)
1914                 return NULL;
1915         return container_of(d_buf, struct hbq_dmabuf, dbuf);
1916 }
1917
1918 /**
1919  * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
1920  * @phba: Pointer to HBA context object.
1921  * @tag: Tag of the hbq buffer.
1922  *
1923  * This function is called with hbalock held. This function searches
1924  * for the hbq buffer associated with the given tag in the hbq buffer
1925  * list. If it finds the hbq buffer, it returns the hbq_buffer other wise
1926  * it returns NULL.
1927  **/
1928 static struct hbq_dmabuf *
1929 lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
1930 {
1931         struct lpfc_dmabuf *d_buf;
1932         struct hbq_dmabuf *hbq_buf;
1933         uint32_t hbqno;
1934
1935         hbqno = tag >> 16;
1936         if (hbqno >= LPFC_MAX_HBQS)
1937                 return NULL;
1938
1939         spin_lock_irq(&phba->hbalock);
1940         list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
1941                 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
1942                 if (hbq_buf->tag == tag) {
1943                         spin_unlock_irq(&phba->hbalock);
1944                         return hbq_buf;
1945                 }
1946         }
1947         spin_unlock_irq(&phba->hbalock);
1948         lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
1949                         "1803 Bad hbq tag. Data: x%x x%x\n",
1950                         tag, phba->hbqs[tag >> 16].buffer_count);
1951         return NULL;
1952 }
1953
1954 /**
1955  * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
1956  * @phba: Pointer to HBA context object.
1957  * @hbq_buffer: Pointer to HBQ buffer.
1958  *
1959  * This function is called with hbalock. This function gives back
1960  * the hbq buffer to firmware. If the HBQ does not have space to
1961  * post the buffer, it will free the buffer.
1962  **/
1963 void
1964 lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
1965 {
1966         uint32_t hbqno;
1967
1968         if (hbq_buffer) {
1969                 hbqno = hbq_buffer->tag >> 16;
1970                 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
1971                         (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1972         }
1973 }
1974
1975 /**
1976  * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
1977  * @mbxCommand: mailbox command code.
1978  *
1979  * This function is called by the mailbox event handler function to verify
1980  * that the completed mailbox command is a legitimate mailbox command. If the
1981  * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
1982  * and the mailbox event handler will take the HBA offline.
1983  **/
1984 static int
1985 lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
1986 {
1987         uint8_t ret;
1988
1989         switch (mbxCommand) {
1990         case MBX_LOAD_SM:
1991         case MBX_READ_NV:
1992         case MBX_WRITE_NV:
1993         case MBX_WRITE_VPARMS:
1994         case MBX_RUN_BIU_DIAG:
1995         case MBX_INIT_LINK:
1996         case MBX_DOWN_LINK:
1997         case MBX_CONFIG_LINK:
1998         case MBX_CONFIG_RING:
1999         case MBX_RESET_RING:
2000         case MBX_READ_CONFIG:
2001         case MBX_READ_RCONFIG:
2002         case MBX_READ_SPARM:
2003         case MBX_READ_STATUS:
2004         case MBX_READ_RPI:
2005         case MBX_READ_XRI:
2006         case MBX_READ_REV:
2007         case MBX_READ_LNK_STAT:
2008         case MBX_REG_LOGIN:
2009         case MBX_UNREG_LOGIN:
2010         case MBX_CLEAR_LA:
2011         case MBX_DUMP_MEMORY:
2012         case MBX_DUMP_CONTEXT:
2013         case MBX_RUN_DIAGS:
2014         case MBX_RESTART:
2015         case MBX_UPDATE_CFG:
2016         case MBX_DOWN_LOAD:
2017         case MBX_DEL_LD_ENTRY:
2018         case MBX_RUN_PROGRAM:
2019         case MBX_SET_MASK:
2020         case MBX_SET_VARIABLE:
2021         case MBX_UNREG_D_ID:
2022         case MBX_KILL_BOARD:
2023         case MBX_CONFIG_FARP:
2024         case MBX_BEACON:
2025         case MBX_LOAD_AREA:
2026         case MBX_RUN_BIU_DIAG64:
2027         case MBX_CONFIG_PORT:
2028         case MBX_READ_SPARM64:
2029         case MBX_READ_RPI64:
2030         case MBX_REG_LOGIN64:
2031         case MBX_READ_TOPOLOGY:
2032         case MBX_WRITE_WWN:
2033         case MBX_SET_DEBUG:
2034         case MBX_LOAD_EXP_ROM:
2035         case MBX_ASYNCEVT_ENABLE:
2036         case MBX_REG_VPI:
2037         case MBX_UNREG_VPI:
2038         case MBX_HEARTBEAT:
2039         case MBX_PORT_CAPABILITIES:
2040         case MBX_PORT_IOV_CONTROL:
2041         case MBX_SLI4_CONFIG:
2042         case MBX_SLI4_REQ_FTRS:
2043         case MBX_REG_FCFI:
2044         case MBX_UNREG_FCFI:
2045         case MBX_REG_VFI:
2046         case MBX_UNREG_VFI:
2047         case MBX_INIT_VPI:
2048         case MBX_INIT_VFI:
2049         case MBX_RESUME_RPI:
2050         case MBX_READ_EVENT_LOG_STATUS:
2051         case MBX_READ_EVENT_LOG:
2052         case MBX_SECURITY_MGMT:
2053         case MBX_AUTH_PORT:
2054                 ret = mbxCommand;
2055                 break;
2056         default:
2057                 ret = MBX_SHUTDOWN;
2058                 break;
2059         }
2060         return ret;
2061 }
2062
2063 /**
2064  * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
2065  * @phba: Pointer to HBA context object.
2066  * @pmboxq: Pointer to mailbox command.
2067  *
2068  * This is completion handler function for mailbox commands issued from
2069  * lpfc_sli_issue_mbox_wait function. This function is called by the
2070  * mailbox event handler function with no lock held. This function
2071  * will wake up thread waiting on the wait queue pointed by context1
2072  * of the mailbox.
2073  **/
2074 void
2075 lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
2076 {
2077         wait_queue_head_t *pdone_q;
2078         unsigned long drvr_flag;
2079
2080         /*
2081          * If pdone_q is empty, the driver thread gave up waiting and
2082          * continued running.
2083          */
2084         pmboxq->mbox_flag |= LPFC_MBX_WAKE;
2085         spin_lock_irqsave(&phba->hbalock, drvr_flag);
2086         pdone_q = (wait_queue_head_t *) pmboxq->context1;
2087         if (pdone_q)
2088                 wake_up_interruptible(pdone_q);
2089         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
2090         return;
2091 }
2092
2093
2094 /**
2095  * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
2096  * @phba: Pointer to HBA context object.
2097  * @pmb: Pointer to mailbox object.
2098  *
2099  * This function is the default mailbox completion handler. It
2100  * frees the memory resources associated with the completed mailbox
2101  * command. If the completed command is a REG_LOGIN mailbox command,
2102  * this function will issue a UREG_LOGIN to re-claim the RPI.
2103  **/
2104 void
2105 lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2106 {
2107         struct lpfc_vport  *vport = pmb->vport;
2108         struct lpfc_dmabuf *mp;
2109         struct lpfc_nodelist *ndlp;
2110         struct Scsi_Host *shost;
2111         uint16_t rpi, vpi;
2112         int rc;
2113
2114         mp = (struct lpfc_dmabuf *) (pmb->context1);
2115
2116         if (mp) {
2117                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2118                 kfree(mp);
2119         }
2120
2121         /*
2122          * If a REG_LOGIN succeeded  after node is destroyed or node
2123          * is in re-discovery driver need to cleanup the RPI.
2124          */
2125         if (!(phba->pport->load_flag & FC_UNLOADING) &&
2126             pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
2127             !pmb->u.mb.mbxStatus) {
2128                 rpi = pmb->u.mb.un.varWords[0];
2129                 vpi = pmb->u.mb.un.varRegLogin.vpi;
2130                 lpfc_unreg_login(phba, vpi, rpi, pmb);
2131                 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
2132                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2133                 if (rc != MBX_NOT_FINISHED)
2134                         return;
2135         }
2136
2137         if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
2138                 !(phba->pport->load_flag & FC_UNLOADING) &&
2139                 !pmb->u.mb.mbxStatus) {
2140                 shost = lpfc_shost_from_vport(vport);
2141                 spin_lock_irq(shost->host_lock);
2142                 vport->vpi_state |= LPFC_VPI_REGISTERED;
2143                 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
2144                 spin_unlock_irq(shost->host_lock);
2145         }
2146
2147         if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
2148                 ndlp = (struct lpfc_nodelist *)pmb->context2;
2149                 lpfc_nlp_put(ndlp);
2150                 pmb->context2 = NULL;
2151         }
2152
2153         /* Check security permission status on INIT_LINK mailbox command */
2154         if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
2155             (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
2156                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
2157                                 "2860 SLI authentication is required "
2158                                 "for INIT_LINK but has not done yet\n");
2159
2160         if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
2161                 lpfc_sli4_mbox_cmd_free(phba, pmb);
2162         else
2163                 mempool_free(pmb, phba->mbox_mem_pool);
2164 }
2165
2166 /**
2167  * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
2168  * @phba: Pointer to HBA context object.
2169  *
2170  * This function is called with no lock held. This function processes all
2171  * the completed mailbox commands and gives it to upper layers. The interrupt
2172  * service routine processes mailbox completion interrupt and adds completed
2173  * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
2174  * Worker thread call lpfc_sli_handle_mb_event, which will return the
2175  * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
2176  * function returns the mailbox commands to the upper layer by calling the
2177  * completion handler function of each mailbox.
2178  **/
2179 int
2180 lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
2181 {
2182         MAILBOX_t *pmbox;
2183         LPFC_MBOXQ_t *pmb;
2184         int rc;
2185         LIST_HEAD(cmplq);
2186
2187         phba->sli.slistat.mbox_event++;
2188
2189         /* Get all completed mailboxe buffers into the cmplq */
2190         spin_lock_irq(&phba->hbalock);
2191         list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
2192         spin_unlock_irq(&phba->hbalock);
2193
2194         /* Get a Mailbox buffer to setup mailbox commands for callback */
2195         do {
2196                 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
2197                 if (pmb == NULL)
2198                         break;
2199
2200                 pmbox = &pmb->u.mb;
2201
2202                 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
2203                         if (pmb->vport) {
2204                                 lpfc_debugfs_disc_trc(pmb->vport,
2205                                         LPFC_DISC_TRC_MBOX_VPORT,
2206                                         "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
2207                                         (uint32_t)pmbox->mbxCommand,
2208                                         pmbox->un.varWords[0],
2209                                         pmbox->un.varWords[1]);
2210                         }
2211                         else {
2212                                 lpfc_debugfs_disc_trc(phba->pport,
2213                                         LPFC_DISC_TRC_MBOX,
2214                                         "MBOX cmpl:       cmd:x%x mb:x%x x%x",
2215                                         (uint32_t)pmbox->mbxCommand,
2216                                         pmbox->un.varWords[0],
2217                                         pmbox->un.varWords[1]);
2218                         }
2219                 }
2220
2221                 /*
2222                  * It is a fatal error if unknown mbox command completion.
2223                  */
2224                 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
2225                     MBX_SHUTDOWN) {
2226                         /* Unknown mailbox command compl */
2227                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
2228                                         "(%d):0323 Unknown Mailbox command "
2229                                         "x%x (x%x/x%x) Cmpl\n",
2230                                         pmb->vport ? pmb->vport->vpi : 0,
2231                                         pmbox->mbxCommand,
2232                                         lpfc_sli_config_mbox_subsys_get(phba,
2233                                                                         pmb),
2234                                         lpfc_sli_config_mbox_opcode_get(phba,
2235                                                                         pmb));
2236                         phba->link_state = LPFC_HBA_ERROR;
2237                         phba->work_hs = HS_FFER3;
2238                         lpfc_handle_eratt(phba);
2239                         continue;
2240                 }
2241
2242                 if (pmbox->mbxStatus) {
2243                         phba->sli.slistat.mbox_stat_err++;
2244                         if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
2245                                 /* Mbox cmd cmpl error - RETRYing */
2246                                 lpfc_printf_log(phba, KERN_INFO,
2247                                         LOG_MBOX | LOG_SLI,
2248                                         "(%d):0305 Mbox cmd cmpl "
2249                                         "error - RETRYing Data: x%x "
2250                                         "(x%x/x%x) x%x x%x x%x\n",
2251                                         pmb->vport ? pmb->vport->vpi : 0,
2252                                         pmbox->mbxCommand,
2253                                         lpfc_sli_config_mbox_subsys_get(phba,
2254                                                                         pmb),
2255                                         lpfc_sli_config_mbox_opcode_get(phba,
2256                                                                         pmb),
2257                                         pmbox->mbxStatus,
2258                                         pmbox->un.varWords[0],
2259                                         pmb->vport->port_state);
2260                                 pmbox->mbxStatus = 0;
2261                                 pmbox->mbxOwner = OWN_HOST;
2262                                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2263                                 if (rc != MBX_NOT_FINISHED)
2264                                         continue;
2265                         }
2266                 }
2267
2268                 /* Mailbox cmd <cmd> Cmpl <cmpl> */
2269                 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
2270                                 "(%d):0307 Mailbox cmd x%x (x%x/x%x) Cmpl x%p "
2271                                 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x\n",
2272                                 pmb->vport ? pmb->vport->vpi : 0,
2273                                 pmbox->mbxCommand,
2274                                 lpfc_sli_config_mbox_subsys_get(phba, pmb),
2275                                 lpfc_sli_config_mbox_opcode_get(phba, pmb),
2276                                 pmb->mbox_cmpl,
2277                                 *((uint32_t *) pmbox),
2278                                 pmbox->un.varWords[0],
2279                                 pmbox->un.varWords[1],
2280                                 pmbox->un.varWords[2],
2281                                 pmbox->un.varWords[3],
2282                                 pmbox->un.varWords[4],
2283                                 pmbox->un.varWords[5],
2284                                 pmbox->un.varWords[6],
2285                                 pmbox->un.varWords[7]);
2286
2287                 if (pmb->mbox_cmpl)
2288                         pmb->mbox_cmpl(phba,pmb);
2289         } while (1);
2290         return 0;
2291 }
2292
2293 /**
2294  * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
2295  * @phba: Pointer to HBA context object.
2296  * @pring: Pointer to driver SLI ring object.
2297  * @tag: buffer tag.
2298  *
2299  * This function is called with no lock held. When QUE_BUFTAG_BIT bit
2300  * is set in the tag the buffer is posted for a particular exchange,
2301  * the function will return the buffer without replacing the buffer.
2302  * If the buffer is for unsolicited ELS or CT traffic, this function
2303  * returns the buffer and also posts another buffer to the firmware.
2304  **/
2305 static struct lpfc_dmabuf *
2306 lpfc_sli_get_buff(struct lpfc_hba *phba,
2307                   struct lpfc_sli_ring *pring,
2308                   uint32_t tag)
2309 {
2310         struct hbq_dmabuf *hbq_entry;
2311
2312         if (tag & QUE_BUFTAG_BIT)
2313                 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
2314         hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
2315         if (!hbq_entry)
2316                 return NULL;
2317         return &hbq_entry->dbuf;
2318 }
2319
2320 /**
2321  * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
2322  * @phba: Pointer to HBA context object.
2323  * @pring: Pointer to driver SLI ring object.
2324  * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
2325  * @fch_r_ctl: the r_ctl for the first frame of the sequence.
2326  * @fch_type: the type for the first frame of the sequence.
2327  *
2328  * This function is called with no lock held. This function uses the r_ctl and
2329  * type of the received sequence to find the correct callback function to call
2330  * to process the sequence.
2331  **/
2332 static int
2333 lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2334                          struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
2335                          uint32_t fch_type)
2336 {
2337         int i;
2338
2339         /* unSolicited Responses */
2340         if (pring->prt[0].profile) {
2341                 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
2342                         (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
2343                                                                         saveq);
2344                 return 1;
2345         }
2346         /* We must search, based on rctl / type
2347            for the right routine */
2348         for (i = 0; i < pring->num_mask; i++) {
2349                 if ((pring->prt[i].rctl == fch_r_ctl) &&
2350                     (pring->prt[i].type == fch_type)) {
2351                         if (pring->prt[i].lpfc_sli_rcv_unsol_event)
2352                                 (pring->prt[i].lpfc_sli_rcv_unsol_event)
2353                                                 (phba, pring, saveq);
2354                         return 1;
2355                 }
2356         }
2357         return 0;
2358 }
2359
2360 /**
2361  * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
2362  * @phba: Pointer to HBA context object.
2363  * @pring: Pointer to driver SLI ring object.
2364  * @saveq: Pointer to the unsolicited iocb.
2365  *
2366  * This function is called with no lock held by the ring event handler
2367  * when there is an unsolicited iocb posted to the response ring by the
2368  * firmware. This function gets the buffer associated with the iocbs
2369  * and calls the event handler for the ring. This function handles both
2370  * qring buffers and hbq buffers.
2371  * When the function returns 1 the caller can free the iocb object otherwise
2372  * upper layer functions will free the iocb objects.
2373  **/
2374 static int
2375 lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2376                             struct lpfc_iocbq *saveq)
2377 {
2378         IOCB_t           * irsp;
2379         WORD5            * w5p;
2380         uint32_t           Rctl, Type;
2381         uint32_t           match;
2382         struct lpfc_iocbq *iocbq;
2383         struct lpfc_dmabuf *dmzbuf;
2384
2385         match = 0;
2386         irsp = &(saveq->iocb);
2387
2388         if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
2389                 if (pring->lpfc_sli_rcv_async_status)
2390                         pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
2391                 else
2392                         lpfc_printf_log(phba,
2393                                         KERN_WARNING,
2394                                         LOG_SLI,
2395                                         "0316 Ring %d handler: unexpected "
2396                                         "ASYNC_STATUS iocb received evt_code "
2397                                         "0x%x\n",
2398                                         pring->ringno,
2399                                         irsp->un.asyncstat.evt_code);
2400                 return 1;
2401         }
2402
2403         if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
2404                 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
2405                 if (irsp->ulpBdeCount > 0) {
2406                         dmzbuf = lpfc_sli_get_buff(phba, pring,
2407                                         irsp->un.ulpWord[3]);
2408                         lpfc_in_buf_free(phba, dmzbuf);
2409                 }
2410
2411                 if (irsp->ulpBdeCount > 1) {
2412                         dmzbuf = lpfc_sli_get_buff(phba, pring,
2413                                         irsp->unsli3.sli3Words[3]);
2414                         lpfc_in_buf_free(phba, dmzbuf);
2415                 }
2416
2417                 if (irsp->ulpBdeCount > 2) {
2418                         dmzbuf = lpfc_sli_get_buff(phba, pring,
2419                                 irsp->unsli3.sli3Words[7]);
2420                         lpfc_in_buf_free(phba, dmzbuf);
2421                 }
2422
2423                 return 1;
2424         }
2425
2426         if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
2427                 if (irsp->ulpBdeCount != 0) {
2428                         saveq->context2 = lpfc_sli_get_buff(phba, pring,
2429                                                 irsp->un.ulpWord[3]);
2430                         if (!saveq->context2)
2431                                 lpfc_printf_log(phba,
2432                                         KERN_ERR,
2433                                         LOG_SLI,
2434                                         "0341 Ring %d Cannot find buffer for "
2435                                         "an unsolicited iocb. tag 0x%x\n",
2436                                         pring->ringno,
2437                                         irsp->un.ulpWord[3]);
2438                 }
2439                 if (irsp->ulpBdeCount == 2) {
2440                         saveq->context3 = lpfc_sli_get_buff(phba, pring,
2441                                                 irsp->unsli3.sli3Words[7]);
2442                         if (!saveq->context3)
2443                                 lpfc_printf_log(phba,
2444                                         KERN_ERR,
2445                                         LOG_SLI,
2446                                         "0342 Ring %d Cannot find buffer for an"
2447                                         " unsolicited iocb. tag 0x%x\n",
2448                                         pring->ringno,
2449                                         irsp->unsli3.sli3Words[7]);
2450                 }
2451                 list_for_each_entry(iocbq, &saveq->list, list) {
2452                         irsp = &(iocbq->iocb);
2453                         if (irsp->ulpBdeCount != 0) {
2454                                 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
2455                                                         irsp->un.ulpWord[3]);
2456                                 if (!iocbq->context2)
2457                                         lpfc_printf_log(phba,
2458                                                 KERN_ERR,
2459                                                 LOG_SLI,
2460                                                 "0343 Ring %d Cannot find "
2461                                                 "buffer for an unsolicited iocb"
2462                                                 ". tag 0x%x\n", pring->ringno,
2463                                                 irsp->un.ulpWord[3]);
2464                         }
2465                         if (irsp->ulpBdeCount == 2) {
2466                                 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
2467                                                 irsp->unsli3.sli3Words[7]);
2468                                 if (!iocbq->context3)
2469                                         lpfc_printf_log(phba,
2470                                                 KERN_ERR,
2471                                                 LOG_SLI,
2472                                                 "0344 Ring %d Cannot find "
2473                                                 "buffer for an unsolicited "
2474                                                 "iocb. tag 0x%x\n",
2475                                                 pring->ringno,
2476                                                 irsp->unsli3.sli3Words[7]);
2477                         }
2478                 }
2479         }
2480         if (irsp->ulpBdeCount != 0 &&
2481             (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
2482              irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
2483                 int found = 0;
2484
2485                 /* search continue save q for same XRI */
2486                 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
2487                         if (iocbq->iocb.unsli3.rcvsli3.ox_id ==
2488                                 saveq->iocb.unsli3.rcvsli3.ox_id) {
2489                                 list_add_tail(&saveq->list, &iocbq->list);
2490                                 found = 1;
2491                                 break;
2492                         }
2493                 }
2494                 if (!found)
2495                         list_add_tail(&saveq->clist,
2496                                       &pring->iocb_continue_saveq);
2497                 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
2498                         list_del_init(&iocbq->clist);
2499                         saveq = iocbq;
2500                         irsp = &(saveq->iocb);
2501                 } else
2502                         return 0;
2503         }
2504         if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
2505             (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
2506             (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
2507                 Rctl = FC_RCTL_ELS_REQ;
2508                 Type = FC_TYPE_ELS;
2509         } else {
2510                 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
2511                 Rctl = w5p->hcsw.Rctl;
2512                 Type = w5p->hcsw.Type;
2513
2514                 /* Firmware Workaround */
2515                 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
2516                         (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
2517                          irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
2518                         Rctl = FC_RCTL_ELS_REQ;
2519                         Type = FC_TYPE_ELS;
2520                         w5p->hcsw.Rctl = Rctl;
2521                         w5p->hcsw.Type = Type;
2522                 }
2523         }
2524
2525         if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
2526                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
2527                                 "0313 Ring %d handler: unexpected Rctl x%x "
2528                                 "Type x%x received\n",
2529                                 pring->ringno, Rctl, Type);
2530
2531         return 1;
2532 }
2533
2534 /**
2535  * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
2536  * @phba: Pointer to HBA context object.
2537  * @pring: Pointer to driver SLI ring object.
2538  * @prspiocb: Pointer to response iocb object.
2539  *
2540  * This function looks up the iocb_lookup table to get the command iocb
2541  * corresponding to the given response iocb using the iotag of the
2542  * response iocb. This function is called with the hbalock held.
2543  * This function returns the command iocb object if it finds the command
2544  * iocb else returns NULL.
2545  **/
2546 static struct lpfc_iocbq *
2547 lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
2548                       struct lpfc_sli_ring *pring,
2549                       struct lpfc_iocbq *prspiocb)
2550 {
2551         struct lpfc_iocbq *cmd_iocb = NULL;
2552         uint16_t iotag;
2553
2554         iotag = prspiocb->iocb.ulpIoTag;
2555
2556         if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2557                 cmd_iocb = phba->sli.iocbq_lookup[iotag];
2558                 list_del_init(&cmd_iocb->list);
2559                 if (cmd_iocb->iocb_flag & LPFC_IO_ON_Q) {
2560                         pring->txcmplq_cnt--;
2561                         cmd_iocb->iocb_flag &= ~LPFC_IO_ON_Q;
2562                 }
2563                 return cmd_iocb;
2564         }
2565
2566         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2567                         "0317 iotag x%x is out off "
2568                         "range: max iotag x%x wd0 x%x\n",
2569                         iotag, phba->sli.last_iotag,
2570                         *(((uint32_t *) &prspiocb->iocb) + 7));
2571         return NULL;
2572 }
2573
2574 /**
2575  * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
2576  * @phba: Pointer to HBA context object.
2577  * @pring: Pointer to driver SLI ring object.
2578  * @iotag: IOCB tag.
2579  *
2580  * This function looks up the iocb_lookup table to get the command iocb
2581  * corresponding to the given iotag. This function is called with the
2582  * hbalock held.
2583  * This function returns the command iocb object if it finds the command
2584  * iocb else returns NULL.
2585  **/
2586 static struct lpfc_iocbq *
2587 lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
2588                              struct lpfc_sli_ring *pring, uint16_t iotag)
2589 {
2590         struct lpfc_iocbq *cmd_iocb;
2591
2592         if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2593                 cmd_iocb = phba->sli.iocbq_lookup[iotag];
2594                 list_del_init(&cmd_iocb->list);
2595                 if (cmd_iocb->iocb_flag & LPFC_IO_ON_Q) {
2596                         cmd_iocb->iocb_flag &= ~LPFC_IO_ON_Q;
2597                         pring->txcmplq_cnt--;
2598                 }
2599                 return cmd_iocb;
2600         }
2601
2602         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2603                         "0372 iotag x%x is out off range: max iotag (x%x)\n",
2604                         iotag, phba->sli.last_iotag);
2605         return NULL;
2606 }
2607
2608 /**
2609  * lpfc_sli_process_sol_iocb - process solicited iocb completion
2610  * @phba: Pointer to HBA context object.
2611  * @pring: Pointer to driver SLI ring object.
2612  * @saveq: Pointer to the response iocb to be processed.
2613  *
2614  * This function is called by the ring event handler for non-fcp
2615  * rings when there is a new response iocb in the response ring.
2616  * The caller is not required to hold any locks. This function
2617  * gets the command iocb associated with the response iocb and
2618  * calls the completion handler for the command iocb. If there
2619  * is no completion handler, the function will free the resources
2620  * associated with command iocb. If the response iocb is for
2621  * an already aborted command iocb, the status of the completion
2622  * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
2623  * This function always returns 1.
2624  **/
2625 static int
2626 lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2627                           struct lpfc_iocbq *saveq)
2628 {
2629         struct lpfc_iocbq *cmdiocbp;
2630         int rc = 1;
2631         unsigned long iflag;
2632
2633         /* Based on the iotag field, get the cmd IOCB from the txcmplq */
2634         spin_lock_irqsave(&phba->hbalock, iflag);
2635         cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
2636         spin_unlock_irqrestore(&phba->hbalock, iflag);
2637
2638         if (cmdiocbp) {
2639                 if (cmdiocbp->iocb_cmpl) {
2640                         /*
2641                          * If an ELS command failed send an event to mgmt
2642                          * application.
2643                          */
2644                         if (saveq->iocb.ulpStatus &&
2645                              (pring->ringno == LPFC_ELS_RING) &&
2646                              (cmdiocbp->iocb.ulpCommand ==
2647                                 CMD_ELS_REQUEST64_CR))
2648                                 lpfc_send_els_failure_event(phba,
2649                                         cmdiocbp, saveq);
2650
2651                         /*
2652                          * Post all ELS completions to the worker thread.
2653                          * All other are passed to the completion callback.
2654                          */
2655                         if (pring->ringno == LPFC_ELS_RING) {
2656                                 if ((phba->sli_rev < LPFC_SLI_REV4) &&
2657                                     (cmdiocbp->iocb_flag &
2658                                                         LPFC_DRIVER_ABORTED)) {
2659                                         spin_lock_irqsave(&phba->hbalock,
2660                                                           iflag);
2661                                         cmdiocbp->iocb_flag &=
2662                                                 ~LPFC_DRIVER_ABORTED;
2663                                         spin_unlock_irqrestore(&phba->hbalock,
2664                                                                iflag);
2665                                         saveq->iocb.ulpStatus =
2666                                                 IOSTAT_LOCAL_REJECT;
2667                                         saveq->iocb.un.ulpWord[4] =
2668                                                 IOERR_SLI_ABORTED;
2669
2670                                         /* Firmware could still be in progress
2671                                          * of DMAing payload, so don't free data
2672                                          * buffer till after a hbeat.
2673                                          */
2674                                         spin_lock_irqsave(&phba->hbalock,
2675                                                           iflag);
2676                                         saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
2677                                         spin_unlock_irqrestore(&phba->hbalock,
2678                                                                iflag);
2679                                 }
2680                                 if (phba->sli_rev == LPFC_SLI_REV4) {
2681                                         if (saveq->iocb_flag &
2682                                             LPFC_EXCHANGE_BUSY) {
2683                                                 /* Set cmdiocb flag for the
2684                                                  * exchange busy so sgl (xri)
2685                                                  * will not be released until
2686                                                  * the abort xri is received
2687                                                  * from hba.
2688                                                  */
2689                                                 spin_lock_irqsave(
2690                                                         &phba->hbalock, iflag);
2691                                                 cmdiocbp->iocb_flag |=
2692                                                         LPFC_EXCHANGE_BUSY;
2693                                                 spin_unlock_irqrestore(
2694                                                         &phba->hbalock, iflag);
2695                                         }
2696                                         if (cmdiocbp->iocb_flag &
2697                                             LPFC_DRIVER_ABORTED) {
2698                                                 /*
2699                                                  * Clear LPFC_DRIVER_ABORTED
2700                                                  * bit in case it was driver
2701                                                  * initiated abort.
2702                                                  */
2703                                                 spin_lock_irqsave(
2704                                                         &phba->hbalock, iflag);
2705                                                 cmdiocbp->iocb_flag &=
2706                                                         ~LPFC_DRIVER_ABORTED;
2707                                                 spin_unlock_irqrestore(
2708                                                         &phba->hbalock, iflag);
2709                                                 cmdiocbp->iocb.ulpStatus =
2710                                                         IOSTAT_LOCAL_REJECT;
2711                                                 cmdiocbp->iocb.un.ulpWord[4] =
2712                                                         IOERR_ABORT_REQUESTED;
2713                                                 /*
2714                                                  * For SLI4, irsiocb contains
2715                                                  * NO_XRI in sli_xritag, it
2716                                                  * shall not affect releasing
2717                                                  * sgl (xri) process.
2718                                                  */
2719                                                 saveq->iocb.ulpStatus =
2720                                                         IOSTAT_LOCAL_REJECT;
2721                                                 saveq->iocb.un.ulpWord[4] =
2722                                                         IOERR_SLI_ABORTED;
2723                                                 spin_lock_irqsave(
2724                                                         &phba->hbalock, iflag);
2725                                                 saveq->iocb_flag |=
2726                                                         LPFC_DELAY_MEM_FREE;
2727                                                 spin_unlock_irqrestore(
2728                                                         &phba->hbalock, iflag);
2729                                         }
2730                                 }
2731                         }
2732                         (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
2733                 } else
2734                         lpfc_sli_release_iocbq(phba, cmdiocbp);
2735         } else {
2736                 /*
2737                  * Unknown initiating command based on the response iotag.
2738                  * This could be the case on the ELS ring because of
2739                  * lpfc_els_abort().
2740                  */
2741                 if (pring->ringno != LPFC_ELS_RING) {
2742                         /*
2743                          * Ring <ringno> handler: unexpected completion IoTag
2744                          * <IoTag>
2745                          */
2746                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
2747                                          "0322 Ring %d handler: "
2748                                          "unexpected completion IoTag x%x "
2749                                          "Data: x%x x%x x%x x%x\n",
2750                                          pring->ringno,
2751                                          saveq->iocb.ulpIoTag,
2752                                          saveq->iocb.ulpStatus,
2753                                          saveq->iocb.un.ulpWord[4],
2754                                          saveq->iocb.ulpCommand,
2755                                          saveq->iocb.ulpContext);
2756                 }
2757         }
2758
2759         return rc;
2760 }
2761
2762 /**
2763  * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
2764  * @phba: Pointer to HBA context object.
2765  * @pring: Pointer to driver SLI ring object.
2766  *
2767  * This function is called from the iocb ring event handlers when
2768  * put pointer is ahead of the get pointer for a ring. This function signal
2769  * an error attention condition to the worker thread and the worker
2770  * thread will transition the HBA to offline state.
2771  **/
2772 static void
2773 lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
2774 {
2775         struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
2776         /*
2777          * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
2778          * rsp ring <portRspMax>
2779          */
2780         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2781                         "0312 Ring %d handler: portRspPut %d "
2782                         "is bigger than rsp ring %d\n",
2783                         pring->ringno, le32_to_cpu(pgp->rspPutInx),
2784                         pring->numRiocb);
2785
2786         phba->link_state = LPFC_HBA_ERROR;
2787
2788         /*
2789          * All error attention handlers are posted to
2790          * worker thread
2791          */
2792         phba->work_ha |= HA_ERATT;
2793         phba->work_hs = HS_FFER3;
2794
2795         lpfc_worker_wake_up(phba);
2796
2797         return;
2798 }
2799
2800 /**
2801  * lpfc_poll_eratt - Error attention polling timer timeout handler
2802  * @ptr: Pointer to address of HBA context object.
2803  *
2804  * This function is invoked by the Error Attention polling timer when the
2805  * timer times out. It will check the SLI Error Attention register for
2806  * possible attention events. If so, it will post an Error Attention event
2807  * and wake up worker thread to process it. Otherwise, it will set up the
2808  * Error Attention polling timer for the next poll.
2809  **/
2810 void lpfc_poll_eratt(unsigned long ptr)
2811 {
2812         struct lpfc_hba *phba;
2813         uint32_t eratt = 0;
2814
2815         phba = (struct lpfc_hba *)ptr;
2816
2817         /* Check chip HA register for error event */
2818         eratt = lpfc_sli_check_eratt(phba);
2819
2820         if (eratt)
2821                 /* Tell the worker thread there is work to do */
2822                 lpfc_worker_wake_up(phba);
2823         else
2824                 /* Restart the timer for next eratt poll */
2825                 mod_timer(&phba->eratt_poll, jiffies +
2826                                         HZ * LPFC_ERATT_POLL_INTERVAL);
2827         return;
2828 }
2829
2830
2831 /**
2832  * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
2833  * @phba: Pointer to HBA context object.
2834  * @pring: Pointer to driver SLI ring object.
2835  * @mask: Host attention register mask for this ring.
2836  *
2837  * This function is called from the interrupt context when there is a ring
2838  * event for the fcp ring. The caller does not hold any lock.
2839  * The function processes each response iocb in the response ring until it
2840  * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
2841  * LE bit set. The function will call the completion handler of the command iocb
2842  * if the response iocb indicates a completion for a command iocb or it is
2843  * an abort completion. The function will call lpfc_sli_process_unsol_iocb
2844  * function if this is an unsolicited iocb.
2845  * This routine presumes LPFC_FCP_RING handling and doesn't bother
2846  * to check it explicitly.
2847  */
2848 int
2849 lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
2850                                 struct lpfc_sli_ring *pring, uint32_t mask)
2851 {
2852         struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
2853         IOCB_t *irsp = NULL;
2854         IOCB_t *entry = NULL;
2855         struct lpfc_iocbq *cmdiocbq = NULL;
2856         struct lpfc_iocbq rspiocbq;
2857         uint32_t status;
2858         uint32_t portRspPut, portRspMax;
2859         int rc = 1;
2860         lpfc_iocb_type type;
2861         unsigned long iflag;
2862         uint32_t rsp_cmpl = 0;
2863
2864         spin_lock_irqsave(&phba->hbalock, iflag);
2865         pring->stats.iocb_event++;
2866
2867         /*
2868          * The next available response entry should never exceed the maximum
2869          * entries.  If it does, treat it as an adapter hardware error.
2870          */
2871         portRspMax = pring->numRiocb;
2872         portRspPut = le32_to_cpu(pgp->rspPutInx);
2873         if (unlikely(portRspPut >= portRspMax)) {
2874                 lpfc_sli_rsp_pointers_error(phba, pring);
2875                 spin_unlock_irqrestore(&phba->hbalock, iflag);
2876                 return 1;
2877         }
2878         if (phba->fcp_ring_in_use) {
2879                 spin_unlock_irqrestore(&phba->hbalock, iflag);
2880                 return 1;
2881         } else
2882                 phba->fcp_ring_in_use = 1;
2883
2884         rmb();
2885         while (pring->rspidx != portRspPut) {
2886                 /*
2887                  * Fetch an entry off the ring and copy it into a local data
2888                  * structure.  The copy involves a byte-swap since the
2889                  * network byte order and pci byte orders are different.
2890                  */
2891                 entry = lpfc_resp_iocb(phba, pring);
2892                 phba->last_completion_time = jiffies;
2893
2894                 if (++pring->rspidx >= portRspMax)
2895                         pring->rspidx = 0;
2896
2897                 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
2898                                       (uint32_t *) &rspiocbq.iocb,
2899                                       phba->iocb_rsp_size);
2900                 INIT_LIST_HEAD(&(rspiocbq.list));
2901                 irsp = &rspiocbq.iocb;
2902
2903                 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
2904                 pring->stats.iocb_rsp++;
2905                 rsp_cmpl++;
2906
2907                 if (unlikely(irsp->ulpStatus)) {
2908                         /*
2909                          * If resource errors reported from HBA, reduce
2910                          * queuedepths of the SCSI device.
2911                          */
2912                         if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
2913                                 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
2914                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
2915                                 phba->lpfc_rampdown_queue_depth(phba);
2916                                 spin_lock_irqsave(&phba->hbalock, iflag);
2917                         }
2918
2919                         /* Rsp ring <ringno> error: IOCB */
2920                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
2921                                         "0336 Rsp Ring %d error: IOCB Data: "
2922                                         "x%x x%x x%x x%x x%x x%x x%x x%x\n",
2923                                         pring->ringno,
2924                                         irsp->un.ulpWord[0],
2925                                         irsp->un.ulpWord[1],
2926                                         irsp->un.ulpWord[2],
2927                                         irsp->un.ulpWord[3],
2928                                         irsp->un.ulpWord[4],
2929                                         irsp->un.ulpWord[5],
2930                                         *(uint32_t *)&irsp->un1,
2931                                         *((uint32_t *)&irsp->un1 + 1));
2932                 }
2933
2934                 switch (type) {
2935                 case LPFC_ABORT_IOCB:
2936                 case LPFC_SOL_IOCB:
2937                         /*
2938                          * Idle exchange closed via ABTS from port.  No iocb
2939                          * resources need to be recovered.
2940                          */
2941                         if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
2942                                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
2943                                                 "0333 IOCB cmd 0x%x"
2944                                                 " processed. Skipping"
2945                                                 " completion\n",
2946                                                 irsp->ulpCommand);
2947                                 break;
2948                         }
2949
2950                         cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
2951                                                          &rspiocbq);
2952                         if (unlikely(!cmdiocbq))
2953                                 break;
2954                         if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
2955                                 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
2956                         if (cmdiocbq->iocb_cmpl) {
2957                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
2958                                 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
2959                                                       &rspiocbq);
2960                                 spin_lock_irqsave(&phba->hbalock, iflag);
2961                         }
2962                         break;
2963                 case LPFC_UNSOL_IOCB:
2964                         spin_unlock_irqrestore(&phba->hbalock, iflag);
2965                         lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
2966                         spin_lock_irqsave(&phba->hbalock, iflag);
2967                         break;
2968                 default:
2969                         if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
2970                                 char adaptermsg[LPFC_MAX_ADPTMSG];
2971                                 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
2972                                 memcpy(&adaptermsg[0], (uint8_t *) irsp,
2973                                        MAX_MSG_DATA);
2974                                 dev_warn(&((phba->pcidev)->dev),
2975                                          "lpfc%d: %s\n",
2976                                          phba->brd_no, adaptermsg);
2977                         } else {
2978                                 /* Unknown IOCB command */
2979                                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2980                                                 "0334 Unknown IOCB command "
2981                                                 "Data: x%x, x%x x%x x%x x%x\n",
2982                                                 type, irsp->ulpCommand,
2983                                                 irsp->ulpStatus,
2984                                                 irsp->ulpIoTag,
2985                                                 irsp->ulpContext);
2986                         }
2987                         break;
2988                 }
2989
2990                 /*
2991                  * The response IOCB has been processed.  Update the ring
2992                  * pointer in SLIM.  If the port response put pointer has not
2993                  * been updated, sync the pgp->rspPutInx and fetch the new port
2994                  * response put pointer.
2995                  */
2996                 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
2997
2998                 if (pring->rspidx == portRspPut)
2999                         portRspPut = le32_to_cpu(pgp->rspPutInx);
3000         }
3001
3002         if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
3003                 pring->stats.iocb_rsp_full++;
3004                 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3005                 writel(status, phba->CAregaddr);
3006                 readl(phba->CAregaddr);
3007         }
3008         if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3009                 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3010                 pring->stats.iocb_cmd_empty++;
3011
3012                 /* Force update of the local copy of cmdGetInx */
3013                 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
3014                 lpfc_sli_resume_iocb(phba, pring);
3015
3016                 if ((pring->lpfc_sli_cmd_available))
3017                         (pring->lpfc_sli_cmd_available) (phba, pring);
3018
3019         }
3020
3021         phba->fcp_ring_in_use = 0;
3022         spin_unlock_irqrestore(&phba->hbalock, iflag);
3023         return rc;
3024 }
3025
3026 /**
3027  * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
3028  * @phba: Pointer to HBA context object.
3029  * @pring: Pointer to driver SLI ring object.
3030  * @rspiocbp: Pointer to driver response IOCB object.
3031  *
3032  * This function is called from the worker thread when there is a slow-path
3033  * response IOCB to process. This function chains all the response iocbs until
3034  * seeing the iocb with the LE bit set. The function will call
3035  * lpfc_sli_process_sol_iocb function if the response iocb indicates a
3036  * completion of a command iocb. The function will call the
3037  * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
3038  * The function frees the resources or calls the completion handler if this
3039  * iocb is an abort completion. The function returns NULL when the response
3040  * iocb has the LE bit set and all the chained iocbs are processed, otherwise
3041  * this function shall chain the iocb on to the iocb_continueq and return the
3042  * response iocb passed in.
3043  **/
3044 static struct lpfc_iocbq *
3045 lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3046                         struct lpfc_iocbq *rspiocbp)
3047 {
3048         struct lpfc_iocbq *saveq;
3049         struct lpfc_iocbq *cmdiocbp;
3050         struct lpfc_iocbq *next_iocb;
3051         IOCB_t *irsp = NULL;
3052         uint32_t free_saveq;
3053         uint8_t iocb_cmd_type;
3054         lpfc_iocb_type type;
3055         unsigned long iflag;
3056         int rc;
3057
3058         spin_lock_irqsave(&phba->hbalock, iflag);
3059         /* First add the response iocb to the countinueq list */
3060         list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
3061         pring->iocb_continueq_cnt++;
3062
3063         /* Now, determine whether the list is completed for processing */
3064         irsp = &rspiocbp->iocb;
3065         if (irsp->ulpLe) {
3066                 /*
3067                  * By default, the driver expects to free all resources
3068                  * associated with this iocb completion.
3069                  */
3070                 free_saveq = 1;
3071                 saveq = list_get_first(&pring->iocb_continueq,
3072                                        struct lpfc_iocbq, list);
3073                 irsp = &(saveq->iocb);
3074                 list_del_init(&pring->iocb_continueq);
3075                 pring->iocb_continueq_cnt = 0;
3076
3077                 pring->stats.iocb_rsp++;
3078
3079                 /*
3080                  * If resource errors reported from HBA, reduce
3081                  * queuedepths of the SCSI device.
3082                  */
3083                 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
3084                     (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
3085                         spin_unlock_irqrestore(&phba->hbalock, iflag);
3086                         phba->lpfc_rampdown_queue_depth(phba);
3087                         spin_lock_irqsave(&phba->hbalock, iflag);
3088                 }
3089
3090                 if (irsp->ulpStatus) {
3091                         /* Rsp ring <ringno> error: IOCB */
3092                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3093                                         "0328 Rsp Ring %d error: "
3094                                         "IOCB Data: "
3095                                         "x%x x%x x%x x%x "
3096                                         "x%x x%x x%x x%x "
3097                                         "x%x x%x x%x x%x "
3098                                         "x%x x%x x%x x%x\n",
3099                                         pring->ringno,
3100                                         irsp->un.ulpWord[0],
3101                                         irsp->un.ulpWord[1],
3102                                         irsp->un.ulpWord[2],
3103                                         irsp->un.ulpWord[3],
3104                                         irsp->un.ulpWord[4],
3105                                         irsp->un.ulpWord[5],
3106                                         *(((uint32_t *) irsp) + 6),
3107                                         *(((uint32_t *) irsp) + 7),
3108                                         *(((uint32_t *) irsp) + 8),
3109                                         *(((uint32_t *) irsp) + 9),
3110                                         *(((uint32_t *) irsp) + 10),
3111                                         *(((uint32_t *) irsp) + 11),
3112                                         *(((uint32_t *) irsp) + 12),
3113                                         *(((uint32_t *) irsp) + 13),
3114                                         *(((uint32_t *) irsp) + 14),
3115                                         *(((uint32_t *) irsp) + 15));
3116                 }
3117
3118                 /*
3119                  * Fetch the IOCB command type and call the correct completion
3120                  * routine. Solicited and Unsolicited IOCBs on the ELS ring
3121                  * get freed back to the lpfc_iocb_list by the discovery
3122                  * kernel thread.
3123                  */
3124                 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
3125                 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
3126                 switch (type) {
3127                 case LPFC_SOL_IOCB:
3128                         spin_unlock_irqrestore(&phba->hbalock, iflag);
3129                         rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
3130                         spin_lock_irqsave(&phba->hbalock, iflag);
3131                         break;
3132
3133                 case LPFC_UNSOL_IOCB:
3134                         spin_unlock_irqrestore(&phba->hbalock, iflag);
3135                         rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
3136                         spin_lock_irqsave(&phba->hbalock, iflag);
3137                         if (!rc)
3138                                 free_saveq = 0;
3139                         break;
3140
3141                 case LPFC_ABORT_IOCB:
3142                         cmdiocbp = NULL;
3143                         if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
3144                                 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
3145                                                                  saveq);
3146                         if (cmdiocbp) {
3147                                 /* Call the specified completion routine */
3148                                 if (cmdiocbp->iocb_cmpl) {
3149                                         spin_unlock_irqrestore(&phba->hbalock,
3150                                                                iflag);
3151                                         (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
3152                                                               saveq);
3153                                         spin_lock_irqsave(&phba->hbalock,
3154                                                           iflag);
3155                                 } else
3156                                         __lpfc_sli_release_iocbq(phba,
3157                                                                  cmdiocbp);
3158                         }
3159                         break;
3160
3161                 case LPFC_UNKNOWN_IOCB:
3162                         if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3163                                 char adaptermsg[LPFC_MAX_ADPTMSG];
3164                                 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3165                                 memcpy(&adaptermsg[0], (uint8_t *)irsp,
3166                                        MAX_MSG_DATA);
3167                                 dev_warn(&((phba->pcidev)->dev),
3168                                          "lpfc%d: %s\n",
3169                                          phba->brd_no, adaptermsg);
3170                         } else {
3171                                 /* Unknown IOCB command */
3172                                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3173                                                 "0335 Unknown IOCB "
3174                                                 "command Data: x%x "
3175                                                 "x%x x%x x%x\n",
3176                                                 irsp->ulpCommand,
3177                                                 irsp->ulpStatus,
3178                                                 irsp->ulpIoTag,
3179                                                 irsp->ulpContext);
3180                         }
3181                         break;
3182                 }
3183
3184                 if (free_saveq) {
3185                         list_for_each_entry_safe(rspiocbp, next_iocb,
3186                                                  &saveq->list, list) {
3187                                 list_del(&rspiocbp->list);
3188                                 __lpfc_sli_release_iocbq(phba, rspiocbp);
3189                         }
3190                         __lpfc_sli_release_iocbq(phba, saveq);
3191                 }
3192                 rspiocbp = NULL;
3193         }
3194         spin_unlock_irqrestore(&phba->hbalock, iflag);
3195         return rspiocbp;
3196 }
3197
3198 /**
3199  * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
3200  * @phba: Pointer to HBA context object.
3201  * @pring: Pointer to driver SLI ring object.
3202  * @mask: Host attention register mask for this ring.
3203  *
3204  * This routine wraps the actual slow_ring event process routine from the
3205  * API jump table function pointer from the lpfc_hba struct.
3206  **/
3207 void
3208 lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
3209                                 struct lpfc_sli_ring *pring, uint32_t mask)
3210 {
3211         phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
3212 }
3213
3214 /**
3215  * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
3216  * @phba: Pointer to HBA context object.
3217  * @pring: Pointer to driver SLI ring object.
3218  * @mask: Host attention register mask for this ring.
3219  *
3220  * This function is called from the worker thread when there is a ring event
3221  * for non-fcp rings. The caller does not hold any lock. The function will
3222  * remove each response iocb in the response ring and calls the handle
3223  * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3224  **/
3225 static void
3226 lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
3227                                    struct lpfc_sli_ring *pring, uint32_t mask)
3228 {
3229         struct lpfc_pgp *pgp;
3230         IOCB_t *entry;
3231         IOCB_t *irsp = NULL;
3232         struct lpfc_iocbq *rspiocbp = NULL;
3233         uint32_t portRspPut, portRspMax;
3234         unsigned long iflag;
3235         uint32_t status;
3236
3237         pgp = &phba->port_gp[pring->ringno];
3238         spin_lock_irqsave(&phba->hbalock, iflag);
3239         pring->stats.iocb_event++;
3240
3241         /*
3242          * The next available response entry should never exceed the maximum
3243          * entries.  If it does, treat it as an adapter hardware error.
3244          */
3245         portRspMax = pring->numRiocb;
3246         portRspPut = le32_to_cpu(pgp->rspPutInx);
3247         if (portRspPut >= portRspMax) {
3248                 /*
3249                  * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
3250                  * rsp ring <portRspMax>
3251                  */
3252                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3253                                 "0303 Ring %d handler: portRspPut %d "
3254                                 "is bigger than rsp ring %d\n",
3255                                 pring->ringno, portRspPut, portRspMax);
3256
3257                 phba->link_state = LPFC_HBA_ERROR;
3258                 spin_unlock_irqrestore(&phba->hbalock, iflag);
3259
3260                 phba->work_hs = HS_FFER3;
3261                 lpfc_handle_eratt(phba);
3262
3263                 return;
3264         }
3265
3266         rmb();
3267         while (pring->rspidx != portRspPut) {
3268                 /*
3269                  * Build a completion list and call the appropriate handler.
3270                  * The process is to get the next available response iocb, get
3271                  * a free iocb from the list, copy the response data into the
3272                  * free iocb, insert to the continuation list, and update the
3273                  * next response index to slim.  This process makes response
3274                  * iocb's in the ring available to DMA as fast as possible but
3275                  * pays a penalty for a copy operation.  Since the iocb is
3276                  * only 32 bytes, this penalty is considered small relative to
3277                  * the PCI reads for register values and a slim write.  When
3278                  * the ulpLe field is set, the entire Command has been
3279                  * received.
3280                  */
3281                 entry = lpfc_resp_iocb(phba, pring);
3282
3283                 phba->last_completion_time = jiffies;
3284                 rspiocbp = __lpfc_sli_get_iocbq(phba);
3285                 if (rspiocbp == NULL) {
3286                         printk(KERN_ERR "%s: out of buffers! Failing "
3287                                "completion.\n", __func__);
3288                         break;
3289                 }
3290
3291                 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
3292                                       phba->iocb_rsp_size);
3293                 irsp = &rspiocbp->iocb;
3294
3295                 if (++pring->rspidx >= portRspMax)
3296                         pring->rspidx = 0;
3297
3298                 if (pring->ringno == LPFC_ELS_RING) {
3299                         lpfc_debugfs_slow_ring_trc(phba,
3300                         "IOCB rsp ring:   wd4:x%08x wd6:x%08x wd7:x%08x",
3301                                 *(((uint32_t *) irsp) + 4),
3302                                 *(((uint32_t *) irsp) + 6),
3303                                 *(((uint32_t *) irsp) + 7));
3304                 }
3305
3306                 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
3307
3308                 spin_unlock_irqrestore(&phba->hbalock, iflag);
3309                 /* Handle the response IOCB */
3310                 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
3311                 spin_lock_irqsave(&phba->hbalock, iflag);
3312
3313                 /*
3314                  * If the port response put pointer has not been updated, sync
3315                  * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
3316                  * response put pointer.
3317                  */
3318                 if (pring->rspidx == portRspPut) {
3319                         portRspPut = le32_to_cpu(pgp->rspPutInx);
3320                 }
3321         } /* while (pring->rspidx != portRspPut) */
3322
3323         if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
3324                 /* At least one response entry has been freed */
3325                 pring->stats.iocb_rsp_full++;
3326                 /* SET RxRE_RSP in Chip Att register */
3327                 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3328                 writel(status, phba->CAregaddr);
3329                 readl(phba->CAregaddr); /* flush */
3330         }
3331         if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3332                 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3333                 pring->stats.iocb_cmd_empty++;
3334
3335                 /* Force update of the local copy of cmdGetInx */
3336                 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
3337                 lpfc_sli_resume_iocb(phba, pring);
3338
3339                 if ((pring->lpfc_sli_cmd_available))
3340                         (pring->lpfc_sli_cmd_available) (phba, pring);
3341
3342         }
3343
3344         spin_unlock_irqrestore(&phba->hbalock, iflag);
3345         return;
3346 }
3347
3348 /**
3349  * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
3350  * @phba: Pointer to HBA context object.
3351  * @pring: Pointer to driver SLI ring object.
3352  * @mask: Host attention register mask for this ring.
3353  *
3354  * This function is called from the worker thread when there is a pending
3355  * ELS response iocb on the driver internal slow-path response iocb worker
3356  * queue. The caller does not hold any lock. The function will remove each
3357  * response iocb from the response worker queue and calls the handle
3358  * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3359  **/
3360 static void
3361 lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
3362                                    struct lpfc_sli_ring *pring, uint32_t mask)
3363 {
3364         struct lpfc_iocbq *irspiocbq;
3365         struct hbq_dmabuf *dmabuf;
3366         struct lpfc_cq_event *cq_event;
3367         unsigned long iflag;
3368
3369         spin_lock_irqsave(&phba->hbalock, iflag);
3370         phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
3371         spin_unlock_irqrestore(&phba->hbalock, iflag);
3372         while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
3373                 /* Get the response iocb from the head of work queue */
3374                 spin_lock_irqsave(&phba->hbalock, iflag);
3375                 list_remove_head(&phba->sli4_hba.sp_queue_event,
3376                                  cq_event, struct lpfc_cq_event, list);
3377                 spin_unlock_irqrestore(&phba->hbalock, iflag);
3378
3379                 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
3380                 case CQE_CODE_COMPL_WQE:
3381                         irspiocbq = container_of(cq_event, struct lpfc_iocbq,
3382                                                  cq_event);
3383                         /* Translate ELS WCQE to response IOCBQ */
3384                         irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
3385                                                                    irspiocbq);
3386                         if (irspiocbq)
3387                                 lpfc_sli_sp_handle_rspiocb(phba, pring,
3388                                                            irspiocbq);
3389                         break;
3390                 case CQE_CODE_RECEIVE:
3391                 case CQE_CODE_RECEIVE_V1:
3392                         dmabuf = container_of(cq_event, struct hbq_dmabuf,
3393                                               cq_event);
3394                         lpfc_sli4_handle_received_buffer(phba, dmabuf);
3395                         break;
3396                 default:
3397                         break;
3398                 }
3399         }
3400 }
3401
3402 /**
3403  * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
3404  * @phba: Pointer to HBA context object.
3405  * @pring: Pointer to driver SLI ring object.
3406  *
3407  * This function aborts all iocbs in the given ring and frees all the iocb
3408  * objects in txq. This function issues an abort iocb for all the iocb commands
3409  * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3410  * the return of this function. The caller is not required to hold any locks.
3411  **/
3412 void
3413 lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3414 {
3415         LIST_HEAD(completions);
3416         struct lpfc_iocbq *iocb, *next_iocb;
3417
3418         if (pring->ringno == LPFC_ELS_RING) {
3419                 lpfc_fabric_abort_hba(phba);
3420         }
3421
3422         /* Error everything on txq and txcmplq
3423          * First do the txq.
3424          */
3425         spin_lock_irq(&phba->hbalock);
3426         list_splice_init(&pring->txq, &completions);
3427         pring->txq_cnt = 0;
3428
3429         /* Next issue ABTS for everything on the txcmplq */
3430         list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3431                 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3432
3433         spin_unlock_irq(&phba->hbalock);
3434
3435         /* Cancel all the IOCBs from the completions list */
3436         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3437                               IOERR_SLI_ABORTED);
3438 }
3439
3440 /**
3441  * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
3442  * @phba: Pointer to HBA context object.
3443  *
3444  * This function flushes all iocbs in the fcp ring and frees all the iocb
3445  * objects in txq and txcmplq. This function will not issue abort iocbs
3446  * for all the iocb commands in txcmplq, they will just be returned with
3447  * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3448  * slot has been permanently disabled.
3449  **/
3450 void
3451 lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
3452 {
3453         LIST_HEAD(txq);
3454         LIST_HEAD(txcmplq);
3455         struct lpfc_sli *psli = &phba->sli;
3456         struct lpfc_sli_ring  *pring;
3457
3458         /* Currently, only one fcp ring */
3459         pring = &psli->ring[psli->fcp_ring];
3460
3461         spin_lock_irq(&phba->hbalock);
3462         /* Retrieve everything on txq */
3463         list_splice_init(&pring->txq, &txq);
3464         pring->txq_cnt = 0;
3465
3466         /* Retrieve everything on the txcmplq */
3467         list_splice_init(&pring->txcmplq, &txcmplq);
3468         pring->txcmplq_cnt = 0;
3469         spin_unlock_irq(&phba->hbalock);
3470
3471         /* Flush the txq */
3472         lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
3473                               IOERR_SLI_DOWN);
3474
3475         /* Flush the txcmpq */
3476         lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
3477                               IOERR_SLI_DOWN);
3478 }
3479
3480 /**
3481  * lpfc_sli_brdready_s3 - Check for sli3 host ready status
3482  * @phba: Pointer to HBA context object.
3483  * @mask: Bit mask to be checked.
3484  *
3485  * This function reads the host status register and compares
3486  * with the provided bit mask to check if HBA completed
3487  * the restart. This function will wait in a loop for the
3488  * HBA to complete restart. If the HBA does not restart within
3489  * 15 iterations, the function will reset the HBA again. The
3490  * function returns 1 when HBA fail to restart otherwise returns
3491  * zero.
3492  **/
3493 static int
3494 lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
3495 {
3496         uint32_t status;
3497         int i = 0;
3498         int retval = 0;
3499
3500         /* Read the HBA Host Status Register */
3501         if (lpfc_readl(phba->HSregaddr, &status))
3502                 return 1;
3503
3504         /*
3505          * Check status register every 100ms for 5 retries, then every
3506          * 500ms for 5, then every 2.5 sec for 5, then reset board and
3507          * every 2.5 sec for 4.
3508          * Break our of the loop if errors occurred during init.
3509          */
3510         while (((status & mask) != mask) &&
3511                !(status & HS_FFERM) &&
3512                i++ < 20) {
3513
3514                 if (i <= 5)
3515                         msleep(10);
3516                 else if (i <= 10)
3517                         msleep(500);
3518                 else
3519                         msleep(2500);
3520
3521                 if (i == 15) {
3522                                 /* Do post */
3523                         phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3524                         lpfc_sli_brdrestart(phba);
3525                 }
3526                 /* Read the HBA Host Status Register */
3527                 if (lpfc_readl(phba->HSregaddr, &status)) {
3528                         retval = 1;
3529                         break;
3530                 }
3531         }
3532
3533         /* Check to see if any errors occurred during init */
3534         if ((status & HS_FFERM) || (i >= 20)) {
3535                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3536                                 "2751 Adapter failed to restart, "
3537                                 "status reg x%x, FW Data: A8 x%x AC x%x\n",
3538                                 status,
3539                                 readl(phba->MBslimaddr + 0xa8),
3540                                 readl(phba->MBslimaddr + 0xac));
3541                 phba->link_state = LPFC_HBA_ERROR;
3542                 retval = 1;
3543         }
3544
3545         return retval;
3546 }
3547
3548 /**
3549  * lpfc_sli_brdready_s4 - Check for sli4 host ready status
3550  * @phba: Pointer to HBA context object.
3551  * @mask: Bit mask to be checked.
3552  *
3553  * This function checks the host status register to check if HBA is
3554  * ready. This function will wait in a loop for the HBA to be ready
3555  * If the HBA is not ready , the function will will reset the HBA PCI
3556  * function again. The function returns 1 when HBA fail to be ready
3557  * otherwise returns zero.
3558  **/
3559 static int
3560 lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
3561 {
3562         uint32_t status;
3563         int retval = 0;
3564
3565         /* Read the HBA Host Status Register */
3566         status = lpfc_sli4_post_status_check(phba);
3567
3568         if (status) {
3569                 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3570                 lpfc_sli_brdrestart(phba);
3571                 status = lpfc_sli4_post_status_check(phba);
3572         }
3573
3574         /* Check to see if any errors occurred during init */
3575         if (status) {
3576                 phba->link_state = LPFC_HBA_ERROR;
3577                 retval = 1;
3578         } else
3579                 phba->sli4_hba.intr_enable = 0;
3580
3581         return retval;
3582 }
3583
3584 /**
3585  * lpfc_sli_brdready - Wrapper func for checking the hba readyness
3586  * @phba: Pointer to HBA context object.
3587  * @mask: Bit mask to be checked.
3588  *
3589  * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
3590  * from the API jump table function pointer from the lpfc_hba struct.
3591  **/
3592 int
3593 lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
3594 {
3595         return phba->lpfc_sli_brdready(phba, mask);
3596 }
3597
3598 #define BARRIER_TEST_PATTERN (0xdeadbeef)
3599
3600 /**
3601  * lpfc_reset_barrier - Make HBA ready for HBA reset
3602  * @phba: Pointer to HBA context object.
3603  *
3604  * This function is called before resetting an HBA. This function is called
3605  * with hbalock held and requests HBA to quiesce DMAs before a reset.
3606  **/
3607 void lpfc_reset_barrier(struct lpfc_hba *phba)
3608 {
3609         uint32_t __iomem *resp_buf;
3610         uint32_t __iomem *mbox_buf;
3611         volatile uint32_t mbox;
3612         uint32_t hc_copy, ha_copy, resp_data;
3613         int  i;
3614         uint8_t hdrtype;
3615
3616         pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
3617         if (hdrtype != 0x80 ||
3618             (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
3619              FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
3620                 return;
3621
3622         /*
3623          * Tell the other part of the chip to suspend temporarily all
3624          * its DMA activity.
3625          */
3626         resp_buf = phba->MBslimaddr;
3627
3628         /* Disable the error attention */
3629         if (lpfc_readl(phba->HCregaddr, &hc_copy))
3630                 return;
3631         writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
3632         readl(phba->HCregaddr); /* flush */
3633         phba->link_flag |= LS_IGNORE_ERATT;
3634
3635         if (lpfc_readl(phba->HAregaddr, &ha_copy))
3636                 return;
3637         if (ha_copy & HA_ERATT) {
3638                 /* Clear Chip error bit */
3639                 writel(HA_ERATT, phba->HAregaddr);
3640                 phba->pport->stopped = 1;
3641         }
3642
3643         mbox = 0;
3644         ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
3645         ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
3646
3647         writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
3648         mbox_buf = phba->MBslimaddr;
3649         writel(mbox, mbox_buf);
3650
3651         for (i = 0; i < 50; i++) {
3652                 if (lpfc_readl((resp_buf + 1), &resp_data))
3653                         return;
3654                 if (resp_data != ~(BARRIER_TEST_PATTERN))
3655                         mdelay(1);
3656                 else
3657                         break;
3658         }
3659         resp_data = 0;
3660         if (lpfc_readl((resp_buf + 1), &resp_data))
3661                 return;
3662         if (resp_data  != ~(BARRIER_TEST_PATTERN)) {
3663                 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
3664                     phba->pport->stopped)
3665                         goto restore_hc;
3666                 else
3667                         goto clear_errat;
3668         }
3669
3670         ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
3671         resp_data = 0;
3672         for (i = 0; i < 500; i++) {
3673                 if (lpfc_readl(resp_buf, &resp_data))
3674                         return;
3675                 if (resp_data != mbox)
3676                         mdelay(1);
3677                 else
3678                         break;
3679         }
3680
3681 clear_errat:
3682
3683         while (++i < 500) {
3684                 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3685                         return;
3686                 if (!(ha_copy & HA_ERATT))
3687                         mdelay(1);
3688                 else
3689                         break;
3690         }
3691
3692         if (readl(phba->HAregaddr) & HA_ERATT) {
3693                 writel(HA_ERATT, phba->HAregaddr);
3694                 phba->pport->stopped = 1;
3695         }
3696
3697 restore_hc:
3698         phba->link_flag &= ~LS_IGNORE_ERATT;
3699         writel(hc_copy, phba->HCregaddr);
3700         readl(phba->HCregaddr); /* flush */
3701 }
3702
3703 /**
3704  * lpfc_sli_brdkill - Issue a kill_board mailbox command
3705  * @phba: Pointer to HBA context object.
3706  *
3707  * This function issues a kill_board mailbox command and waits for
3708  * the error attention interrupt. This function is called for stopping
3709  * the firmware processing. The caller is not required to hold any
3710  * locks. This function calls lpfc_hba_down_post function to free
3711  * any pending commands after the kill. The function will return 1 when it
3712  * fails to kill the board else will return 0.
3713  **/
3714 int
3715 lpfc_sli_brdkill(struct lpfc_hba *phba)
3716 {
3717         struct lpfc_sli *psli;
3718         LPFC_MBOXQ_t *pmb;
3719         uint32_t status;
3720         uint32_t ha_copy;
3721         int retval;
3722         int i = 0;
3723
3724         psli = &phba->sli;
3725
3726         /* Kill HBA */
3727         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3728                         "0329 Kill HBA Data: x%x x%x\n",
3729                         phba->pport->port_state, psli->sli_flag);
3730
3731         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3732         if (!pmb)
3733                 return 1;
3734
3735         /* Disable the error attention */
3736         spin_lock_irq(&phba->hbalock);
3737         if (lpfc_readl(phba->HCregaddr, &status)) {
3738                 spin_unlock_irq(&phba->hbalock);
3739                 mempool_free(pmb, phba->mbox_mem_pool);
3740                 return 1;
3741         }
3742         status &= ~HC_ERINT_ENA;
3743         writel(status, phba->HCregaddr);
3744         readl(phba->HCregaddr); /* flush */
3745         phba->link_flag |= LS_IGNORE_ERATT;
3746         spin_unlock_irq(&phba->hbalock);
3747
3748         lpfc_kill_board(phba, pmb);
3749         pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3750         retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3751
3752         if (retval != MBX_SUCCESS) {
3753                 if (retval != MBX_BUSY)
3754                         mempool_free(pmb, phba->mbox_mem_pool);
3755                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3756                                 "2752 KILL_BOARD command failed retval %d\n",
3757                                 retval);
3758                 spin_lock_irq(&phba->hbalock);
3759                 phba->link_flag &= ~LS_IGNORE_ERATT;
3760                 spin_unlock_irq(&phba->hbalock);
3761                 return 1;
3762         }
3763
3764         spin_lock_irq(&phba->hbalock);
3765         psli->sli_flag &= ~LPFC_SLI_ACTIVE;
3766         spin_unlock_irq(&phba->hbalock);
3767
3768         mempool_free(pmb, phba->mbox_mem_pool);
3769
3770         /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
3771          * attention every 100ms for 3 seconds. If we don't get ERATT after
3772          * 3 seconds we still set HBA_ERROR state because the status of the
3773          * board is now undefined.
3774          */
3775         if (lpfc_readl(phba->HAregaddr, &ha_copy))
3776                 return 1;
3777         while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
3778                 mdelay(100);
3779                 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3780                         return 1;
3781         }
3782
3783         del_timer_sync(&psli->mbox_tmo);
3784         if (ha_copy & HA_ERATT) {
3785                 writel(HA_ERATT, phba->HAregaddr);
3786                 phba->pport->stopped = 1;
3787         }
3788         spin_lock_irq(&phba->hbalock);
3789         psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
3790         psli->mbox_active = NULL;
3791         phba->link_flag &= ~LS_IGNORE_ERATT;
3792         spin_unlock_irq(&phba->hbalock);
3793
3794         lpfc_hba_down_post(phba);
3795         phba->link_state = LPFC_HBA_ERROR;
3796
3797         return ha_copy & HA_ERATT ? 0 : 1;
3798 }
3799
3800 /**
3801  * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
3802  * @phba: Pointer to HBA context object.
3803  *
3804  * This function resets the HBA by writing HC_INITFF to the control
3805  * register. After the HBA resets, this function resets all the iocb ring
3806  * indices. This function disables PCI layer parity checking during
3807  * the reset.
3808  * This function returns 0 always.
3809  * The caller is not required to hold any locks.
3810  **/
3811 int
3812 lpfc_sli_brdreset(struct lpfc_hba *phba)
3813 {
3814         struct lpfc_sli *psli;
3815         struct lpfc_sli_ring *pring;
3816         uint16_t cfg_value;
3817         int i;
3818
3819         psli = &phba->sli;
3820
3821         /* Reset HBA */
3822         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3823                         "0325 Reset HBA Data: x%x x%x\n",
3824                         phba->pport->port_state, psli->sli_flag);
3825
3826         /* perform board reset */
3827         phba->fc_eventTag = 0;
3828         phba->link_events = 0;
3829         phba->pport->fc_myDID = 0;
3830         phba->pport->fc_prevDID = 0;
3831
3832         /* Turn off parity checking and serr during the physical reset */
3833         pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3834         pci_write_config_word(phba->pcidev, PCI_COMMAND,
3835                               (cfg_value &
3836                                ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3837
3838         psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
3839
3840         /* Now toggle INITFF bit in the Host Control Register */
3841         writel(HC_INITFF, phba->HCregaddr);
3842         mdelay(1);
3843         readl(phba->HCregaddr); /* flush */
3844         writel(0, phba->HCregaddr);
3845         readl(phba->HCregaddr); /* flush */
3846
3847         /* Restore PCI cmd register */
3848         pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
3849
3850         /* Initialize relevant SLI info */
3851         for (i = 0; i < psli->num_rings; i++) {
3852                 pring = &psli->ring[i];
3853                 pring->flag = 0;
3854                 pring->rspidx = 0;
3855                 pring->next_cmdidx  = 0;
3856                 pring->local_getidx = 0;
3857                 pring->cmdidx = 0;
3858                 pring->missbufcnt = 0;
3859         }
3860
3861         phba->link_state = LPFC_WARM_START;
3862         return 0;
3863 }
3864
3865 /**
3866  * lpfc_sli4_brdreset - Reset a sli-4 HBA
3867  * @phba: Pointer to HBA context object.
3868  *
3869  * This function resets a SLI4 HBA. This function disables PCI layer parity
3870  * checking during resets the device. The caller is not required to hold
3871  * any locks.
3872  *
3873  * This function returns 0 always.
3874  **/
3875 int
3876 lpfc_sli4_brdreset(struct lpfc_hba *phba)
3877 {
3878         struct lpfc_sli *psli = &phba->sli;
3879         uint16_t cfg_value;
3880
3881         /* Reset HBA */
3882         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3883                         "0295 Reset HBA Data: x%x x%x\n",
3884                         phba->pport->port_state, psli->sli_flag);
3885
3886         /* perform board reset */
3887         phba->fc_eventTag = 0;
3888         phba->link_events = 0;
3889         phba->pport->fc_myDID = 0;
3890         phba->pport->fc_prevDID = 0;
3891
3892         spin_lock_irq(&phba->hbalock);
3893         psli->sli_flag &= ~(LPFC_PROCESS_LA);
3894         phba->fcf.fcf_flag = 0;
3895         spin_unlock_irq(&phba->hbalock);
3896
3897         /* Now physically reset the device */
3898         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3899                         "0389 Performing PCI function reset!\n");
3900
3901         /* Turn off parity checking and serr during the physical reset */
3902         pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3903         pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
3904                               ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3905
3906         /* Perform FCoE PCI function reset */
3907         lpfc_sli4_queue_destroy(phba);
3908         lpfc_pci_function_reset(phba);
3909
3910         /* Restore PCI cmd register */
3911         pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
3912
3913         return 0;
3914 }
3915
3916 /**
3917  * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
3918  * @phba: Pointer to HBA context object.
3919  *
3920  * This function is called in the SLI initialization code path to
3921  * restart the HBA. The caller is not required to hold any lock.
3922  * This function writes MBX_RESTART mailbox command to the SLIM and
3923  * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
3924  * function to free any pending commands. The function enables
3925  * POST only during the first initialization. The function returns zero.
3926  * The function does not guarantee completion of MBX_RESTART mailbox
3927  * command before the return of this function.
3928  **/
3929 static int
3930 lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
3931 {
3932         MAILBOX_t *mb;
3933         struct lpfc_sli *psli;
3934         volatile uint32_t word0;
3935         void __iomem *to_slim;
3936         uint32_t hba_aer_enabled;
3937
3938         spin_lock_irq(&phba->hbalock);
3939
3940         /* Take PCIe device Advanced Error Reporting (AER) state */
3941         hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
3942
3943         psli = &phba->sli;
3944
3945         /* Restart HBA */
3946         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3947                         "0337 Restart HBA Data: x%x x%x\n",
3948                         phba->pport->port_state, psli->sli_flag);
3949
3950         word0 = 0;
3951         mb = (MAILBOX_t *) &word0;
3952         mb->mbxCommand = MBX_RESTART;
3953         mb->mbxHc = 1;
3954
3955         lpfc_reset_barrier(phba);
3956
3957         to_slim = phba->MBslimaddr;
3958         writel(*(uint32_t *) mb, to_slim);
3959         readl(to_slim); /* flush */
3960
3961         /* Only skip post after fc_ffinit is completed */
3962         if (phba->pport->port_state)
3963                 word0 = 1;      /* This is really setting up word1 */
3964         else
3965                 word0 = 0;      /* This is really setting up word1 */
3966         to_slim = phba->MBslimaddr + sizeof (uint32_t);
3967         writel(*(uint32_t *) mb, to_slim);
3968         readl(to_slim); /* flush */
3969
3970         lpfc_sli_brdreset(phba);
3971         phba->pport->stopped = 0;
3972         phba->link_state = LPFC_INIT_START;
3973         phba->hba_flag = 0;
3974         spin_unlock_irq(&phba->hbalock);
3975
3976         memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
3977         psli->stats_start = get_seconds();
3978
3979         /* Give the INITFF and Post time to settle. */
3980         mdelay(100);
3981
3982         /* Reset HBA AER if it was enabled, note hba_flag was reset above */
3983         if (hba_aer_enabled)
3984                 pci_disable_pcie_error_reporting(phba->pcidev);
3985
3986         lpfc_hba_down_post(phba);
3987
3988         return 0;
3989 }
3990
3991 /**
3992  * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
3993  * @phba: Pointer to HBA context object.
3994  *
3995  * This function is called in the SLI initialization code path to restart
3996  * a SLI4 HBA. The caller is not required to hold any lock.
3997  * At the end of the function, it calls lpfc_hba_down_post function to
3998  * free any pending commands.
3999  **/
4000 static int
4001 lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
4002 {
4003         struct lpfc_sli *psli = &phba->sli;
4004         uint32_t hba_aer_enabled;
4005
4006         /* Restart HBA */
4007         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4008                         "0296 Restart HBA Data: x%x x%x\n",
4009                         phba->pport->port_state, psli->sli_flag);
4010
4011         /* Take PCIe device Advanced Error Reporting (AER) state */
4012         hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4013
4014         lpfc_sli4_brdreset(phba);
4015
4016         spin_lock_irq(&phba->hbalock);
4017         phba->pport->stopped = 0;
4018         phba->link_state = LPFC_INIT_START;
4019         phba->hba_flag = 0;
4020         spin_unlock_irq(&phba->hbalock);
4021
4022         memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4023         psli->stats_start = get_seconds();
4024
4025         /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4026         if (hba_aer_enabled)
4027                 pci_disable_pcie_error_reporting(phba->pcidev);
4028
4029         lpfc_hba_down_post(phba);
4030
4031         return 0;
4032 }
4033
4034 /**
4035  * lpfc_sli_brdrestart - Wrapper func for restarting hba
4036  * @phba: Pointer to HBA context object.
4037  *
4038  * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
4039  * API jump table function pointer from the lpfc_hba struct.
4040 **/
4041 int
4042 lpfc_sli_brdrestart(struct lpfc_hba *phba)
4043 {
4044         return phba->lpfc_sli_brdrestart(phba);
4045 }
4046
4047 /**
4048  * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
4049  * @phba: Pointer to HBA context object.
4050  *
4051  * This function is called after a HBA restart to wait for successful
4052  * restart of the HBA. Successful restart of the HBA is indicated by
4053  * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
4054  * iteration, the function will restart the HBA again. The function returns
4055  * zero if HBA successfully restarted else returns negative error code.
4056  **/
4057 static int
4058 lpfc_sli_chipset_init(struct lpfc_hba *phba)
4059 {
4060         uint32_t status, i = 0;
4061
4062         /* Read the HBA Host Status Register */
4063         if (lpfc_readl(phba->HSregaddr, &status))
4064                 return -EIO;
4065
4066         /* Check status register to see what current state is */
4067         i = 0;
4068         while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
4069
4070                 /* Check every 10ms for 10 retries, then every 100ms for 90
4071                  * retries, then every 1 sec for 50 retires for a total of
4072                  * ~60 seconds before reset the board again and check every
4073                  * 1 sec for 50 retries. The up to 60 seconds before the
4074                  * board ready is required by the Falcon FIPS zeroization
4075                  * complete, and any reset the board in between shall cause
4076                  * restart of zeroization, further delay the board ready.
4077                  */
4078                 if (i++ >= 200) {
4079                         /* Adapter failed to init, timeout, status reg
4080                            <status> */
4081                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4082                                         "0436 Adapter failed to init, "
4083                                         "timeout, status reg x%x, "
4084                                         "FW Data: A8 x%x AC x%x\n", status,
4085                                         readl(phba->MBslimaddr + 0xa8),
4086                                         readl(phba->MBslimaddr + 0xac));
4087                         phba->link_state = LPFC_HBA_ERROR;
4088                         return -ETIMEDOUT;
4089                 }
4090
4091                 /* Check to see if any errors occurred during init */
4092                 if (status & HS_FFERM) {
4093                         /* ERROR: During chipset initialization */
4094                         /* Adapter failed to init, chipset, status reg
4095                            <status> */
4096                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4097                                         "0437 Adapter failed to init, "
4098                                         "chipset, status reg x%x, "
4099                                         "FW Data: A8 x%x AC x%x\n", status,
4100                                         readl(phba->MBslimaddr + 0xa8),
4101                                         readl(phba->MBslimaddr + 0xac));
4102                         phba->link_state = LPFC_HBA_ERROR;
4103                         return -EIO;
4104                 }
4105
4106                 if (i <= 10)
4107                         msleep(10);
4108                 else if (i <= 100)
4109                         msleep(100);
4110                 else
4111                         msleep(1000);
4112
4113                 if (i == 150) {
4114                         /* Do post */
4115                         phba->pport->port_state = LPFC_VPORT_UNKNOWN;
4116                         lpfc_sli_brdrestart(phba);
4117                 }
4118                 /* Read the HBA Host Status Register */
4119                 if (lpfc_readl(phba->HSregaddr, &status))
4120                         return -EIO;
4121         }
4122
4123         /* Check to see if any errors occurred during init */
4124         if (status & HS_FFERM) {
4125                 /* ERROR: During chipset initialization */
4126                 /* Adapter failed to init, chipset, status reg <status> */
4127                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4128                                 "0438 Adapter failed to init, chipset, "
4129                                 "status reg x%x, "
4130                                 "FW Data: A8 x%x AC x%x\n", status,
4131                                 readl(phba->MBslimaddr + 0xa8),
4132                                 readl(phba->MBslimaddr + 0xac));
4133                 phba->link_state = LPFC_HBA_ERROR;
4134                 return -EIO;
4135         }
4136
4137         /* Clear all interrupt enable conditions */
4138         writel(0, phba->HCregaddr);
4139         readl(phba->HCregaddr); /* flush */
4140
4141         /* setup host attn register */
4142         writel(0xffffffff, phba->HAregaddr);
4143         readl(phba->HAregaddr); /* flush */
4144         return 0;
4145 }
4146
4147 /**
4148  * lpfc_sli_hbq_count - Get the number of HBQs to be configured
4149  *
4150  * This function calculates and returns the number of HBQs required to be
4151  * configured.
4152  **/
4153 int
4154 lpfc_sli_hbq_count(void)
4155 {
4156         return ARRAY_SIZE(lpfc_hbq_defs);
4157 }
4158
4159 /**
4160  * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
4161  *
4162  * This function adds the number of hbq entries in every HBQ to get
4163  * the total number of hbq entries required for the HBA and returns
4164  * the total count.
4165  **/
4166 static int
4167 lpfc_sli_hbq_entry_count(void)
4168 {
4169         int  hbq_count = lpfc_sli_hbq_count();
4170         int  count = 0;
4171         int  i;
4172
4173         for (i = 0; i < hbq_count; ++i)
4174                 count += lpfc_hbq_defs[i]->entry_count;
4175         return count;
4176 }
4177
4178 /**
4179  * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
4180  *
4181  * This function calculates amount of memory required for all hbq entries
4182  * to be configured and returns the total memory required.
4183  **/
4184 int
4185 lpfc_sli_hbq_size(void)
4186 {
4187         return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
4188 }
4189
4190 /**
4191  * lpfc_sli_hbq_setup - configure and initialize HBQs
4192  * @phba: Pointer to HBA context object.
4193  *
4194  * This function is called during the SLI initialization to configure
4195  * all the HBQs and post buffers to the HBQ. The caller is not
4196  * required to hold any locks. This function will return zero if successful
4197  * else it will return negative error code.
4198  **/
4199 static int
4200 lpfc_sli_hbq_setup(struct lpfc_hba *phba)
4201 {
4202         int  hbq_count = lpfc_sli_hbq_count();
4203         LPFC_MBOXQ_t *pmb;
4204         MAILBOX_t *pmbox;
4205         uint32_t hbqno;
4206         uint32_t hbq_entry_index;
4207
4208                                 /* Get a Mailbox buffer to setup mailbox
4209                                  * commands for HBA initialization
4210                                  */
4211         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4212
4213         if (!pmb)
4214                 return -ENOMEM;
4215
4216         pmbox = &pmb->u.mb;
4217
4218         /* Initialize the struct lpfc_sli_hbq structure for each hbq */
4219         phba->link_state = LPFC_INIT_MBX_CMDS;
4220         phba->hbq_in_use = 1;
4221
4222         hbq_entry_index = 0;
4223         for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
4224                 phba->hbqs[hbqno].next_hbqPutIdx = 0;
4225                 phba->hbqs[hbqno].hbqPutIdx      = 0;
4226                 phba->hbqs[hbqno].local_hbqGetIdx   = 0;
4227                 phba->hbqs[hbqno].entry_count =
4228                         lpfc_hbq_defs[hbqno]->entry_count;
4229                 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
4230                         hbq_entry_index, pmb);
4231                 hbq_entry_index += phba->hbqs[hbqno].entry_count;
4232
4233                 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
4234                         /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
4235                            mbxStatus <status>, ring <num> */
4236
4237                         lpfc_printf_log(phba, KERN_ERR,
4238                                         LOG_SLI | LOG_VPORT,
4239                                         "1805 Adapter failed to init. "
4240                                         "Data: x%x x%x x%x\n",
4241                                         pmbox->mbxCommand,
4242                                         pmbox->mbxStatus, hbqno);
4243
4244                         phba->link_state = LPFC_HBA_ERROR;
4245                         mempool_free(pmb, phba->mbox_mem_pool);
4246                         return -ENXIO;
4247                 }
4248         }
4249         phba->hbq_count = hbq_count;
4250
4251         mempool_free(pmb, phba->mbox_mem_pool);
4252
4253         /* Initially populate or replenish the HBQs */
4254         for (hbqno = 0; hbqno < hbq_count; ++hbqno)
4255                 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
4256         return 0;
4257 }
4258
4259 /**
4260  * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
4261  * @phba: Pointer to HBA context object.
4262  *
4263  * This function is called during the SLI initialization to configure
4264  * all the HBQs and post buffers to the HBQ. The caller is not
4265  * required to hold any locks. This function will return zero if successful
4266  * else it will return negative error code.
4267  **/
4268 static int
4269 lpfc_sli4_rb_setup(struct lpfc_hba *phba)
4270 {
4271         phba->hbq_in_use = 1;
4272         phba->hbqs[0].entry_count = lpfc_hbq_defs[0]->entry_count;
4273         phba->hbq_count = 1;
4274         /* Initially populate or replenish the HBQs */
4275         lpfc_sli_hbqbuf_init_hbqs(phba, 0);
4276         return 0;
4277 }
4278
4279 /**
4280  * lpfc_sli_config_port - Issue config port mailbox command
4281  * @phba: Pointer to HBA context object.
4282  * @sli_mode: sli mode - 2/3
4283  *
4284  * This function is called by the sli intialization code path
4285  * to issue config_port mailbox command. This function restarts the
4286  * HBA firmware and issues a config_port mailbox command to configure
4287  * the SLI interface in the sli mode specified by sli_mode
4288  * variable. The caller is not required to hold any locks.
4289  * The function returns 0 if successful, else returns negative error
4290  * code.
4291  **/
4292 int
4293 lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
4294 {
4295         LPFC_MBOXQ_t *pmb;
4296         uint32_t resetcount = 0, rc = 0, done = 0;
4297
4298         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4299         if (!pmb) {
4300                 phba->link_state = LPFC_HBA_ERROR;
4301                 return -ENOMEM;
4302         }
4303
4304         phba->sli_rev = sli_mode;
4305         while (resetcount < 2 && !done) {
4306                 spin_lock_irq(&phba->hbalock);
4307                 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
4308                 spin_unlock_irq(&phba->hbalock);
4309                 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
4310                 lpfc_sli_brdrestart(phba);
4311                 rc = lpfc_sli_chipset_init(phba);
4312                 if (rc)
4313                         break;
4314
4315                 spin_lock_irq(&phba->hbalock);
4316                 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
4317                 spin_unlock_irq(&phba->hbalock);
4318                 resetcount++;
4319
4320                 /* Call pre CONFIG_PORT mailbox command initialization.  A
4321                  * value of 0 means the call was successful.  Any other
4322                  * nonzero value is a failure, but if ERESTART is returned,
4323                  * the driver may reset the HBA and try again.
4324                  */
4325                 rc = lpfc_config_port_prep(phba);
4326                 if (rc == -ERESTART) {
4327                         phba->link_state = LPFC_LINK_UNKNOWN;
4328                         continue;
4329                 } else if (rc)
4330                         break;
4331
4332                 phba->link_state = LPFC_INIT_MBX_CMDS;
4333                 lpfc_config_port(phba, pmb);
4334                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
4335                 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
4336                                         LPFC_SLI3_HBQ_ENABLED |
4337                                         LPFC_SLI3_CRP_ENABLED |
4338                                         LPFC_SLI3_BG_ENABLED |
4339                                         LPFC_SLI3_DSS_ENABLED);
4340                 if (rc != MBX_SUCCESS) {
4341                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4342                                 "0442 Adapter failed to init, mbxCmd x%x "
4343                                 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
4344                                 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
4345                         spin_lock_irq(&phba->hbalock);
4346                         phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
4347                         spin_unlock_irq(&phba->hbalock);
4348                         rc = -ENXIO;
4349                 } else {
4350                         /* Allow asynchronous mailbox command to go through */
4351                         spin_lock_irq(&phba->hbalock);
4352                         phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4353                         spin_unlock_irq(&phba->hbalock);
4354                         done = 1;
4355
4356                         if ((pmb->u.mb.un.varCfgPort.casabt == 1) &&
4357                             (pmb->u.mb.un.varCfgPort.gasabt == 0))
4358                                 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4359                                         "3110 Port did not grant ASABT\n");
4360                 }
4361         }
4362         if (!done) {
4363                 rc = -EINVAL;
4364                 goto do_prep_failed;
4365         }
4366         if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
4367                 if (!pmb->u.mb.un.varCfgPort.cMA) {
4368                         rc = -ENXIO;
4369                         goto do_prep_failed;
4370                 }
4371                 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
4372                         phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
4373                         phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
4374                         phba->max_vports = (phba->max_vpi > phba->max_vports) ?
4375                                 phba->max_vpi : phba->max_vports;
4376
4377                 } else
4378                         phba->max_vpi = 0;
4379                 phba->fips_level = 0;
4380                 phba->fips_spec_rev = 0;
4381                 if (pmb->u.mb.un.varCfgPort.gdss) {
4382                         phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
4383                         phba->fips_level = pmb->u.mb.un.varCfgPort.fips_level;
4384                         phba->fips_spec_rev = pmb->u.mb.un.varCfgPort.fips_rev;
4385                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4386                                         "2850 Security Crypto Active. FIPS x%d "
4387                                         "(Spec Rev: x%d)",
4388                                         phba->fips_level, phba->fips_spec_rev);
4389                 }
4390                 if (pmb->u.mb.un.varCfgPort.sec_err) {
4391                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4392                                         "2856 Config Port Security Crypto "
4393                                         "Error: x%x ",
4394                                         pmb->u.mb.un.varCfgPort.sec_err);
4395                 }
4396                 if (pmb->u.mb.un.varCfgPort.gerbm)
4397                         phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
4398                 if (pmb->u.mb.un.varCfgPort.gcrp)
4399                         phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
4400
4401                 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
4402                 phba->port_gp = phba->mbox->us.s3_pgp.port;
4403
4404                 if (phba->cfg_enable_bg) {
4405                         if (pmb->u.mb.un.varCfgPort.gbg)
4406                                 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
4407                         else
4408                                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4409                                                 "0443 Adapter did not grant "
4410                                                 "BlockGuard\n");
4411                 }
4412         } else {
4413                 phba->hbq_get = NULL;
4414                 phba->port_gp = phba->mbox->us.s2.port;
4415                 phba->max_vpi = 0;
4416         }
4417 do_prep_failed:
4418         mempool_free(pmb, phba->mbox_mem_pool);
4419         return rc;
4420 }
4421
4422
4423 /**
4424  * lpfc_sli_hba_setup - SLI intialization function
4425  * @phba: Pointer to HBA context object.
4426  *
4427  * This function is the main SLI intialization function. This function
4428  * is called by the HBA intialization code, HBA reset code and HBA
4429  * error attention handler code. Caller is not required to hold any
4430  * locks. This function issues config_port mailbox command to configure
4431  * the SLI, setup iocb rings and HBQ rings. In the end the function
4432  * calls the config_port_post function to issue init_link mailbox
4433  * command and to start the discovery. The function will return zero
4434  * if successful, else it will return negative error code.
4435  **/
4436 int
4437 lpfc_sli_hba_setup(struct lpfc_hba *phba)
4438 {
4439         uint32_t rc;
4440         int  mode = 3, i;
4441         int longs;
4442
4443         switch (lpfc_sli_mode) {
4444         case 2:
4445                 if (phba->cfg_enable_npiv) {
4446                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
4447                                 "1824 NPIV enabled: Override lpfc_sli_mode "
4448                                 "parameter (%d) to auto (0).\n",
4449                                 lpfc_sli_mode);
4450                         break;
4451                 }
4452                 mode = 2;
4453                 break;
4454         case 0:
4455         case 3:
4456                 break;
4457         default:
4458                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
4459                                 "1819 Unrecognized lpfc_sli_mode "
4460                                 "parameter: %d.\n", lpfc_sli_mode);
4461
4462                 break;
4463         }
4464
4465         rc = lpfc_sli_config_port(phba, mode);
4466
4467         if (rc && lpfc_sli_mode == 3)
4468                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
4469                                 "1820 Unable to select SLI-3.  "
4470                                 "Not supported by adapter.\n");
4471         if (rc && mode != 2)
4472                 rc = lpfc_sli_config_port(phba, 2);
4473         if (rc)
4474                 goto lpfc_sli_hba_setup_error;
4475
4476         /* Enable PCIe device Advanced Error Reporting (AER) if configured */
4477         if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
4478                 rc = pci_enable_pcie_error_reporting(phba->pcidev);
4479                 if (!rc) {
4480                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4481                                         "2709 This device supports "
4482                                         "Advanced Error Reporting (AER)\n");
4483                         spin_lock_irq(&phba->hbalock);
4484                         phba->hba_flag |= HBA_AER_ENABLED;
4485                         spin_unlock_irq(&phba->hbalock);
4486                 } else {
4487                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4488                                         "2708 This device does not support "
4489                                         "Advanced Error Reporting (AER)\n");
4490                         phba->cfg_aer_support = 0;
4491                 }
4492         }
4493
4494         if (phba->sli_rev == 3) {
4495                 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
4496                 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
4497         } else {
4498                 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
4499                 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
4500                 phba->sli3_options = 0;
4501         }
4502
4503         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4504                         "0444 Firmware in SLI %x mode. Max_vpi %d\n",
4505                         phba->sli_rev, phba->max_vpi);
4506         rc = lpfc_sli_ring_map(phba);
4507
4508         if (rc)
4509                 goto lpfc_sli_hba_setup_error;
4510
4511         /* Initialize VPIs. */
4512         if (phba->sli_rev == LPFC_SLI_REV3) {
4513                 /*
4514                  * The VPI bitmask and physical ID array are allocated
4515                  * and initialized once only - at driver load.  A port
4516                  * reset doesn't need to reinitialize this memory.
4517                  */
4518                 if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
4519                         longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
4520                         phba->vpi_bmask = kzalloc(longs * sizeof(unsigned long),
4521                                                   GFP_KERNEL);
4522                         if (!phba->vpi_bmask) {
4523                                 rc = -ENOMEM;
4524                                 goto lpfc_sli_hba_setup_error;
4525                         }
4526
4527                         phba->vpi_ids = kzalloc(
4528                                         (phba->max_vpi+1) * sizeof(uint16_t),
4529                                         GFP_KERNEL);
4530                         if (!phba->vpi_ids) {
4531                                 kfree(phba->vpi_bmask);
4532                                 rc = -ENOMEM;
4533                                 goto lpfc_sli_hba_setup_error;
4534                         }
4535                         for (i = 0; i < phba->max_vpi; i++)
4536                                 phba->vpi_ids[i] = i;
4537                 }
4538         }
4539
4540         /* Init HBQs */
4541         if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
4542                 rc = lpfc_sli_hbq_setup(phba);
4543                 if (rc)
4544                         goto lpfc_sli_hba_setup_error;
4545         }
4546         spin_lock_irq(&phba->hbalock);
4547         phba->sli.sli_flag |= LPFC_PROCESS_LA;
4548         spin_unlock_irq(&phba->hbalock);
4549
4550         rc = lpfc_config_port_post(phba);
4551         if (rc)
4552                 goto lpfc_sli_hba_setup_error;
4553
4554         return rc;
4555
4556 lpfc_sli_hba_setup_error:
4557         phba->link_state = LPFC_HBA_ERROR;
4558         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4559                         "0445 Firmware initialization failed\n");
4560         return rc;
4561 }
4562
4563 /**
4564  * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
4565  * @phba: Pointer to HBA context object.
4566  * @mboxq: mailbox pointer.
4567  * This function issue a dump mailbox command to read config region
4568  * 23 and parse the records in the region and populate driver
4569  * data structure.
4570  **/
4571 static int
4572 lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba)
4573 {
4574         LPFC_MBOXQ_t *mboxq;
4575         struct lpfc_dmabuf *mp;
4576         struct lpfc_mqe *mqe;
4577         uint32_t data_length;
4578         int rc;
4579
4580         /* Program the default value of vlan_id and fc_map */
4581         phba->valid_vlan = 0;
4582         phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4583         phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4584         phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
4585
4586         mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4587         if (!mboxq)
4588                 return -ENOMEM;
4589
4590         mqe = &mboxq->u.mqe;
4591         if (lpfc_sli4_dump_cfg_rg23(phba, mboxq)) {
4592                 rc = -ENOMEM;
4593                 goto out_free_mboxq;
4594         }
4595
4596         mp = (struct lpfc_dmabuf *) mboxq->context1;
4597         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4598
4599         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4600                         "(%d):2571 Mailbox cmd x%x Status x%x "
4601                         "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4602                         "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4603                         "CQ: x%x x%x x%x x%x\n",
4604                         mboxq->vport ? mboxq->vport->vpi : 0,
4605                         bf_get(lpfc_mqe_command, mqe),
4606                         bf_get(lpfc_mqe_status, mqe),
4607                         mqe->un.mb_words[0], mqe->un.mb_words[1],
4608                         mqe->un.mb_words[2], mqe->un.mb_words[3],
4609                         mqe->un.mb_words[4], mqe->un.mb_words[5],
4610                         mqe->un.mb_words[6], mqe->un.mb_words[7],
4611                         mqe->un.mb_words[8], mqe->un.mb_words[9],
4612                         mqe->un.mb_words[10], mqe->un.mb_words[11],
4613                         mqe->un.mb_words[12], mqe->un.mb_words[13],
4614                         mqe->un.mb_words[14], mqe->un.mb_words[15],
4615                         mqe->un.mb_words[16], mqe->un.mb_words[50],
4616                         mboxq->mcqe.word0,
4617                         mboxq->mcqe.mcqe_tag0,  mboxq->mcqe.mcqe_tag1,
4618                         mboxq->mcqe.trailer);
4619
4620         if (rc) {
4621                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4622                 kfree(mp);
4623                 rc = -EIO;
4624                 goto out_free_mboxq;
4625         }
4626         data_length = mqe->un.mb_words[5];
4627         if (data_length > DMP_RGN23_SIZE) {
4628                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4629                 kfree(mp);
4630                 rc = -EIO;
4631                 goto out_free_mboxq;
4632         }
4633
4634         lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
4635         lpfc_mbuf_free(phba, mp->virt, mp->phys);
4636         kfree(mp);
4637         rc = 0;
4638
4639 out_free_mboxq:
4640         mempool_free(mboxq, phba->mbox_mem_pool);
4641         return rc;
4642 }
4643
4644 /**
4645  * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
4646  * @phba: pointer to lpfc hba data structure.
4647  * @mboxq: pointer to the LPFC_MBOXQ_t structure.
4648  * @vpd: pointer to the memory to hold resulting port vpd data.
4649  * @vpd_size: On input, the number of bytes allocated to @vpd.
4650  *            On output, the number of data bytes in @vpd.
4651  *
4652  * This routine executes a READ_REV SLI4 mailbox command.  In
4653  * addition, this routine gets the port vpd data.
4654  *
4655  * Return codes
4656  *      0 - successful
4657  *      -ENOMEM - could not allocated memory.
4658  **/
4659 static int
4660 lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
4661                     uint8_t *vpd, uint32_t *vpd_size)
4662 {
4663         int rc = 0;
4664         uint32_t dma_size;
4665         struct lpfc_dmabuf *dmabuf;
4666         struct lpfc_mqe *mqe;
4667
4668         dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4669         if (!dmabuf)
4670                 return -ENOMEM;
4671
4672         /*
4673          * Get a DMA buffer for the vpd data resulting from the READ_REV
4674          * mailbox command.
4675          */
4676         dma_size = *vpd_size;
4677         dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
4678                                           dma_size,
4679                                           &dmabuf->phys,
4680                                           GFP_KERNEL);
4681         if (!dmabuf->virt) {
4682                 kfree(dmabuf);
4683                 return -ENOMEM;
4684         }
4685         memset(dmabuf->virt, 0, dma_size);
4686
4687         /*
4688          * The SLI4 implementation of READ_REV conflicts at word1,
4689          * bits 31:16 and SLI4 adds vpd functionality not present
4690          * in SLI3.  This code corrects the conflicts.
4691          */
4692         lpfc_read_rev(phba, mboxq);
4693         mqe = &mboxq->u.mqe;
4694         mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
4695         mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
4696         mqe->un.read_rev.word1 &= 0x0000FFFF;
4697         bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
4698         bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
4699
4700         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4701         if (rc) {
4702                 dma_free_coherent(&phba->pcidev->dev, dma_size,
4703                                   dmabuf->virt, dmabuf->phys);
4704                 kfree(dmabuf);
4705                 return -EIO;
4706         }
4707
4708         /*
4709          * The available vpd length cannot be bigger than the
4710          * DMA buffer passed to the port.  Catch the less than
4711          * case and update the caller's size.
4712          */
4713         if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
4714                 *vpd_size = mqe->un.read_rev.avail_vpd_len;
4715
4716         memcpy(vpd, dmabuf->virt, *vpd_size);
4717
4718         dma_free_coherent(&phba->pcidev->dev, dma_size,
4719                           dmabuf->virt, dmabuf->phys);
4720         kfree(dmabuf);
4721         return 0;
4722 }
4723
4724 /**
4725  * lpfc_sli4_retrieve_pport_name - Retrieve SLI4 device physical port name
4726  * @phba: pointer to lpfc hba data structure.
4727  *
4728  * This routine retrieves SLI4 device physical port name this PCI function
4729  * is attached to.
4730  *
4731  * Return codes
4732  *      0 - sucessful
4733  *      otherwise - failed to retrieve physical port name
4734  **/
4735 static int
4736 lpfc_sli4_retrieve_pport_name(struct lpfc_hba *phba)
4737 {
4738         LPFC_MBOXQ_t *mboxq;
4739         struct lpfc_mbx_get_cntl_attributes *mbx_cntl_attr;
4740         struct lpfc_controller_attribute *cntl_attr;
4741         struct lpfc_mbx_get_port_name *get_port_name;
4742         void *virtaddr = NULL;
4743         uint32_t alloclen, reqlen;
4744         uint32_t shdr_status, shdr_add_status;
4745         union lpfc_sli4_cfg_shdr *shdr;
4746         char cport_name = 0;
4747         int rc;
4748
4749         /* We assume nothing at this point */
4750         phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
4751         phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_NON;
4752
4753         mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4754         if (!mboxq)
4755                 return -ENOMEM;
4756         /* obtain link type and link number via READ_CONFIG */
4757         phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
4758         lpfc_sli4_read_config(phba);
4759         if (phba->sli4_hba.lnk_info.lnk_dv == LPFC_LNK_DAT_VAL)
4760                 goto retrieve_ppname;
4761
4762         /* obtain link type and link number via COMMON_GET_CNTL_ATTRIBUTES */
4763         reqlen = sizeof(struct lpfc_mbx_get_cntl_attributes);
4764         alloclen = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
4765                         LPFC_MBOX_OPCODE_GET_CNTL_ATTRIBUTES, reqlen,
4766                         LPFC_SLI4_MBX_NEMBED);
4767         if (alloclen < reqlen) {
4768                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4769                                 "3084 Allocated DMA memory size (%d) is "
4770                                 "less than the requested DMA memory size "
4771                                 "(%d)\n", alloclen, reqlen);
4772                 rc = -ENOMEM;
4773                 goto out_free_mboxq;
4774         }
4775         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4776         virtaddr = mboxq->sge_array->addr[0];
4777         mbx_cntl_attr = (struct lpfc_mbx_get_cntl_attributes *)virtaddr;
4778         shdr = &mbx_cntl_attr->cfg_shdr;
4779         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
4780         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
4781         if (shdr_status || shdr_add_status || rc) {
4782                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
4783                                 "3085 Mailbox x%x (x%x/x%x) failed, "
4784                                 "rc:x%x, status:x%x, add_status:x%x\n",
4785                                 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
4786                                 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
4787                                 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
4788                                 rc, shdr_status, shdr_add_status);
4789                 rc = -ENXIO;
4790                 goto out_free_mboxq;
4791         }
4792         cntl_attr = &mbx_cntl_attr->cntl_attr;
4793         phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
4794         phba->sli4_hba.lnk_info.lnk_tp =
4795                 bf_get(lpfc_cntl_attr_lnk_type, cntl_attr);
4796         phba->sli4_hba.lnk_info.lnk_no =
4797                 bf_get(lpfc_cntl_attr_lnk_numb, cntl_attr);
4798         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4799                         "3086 lnk_type:%d, lnk_numb:%d\n",
4800                         phba->sli4_hba.lnk_info.lnk_tp,
4801                         phba->sli4_hba.lnk_info.lnk_no);
4802
4803 retrieve_ppname:
4804         lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
4805                 LPFC_MBOX_OPCODE_GET_PORT_NAME,
4806                 sizeof(struct lpfc_mbx_get_port_name) -
4807                 sizeof(struct lpfc_sli4_cfg_mhdr),
4808                 LPFC_SLI4_MBX_EMBED);
4809         get_port_name = &mboxq->u.mqe.un.get_port_name;
4810         shdr = (union lpfc_sli4_cfg_shdr *)&get_port_name->header.cfg_shdr;
4811         bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_OPCODE_VERSION_1);
4812         bf_set(lpfc_mbx_get_port_name_lnk_type, &get_port_name->u.request,
4813                 phba->sli4_hba.lnk_info.lnk_tp);
4814         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4815         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
4816         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
4817         if (shdr_status || shdr_add_status || rc) {
4818                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
4819                                 "3087 Mailbox x%x (x%x/x%x) failed: "
4820                                 "rc:x%x, status:x%x, add_status:x%x\n",
4821                                 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
4822                                 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
4823                                 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
4824                                 rc, shdr_status, shdr_add_status);
4825                 rc = -ENXIO;
4826                 goto out_free_mboxq;
4827         }
4828         switch (phba->sli4_hba.lnk_info.lnk_no) {
4829         case LPFC_LINK_NUMBER_0:
4830                 cport_name = bf_get(lpfc_mbx_get_port_name_name0,
4831                                 &get_port_name->u.response);
4832                 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
4833                 break;
4834         case LPFC_LINK_NUMBER_1:
4835                 cport_name = bf_get(lpfc_mbx_get_port_name_name1,
4836                                 &get_port_name->u.response);
4837                 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
4838                 break;
4839         case LPFC_LINK_NUMBER_2:
4840                 cport_name = bf_get(lpfc_mbx_get_port_name_name2,
4841                                 &get_port_name->u.response);
4842                 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
4843                 break;
4844         case LPFC_LINK_NUMBER_3:
4845                 cport_name = bf_get(lpfc_mbx_get_port_name_name3,
4846                                 &get_port_name->u.response);
4847                 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
4848                 break;
4849         default:
4850                 break;
4851         }
4852
4853         if (phba->sli4_hba.pport_name_sta == LPFC_SLI4_PPNAME_GET) {
4854                 phba->Port[0] = cport_name;
4855                 phba->Port[1] = '\0';
4856                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4857                                 "3091 SLI get port name: %s\n", phba->Port);
4858         }
4859
4860 out_free_mboxq:
4861         if (rc != MBX_TIMEOUT) {
4862                 if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
4863                         lpfc_sli4_mbox_cmd_free(phba, mboxq);
4864                 else
4865                         mempool_free(mboxq, phba->mbox_mem_pool);
4866         }
4867         return rc;
4868 }
4869
4870 /**
4871  * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
4872  * @phba: pointer to lpfc hba data structure.
4873  *
4874  * This routine is called to explicitly arm the SLI4 device's completion and
4875  * event queues
4876  **/
4877 static void
4878 lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
4879 {
4880         uint8_t fcp_eqidx;
4881
4882         lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
4883         lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
4884         fcp_eqidx = 0;
4885         if (phba->sli4_hba.fcp_cq) {
4886                 do
4887                         lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[fcp_eqidx],
4888                                              LPFC_QUEUE_REARM);
4889                 while (++fcp_eqidx < phba->cfg_fcp_eq_count);
4890         }
4891         lpfc_sli4_eq_release(phba->sli4_hba.sp_eq, LPFC_QUEUE_REARM);
4892         if (phba->sli4_hba.fp_eq) {
4893                 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count;
4894                      fcp_eqidx++)
4895                         lpfc_sli4_eq_release(phba->sli4_hba.fp_eq[fcp_eqidx],
4896                                              LPFC_QUEUE_REARM);
4897         }
4898 }
4899
4900 /**
4901  * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
4902  * @phba: Pointer to HBA context object.
4903  * @type: The resource extent type.
4904  * @extnt_count: buffer to hold port available extent count.
4905  * @extnt_size: buffer to hold element count per extent.
4906  *
4907  * This function calls the port and retrievs the number of available
4908  * extents and their size for a particular extent type.
4909  *
4910  * Returns: 0 if successful.  Nonzero otherwise.
4911  **/
4912 int
4913 lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
4914                                uint16_t *extnt_count, uint16_t *extnt_size)
4915 {
4916         int rc = 0;
4917         uint32_t length;
4918         uint32_t mbox_tmo;
4919         struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
4920         LPFC_MBOXQ_t *mbox;
4921
4922         mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4923         if (!mbox)
4924                 return -ENOMEM;
4925
4926         /* Find out how many extents are available for this resource type */
4927         length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
4928                   sizeof(struct lpfc_sli4_cfg_mhdr));
4929         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
4930                          LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
4931                          length, LPFC_SLI4_MBX_EMBED);
4932
4933         /* Send an extents count of 0 - the GET doesn't use it. */
4934         rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
4935                                         LPFC_SLI4_MBX_EMBED);
4936         if (unlikely(rc)) {
4937                 rc = -EIO;
4938                 goto err_exit;
4939         }
4940
4941         if (!phba->sli4_hba.intr_enable)
4942                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
4943         else {
4944                 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
4945                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
4946         }
4947         if (unlikely(rc)) {
4948                 rc = -EIO;
4949                 goto err_exit;
4950         }
4951
4952         rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
4953         if (bf_get(lpfc_mbox_hdr_status,
4954                    &rsrc_info->header.cfg_shdr.response)) {
4955                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
4956                                 "2930 Failed to get resource extents "
4957                                 "Status 0x%x Add'l Status 0x%x\n",
4958                                 bf_get(lpfc_mbox_hdr_status,
4959                                        &rsrc_info->header.cfg_shdr.response),
4960                                 bf_get(lpfc_mbox_hdr_add_status,
4961                                        &rsrc_info->header.cfg_shdr.response));
4962                 rc = -EIO;
4963                 goto err_exit;
4964         }
4965
4966         *extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
4967                               &rsrc_info->u.rsp);
4968         *extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
4969                              &rsrc_info->u.rsp);
4970  err_exit:
4971         mempool_free(mbox, phba->mbox_mem_pool);
4972         return rc;
4973 }
4974
4975 /**
4976  * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
4977  * @phba: Pointer to HBA context object.
4978  * @type: The extent type to check.
4979  *
4980  * This function reads the current available extents from the port and checks
4981  * if the extent count or extent size has changed since the last access.
4982  * Callers use this routine post port reset to understand if there is a
4983  * extent reprovisioning requirement.
4984  *
4985  * Returns:
4986  *   -Error: error indicates problem.
4987  *   1: Extent count or size has changed.
4988  *   0: No changes.
4989  **/
4990 static int
4991 lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
4992 {
4993         uint16_t curr_ext_cnt, rsrc_ext_cnt;
4994         uint16_t size_diff, rsrc_ext_size;
4995         int rc = 0;
4996         struct lpfc_rsrc_blks *rsrc_entry;
4997         struct list_head *rsrc_blk_list = NULL;
4998
4999         size_diff = 0;
5000         curr_ext_cnt = 0;
5001         rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5002                                             &rsrc_ext_cnt,
5003                                             &rsrc_ext_size);
5004         if (unlikely(rc))
5005                 return -EIO;
5006
5007         switch (type) {
5008         case LPFC_RSC_TYPE_FCOE_RPI:
5009                 rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5010                 break;
5011         case LPFC_RSC_TYPE_FCOE_VPI:
5012                 rsrc_blk_list = &phba->lpfc_vpi_blk_list;
5013                 break;
5014         case LPFC_RSC_TYPE_FCOE_XRI:
5015                 rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5016                 break;
5017         case LPFC_RSC_TYPE_FCOE_VFI:
5018                 rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5019                 break;
5020         default:
5021                 break;
5022         }
5023
5024         list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
5025                 curr_ext_cnt++;
5026                 if (rsrc_entry->rsrc_size != rsrc_ext_size)
5027                         size_diff++;
5028         }
5029
5030         if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
5031                 rc = 1;
5032
5033         return rc;
5034 }
5035
5036 /**
5037  * lpfc_sli4_cfg_post_extnts -
5038  * @phba: Pointer to HBA context object.
5039  * @extnt_cnt - number of available extents.
5040  * @type - the extent type (rpi, xri, vfi, vpi).
5041  * @emb - buffer to hold either MBX_EMBED or MBX_NEMBED operation.
5042  * @mbox - pointer to the caller's allocated mailbox structure.
5043  *
5044  * This function executes the extents allocation request.  It also
5045  * takes care of the amount of memory needed to allocate or get the
5046  * allocated extents. It is the caller's responsibility to evaluate
5047  * the response.
5048  *
5049  * Returns:
5050  *   -Error:  Error value describes the condition found.
5051  *   0: if successful
5052  **/
5053 static int
5054 lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t *extnt_cnt,
5055                           uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
5056 {
5057         int rc = 0;
5058         uint32_t req_len;
5059         uint32_t emb_len;
5060         uint32_t alloc_len, mbox_tmo;
5061
5062         /* Calculate the total requested length of the dma memory */
5063         req_len = *extnt_cnt * sizeof(uint16_t);
5064
5065         /*
5066          * Calculate the size of an embedded mailbox.  The uint32_t
5067          * accounts for extents-specific word.
5068          */
5069         emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
5070                 sizeof(uint32_t);
5071
5072         /*
5073          * Presume the allocation and response will fit into an embedded
5074          * mailbox.  If not true, reconfigure to a non-embedded mailbox.
5075          */
5076         *emb = LPFC_SLI4_MBX_EMBED;
5077         if (req_len > emb_len) {
5078                 req_len = *extnt_cnt * sizeof(uint16_t) +
5079                         sizeof(union lpfc_sli4_cfg_shdr) +
5080                         sizeof(uint32_t);
5081                 *emb = LPFC_SLI4_MBX_NEMBED;
5082         }
5083
5084         alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5085                                      LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
5086                                      req_len, *emb);
5087         if (alloc_len < req_len) {
5088                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5089                         "2982 Allocated DMA memory size (x%x) is "
5090                         "less than the requested DMA memory "
5091                         "size (x%x)\n", alloc_len, req_len);
5092                 return -ENOMEM;
5093         }
5094         rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, *extnt_cnt, type, *emb);
5095         if (unlikely(rc))
5096                 return -EIO;
5097
5098         if (!phba->sli4_hba.intr_enable)
5099                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5100         else {
5101                 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
5102                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5103         }
5104
5105         if (unlikely(rc))
5106                 rc = -EIO;
5107         return rc;
5108 }
5109
5110 /**
5111  * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
5112  * @phba: Pointer to HBA context object.
5113  * @type:  The resource extent type to allocate.
5114  *
5115  * This function allocates the number of elements for the specified
5116  * resource type.
5117  **/
5118 static int
5119 lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
5120 {
5121         bool emb = false;
5122         uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
5123         uint16_t rsrc_id, rsrc_start, j, k;
5124         uint16_t *ids;
5125         int i, rc;
5126         unsigned long longs;
5127         unsigned long *bmask;
5128         struct lpfc_rsrc_blks *rsrc_blks;
5129         LPFC_MBOXQ_t *mbox;
5130         uint32_t length;
5131         struct lpfc_id_range *id_array = NULL;
5132         void *virtaddr = NULL;
5133         struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
5134         struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
5135         struct list_head *ext_blk_list;
5136
5137         rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5138                                             &rsrc_cnt,
5139                                             &rsrc_size);
5140         if (unlikely(rc))
5141                 return -EIO;
5142
5143         if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
5144                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5145                         "3009 No available Resource Extents "
5146                         "for resource type 0x%x: Count: 0x%x, "
5147                         "Size 0x%x\n", type, rsrc_cnt,
5148                         rsrc_size);
5149                 return -ENOMEM;
5150         }
5151
5152         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT,
5153                         "2903 Available Resource Extents "
5154                         "for resource type 0x%x: Count: 0x%x, "
5155                         "Size 0x%x\n", type, rsrc_cnt,
5156                         rsrc_size);
5157
5158         mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5159         if (!mbox)
5160                 return -ENOMEM;
5161
5162         rc = lpfc_sli4_cfg_post_extnts(phba, &rsrc_cnt, type, &emb, mbox);
5163         if (unlikely(rc)) {
5164                 rc = -EIO;
5165                 goto err_exit;
5166         }
5167
5168         /*
5169          * Figure out where the response is located.  Then get local pointers
5170          * to the response data.  The port does not guarantee to respond to
5171          * all extents counts request so update the local variable with the
5172          * allocated count from the port.
5173          */
5174         if (emb == LPFC_SLI4_MBX_EMBED) {
5175                 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
5176                 id_array = &rsrc_ext->u.rsp.id[0];
5177                 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
5178         } else {
5179                 virtaddr = mbox->sge_array->addr[0];
5180                 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
5181                 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
5182                 id_array = &n_rsrc->id;
5183         }
5184
5185         longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
5186         rsrc_id_cnt = rsrc_cnt * rsrc_size;
5187
5188         /*
5189          * Based on the resource size and count, correct the base and max
5190          * resource values.
5191          */
5192         length = sizeof(struct lpfc_rsrc_blks);
5193         switch (type) {
5194         case LPFC_RSC_TYPE_FCOE_RPI:
5195                 phba->sli4_hba.rpi_bmask = kzalloc(longs *
5196                                                    sizeof(unsigned long),
5197                                                    GFP_KERNEL);
5198                 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5199                         rc = -ENOMEM;
5200                         goto err_exit;
5201                 }
5202                 phba->sli4_hba.rpi_ids = kzalloc(rsrc_id_cnt *
5203                                                  sizeof(uint16_t),
5204                                                  GFP_KERNEL);
5205                 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5206                         kfree(phba->sli4_hba.rpi_bmask);
5207                         rc = -ENOMEM;
5208                         goto err_exit;
5209                 }
5210
5211                 /*
5212                  * The next_rpi was initialized with the maximum available
5213                  * count but the port may allocate a smaller number.  Catch
5214                  * that case and update the next_rpi.
5215                  */
5216                 phba->sli4_hba.next_rpi = rsrc_id_cnt;
5217
5218                 /* Initialize local ptrs for common extent processing later. */
5219                 bmask = phba->sli4_hba.rpi_bmask;
5220                 ids = phba->sli4_hba.rpi_ids;
5221                 ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5222                 break;
5223         case LPFC_RSC_TYPE_FCOE_VPI:
5224                 phba->vpi_bmask = kzalloc(longs *
5225                                           sizeof(unsigned long),
5226                                           GFP_KERNEL);
5227                 if (unlikely(!phba->vpi_bmask)) {
5228                         rc = -ENOMEM;
5229                         goto err_exit;
5230                 }
5231                 phba->vpi_ids = kzalloc(rsrc_id_cnt *
5232                                          sizeof(uint16_t),
5233                                          GFP_KERNEL);
5234                 if (unlikely(!phba->vpi_ids)) {
5235                         kfree(phba->vpi_bmask);
5236                         rc = -ENOMEM;
5237                         goto err_exit;
5238                 }
5239
5240                 /* Initialize local ptrs for common extent processing later. */
5241                 bmask = phba->vpi_bmask;
5242                 ids = phba->vpi_ids;
5243                 ext_blk_list = &phba->lpfc_vpi_blk_list;
5244                 break;
5245         case LPFC_RSC_TYPE_FCOE_XRI:
5246                 phba->sli4_hba.xri_bmask = kzalloc(longs *
5247                                                    sizeof(unsigned long),
5248                                                    GFP_KERNEL);
5249                 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5250                         rc = -ENOMEM;
5251                         goto err_exit;
5252                 }
5253                 phba->sli4_hba.xri_ids = kzalloc(rsrc_id_cnt *
5254                                                  sizeof(uint16_t),
5255                                                  GFP_KERNEL);
5256                 if (unlikely(!phba->sli4_hba.xri_ids)) {
5257                         kfree(phba->sli4_hba.xri_bmask);
5258                         rc = -ENOMEM;
5259                         goto err_exit;
5260                 }
5261
5262                 /* Initialize local ptrs for common extent processing later. */
5263                 bmask = phba->sli4_hba.xri_bmask;
5264                 ids = phba->sli4_hba.xri_ids;
5265                 ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5266                 break;
5267         case LPFC_RSC_TYPE_FCOE_VFI:
5268                 phba->sli4_hba.vfi_bmask = kzalloc(longs *
5269                                                    sizeof(unsigned long),
5270                                                    GFP_KERNEL);
5271                 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5272                         rc = -ENOMEM;
5273                         goto err_exit;
5274                 }
5275                 phba->sli4_hba.vfi_ids = kzalloc(rsrc_id_cnt *
5276                                                  sizeof(uint16_t),
5277                                                  GFP_KERNEL);
5278                 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5279                         kfree(phba->sli4_hba.vfi_bmask);
5280                         rc = -ENOMEM;
5281                         goto err_exit;
5282                 }
5283
5284                 /* Initialize local ptrs for common extent processing later. */
5285                 bmask = phba->sli4_hba.vfi_bmask;
5286                 ids = phba->sli4_hba.vfi_ids;
5287                 ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5288                 break;
5289         default:
5290                 /* Unsupported Opcode.  Fail call. */
5291                 id_array = NULL;
5292                 bmask = NULL;
5293                 ids = NULL;
5294                 ext_blk_list = NULL;
5295                 goto err_exit;
5296         }
5297
5298         /*
5299          * Complete initializing the extent configuration with the
5300          * allocated ids assigned to this function.  The bitmask serves
5301          * as an index into the array and manages the available ids.  The
5302          * array just stores the ids communicated to the port via the wqes.
5303          */
5304         for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
5305                 if ((i % 2) == 0)
5306                         rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
5307                                          &id_array[k]);
5308                 else
5309                         rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
5310                                          &id_array[k]);
5311
5312                 rsrc_blks = kzalloc(length, GFP_KERNEL);
5313                 if (unlikely(!rsrc_blks)) {
5314                         rc = -ENOMEM;
5315                         kfree(bmask);
5316                         kfree(ids);
5317                         goto err_exit;
5318                 }
5319                 rsrc_blks->rsrc_start = rsrc_id;
5320                 rsrc_blks->rsrc_size = rsrc_size;
5321                 list_add_tail(&rsrc_blks->list, ext_blk_list);
5322                 rsrc_start = rsrc_id;
5323                 if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0))
5324                         phba->sli4_hba.scsi_xri_start = rsrc_start +
5325                                 lpfc_sli4_get_els_iocb_cnt(phba);
5326
5327                 while (rsrc_id < (rsrc_start + rsrc_size)) {
5328                         ids[j] = rsrc_id;
5329                         rsrc_id++;
5330                         j++;
5331                 }
5332                 /* Entire word processed.  Get next word.*/
5333                 if ((i % 2) == 1)
5334                         k++;
5335         }
5336  err_exit:
5337         lpfc_sli4_mbox_cmd_free(phba, mbox);
5338         return rc;
5339 }
5340
5341 /**
5342  * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
5343  * @phba: Pointer to HBA context object.
5344  * @type: the extent's type.
5345  *
5346  * This function deallocates all extents of a particular resource type.
5347  * SLI4 does not allow for deallocating a particular extent range.  It
5348  * is the caller's responsibility to release all kernel memory resources.
5349  **/
5350 static int
5351 lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
5352 {
5353         int rc;
5354         uint32_t length, mbox_tmo = 0;
5355         LPFC_MBOXQ_t *mbox;
5356         struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
5357         struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
5358
5359         mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5360         if (!mbox)
5361                 return -ENOMEM;
5362
5363         /*
5364          * This function sends an embedded mailbox because it only sends the
5365          * the resource type.  All extents of this type are released by the
5366          * port.
5367          */
5368         length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
5369                   sizeof(struct lpfc_sli4_cfg_mhdr));
5370         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5371                          LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
5372                          length, LPFC_SLI4_MBX_EMBED);
5373
5374         /* Send an extents count of 0 - the dealloc doesn't use it. */
5375         rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5376                                         LPFC_SLI4_MBX_EMBED);
5377         if (unlikely(rc)) {
5378                 rc = -EIO;
5379                 goto out_free_mbox;
5380         }
5381         if (!phba->sli4_hba.intr_enable)
5382                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5383         else {
5384                 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
5385                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5386         }
5387         if (unlikely(rc)) {
5388                 rc = -EIO;
5389                 goto out_free_mbox;
5390         }
5391
5392         dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
5393         if (bf_get(lpfc_mbox_hdr_status,
5394                    &dealloc_rsrc->header.cfg_shdr.response)) {
5395                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5396                                 "2919 Failed to release resource extents "
5397                                 "for type %d - Status 0x%x Add'l Status 0x%x. "
5398                                 "Resource memory not released.\n",
5399                                 type,
5400                                 bf_get(lpfc_mbox_hdr_status,
5401                                     &dealloc_rsrc->header.cfg_shdr.response),
5402                                 bf_get(lpfc_mbox_hdr_add_status,
5403                                     &dealloc_rsrc->header.cfg_shdr.response));
5404                 rc = -EIO;
5405                 goto out_free_mbox;
5406         }
5407
5408         /* Release kernel memory resources for the specific type. */
5409         switch (type) {
5410         case LPFC_RSC_TYPE_FCOE_VPI:
5411                 kfree(phba->vpi_bmask);
5412                 kfree(phba->vpi_ids);
5413                 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5414                 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5415                                     &phba->lpfc_vpi_blk_list, list) {
5416                         list_del_init(&rsrc_blk->list);
5417                         kfree(rsrc_blk);
5418                 }
5419                 break;
5420         case LPFC_RSC_TYPE_FCOE_XRI:
5421                 kfree(phba->sli4_hba.xri_bmask);
5422                 kfree(phba->sli4_hba.xri_ids);
5423                 bf_set(lpfc_xri_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5424                 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5425                                     &phba->sli4_hba.lpfc_xri_blk_list, list) {
5426                         list_del_init(&rsrc_blk->list);
5427                         kfree(rsrc_blk);
5428                 }
5429                 break;
5430         case LPFC_RSC_TYPE_FCOE_VFI:
5431                 kfree(phba->sli4_hba.vfi_bmask);
5432                 kfree(phba->sli4_hba.vfi_ids);
5433                 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5434                 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5435                                     &phba->sli4_hba.lpfc_vfi_blk_list, list) {
5436                         list_del_init(&rsrc_blk->list);
5437                         kfree(rsrc_blk);
5438                 }
5439                 break;
5440         case LPFC_RSC_TYPE_FCOE_RPI:
5441                 /* RPI bitmask and physical id array are cleaned up earlier. */
5442                 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5443                                     &phba->sli4_hba.lpfc_rpi_blk_list, list) {
5444                         list_del_init(&rsrc_blk->list);
5445                         kfree(rsrc_blk);
5446                 }
5447                 break;
5448         default:
5449                 break;
5450         }
5451
5452         bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5453
5454  out_free_mbox:
5455         mempool_free(mbox, phba->mbox_mem_pool);
5456         return rc;
5457 }
5458
5459 /**
5460  * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
5461  * @phba: Pointer to HBA context object.
5462  *
5463  * This function allocates all SLI4 resource identifiers.
5464  **/
5465 int
5466 lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
5467 {
5468         int i, rc, error = 0;
5469         uint16_t count, base;
5470         unsigned long longs;
5471
5472         if (!phba->sli4_hba.rpi_hdrs_in_use)
5473                 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
5474         if (phba->sli4_hba.extents_in_use) {
5475                 /*
5476                  * The port supports resource extents. The XRI, VPI, VFI, RPI
5477                  * resource extent count must be read and allocated before
5478                  * provisioning the resource id arrays.
5479                  */
5480                 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
5481                     LPFC_IDX_RSRC_RDY) {
5482                         /*
5483                          * Extent-based resources are set - the driver could
5484                          * be in a port reset. Figure out if any corrective
5485                          * actions need to be taken.
5486                          */
5487                         rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5488                                                  LPFC_RSC_TYPE_FCOE_VFI);
5489                         if (rc != 0)
5490                                 error++;
5491                         rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5492                                                  LPFC_RSC_TYPE_FCOE_VPI);
5493                         if (rc != 0)
5494                                 error++;
5495                         rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5496                                                  LPFC_RSC_TYPE_FCOE_XRI);
5497                         if (rc != 0)
5498                                 error++;
5499                         rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5500                                                  LPFC_RSC_TYPE_FCOE_RPI);
5501                         if (rc != 0)
5502                                 error++;
5503
5504                         /*
5505                          * It's possible that the number of resources
5506                          * provided to this port instance changed between
5507                          * resets.  Detect this condition and reallocate
5508                          * resources.  Otherwise, there is no action.
5509                          */
5510                         if (error) {
5511                                 lpfc_printf_log(phba, KERN_INFO,
5512                                                 LOG_MBOX | LOG_INIT,
5513                                                 "2931 Detected extent resource "
5514                                                 "change.  Reallocating all "
5515                                                 "extents.\n");
5516                                 rc = lpfc_sli4_dealloc_extent(phba,
5517                                                  LPFC_RSC_TYPE_FCOE_VFI);
5518                                 rc = lpfc_sli4_dealloc_extent(phba,
5519                                                  LPFC_RSC_TYPE_FCOE_VPI);
5520                                 rc = lpfc_sli4_dealloc_extent(phba,
5521                                                  LPFC_RSC_TYPE_FCOE_XRI);
5522                                 rc = lpfc_sli4_dealloc_extent(phba,
5523                                                  LPFC_RSC_TYPE_FCOE_RPI);
5524                         } else
5525                                 return 0;
5526                 }
5527
5528                 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
5529                 if (unlikely(rc))
5530                         goto err_exit;
5531
5532                 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
5533                 if (unlikely(rc))
5534                         goto err_exit;
5535
5536                 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
5537                 if (unlikely(rc))
5538                         goto err_exit;
5539
5540                 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
5541                 if (unlikely(rc))
5542                         goto err_exit;
5543                 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
5544                        LPFC_IDX_RSRC_RDY);
5545                 return rc;
5546         } else {
5547                 /*
5548                  * The port does not support resource extents.  The XRI, VPI,
5549                  * VFI, RPI resource ids were determined from READ_CONFIG.
5550                  * Just allocate the bitmasks and provision the resource id
5551                  * arrays.  If a port reset is active, the resources don't
5552                  * need any action - just exit.
5553                  */
5554                 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
5555                     LPFC_IDX_RSRC_RDY) {
5556                         lpfc_sli4_dealloc_resource_identifiers(phba);
5557                         lpfc_sli4_remove_rpis(phba);
5558                 }
5559                 /* RPIs. */
5560                 count = phba->sli4_hba.max_cfg_param.max_rpi;
5561                 base = phba->sli4_hba.max_cfg_param.rpi_base;
5562                 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5563                 phba->sli4_hba.rpi_bmask = kzalloc(longs *
5564                                                    sizeof(unsigned long),
5565                                                    GFP_KERNEL);
5566                 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5567                         rc = -ENOMEM;
5568                         goto err_exit;
5569                 }
5570                 phba->sli4_hba.rpi_ids = kzalloc(count *
5571                                                  sizeof(uint16_t),
5572                                                  GFP_KERNEL);
5573                 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5574                         rc = -ENOMEM;
5575                         goto free_rpi_bmask;
5576                 }
5577
5578                 for (i = 0; i < count; i++)
5579                         phba->sli4_hba.rpi_ids[i] = base + i;
5580
5581                 lpfc_sli4_node_prep(phba);
5582
5583                 /* VPIs. */
5584                 count = phba->sli4_hba.max_cfg_param.max_vpi;
5585                 base = phba->sli4_hba.max_cfg_param.vpi_base;
5586                 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5587                 phba->vpi_bmask = kzalloc(longs *
5588                                           sizeof(unsigned long),
5589                                           GFP_KERNEL);
5590                 if (unlikely(!phba->vpi_bmask)) {
5591                         rc = -ENOMEM;
5592                         goto free_rpi_ids;
5593                 }
5594                 phba->vpi_ids = kzalloc(count *
5595                                         sizeof(uint16_t),
5596                                         GFP_KERNEL);
5597                 if (unlikely(!phba->vpi_ids)) {
5598                         rc = -ENOMEM;
5599                         goto free_vpi_bmask;
5600                 }
5601
5602                 for (i = 0; i < count; i++)
5603                         phba->vpi_ids[i] = base + i;
5604
5605                 /* XRIs. */
5606                 count = phba->sli4_hba.max_cfg_param.max_xri;
5607                 base = phba->sli4_hba.max_cfg_param.xri_base;
5608                 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5609                 phba->sli4_hba.xri_bmask = kzalloc(longs *
5610                                                    sizeof(unsigned long),
5611                                                    GFP_KERNEL);
5612                 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5613                         rc = -ENOMEM;
5614                         goto free_vpi_ids;
5615                 }
5616                 phba->sli4_hba.xri_ids = kzalloc(count *
5617                                                  sizeof(uint16_t),
5618                                                  GFP_KERNEL);
5619                 if (unlikely(!phba->sli4_hba.xri_ids)) {
5620                         rc = -ENOMEM;
5621                         goto free_xri_bmask;
5622                 }
5623
5624                 for (i = 0; i < count; i++)
5625                         phba->sli4_hba.xri_ids[i] = base + i;
5626
5627                 /* VFIs. */
5628                 count = phba->sli4_hba.max_cfg_param.max_vfi;
5629                 base = phba->sli4_hba.max_cfg_param.vfi_base;
5630                 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5631                 phba->sli4_hba.vfi_bmask = kzalloc(longs *
5632                                                    sizeof(unsigned long),
5633                                                    GFP_KERNEL);
5634                 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5635                         rc = -ENOMEM;
5636                         goto free_xri_ids;
5637                 }
5638                 phba->sli4_hba.vfi_ids = kzalloc(count *
5639                                                  sizeof(uint16_t),
5640                                                  GFP_KERNEL);
5641                 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5642                         rc = -ENOMEM;
5643                         goto free_vfi_bmask;
5644                 }
5645
5646                 for (i = 0; i < count; i++)
5647                         phba->sli4_hba.vfi_ids[i] = base + i;
5648
5649                 /*
5650                  * Mark all resources ready.  An HBA reset doesn't need
5651                  * to reset the initialization.
5652                  */
5653                 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
5654                        LPFC_IDX_RSRC_RDY);
5655                 return 0;
5656         }
5657
5658  free_vfi_bmask:
5659         kfree(phba->sli4_hba.vfi_bmask);
5660  free_xri_ids:
5661         kfree(phba->sli4_hba.xri_ids);
5662  free_xri_bmask:
5663         kfree(phba->sli4_hba.xri_bmask);
5664  free_vpi_ids:
5665         kfree(phba->vpi_ids);
5666  free_vpi_bmask:
5667         kfree(phba->vpi_bmask);
5668  free_rpi_ids:
5669         kfree(phba->sli4_hba.rpi_ids);
5670  free_rpi_bmask:
5671         kfree(phba->sli4_hba.rpi_bmask);
5672  err_exit:
5673         return rc;
5674 }
5675
5676 /**
5677  * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
5678  * @phba: Pointer to HBA context object.
5679  *
5680  * This function allocates the number of elements for the specified
5681  * resource type.
5682  **/
5683 int
5684 lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
5685 {
5686         if (phba->sli4_hba.extents_in_use) {
5687                 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
5688                 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
5689                 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
5690                 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
5691         } else {
5692                 kfree(phba->vpi_bmask);
5693                 kfree(phba->vpi_ids);
5694                 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5695                 kfree(phba->sli4_hba.xri_bmask);
5696                 kfree(phba->sli4_hba.xri_ids);
5697                 bf_set(lpfc_xri_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5698                 kfree(phba->sli4_hba.vfi_bmask);
5699                 kfree(phba->sli4_hba.vfi_ids);
5700                 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5701                 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5702         }
5703
5704         return 0;
5705 }
5706
5707 /**
5708  * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.
5709  * @phba: Pointer to HBA context object.
5710  * @type: The resource extent type.
5711  * @extnt_count: buffer to hold port extent count response
5712  * @extnt_size: buffer to hold port extent size response.
5713  *
5714  * This function calls the port to read the host allocated extents
5715  * for a particular type.
5716  **/
5717 int
5718 lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,
5719                                uint16_t *extnt_cnt, uint16_t *extnt_size)
5720 {
5721         bool emb;
5722         int rc = 0;
5723         uint16_t curr_blks = 0;
5724         uint32_t req_len, emb_len;
5725         uint32_t alloc_len, mbox_tmo;
5726         struct list_head *blk_list_head;
5727         struct lpfc_rsrc_blks *rsrc_blk;
5728         LPFC_MBOXQ_t *mbox;
5729         void *virtaddr = NULL;
5730         struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
5731         struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
5732         union  lpfc_sli4_cfg_shdr *shdr;
5733
5734         switch (type) {
5735         case LPFC_RSC_TYPE_FCOE_VPI:
5736                 blk_list_head = &phba->lpfc_vpi_blk_list;
5737                 break;
5738         case LPFC_RSC_TYPE_FCOE_XRI:
5739                 blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;
5740                 break;
5741         case LPFC_RSC_TYPE_FCOE_VFI:
5742                 blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;
5743                 break;
5744         case LPFC_RSC_TYPE_FCOE_RPI:
5745                 blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;
5746                 break;
5747         default:
5748                 return -EIO;
5749         }
5750
5751         /* Count the number of extents currently allocatd for this type. */
5752         list_for_each_entry(rsrc_blk, blk_list_head, list) {
5753                 if (curr_blks == 0) {
5754                         /*
5755                          * The GET_ALLOCATED mailbox does not return the size,
5756                          * just the count.  The size should be just the size
5757                          * stored in the current allocated block and all sizes
5758                          * for an extent type are the same so set the return
5759                          * value now.
5760                          */
5761                         *extnt_size = rsrc_blk->rsrc_size;
5762                 }
5763                 curr_blks++;
5764         }
5765
5766         /* Calculate the total requested length of the dma memory. */
5767         req_len = curr_blks * sizeof(uint16_t);
5768
5769         /*
5770          * Calculate the size of an embedded mailbox.  The uint32_t
5771          * accounts for extents-specific word.
5772          */
5773         emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
5774                 sizeof(uint32_t);
5775
5776         /*
5777          * Presume the allocation and response will fit into an embedded
5778          * mailbox.  If not true, reconfigure to a non-embedded mailbox.
5779          */
5780         emb = LPFC_SLI4_MBX_EMBED;
5781         req_len = emb_len;
5782         if (req_len > emb_len) {
5783                 req_len = curr_blks * sizeof(uint16_t) +
5784                         sizeof(union lpfc_sli4_cfg_shdr) +
5785                         sizeof(uint32_t);
5786                 emb = LPFC_SLI4_MBX_NEMBED;
5787         }
5788
5789         mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5790         if (!mbox)
5791                 return -ENOMEM;
5792         memset(mbox, 0, sizeof(LPFC_MBOXQ_t));
5793
5794         alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5795                                      LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,
5796                                      req_len, emb);
5797         if (alloc_len < req_len) {
5798                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5799                         "2983 Allocated DMA memory size (x%x) is "
5800                         "less than the requested DMA memory "
5801                         "size (x%x)\n", alloc_len, req_len);
5802                 rc = -ENOMEM;
5803                 goto err_exit;
5804         }
5805         rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);
5806         if (unlikely(rc)) {
5807                 rc = -EIO;
5808                 goto err_exit;
5809         }
5810
5811         if (!phba->sli4_hba.intr_enable)
5812                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5813         else {
5814                 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
5815                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5816         }
5817
5818         if (unlikely(rc)) {
5819                 rc = -EIO;
5820                 goto err_exit;
5821         }
5822
5823         /*
5824          * Figure out where the response is located.  Then get local pointers
5825          * to the response data.  The port does not guarantee to respond to
5826          * all extents counts request so update the local variable with the
5827          * allocated count from the port.
5828          */
5829         if (emb == LPFC_SLI4_MBX_EMBED) {
5830                 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
5831                 shdr = &rsrc_ext->header.cfg_shdr;
5832                 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
5833         } else {
5834                 virtaddr = mbox->sge_array->addr[0];
5835                 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
5836                 shdr = &n_rsrc->cfg_shdr;
5837                 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
5838         }
5839
5840         if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {
5841                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5842                         "2984 Failed to read allocated resources "
5843                         "for type %d - Status 0x%x Add'l Status 0x%x.\n",
5844                         type,
5845                         bf_get(lpfc_mbox_hdr_status, &shdr->response),
5846                         bf_get(lpfc_mbox_hdr_add_status, &shdr->response));
5847                 rc = -EIO;
5848                 goto err_exit;
5849         }
5850  err_exit:
5851         lpfc_sli4_mbox_cmd_free(phba, mbox);
5852         return rc;
5853 }
5854
5855 /**
5856  * lpfc_sli4_hba_setup - SLI4 device intialization PCI function
5857  * @phba: Pointer to HBA context object.
5858  *
5859  * This function is the main SLI4 device intialization PCI function. This
5860  * function is called by the HBA intialization code, HBA reset code and
5861  * HBA error attention handler code. Caller is not required to hold any
5862  * locks.
5863  **/
5864 int
5865 lpfc_sli4_hba_setup(struct lpfc_hba *phba)
5866 {
5867         int rc;
5868         LPFC_MBOXQ_t *mboxq;
5869         struct lpfc_mqe *mqe;
5870         uint8_t *vpd;
5871         uint32_t vpd_size;
5872         uint32_t ftr_rsp = 0;
5873         struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
5874         struct lpfc_vport *vport = phba->pport;
5875         struct lpfc_dmabuf *mp;
5876
5877         /* Perform a PCI function reset to start from clean */
5878         rc = lpfc_pci_function_reset(phba);
5879         if (unlikely(rc))
5880                 return -ENODEV;
5881
5882         /* Check the HBA Host Status Register for readyness */
5883         rc = lpfc_sli4_post_status_check(phba);
5884         if (unlikely(rc))
5885                 return -ENODEV;
5886         else {
5887                 spin_lock_irq(&phba->hbalock);
5888                 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
5889                 spin_unlock_irq(&phba->hbalock);
5890         }
5891
5892         /*
5893          * Allocate a single mailbox container for initializing the
5894          * port.
5895          */
5896         mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5897         if (!mboxq)
5898                 return -ENOMEM;
5899
5900         /* Issue READ_REV to collect vpd and FW information. */
5901         vpd_size = SLI4_PAGE_SIZE;
5902         vpd = kzalloc(vpd_size, GFP_KERNEL);
5903         if (!vpd) {
5904                 rc = -ENOMEM;
5905                 goto out_free_mbox;
5906         }
5907
5908         rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
5909         if (unlikely(rc)) {
5910                 kfree(vpd);
5911                 goto out_free_mbox;
5912         }
5913         mqe = &mboxq->u.mqe;
5914         phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
5915         if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev))
5916                 phba->hba_flag |= HBA_FCOE_MODE;
5917         else
5918                 phba->hba_flag &= ~HBA_FCOE_MODE;
5919
5920         if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
5921                 LPFC_DCBX_CEE_MODE)
5922                 phba->hba_flag |= HBA_FIP_SUPPORT;
5923         else
5924                 phba->hba_flag &= ~HBA_FIP_SUPPORT;
5925
5926         if (phba->sli_rev != LPFC_SLI_REV4) {
5927                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5928                         "0376 READ_REV Error. SLI Level %d "
5929                         "FCoE enabled %d\n",
5930                         phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
5931                 rc = -EIO;
5932                 kfree(vpd);
5933                 goto out_free_mbox;
5934         }
5935
5936         /*
5937          * Continue initialization with default values even if driver failed
5938          * to read FCoE param config regions, only read parameters if the
5939          * board is FCoE
5940          */
5941         if (phba->hba_flag & HBA_FCOE_MODE &&
5942             lpfc_sli4_read_fcoe_params(phba))
5943                 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
5944                         "2570 Failed to read FCoE parameters\n");
5945
5946         /*
5947          * Retrieve sli4 device physical port name, failure of doing it
5948          * is considered as non-fatal.
5949          */
5950         rc = lpfc_sli4_retrieve_pport_name(phba);
5951         if (!rc)
5952                 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5953                                 "3080 Successful retrieving SLI4 device "
5954                                 "physical port name: %s.\n", phba->Port);
5955
5956         /*
5957          * Evaluate the read rev and vpd data. Populate the driver
5958          * state with the results. If this routine fails, the failure
5959          * is not fatal as the driver will use generic values.
5960          */
5961         rc = lpfc_parse_vpd(phba, vpd, vpd_size);
5962         if (unlikely(!rc)) {
5963                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5964                                 "0377 Error %d parsing vpd. "
5965                                 "Using defaults.\n", rc);
5966                 rc = 0;
5967         }
5968         kfree(vpd);
5969
5970         /* Save information as VPD data */
5971         phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
5972         phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
5973         phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
5974         phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
5975                                          &mqe->un.read_rev);
5976         phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
5977                                        &mqe->un.read_rev);
5978         phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
5979                                             &mqe->un.read_rev);
5980         phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
5981                                            &mqe->un.read_rev);
5982         phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
5983         memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
5984         phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
5985         memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
5986         phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
5987         memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
5988         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5989                         "(%d):0380 READ_REV Status x%x "
5990                         "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
5991                         mboxq->vport ? mboxq->vport->vpi : 0,
5992                         bf_get(lpfc_mqe_status, mqe),
5993                         phba->vpd.rev.opFwName,
5994                         phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
5995                         phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
5996
5997         /*
5998          * Discover the port's supported feature set and match it against the
5999          * hosts requests.
6000          */
6001         lpfc_request_features(phba, mboxq);
6002         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6003         if (unlikely(rc)) {
6004                 rc = -EIO;
6005                 goto out_free_mbox;
6006         }
6007
6008         /*
6009          * The port must support FCP initiator mode as this is the
6010          * only mode running in the host.
6011          */
6012         if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
6013                 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6014                                 "0378 No support for fcpi mode.\n");
6015                 ftr_rsp++;
6016         }
6017         if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
6018                 phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
6019         else
6020                 phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
6021         /*
6022          * If the port cannot support the host's requested features
6023          * then turn off the global config parameters to disable the
6024          * feature in the driver.  This is not a fatal error.
6025          */
6026         phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
6027         if (phba->cfg_enable_bg) {
6028                 if (bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))
6029                         phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
6030                 else
6031                         ftr_rsp++;
6032         }
6033
6034         if (phba->max_vpi && phba->cfg_enable_npiv &&
6035             !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
6036                 ftr_rsp++;
6037
6038         if (ftr_rsp) {
6039                 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6040                                 "0379 Feature Mismatch Data: x%08x %08x "
6041                                 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
6042                                 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
6043                                 phba->cfg_enable_npiv, phba->max_vpi);
6044                 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
6045                         phba->cfg_enable_bg = 0;
6046                 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
6047                         phba->cfg_enable_npiv = 0;
6048         }
6049
6050         /* These SLI3 features are assumed in SLI4 */
6051         spin_lock_irq(&phba->hbalock);
6052         phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
6053         spin_unlock_irq(&phba->hbalock);
6054
6055         /*
6056          * Allocate all resources (xri,rpi,vpi,vfi) now.  Subsequent
6057          * calls depends on these resources to complete port setup.
6058          */
6059         rc = lpfc_sli4_alloc_resource_identifiers(phba);
6060         if (rc) {
6061                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6062                                 "2920 Failed to alloc Resource IDs "
6063                                 "rc = x%x\n", rc);
6064                 goto out_free_mbox;
6065         }
6066         /* update physical xri mappings in the scsi buffers */
6067         lpfc_scsi_buf_update(phba);
6068
6069         /* Read the port's service parameters. */
6070         rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
6071         if (rc) {
6072                 phba->link_state = LPFC_HBA_ERROR;
6073                 rc = -ENOMEM;
6074                 goto out_free_mbox;
6075         }
6076
6077         mboxq->vport = vport;
6078         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6079         mp = (struct lpfc_dmabuf *) mboxq->context1;
6080         if (rc == MBX_SUCCESS) {
6081                 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
6082                 rc = 0;
6083         }
6084
6085         /*
6086          * This memory was allocated by the lpfc_read_sparam routine. Release
6087          * it to the mbuf pool.
6088          */
6089         lpfc_mbuf_free(phba, mp->virt, mp->phys);
6090         kfree(mp);
6091         mboxq->context1 = NULL;
6092         if (unlikely(rc)) {
6093                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6094                                 "0382 READ_SPARAM command failed "
6095                                 "status %d, mbxStatus x%x\n",
6096                                 rc, bf_get(lpfc_mqe_status, mqe));
6097                 phba->link_state = LPFC_HBA_ERROR;
6098                 rc = -EIO;
6099                 goto out_free_mbox;
6100         }
6101
6102         lpfc_update_vport_wwn(vport);
6103
6104         /* Update the fc_host data structures with new wwn. */
6105         fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
6106         fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
6107
6108         /* Register SGL pool to the device using non-embedded mailbox command */
6109         if (!phba->sli4_hba.extents_in_use) {
6110                 rc = lpfc_sli4_post_els_sgl_list(phba);
6111                 if (unlikely(rc)) {
6112                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6113                                         "0582 Error %d during els sgl post "
6114                                         "operation\n", rc);
6115                         rc = -ENODEV;
6116                         goto out_free_mbox;
6117                 }
6118         } else {
6119                 rc = lpfc_sli4_post_els_sgl_list_ext(phba);
6120                 if (unlikely(rc)) {
6121                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6122                                         "2560 Error %d during els sgl post "
6123                                         "operation\n", rc);
6124                         rc = -ENODEV;
6125                         goto out_free_mbox;
6126                 }
6127         }
6128
6129         /* Register SCSI SGL pool to the device */
6130         rc = lpfc_sli4_repost_scsi_sgl_list(phba);
6131         if (unlikely(rc)) {
6132                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6133                                 "0383 Error %d during scsi sgl post "
6134                                 "operation\n", rc);
6135                 /* Some Scsi buffers were moved to the abort scsi list */
6136                 /* A pci function reset will repost them */
6137                 rc = -ENODEV;
6138                 goto out_free_mbox;
6139         }
6140
6141         /* Post the rpi header region to the device. */
6142         rc = lpfc_sli4_post_all_rpi_hdrs(phba);
6143         if (unlikely(rc)) {
6144                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6145                                 "0393 Error %d during rpi post operation\n",
6146                                 rc);
6147                 rc = -ENODEV;
6148                 goto out_free_mbox;
6149         }
6150
6151         /* Create all the SLI4 queues */
6152         rc = lpfc_sli4_queue_create(phba);
6153         if (rc) {
6154                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6155                                 "3089 Failed to allocate queues\n");
6156                 rc = -ENODEV;
6157                 goto out_stop_timers;
6158         }
6159         /* Set up all the queues to the device */
6160         rc = lpfc_sli4_queue_setup(phba);
6161         if (unlikely(rc)) {
6162                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6163                                 "0381 Error %d during queue setup.\n ", rc);
6164                 goto out_destroy_queue;
6165         }
6166
6167         /* Arm the CQs and then EQs on device */
6168         lpfc_sli4_arm_cqeq_intr(phba);
6169
6170         /* Indicate device interrupt mode */
6171         phba->sli4_hba.intr_enable = 1;
6172
6173         /* Allow asynchronous mailbox command to go through */
6174         spin_lock_irq(&phba->hbalock);
6175         phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
6176         spin_unlock_irq(&phba->hbalock);
6177
6178         /* Post receive buffers to the device */
6179         lpfc_sli4_rb_setup(phba);
6180
6181         /* Reset HBA FCF states after HBA reset */
6182         phba->fcf.fcf_flag = 0;
6183         phba->fcf.current_rec.flag = 0;
6184
6185         /* Start the ELS watchdog timer */
6186         mod_timer(&vport->els_tmofunc,
6187                   jiffies + HZ * (phba->fc_ratov * 2));
6188
6189         /* Start heart beat timer */
6190         mod_timer(&phba->hb_tmofunc,
6191                   jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
6192         phba->hb_outstanding = 0;
6193         phba->last_completion_time = jiffies;
6194
6195         /* Start error attention (ERATT) polling timer */
6196         mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
6197
6198         /* Enable PCIe device Advanced Error Reporting (AER) if configured */
6199         if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
6200                 rc = pci_enable_pcie_error_reporting(phba->pcidev);
6201                 if (!rc) {
6202                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6203                                         "2829 This device supports "
6204                                         "Advanced Error Reporting (AER)\n");
6205                         spin_lock_irq(&phba->hbalock);
6206                         phba->hba_flag |= HBA_AER_ENABLED;
6207                         spin_unlock_irq(&phba->hbalock);
6208                 } else {
6209                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6210                                         "2830 This device does not support "
6211                                         "Advanced Error Reporting (AER)\n");
6212                         phba->cfg_aer_support = 0;
6213                 }
6214                 rc = 0;
6215         }
6216
6217         if (!(phba->hba_flag & HBA_FCOE_MODE)) {
6218                 /*
6219                  * The FC Port needs to register FCFI (index 0)
6220                  */
6221                 lpfc_reg_fcfi(phba, mboxq);
6222                 mboxq->vport = phba->pport;
6223                 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6224                 if (rc != MBX_SUCCESS)
6225                         goto out_unset_queue;
6226                 rc = 0;
6227                 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
6228                                         &mboxq->u.mqe.un.reg_fcfi);
6229
6230                 /* Check if the port is configured to be disabled */
6231                 lpfc_sli_read_link_ste(phba);
6232         }
6233
6234         /*
6235          * The port is ready, set the host's link state to LINK_DOWN
6236          * in preparation for link interrupts.
6237          */
6238         spin_lock_irq(&phba->hbalock);
6239         phba->link_state = LPFC_LINK_DOWN;
6240         spin_unlock_irq(&phba->hbalock);
6241         if (!(phba->hba_flag & HBA_FCOE_MODE) &&
6242             (phba->hba_flag & LINK_DISABLED)) {
6243                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
6244                                 "3103 Adapter Link is disabled.\n");
6245                 lpfc_down_link(phba, mboxq);
6246                 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6247                 if (rc != MBX_SUCCESS) {
6248                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
6249                                         "3104 Adapter failed to issue "
6250                                         "DOWN_LINK mbox cmd, rc:x%x\n", rc);
6251                         goto out_unset_queue;
6252                 }
6253         } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
6254                 /* don't perform init_link on SLI4 FC port loopback test */
6255                 if (!(phba->link_flag & LS_LOOPBACK_MODE)) {
6256                         rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
6257                         if (rc)
6258                                 goto out_unset_queue;
6259                 }
6260         }
6261         mempool_free(mboxq, phba->mbox_mem_pool);
6262         return rc;
6263 out_unset_queue:
6264         /* Unset all the queues set up in this routine when error out */
6265         lpfc_sli4_queue_unset(phba);
6266 out_destroy_queue:
6267         lpfc_sli4_queue_destroy(phba);
6268 out_stop_timers:
6269         lpfc_stop_hba_timers(phba);
6270 out_free_mbox:
6271         mempool_free(mboxq, phba->mbox_mem_pool);
6272         return rc;
6273 }
6274
6275 /**
6276  * lpfc_mbox_timeout - Timeout call back function for mbox timer
6277  * @ptr: context object - pointer to hba structure.
6278  *
6279  * This is the callback function for mailbox timer. The mailbox
6280  * timer is armed when a new mailbox command is issued and the timer
6281  * is deleted when the mailbox complete. The function is called by
6282  * the kernel timer code when a mailbox does not complete within
6283  * expected time. This function wakes up the worker thread to
6284  * process the mailbox timeout and returns. All the processing is
6285  * done by the worker thread function lpfc_mbox_timeout_handler.
6286  **/
6287 void
6288 lpfc_mbox_timeout(unsigned long ptr)
6289 {
6290         struct lpfc_hba  *phba = (struct lpfc_hba *) ptr;
6291         unsigned long iflag;
6292         uint32_t tmo_posted;
6293
6294         spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
6295         tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
6296         if (!tmo_posted)
6297                 phba->pport->work_port_events |= WORKER_MBOX_TMO;
6298         spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
6299
6300         if (!tmo_posted)
6301                 lpfc_worker_wake_up(phba);
6302         return;
6303 }
6304
6305
6306 /**
6307  * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
6308  * @phba: Pointer to HBA context object.
6309  *
6310  * This function is called from worker thread when a mailbox command times out.
6311  * The caller is not required to hold any locks. This function will reset the
6312  * HBA and recover all the pending commands.
6313  **/
6314 void
6315 lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
6316 {
6317         LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
6318         MAILBOX_t *mb = &pmbox->u.mb;
6319         struct lpfc_sli *psli = &phba->sli;
6320         struct lpfc_sli_ring *pring;
6321
6322         /* Check the pmbox pointer first.  There is a race condition
6323          * between the mbox timeout handler getting executed in the
6324          * worklist and the mailbox actually completing. When this
6325          * race condition occurs, the mbox_active will be NULL.
6326          */
6327         spin_lock_irq(&phba->hbalock);
6328         if (pmbox == NULL) {
6329                 lpfc_printf_log(phba, KERN_WARNING,
6330                                 LOG_MBOX | LOG_SLI,
6331                                 "0353 Active Mailbox cleared - mailbox timeout "
6332                                 "exiting\n");
6333                 spin_unlock_irq(&phba->hbalock);
6334                 return;
6335         }
6336
6337         /* Mbox cmd <mbxCommand> timeout */
6338         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6339                         "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
6340                         mb->mbxCommand,
6341                         phba->pport->port_state,
6342                         phba->sli.sli_flag,
6343                         phba->sli.mbox_active);
6344         spin_unlock_irq(&phba->hbalock);
6345
6346         /* Setting state unknown so lpfc_sli_abort_iocb_ring
6347          * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
6348          * it to fail all outstanding SCSI IO.
6349          */
6350         spin_lock_irq(&phba->pport->work_port_lock);
6351         phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
6352         spin_unlock_irq(&phba->pport->work_port_lock);
6353         spin_lock_irq(&phba->hbalock);
6354         phba->link_state = LPFC_LINK_UNKNOWN;
6355         psli->sli_flag &= ~LPFC_SLI_ACTIVE;
6356         spin_unlock_irq(&phba->hbalock);
6357
6358         pring = &psli->ring[psli->fcp_ring];
6359         lpfc_sli_abort_iocb_ring(phba, pring);
6360
6361         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6362                         "0345 Resetting board due to mailbox timeout\n");
6363
6364         /* Reset the HBA device */
6365         lpfc_reset_hba(phba);
6366 }
6367
6368 /**
6369  * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
6370  * @phba: Pointer to HBA context object.
6371  * @pmbox: Pointer to mailbox object.
6372  * @flag: Flag indicating how the mailbox need to be processed.
6373  *
6374  * This function is called by discovery code and HBA management code
6375  * to submit a mailbox command to firmware with SLI-3 interface spec. This
6376  * function gets the hbalock to protect the data structures.
6377  * The mailbox command can be submitted in polling mode, in which case
6378  * this function will wait in a polling loop for the completion of the
6379  * mailbox.
6380  * If the mailbox is submitted in no_wait mode (not polling) the
6381  * function will submit the command and returns immediately without waiting
6382  * for the mailbox completion. The no_wait is supported only when HBA
6383  * is in SLI2/SLI3 mode - interrupts are enabled.
6384  * The SLI interface allows only one mailbox pending at a time. If the
6385  * mailbox is issued in polling mode and there is already a mailbox
6386  * pending, then the function will return an error. If the mailbox is issued
6387  * in NO_WAIT mode and there is a mailbox pending already, the function
6388  * will return MBX_BUSY after queuing the mailbox into mailbox queue.
6389  * The sli layer owns the mailbox object until the completion of mailbox
6390  * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
6391  * return codes the caller owns the mailbox command after the return of
6392  * the function.
6393  **/
6394 static int
6395 lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
6396                        uint32_t flag)
6397 {
6398         MAILBOX_t *mb;
6399         struct lpfc_sli *psli = &phba->sli;
6400         uint32_t status, evtctr;
6401         uint32_t ha_copy, hc_copy;
6402         int i;
6403         unsigned long timeout;
6404         unsigned long drvr_flag = 0;
6405         uint32_t word0, ldata;
6406         void __iomem *to_slim;
6407         int processing_queue = 0;
6408
6409         spin_lock_irqsave(&phba->hbalock, drvr_flag);
6410         if (!pmbox) {
6411                 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
6412                 /* processing mbox queue from intr_handler */
6413                 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
6414                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6415                         return MBX_SUCCESS;
6416                 }
6417                 processing_queue = 1;
6418                 pmbox = lpfc_mbox_get(phba);
6419                 if (!pmbox) {
6420                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6421                         return MBX_SUCCESS;
6422                 }
6423         }
6424
6425         if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
6426                 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
6427                 if(!pmbox->vport) {
6428                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6429                         lpfc_printf_log(phba, KERN_ERR,
6430                                         LOG_MBOX | LOG_VPORT,
6431                                         "1806 Mbox x%x failed. No vport\n",
6432                                         pmbox->u.mb.mbxCommand);
6433                         dump_stack();
6434                         goto out_not_finished;
6435                 }
6436         }
6437
6438         /* If the PCI channel is in offline state, do not post mbox. */
6439         if (unlikely(pci_channel_offline(phba->pcidev))) {
6440                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6441                 goto out_not_finished;
6442         }
6443
6444         /* If HBA has a deferred error attention, fail the iocb. */
6445         if (unlikely(phba->hba_flag & DEFER_ERATT)) {
6446                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6447                 goto out_not_finished;
6448         }
6449
6450         psli = &phba->sli;
6451
6452         mb = &pmbox->u.mb;
6453         status = MBX_SUCCESS;
6454
6455         if (phba->link_state == LPFC_HBA_ERROR) {
6456                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6457
6458                 /* Mbox command <mbxCommand> cannot issue */
6459                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6460                                 "(%d):0311 Mailbox command x%x cannot "
6461                                 "issue Data: x%x x%x\n",
6462                                 pmbox->vport ? pmbox->vport->vpi : 0,
6463                                 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
6464                 goto out_not_finished;
6465         }
6466
6467         if (mb->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
6468                 if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
6469                         !(hc_copy & HC_MBINT_ENA)) {
6470                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6471                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6472                                 "(%d):2528 Mailbox command x%x cannot "
6473                                 "issue Data: x%x x%x\n",
6474                                 pmbox->vport ? pmbox->vport->vpi : 0,
6475                                 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
6476                         goto out_not_finished;
6477                 }
6478         }
6479
6480         if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
6481                 /* Polling for a mbox command when another one is already active
6482                  * is not allowed in SLI. Also, the driver must have established
6483                  * SLI2 mode to queue and process multiple mbox commands.
6484                  */
6485
6486                 if (flag & MBX_POLL) {
6487                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6488
6489                         /* Mbox command <mbxCommand> cannot issue */
6490                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6491                                         "(%d):2529 Mailbox command x%x "
6492                                         "cannot issue Data: x%x x%x\n",
6493                                         pmbox->vport ? pmbox->vport->vpi : 0,
6494                                         pmbox->u.mb.mbxCommand,
6495                                         psli->sli_flag, flag);
6496                         goto out_not_finished;
6497                 }
6498
6499                 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
6500                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6501                         /* Mbox command <mbxCommand> cannot issue */
6502                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6503                                         "(%d):2530 Mailbox command x%x "
6504                                         "cannot issue Data: x%x x%x\n",
6505                                         pmbox->vport ? pmbox->vport->vpi : 0,
6506                                         pmbox->u.mb.mbxCommand,
6507                                         psli->sli_flag, flag);
6508                         goto out_not_finished;
6509                 }
6510
6511                 /* Another mailbox command is still being processed, queue this
6512                  * command to be processed later.
6513                  */
6514                 lpfc_mbox_put(phba, pmbox);
6515
6516                 /* Mbox cmd issue - BUSY */
6517                 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6518                                 "(%d):0308 Mbox cmd issue - BUSY Data: "
6519                                 "x%x x%x x%x x%x\n",
6520                                 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
6521                                 mb->mbxCommand, phba->pport->port_state,
6522                                 psli->sli_flag, flag);
6523
6524                 psli->slistat.mbox_busy++;
6525                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6526
6527                 if (pmbox->vport) {
6528                         lpfc_debugfs_disc_trc(pmbox->vport,
6529                                 LPFC_DISC_TRC_MBOX_VPORT,
6530                                 "MBOX Bsy vport:  cmd:x%x mb:x%x x%x",
6531                                 (uint32_t)mb->mbxCommand,
6532                                 mb->un.varWords[0], mb->un.varWords[1]);
6533                 }
6534                 else {
6535                         lpfc_debugfs_disc_trc(phba->pport,
6536                                 LPFC_DISC_TRC_MBOX,
6537                                 "MBOX Bsy:        cmd:x%x mb:x%x x%x",
6538                                 (uint32_t)mb->mbxCommand,
6539                                 mb->un.varWords[0], mb->un.varWords[1]);
6540                 }
6541
6542                 return MBX_BUSY;
6543         }
6544
6545         psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
6546
6547         /* If we are not polling, we MUST be in SLI2 mode */
6548         if (flag != MBX_POLL) {
6549                 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
6550                     (mb->mbxCommand != MBX_KILL_BOARD)) {
6551                         psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
6552                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6553                         /* Mbox command <mbxCommand> cannot issue */
6554                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6555                                         "(%d):2531 Mailbox command x%x "
6556                                         "cannot issue Data: x%x x%x\n",
6557                                         pmbox->vport ? pmbox->vport->vpi : 0,
6558                                         pmbox->u.mb.mbxCommand,
6559                                         psli->sli_flag, flag);
6560                         goto out_not_finished;
6561                 }
6562                 /* timeout active mbox command */
6563                 mod_timer(&psli->mbox_tmo, (jiffies +
6564                                (HZ * lpfc_mbox_tmo_val(phba, pmbox))));
6565         }
6566
6567         /* Mailbox cmd <cmd> issue */
6568         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6569                         "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
6570                         "x%x\n",
6571                         pmbox->vport ? pmbox->vport->vpi : 0,
6572                         mb->mbxCommand, phba->pport->port_state,
6573                         psli->sli_flag, flag);
6574
6575         if (mb->mbxCommand != MBX_HEARTBEAT) {
6576                 if (pmbox->vport) {
6577                         lpfc_debugfs_disc_trc(pmbox->vport,
6578                                 LPFC_DISC_TRC_MBOX_VPORT,
6579                                 "MBOX Send vport: cmd:x%x mb:x%x x%x",
6580                                 (uint32_t)mb->mbxCommand,
6581                                 mb->un.varWords[0], mb->un.varWords[1]);
6582                 }
6583                 else {
6584                         lpfc_debugfs_disc_trc(phba->pport,
6585                                 LPFC_DISC_TRC_MBOX,
6586                                 "MBOX Send:       cmd:x%x mb:x%x x%x",
6587                                 (uint32_t)mb->mbxCommand,
6588                                 mb->un.varWords[0], mb->un.varWords[1]);
6589                 }
6590         }
6591
6592         psli->slistat.mbox_cmd++;
6593         evtctr = psli->slistat.mbox_event;
6594
6595         /* next set own bit for the adapter and copy over command word */
6596         mb->mbxOwner = OWN_CHIP;
6597
6598         if (psli->sli_flag & LPFC_SLI_ACTIVE) {
6599                 /* Populate mbox extension offset word. */
6600                 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
6601                         *(((uint32_t *)mb) + pmbox->mbox_offset_word)
6602                                 = (uint8_t *)phba->mbox_ext
6603                                   - (uint8_t *)phba->mbox;
6604                 }
6605
6606                 /* Copy the mailbox extension data */
6607                 if (pmbox->in_ext_byte_len && pmbox->context2) {
6608                         lpfc_sli_pcimem_bcopy(pmbox->context2,
6609                                 (uint8_t *)phba->mbox_ext,
6610                                 pmbox->in_ext_byte_len);
6611                 }
6612                 /* Copy command data to host SLIM area */
6613                 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
6614         } else {
6615                 /* Populate mbox extension offset word. */
6616                 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
6617                         *(((uint32_t *)mb) + pmbox->mbox_offset_word)
6618                                 = MAILBOX_HBA_EXT_OFFSET;
6619
6620                 /* Copy the mailbox extension data */
6621                 if (pmbox->in_ext_byte_len && pmbox->context2) {
6622                         lpfc_memcpy_to_slim(phba->MBslimaddr +
6623                                 MAILBOX_HBA_EXT_OFFSET,
6624                                 pmbox->context2, pmbox->in_ext_byte_len);
6625
6626                 }
6627                 if (mb->mbxCommand == MBX_CONFIG_PORT) {
6628                         /* copy command data into host mbox for cmpl */
6629                         lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
6630                 }
6631
6632                 /* First copy mbox command data to HBA SLIM, skip past first
6633                    word */
6634                 to_slim = phba->MBslimaddr + sizeof (uint32_t);
6635                 lpfc_memcpy_to_slim(to_slim, &mb->un.varWords[0],
6636                             MAILBOX_CMD_SIZE - sizeof (uint32_t));
6637
6638                 /* Next copy over first word, with mbxOwner set */
6639                 ldata = *((uint32_t *)mb);
6640                 to_slim = phba->MBslimaddr;
6641                 writel(ldata, to_slim);
6642                 readl(to_slim); /* flush */
6643
6644                 if (mb->mbxCommand == MBX_CONFIG_PORT) {
6645                         /* switch over to host mailbox */
6646                         psli->sli_flag |= LPFC_SLI_ACTIVE;
6647                 }
6648         }
6649
6650         wmb();
6651
6652         switch (flag) {
6653         case MBX_NOWAIT:
6654                 /* Set up reference to mailbox command */
6655                 psli->mbox_active = pmbox;
6656                 /* Interrupt board to do it */
6657                 writel(CA_MBATT, phba->CAregaddr);
6658                 readl(phba->CAregaddr); /* flush */
6659                 /* Don't wait for it to finish, just return */
6660                 break;
6661
6662         case MBX_POLL:
6663                 /* Set up null reference to mailbox command */
6664                 psli->mbox_active = NULL;
6665                 /* Interrupt board to do it */
6666                 writel(CA_MBATT, phba->CAregaddr);
6667                 readl(phba->CAregaddr); /* flush */
6668
6669                 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
6670                         /* First read mbox status word */
6671                         word0 = *((uint32_t *)phba->mbox);
6672                         word0 = le32_to_cpu(word0);
6673                 } else {
6674                         /* First read mbox status word */
6675                         if (lpfc_readl(phba->MBslimaddr, &word0)) {
6676                                 spin_unlock_irqrestore(&phba->hbalock,
6677                                                        drvr_flag);
6678                                 goto out_not_finished;
6679                         }
6680                 }
6681
6682                 /* Read the HBA Host Attention Register */
6683                 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
6684                         spin_unlock_irqrestore(&phba->hbalock,
6685                                                        drvr_flag);
6686                         goto out_not_finished;
6687                 }
6688                 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
6689                                                         1000) + jiffies;
6690                 i = 0;
6691                 /* Wait for command to complete */
6692                 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
6693                        (!(ha_copy & HA_MBATT) &&
6694                         (phba->link_state > LPFC_WARM_START))) {
6695                         if (time_after(jiffies, timeout)) {
6696                                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
6697                                 spin_unlock_irqrestore(&phba->hbalock,
6698                                                        drvr_flag);
6699                                 goto out_not_finished;
6700                         }
6701
6702                         /* Check if we took a mbox interrupt while we were
6703                            polling */
6704                         if (((word0 & OWN_CHIP) != OWN_CHIP)
6705                             && (evtctr != psli->slistat.mbox_event))
6706                                 break;
6707
6708                         if (i++ > 10) {
6709                                 spin_unlock_irqrestore(&phba->hbalock,
6710                                                        drvr_flag);
6711                                 msleep(1);
6712                                 spin_lock_irqsave(&phba->hbalock, drvr_flag);
6713                         }
6714
6715                         if (psli->sli_flag & LPFC_SLI_ACTIVE) {
6716                                 /* First copy command data */
6717                                 word0 = *((uint32_t *)phba->mbox);
6718                                 word0 = le32_to_cpu(word0);
6719                                 if (mb->mbxCommand == MBX_CONFIG_PORT) {
6720                                         MAILBOX_t *slimmb;
6721                                         uint32_t slimword0;
6722                                         /* Check real SLIM for any errors */
6723                                         slimword0 = readl(phba->MBslimaddr);
6724                                         slimmb = (MAILBOX_t *) & slimword0;
6725                                         if (((slimword0 & OWN_CHIP) != OWN_CHIP)
6726                                             && slimmb->mbxStatus) {
6727                                                 psli->sli_flag &=
6728                                                     ~LPFC_SLI_ACTIVE;
6729                                                 word0 = slimword0;
6730                                         }
6731                                 }
6732                         } else {
6733                                 /* First copy command data */
6734                                 word0 = readl(phba->MBslimaddr);
6735                         }
6736                         /* Read the HBA Host Attention Register */
6737                         if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
6738                                 spin_unlock_irqrestore(&phba->hbalock,
6739                                                        drvr_flag);
6740                                 goto out_not_finished;
6741                         }
6742                 }
6743
6744                 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
6745                         /* copy results back to user */
6746                         lpfc_sli_pcimem_bcopy(phba->mbox, mb, MAILBOX_CMD_SIZE);
6747                         /* Copy the mailbox extension data */
6748                         if (pmbox->out_ext_byte_len && pmbox->context2) {
6749                                 lpfc_sli_pcimem_bcopy(phba->mbox_ext,
6750                                                       pmbox->context2,
6751                                                       pmbox->out_ext_byte_len);
6752                         }
6753                 } else {
6754                         /* First copy command data */
6755                         lpfc_memcpy_from_slim(mb, phba->MBslimaddr,
6756                                                         MAILBOX_CMD_SIZE);
6757                         /* Copy the mailbox extension data */
6758                         if (pmbox->out_ext_byte_len && pmbox->context2) {
6759                                 lpfc_memcpy_from_slim(pmbox->context2,
6760                                         phba->MBslimaddr +
6761                                         MAILBOX_HBA_EXT_OFFSET,
6762                                         pmbox->out_ext_byte_len);
6763                         }
6764                 }
6765
6766                 writel(HA_MBATT, phba->HAregaddr);
6767                 readl(phba->HAregaddr); /* flush */
6768
6769                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
6770                 status = mb->mbxStatus;
6771         }
6772
6773         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6774         return status;
6775
6776 out_not_finished:
6777         if (processing_queue) {
6778                 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
6779                 lpfc_mbox_cmpl_put(phba, pmbox);
6780         }
6781         return MBX_NOT_FINISHED;
6782 }
6783
6784 /**
6785  * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
6786  * @phba: Pointer to HBA context object.
6787  *
6788  * The function blocks the posting of SLI4 asynchronous mailbox commands from
6789  * the driver internal pending mailbox queue. It will then try to wait out the
6790  * possible outstanding mailbox command before return.
6791  *
6792  * Returns:
6793  *      0 - the outstanding mailbox command completed; otherwise, the wait for
6794  *      the outstanding mailbox command timed out.
6795  **/
6796 static int
6797 lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
6798 {
6799         struct lpfc_sli *psli = &phba->sli;
6800         int rc = 0;
6801         unsigned long timeout = 0;
6802
6803         /* Mark the asynchronous mailbox command posting as blocked */
6804         spin_lock_irq(&phba->hbalock);
6805         psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
6806         /* Determine how long we might wait for the active mailbox
6807          * command to be gracefully completed by firmware.
6808          */
6809         if (phba->sli.mbox_active)
6810                 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
6811                                                 phba->sli.mbox_active) *
6812                                                 1000) + jiffies;
6813         spin_unlock_irq(&phba->hbalock);
6814
6815         /* Wait for the outstnading mailbox command to complete */
6816         while (phba->sli.mbox_active) {
6817                 /* Check active mailbox complete status every 2ms */
6818                 msleep(2);
6819                 if (time_after(jiffies, timeout)) {
6820                         /* Timeout, marked the outstanding cmd not complete */
6821                         rc = 1;
6822                         break;
6823                 }
6824         }
6825
6826         /* Can not cleanly block async mailbox command, fails it */
6827         if (rc) {
6828                 spin_lock_irq(&phba->hbalock);
6829                 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
6830                 spin_unlock_irq(&phba->hbalock);
6831         }
6832         return rc;
6833 }
6834
6835 /**
6836  * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
6837  * @phba: Pointer to HBA context object.
6838  *
6839  * The function unblocks and resume posting of SLI4 asynchronous mailbox
6840  * commands from the driver internal pending mailbox queue. It makes sure
6841  * that there is no outstanding mailbox command before resuming posting
6842  * asynchronous mailbox commands. If, for any reason, there is outstanding
6843  * mailbox command, it will try to wait it out before resuming asynchronous
6844  * mailbox command posting.
6845  **/
6846 static void
6847 lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
6848 {
6849         struct lpfc_sli *psli = &phba->sli;
6850
6851         spin_lock_irq(&phba->hbalock);
6852         if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
6853                 /* Asynchronous mailbox posting is not blocked, do nothing */
6854                 spin_unlock_irq(&phba->hbalock);
6855                 return;
6856         }
6857
6858         /* Outstanding synchronous mailbox command is guaranteed to be done,
6859          * successful or timeout, after timing-out the outstanding mailbox
6860          * command shall always be removed, so just unblock posting async
6861          * mailbox command and resume
6862          */
6863         psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
6864         spin_unlock_irq(&phba->hbalock);
6865
6866         /* wake up worker thread to post asynchronlous mailbox command */
6867         lpfc_worker_wake_up(phba);
6868 }
6869
6870 /**
6871  * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
6872  * @phba: Pointer to HBA context object.
6873  * @mboxq: Pointer to mailbox object.
6874  *
6875  * The function posts a mailbox to the port.  The mailbox is expected
6876  * to be comletely filled in and ready for the port to operate on it.
6877  * This routine executes a synchronous completion operation on the
6878  * mailbox by polling for its completion.
6879  *
6880  * The caller must not be holding any locks when calling this routine.
6881  *
6882  * Returns:
6883  *      MBX_SUCCESS - mailbox posted successfully
6884  *      Any of the MBX error values.
6885  **/
6886 static int
6887 lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
6888 {
6889         int rc = MBX_SUCCESS;
6890         unsigned long iflag;
6891         uint32_t db_ready;
6892         uint32_t mcqe_status;
6893         uint32_t mbx_cmnd;
6894         unsigned long timeout;
6895         struct lpfc_sli *psli = &phba->sli;
6896         struct lpfc_mqe *mb = &mboxq->u.mqe;
6897         struct lpfc_bmbx_create *mbox_rgn;
6898         struct dma_address *dma_address;
6899         struct lpfc_register bmbx_reg;
6900
6901         /*
6902          * Only one mailbox can be active to the bootstrap mailbox region
6903          * at a time and there is no queueing provided.
6904          */
6905         spin_lock_irqsave(&phba->hbalock, iflag);
6906         if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
6907                 spin_unlock_irqrestore(&phba->hbalock, iflag);
6908                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6909                                 "(%d):2532 Mailbox command x%x (x%x/x%x) "
6910                                 "cannot issue Data: x%x x%x\n",
6911                                 mboxq->vport ? mboxq->vport->vpi : 0,
6912                                 mboxq->u.mb.mbxCommand,
6913                                 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
6914                                 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
6915                                 psli->sli_flag, MBX_POLL);
6916                 return MBXERR_ERROR;
6917         }
6918         /* The server grabs the token and owns it until release */
6919         psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
6920         phba->sli.mbox_active = mboxq;
6921         spin_unlock_irqrestore(&phba->hbalock, iflag);
6922
6923         /*
6924          * Initialize the bootstrap memory region to avoid stale data areas
6925          * in the mailbox post.  Then copy the caller's mailbox contents to
6926          * the bmbx mailbox region.
6927          */
6928         mbx_cmnd = bf_get(lpfc_mqe_command, mb);
6929         memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
6930         lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
6931                               sizeof(struct lpfc_mqe));
6932
6933         /* Post the high mailbox dma address to the port and wait for ready. */
6934         dma_address = &phba->sli4_hba.bmbx.dma_address;
6935         writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
6936
6937         timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
6938                                    * 1000) + jiffies;
6939         do {
6940                 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
6941                 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
6942                 if (!db_ready)
6943                         msleep(2);
6944
6945                 if (time_after(jiffies, timeout)) {
6946                         rc = MBXERR_ERROR;
6947                         goto exit;
6948                 }
6949         } while (!db_ready);
6950
6951         /* Post the low mailbox dma address to the port. */
6952         writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
6953         timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
6954                                    * 1000) + jiffies;
6955         do {
6956                 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
6957                 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
6958                 if (!db_ready)
6959                         msleep(2);
6960
6961                 if (time_after(jiffies, timeout)) {
6962                         rc = MBXERR_ERROR;
6963                         goto exit;
6964                 }
6965         } while (!db_ready);
6966
6967         /*
6968          * Read the CQ to ensure the mailbox has completed.
6969          * If so, update the mailbox status so that the upper layers
6970          * can complete the request normally.
6971          */
6972         lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
6973                               sizeof(struct lpfc_mqe));
6974         mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
6975         lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
6976                               sizeof(struct lpfc_mcqe));
6977         mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
6978         /*
6979          * When the CQE status indicates a failure and the mailbox status
6980          * indicates success then copy the CQE status into the mailbox status
6981          * (and prefix it with x4000).
6982          */
6983         if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
6984                 if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
6985                         bf_set(lpfc_mqe_status, mb,
6986                                (LPFC_MBX_ERROR_RANGE | mcqe_status));
6987                 rc = MBXERR_ERROR;
6988         } else
6989                 lpfc_sli4_swap_str(phba, mboxq);
6990
6991         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6992                         "(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
6993                         "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
6994                         " x%x x%x CQ: x%x x%x x%x x%x\n",
6995                         mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
6996                         lpfc_sli_config_mbox_subsys_get(phba, mboxq),
6997                         lpfc_sli_config_mbox_opcode_get(phba, mboxq),
6998                         bf_get(lpfc_mqe_status, mb),
6999                         mb->un.mb_words[0], mb->un.mb_words[1],
7000                         mb->un.mb_words[2], mb->un.mb_words[3],
7001                         mb->un.mb_words[4], mb->un.mb_words[5],
7002                         mb->un.mb_words[6], mb->un.mb_words[7],
7003                         mb->un.mb_words[8], mb->un.mb_words[9],
7004                         mb->un.mb_words[10], mb->un.mb_words[11],
7005                         mb->un.mb_words[12], mboxq->mcqe.word0,
7006                         mboxq->mcqe.mcqe_tag0,  mboxq->mcqe.mcqe_tag1,
7007                         mboxq->mcqe.trailer);
7008 exit:
7009         /* We are holding the token, no needed for lock when release */
7010         spin_lock_irqsave(&phba->hbalock, iflag);
7011         psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7012         phba->sli.mbox_active = NULL;
7013         spin_unlock_irqrestore(&phba->hbalock, iflag);
7014         return rc;
7015 }
7016
7017 /**
7018  * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
7019  * @phba: Pointer to HBA context object.
7020  * @pmbox: Pointer to mailbox object.
7021  * @flag: Flag indicating how the mailbox need to be processed.
7022  *
7023  * This function is called by discovery code and HBA management code to submit
7024  * a mailbox command to firmware with SLI-4 interface spec.
7025  *
7026  * Return codes the caller owns the mailbox command after the return of the
7027  * function.
7028  **/
7029 static int
7030 lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
7031                        uint32_t flag)
7032 {
7033         struct lpfc_sli *psli = &phba->sli;
7034         unsigned long iflags;
7035         int rc;
7036
7037         /* dump from issue mailbox command if setup */
7038         lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
7039
7040         rc = lpfc_mbox_dev_check(phba);
7041         if (unlikely(rc)) {
7042                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7043                                 "(%d):2544 Mailbox command x%x (x%x/x%x) "
7044                                 "cannot issue Data: x%x x%x\n",
7045                                 mboxq->vport ? mboxq->vport->vpi : 0,
7046                                 mboxq->u.mb.mbxCommand,
7047                                 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7048                                 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
7049                                 psli->sli_flag, flag);
7050                 goto out_not_finished;
7051         }
7052
7053         /* Detect polling mode and jump to a handler */
7054         if (!phba->sli4_hba.intr_enable) {
7055                 if (flag == MBX_POLL)
7056                         rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
7057                 else
7058                         rc = -EIO;
7059                 if (rc != MBX_SUCCESS)
7060                         lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7061                                         "(%d):2541 Mailbox command x%x "
7062                                         "(x%x/x%x) cannot issue Data: "
7063                                         "x%x x%x\n",
7064                                         mboxq->vport ? mboxq->vport->vpi : 0,
7065                                         mboxq->u.mb.mbxCommand,
7066                                         lpfc_sli_config_mbox_subsys_get(phba,
7067                                                                         mboxq),
7068                                         lpfc_sli_config_mbox_opcode_get(phba,
7069                                                                         mboxq),
7070                                         psli->sli_flag, flag);
7071                 return rc;
7072         } else if (flag == MBX_POLL) {
7073                 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7074                                 "(%d):2542 Try to issue mailbox command "
7075                                 "x%x (x%x/x%x) synchronously ahead of async"
7076                                 "mailbox command queue: x%x x%x\n",
7077                                 mboxq->vport ? mboxq->vport->vpi : 0,
7078                                 mboxq->u.mb.mbxCommand,
7079                                 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7080                                 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
7081                                 psli->sli_flag, flag);
7082                 /* Try to block the asynchronous mailbox posting */
7083                 rc = lpfc_sli4_async_mbox_block(phba);
7084                 if (!rc) {
7085                         /* Successfully blocked, now issue sync mbox cmd */
7086                         rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
7087                         if (rc != MBX_SUCCESS)
7088                                 lpfc_printf_log(phba, KERN_ERR,
7089                                         LOG_MBOX | LOG_SLI,
7090                                         "(%d):2597 Mailbox command "
7091                                         "x%x (x%x/x%x) cannot issue "
7092                                         "Data: x%x x%x\n",
7093                                         mboxq->vport ?
7094                                         mboxq->vport->vpi : 0,
7095                                         mboxq->u.mb.mbxCommand,
7096                                         lpfc_sli_config_mbox_subsys_get(phba,
7097                                                                         mboxq),
7098                                         lpfc_sli_config_mbox_opcode_get(phba,
7099                                                                         mboxq),
7100                                         psli->sli_flag, flag);
7101                         /* Unblock the async mailbox posting afterward */
7102                         lpfc_sli4_async_mbox_unblock(phba);
7103                 }
7104                 return rc;
7105         }
7106
7107         /* Now, interrupt mode asynchrous mailbox command */
7108         rc = lpfc_mbox_cmd_check(phba, mboxq);
7109         if (rc) {
7110                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7111                                 "(%d):2543 Mailbox command x%x (x%x/x%x) "
7112                                 "cannot issue Data: x%x x%x\n",
7113                                 mboxq->vport ? mboxq->vport->vpi : 0,
7114                                 mboxq->u.mb.mbxCommand,
7115                                 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7116                                 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
7117                                 psli->sli_flag, flag);
7118                 goto out_not_finished;
7119         }
7120
7121         /* Put the mailbox command to the driver internal FIFO */
7122         psli->slistat.mbox_busy++;
7123         spin_lock_irqsave(&phba->hbalock, iflags);
7124         lpfc_mbox_put(phba, mboxq);
7125         spin_unlock_irqrestore(&phba->hbalock, iflags);
7126         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7127                         "(%d):0354 Mbox cmd issue - Enqueue Data: "
7128                         "x%x (x%x/x%x) x%x x%x x%x\n",
7129                         mboxq->vport ? mboxq->vport->vpi : 0xffffff,
7130                         bf_get(lpfc_mqe_command, &mboxq->u.mqe),
7131                         lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7132                         lpfc_sli_config_mbox_opcode_get(phba, mboxq),
7133                         phba->pport->port_state,
7134                         psli->sli_flag, MBX_NOWAIT);
7135         /* Wake up worker thread to transport mailbox command from head */
7136         lpfc_worker_wake_up(phba);
7137
7138         return MBX_BUSY;
7139
7140 out_not_finished:
7141         return MBX_NOT_FINISHED;
7142 }
7143
7144 /**
7145  * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
7146  * @phba: Pointer to HBA context object.
7147  *
7148  * This function is called by worker thread to send a mailbox command to
7149  * SLI4 HBA firmware.
7150  *
7151  **/
7152 int
7153 lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
7154 {
7155         struct lpfc_sli *psli = &phba->sli;
7156         LPFC_MBOXQ_t *mboxq;
7157         int rc = MBX_SUCCESS;
7158         unsigned long iflags;
7159         struct lpfc_mqe *mqe;
7160         uint32_t mbx_cmnd;
7161
7162         /* Check interrupt mode before post async mailbox command */
7163         if (unlikely(!phba->sli4_hba.intr_enable))
7164                 return MBX_NOT_FINISHED;
7165
7166         /* Check for mailbox command service token */
7167         spin_lock_irqsave(&phba->hbalock, iflags);
7168         if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7169                 spin_unlock_irqrestore(&phba->hbalock, iflags);
7170                 return MBX_NOT_FINISHED;
7171         }
7172         if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7173                 spin_unlock_irqrestore(&phba->hbalock, iflags);
7174                 return MBX_NOT_FINISHED;
7175         }
7176         if (unlikely(phba->sli.mbox_active)) {
7177                 spin_unlock_irqrestore(&phba->hbalock, iflags);
7178                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7179                                 "0384 There is pending active mailbox cmd\n");
7180                 return MBX_NOT_FINISHED;
7181         }
7182         /* Take the mailbox command service token */
7183         psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7184
7185         /* Get the next mailbox command from head of queue */
7186         mboxq = lpfc_mbox_get(phba);
7187
7188         /* If no more mailbox command waiting for post, we're done */
7189         if (!mboxq) {
7190                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7191                 spin_unlock_irqrestore(&phba->hbalock, iflags);
7192                 return MBX_SUCCESS;
7193         }
7194         phba->sli.mbox_active = mboxq;
7195         spin_unlock_irqrestore(&phba->hbalock, iflags);
7196
7197         /* Check device readiness for posting mailbox command */
7198         rc = lpfc_mbox_dev_check(phba);
7199         if (unlikely(rc))
7200                 /* Driver clean routine will clean up pending mailbox */
7201                 goto out_not_finished;
7202
7203         /* Prepare the mbox command to be posted */
7204         mqe = &mboxq->u.mqe;
7205         mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
7206
7207         /* Start timer for the mbox_tmo and log some mailbox post messages */
7208         mod_timer(&psli->mbox_tmo, (jiffies +
7209                   (HZ * lpfc_mbox_tmo_val(phba, mboxq))));
7210
7211         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7212                         "(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
7213                         "x%x x%x\n",
7214                         mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
7215                         lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7216                         lpfc_sli_config_mbox_opcode_get(phba, mboxq),
7217                         phba->pport->port_state, psli->sli_flag);
7218
7219         if (mbx_cmnd != MBX_HEARTBEAT) {
7220                 if (mboxq->vport) {
7221                         lpfc_debugfs_disc_trc(mboxq->vport,
7222                                 LPFC_DISC_TRC_MBOX_VPORT,
7223                                 "MBOX Send vport: cmd:x%x mb:x%x x%x",
7224                                 mbx_cmnd, mqe->un.mb_words[0],
7225                                 mqe->un.mb_words[1]);
7226                 } else {
7227                         lpfc_debugfs_disc_trc(phba->pport,
7228                                 LPFC_DISC_TRC_MBOX,
7229                                 "MBOX Send: cmd:x%x mb:x%x x%x",
7230                                 mbx_cmnd, mqe->un.mb_words[0],
7231                                 mqe->un.mb_words[1]);
7232                 }
7233         }
7234         psli->slistat.mbox_cmd++;
7235
7236         /* Post the mailbox command to the port */
7237         rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
7238         if (rc != MBX_SUCCESS) {
7239                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7240                                 "(%d):2533 Mailbox command x%x (x%x/x%x) "
7241                                 "cannot issue Data: x%x x%x\n",
7242                                 mboxq->vport ? mboxq->vport->vpi : 0,
7243                                 mboxq->u.mb.mbxCommand,
7244                                 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7245                                 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
7246                                 psli->sli_flag, MBX_NOWAIT);
7247                 goto out_not_finished;
7248         }
7249
7250         return rc;
7251
7252 out_not_finished:
7253         spin_lock_irqsave(&phba->hbalock, iflags);
7254         mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
7255         __lpfc_mbox_cmpl_put(phba, mboxq);
7256         /* Release the token */
7257         psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7258         phba->sli.mbox_active = NULL;
7259         spin_unlock_irqrestore(&phba->hbalock, iflags);
7260
7261         return MBX_NOT_FINISHED;
7262 }
7263
7264 /**
7265  * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
7266  * @phba: Pointer to HBA context object.
7267  * @pmbox: Pointer to mailbox object.
7268  * @flag: Flag indicating how the mailbox need to be processed.
7269  *
7270  * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
7271  * the API jump table function pointer from the lpfc_hba struct.
7272  *
7273  * Return codes the caller owns the mailbox command after the return of the
7274  * function.
7275  **/
7276 int
7277 lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
7278 {
7279         return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
7280 }
7281
7282 /**
7283  * lpfc_mbox_api_table_setup - Set up mbox api function jump table
7284  * @phba: The hba struct for which this call is being executed.
7285  * @dev_grp: The HBA PCI-Device group number.
7286  *
7287  * This routine sets up the mbox interface API function jump table in @phba
7288  * struct.
7289  * Returns: 0 - success, -ENODEV - failure.
7290  **/
7291 int
7292 lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
7293 {
7294
7295         switch (dev_grp) {
7296         case LPFC_PCI_DEV_LP:
7297                 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
7298                 phba->lpfc_sli_handle_slow_ring_event =
7299                                 lpfc_sli_handle_slow_ring_event_s3;
7300                 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
7301                 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
7302                 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
7303                 break;
7304         case LPFC_PCI_DEV_OC:
7305                 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
7306                 phba->lpfc_sli_handle_slow_ring_event =
7307                                 lpfc_sli_handle_slow_ring_event_s4;
7308                 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
7309                 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
7310                 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
7311                 break;
7312         default:
7313                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7314                                 "1420 Invalid HBA PCI-device group: 0x%x\n",
7315                                 dev_grp);
7316                 return -ENODEV;
7317                 break;
7318         }
7319         return 0;
7320 }
7321
7322 /**
7323  * __lpfc_sli_ringtx_put - Add an iocb to the txq
7324  * @phba: Pointer to HBA context object.
7325  * @pring: Pointer to driver SLI ring object.
7326  * @piocb: Pointer to address of newly added command iocb.
7327  *
7328  * This function is called with hbalock held to add a command
7329  * iocb to the txq when SLI layer cannot submit the command iocb
7330  * to the ring.
7331  **/
7332 void
7333 __lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7334                     struct lpfc_iocbq *piocb)
7335 {
7336         /* Insert the caller's iocb in the txq tail for later processing. */
7337         list_add_tail(&piocb->list, &pring->txq);
7338         pring->txq_cnt++;
7339 }
7340
7341 /**
7342  * lpfc_sli_next_iocb - Get the next iocb in the txq
7343  * @phba: Pointer to HBA context object.
7344  * @pring: Pointer to driver SLI ring object.
7345  * @piocb: Pointer to address of newly added command iocb.
7346  *
7347  * This function is called with hbalock held before a new
7348  * iocb is submitted to the firmware. This function checks
7349  * txq to flush the iocbs in txq to Firmware before
7350  * submitting new iocbs to the Firmware.
7351  * If there are iocbs in the txq which need to be submitted
7352  * to firmware, lpfc_sli_next_iocb returns the first element
7353  * of the txq after dequeuing it from txq.
7354  * If there is no iocb in the txq then the function will return
7355  * *piocb and *piocb is set to NULL. Caller needs to check
7356  * *piocb to find if there are more commands in the txq.
7357  **/
7358 static struct lpfc_iocbq *
7359 lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7360                    struct lpfc_iocbq **piocb)
7361 {
7362         struct lpfc_iocbq * nextiocb;
7363
7364         nextiocb = lpfc_sli_ringtx_get(phba, pring);
7365         if (!nextiocb) {
7366                 nextiocb = *piocb;
7367                 *piocb = NULL;
7368         }
7369
7370         return nextiocb;
7371 }
7372
7373 /**
7374  * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
7375  * @phba: Pointer to HBA context object.
7376  * @ring_number: SLI ring number to issue iocb on.
7377  * @piocb: Pointer to command iocb.
7378  * @flag: Flag indicating if this command can be put into txq.
7379  *
7380  * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
7381  * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
7382  * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
7383  * flag is turned on, the function returns IOCB_ERROR. When the link is down,
7384  * this function allows only iocbs for posting buffers. This function finds
7385  * next available slot in the command ring and posts the command to the
7386  * available slot and writes the port attention register to request HBA start
7387  * processing new iocb. If there is no slot available in the ring and
7388  * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
7389  * the function returns IOCB_BUSY.
7390  *
7391  * This function is called with hbalock held. The function will return success
7392  * after it successfully submit the iocb to firmware or after adding to the
7393  * txq.
7394  **/
7395 static int
7396 __lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
7397                     struct lpfc_iocbq *piocb, uint32_t flag)
7398 {
7399         struct lpfc_iocbq *nextiocb;
7400         IOCB_t *iocb;
7401         struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
7402
7403         if (piocb->iocb_cmpl && (!piocb->vport) &&
7404            (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
7405            (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
7406                 lpfc_printf_log(phba, KERN_ERR,
7407                                 LOG_SLI | LOG_VPORT,
7408                                 "1807 IOCB x%x failed. No vport\n",
7409                                 piocb->iocb.ulpCommand);
7410                 dump_stack();
7411                 return IOCB_ERROR;
7412         }
7413
7414
7415         /* If the PCI channel is in offline state, do not post iocbs. */
7416         if (unlikely(pci_channel_offline(phba->pcidev)))
7417                 return IOCB_ERROR;
7418
7419         /* If HBA has a deferred error attention, fail the iocb. */
7420         if (unlikely(phba->hba_flag & DEFER_ERATT))
7421                 return IOCB_ERROR;
7422
7423         /*
7424          * We should never get an IOCB if we are in a < LINK_DOWN state
7425          */
7426         if (unlikely(phba->link_state < LPFC_LINK_DOWN))
7427                 return IOCB_ERROR;
7428
7429         /*
7430          * Check to see if we are blocking IOCB processing because of a
7431          * outstanding event.
7432          */
7433         if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
7434                 goto iocb_busy;
7435
7436         if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
7437                 /*
7438                  * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
7439                  * can be issued if the link is not up.
7440                  */
7441                 switch (piocb->iocb.ulpCommand) {
7442                 case CMD_GEN_REQUEST64_CR:
7443                 case CMD_GEN_REQUEST64_CX:
7444                         if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
7445                                 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
7446                                         FC_RCTL_DD_UNSOL_CMD) ||
7447                                 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
7448                                         MENLO_TRANSPORT_TYPE))
7449
7450                                 goto iocb_busy;
7451                         break;
7452                 case CMD_QUE_RING_BUF_CN:
7453                 case CMD_QUE_RING_BUF64_CN:
7454                         /*
7455                          * For IOCBs, like QUE_RING_BUF, that have no rsp ring
7456                          * completion, iocb_cmpl MUST be 0.
7457                          */
7458                         if (piocb->iocb_cmpl)
7459                                 piocb->iocb_cmpl = NULL;
7460                         /*FALLTHROUGH*/
7461                 case CMD_CREATE_XRI_CR:
7462                 case CMD_CLOSE_XRI_CN:
7463                 case CMD_CLOSE_XRI_CX:
7464                         break;
7465                 default:
7466                         goto iocb_busy;
7467                 }
7468
7469         /*
7470          * For FCP commands, we must be in a state where we can process link
7471          * attention events.
7472          */
7473         } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
7474                             !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
7475                 goto iocb_busy;
7476         }
7477
7478         while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
7479                (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
7480                 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
7481
7482         if (iocb)
7483                 lpfc_sli_update_ring(phba, pring);
7484         else
7485                 lpfc_sli_update_full_ring(phba, pring);
7486
7487         if (!piocb)
7488                 return IOCB_SUCCESS;
7489
7490         goto out_busy;
7491
7492  iocb_busy:
7493         pring->stats.iocb_cmd_delay++;
7494
7495  out_busy:
7496
7497         if (!(flag & SLI_IOCB_RET_IOCB)) {
7498                 __lpfc_sli_ringtx_put(phba, pring, piocb);
7499                 return IOCB_SUCCESS;
7500         }
7501
7502         return IOCB_BUSY;
7503 }
7504
7505 /**
7506  * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
7507  * @phba: Pointer to HBA context object.
7508  * @piocb: Pointer to command iocb.
7509  * @sglq: Pointer to the scatter gather queue object.
7510  *
7511  * This routine converts the bpl or bde that is in the IOCB
7512  * to a sgl list for the sli4 hardware. The physical address
7513  * of the bpl/bde is converted back to a virtual address.
7514  * If the IOCB contains a BPL then the list of BDE's is
7515  * converted to sli4_sge's. If the IOCB contains a single
7516  * BDE then it is converted to a single sli_sge.
7517  * The IOCB is still in cpu endianess so the contents of
7518  * the bpl can be used without byte swapping.
7519  *
7520  * Returns valid XRI = Success, NO_XRI = Failure.
7521 **/
7522 static uint16_t
7523 lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
7524                 struct lpfc_sglq *sglq)
7525 {
7526         uint16_t xritag = NO_XRI;
7527         struct ulp_bde64 *bpl = NULL;
7528         struct ulp_bde64 bde;
7529         struct sli4_sge *sgl  = NULL;
7530         struct lpfc_dmabuf *dmabuf;
7531         IOCB_t *icmd;
7532         int numBdes = 0;
7533         int i = 0;
7534         uint32_t offset = 0; /* accumulated offset in the sg request list */
7535         int inbound = 0; /* number of sg reply entries inbound from firmware */
7536
7537         if (!piocbq || !sglq)
7538                 return xritag;
7539
7540         sgl  = (struct sli4_sge *)sglq->sgl;
7541         icmd = &piocbq->iocb;
7542         if (icmd->ulpCommand == CMD_XMIT_BLS_RSP64_CX)
7543                 return sglq->sli4_xritag;
7544         if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
7545                 numBdes = icmd->un.genreq64.bdl.bdeSize /
7546                                 sizeof(struct ulp_bde64);
7547                 /* The addrHigh and addrLow fields within the IOCB
7548                  * have not been byteswapped yet so there is no
7549                  * need to swap them back.
7550                  */
7551                 if (piocbq->context3)
7552                         dmabuf = (struct lpfc_dmabuf *)piocbq->context3;
7553                 else
7554                         return xritag;
7555
7556                 bpl  = (struct ulp_bde64 *)dmabuf->virt;
7557                 if (!bpl)
7558                         return xritag;
7559
7560                 for (i = 0; i < numBdes; i++) {
7561                         /* Should already be byte swapped. */
7562                         sgl->addr_hi = bpl->addrHigh;
7563                         sgl->addr_lo = bpl->addrLow;
7564
7565                         sgl->word2 = le32_to_cpu(sgl->word2);
7566                         if ((i+1) == numBdes)
7567                                 bf_set(lpfc_sli4_sge_last, sgl, 1);
7568                         else
7569                                 bf_set(lpfc_sli4_sge_last, sgl, 0);
7570                         /* swap the size field back to the cpu so we
7571                          * can assign it to the sgl.
7572                          */
7573                         bde.tus.w = le32_to_cpu(bpl->tus.w);
7574                         sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
7575                         /* The offsets in the sgl need to be accumulated
7576                          * separately for the request and reply lists.
7577                          * The request is always first, the reply follows.
7578                          */
7579                         if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
7580                                 /* add up the reply sg entries */
7581                                 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
7582                                         inbound++;
7583                                 /* first inbound? reset the offset */
7584                                 if (inbound == 1)
7585                                         offset = 0;
7586                                 bf_set(lpfc_sli4_sge_offset, sgl, offset);
7587                                 bf_set(lpfc_sli4_sge_type, sgl,
7588                                         LPFC_SGE_TYPE_DATA);
7589                                 offset += bde.tus.f.bdeSize;
7590                         }
7591                         sgl->word2 = cpu_to_le32(sgl->word2);
7592                         bpl++;
7593                         sgl++;
7594                 }
7595         } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
7596                         /* The addrHigh and addrLow fields of the BDE have not
7597                          * been byteswapped yet so they need to be swapped
7598                          * before putting them in the sgl.
7599                          */
7600                         sgl->addr_hi =
7601                                 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
7602                         sgl->addr_lo =
7603                                 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
7604                         sgl->word2 = le32_to_cpu(sgl->word2);
7605                         bf_set(lpfc_sli4_sge_last, sgl, 1);
7606                         sgl->word2 = cpu_to_le32(sgl->word2);
7607                         sgl->sge_len =
7608                                 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
7609         }
7610         return sglq->sli4_xritag;
7611 }
7612
7613 /**
7614  * lpfc_sli4_scmd_to_wqidx_distr - scsi command to SLI4 WQ index distribution
7615  * @phba: Pointer to HBA context object.
7616  *
7617  * This routine performs a roundrobin SCSI command to SLI4 FCP WQ index
7618  * distribution.  This is called by __lpfc_sli_issue_iocb_s4() with the hbalock
7619  * held.
7620  *
7621  * Return: index into SLI4 fast-path FCP queue index.
7622  **/
7623 static uint32_t
7624 lpfc_sli4_scmd_to_wqidx_distr(struct lpfc_hba *phba)
7625 {
7626         ++phba->fcp_qidx;
7627         if (phba->fcp_qidx >= phba->cfg_fcp_wq_count)
7628                 phba->fcp_qidx = 0;
7629
7630         return phba->fcp_qidx;
7631 }
7632
7633 /**
7634  * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
7635  * @phba: Pointer to HBA context object.
7636  * @piocb: Pointer to command iocb.
7637  * @wqe: Pointer to the work queue entry.
7638  *
7639  * This routine converts the iocb command to its Work Queue Entry
7640  * equivalent. The wqe pointer should not have any fields set when
7641  * this routine is called because it will memcpy over them.
7642  * This routine does not set the CQ_ID or the WQEC bits in the
7643  * wqe.
7644  *
7645  * Returns: 0 = Success, IOCB_ERROR = Failure.
7646  **/
7647 static int
7648 lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
7649                 union lpfc_wqe *wqe)
7650 {
7651         uint32_t xmit_len = 0, total_len = 0;
7652         uint8_t ct = 0;
7653         uint32_t fip;
7654         uint32_t abort_tag;
7655         uint8_t command_type = ELS_COMMAND_NON_FIP;
7656         uint8_t cmnd;
7657         uint16_t xritag;
7658         uint16_t abrt_iotag;
7659         struct lpfc_iocbq *abrtiocbq;
7660         struct ulp_bde64 *bpl = NULL;
7661         uint32_t els_id = LPFC_ELS_ID_DEFAULT;
7662         int numBdes, i;
7663         struct ulp_bde64 bde;
7664         struct lpfc_nodelist *ndlp;
7665         uint32_t *pcmd;
7666         uint32_t if_type;
7667
7668         fip = phba->hba_flag & HBA_FIP_SUPPORT;
7669         /* The fcp commands will set command type */
7670         if (iocbq->iocb_flag &  LPFC_IO_FCP)
7671                 command_type = FCP_COMMAND;
7672         else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
7673                 command_type = ELS_COMMAND_FIP;
7674         else
7675                 command_type = ELS_COMMAND_NON_FIP;
7676
7677         /* Some of the fields are in the right position already */
7678         memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
7679         abort_tag = (uint32_t) iocbq->iotag;
7680         xritag = iocbq->sli4_xritag;
7681         wqe->generic.wqe_com.word7 = 0; /* The ct field has moved so reset */
7682         /* words0-2 bpl convert bde */
7683         if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
7684                 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
7685                                 sizeof(struct ulp_bde64);
7686                 bpl  = (struct ulp_bde64 *)
7687                         ((struct lpfc_dmabuf *)iocbq->context3)->virt;
7688                 if (!bpl)
7689                         return IOCB_ERROR;
7690
7691                 /* Should already be byte swapped. */
7692                 wqe->generic.bde.addrHigh =  le32_to_cpu(bpl->addrHigh);
7693                 wqe->generic.bde.addrLow =  le32_to_cpu(bpl->addrLow);
7694                 /* swap the size field back to the cpu so we
7695                  * can assign it to the sgl.
7696                  */
7697                 wqe->generic.bde.tus.w  = le32_to_cpu(bpl->tus.w);
7698                 xmit_len = wqe->generic.bde.tus.f.bdeSize;
7699                 total_len = 0;
7700                 for (i = 0; i < numBdes; i++) {
7701                         bde.tus.w  = le32_to_cpu(bpl[i].tus.w);
7702                         total_len += bde.tus.f.bdeSize;
7703                 }
7704         } else
7705                 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
7706
7707         iocbq->iocb.ulpIoTag = iocbq->iotag;
7708         cmnd = iocbq->iocb.ulpCommand;
7709
7710         switch (iocbq->iocb.ulpCommand) {
7711         case CMD_ELS_REQUEST64_CR:
7712                 ndlp = (struct lpfc_nodelist *)iocbq->context1;
7713                 if (!iocbq->iocb.ulpLe) {
7714                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7715                                 "2007 Only Limited Edition cmd Format"
7716                                 " supported 0x%x\n",
7717                                 iocbq->iocb.ulpCommand);
7718                         return IOCB_ERROR;
7719                 }
7720
7721                 wqe->els_req.payload_len = xmit_len;
7722                 /* Els_reguest64 has a TMO */
7723                 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
7724                         iocbq->iocb.ulpTimeout);
7725                 /* Need a VF for word 4 set the vf bit*/
7726                 bf_set(els_req64_vf, &wqe->els_req, 0);
7727                 /* And a VFID for word 12 */
7728                 bf_set(els_req64_vfid, &wqe->els_req, 0);
7729                 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
7730                 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
7731                        iocbq->iocb.ulpContext);
7732                 bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
7733                 bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
7734                 /* CCP CCPE PV PRI in word10 were set in the memcpy */
7735                 if (command_type == ELS_COMMAND_FIP)
7736                         els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
7737                                         >> LPFC_FIP_ELS_ID_SHIFT);
7738                 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
7739                                         iocbq->context2)->virt);
7740                 if_type = bf_get(lpfc_sli_intf_if_type,
7741                                         &phba->sli4_hba.sli_intf);
7742                 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
7743                         if (pcmd && (*pcmd == ELS_CMD_FLOGI ||
7744                                 *pcmd == ELS_CMD_SCR ||
7745                                 *pcmd == ELS_CMD_FDISC ||
7746                                 *pcmd == ELS_CMD_PLOGI)) {
7747                                 bf_set(els_req64_sp, &wqe->els_req, 1);
7748                                 bf_set(els_req64_sid, &wqe->els_req,
7749                                         iocbq->vport->fc_myDID);
7750                                 bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
7751                                 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
7752                                         phba->vpi_ids[phba->pport->vpi]);
7753                         } else if (pcmd && iocbq->context1) {
7754                                 bf_set(wqe_ct, &wqe->els_req.wqe_com, 0);
7755                                 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
7756                                         phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
7757                         }
7758                 }
7759                 bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
7760                        phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
7761                 bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
7762                 bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
7763                 bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
7764                 bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
7765                 bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
7766                 bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
7767                 break;
7768         case CMD_XMIT_SEQUENCE64_CX:
7769                 bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
7770                        iocbq->iocb.un.ulpWord[3]);
7771                 bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
7772                        iocbq->iocb.unsli3.rcvsli3.ox_id);
7773                 /* The entire sequence is transmitted for this IOCB */
7774                 xmit_len = total_len;
7775                 cmnd = CMD_XMIT_SEQUENCE64_CR;
7776                 if (phba->link_flag & LS_LOOPBACK_MODE)
7777                         bf_set(wqe_xo, &wqe->xmit_sequence.wge_ctl, 1);
7778         case CMD_XMIT_SEQUENCE64_CR:
7779                 /* word3 iocb=io_tag32 wqe=reserved */
7780                 wqe->xmit_sequence.rsvd3 = 0;
7781                 /* word4 relative_offset memcpy */
7782                 /* word5 r_ctl/df_ctl memcpy */
7783                 bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
7784                 bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
7785                 bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
7786                        LPFC_WQE_IOD_WRITE);
7787                 bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
7788                        LPFC_WQE_LENLOC_WORD12);
7789                 bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
7790                 wqe->xmit_sequence.xmit_len = xmit_len;
7791                 command_type = OTHER_COMMAND;
7792                 break;
7793         case CMD_XMIT_BCAST64_CN:
7794                 /* word3 iocb=iotag32 wqe=seq_payload_len */
7795                 wqe->xmit_bcast64.seq_payload_len = xmit_len;
7796                 /* word4 iocb=rsvd wqe=rsvd */
7797                 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
7798                 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
7799                 bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
7800                         ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
7801                 bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
7802                 bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
7803                 bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
7804                        LPFC_WQE_LENLOC_WORD3);
7805                 bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
7806                 break;
7807         case CMD_FCP_IWRITE64_CR:
7808                 command_type = FCP_COMMAND_DATA_OUT;
7809                 /* word3 iocb=iotag wqe=payload_offset_len */
7810                 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
7811                 wqe->fcp_iwrite.payload_offset_len =
7812                         xmit_len + sizeof(struct fcp_rsp);
7813                 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
7814                 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
7815                 bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
7816                        iocbq->iocb.ulpFCP2Rcvy);
7817                 bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
7818                 /* Always open the exchange */
7819                 bf_set(wqe_xc, &wqe->fcp_iwrite.wqe_com, 0);
7820                 bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
7821                 bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
7822                        LPFC_WQE_LENLOC_WORD4);
7823                 bf_set(wqe_ebde_cnt, &wqe->fcp_iwrite.wqe_com, 0);
7824                 bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
7825                 if (iocbq->iocb_flag & LPFC_IO_DIF) {
7826                         iocbq->iocb_flag &= ~LPFC_IO_DIF;
7827                         bf_set(wqe_dif, &wqe->generic.wqe_com, 1);
7828                 }
7829                 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
7830                 break;
7831         case CMD_FCP_IREAD64_CR:
7832                 /* word3 iocb=iotag wqe=payload_offset_len */
7833                 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
7834                 wqe->fcp_iread.payload_offset_len =
7835                         xmit_len + sizeof(struct fcp_rsp);
7836                 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
7837                 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
7838                 bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
7839                        iocbq->iocb.ulpFCP2Rcvy);
7840                 bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
7841                 /* Always open the exchange */
7842                 bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
7843                 bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
7844                 bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
7845                        LPFC_WQE_LENLOC_WORD4);
7846                 bf_set(wqe_ebde_cnt, &wqe->fcp_iread.wqe_com, 0);
7847                 bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
7848                 if (iocbq->iocb_flag & LPFC_IO_DIF) {
7849                         iocbq->iocb_flag &= ~LPFC_IO_DIF;
7850                         bf_set(wqe_dif, &wqe->generic.wqe_com, 1);
7851                 }
7852                 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
7853                 break;
7854         case CMD_FCP_ICMND64_CR:
7855                 /* word3 iocb=IO_TAG wqe=reserved */
7856                 wqe->fcp_icmd.rsrvd3 = 0;
7857                 bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
7858                 /* Always open the exchange */
7859                 bf_set(wqe_xc, &wqe->fcp_icmd.wqe_com, 0);
7860                 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
7861                 bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
7862                 bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
7863                 bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
7864                        LPFC_WQE_LENLOC_NONE);
7865                 bf_set(wqe_ebde_cnt, &wqe->fcp_icmd.wqe_com, 0);
7866                 break;
7867         case CMD_GEN_REQUEST64_CR:
7868                 /* For this command calculate the xmit length of the
7869                  * request bde.
7870                  */
7871                 xmit_len = 0;
7872                 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
7873                         sizeof(struct ulp_bde64);
7874                 for (i = 0; i < numBdes; i++) {
7875                         bde.tus.w = le32_to_cpu(bpl[i].tus.w);
7876                         if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
7877                                 break;
7878                         xmit_len += bde.tus.f.bdeSize;
7879                 }
7880                 /* word3 iocb=IO_TAG wqe=request_payload_len */
7881                 wqe->gen_req.request_payload_len = xmit_len;
7882                 /* word4 iocb=parameter wqe=relative_offset memcpy */
7883                 /* word5 [rctl, type, df_ctl, la] copied in memcpy */
7884                 /* word6 context tag copied in memcpy */
7885                 if (iocbq->iocb.ulpCt_h  || iocbq->iocb.ulpCt_l) {
7886                         ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
7887                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7888                                 "2015 Invalid CT %x command 0x%x\n",
7889                                 ct, iocbq->iocb.ulpCommand);
7890                         return IOCB_ERROR;
7891                 }
7892                 bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
7893                 bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
7894                 bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
7895                 bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
7896                 bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
7897                 bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
7898                 bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
7899                 bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
7900                 command_type = OTHER_COMMAND;
7901                 break;
7902         case CMD_XMIT_ELS_RSP64_CX:
7903                 ndlp = (struct lpfc_nodelist *)iocbq->context1;
7904                 /* words0-2 BDE memcpy */
7905                 /* word3 iocb=iotag32 wqe=response_payload_len */
7906                 wqe->xmit_els_rsp.response_payload_len = xmit_len;
7907                 /* word4 iocb=did wge=rsvd. */
7908                 wqe->xmit_els_rsp.rsvd4 = 0;
7909                 /* word5 iocb=rsvd wge=did */
7910                 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
7911                          iocbq->iocb.un.elsreq64.remoteID);
7912                 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
7913                        ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
7914                 bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
7915                 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
7916                        iocbq->iocb.unsli3.rcvsli3.ox_id);
7917                 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
7918                         bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
7919                                phba->vpi_ids[iocbq->vport->vpi]);
7920                 bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
7921                 bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
7922                 bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
7923                 bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
7924                        LPFC_WQE_LENLOC_WORD3);
7925                 bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
7926                 bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
7927                        phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
7928                 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
7929                                         iocbq->context2)->virt);
7930                 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
7931                                 bf_set(els_req64_sp, &wqe->els_req, 1);
7932                                 bf_set(els_req64_sid, &wqe->els_req,
7933                                         iocbq->vport->fc_myDID);
7934                                 bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
7935                                 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
7936                                         phba->vpi_ids[phba->pport->vpi]);
7937                 }
7938                 command_type = OTHER_COMMAND;
7939                 break;
7940         case CMD_CLOSE_XRI_CN:
7941         case CMD_ABORT_XRI_CN:
7942         case CMD_ABORT_XRI_CX:
7943                 /* words 0-2 memcpy should be 0 rserved */
7944                 /* port will send abts */
7945                 abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
7946                 if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
7947                         abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
7948                         fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
7949                 } else
7950                         fip = 0;
7951
7952                 if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
7953                         /*
7954                          * The link is down, or the command was ELS_FIP
7955                          * so the fw does not need to send abts
7956                          * on the wire.
7957                          */
7958                         bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
7959                 else
7960                         bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
7961                 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
7962                 /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
7963                 wqe->abort_cmd.rsrvd5 = 0;
7964                 bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
7965                         ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
7966                 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
7967                 /*
7968                  * The abort handler will send us CMD_ABORT_XRI_CN or
7969                  * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
7970                  */
7971                 bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
7972                 bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
7973                 bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
7974                        LPFC_WQE_LENLOC_NONE);
7975                 cmnd = CMD_ABORT_XRI_CX;
7976                 command_type = OTHER_COMMAND;
7977                 xritag = 0;
7978                 break;
7979         case CMD_XMIT_BLS_RSP64_CX:
7980                 ndlp = (struct lpfc_nodelist *)iocbq->context1;
7981                 /* As BLS ABTS RSP WQE is very different from other WQEs,
7982                  * we re-construct this WQE here based on information in
7983                  * iocbq from scratch.
7984                  */
7985                 memset(wqe, 0, sizeof(union lpfc_wqe));
7986                 /* OX_ID is invariable to who sent ABTS to CT exchange */
7987                 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
7988                        bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
7989                 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
7990                     LPFC_ABTS_UNSOL_INT) {
7991                         /* ABTS sent by initiator to CT exchange, the
7992                          * RX_ID field will be filled with the newly
7993                          * allocated responder XRI.
7994                          */
7995                         bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
7996                                iocbq->sli4_xritag);
7997                 } else {
7998                         /* ABTS sent by responder to CT exchange, the
7999                          * RX_ID field will be filled with the responder
8000                          * RX_ID from ABTS.
8001                          */
8002                         bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
8003                                bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
8004                 }
8005                 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
8006                 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
8007
8008                 /* Use CT=VPI */
8009                 bf_set(wqe_els_did, &wqe->xmit_bls_rsp.wqe_dest,
8010                         ndlp->nlp_DID);
8011                 bf_set(xmit_bls_rsp64_temprpi, &wqe->xmit_bls_rsp,
8012                         iocbq->iocb.ulpContext);
8013                 bf_set(wqe_ct, &wqe->xmit_bls_rsp.wqe_com, 1);
8014                 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
8015                         phba->vpi_ids[phba->pport->vpi]);
8016                 bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
8017                 bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
8018                        LPFC_WQE_LENLOC_NONE);
8019                 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
8020                 command_type = OTHER_COMMAND;
8021                 if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
8022                         bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
8023                                bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
8024                         bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
8025                                bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
8026                         bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
8027                                bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
8028                 }
8029
8030                 break;
8031         case CMD_XRI_ABORTED_CX:
8032         case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
8033         case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
8034         case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
8035         case CMD_FCP_TRSP64_CX: /* Target mode rcv */
8036         case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
8037         default:
8038                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8039                                 "2014 Invalid command 0x%x\n",
8040                                 iocbq->iocb.ulpCommand);
8041                 return IOCB_ERROR;
8042                 break;
8043         }
8044
8045         bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
8046         bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
8047         wqe->generic.wqe_com.abort_tag = abort_tag;
8048         bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
8049         bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
8050         bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
8051         bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
8052         return 0;
8053 }
8054
8055 /**
8056  * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
8057  * @phba: Pointer to HBA context object.
8058  * @ring_number: SLI ring number to issue iocb on.
8059  * @piocb: Pointer to command iocb.
8060  * @flag: Flag indicating if this command can be put into txq.
8061  *
8062  * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
8063  * an iocb command to an HBA with SLI-4 interface spec.
8064  *
8065  * This function is called with hbalock held. The function will return success
8066  * after it successfully submit the iocb to firmware or after adding to the
8067  * txq.
8068  **/
8069 static int
8070 __lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
8071                          struct lpfc_iocbq *piocb, uint32_t flag)
8072 {
8073         struct lpfc_sglq *sglq;
8074         union lpfc_wqe wqe;
8075         struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
8076
8077         if (piocb->sli4_xritag == NO_XRI) {
8078                 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
8079                     piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
8080                         sglq = NULL;
8081                 else {
8082                         if (pring->txq_cnt) {
8083                                 if (!(flag & SLI_IOCB_RET_IOCB)) {
8084                                         __lpfc_sli_ringtx_put(phba,
8085                                                 pring, piocb);
8086                                         return IOCB_SUCCESS;
8087                                 } else {
8088                                         return IOCB_BUSY;
8089                                 }
8090                         } else {
8091                                 sglq = __lpfc_sli_get_sglq(phba, piocb);
8092                                 if (!sglq) {
8093                                         if (!(flag & SLI_IOCB_RET_IOCB)) {
8094                                                 __lpfc_sli_ringtx_put(phba,
8095                                                                 pring,
8096                                                                 piocb);
8097                                                 return IOCB_SUCCESS;
8098                                         } else
8099                                                 return IOCB_BUSY;
8100                                 }
8101                         }
8102                 }
8103         } else if (piocb->iocb_flag &  LPFC_IO_FCP) {
8104                 /* These IO's already have an XRI and a mapped sgl. */
8105                 sglq = NULL;
8106         } else {
8107                 /*
8108                  * This is a continuation of a commandi,(CX) so this
8109                  * sglq is on the active list
8110                  */
8111                 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_xritag);
8112                 if (!sglq)
8113                         return IOCB_ERROR;
8114         }
8115
8116         if (sglq) {
8117                 piocb->sli4_lxritag = sglq->sli4_lxritag;
8118                 piocb->sli4_xritag = sglq->sli4_xritag;
8119                 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
8120                         return IOCB_ERROR;
8121         }
8122
8123         if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
8124                 return IOCB_ERROR;
8125
8126         if ((piocb->iocb_flag & LPFC_IO_FCP) ||
8127                 (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
8128                 /*
8129                  * For FCP command IOCB, get a new WQ index to distribute
8130                  * WQE across the WQsr. On the other hand, for abort IOCB,
8131                  * it carries the same WQ index to the original command
8132                  * IOCB.
8133                  */
8134                 if (piocb->iocb_flag & LPFC_IO_FCP)
8135                         piocb->fcp_wqidx = lpfc_sli4_scmd_to_wqidx_distr(phba);
8136                 if (unlikely(!phba->sli4_hba.fcp_wq))
8137                         return IOCB_ERROR;
8138                 if (lpfc_sli4_wq_put(phba->sli4_hba.fcp_wq[piocb->fcp_wqidx],
8139                                      &wqe))
8140                         return IOCB_ERROR;
8141         } else {
8142                 if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
8143                         return IOCB_ERROR;
8144         }
8145         lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
8146
8147         return 0;
8148 }
8149
8150 /**
8151  * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
8152  *
8153  * This routine wraps the actual lockless version for issusing IOCB function
8154  * pointer from the lpfc_hba struct.
8155  *
8156  * Return codes:
8157  *      IOCB_ERROR - Error
8158  *      IOCB_SUCCESS - Success
8159  *      IOCB_BUSY - Busy
8160  **/
8161 int
8162 __lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
8163                 struct lpfc_iocbq *piocb, uint32_t flag)
8164 {
8165         return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
8166 }
8167
8168 /**
8169  * lpfc_sli_api_table_setup - Set up sli api function jump table
8170  * @phba: The hba struct for which this call is being executed.
8171  * @dev_grp: The HBA PCI-Device group number.
8172  *
8173  * This routine sets up the SLI interface API function jump table in @phba
8174  * struct.
8175  * Returns: 0 - success, -ENODEV - failure.
8176  **/
8177 int
8178 lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
8179 {
8180
8181         switch (dev_grp) {
8182         case LPFC_PCI_DEV_LP:
8183                 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
8184                 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
8185                 break;
8186         case LPFC_PCI_DEV_OC:
8187                 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
8188                 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
8189                 break;
8190         default:
8191                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8192                                 "1419 Invalid HBA PCI-device group: 0x%x\n",
8193                                 dev_grp);
8194                 return -ENODEV;
8195                 break;
8196         }
8197         phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
8198         return 0;
8199 }
8200
8201 /**
8202  * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
8203  * @phba: Pointer to HBA context object.
8204  * @pring: Pointer to driver SLI ring object.
8205  * @piocb: Pointer to command iocb.
8206  * @flag: Flag indicating if this command can be put into txq.
8207  *
8208  * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
8209  * function. This function gets the hbalock and calls
8210  * __lpfc_sli_issue_iocb function and will return the error returned
8211  * by __lpfc_sli_issue_iocb function. This wrapper is used by
8212  * functions which do not hold hbalock.
8213  **/
8214 int
8215 lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
8216                     struct lpfc_iocbq *piocb, uint32_t flag)
8217 {
8218         unsigned long iflags;
8219         int rc;
8220
8221         spin_lock_irqsave(&phba->hbalock, iflags);
8222         rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
8223         spin_unlock_irqrestore(&phba->hbalock, iflags);
8224
8225         return rc;
8226 }
8227
8228 /**
8229  * lpfc_extra_ring_setup - Extra ring setup function
8230  * @phba: Pointer to HBA context object.
8231  *
8232  * This function is called while driver attaches with the
8233  * HBA to setup the extra ring. The extra ring is used
8234  * only when driver needs to support target mode functionality
8235  * or IP over FC functionalities.
8236  *
8237  * This function is called with no lock held.
8238  **/
8239 static int
8240 lpfc_extra_ring_setup( struct lpfc_hba *phba)
8241 {
8242         struct lpfc_sli *psli;
8243         struct lpfc_sli_ring *pring;
8244
8245         psli = &phba->sli;
8246
8247         /* Adjust cmd/rsp ring iocb entries more evenly */
8248
8249         /* Take some away from the FCP ring */
8250         pring = &psli->ring[psli->fcp_ring];
8251         pring->numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
8252         pring->numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
8253         pring->numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
8254         pring->numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
8255
8256         /* and give them to the extra ring */
8257         pring = &psli->ring[psli->extra_ring];
8258
8259         pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
8260         pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
8261         pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
8262         pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
8263
8264         /* Setup default profile for this ring */
8265         pring->iotag_max = 4096;
8266         pring->num_mask = 1;
8267         pring->prt[0].profile = 0;      /* Mask 0 */
8268         pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
8269         pring->prt[0].type = phba->cfg_multi_ring_type;
8270         pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
8271         return 0;
8272 }
8273
8274 /* lpfc_sli_abts_recover_port - Recover a port that failed an ABTS.
8275  * @vport: pointer to virtual port object.
8276  * @ndlp: nodelist pointer for the impacted rport.
8277  *
8278  * The driver calls this routine in response to a XRI ABORT CQE
8279  * event from the port.  In this event, the driver is required to
8280  * recover its login to the rport even though its login may be valid
8281  * from the driver's perspective.  The failed ABTS notice from the
8282  * port indicates the rport is not responding.
8283  */
8284 static void
8285 lpfc_sli_abts_recover_port(struct lpfc_vport *vport,
8286                            struct lpfc_nodelist *ndlp)
8287 {
8288         struct Scsi_Host *shost;
8289         struct lpfc_hba *phba;
8290         unsigned long flags = 0;
8291
8292         shost = lpfc_shost_from_vport(vport);
8293         phba = vport->phba;
8294         if (ndlp->nlp_state != NLP_STE_MAPPED_NODE) {
8295                 lpfc_printf_log(phba, KERN_INFO,
8296                         LOG_SLI, "3093 No rport recovery needed. "
8297                         "rport in state 0x%x\n",
8298                         ndlp->nlp_state);
8299                 return;
8300         }
8301         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8302                         "3094 Start rport recovery on shost id 0x%x "
8303                         "fc_id 0x%06x vpi 0x%x rpi 0x%x state 0x%x "
8304                         "flags 0x%x\n",
8305                         shost->host_no, ndlp->nlp_DID,
8306                         vport->vpi, ndlp->nlp_rpi, ndlp->nlp_state,
8307                         ndlp->nlp_flag);
8308         /*
8309          * The rport is not responding.  Don't attempt ADISC recovery.
8310          * Remove the FCP-2 flag to force a PLOGI.
8311          */
8312         spin_lock_irqsave(shost->host_lock, flags);
8313         ndlp->nlp_fcp_info &= ~NLP_FCP_2_DEVICE;
8314         spin_unlock_irqrestore(shost->host_lock, flags);
8315         lpfc_disc_state_machine(vport, ndlp, NULL,
8316                                 NLP_EVT_DEVICE_RECOVERY);
8317         lpfc_cancel_retry_delay_tmo(vport, ndlp);
8318         spin_lock_irqsave(shost->host_lock, flags);
8319         ndlp->nlp_flag |= NLP_NPR_2B_DISC;
8320         spin_unlock_irqrestore(shost->host_lock, flags);
8321         lpfc_disc_start(vport);
8322 }
8323
8324 /* lpfc_sli_abts_err_handler - handle a failed ABTS request from an SLI3 port.
8325  * @phba: Pointer to HBA context object.
8326  * @iocbq: Pointer to iocb object.
8327  *
8328  * The async_event handler calls this routine when it receives
8329  * an ASYNC_STATUS_CN event from the port.  The port generates
8330  * this event when an Abort Sequence request to an rport fails
8331  * twice in succession.  The abort could be originated by the
8332  * driver or by the port.  The ABTS could have been for an ELS
8333  * or FCP IO.  The port only generates this event when an ABTS
8334  * fails to complete after one retry.
8335  */
8336 static void
8337 lpfc_sli_abts_err_handler(struct lpfc_hba *phba,
8338                           struct lpfc_iocbq *iocbq)
8339 {
8340         struct lpfc_nodelist *ndlp = NULL;
8341         uint16_t rpi = 0, vpi = 0;
8342         struct lpfc_vport *vport = NULL;
8343
8344         /* The rpi in the ulpContext is vport-sensitive. */
8345         vpi = iocbq->iocb.un.asyncstat.sub_ctxt_tag;
8346         rpi = iocbq->iocb.ulpContext;
8347
8348         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8349                         "3092 Port generated ABTS async event "
8350                         "on vpi %d rpi %d status 0x%x\n",
8351                         vpi, rpi, iocbq->iocb.ulpStatus);
8352
8353         vport = lpfc_find_vport_by_vpid(phba, vpi);
8354         if (!vport)
8355                 goto err_exit;
8356         ndlp = lpfc_findnode_rpi(vport, rpi);
8357         if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
8358                 goto err_exit;
8359
8360         if (iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
8361                 lpfc_sli_abts_recover_port(vport, ndlp);
8362         return;
8363
8364  err_exit:
8365         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8366                         "3095 Event Context not found, no "
8367                         "action on vpi %d rpi %d status 0x%x, reason 0x%x\n",
8368                         iocbq->iocb.ulpContext, iocbq->iocb.ulpStatus,
8369                         vpi, rpi);
8370 }
8371
8372 /* lpfc_sli4_abts_err_handler - handle a failed ABTS request from an SLI4 port.
8373  * @phba: pointer to HBA context object.
8374  * @ndlp: nodelist pointer for the impacted rport.
8375  * @axri: pointer to the wcqe containing the failed exchange.
8376  *
8377  * The driver calls this routine when it receives an ABORT_XRI_FCP CQE from the
8378  * port.  The port generates this event when an abort exchange request to an
8379  * rport fails twice in succession with no reply.  The abort could be originated
8380  * by the driver or by the port.  The ABTS could have been for an ELS or FCP IO.
8381  */
8382 void
8383 lpfc_sli4_abts_err_handler(struct lpfc_hba *phba,
8384                            struct lpfc_nodelist *ndlp,
8385                            struct sli4_wcqe_xri_aborted *axri)
8386 {
8387         struct lpfc_vport *vport;
8388
8389         if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
8390                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8391                                 "3115 Node Context not found, driver "
8392                                 "ignoring abts err event\n");
8393                 return;
8394         }
8395
8396         vport = ndlp->vport;
8397         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8398                         "3116 Port generated FCP XRI ABORT event on "
8399                         "vpi %d rpi %d xri x%x status 0x%x\n",
8400                         ndlp->vport->vpi, ndlp->nlp_rpi,
8401                         bf_get(lpfc_wcqe_xa_xri, axri),
8402                         bf_get(lpfc_wcqe_xa_status, axri));
8403
8404         if (bf_get(lpfc_wcqe_xa_status, axri) == IOSTAT_LOCAL_REJECT)
8405                 lpfc_sli_abts_recover_port(vport, ndlp);
8406 }
8407
8408 /**
8409  * lpfc_sli_async_event_handler - ASYNC iocb handler function
8410  * @phba: Pointer to HBA context object.
8411  * @pring: Pointer to driver SLI ring object.
8412  * @iocbq: Pointer to iocb object.
8413  *
8414  * This function is called by the slow ring event handler
8415  * function when there is an ASYNC event iocb in the ring.
8416  * This function is called with no lock held.
8417  * Currently this function handles only temperature related
8418  * ASYNC events. The function decodes the temperature sensor
8419  * event message and posts events for the management applications.
8420  **/
8421 static void
8422 lpfc_sli_async_event_handler(struct lpfc_hba * phba,
8423         struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
8424 {
8425         IOCB_t *icmd;
8426         uint16_t evt_code;
8427         struct temp_event temp_event_data;
8428         struct Scsi_Host *shost;
8429         uint32_t *iocb_w;
8430
8431         icmd = &iocbq->iocb;
8432         evt_code = icmd->un.asyncstat.evt_code;
8433
8434         switch (evt_code) {
8435         case ASYNC_TEMP_WARN:
8436         case ASYNC_TEMP_SAFE:
8437                 temp_event_data.data = (uint32_t) icmd->ulpContext;
8438                 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
8439                 if (evt_code == ASYNC_TEMP_WARN) {
8440                         temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
8441                         lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
8442                                 "0347 Adapter is very hot, please take "
8443                                 "corrective action. temperature : %d Celsius\n",
8444                                 (uint32_t) icmd->ulpContext);
8445                 } else {
8446                         temp_event_data.event_code = LPFC_NORMAL_TEMP;
8447                         lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
8448                                 "0340 Adapter temperature is OK now. "
8449                                 "temperature : %d Celsius\n",
8450                                 (uint32_t) icmd->ulpContext);
8451                 }
8452
8453                 /* Send temperature change event to applications */
8454                 shost = lpfc_shost_from_vport(phba->pport);
8455                 fc_host_post_vendor_event(shost, fc_get_event_number(),
8456                         sizeof(temp_event_data), (char *) &temp_event_data,
8457                         LPFC_NL_VENDOR_ID);
8458                 break;
8459         case ASYNC_STATUS_CN:
8460                 lpfc_sli_abts_err_handler(phba, iocbq);
8461                 break;
8462         default:
8463                 iocb_w = (uint32_t *) icmd;
8464                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8465                         "0346 Ring %d handler: unexpected ASYNC_STATUS"
8466                         " evt_code 0x%x\n"
8467                         "W0  0x%08x W1  0x%08x W2  0x%08x W3  0x%08x\n"
8468                         "W4  0x%08x W5  0x%08x W6  0x%08x W7  0x%08x\n"
8469                         "W8  0x%08x W9  0x%08x W10 0x%08x W11 0x%08x\n"
8470                         "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
8471                         pring->ringno, icmd->un.asyncstat.evt_code,
8472                         iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
8473                         iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
8474                         iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
8475                         iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
8476
8477                 break;
8478         }
8479 }
8480
8481
8482 /**
8483  * lpfc_sli_setup - SLI ring setup function
8484  * @phba: Pointer to HBA context object.
8485  *
8486  * lpfc_sli_setup sets up rings of the SLI interface with
8487  * number of iocbs per ring and iotags. This function is
8488  * called while driver attach to the HBA and before the
8489  * interrupts are enabled. So there is no need for locking.
8490  *
8491  * This function always returns 0.
8492  **/
8493 int
8494 lpfc_sli_setup(struct lpfc_hba *phba)
8495 {
8496         int i, totiocbsize = 0;
8497         struct lpfc_sli *psli = &phba->sli;
8498         struct lpfc_sli_ring *pring;
8499
8500         psli->num_rings = MAX_CONFIGURED_RINGS;
8501         psli->sli_flag = 0;
8502         psli->fcp_ring = LPFC_FCP_RING;
8503         psli->next_ring = LPFC_FCP_NEXT_RING;
8504         psli->extra_ring = LPFC_EXTRA_RING;
8505
8506         psli->iocbq_lookup = NULL;
8507         psli->iocbq_lookup_len = 0;
8508         psli->last_iotag = 0;
8509
8510         for (i = 0; i < psli->num_rings; i++) {
8511                 pring = &psli->ring[i];
8512                 switch (i) {
8513                 case LPFC_FCP_RING:     /* ring 0 - FCP */
8514                         /* numCiocb and numRiocb are used in config_port */
8515                         pring->numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
8516                         pring->numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
8517                         pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
8518                         pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
8519                         pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
8520                         pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
8521                         pring->sizeCiocb = (phba->sli_rev == 3) ?
8522                                                         SLI3_IOCB_CMD_SIZE :
8523                                                         SLI2_IOCB_CMD_SIZE;
8524                         pring->sizeRiocb = (phba->sli_rev == 3) ?
8525                                                         SLI3_IOCB_RSP_SIZE :
8526                                                         SLI2_IOCB_RSP_SIZE;
8527                         pring->iotag_ctr = 0;
8528                         pring->iotag_max =
8529                             (phba->cfg_hba_queue_depth * 2);
8530                         pring->fast_iotag = pring->iotag_max;
8531                         pring->num_mask = 0;
8532                         break;
8533                 case LPFC_EXTRA_RING:   /* ring 1 - EXTRA */
8534                         /* numCiocb and numRiocb are used in config_port */
8535                         pring->numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
8536                         pring->numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
8537                         pring->sizeCiocb = (phba->sli_rev == 3) ?
8538                                                         SLI3_IOCB_CMD_SIZE :
8539                                                         SLI2_IOCB_CMD_SIZE;
8540                         pring->sizeRiocb = (phba->sli_rev == 3) ?
8541                                                         SLI3_IOCB_RSP_SIZE :
8542                                                         SLI2_IOCB_RSP_SIZE;
8543                         pring->iotag_max = phba->cfg_hba_queue_depth;
8544                         pring->num_mask = 0;
8545                         break;
8546                 case LPFC_ELS_RING:     /* ring 2 - ELS / CT */
8547                         /* numCiocb and numRiocb are used in config_port */
8548                         pring->numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
8549                         pring->numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
8550                         pring->sizeCiocb = (phba->sli_rev == 3) ?
8551                                                         SLI3_IOCB_CMD_SIZE :
8552                                                         SLI2_IOCB_CMD_SIZE;
8553                         pring->sizeRiocb = (phba->sli_rev == 3) ?
8554                                                         SLI3_IOCB_RSP_SIZE :
8555                                                         SLI2_IOCB_RSP_SIZE;
8556                         pring->fast_iotag = 0;
8557                         pring->iotag_ctr = 0;
8558                         pring->iotag_max = 4096;
8559                         pring->lpfc_sli_rcv_async_status =
8560                                 lpfc_sli_async_event_handler;
8561                         pring->num_mask = LPFC_MAX_RING_MASK;
8562                         pring->prt[0].profile = 0;      /* Mask 0 */
8563                         pring->prt[0].rctl = FC_RCTL_ELS_REQ;
8564                         pring->prt[0].type = FC_TYPE_ELS;
8565                         pring->prt[0].lpfc_sli_rcv_unsol_event =
8566                             lpfc_els_unsol_event;
8567                         pring->prt[1].profile = 0;      /* Mask 1 */
8568                         pring->prt[1].rctl = FC_RCTL_ELS_REP;
8569                         pring->prt[1].type = FC_TYPE_ELS;
8570                         pring->prt[1].lpfc_sli_rcv_unsol_event =
8571                             lpfc_els_unsol_event;
8572                         pring->prt[2].profile = 0;      /* Mask 2 */
8573                         /* NameServer Inquiry */
8574                         pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
8575                         /* NameServer */
8576                         pring->prt[2].type = FC_TYPE_CT;
8577                         pring->prt[2].lpfc_sli_rcv_unsol_event =
8578                             lpfc_ct_unsol_event;
8579                         pring->prt[3].profile = 0;      /* Mask 3 */
8580                         /* NameServer response */
8581                         pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
8582                         /* NameServer */
8583                         pring->prt[3].type = FC_TYPE_CT;
8584                         pring->prt[3].lpfc_sli_rcv_unsol_event =
8585                             lpfc_ct_unsol_event;
8586                         /* abort unsolicited sequence */
8587                         pring->prt[4].profile = 0;      /* Mask 4 */
8588                         pring->prt[4].rctl = FC_RCTL_BA_ABTS;
8589                         pring->prt[4].type = FC_TYPE_BLS;
8590                         pring->prt[4].lpfc_sli_rcv_unsol_event =
8591                             lpfc_sli4_ct_abort_unsol_event;
8592                         break;
8593                 }
8594                 totiocbsize += (pring->numCiocb * pring->sizeCiocb) +
8595                                 (pring->numRiocb * pring->sizeRiocb);
8596         }
8597         if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
8598                 /* Too many cmd / rsp ring entries in SLI2 SLIM */
8599                 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
8600                        "SLI2 SLIM Data: x%x x%lx\n",
8601                        phba->brd_no, totiocbsize,
8602                        (unsigned long) MAX_SLIM_IOCB_SIZE);
8603         }
8604         if (phba->cfg_multi_ring_support == 2)
8605                 lpfc_extra_ring_setup(phba);
8606
8607         return 0;
8608 }
8609
8610 /**
8611  * lpfc_sli_queue_setup - Queue initialization function
8612  * @phba: Pointer to HBA context object.
8613  *
8614  * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
8615  * ring. This function also initializes ring indices of each ring.
8616  * This function is called during the initialization of the SLI
8617  * interface of an HBA.
8618  * This function is called with no lock held and always returns
8619  * 1.
8620  **/
8621 int
8622 lpfc_sli_queue_setup(struct lpfc_hba *phba)
8623 {
8624         struct lpfc_sli *psli;
8625         struct lpfc_sli_ring *pring;
8626         int i;
8627
8628         psli = &phba->sli;
8629         spin_lock_irq(&phba->hbalock);
8630         INIT_LIST_HEAD(&psli->mboxq);
8631         INIT_LIST_HEAD(&psli->mboxq_cmpl);
8632         /* Initialize list headers for txq and txcmplq as double linked lists */
8633         for (i = 0; i < psli->num_rings; i++) {
8634                 pring = &psli->ring[i];
8635                 pring->ringno = i;
8636                 pring->next_cmdidx  = 0;
8637                 pring->local_getidx = 0;
8638                 pring->cmdidx = 0;
8639                 INIT_LIST_HEAD(&pring->txq);
8640                 INIT_LIST_HEAD(&pring->txcmplq);
8641                 INIT_LIST_HEAD(&pring->iocb_continueq);
8642                 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
8643                 INIT_LIST_HEAD(&pring->postbufq);
8644         }
8645         spin_unlock_irq(&phba->hbalock);
8646         return 1;
8647 }
8648
8649 /**
8650  * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
8651  * @phba: Pointer to HBA context object.
8652  *
8653  * This routine flushes the mailbox command subsystem. It will unconditionally
8654  * flush all the mailbox commands in the three possible stages in the mailbox
8655  * command sub-system: pending mailbox command queue; the outstanding mailbox
8656  * command; and completed mailbox command queue. It is caller's responsibility
8657  * to make sure that the driver is in the proper state to flush the mailbox
8658  * command sub-system. Namely, the posting of mailbox commands into the
8659  * pending mailbox command queue from the various clients must be stopped;
8660  * either the HBA is in a state that it will never works on the outstanding
8661  * mailbox command (such as in EEH or ERATT conditions) or the outstanding
8662  * mailbox command has been completed.
8663  **/
8664 static void
8665 lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
8666 {
8667         LIST_HEAD(completions);
8668         struct lpfc_sli *psli = &phba->sli;
8669         LPFC_MBOXQ_t *pmb;
8670         unsigned long iflag;
8671
8672         /* Flush all the mailbox commands in the mbox system */
8673         spin_lock_irqsave(&phba->hbalock, iflag);
8674         /* The pending mailbox command queue */
8675         list_splice_init(&phba->sli.mboxq, &completions);
8676         /* The outstanding active mailbox command */
8677         if (psli->mbox_active) {
8678                 list_add_tail(&psli->mbox_active->list, &completions);
8679                 psli->mbox_active = NULL;
8680                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8681         }
8682         /* The completed mailbox command queue */
8683         list_splice_init(&phba->sli.mboxq_cmpl, &completions);
8684         spin_unlock_irqrestore(&phba->hbalock, iflag);
8685
8686         /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
8687         while (!list_empty(&completions)) {
8688                 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
8689                 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
8690                 if (pmb->mbox_cmpl)
8691                         pmb->mbox_cmpl(phba, pmb);
8692         }
8693 }
8694
8695 /**
8696  * lpfc_sli_host_down - Vport cleanup function
8697  * @vport: Pointer to virtual port object.
8698  *
8699  * lpfc_sli_host_down is called to clean up the resources
8700  * associated with a vport before destroying virtual
8701  * port data structures.
8702  * This function does following operations:
8703  * - Free discovery resources associated with this virtual
8704  *   port.
8705  * - Free iocbs associated with this virtual port in
8706  *   the txq.
8707  * - Send abort for all iocb commands associated with this
8708  *   vport in txcmplq.
8709  *
8710  * This function is called with no lock held and always returns 1.
8711  **/
8712 int
8713 lpfc_sli_host_down(struct lpfc_vport *vport)
8714 {
8715         LIST_HEAD(completions);
8716         struct lpfc_hba *phba = vport->phba;
8717         struct lpfc_sli *psli = &phba->sli;
8718         struct lpfc_sli_ring *pring;
8719         struct lpfc_iocbq *iocb, *next_iocb;
8720         int i;
8721         unsigned long flags = 0;
8722         uint16_t prev_pring_flag;
8723
8724         lpfc_cleanup_discovery_resources(vport);
8725
8726         spin_lock_irqsave(&phba->hbalock, flags);
8727         for (i = 0; i < psli->num_rings; i++) {
8728                 pring = &psli->ring[i];
8729                 prev_pring_flag = pring->flag;
8730                 /* Only slow rings */
8731                 if (pring->ringno == LPFC_ELS_RING) {
8732                         pring->flag |= LPFC_DEFERRED_RING_EVENT;
8733                         /* Set the lpfc data pending flag */
8734                         set_bit(LPFC_DATA_READY, &phba->data_flags);
8735                 }
8736                 /*
8737                  * Error everything on the txq since these iocbs have not been
8738                  * given to the FW yet.
8739                  */
8740                 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
8741                         if (iocb->vport != vport)
8742                                 continue;
8743                         list_move_tail(&iocb->list, &completions);
8744                         pring->txq_cnt--;
8745                 }
8746
8747                 /* Next issue ABTS for everything on the txcmplq */
8748                 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
8749                                                                         list) {
8750                         if (iocb->vport != vport)
8751                                 continue;
8752                         lpfc_sli_issue_abort_iotag(phba, pring, iocb);
8753                 }
8754
8755                 pring->flag = prev_pring_flag;
8756         }
8757
8758         spin_unlock_irqrestore(&phba->hbalock, flags);
8759
8760         /* Cancel all the IOCBs from the completions list */
8761         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
8762                               IOERR_SLI_DOWN);
8763         return 1;
8764 }
8765
8766 /**
8767  * lpfc_sli_hba_down - Resource cleanup function for the HBA
8768  * @phba: Pointer to HBA context object.
8769  *
8770  * This function cleans up all iocb, buffers, mailbox commands
8771  * while shutting down the HBA. This function is called with no
8772  * lock held and always returns 1.
8773  * This function does the following to cleanup driver resources:
8774  * - Free discovery resources for each virtual port
8775  * - Cleanup any pending fabric iocbs
8776  * - Iterate through the iocb txq and free each entry
8777  *   in the list.
8778  * - Free up any buffer posted to the HBA
8779  * - Free mailbox commands in the mailbox queue.
8780  **/
8781 int
8782 lpfc_sli_hba_down(struct lpfc_hba *phba)
8783 {
8784         LIST_HEAD(completions);
8785         struct lpfc_sli *psli = &phba->sli;
8786         struct lpfc_sli_ring *pring;
8787         struct lpfc_dmabuf *buf_ptr;
8788         unsigned long flags = 0;
8789         int i;
8790
8791         /* Shutdown the mailbox command sub-system */
8792         lpfc_sli_mbox_sys_shutdown(phba);
8793
8794         lpfc_hba_down_prep(phba);
8795
8796         lpfc_fabric_abort_hba(phba);
8797
8798         spin_lock_irqsave(&phba->hbalock, flags);
8799         for (i = 0; i < psli->num_rings; i++) {
8800                 pring = &psli->ring[i];
8801                 /* Only slow rings */
8802                 if (pring->ringno == LPFC_ELS_RING) {
8803                         pring->flag |= LPFC_DEFERRED_RING_EVENT;
8804                         /* Set the lpfc data pending flag */
8805                         set_bit(LPFC_DATA_READY, &phba->data_flags);
8806                 }
8807
8808                 /*
8809                  * Error everything on the txq since these iocbs have not been
8810                  * given to the FW yet.
8811                  */
8812                 list_splice_init(&pring->txq, &completions);
8813                 pring->txq_cnt = 0;
8814
8815         }
8816         spin_unlock_irqrestore(&phba->hbalock, flags);
8817
8818         /* Cancel all the IOCBs from the completions list */
8819         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
8820                               IOERR_SLI_DOWN);
8821
8822         spin_lock_irqsave(&phba->hbalock, flags);
8823         list_splice_init(&phba->elsbuf, &completions);
8824         phba->elsbuf_cnt = 0;
8825         phba->elsbuf_prev_cnt = 0;
8826         spin_unlock_irqrestore(&phba->hbalock, flags);
8827
8828         while (!list_empty(&completions)) {
8829                 list_remove_head(&completions, buf_ptr,
8830                         struct lpfc_dmabuf, list);
8831                 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
8832                 kfree(buf_ptr);
8833         }
8834
8835         /* Return any active mbox cmds */
8836         del_timer_sync(&psli->mbox_tmo);
8837
8838         spin_lock_irqsave(&phba->pport->work_port_lock, flags);
8839         phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
8840         spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
8841
8842         return 1;
8843 }
8844
8845 /**
8846  * lpfc_sli_pcimem_bcopy - SLI memory copy function
8847  * @srcp: Source memory pointer.
8848  * @destp: Destination memory pointer.
8849  * @cnt: Number of words required to be copied.
8850  *
8851  * This function is used for copying data between driver memory
8852  * and the SLI memory. This function also changes the endianness
8853  * of each word if native endianness is different from SLI
8854  * endianness. This function can be called with or without
8855  * lock.
8856  **/
8857 void
8858 lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
8859 {
8860         uint32_t *src = srcp;
8861         uint32_t *dest = destp;
8862         uint32_t ldata;
8863         int i;
8864
8865         for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
8866                 ldata = *src;
8867                 ldata = le32_to_cpu(ldata);
8868                 *dest = ldata;
8869                 src++;
8870                 dest++;
8871         }
8872 }
8873
8874
8875 /**
8876  * lpfc_sli_bemem_bcopy - SLI memory copy function
8877  * @srcp: Source memory pointer.
8878  * @destp: Destination memory pointer.
8879  * @cnt: Number of words required to be copied.
8880  *
8881  * This function is used for copying data between a data structure
8882  * with big endian representation to local endianness.
8883  * This function can be called with or without lock.
8884  **/
8885 void
8886 lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
8887 {
8888         uint32_t *src = srcp;
8889         uint32_t *dest = destp;
8890         uint32_t ldata;
8891         int i;
8892
8893         for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
8894                 ldata = *src;
8895                 ldata = be32_to_cpu(ldata);
8896                 *dest = ldata;
8897                 src++;
8898                 dest++;
8899         }
8900 }
8901
8902 /**
8903  * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
8904  * @phba: Pointer to HBA context object.
8905  * @pring: Pointer to driver SLI ring object.
8906  * @mp: Pointer to driver buffer object.
8907  *
8908  * This function is called with no lock held.
8909  * It always return zero after adding the buffer to the postbufq
8910  * buffer list.
8911  **/
8912 int
8913 lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
8914                          struct lpfc_dmabuf *mp)
8915 {
8916         /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
8917            later */
8918         spin_lock_irq(&phba->hbalock);
8919         list_add_tail(&mp->list, &pring->postbufq);
8920         pring->postbufq_cnt++;
8921         spin_unlock_irq(&phba->hbalock);
8922         return 0;
8923 }
8924
8925 /**
8926  * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
8927  * @phba: Pointer to HBA context object.
8928  *
8929  * When HBQ is enabled, buffers are searched based on tags. This function
8930  * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
8931  * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
8932  * does not conflict with tags of buffer posted for unsolicited events.
8933  * The function returns the allocated tag. The function is called with
8934  * no locks held.
8935  **/
8936 uint32_t
8937 lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
8938 {
8939         spin_lock_irq(&phba->hbalock);
8940         phba->buffer_tag_count++;
8941         /*
8942          * Always set the QUE_BUFTAG_BIT to distiguish between
8943          * a tag assigned by HBQ.
8944          */
8945         phba->buffer_tag_count |= QUE_BUFTAG_BIT;
8946         spin_unlock_irq(&phba->hbalock);
8947         return phba->buffer_tag_count;
8948 }
8949
8950 /**
8951  * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
8952  * @phba: Pointer to HBA context object.
8953  * @pring: Pointer to driver SLI ring object.
8954  * @tag: Buffer tag.
8955  *
8956  * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
8957  * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
8958  * iocb is posted to the response ring with the tag of the buffer.
8959  * This function searches the pring->postbufq list using the tag
8960  * to find buffer associated with CMD_IOCB_RET_XRI64_CX
8961  * iocb. If the buffer is found then lpfc_dmabuf object of the
8962  * buffer is returned to the caller else NULL is returned.
8963  * This function is called with no lock held.
8964  **/
8965 struct lpfc_dmabuf *
8966 lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
8967                         uint32_t tag)
8968 {
8969         struct lpfc_dmabuf *mp, *next_mp;
8970         struct list_head *slp = &pring->postbufq;
8971
8972         /* Search postbufq, from the beginning, looking for a match on tag */
8973         spin_lock_irq(&phba->hbalock);
8974         list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
8975                 if (mp->buffer_tag == tag) {
8976                         list_del_init(&mp->list);
8977                         pring->postbufq_cnt--;
8978                         spin_unlock_irq(&phba->hbalock);
8979                         return mp;
8980                 }
8981         }
8982
8983         spin_unlock_irq(&phba->hbalock);
8984         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8985                         "0402 Cannot find virtual addr for buffer tag on "
8986                         "ring %d Data x%lx x%p x%p x%x\n",
8987                         pring->ringno, (unsigned long) tag,
8988                         slp->next, slp->prev, pring->postbufq_cnt);
8989
8990         return NULL;
8991 }
8992
8993 /**
8994  * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
8995  * @phba: Pointer to HBA context object.
8996  * @pring: Pointer to driver SLI ring object.
8997  * @phys: DMA address of the buffer.
8998  *
8999  * This function searches the buffer list using the dma_address
9000  * of unsolicited event to find the driver's lpfc_dmabuf object
9001  * corresponding to the dma_address. The function returns the
9002  * lpfc_dmabuf object if a buffer is found else it returns NULL.
9003  * This function is called by the ct and els unsolicited event
9004  * handlers to get the buffer associated with the unsolicited
9005  * event.
9006  *
9007  * This function is called with no lock held.
9008  **/
9009 struct lpfc_dmabuf *
9010 lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9011                          dma_addr_t phys)
9012 {
9013         struct lpfc_dmabuf *mp, *next_mp;
9014         struct list_head *slp = &pring->postbufq;
9015
9016         /* Search postbufq, from the beginning, looking for a match on phys */
9017         spin_lock_irq(&phba->hbalock);
9018         list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
9019                 if (mp->phys == phys) {
9020                         list_del_init(&mp->list);
9021                         pring->postbufq_cnt--;
9022                         spin_unlock_irq(&phba->hbalock);
9023                         return mp;
9024                 }
9025         }
9026
9027         spin_unlock_irq(&phba->hbalock);
9028         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9029                         "0410 Cannot find virtual addr for mapped buf on "
9030                         "ring %d Data x%llx x%p x%p x%x\n",
9031                         pring->ringno, (unsigned long long)phys,
9032                         slp->next, slp->prev, pring->postbufq_cnt);
9033         return NULL;
9034 }
9035
9036 /**
9037  * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
9038  * @phba: Pointer to HBA context object.
9039  * @cmdiocb: Pointer to driver command iocb object.
9040  * @rspiocb: Pointer to driver response iocb object.
9041  *
9042  * This function is the completion handler for the abort iocbs for
9043  * ELS commands. This function is called from the ELS ring event
9044  * handler with no lock held. This function frees memory resources
9045  * associated with the abort iocb.
9046  **/
9047 static void
9048 lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9049                         struct lpfc_iocbq *rspiocb)
9050 {
9051         IOCB_t *irsp = &rspiocb->iocb;
9052         uint16_t abort_iotag, abort_context;
9053         struct lpfc_iocbq *abort_iocb = NULL;
9054
9055         if (irsp->ulpStatus) {
9056
9057                 /*
9058                  * Assume that the port already completed and returned, or
9059                  * will return the iocb. Just Log the message.
9060                  */
9061                 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
9062                 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
9063
9064                 spin_lock_irq(&phba->hbalock);
9065                 if (phba->sli_rev < LPFC_SLI_REV4) {
9066                         if (abort_iotag != 0 &&
9067                                 abort_iotag <= phba->sli.last_iotag)
9068                                 abort_iocb =
9069                                         phba->sli.iocbq_lookup[abort_iotag];
9070                 } else
9071                         /* For sli4 the abort_tag is the XRI,
9072                          * so the abort routine puts the iotag  of the iocb
9073                          * being aborted in the context field of the abort
9074                          * IOCB.
9075                          */
9076                         abort_iocb = phba->sli.iocbq_lookup[abort_context];
9077
9078                 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
9079                                 "0327 Cannot abort els iocb %p "
9080                                 "with tag %x context %x, abort status %x, "
9081                                 "abort code %x\n",
9082                                 abort_iocb, abort_iotag, abort_context,
9083                                 irsp->ulpStatus, irsp->un.ulpWord[4]);
9084
9085                 spin_unlock_irq(&phba->hbalock);
9086         }
9087         lpfc_sli_release_iocbq(phba, cmdiocb);
9088         return;
9089 }
9090
9091 /**
9092  * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
9093  * @phba: Pointer to HBA context object.
9094  * @cmdiocb: Pointer to driver command iocb object.
9095  * @rspiocb: Pointer to driver response iocb object.
9096  *
9097  * The function is called from SLI ring event handler with no
9098  * lock held. This function is the completion handler for ELS commands
9099  * which are aborted. The function frees memory resources used for
9100  * the aborted ELS commands.
9101  **/
9102 static void
9103 lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9104                      struct lpfc_iocbq *rspiocb)
9105 {
9106         IOCB_t *irsp = &rspiocb->iocb;
9107
9108         /* ELS cmd tag <ulpIoTag> completes */
9109         lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
9110                         "0139 Ignoring ELS cmd tag x%x completion Data: "
9111                         "x%x x%x x%x\n",
9112                         irsp->ulpIoTag, irsp->ulpStatus,
9113                         irsp->un.ulpWord[4], irsp->ulpTimeout);
9114         if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
9115                 lpfc_ct_free_iocb(phba, cmdiocb);
9116         else
9117                 lpfc_els_free_iocb(phba, cmdiocb);
9118         return;
9119 }
9120
9121 /**
9122  * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
9123  * @phba: Pointer to HBA context object.
9124  * @pring: Pointer to driver SLI ring object.
9125  * @cmdiocb: Pointer to driver command iocb object.
9126  *
9127  * This function issues an abort iocb for the provided command iocb down to
9128  * the port. Other than the case the outstanding command iocb is an abort
9129  * request, this function issues abort out unconditionally. This function is
9130  * called with hbalock held. The function returns 0 when it fails due to
9131  * memory allocation failure or when the command iocb is an abort request.
9132  **/
9133 static int
9134 lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9135                            struct lpfc_iocbq *cmdiocb)
9136 {
9137         struct lpfc_vport *vport = cmdiocb->vport;
9138         struct lpfc_iocbq *abtsiocbp;
9139         IOCB_t *icmd = NULL;
9140         IOCB_t *iabt = NULL;
9141         int retval;
9142
9143         /*
9144          * There are certain command types we don't want to abort.  And we
9145          * don't want to abort commands that are already in the process of
9146          * being aborted.
9147          */
9148         icmd = &cmdiocb->iocb;
9149         if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
9150             icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
9151             (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
9152                 return 0;
9153
9154         /* issue ABTS for this IOCB based on iotag */
9155         abtsiocbp = __lpfc_sli_get_iocbq(phba);
9156         if (abtsiocbp == NULL)
9157                 return 0;
9158
9159         /* This signals the response to set the correct status
9160          * before calling the completion handler
9161          */
9162         cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
9163
9164         iabt = &abtsiocbp->iocb;
9165         iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
9166         iabt->un.acxri.abortContextTag = icmd->ulpContext;
9167         if (phba->sli_rev == LPFC_SLI_REV4) {
9168                 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
9169                 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
9170         }
9171         else
9172                 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
9173         iabt->ulpLe = 1;
9174         iabt->ulpClass = icmd->ulpClass;
9175
9176         /* ABTS WQE must go to the same WQ as the WQE to be aborted */
9177         abtsiocbp->fcp_wqidx = cmdiocb->fcp_wqidx;
9178         if (cmdiocb->iocb_flag & LPFC_IO_FCP)
9179                 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
9180
9181         if (phba->link_state >= LPFC_LINK_UP)
9182                 iabt->ulpCommand = CMD_ABORT_XRI_CN;
9183         else
9184                 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
9185
9186         abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
9187
9188         lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
9189                          "0339 Abort xri x%x, original iotag x%x, "
9190                          "abort cmd iotag x%x\n",
9191                          iabt->un.acxri.abortIoTag,
9192                          iabt->un.acxri.abortContextTag,
9193                          abtsiocbp->iotag);
9194         retval = __lpfc_sli_issue_iocb(phba, pring->ringno, abtsiocbp, 0);
9195
9196         if (retval)
9197                 __lpfc_sli_release_iocbq(phba, abtsiocbp);
9198
9199         /*
9200          * Caller to this routine should check for IOCB_ERROR
9201          * and handle it properly.  This routine no longer removes
9202          * iocb off txcmplq and call compl in case of IOCB_ERROR.
9203          */
9204         return retval;
9205 }
9206
9207 /**
9208  * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
9209  * @phba: Pointer to HBA context object.
9210  * @pring: Pointer to driver SLI ring object.
9211  * @cmdiocb: Pointer to driver command iocb object.
9212  *
9213  * This function issues an abort iocb for the provided command iocb. In case
9214  * of unloading, the abort iocb will not be issued to commands on the ELS
9215  * ring. Instead, the callback function shall be changed to those commands
9216  * so that nothing happens when them finishes. This function is called with
9217  * hbalock held. The function returns 0 when the command iocb is an abort
9218  * request.
9219  **/
9220 int
9221 lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9222                            struct lpfc_iocbq *cmdiocb)
9223 {
9224         struct lpfc_vport *vport = cmdiocb->vport;
9225         int retval = IOCB_ERROR;
9226         IOCB_t *icmd = NULL;
9227
9228         /*
9229          * There are certain command types we don't want to abort.  And we
9230          * don't want to abort commands that are already in the process of
9231          * being aborted.
9232          */
9233         icmd = &cmdiocb->iocb;
9234         if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
9235             icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
9236             (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
9237                 return 0;
9238
9239         /*
9240          * If we're unloading, don't abort iocb on the ELS ring, but change
9241          * the callback so that nothing happens when it finishes.
9242          */
9243         if ((vport->load_flag & FC_UNLOADING) &&
9244             (pring->ringno == LPFC_ELS_RING)) {
9245                 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
9246                         cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
9247                 else
9248                         cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
9249                 goto abort_iotag_exit;
9250         }
9251
9252         /* Now, we try to issue the abort to the cmdiocb out */
9253         retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
9254
9255 abort_iotag_exit:
9256         /*
9257          * Caller to this routine should check for IOCB_ERROR
9258          * and handle it properly.  This routine no longer removes
9259          * iocb off txcmplq and call compl in case of IOCB_ERROR.
9260          */
9261         return retval;
9262 }
9263
9264 /**
9265  * lpfc_sli_iocb_ring_abort - Unconditionally abort all iocbs on an iocb ring
9266  * @phba: Pointer to HBA context object.
9267  * @pring: Pointer to driver SLI ring object.
9268  *
9269  * This function aborts all iocbs in the given ring and frees all the iocb
9270  * objects in txq. This function issues abort iocbs unconditionally for all
9271  * the iocb commands in txcmplq. The iocbs in the txcmplq is not guaranteed
9272  * to complete before the return of this function. The caller is not required
9273  * to hold any locks.
9274  **/
9275 static void
9276 lpfc_sli_iocb_ring_abort(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
9277 {
9278         LIST_HEAD(completions);
9279         struct lpfc_iocbq *iocb, *next_iocb;
9280
9281         if (pring->ringno == LPFC_ELS_RING)
9282                 lpfc_fabric_abort_hba(phba);
9283
9284         spin_lock_irq(&phba->hbalock);
9285
9286         /* Take off all the iocbs on txq for cancelling */
9287         list_splice_init(&pring->txq, &completions);
9288         pring->txq_cnt = 0;
9289
9290         /* Next issue ABTS for everything on the txcmplq */
9291         list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
9292                 lpfc_sli_abort_iotag_issue(phba, pring, iocb);
9293
9294         spin_unlock_irq(&phba->hbalock);
9295
9296         /* Cancel all the IOCBs from the completions list */
9297         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
9298                               IOERR_SLI_ABORTED);
9299 }
9300
9301 /**
9302  * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
9303  * @phba: pointer to lpfc HBA data structure.
9304  *
9305  * This routine will abort all pending and outstanding iocbs to an HBA.
9306  **/
9307 void
9308 lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
9309 {
9310         struct lpfc_sli *psli = &phba->sli;
9311         struct lpfc_sli_ring *pring;
9312         int i;
9313
9314         for (i = 0; i < psli->num_rings; i++) {
9315                 pring = &psli->ring[i];
9316                 lpfc_sli_iocb_ring_abort(phba, pring);
9317         }
9318 }
9319
9320 /**
9321  * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
9322  * @iocbq: Pointer to driver iocb object.
9323  * @vport: Pointer to driver virtual port object.
9324  * @tgt_id: SCSI ID of the target.
9325  * @lun_id: LUN ID of the scsi device.
9326  * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
9327  *
9328  * This function acts as an iocb filter for functions which abort or count
9329  * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
9330  * 0 if the filtering criteria is met for the given iocb and will return
9331  * 1 if the filtering criteria is not met.
9332  * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
9333  * given iocb is for the SCSI device specified by vport, tgt_id and
9334  * lun_id parameter.
9335  * If ctx_cmd == LPFC_CTX_TGT,  the function returns 0 only if the
9336  * given iocb is for the SCSI target specified by vport and tgt_id
9337  * parameters.
9338  * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
9339  * given iocb is for the SCSI host associated with the given vport.
9340  * This function is called with no locks held.
9341  **/
9342 static int
9343 lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
9344                            uint16_t tgt_id, uint64_t lun_id,
9345                            lpfc_ctx_cmd ctx_cmd)
9346 {
9347         struct lpfc_scsi_buf *lpfc_cmd;
9348         int rc = 1;
9349
9350         if (!(iocbq->iocb_flag &  LPFC_IO_FCP))
9351                 return rc;
9352
9353         if (iocbq->vport != vport)
9354                 return rc;
9355
9356         lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
9357
9358         if (lpfc_cmd->pCmd == NULL)
9359                 return rc;
9360
9361         switch (ctx_cmd) {
9362         case LPFC_CTX_LUN:
9363                 if ((lpfc_cmd->rdata->pnode) &&
9364                     (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
9365                     (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
9366                         rc = 0;
9367                 break;
9368         case LPFC_CTX_TGT:
9369                 if ((lpfc_cmd->rdata->pnode) &&
9370                     (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
9371                         rc = 0;
9372                 break;
9373         case LPFC_CTX_HOST:
9374                 rc = 0;
9375                 break;
9376         default:
9377                 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
9378                         __func__, ctx_cmd);
9379                 break;
9380         }
9381
9382         return rc;
9383 }
9384
9385 /**
9386  * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
9387  * @vport: Pointer to virtual port.
9388  * @tgt_id: SCSI ID of the target.
9389  * @lun_id: LUN ID of the scsi device.
9390  * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
9391  *
9392  * This function returns number of FCP commands pending for the vport.
9393  * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
9394  * commands pending on the vport associated with SCSI device specified
9395  * by tgt_id and lun_id parameters.
9396  * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
9397  * commands pending on the vport associated with SCSI target specified
9398  * by tgt_id parameter.
9399  * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
9400  * commands pending on the vport.
9401  * This function returns the number of iocbs which satisfy the filter.
9402  * This function is called without any lock held.
9403  **/
9404 int
9405 lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
9406                   lpfc_ctx_cmd ctx_cmd)
9407 {
9408         struct lpfc_hba *phba = vport->phba;
9409         struct lpfc_iocbq *iocbq;
9410         int sum, i;
9411
9412         for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
9413                 iocbq = phba->sli.iocbq_lookup[i];
9414
9415                 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
9416                                                 ctx_cmd) == 0)
9417                         sum++;
9418         }
9419
9420         return sum;
9421 }
9422
9423 /**
9424  * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
9425  * @phba: Pointer to HBA context object
9426  * @cmdiocb: Pointer to command iocb object.
9427  * @rspiocb: Pointer to response iocb object.
9428  *
9429  * This function is called when an aborted FCP iocb completes. This
9430  * function is called by the ring event handler with no lock held.
9431  * This function frees the iocb.
9432  **/
9433 void
9434 lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9435                         struct lpfc_iocbq *rspiocb)
9436 {
9437         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9438                         "3096 ABORT_XRI_CN completing on xri x%x "
9439                         "original iotag x%x, abort cmd iotag x%x "
9440                         "status 0x%x, reason 0x%x\n",
9441                         cmdiocb->iocb.un.acxri.abortContextTag,
9442                         cmdiocb->iocb.un.acxri.abortIoTag,
9443                         cmdiocb->iotag, rspiocb->iocb.ulpStatus,
9444                         rspiocb->iocb.un.ulpWord[4]);
9445         lpfc_sli_release_iocbq(phba, cmdiocb);
9446         return;
9447 }
9448
9449 /**
9450  * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
9451  * @vport: Pointer to virtual port.
9452  * @pring: Pointer to driver SLI ring object.
9453  * @tgt_id: SCSI ID of the target.
9454  * @lun_id: LUN ID of the scsi device.
9455  * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
9456  *
9457  * This function sends an abort command for every SCSI command
9458  * associated with the given virtual port pending on the ring
9459  * filtered by lpfc_sli_validate_fcp_iocb function.
9460  * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
9461  * FCP iocbs associated with lun specified by tgt_id and lun_id
9462  * parameters
9463  * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
9464  * FCP iocbs associated with SCSI target specified by tgt_id parameter.
9465  * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
9466  * FCP iocbs associated with virtual port.
9467  * This function returns number of iocbs it failed to abort.
9468  * This function is called with no locks held.
9469  **/
9470 int
9471 lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
9472                     uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
9473 {
9474         struct lpfc_hba *phba = vport->phba;
9475         struct lpfc_iocbq *iocbq;
9476         struct lpfc_iocbq *abtsiocb;
9477         IOCB_t *cmd = NULL;
9478         int errcnt = 0, ret_val = 0;
9479         int i;
9480
9481         for (i = 1; i <= phba->sli.last_iotag; i++) {
9482                 iocbq = phba->sli.iocbq_lookup[i];
9483
9484                 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
9485                                                abort_cmd) != 0)
9486                         continue;
9487
9488                 /* issue ABTS for this IOCB based on iotag */
9489                 abtsiocb = lpfc_sli_get_iocbq(phba);
9490                 if (abtsiocb == NULL) {
9491                         errcnt++;
9492                         continue;
9493                 }
9494
9495                 cmd = &iocbq->iocb;
9496                 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
9497                 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
9498                 if (phba->sli_rev == LPFC_SLI_REV4)
9499                         abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
9500                 else
9501                         abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
9502                 abtsiocb->iocb.ulpLe = 1;
9503                 abtsiocb->iocb.ulpClass = cmd->ulpClass;
9504                 abtsiocb->vport = phba->pport;
9505
9506                 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
9507                 abtsiocb->fcp_wqidx = iocbq->fcp_wqidx;
9508                 if (iocbq->iocb_flag & LPFC_IO_FCP)
9509                         abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
9510
9511                 if (lpfc_is_link_up(phba))
9512                         abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
9513                 else
9514                         abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
9515
9516                 /* Setup callback routine and issue the command. */
9517                 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
9518                 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
9519                                               abtsiocb, 0);
9520                 if (ret_val == IOCB_ERROR) {
9521                         lpfc_sli_release_iocbq(phba, abtsiocb);
9522                         errcnt++;
9523                         continue;
9524                 }
9525         }
9526
9527         return errcnt;
9528 }
9529
9530 /**
9531  * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
9532  * @phba: Pointer to HBA context object.
9533  * @cmdiocbq: Pointer to command iocb.
9534  * @rspiocbq: Pointer to response iocb.
9535  *
9536  * This function is the completion handler for iocbs issued using
9537  * lpfc_sli_issue_iocb_wait function. This function is called by the
9538  * ring event handler function without any lock held. This function
9539  * can be called from both worker thread context and interrupt
9540  * context. This function also can be called from other thread which
9541  * cleans up the SLI layer objects.
9542  * This function copy the contents of the response iocb to the
9543  * response iocb memory object provided by the caller of
9544  * lpfc_sli_issue_iocb_wait and then wakes up the thread which
9545  * sleeps for the iocb completion.
9546  **/
9547 static void
9548 lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
9549                         struct lpfc_iocbq *cmdiocbq,
9550                         struct lpfc_iocbq *rspiocbq)
9551 {
9552         wait_queue_head_t *pdone_q;
9553         unsigned long iflags;
9554         struct lpfc_scsi_buf *lpfc_cmd;
9555
9556         spin_lock_irqsave(&phba->hbalock, iflags);
9557         cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
9558         if (cmdiocbq->context2 && rspiocbq)
9559                 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
9560                        &rspiocbq->iocb, sizeof(IOCB_t));
9561
9562         /* Set the exchange busy flag for task management commands */
9563         if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
9564                 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
9565                 lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
9566                         cur_iocbq);
9567                 lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
9568         }
9569
9570         pdone_q = cmdiocbq->context_un.wait_queue;
9571         if (pdone_q)
9572                 wake_up(pdone_q);
9573         spin_unlock_irqrestore(&phba->hbalock, iflags);
9574         return;
9575 }
9576
9577 /**
9578  * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
9579  * @phba: Pointer to HBA context object..
9580  * @piocbq: Pointer to command iocb.
9581  * @flag: Flag to test.
9582  *
9583  * This routine grabs the hbalock and then test the iocb_flag to
9584  * see if the passed in flag is set.
9585  * Returns:
9586  * 1 if flag is set.
9587  * 0 if flag is not set.
9588  **/
9589 static int
9590 lpfc_chk_iocb_flg(struct lpfc_hba *phba,
9591                  struct lpfc_iocbq *piocbq, uint32_t flag)
9592 {
9593         unsigned long iflags;
9594         int ret;
9595
9596         spin_lock_irqsave(&phba->hbalock, iflags);
9597         ret = piocbq->iocb_flag & flag;
9598         spin_unlock_irqrestore(&phba->hbalock, iflags);
9599         return ret;
9600
9601 }
9602
9603 /**
9604  * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
9605  * @phba: Pointer to HBA context object..
9606  * @pring: Pointer to sli ring.
9607  * @piocb: Pointer to command iocb.
9608  * @prspiocbq: Pointer to response iocb.
9609  * @timeout: Timeout in number of seconds.
9610  *
9611  * This function issues the iocb to firmware and waits for the
9612  * iocb to complete. If the iocb command is not
9613  * completed within timeout seconds, it returns IOCB_TIMEDOUT.
9614  * Caller should not free the iocb resources if this function
9615  * returns IOCB_TIMEDOUT.
9616  * The function waits for the iocb completion using an
9617  * non-interruptible wait.
9618  * This function will sleep while waiting for iocb completion.
9619  * So, this function should not be called from any context which
9620  * does not allow sleeping. Due to the same reason, this function
9621  * cannot be called with interrupt disabled.
9622  * This function assumes that the iocb completions occur while
9623  * this function sleep. So, this function cannot be called from
9624  * the thread which process iocb completion for this ring.
9625  * This function clears the iocb_flag of the iocb object before
9626  * issuing the iocb and the iocb completion handler sets this
9627  * flag and wakes this thread when the iocb completes.
9628  * The contents of the response iocb will be copied to prspiocbq
9629  * by the completion handler when the command completes.
9630  * This function returns IOCB_SUCCESS when success.
9631  * This function is called with no lock held.
9632  **/
9633 int
9634 lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
9635                          uint32_t ring_number,
9636                          struct lpfc_iocbq *piocb,
9637                          struct lpfc_iocbq *prspiocbq,
9638                          uint32_t timeout)
9639 {
9640         DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
9641         long timeleft, timeout_req = 0;
9642         int retval = IOCB_SUCCESS;
9643         uint32_t creg_val;
9644         struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
9645         /*
9646          * If the caller has provided a response iocbq buffer, then context2
9647          * is NULL or its an error.
9648          */
9649         if (prspiocbq) {
9650                 if (piocb->context2)
9651                         return IOCB_ERROR;
9652                 piocb->context2 = prspiocbq;
9653         }
9654
9655         piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
9656         piocb->context_un.wait_queue = &done_q;
9657         piocb->iocb_flag &= ~LPFC_IO_WAKE;
9658
9659         if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
9660                 if (lpfc_readl(phba->HCregaddr, &creg_val))
9661                         return IOCB_ERROR;
9662                 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
9663                 writel(creg_val, phba->HCregaddr);
9664                 readl(phba->HCregaddr); /* flush */
9665         }
9666
9667         retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
9668                                      SLI_IOCB_RET_IOCB);
9669         if (retval == IOCB_SUCCESS) {
9670                 timeout_req = timeout * HZ;
9671                 timeleft = wait_event_timeout(done_q,
9672                                 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
9673                                 timeout_req);
9674
9675                 if (piocb->iocb_flag & LPFC_IO_WAKE) {
9676                         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9677                                         "0331 IOCB wake signaled\n");
9678                 } else if (timeleft == 0) {
9679                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9680                                         "0338 IOCB wait timeout error - no "
9681                                         "wake response Data x%x\n", timeout);
9682                         retval = IOCB_TIMEDOUT;
9683                 } else {
9684                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9685                                         "0330 IOCB wake NOT set, "
9686                                         "Data x%x x%lx\n",
9687                                         timeout, (timeleft / jiffies));
9688                         retval = IOCB_TIMEDOUT;
9689                 }
9690         } else if (retval == IOCB_BUSY) {
9691                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9692                         "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
9693                         phba->iocb_cnt, pring->txq_cnt, pring->txcmplq_cnt);
9694                 return retval;
9695         } else {
9696                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9697                                 "0332 IOCB wait issue failed, Data x%x\n",
9698                                 retval);
9699                 retval = IOCB_ERROR;
9700         }
9701
9702         if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
9703                 if (lpfc_readl(phba->HCregaddr, &creg_val))
9704                         return IOCB_ERROR;
9705                 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
9706                 writel(creg_val, phba->HCregaddr);
9707                 readl(phba->HCregaddr); /* flush */
9708         }
9709
9710         if (prspiocbq)
9711                 piocb->context2 = NULL;
9712
9713         piocb->context_un.wait_queue = NULL;
9714         piocb->iocb_cmpl = NULL;
9715         return retval;
9716 }
9717
9718 /**
9719  * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
9720  * @phba: Pointer to HBA context object.
9721  * @pmboxq: Pointer to driver mailbox object.
9722  * @timeout: Timeout in number of seconds.
9723  *
9724  * This function issues the mailbox to firmware and waits for the
9725  * mailbox command to complete. If the mailbox command is not
9726  * completed within timeout seconds, it returns MBX_TIMEOUT.
9727  * The function waits for the mailbox completion using an
9728  * interruptible wait. If the thread is woken up due to a
9729  * signal, MBX_TIMEOUT error is returned to the caller. Caller
9730  * should not free the mailbox resources, if this function returns
9731  * MBX_TIMEOUT.
9732  * This function will sleep while waiting for mailbox completion.
9733  * So, this function should not be called from any context which
9734  * does not allow sleeping. Due to the same reason, this function
9735  * cannot be called with interrupt disabled.
9736  * This function assumes that the mailbox completion occurs while
9737  * this function sleep. So, this function cannot be called from
9738  * the worker thread which processes mailbox completion.
9739  * This function is called in the context of HBA management
9740  * applications.
9741  * This function returns MBX_SUCCESS when successful.
9742  * This function is called with no lock held.
9743  **/
9744 int
9745 lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
9746                          uint32_t timeout)
9747 {
9748         DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
9749         int retval;
9750         unsigned long flag;
9751
9752         /* The caller must leave context1 empty. */
9753         if (pmboxq->context1)
9754                 return MBX_NOT_FINISHED;
9755
9756         pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
9757         /* setup wake call as IOCB callback */
9758         pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
9759         /* setup context field to pass wait_queue pointer to wake function  */
9760         pmboxq->context1 = &done_q;
9761
9762         /* now issue the command */
9763         retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
9764         if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
9765                 wait_event_interruptible_timeout(done_q,
9766                                 pmboxq->mbox_flag & LPFC_MBX_WAKE,
9767                                 timeout * HZ);
9768
9769                 spin_lock_irqsave(&phba->hbalock, flag);
9770                 pmboxq->context1 = NULL;
9771                 /*
9772                  * if LPFC_MBX_WAKE flag is set the mailbox is completed
9773                  * else do not free the resources.
9774                  */
9775                 if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
9776                         retval = MBX_SUCCESS;
9777                         lpfc_sli4_swap_str(phba, pmboxq);
9778                 } else {
9779                         retval = MBX_TIMEOUT;
9780                         pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
9781                 }
9782                 spin_unlock_irqrestore(&phba->hbalock, flag);
9783         }
9784
9785         return retval;
9786 }
9787
9788 /**
9789  * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
9790  * @phba: Pointer to HBA context.
9791  *
9792  * This function is called to shutdown the driver's mailbox sub-system.
9793  * It first marks the mailbox sub-system is in a block state to prevent
9794  * the asynchronous mailbox command from issued off the pending mailbox
9795  * command queue. If the mailbox command sub-system shutdown is due to
9796  * HBA error conditions such as EEH or ERATT, this routine shall invoke
9797  * the mailbox sub-system flush routine to forcefully bring down the
9798  * mailbox sub-system. Otherwise, if it is due to normal condition (such
9799  * as with offline or HBA function reset), this routine will wait for the
9800  * outstanding mailbox command to complete before invoking the mailbox
9801  * sub-system flush routine to gracefully bring down mailbox sub-system.
9802  **/
9803 void
9804 lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba)
9805 {
9806         struct lpfc_sli *psli = &phba->sli;
9807         unsigned long timeout;
9808
9809         timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
9810         spin_lock_irq(&phba->hbalock);
9811         psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
9812         spin_unlock_irq(&phba->hbalock);
9813
9814         if (psli->sli_flag & LPFC_SLI_ACTIVE) {
9815                 spin_lock_irq(&phba->hbalock);
9816                 /* Determine how long we might wait for the active mailbox
9817                  * command to be gracefully completed by firmware.
9818                  */
9819                 if (phba->sli.mbox_active)
9820                         timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
9821                                                 phba->sli.mbox_active) *
9822                                                 1000) + jiffies;
9823                 spin_unlock_irq(&phba->hbalock);
9824
9825                 while (phba->sli.mbox_active) {
9826                         /* Check active mailbox complete status every 2ms */
9827                         msleep(2);
9828                         if (time_after(jiffies, timeout))
9829                                 /* Timeout, let the mailbox flush routine to
9830                                  * forcefully release active mailbox command
9831                                  */
9832                                 break;
9833                 }
9834         }
9835         lpfc_sli_mbox_sys_flush(phba);
9836 }
9837
9838 /**
9839  * lpfc_sli_eratt_read - read sli-3 error attention events
9840  * @phba: Pointer to HBA context.
9841  *
9842  * This function is called to read the SLI3 device error attention registers
9843  * for possible error attention events. The caller must hold the hostlock
9844  * with spin_lock_irq().
9845  *
9846  * This function returns 1 when there is Error Attention in the Host Attention
9847  * Register and returns 0 otherwise.
9848  **/
9849 static int
9850 lpfc_sli_eratt_read(struct lpfc_hba *phba)
9851 {
9852         uint32_t ha_copy;
9853
9854         /* Read chip Host Attention (HA) register */
9855         if (lpfc_readl(phba->HAregaddr, &ha_copy))
9856                 goto unplug_err;
9857
9858         if (ha_copy & HA_ERATT) {
9859                 /* Read host status register to retrieve error event */
9860                 if (lpfc_sli_read_hs(phba))
9861                         goto unplug_err;
9862
9863                 /* Check if there is a deferred error condition is active */
9864                 if ((HS_FFER1 & phba->work_hs) &&
9865                     ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
9866                       HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
9867                         phba->hba_flag |= DEFER_ERATT;
9868                         /* Clear all interrupt enable conditions */
9869                         writel(0, phba->HCregaddr);
9870                         readl(phba->HCregaddr);
9871                 }
9872
9873                 /* Set the driver HA work bitmap */
9874                 phba->work_ha |= HA_ERATT;
9875                 /* Indicate polling handles this ERATT */
9876                 phba->hba_flag |= HBA_ERATT_HANDLED;
9877                 return 1;
9878         }
9879         return 0;
9880
9881 unplug_err:
9882         /* Set the driver HS work bitmap */
9883         phba->work_hs |= UNPLUG_ERR;
9884         /* Set the driver HA work bitmap */
9885         phba->work_ha |= HA_ERATT;
9886         /* Indicate polling handles this ERATT */
9887         phba->hba_flag |= HBA_ERATT_HANDLED;
9888         return 1;
9889 }
9890
9891 /**
9892  * lpfc_sli4_eratt_read - read sli-4 error attention events
9893  * @phba: Pointer to HBA context.
9894  *
9895  * This function is called to read the SLI4 device error attention registers
9896  * for possible error attention events. The caller must hold the hostlock
9897  * with spin_lock_irq().
9898  *
9899  * This function returns 1 when there is Error Attention in the Host Attention
9900  * Register and returns 0 otherwise.
9901  **/
9902 static int
9903 lpfc_sli4_eratt_read(struct lpfc_hba *phba)
9904 {
9905         uint32_t uerr_sta_hi, uerr_sta_lo;
9906         uint32_t if_type, portsmphr;
9907         struct lpfc_register portstat_reg;
9908
9909         /*
9910          * For now, use the SLI4 device internal unrecoverable error
9911          * registers for error attention. This can be changed later.
9912          */
9913         if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
9914         switch (if_type) {
9915         case LPFC_SLI_INTF_IF_TYPE_0:
9916                 if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
9917                         &uerr_sta_lo) ||
9918                         lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
9919                         &uerr_sta_hi)) {
9920                         phba->work_hs |= UNPLUG_ERR;
9921                         phba->work_ha |= HA_ERATT;
9922                         phba->hba_flag |= HBA_ERATT_HANDLED;
9923                         return 1;
9924                 }
9925                 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
9926                     (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
9927                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9928                                         "1423 HBA Unrecoverable error: "
9929                                         "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
9930                                         "ue_mask_lo_reg=0x%x, "
9931                                         "ue_mask_hi_reg=0x%x\n",
9932                                         uerr_sta_lo, uerr_sta_hi,
9933                                         phba->sli4_hba.ue_mask_lo,
9934                                         phba->sli4_hba.ue_mask_hi);
9935                         phba->work_status[0] = uerr_sta_lo;
9936                         phba->work_status[1] = uerr_sta_hi;
9937                         phba->work_ha |= HA_ERATT;
9938                         phba->hba_flag |= HBA_ERATT_HANDLED;
9939                         return 1;
9940                 }
9941                 break;
9942         case LPFC_SLI_INTF_IF_TYPE_2:
9943                 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
9944                         &portstat_reg.word0) ||
9945                         lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
9946                         &portsmphr)){
9947                         phba->work_hs |= UNPLUG_ERR;
9948                         phba->work_ha |= HA_ERATT;
9949                         phba->hba_flag |= HBA_ERATT_HANDLED;
9950                         return 1;
9951                 }
9952                 if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
9953                         phba->work_status[0] =
9954                                 readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
9955                         phba->work_status[1] =
9956                                 readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
9957                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9958                                         "2885 Port Status Event: "
9959                                         "port status reg 0x%x, "
9960                                         "port smphr reg 0x%x, "
9961                                         "error 1=0x%x, error 2=0x%x\n",
9962                                         portstat_reg.word0,
9963                                         portsmphr,
9964                                         phba->work_status[0],
9965                                         phba->work_status[1]);
9966                         phba->work_ha |= HA_ERATT;
9967                         phba->hba_flag |= HBA_ERATT_HANDLED;
9968                         return 1;
9969                 }
9970                 break;
9971         case LPFC_SLI_INTF_IF_TYPE_1:
9972         default:
9973                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9974                                 "2886 HBA Error Attention on unsupported "
9975                                 "if type %d.", if_type);
9976                 return 1;
9977         }
9978
9979         return 0;
9980 }
9981
9982 /**
9983  * lpfc_sli_check_eratt - check error attention events
9984  * @phba: Pointer to HBA context.
9985  *
9986  * This function is called from timer soft interrupt context to check HBA's
9987  * error attention register bit for error attention events.
9988  *
9989  * This function returns 1 when there is Error Attention in the Host Attention
9990  * Register and returns 0 otherwise.
9991  **/
9992 int
9993 lpfc_sli_check_eratt(struct lpfc_hba *phba)
9994 {
9995         uint32_t ha_copy;
9996
9997         /* If somebody is waiting to handle an eratt, don't process it
9998          * here. The brdkill function will do this.
9999          */
10000         if (phba->link_flag & LS_IGNORE_ERATT)
10001                 return 0;
10002
10003         /* Check if interrupt handler handles this ERATT */
10004         spin_lock_irq(&phba->hbalock);
10005         if (phba->hba_flag & HBA_ERATT_HANDLED) {
10006                 /* Interrupt handler has handled ERATT */
10007                 spin_unlock_irq(&phba->hbalock);
10008                 return 0;
10009         }
10010
10011         /*
10012          * If there is deferred error attention, do not check for error
10013          * attention
10014          */
10015         if (unlikely(phba->hba_flag & DEFER_ERATT)) {
10016                 spin_unlock_irq(&phba->hbalock);
10017                 return 0;
10018         }
10019
10020         /* If PCI channel is offline, don't process it */
10021         if (unlikely(pci_channel_offline(phba->pcidev))) {
10022                 spin_unlock_irq(&phba->hbalock);
10023                 return 0;
10024         }
10025
10026         switch (phba->sli_rev) {
10027         case LPFC_SLI_REV2:
10028         case LPFC_SLI_REV3:
10029                 /* Read chip Host Attention (HA) register */
10030                 ha_copy = lpfc_sli_eratt_read(phba);
10031                 break;
10032         case LPFC_SLI_REV4:
10033                 /* Read device Uncoverable Error (UERR) registers */
10034                 ha_copy = lpfc_sli4_eratt_read(phba);
10035                 break;
10036         default:
10037                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10038                                 "0299 Invalid SLI revision (%d)\n",
10039                                 phba->sli_rev);
10040                 ha_copy = 0;
10041                 break;
10042         }
10043         spin_unlock_irq(&phba->hbalock);
10044
10045         return ha_copy;
10046 }
10047
10048 /**
10049  * lpfc_intr_state_check - Check device state for interrupt handling
10050  * @phba: Pointer to HBA context.
10051  *
10052  * This inline routine checks whether a device or its PCI slot is in a state
10053  * that the interrupt should be handled.
10054  *
10055  * This function returns 0 if the device or the PCI slot is in a state that
10056  * interrupt should be handled, otherwise -EIO.
10057  */
10058 static inline int
10059 lpfc_intr_state_check(struct lpfc_hba *phba)
10060 {
10061         /* If the pci channel is offline, ignore all the interrupts */
10062         if (unlikely(pci_channel_offline(phba->pcidev)))
10063                 return -EIO;
10064
10065         /* Update device level interrupt statistics */
10066         phba->sli.slistat.sli_intr++;
10067
10068         /* Ignore all interrupts during initialization. */
10069         if (unlikely(phba->link_state < LPFC_LINK_DOWN))
10070                 return -EIO;
10071
10072         return 0;
10073 }
10074
10075 /**
10076  * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
10077  * @irq: Interrupt number.
10078  * @dev_id: The device context pointer.
10079  *
10080  * This function is directly called from the PCI layer as an interrupt
10081  * service routine when device with SLI-3 interface spec is enabled with
10082  * MSI-X multi-message interrupt mode and there are slow-path events in
10083  * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
10084  * interrupt mode, this function is called as part of the device-level
10085  * interrupt handler. When the PCI slot is in error recovery or the HBA
10086  * is undergoing initialization, the interrupt handler will not process
10087  * the interrupt. The link attention and ELS ring attention events are
10088  * handled by the worker thread. The interrupt handler signals the worker
10089  * thread and returns for these events. This function is called without
10090  * any lock held. It gets the hbalock to access and update SLI data
10091  * structures.
10092  *
10093  * This function returns IRQ_HANDLED when interrupt is handled else it
10094  * returns IRQ_NONE.
10095  **/
10096 irqreturn_t
10097 lpfc_sli_sp_intr_handler(int irq, void *dev_id)
10098 {
10099         struct lpfc_hba  *phba;
10100         uint32_t ha_copy, hc_copy;
10101         uint32_t work_ha_copy;
10102         unsigned long status;
10103         unsigned long iflag;
10104         uint32_t control;
10105
10106         MAILBOX_t *mbox, *pmbox;
10107         struct lpfc_vport *vport;
10108         struct lpfc_nodelist *ndlp;
10109         struct lpfc_dmabuf *mp;
10110         LPFC_MBOXQ_t *pmb;
10111         int rc;
10112
10113         /*
10114          * Get the driver's phba structure from the dev_id and
10115          * assume the HBA is not interrupting.
10116          */
10117         phba = (struct lpfc_hba *)dev_id;
10118
10119         if (unlikely(!phba))
10120                 return IRQ_NONE;
10121
10122         /*
10123          * Stuff needs to be attented to when this function is invoked as an
10124          * individual interrupt handler in MSI-X multi-message interrupt mode
10125          */
10126         if (phba->intr_type == MSIX) {
10127                 /* Check device state for handling interrupt */
10128                 if (lpfc_intr_state_check(phba))
10129                         return IRQ_NONE;
10130                 /* Need to read HA REG for slow-path events */
10131                 spin_lock_irqsave(&phba->hbalock, iflag);
10132                 if (lpfc_readl(phba->HAregaddr, &ha_copy))
10133                         goto unplug_error;
10134                 /* If somebody is waiting to handle an eratt don't process it
10135                  * here. The brdkill function will do this.
10136                  */
10137                 if (phba->link_flag & LS_IGNORE_ERATT)
10138                         ha_copy &= ~HA_ERATT;
10139                 /* Check the need for handling ERATT in interrupt handler */
10140                 if (ha_copy & HA_ERATT) {
10141                         if (phba->hba_flag & HBA_ERATT_HANDLED)
10142                                 /* ERATT polling has handled ERATT */
10143                                 ha_copy &= ~HA_ERATT;
10144                         else
10145                                 /* Indicate interrupt handler handles ERATT */
10146                                 phba->hba_flag |= HBA_ERATT_HANDLED;
10147                 }
10148
10149                 /*
10150                  * If there is deferred error attention, do not check for any
10151                  * interrupt.
10152                  */
10153                 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
10154                         spin_unlock_irqrestore(&phba->hbalock, iflag);
10155                         return IRQ_NONE;
10156                 }
10157
10158                 /* Clear up only attention source related to slow-path */
10159                 if (lpfc_readl(phba->HCregaddr, &hc_copy))
10160                         goto unplug_error;
10161
10162                 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
10163                         HC_LAINT_ENA | HC_ERINT_ENA),
10164                         phba->HCregaddr);
10165                 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
10166                         phba->HAregaddr);
10167                 writel(hc_copy, phba->HCregaddr);
10168                 readl(phba->HAregaddr); /* flush */
10169                 spin_unlock_irqrestore(&phba->hbalock, iflag);
10170         } else
10171                 ha_copy = phba->ha_copy;
10172
10173         work_ha_copy = ha_copy & phba->work_ha_mask;
10174
10175         if (work_ha_copy) {
10176                 if (work_ha_copy & HA_LATT) {
10177                         if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
10178                                 /*
10179                                  * Turn off Link Attention interrupts
10180                                  * until CLEAR_LA done
10181                                  */
10182                                 spin_lock_irqsave(&phba->hbalock, iflag);
10183                                 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
10184                                 if (lpfc_readl(phba->HCregaddr, &control))
10185                                         goto unplug_error;
10186                                 control &= ~HC_LAINT_ENA;
10187                                 writel(control, phba->HCregaddr);
10188                                 readl(phba->HCregaddr); /* flush */
10189                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
10190                         }
10191                         else
10192                                 work_ha_copy &= ~HA_LATT;
10193                 }
10194
10195                 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
10196                         /*
10197                          * Turn off Slow Rings interrupts, LPFC_ELS_RING is
10198                          * the only slow ring.
10199                          */
10200                         status = (work_ha_copy &
10201                                 (HA_RXMASK  << (4*LPFC_ELS_RING)));
10202                         status >>= (4*LPFC_ELS_RING);
10203                         if (status & HA_RXMASK) {
10204                                 spin_lock_irqsave(&phba->hbalock, iflag);
10205                                 if (lpfc_readl(phba->HCregaddr, &control))
10206                                         goto unplug_error;
10207
10208                                 lpfc_debugfs_slow_ring_trc(phba,
10209                                 "ISR slow ring:   ctl:x%x stat:x%x isrcnt:x%x",
10210                                 control, status,
10211                                 (uint32_t)phba->sli.slistat.sli_intr);
10212
10213                                 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
10214                                         lpfc_debugfs_slow_ring_trc(phba,
10215                                                 "ISR Disable ring:"
10216                                                 "pwork:x%x hawork:x%x wait:x%x",
10217                                                 phba->work_ha, work_ha_copy,
10218                                                 (uint32_t)((unsigned long)
10219                                                 &phba->work_waitq));
10220
10221                                         control &=
10222                                             ~(HC_R0INT_ENA << LPFC_ELS_RING);
10223                                         writel(control, phba->HCregaddr);
10224                                         readl(phba->HCregaddr); /* flush */
10225                                 }
10226                                 else {
10227                                         lpfc_debugfs_slow_ring_trc(phba,
10228                                                 "ISR slow ring:   pwork:"
10229                                                 "x%x hawork:x%x wait:x%x",
10230                                                 phba->work_ha, work_ha_copy,
10231                                                 (uint32_t)((unsigned long)
10232                                                 &phba->work_waitq));
10233                                 }
10234                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
10235                         }
10236                 }
10237                 spin_lock_irqsave(&phba->hbalock, iflag);
10238                 if (work_ha_copy & HA_ERATT) {
10239                         if (lpfc_sli_read_hs(phba))
10240                                 goto unplug_error;
10241                         /*
10242                          * Check if there is a deferred error condition
10243                          * is active
10244                          */
10245                         if ((HS_FFER1 & phba->work_hs) &&
10246                                 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
10247                                   HS_FFER6 | HS_FFER7 | HS_FFER8) &
10248                                   phba->work_hs)) {
10249                                 phba->hba_flag |= DEFER_ERATT;
10250                                 /* Clear all interrupt enable conditions */
10251                                 writel(0, phba->HCregaddr);
10252                                 readl(phba->HCregaddr);
10253                         }
10254                 }
10255
10256                 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
10257                         pmb = phba->sli.mbox_active;
10258                         pmbox = &pmb->u.mb;
10259                         mbox = phba->mbox;
10260                         vport = pmb->vport;
10261
10262                         /* First check out the status word */
10263                         lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
10264                         if (pmbox->mbxOwner != OWN_HOST) {
10265                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
10266                                 /*
10267                                  * Stray Mailbox Interrupt, mbxCommand <cmd>
10268                                  * mbxStatus <status>
10269                                  */
10270                                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
10271                                                 LOG_SLI,
10272                                                 "(%d):0304 Stray Mailbox "
10273                                                 "Interrupt mbxCommand x%x "
10274                                                 "mbxStatus x%x\n",
10275                                                 (vport ? vport->vpi : 0),
10276                                                 pmbox->mbxCommand,
10277                                                 pmbox->mbxStatus);
10278                                 /* clear mailbox attention bit */
10279                                 work_ha_copy &= ~HA_MBATT;
10280                         } else {
10281                                 phba->sli.mbox_active = NULL;
10282                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
10283                                 phba->last_completion_time = jiffies;
10284                                 del_timer(&phba->sli.mbox_tmo);
10285                                 if (pmb->mbox_cmpl) {
10286                                         lpfc_sli_pcimem_bcopy(mbox, pmbox,
10287                                                         MAILBOX_CMD_SIZE);
10288                                         if (pmb->out_ext_byte_len &&
10289                                                 pmb->context2)
10290                                                 lpfc_sli_pcimem_bcopy(
10291                                                 phba->mbox_ext,
10292                                                 pmb->context2,
10293                                                 pmb->out_ext_byte_len);
10294                                 }
10295                                 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
10296                                         pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
10297
10298                                         lpfc_debugfs_disc_trc(vport,
10299                                                 LPFC_DISC_TRC_MBOX_VPORT,
10300                                                 "MBOX dflt rpi: : "
10301                                                 "status:x%x rpi:x%x",
10302                                                 (uint32_t)pmbox->mbxStatus,
10303                                                 pmbox->un.varWords[0], 0);
10304
10305                                         if (!pmbox->mbxStatus) {
10306                                                 mp = (struct lpfc_dmabuf *)
10307                                                         (pmb->context1);
10308                                                 ndlp = (struct lpfc_nodelist *)
10309                                                         pmb->context2;
10310
10311                                                 /* Reg_LOGIN of dflt RPI was
10312                                                  * successful. new lets get
10313                                                  * rid of the RPI using the
10314                                                  * same mbox buffer.
10315                                                  */
10316                                                 lpfc_unreg_login(phba,
10317                                                         vport->vpi,
10318                                                         pmbox->un.varWords[0],
10319                                                         pmb);
10320                                                 pmb->mbox_cmpl =
10321                                                         lpfc_mbx_cmpl_dflt_rpi;
10322                                                 pmb->context1 = mp;
10323                                                 pmb->context2 = ndlp;
10324                                                 pmb->vport = vport;
10325                                                 rc = lpfc_sli_issue_mbox(phba,
10326                                                                 pmb,
10327                                                                 MBX_NOWAIT);
10328                                                 if (rc != MBX_BUSY)
10329                                                         lpfc_printf_log(phba,
10330                                                         KERN_ERR,
10331                                                         LOG_MBOX | LOG_SLI,
10332                                                         "0350 rc should have"
10333                                                         "been MBX_BUSY\n");
10334                                                 if (rc != MBX_NOT_FINISHED)
10335                                                         goto send_current_mbox;
10336                                         }
10337                                 }
10338                                 spin_lock_irqsave(
10339                                                 &phba->pport->work_port_lock,
10340                                                 iflag);
10341                                 phba->pport->work_port_events &=
10342                                         ~WORKER_MBOX_TMO;
10343                                 spin_unlock_irqrestore(
10344                                                 &phba->pport->work_port_lock,
10345                                                 iflag);
10346                                 lpfc_mbox_cmpl_put(phba, pmb);
10347                         }
10348                 } else
10349                         spin_unlock_irqrestore(&phba->hbalock, iflag);
10350
10351                 if ((work_ha_copy & HA_MBATT) &&
10352                     (phba->sli.mbox_active == NULL)) {
10353 send_current_mbox:
10354                         /* Process next mailbox command if there is one */
10355                         do {
10356                                 rc = lpfc_sli_issue_mbox(phba, NULL,
10357                                                          MBX_NOWAIT);
10358                         } while (rc == MBX_NOT_FINISHED);
10359                         if (rc != MBX_SUCCESS)
10360                                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
10361                                                 LOG_SLI, "0349 rc should be "
10362                                                 "MBX_SUCCESS\n");
10363                 }
10364
10365                 spin_lock_irqsave(&phba->hbalock, iflag);
10366                 phba->work_ha |= work_ha_copy;
10367                 spin_unlock_irqrestore(&phba->hbalock, iflag);
10368                 lpfc_worker_wake_up(phba);
10369         }
10370         return IRQ_HANDLED;
10371 unplug_error:
10372         spin_unlock_irqrestore(&phba->hbalock, iflag);
10373         return IRQ_HANDLED;
10374
10375 } /* lpfc_sli_sp_intr_handler */
10376
10377 /**
10378  * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
10379  * @irq: Interrupt number.
10380  * @dev_id: The device context pointer.
10381  *
10382  * This function is directly called from the PCI layer as an interrupt
10383  * service routine when device with SLI-3 interface spec is enabled with
10384  * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
10385  * ring event in the HBA. However, when the device is enabled with either
10386  * MSI or Pin-IRQ interrupt mode, this function is called as part of the
10387  * device-level interrupt handler. When the PCI slot is in error recovery
10388  * or the HBA is undergoing initialization, the interrupt handler will not
10389  * process the interrupt. The SCSI FCP fast-path ring event are handled in
10390  * the intrrupt context. This function is called without any lock held.
10391  * It gets the hbalock to access and update SLI data structures.
10392  *
10393  * This function returns IRQ_HANDLED when interrupt is handled else it
10394  * returns IRQ_NONE.
10395  **/
10396 irqreturn_t
10397 lpfc_sli_fp_intr_handler(int irq, void *dev_id)
10398 {
10399         struct lpfc_hba  *phba;
10400         uint32_t ha_copy;
10401         unsigned long status;
10402         unsigned long iflag;
10403
10404         /* Get the driver's phba structure from the dev_id and
10405          * assume the HBA is not interrupting.
10406          */
10407         phba = (struct lpfc_hba *) dev_id;
10408
10409         if (unlikely(!phba))
10410                 return IRQ_NONE;
10411
10412         /*
10413          * Stuff needs to be attented to when this function is invoked as an
10414          * individual interrupt handler in MSI-X multi-message interrupt mode
10415          */
10416         if (phba->intr_type == MSIX) {
10417                 /* Check device state for handling interrupt */
10418                 if (lpfc_intr_state_check(phba))
10419                         return IRQ_NONE;
10420                 /* Need to read HA REG for FCP ring and other ring events */
10421                 if (lpfc_readl(phba->HAregaddr, &ha_copy))
10422                         return IRQ_HANDLED;
10423                 /* Clear up only attention source related to fast-path */
10424                 spin_lock_irqsave(&phba->hbalock, iflag);
10425                 /*
10426                  * If there is deferred error attention, do not check for
10427                  * any interrupt.
10428                  */
10429                 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
10430                         spin_unlock_irqrestore(&phba->hbalock, iflag);
10431                         return IRQ_NONE;
10432                 }
10433                 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
10434                         phba->HAregaddr);
10435                 readl(phba->HAregaddr); /* flush */
10436                 spin_unlock_irqrestore(&phba->hbalock, iflag);
10437         } else
10438                 ha_copy = phba->ha_copy;
10439
10440         /*
10441          * Process all events on FCP ring. Take the optimized path for FCP IO.
10442          */
10443         ha_copy &= ~(phba->work_ha_mask);
10444
10445         status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
10446         status >>= (4*LPFC_FCP_RING);
10447         if (status & HA_RXMASK)
10448                 lpfc_sli_handle_fast_ring_event(phba,
10449                                                 &phba->sli.ring[LPFC_FCP_RING],
10450                                                 status);
10451
10452         if (phba->cfg_multi_ring_support == 2) {
10453                 /*
10454                  * Process all events on extra ring. Take the optimized path
10455                  * for extra ring IO.
10456                  */
10457                 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
10458                 status >>= (4*LPFC_EXTRA_RING);
10459                 if (status & HA_RXMASK) {
10460                         lpfc_sli_handle_fast_ring_event(phba,
10461                                         &phba->sli.ring[LPFC_EXTRA_RING],
10462                                         status);
10463                 }
10464         }
10465         return IRQ_HANDLED;
10466 }  /* lpfc_sli_fp_intr_handler */
10467
10468 /**
10469  * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
10470  * @irq: Interrupt number.
10471  * @dev_id: The device context pointer.
10472  *
10473  * This function is the HBA device-level interrupt handler to device with
10474  * SLI-3 interface spec, called from the PCI layer when either MSI or
10475  * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
10476  * requires driver attention. This function invokes the slow-path interrupt
10477  * attention handling function and fast-path interrupt attention handling
10478  * function in turn to process the relevant HBA attention events. This
10479  * function is called without any lock held. It gets the hbalock to access
10480  * and update SLI data structures.
10481  *
10482  * This function returns IRQ_HANDLED when interrupt is handled, else it
10483  * returns IRQ_NONE.
10484  **/
10485 irqreturn_t
10486 lpfc_sli_intr_handler(int irq, void *dev_id)
10487 {
10488         struct lpfc_hba  *phba;
10489         irqreturn_t sp_irq_rc, fp_irq_rc;
10490         unsigned long status1, status2;
10491         uint32_t hc_copy;
10492
10493         /*
10494          * Get the driver's phba structure from the dev_id and
10495          * assume the HBA is not interrupting.
10496          */
10497         phba = (struct lpfc_hba *) dev_id;
10498
10499         if (unlikely(!phba))
10500                 return IRQ_NONE;
10501
10502         /* Check device state for handling interrupt */
10503         if (lpfc_intr_state_check(phba))
10504                 return IRQ_NONE;
10505
10506         spin_lock(&phba->hbalock);
10507         if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
10508                 spin_unlock(&phba->hbalock);
10509                 return IRQ_HANDLED;
10510         }
10511
10512         if (unlikely(!phba->ha_copy)) {
10513                 spin_unlock(&phba->hbalock);
10514                 return IRQ_NONE;
10515         } else if (phba->ha_copy & HA_ERATT) {
10516                 if (phba->hba_flag & HBA_ERATT_HANDLED)
10517                         /* ERATT polling has handled ERATT */
10518                         phba->ha_copy &= ~HA_ERATT;
10519                 else
10520                         /* Indicate interrupt handler handles ERATT */
10521                         phba->hba_flag |= HBA_ERATT_HANDLED;
10522         }
10523
10524         /*
10525          * If there is deferred error attention, do not check for any interrupt.
10526          */
10527         if (unlikely(phba->hba_flag & DEFER_ERATT)) {
10528                 spin_unlock(&phba->hbalock);
10529                 return IRQ_NONE;
10530         }
10531
10532         /* Clear attention sources except link and error attentions */
10533         if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
10534                 spin_unlock(&phba->hbalock);
10535                 return IRQ_HANDLED;
10536         }
10537         writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
10538                 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
10539                 phba->HCregaddr);
10540         writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
10541         writel(hc_copy, phba->HCregaddr);
10542         readl(phba->HAregaddr); /* flush */
10543         spin_unlock(&phba->hbalock);
10544
10545         /*
10546          * Invokes slow-path host attention interrupt handling as appropriate.
10547          */
10548
10549         /* status of events with mailbox and link attention */
10550         status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
10551
10552         /* status of events with ELS ring */
10553         status2 = (phba->ha_copy & (HA_RXMASK  << (4*LPFC_ELS_RING)));
10554         status2 >>= (4*LPFC_ELS_RING);
10555
10556         if (status1 || (status2 & HA_RXMASK))
10557                 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
10558         else
10559                 sp_irq_rc = IRQ_NONE;
10560
10561         /*
10562          * Invoke fast-path host attention interrupt handling as appropriate.
10563          */
10564
10565         /* status of events with FCP ring */
10566         status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
10567         status1 >>= (4*LPFC_FCP_RING);
10568
10569         /* status of events with extra ring */
10570         if (phba->cfg_multi_ring_support == 2) {
10571                 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
10572                 status2 >>= (4*LPFC_EXTRA_RING);
10573         } else
10574                 status2 = 0;
10575
10576         if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
10577                 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
10578         else
10579                 fp_irq_rc = IRQ_NONE;
10580
10581         /* Return device-level interrupt handling status */
10582         return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
10583 }  /* lpfc_sli_intr_handler */
10584
10585 /**
10586  * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
10587  * @phba: pointer to lpfc hba data structure.
10588  *
10589  * This routine is invoked by the worker thread to process all the pending
10590  * SLI4 FCP abort XRI events.
10591  **/
10592 void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
10593 {
10594         struct lpfc_cq_event *cq_event;
10595
10596         /* First, declare the fcp xri abort event has been handled */
10597         spin_lock_irq(&phba->hbalock);
10598         phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
10599         spin_unlock_irq(&phba->hbalock);
10600         /* Now, handle all the fcp xri abort events */
10601         while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
10602                 /* Get the first event from the head of the event queue */
10603                 spin_lock_irq(&phba->hbalock);
10604                 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
10605                                  cq_event, struct lpfc_cq_event, list);
10606                 spin_unlock_irq(&phba->hbalock);
10607                 /* Notify aborted XRI for FCP work queue */
10608                 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
10609                 /* Free the event processed back to the free pool */
10610                 lpfc_sli4_cq_event_release(phba, cq_event);
10611         }
10612 }
10613
10614 /**
10615  * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
10616  * @phba: pointer to lpfc hba data structure.
10617  *
10618  * This routine is invoked by the worker thread to process all the pending
10619  * SLI4 els abort xri events.
10620  **/
10621 void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
10622 {
10623         struct lpfc_cq_event *cq_event;
10624
10625         /* First, declare the els xri abort event has been handled */
10626         spin_lock_irq(&phba->hbalock);
10627         phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
10628         spin_unlock_irq(&phba->hbalock);
10629         /* Now, handle all the els xri abort events */
10630         while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
10631                 /* Get the first event from the head of the event queue */
10632                 spin_lock_irq(&phba->hbalock);
10633                 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
10634                                  cq_event, struct lpfc_cq_event, list);
10635                 spin_unlock_irq(&phba->hbalock);
10636                 /* Notify aborted XRI for ELS work queue */
10637                 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
10638                 /* Free the event processed back to the free pool */
10639                 lpfc_sli4_cq_event_release(phba, cq_event);
10640         }
10641 }
10642
10643 /**
10644  * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
10645  * @phba: pointer to lpfc hba data structure
10646  * @pIocbIn: pointer to the rspiocbq
10647  * @pIocbOut: pointer to the cmdiocbq
10648  * @wcqe: pointer to the complete wcqe
10649  *
10650  * This routine transfers the fields of a command iocbq to a response iocbq
10651  * by copying all the IOCB fields from command iocbq and transferring the
10652  * completion status information from the complete wcqe.
10653  **/
10654 static void
10655 lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
10656                               struct lpfc_iocbq *pIocbIn,
10657                               struct lpfc_iocbq *pIocbOut,
10658                               struct lpfc_wcqe_complete *wcqe)
10659 {
10660         unsigned long iflags;
10661         uint32_t status;
10662         size_t offset = offsetof(struct lpfc_iocbq, iocb);
10663
10664         memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
10665                sizeof(struct lpfc_iocbq) - offset);
10666         /* Map WCQE parameters into irspiocb parameters */
10667         status = bf_get(lpfc_wcqe_c_status, wcqe);
10668         pIocbIn->iocb.ulpStatus = (status & LPFC_IOCB_STATUS_MASK);
10669         if (pIocbOut->iocb_flag & LPFC_IO_FCP)
10670                 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
10671                         pIocbIn->iocb.un.fcpi.fcpi_parm =
10672                                         pIocbOut->iocb.un.fcpi.fcpi_parm -
10673                                         wcqe->total_data_placed;
10674                 else
10675                         pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
10676         else {
10677                 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
10678                 pIocbIn->iocb.un.genreq64.bdl.bdeSize = wcqe->total_data_placed;
10679         }
10680
10681         /* Convert BG errors for completion status */
10682         if (status == CQE_STATUS_DI_ERROR) {
10683                 pIocbIn->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
10684
10685                 if (bf_get(lpfc_wcqe_c_bg_edir, wcqe))
10686                         pIocbIn->iocb.un.ulpWord[4] = IOERR_RX_DMA_FAILED;
10687                 else
10688                         pIocbIn->iocb.un.ulpWord[4] = IOERR_TX_DMA_FAILED;
10689
10690                 pIocbIn->iocb.unsli3.sli3_bg.bgstat = 0;
10691                 if (bf_get(lpfc_wcqe_c_bg_ge, wcqe)) /* Guard Check failed */
10692                         pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
10693                                 BGS_GUARD_ERR_MASK;
10694                 if (bf_get(lpfc_wcqe_c_bg_ae, wcqe)) /* App Tag Check failed */
10695                         pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
10696                                 BGS_APPTAG_ERR_MASK;
10697                 if (bf_get(lpfc_wcqe_c_bg_re, wcqe)) /* Ref Tag Check failed */
10698                         pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
10699                                 BGS_REFTAG_ERR_MASK;
10700
10701                 /* Check to see if there was any good data before the error */
10702                 if (bf_get(lpfc_wcqe_c_bg_tdpv, wcqe)) {
10703                         pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
10704                                 BGS_HI_WATER_MARK_PRESENT_MASK;
10705                         pIocbIn->iocb.unsli3.sli3_bg.bghm =
10706                                 wcqe->total_data_placed;
10707                 }
10708
10709                 /*
10710                 * Set ALL the error bits to indicate we don't know what
10711                 * type of error it is.
10712                 */
10713                 if (!pIocbIn->iocb.unsli3.sli3_bg.bgstat)
10714                         pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
10715                                 (BGS_REFTAG_ERR_MASK | BGS_APPTAG_ERR_MASK |
10716                                 BGS_GUARD_ERR_MASK);
10717         }
10718
10719         /* Pick up HBA exchange busy condition */
10720         if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
10721                 spin_lock_irqsave(&phba->hbalock, iflags);
10722                 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
10723                 spin_unlock_irqrestore(&phba->hbalock, iflags);
10724         }
10725 }
10726
10727 /**
10728  * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
10729  * @phba: Pointer to HBA context object.
10730  * @wcqe: Pointer to work-queue completion queue entry.
10731  *
10732  * This routine handles an ELS work-queue completion event and construct
10733  * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
10734  * discovery engine to handle.
10735  *
10736  * Return: Pointer to the receive IOCBQ, NULL otherwise.
10737  **/
10738 static struct lpfc_iocbq *
10739 lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
10740                                struct lpfc_iocbq *irspiocbq)
10741 {
10742         struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
10743         struct lpfc_iocbq *cmdiocbq;
10744         struct lpfc_wcqe_complete *wcqe;
10745         unsigned long iflags;
10746
10747         wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
10748         spin_lock_irqsave(&phba->hbalock, iflags);
10749         pring->stats.iocb_event++;
10750         /* Look up the ELS command IOCB and create pseudo response IOCB */
10751         cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
10752                                 bf_get(lpfc_wcqe_c_request_tag, wcqe));
10753         spin_unlock_irqrestore(&phba->hbalock, iflags);
10754
10755         if (unlikely(!cmdiocbq)) {
10756                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
10757                                 "0386 ELS complete with no corresponding "
10758                                 "cmdiocb: iotag (%d)\n",
10759                                 bf_get(lpfc_wcqe_c_request_tag, wcqe));
10760                 lpfc_sli_release_iocbq(phba, irspiocbq);
10761                 return NULL;
10762         }
10763
10764         /* Fake the irspiocbq and copy necessary response information */
10765         lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
10766
10767         return irspiocbq;
10768 }
10769
10770 /**
10771  * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
10772  * @phba: Pointer to HBA context object.
10773  * @cqe: Pointer to mailbox completion queue entry.
10774  *
10775  * This routine process a mailbox completion queue entry with asynchrous
10776  * event.
10777  *
10778  * Return: true if work posted to worker thread, otherwise false.
10779  **/
10780 static bool
10781 lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
10782 {
10783         struct lpfc_cq_event *cq_event;
10784         unsigned long iflags;
10785
10786         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
10787                         "0392 Async Event: word0:x%x, word1:x%x, "
10788                         "word2:x%x, word3:x%x\n", mcqe->word0,
10789                         mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
10790
10791         /* Allocate a new internal CQ_EVENT entry */
10792         cq_event = lpfc_sli4_cq_event_alloc(phba);
10793         if (!cq_event) {
10794                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10795                                 "0394 Failed to allocate CQ_EVENT entry\n");
10796                 return false;
10797         }
10798
10799         /* Move the CQE into an asynchronous event entry */
10800         memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
10801         spin_lock_irqsave(&phba->hbalock, iflags);
10802         list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
10803         /* Set the async event flag */
10804         phba->hba_flag |= ASYNC_EVENT;
10805         spin_unlock_irqrestore(&phba->hbalock, iflags);
10806
10807         return true;
10808 }
10809
10810 /**
10811  * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
10812  * @phba: Pointer to HBA context object.
10813  * @cqe: Pointer to mailbox completion queue entry.
10814  *
10815  * This routine process a mailbox completion queue entry with mailbox
10816  * completion event.
10817  *
10818  * Return: true if work posted to worker thread, otherwise false.
10819  **/
10820 static bool
10821 lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
10822 {
10823         uint32_t mcqe_status;
10824         MAILBOX_t *mbox, *pmbox;
10825         struct lpfc_mqe *mqe;
10826         struct lpfc_vport *vport;
10827         struct lpfc_nodelist *ndlp;
10828         struct lpfc_dmabuf *mp;
10829         unsigned long iflags;
10830         LPFC_MBOXQ_t *pmb;
10831         bool workposted = false;
10832         int rc;
10833
10834         /* If not a mailbox complete MCQE, out by checking mailbox consume */
10835         if (!bf_get(lpfc_trailer_completed, mcqe))
10836                 goto out_no_mqe_complete;
10837
10838         /* Get the reference to the active mbox command */
10839         spin_lock_irqsave(&phba->hbalock, iflags);
10840         pmb = phba->sli.mbox_active;
10841         if (unlikely(!pmb)) {
10842                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
10843                                 "1832 No pending MBOX command to handle\n");
10844                 spin_unlock_irqrestore(&phba->hbalock, iflags);
10845                 goto out_no_mqe_complete;
10846         }
10847         spin_unlock_irqrestore(&phba->hbalock, iflags);
10848         mqe = &pmb->u.mqe;
10849         pmbox = (MAILBOX_t *)&pmb->u.mqe;
10850         mbox = phba->mbox;
10851         vport = pmb->vport;
10852
10853         /* Reset heartbeat timer */
10854         phba->last_completion_time = jiffies;
10855         del_timer(&phba->sli.mbox_tmo);
10856
10857         /* Move mbox data to caller's mailbox region, do endian swapping */
10858         if (pmb->mbox_cmpl && mbox)
10859                 lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
10860
10861         /*
10862          * For mcqe errors, conditionally move a modified error code to
10863          * the mbox so that the error will not be missed.
10864          */
10865         mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
10866         if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
10867                 if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
10868                         bf_set(lpfc_mqe_status, mqe,
10869                                (LPFC_MBX_ERROR_RANGE | mcqe_status));
10870         }
10871         if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
10872                 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
10873                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
10874                                       "MBOX dflt rpi: status:x%x rpi:x%x",
10875                                       mcqe_status,
10876                                       pmbox->un.varWords[0], 0);
10877                 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
10878                         mp = (struct lpfc_dmabuf *)(pmb->context1);
10879                         ndlp = (struct lpfc_nodelist *)pmb->context2;
10880                         /* Reg_LOGIN of dflt RPI was successful. Now lets get
10881                          * RID of the PPI using the same mbox buffer.
10882                          */
10883                         lpfc_unreg_login(phba, vport->vpi,
10884                                          pmbox->un.varWords[0], pmb);
10885                         pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
10886                         pmb->context1 = mp;
10887                         pmb->context2 = ndlp;
10888                         pmb->vport = vport;
10889                         rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
10890                         if (rc != MBX_BUSY)
10891                                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
10892                                                 LOG_SLI, "0385 rc should "
10893                                                 "have been MBX_BUSY\n");
10894                         if (rc != MBX_NOT_FINISHED)
10895                                 goto send_current_mbox;
10896                 }
10897         }
10898         spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
10899         phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
10900         spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
10901
10902         /* There is mailbox completion work to do */
10903         spin_lock_irqsave(&phba->hbalock, iflags);
10904         __lpfc_mbox_cmpl_put(phba, pmb);
10905         phba->work_ha |= HA_MBATT;
10906         spin_unlock_irqrestore(&phba->hbalock, iflags);
10907         workposted = true;
10908
10909 send_current_mbox:
10910         spin_lock_irqsave(&phba->hbalock, iflags);
10911         /* Release the mailbox command posting token */
10912         phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
10913         /* Setting active mailbox pointer need to be in sync to flag clear */
10914         phba->sli.mbox_active = NULL;
10915         spin_unlock_irqrestore(&phba->hbalock, iflags);
10916         /* Wake up worker thread to post the next pending mailbox command */
10917         lpfc_worker_wake_up(phba);
10918 out_no_mqe_complete:
10919         if (bf_get(lpfc_trailer_consumed, mcqe))
10920                 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
10921         return workposted;
10922 }
10923
10924 /**
10925  * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
10926  * @phba: Pointer to HBA context object.
10927  * @cqe: Pointer to mailbox completion queue entry.
10928  *
10929  * This routine process a mailbox completion queue entry, it invokes the
10930  * proper mailbox complete handling or asynchrous event handling routine
10931  * according to the MCQE's async bit.
10932  *
10933  * Return: true if work posted to worker thread, otherwise false.
10934  **/
10935 static bool
10936 lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
10937 {
10938         struct lpfc_mcqe mcqe;
10939         bool workposted;
10940
10941         /* Copy the mailbox MCQE and convert endian order as needed */
10942         lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
10943
10944         /* Invoke the proper event handling routine */
10945         if (!bf_get(lpfc_trailer_async, &mcqe))
10946                 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
10947         else
10948                 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
10949         return workposted;
10950 }
10951
10952 /**
10953  * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
10954  * @phba: Pointer to HBA context object.
10955  * @wcqe: Pointer to work-queue completion queue entry.
10956  *
10957  * This routine handles an ELS work-queue completion event.
10958  *
10959  * Return: true if work posted to worker thread, otherwise false.
10960  **/
10961 static bool
10962 lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba,
10963                              struct lpfc_wcqe_complete *wcqe)
10964 {
10965         struct lpfc_iocbq *irspiocbq;
10966         unsigned long iflags;
10967         struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
10968
10969         /* Get an irspiocbq for later ELS response processing use */
10970         irspiocbq = lpfc_sli_get_iocbq(phba);
10971         if (!irspiocbq) {
10972                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10973                         "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
10974                         "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
10975                         pring->txq_cnt, phba->iocb_cnt,
10976                         phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt,
10977                         phba->sli.ring[LPFC_ELS_RING].txcmplq_cnt);
10978                 return false;
10979         }
10980
10981         /* Save off the slow-path queue event for work thread to process */
10982         memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
10983         spin_lock_irqsave(&phba->hbalock, iflags);
10984         list_add_tail(&irspiocbq->cq_event.list,
10985                       &phba->sli4_hba.sp_queue_event);
10986         phba->hba_flag |= HBA_SP_QUEUE_EVT;
10987         spin_unlock_irqrestore(&phba->hbalock, iflags);
10988
10989         return true;
10990 }
10991
10992 /**
10993  * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
10994  * @phba: Pointer to HBA context object.
10995  * @wcqe: Pointer to work-queue completion queue entry.
10996  *
10997  * This routine handles slow-path WQ entry comsumed event by invoking the
10998  * proper WQ release routine to the slow-path WQ.
10999  **/
11000 static void
11001 lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
11002                              struct lpfc_wcqe_release *wcqe)
11003 {
11004         /* sanity check on queue memory */
11005         if (unlikely(!phba->sli4_hba.els_wq))
11006                 return;
11007         /* Check for the slow-path ELS work queue */
11008         if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
11009                 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
11010                                      bf_get(lpfc_wcqe_r_wqe_index, wcqe));
11011         else
11012                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11013                                 "2579 Slow-path wqe consume event carries "
11014                                 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
11015                                 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
11016                                 phba->sli4_hba.els_wq->queue_id);
11017 }
11018
11019 /**
11020  * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
11021  * @phba: Pointer to HBA context object.
11022  * @cq: Pointer to a WQ completion queue.
11023  * @wcqe: Pointer to work-queue completion queue entry.
11024  *
11025  * This routine handles an XRI abort event.
11026  *
11027  * Return: true if work posted to worker thread, otherwise false.
11028  **/
11029 static bool
11030 lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
11031                                    struct lpfc_queue *cq,
11032                                    struct sli4_wcqe_xri_aborted *wcqe)
11033 {
11034         bool workposted = false;
11035         struct lpfc_cq_event *cq_event;
11036         unsigned long iflags;
11037
11038         /* Allocate a new internal CQ_EVENT entry */
11039         cq_event = lpfc_sli4_cq_event_alloc(phba);
11040         if (!cq_event) {
11041                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11042                                 "0602 Failed to allocate CQ_EVENT entry\n");
11043                 return false;
11044         }
11045
11046         /* Move the CQE into the proper xri abort event list */
11047         memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
11048         switch (cq->subtype) {
11049         case LPFC_FCP:
11050                 spin_lock_irqsave(&phba->hbalock, iflags);
11051                 list_add_tail(&cq_event->list,
11052                               &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
11053                 /* Set the fcp xri abort event flag */
11054                 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
11055                 spin_unlock_irqrestore(&phba->hbalock, iflags);
11056                 workposted = true;
11057                 break;
11058         case LPFC_ELS:
11059                 spin_lock_irqsave(&phba->hbalock, iflags);
11060                 list_add_tail(&cq_event->list,
11061                               &phba->sli4_hba.sp_els_xri_aborted_work_queue);
11062                 /* Set the els xri abort event flag */
11063                 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
11064                 spin_unlock_irqrestore(&phba->hbalock, iflags);
11065                 workposted = true;
11066                 break;
11067         default:
11068                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11069                                 "0603 Invalid work queue CQE subtype (x%x)\n",
11070                                 cq->subtype);
11071                 workposted = false;
11072                 break;
11073         }
11074         return workposted;
11075 }
11076
11077 /**
11078  * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
11079  * @phba: Pointer to HBA context object.
11080  * @rcqe: Pointer to receive-queue completion queue entry.
11081  *
11082  * This routine process a receive-queue completion queue entry.
11083  *
11084  * Return: true if work posted to worker thread, otherwise false.
11085  **/
11086 static bool
11087 lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
11088 {
11089         bool workposted = false;
11090         struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
11091         struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
11092         struct hbq_dmabuf *dma_buf;
11093         uint32_t status, rq_id;
11094         unsigned long iflags;
11095
11096         /* sanity check on queue memory */
11097         if (unlikely(!hrq) || unlikely(!drq))
11098                 return workposted;
11099
11100         if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
11101                 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
11102         else
11103                 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
11104         if (rq_id != hrq->queue_id)
11105                 goto out;
11106
11107         status = bf_get(lpfc_rcqe_status, rcqe);
11108         switch (status) {
11109         case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
11110                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11111                                 "2537 Receive Frame Truncated!!\n");
11112         case FC_STATUS_RQ_SUCCESS:
11113                 lpfc_sli4_rq_release(hrq, drq);
11114                 spin_lock_irqsave(&phba->hbalock, iflags);
11115                 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
11116                 if (!dma_buf) {
11117                         spin_unlock_irqrestore(&phba->hbalock, iflags);
11118                         goto out;
11119                 }
11120                 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
11121                 /* save off the frame for the word thread to process */
11122                 list_add_tail(&dma_buf->cq_event.list,
11123                               &phba->sli4_hba.sp_queue_event);
11124                 /* Frame received */
11125                 phba->hba_flag |= HBA_SP_QUEUE_EVT;
11126                 spin_unlock_irqrestore(&phba->hbalock, iflags);
11127                 workposted = true;
11128                 break;
11129         case FC_STATUS_INSUFF_BUF_NEED_BUF:
11130         case FC_STATUS_INSUFF_BUF_FRM_DISC:
11131                 /* Post more buffers if possible */
11132                 spin_lock_irqsave(&phba->hbalock, iflags);
11133                 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
11134                 spin_unlock_irqrestore(&phba->hbalock, iflags);
11135                 workposted = true;
11136                 break;
11137         }
11138 out:
11139         return workposted;
11140 }
11141
11142 /**
11143  * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
11144  * @phba: Pointer to HBA context object.
11145  * @cq: Pointer to the completion queue.
11146  * @wcqe: Pointer to a completion queue entry.
11147  *
11148  * This routine process a slow-path work-queue or receive queue completion queue
11149  * entry.
11150  *
11151  * Return: true if work posted to worker thread, otherwise false.
11152  **/
11153 static bool
11154 lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
11155                          struct lpfc_cqe *cqe)
11156 {
11157         struct lpfc_cqe cqevt;
11158         bool workposted = false;
11159
11160         /* Copy the work queue CQE and convert endian order if needed */
11161         lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
11162
11163         /* Check and process for different type of WCQE and dispatch */
11164         switch (bf_get(lpfc_cqe_code, &cqevt)) {
11165         case CQE_CODE_COMPL_WQE:
11166                 /* Process the WQ/RQ complete event */
11167                 phba->last_completion_time = jiffies;
11168                 workposted = lpfc_sli4_sp_handle_els_wcqe(phba,
11169                                 (struct lpfc_wcqe_complete *)&cqevt);
11170                 break;
11171         case CQE_CODE_RELEASE_WQE:
11172                 /* Process the WQ release event */
11173                 lpfc_sli4_sp_handle_rel_wcqe(phba,
11174                                 (struct lpfc_wcqe_release *)&cqevt);
11175                 break;
11176         case CQE_CODE_XRI_ABORTED:
11177                 /* Process the WQ XRI abort event */
11178                 phba->last_completion_time = jiffies;
11179                 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
11180                                 (struct sli4_wcqe_xri_aborted *)&cqevt);
11181                 break;
11182         case CQE_CODE_RECEIVE:
11183         case CQE_CODE_RECEIVE_V1:
11184                 /* Process the RQ event */
11185                 phba->last_completion_time = jiffies;
11186                 workposted = lpfc_sli4_sp_handle_rcqe(phba,
11187                                 (struct lpfc_rcqe *)&cqevt);
11188                 break;
11189         default:
11190                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11191                                 "0388 Not a valid WCQE code: x%x\n",
11192                                 bf_get(lpfc_cqe_code, &cqevt));
11193                 break;
11194         }
11195         return workposted;
11196 }
11197
11198 /**
11199  * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
11200  * @phba: Pointer to HBA context object.
11201  * @eqe: Pointer to fast-path event queue entry.
11202  *
11203  * This routine process a event queue entry from the slow-path event queue.
11204  * It will check the MajorCode and MinorCode to determine this is for a
11205  * completion event on a completion queue, if not, an error shall be logged
11206  * and just return. Otherwise, it will get to the corresponding completion
11207  * queue and process all the entries on that completion queue, rearm the
11208  * completion queue, and then return.
11209  *
11210  **/
11211 static void
11212 lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
11213 {
11214         struct lpfc_queue *cq = NULL, *childq, *speq;
11215         struct lpfc_cqe *cqe;
11216         bool workposted = false;
11217         int ecount = 0;
11218         uint16_t cqid;
11219
11220         if (bf_get_le32(lpfc_eqe_major_code, eqe) != 0) {
11221                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11222                                 "0359 Not a valid slow-path completion "
11223                                 "event: majorcode=x%x, minorcode=x%x\n",
11224                                 bf_get_le32(lpfc_eqe_major_code, eqe),
11225                                 bf_get_le32(lpfc_eqe_minor_code, eqe));
11226                 return;
11227         }
11228
11229         /* Get the reference to the corresponding CQ */
11230         cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
11231
11232         /* Search for completion queue pointer matching this cqid */
11233         speq = phba->sli4_hba.sp_eq;
11234         /* sanity check on queue memory */
11235         if (unlikely(!speq))
11236                 return;
11237         list_for_each_entry(childq, &speq->child_list, list) {
11238                 if (childq->queue_id == cqid) {
11239                         cq = childq;
11240                         break;
11241                 }
11242         }
11243         if (unlikely(!cq)) {
11244                 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
11245                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11246                                         "0365 Slow-path CQ identifier "
11247                                         "(%d) does not exist\n", cqid);
11248                 return;
11249         }
11250
11251         /* Process all the entries to the CQ */
11252         switch (cq->type) {
11253         case LPFC_MCQ:
11254                 while ((cqe = lpfc_sli4_cq_get(cq))) {
11255                         workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
11256                         if (!(++ecount % cq->entry_repost))
11257                                 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
11258                 }
11259                 break;
11260         case LPFC_WCQ:
11261                 while ((cqe = lpfc_sli4_cq_get(cq))) {
11262                         if (cq->subtype == LPFC_FCP)
11263                                 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq,
11264                                                                        cqe);
11265                         else
11266                                 workposted |= lpfc_sli4_sp_handle_cqe(phba, cq,
11267                                                                       cqe);
11268                         if (!(++ecount % cq->entry_repost))
11269                                 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
11270                 }
11271                 break;
11272         default:
11273                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11274                                 "0370 Invalid completion queue type (%d)\n",
11275                                 cq->type);
11276                 return;
11277         }
11278
11279         /* Catch the no cq entry condition, log an error */
11280         if (unlikely(ecount == 0))
11281                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11282                                 "0371 No entry from the CQ: identifier "
11283                                 "(x%x), type (%d)\n", cq->queue_id, cq->type);
11284
11285         /* In any case, flash and re-arm the RCQ */
11286         lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
11287
11288         /* wake up worker thread if there are works to be done */
11289         if (workposted)
11290                 lpfc_worker_wake_up(phba);
11291 }
11292
11293 /**
11294  * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
11295  * @eqe: Pointer to fast-path completion queue entry.
11296  *
11297  * This routine process a fast-path work queue completion entry from fast-path
11298  * event queue for FCP command response completion.
11299  **/
11300 static void
11301 lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba,
11302                              struct lpfc_wcqe_complete *wcqe)
11303 {
11304         struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
11305         struct lpfc_iocbq *cmdiocbq;
11306         struct lpfc_iocbq irspiocbq;
11307         unsigned long iflags;
11308
11309         spin_lock_irqsave(&phba->hbalock, iflags);
11310         pring->stats.iocb_event++;
11311         spin_unlock_irqrestore(&phba->hbalock, iflags);
11312
11313         /* Check for response status */
11314         if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
11315                 /* If resource errors reported from HBA, reduce queue
11316                  * depth of the SCSI device.
11317                  */
11318                 if ((bf_get(lpfc_wcqe_c_status, wcqe) ==
11319                      IOSTAT_LOCAL_REJECT) &&
11320                     (wcqe->parameter == IOERR_NO_RESOURCES)) {
11321                         phba->lpfc_rampdown_queue_depth(phba);
11322                 }
11323                 /* Log the error status */
11324                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11325                                 "0373 FCP complete error: status=x%x, "
11326                                 "hw_status=x%x, total_data_specified=%d, "
11327                                 "parameter=x%x, word3=x%x\n",
11328                                 bf_get(lpfc_wcqe_c_status, wcqe),
11329                                 bf_get(lpfc_wcqe_c_hw_status, wcqe),
11330                                 wcqe->total_data_placed, wcqe->parameter,
11331                                 wcqe->word3);
11332         }
11333
11334         /* Look up the FCP command IOCB and create pseudo response IOCB */
11335         spin_lock_irqsave(&phba->hbalock, iflags);
11336         cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
11337                                 bf_get(lpfc_wcqe_c_request_tag, wcqe));
11338         spin_unlock_irqrestore(&phba->hbalock, iflags);
11339         if (unlikely(!cmdiocbq)) {
11340                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11341                                 "0374 FCP complete with no corresponding "
11342                                 "cmdiocb: iotag (%d)\n",
11343                                 bf_get(lpfc_wcqe_c_request_tag, wcqe));
11344                 return;
11345         }
11346         if (unlikely(!cmdiocbq->iocb_cmpl)) {
11347                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11348                                 "0375 FCP cmdiocb not callback function "
11349                                 "iotag: (%d)\n",
11350                                 bf_get(lpfc_wcqe_c_request_tag, wcqe));
11351                 return;
11352         }
11353
11354         /* Fake the irspiocb and copy necessary response information */
11355         lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
11356
11357         if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
11358                 spin_lock_irqsave(&phba->hbalock, iflags);
11359                 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
11360                 spin_unlock_irqrestore(&phba->hbalock, iflags);
11361         }
11362
11363         /* Pass the cmd_iocb and the rsp state to the upper layer */
11364         (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
11365 }
11366
11367 /**
11368  * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
11369  * @phba: Pointer to HBA context object.
11370  * @cq: Pointer to completion queue.
11371  * @wcqe: Pointer to work-queue completion queue entry.
11372  *
11373  * This routine handles an fast-path WQ entry comsumed event by invoking the
11374  * proper WQ release routine to the slow-path WQ.
11375  **/
11376 static void
11377 lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
11378                              struct lpfc_wcqe_release *wcqe)
11379 {
11380         struct lpfc_queue *childwq;
11381         bool wqid_matched = false;
11382         uint16_t fcp_wqid;
11383
11384         /* Check for fast-path FCP work queue release */
11385         fcp_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
11386         list_for_each_entry(childwq, &cq->child_list, list) {
11387                 if (childwq->queue_id == fcp_wqid) {
11388                         lpfc_sli4_wq_release(childwq,
11389                                         bf_get(lpfc_wcqe_r_wqe_index, wcqe));
11390                         wqid_matched = true;
11391                         break;
11392                 }
11393         }
11394         /* Report warning log message if no match found */
11395         if (wqid_matched != true)
11396                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11397                                 "2580 Fast-path wqe consume event carries "
11398                                 "miss-matched qid: wcqe-qid=x%x\n", fcp_wqid);
11399 }
11400
11401 /**
11402  * lpfc_sli4_fp_handle_wcqe - Process fast-path work queue completion entry
11403  * @cq: Pointer to the completion queue.
11404  * @eqe: Pointer to fast-path completion queue entry.
11405  *
11406  * This routine process a fast-path work queue completion entry from fast-path
11407  * event queue for FCP command response completion.
11408  **/
11409 static int
11410 lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
11411                          struct lpfc_cqe *cqe)
11412 {
11413         struct lpfc_wcqe_release wcqe;
11414         bool workposted = false;
11415
11416         /* Copy the work queue CQE and convert endian order if needed */
11417         lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
11418
11419         /* Check and process for different type of WCQE and dispatch */
11420         switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
11421         case CQE_CODE_COMPL_WQE:
11422                 /* Process the WQ complete event */
11423                 phba->last_completion_time = jiffies;
11424                 lpfc_sli4_fp_handle_fcp_wcqe(phba,
11425                                 (struct lpfc_wcqe_complete *)&wcqe);
11426                 break;
11427         case CQE_CODE_RELEASE_WQE:
11428                 /* Process the WQ release event */
11429                 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
11430                                 (struct lpfc_wcqe_release *)&wcqe);
11431                 break;
11432         case CQE_CODE_XRI_ABORTED:
11433                 /* Process the WQ XRI abort event */
11434                 phba->last_completion_time = jiffies;
11435                 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
11436                                 (struct sli4_wcqe_xri_aborted *)&wcqe);
11437                 break;
11438         default:
11439                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11440                                 "0144 Not a valid WCQE code: x%x\n",
11441                                 bf_get(lpfc_wcqe_c_code, &wcqe));
11442                 break;
11443         }
11444         return workposted;
11445 }
11446
11447 /**
11448  * lpfc_sli4_fp_handle_eqe - Process a fast-path event queue entry
11449  * @phba: Pointer to HBA context object.
11450  * @eqe: Pointer to fast-path event queue entry.
11451  *
11452  * This routine process a event queue entry from the fast-path event queue.
11453  * It will check the MajorCode and MinorCode to determine this is for a
11454  * completion event on a completion queue, if not, an error shall be logged
11455  * and just return. Otherwise, it will get to the corresponding completion
11456  * queue and process all the entries on the completion queue, rearm the
11457  * completion queue, and then return.
11458  **/
11459 static void
11460 lpfc_sli4_fp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
11461                         uint32_t fcp_cqidx)
11462 {
11463         struct lpfc_queue *cq;
11464         struct lpfc_cqe *cqe;
11465         bool workposted = false;
11466         uint16_t cqid;
11467         int ecount = 0;
11468
11469         if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
11470                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11471                                 "0366 Not a valid fast-path completion "
11472                                 "event: majorcode=x%x, minorcode=x%x\n",
11473                                 bf_get_le32(lpfc_eqe_major_code, eqe),
11474                                 bf_get_le32(lpfc_eqe_minor_code, eqe));
11475                 return;
11476         }
11477
11478         if (unlikely(!phba->sli4_hba.fcp_cq)) {
11479                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11480                                 "3146 Fast-path completion queues "
11481                                 "does not exist\n");
11482                 return;
11483         }
11484         cq = phba->sli4_hba.fcp_cq[fcp_cqidx];
11485         if (unlikely(!cq)) {
11486                 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
11487                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11488                                         "0367 Fast-path completion queue "
11489                                         "(%d) does not exist\n", fcp_cqidx);
11490                 return;
11491         }
11492
11493         /* Get the reference to the corresponding CQ */
11494         cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
11495         if (unlikely(cqid != cq->queue_id)) {
11496                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11497                                 "0368 Miss-matched fast-path completion "
11498                                 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
11499                                 cqid, cq->queue_id);
11500                 return;
11501         }
11502
11503         /* Process all the entries to the CQ */
11504         while ((cqe = lpfc_sli4_cq_get(cq))) {
11505                 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
11506                 if (!(++ecount % cq->entry_repost))
11507                         lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
11508         }
11509
11510         /* Catch the no cq entry condition */
11511         if (unlikely(ecount == 0))
11512                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11513                                 "0369 No entry from fast-path completion "
11514                                 "queue fcpcqid=%d\n", cq->queue_id);
11515
11516         /* In any case, flash and re-arm the CQ */
11517         lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
11518
11519         /* wake up worker thread if there are works to be done */
11520         if (workposted)
11521                 lpfc_worker_wake_up(phba);
11522 }
11523
11524 static void
11525 lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
11526 {
11527         struct lpfc_eqe *eqe;
11528
11529         /* walk all the EQ entries and drop on the floor */
11530         while ((eqe = lpfc_sli4_eq_get(eq)))
11531                 ;
11532
11533         /* Clear and re-arm the EQ */
11534         lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
11535 }
11536
11537 /**
11538  * lpfc_sli4_sp_intr_handler - Slow-path interrupt handler to SLI-4 device
11539  * @irq: Interrupt number.
11540  * @dev_id: The device context pointer.
11541  *
11542  * This function is directly called from the PCI layer as an interrupt
11543  * service routine when device with SLI-4 interface spec is enabled with
11544  * MSI-X multi-message interrupt mode and there are slow-path events in
11545  * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
11546  * interrupt mode, this function is called as part of the device-level
11547  * interrupt handler. When the PCI slot is in error recovery or the HBA is
11548  * undergoing initialization, the interrupt handler will not process the
11549  * interrupt. The link attention and ELS ring attention events are handled
11550  * by the worker thread. The interrupt handler signals the worker thread
11551  * and returns for these events. This function is called without any lock
11552  * held. It gets the hbalock to access and update SLI data structures.
11553  *
11554  * This function returns IRQ_HANDLED when interrupt is handled else it
11555  * returns IRQ_NONE.
11556  **/
11557 irqreturn_t
11558 lpfc_sli4_sp_intr_handler(int irq, void *dev_id)
11559 {
11560         struct lpfc_hba *phba;
11561         struct lpfc_queue *speq;
11562         struct lpfc_eqe *eqe;
11563         unsigned long iflag;
11564         int ecount = 0;
11565
11566         /*
11567          * Get the driver's phba structure from the dev_id
11568          */
11569         phba = (struct lpfc_hba *)dev_id;
11570
11571         if (unlikely(!phba))
11572                 return IRQ_NONE;
11573
11574         /* Get to the EQ struct associated with this vector */
11575         speq = phba->sli4_hba.sp_eq;
11576         if (unlikely(!speq))
11577                 return IRQ_NONE;
11578
11579         /* Check device state for handling interrupt */
11580         if (unlikely(lpfc_intr_state_check(phba))) {
11581                 /* Check again for link_state with lock held */
11582                 spin_lock_irqsave(&phba->hbalock, iflag);
11583                 if (phba->link_state < LPFC_LINK_DOWN)
11584                         /* Flush, clear interrupt, and rearm the EQ */
11585                         lpfc_sli4_eq_flush(phba, speq);
11586                 spin_unlock_irqrestore(&phba->hbalock, iflag);
11587                 return IRQ_NONE;
11588         }
11589
11590         /*
11591          * Process all the event on FCP slow-path EQ
11592          */
11593         while ((eqe = lpfc_sli4_eq_get(speq))) {
11594                 lpfc_sli4_sp_handle_eqe(phba, eqe);
11595                 if (!(++ecount % speq->entry_repost))
11596                         lpfc_sli4_eq_release(speq, LPFC_QUEUE_NOARM);
11597         }
11598
11599         /* Always clear and re-arm the slow-path EQ */
11600         lpfc_sli4_eq_release(speq, LPFC_QUEUE_REARM);
11601
11602         /* Catch the no cq entry condition */
11603         if (unlikely(ecount == 0)) {
11604                 if (phba->intr_type == MSIX)
11605                         /* MSI-X treated interrupt served as no EQ share INT */
11606                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11607                                         "0357 MSI-X interrupt with no EQE\n");
11608                 else
11609                         /* Non MSI-X treated on interrupt as EQ share INT */
11610                         return IRQ_NONE;
11611         }
11612
11613         return IRQ_HANDLED;
11614 } /* lpfc_sli4_sp_intr_handler */
11615
11616 /**
11617  * lpfc_sli4_fp_intr_handler - Fast-path interrupt handler to SLI-4 device
11618  * @irq: Interrupt number.
11619  * @dev_id: The device context pointer.
11620  *
11621  * This function is directly called from the PCI layer as an interrupt
11622  * service routine when device with SLI-4 interface spec is enabled with
11623  * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
11624  * ring event in the HBA. However, when the device is enabled with either
11625  * MSI or Pin-IRQ interrupt mode, this function is called as part of the
11626  * device-level interrupt handler. When the PCI slot is in error recovery
11627  * or the HBA is undergoing initialization, the interrupt handler will not
11628  * process the interrupt. The SCSI FCP fast-path ring event are handled in
11629  * the intrrupt context. This function is called without any lock held.
11630  * It gets the hbalock to access and update SLI data structures. Note that,
11631  * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
11632  * equal to that of FCP CQ index.
11633  *
11634  * This function returns IRQ_HANDLED when interrupt is handled else it
11635  * returns IRQ_NONE.
11636  **/
11637 irqreturn_t
11638 lpfc_sli4_fp_intr_handler(int irq, void *dev_id)
11639 {
11640         struct lpfc_hba *phba;
11641         struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
11642         struct lpfc_queue *fpeq;
11643         struct lpfc_eqe *eqe;
11644         unsigned long iflag;
11645         int ecount = 0;
11646         uint32_t fcp_eqidx;
11647
11648         /* Get the driver's phba structure from the dev_id */
11649         fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
11650         phba = fcp_eq_hdl->phba;
11651         fcp_eqidx = fcp_eq_hdl->idx;
11652
11653         if (unlikely(!phba))
11654                 return IRQ_NONE;
11655         if (unlikely(!phba->sli4_hba.fp_eq))
11656                 return IRQ_NONE;
11657
11658         /* Get to the EQ struct associated with this vector */
11659         fpeq = phba->sli4_hba.fp_eq[fcp_eqidx];
11660         if (unlikely(!fpeq))
11661                 return IRQ_NONE;
11662
11663         /* Check device state for handling interrupt */
11664         if (unlikely(lpfc_intr_state_check(phba))) {
11665                 /* Check again for link_state with lock held */
11666                 spin_lock_irqsave(&phba->hbalock, iflag);
11667                 if (phba->link_state < LPFC_LINK_DOWN)
11668                         /* Flush, clear interrupt, and rearm the EQ */
11669                         lpfc_sli4_eq_flush(phba, fpeq);
11670                 spin_unlock_irqrestore(&phba->hbalock, iflag);
11671                 return IRQ_NONE;
11672         }
11673
11674         /*
11675          * Process all the event on FCP fast-path EQ
11676          */
11677         while ((eqe = lpfc_sli4_eq_get(fpeq))) {
11678                 lpfc_sli4_fp_handle_eqe(phba, eqe, fcp_eqidx);
11679                 if (!(++ecount % fpeq->entry_repost))
11680                         lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
11681         }
11682
11683         /* Always clear and re-arm the fast-path EQ */
11684         lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
11685
11686         if (unlikely(ecount == 0)) {
11687                 if (phba->intr_type == MSIX)
11688                         /* MSI-X treated interrupt served as no EQ share INT */
11689                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11690                                         "0358 MSI-X interrupt with no EQE\n");
11691                 else
11692                         /* Non MSI-X treated on interrupt as EQ share INT */
11693                         return IRQ_NONE;
11694         }
11695
11696         return IRQ_HANDLED;
11697 } /* lpfc_sli4_fp_intr_handler */
11698
11699 /**
11700  * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
11701  * @irq: Interrupt number.
11702  * @dev_id: The device context pointer.
11703  *
11704  * This function is the device-level interrupt handler to device with SLI-4
11705  * interface spec, called from the PCI layer when either MSI or Pin-IRQ
11706  * interrupt mode is enabled and there is an event in the HBA which requires
11707  * driver attention. This function invokes the slow-path interrupt attention
11708  * handling function and fast-path interrupt attention handling function in
11709  * turn to process the relevant HBA attention events. This function is called
11710  * without any lock held. It gets the hbalock to access and update SLI data
11711  * structures.
11712  *
11713  * This function returns IRQ_HANDLED when interrupt is handled, else it
11714  * returns IRQ_NONE.
11715  **/
11716 irqreturn_t
11717 lpfc_sli4_intr_handler(int irq, void *dev_id)
11718 {
11719         struct lpfc_hba  *phba;
11720         irqreturn_t sp_irq_rc, fp_irq_rc;
11721         bool fp_handled = false;
11722         uint32_t fcp_eqidx;
11723
11724         /* Get the driver's phba structure from the dev_id */
11725         phba = (struct lpfc_hba *)dev_id;
11726
11727         if (unlikely(!phba))
11728                 return IRQ_NONE;
11729
11730         /*
11731          * Invokes slow-path host attention interrupt handling as appropriate.
11732          */
11733         sp_irq_rc = lpfc_sli4_sp_intr_handler(irq, dev_id);
11734
11735         /*
11736          * Invoke fast-path host attention interrupt handling as appropriate.
11737          */
11738         for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
11739                 fp_irq_rc = lpfc_sli4_fp_intr_handler(irq,
11740                                         &phba->sli4_hba.fcp_eq_hdl[fcp_eqidx]);
11741                 if (fp_irq_rc == IRQ_HANDLED)
11742                         fp_handled |= true;
11743         }
11744
11745         return (fp_handled == true) ? IRQ_HANDLED : sp_irq_rc;
11746 } /* lpfc_sli4_intr_handler */
11747
11748 /**
11749  * lpfc_sli4_queue_free - free a queue structure and associated memory
11750  * @queue: The queue structure to free.
11751  *
11752  * This function frees a queue structure and the DMAable memory used for
11753  * the host resident queue. This function must be called after destroying the
11754  * queue on the HBA.
11755  **/
11756 void
11757 lpfc_sli4_queue_free(struct lpfc_queue *queue)
11758 {
11759         struct lpfc_dmabuf *dmabuf;
11760
11761         if (!queue)
11762                 return;
11763
11764         while (!list_empty(&queue->page_list)) {
11765                 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
11766                                  list);
11767                 dma_free_coherent(&queue->phba->pcidev->dev, SLI4_PAGE_SIZE,
11768                                   dmabuf->virt, dmabuf->phys);
11769                 kfree(dmabuf);
11770         }
11771         kfree(queue);
11772         return;
11773 }
11774
11775 /**
11776  * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
11777  * @phba: The HBA that this queue is being created on.
11778  * @entry_size: The size of each queue entry for this queue.
11779  * @entry count: The number of entries that this queue will handle.
11780  *
11781  * This function allocates a queue structure and the DMAable memory used for
11782  * the host resident queue. This function must be called before creating the
11783  * queue on the HBA.
11784  **/
11785 struct lpfc_queue *
11786 lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
11787                       uint32_t entry_count)
11788 {
11789         struct lpfc_queue *queue;
11790         struct lpfc_dmabuf *dmabuf;
11791         int x, total_qe_count;
11792         void *dma_pointer;
11793         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
11794
11795         if (!phba->sli4_hba.pc_sli4_params.supported)
11796                 hw_page_size = SLI4_PAGE_SIZE;
11797
11798         queue = kzalloc(sizeof(struct lpfc_queue) +
11799                         (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
11800         if (!queue)
11801                 return NULL;
11802         queue->page_count = (ALIGN(entry_size * entry_count,
11803                         hw_page_size))/hw_page_size;
11804         INIT_LIST_HEAD(&queue->list);
11805         INIT_LIST_HEAD(&queue->page_list);
11806         INIT_LIST_HEAD(&queue->child_list);
11807         for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
11808                 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
11809                 if (!dmabuf)
11810                         goto out_fail;
11811                 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
11812                                                   hw_page_size, &dmabuf->phys,
11813                                                   GFP_KERNEL);
11814                 if (!dmabuf->virt) {
11815                         kfree(dmabuf);
11816                         goto out_fail;
11817                 }
11818                 memset(dmabuf->virt, 0, hw_page_size);
11819                 dmabuf->buffer_tag = x;
11820                 list_add_tail(&dmabuf->list, &queue->page_list);
11821                 /* initialize queue's entry array */
11822                 dma_pointer = dmabuf->virt;
11823                 for (; total_qe_count < entry_count &&
11824                      dma_pointer < (hw_page_size + dmabuf->virt);
11825                      total_qe_count++, dma_pointer += entry_size) {
11826                         queue->qe[total_qe_count].address = dma_pointer;
11827                 }
11828         }
11829         queue->entry_size = entry_size;
11830         queue->entry_count = entry_count;
11831
11832         /*
11833          * entry_repost is calculated based on the number of entries in the
11834          * queue. This works out except for RQs. If buffers are NOT initially
11835          * posted for every RQE, entry_repost should be adjusted accordingly.
11836          */
11837         queue->entry_repost = (entry_count >> 3);
11838         if (queue->entry_repost < LPFC_QUEUE_MIN_REPOST)
11839                 queue->entry_repost = LPFC_QUEUE_MIN_REPOST;
11840         queue->phba = phba;
11841
11842         return queue;
11843 out_fail:
11844         lpfc_sli4_queue_free(queue);
11845         return NULL;
11846 }
11847
11848 /**
11849  * lpfc_eq_create - Create an Event Queue on the HBA
11850  * @phba: HBA structure that indicates port to create a queue on.
11851  * @eq: The queue structure to use to create the event queue.
11852  * @imax: The maximum interrupt per second limit.
11853  *
11854  * This function creates an event queue, as detailed in @eq, on a port,
11855  * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
11856  *
11857  * The @phba struct is used to send mailbox command to HBA. The @eq struct
11858  * is used to get the entry count and entry size that are necessary to
11859  * determine the number of pages to allocate and use for this queue. This
11860  * function will send the EQ_CREATE mailbox command to the HBA to setup the
11861  * event queue. This function is asynchronous and will wait for the mailbox
11862  * command to finish before continuing.
11863  *
11864  * On success this function will return a zero. If unable to allocate enough
11865  * memory this function will return -ENOMEM. If the queue create mailbox command
11866  * fails this function will return -ENXIO.
11867  **/
11868 uint32_t
11869 lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint16_t imax)
11870 {
11871         struct lpfc_mbx_eq_create *eq_create;
11872         LPFC_MBOXQ_t *mbox;
11873         int rc, length, status = 0;
11874         struct lpfc_dmabuf *dmabuf;
11875         uint32_t shdr_status, shdr_add_status;
11876         union lpfc_sli4_cfg_shdr *shdr;
11877         uint16_t dmult;
11878         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
11879
11880         /* sanity check on queue memory */
11881         if (!eq)
11882                 return -ENODEV;
11883         if (!phba->sli4_hba.pc_sli4_params.supported)
11884                 hw_page_size = SLI4_PAGE_SIZE;
11885
11886         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11887         if (!mbox)
11888                 return -ENOMEM;
11889         length = (sizeof(struct lpfc_mbx_eq_create) -
11890                   sizeof(struct lpfc_sli4_cfg_mhdr));
11891         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
11892                          LPFC_MBOX_OPCODE_EQ_CREATE,
11893                          length, LPFC_SLI4_MBX_EMBED);
11894         eq_create = &mbox->u.mqe.un.eq_create;
11895         bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
11896                eq->page_count);
11897         bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
11898                LPFC_EQE_SIZE);
11899         bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
11900         /* Calculate delay multiper from maximum interrupt per second */
11901         dmult = LPFC_DMULT_CONST/imax - 1;
11902         bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
11903                dmult);
11904         switch (eq->entry_count) {
11905         default:
11906                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11907                                 "0360 Unsupported EQ count. (%d)\n",
11908                                 eq->entry_count);
11909                 if (eq->entry_count < 256)
11910                         return -EINVAL;
11911                 /* otherwise default to smallest count (drop through) */
11912         case 256:
11913                 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
11914                        LPFC_EQ_CNT_256);
11915                 break;
11916         case 512:
11917                 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
11918                        LPFC_EQ_CNT_512);
11919                 break;
11920         case 1024:
11921                 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
11922                        LPFC_EQ_CNT_1024);
11923                 break;
11924         case 2048:
11925                 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
11926                        LPFC_EQ_CNT_2048);
11927                 break;
11928         case 4096:
11929                 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
11930                        LPFC_EQ_CNT_4096);
11931                 break;
11932         }
11933         list_for_each_entry(dmabuf, &eq->page_list, list) {
11934                 memset(dmabuf->virt, 0, hw_page_size);
11935                 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
11936                                         putPaddrLow(dmabuf->phys);
11937                 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
11938                                         putPaddrHigh(dmabuf->phys);
11939         }
11940         mbox->vport = phba->pport;
11941         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
11942         mbox->context1 = NULL;
11943         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
11944         shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
11945         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11946         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11947         if (shdr_status || shdr_add_status || rc) {
11948                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11949                                 "2500 EQ_CREATE mailbox failed with "
11950                                 "status x%x add_status x%x, mbx status x%x\n",
11951                                 shdr_status, shdr_add_status, rc);
11952                 status = -ENXIO;
11953         }
11954         eq->type = LPFC_EQ;
11955         eq->subtype = LPFC_NONE;
11956         eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
11957         if (eq->queue_id == 0xFFFF)
11958                 status = -ENXIO;
11959         eq->host_index = 0;
11960         eq->hba_index = 0;
11961
11962         mempool_free(mbox, phba->mbox_mem_pool);
11963         return status;
11964 }
11965
11966 /**
11967  * lpfc_cq_create - Create a Completion Queue on the HBA
11968  * @phba: HBA structure that indicates port to create a queue on.
11969  * @cq: The queue structure to use to create the completion queue.
11970  * @eq: The event queue to bind this completion queue to.
11971  *
11972  * This function creates a completion queue, as detailed in @wq, on a port,
11973  * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
11974  *
11975  * The @phba struct is used to send mailbox command to HBA. The @cq struct
11976  * is used to get the entry count and entry size that are necessary to
11977  * determine the number of pages to allocate and use for this queue. The @eq
11978  * is used to indicate which event queue to bind this completion queue to. This
11979  * function will send the CQ_CREATE mailbox command to the HBA to setup the
11980  * completion queue. This function is asynchronous and will wait for the mailbox
11981  * command to finish before continuing.
11982  *
11983  * On success this function will return a zero. If unable to allocate enough
11984  * memory this function will return -ENOMEM. If the queue create mailbox command
11985  * fails this function will return -ENXIO.
11986  **/
11987 uint32_t
11988 lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
11989                struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
11990 {
11991         struct lpfc_mbx_cq_create *cq_create;
11992         struct lpfc_dmabuf *dmabuf;
11993         LPFC_MBOXQ_t *mbox;
11994         int rc, length, status = 0;
11995         uint32_t shdr_status, shdr_add_status;
11996         union lpfc_sli4_cfg_shdr *shdr;
11997         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
11998
11999         /* sanity check on queue memory */
12000         if (!cq || !eq)
12001                 return -ENODEV;
12002         if (!phba->sli4_hba.pc_sli4_params.supported)
12003                 hw_page_size = SLI4_PAGE_SIZE;
12004
12005         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12006         if (!mbox)
12007                 return -ENOMEM;
12008         length = (sizeof(struct lpfc_mbx_cq_create) -
12009                   sizeof(struct lpfc_sli4_cfg_mhdr));
12010         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12011                          LPFC_MBOX_OPCODE_CQ_CREATE,
12012                          length, LPFC_SLI4_MBX_EMBED);
12013         cq_create = &mbox->u.mqe.un.cq_create;
12014         shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
12015         bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
12016                     cq->page_count);
12017         bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
12018         bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
12019         bf_set(lpfc_mbox_hdr_version, &shdr->request,
12020                phba->sli4_hba.pc_sli4_params.cqv);
12021         if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
12022                 /* FW only supports 1. Should be PAGE_SIZE/SLI4_PAGE_SIZE */
12023                 bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request, 1);
12024                 bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
12025                        eq->queue_id);
12026         } else {
12027                 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
12028                        eq->queue_id);
12029         }
12030         switch (cq->entry_count) {
12031         default:
12032                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12033                                 "0361 Unsupported CQ count. (%d)\n",
12034                                 cq->entry_count);
12035                 if (cq->entry_count < 256)
12036                         return -EINVAL;
12037                 /* otherwise default to smallest count (drop through) */
12038         case 256:
12039                 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
12040                        LPFC_CQ_CNT_256);
12041                 break;
12042         case 512:
12043                 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
12044                        LPFC_CQ_CNT_512);
12045                 break;
12046         case 1024:
12047                 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
12048                        LPFC_CQ_CNT_1024);
12049                 break;
12050         }
12051         list_for_each_entry(dmabuf, &cq->page_list, list) {
12052                 memset(dmabuf->virt, 0, hw_page_size);
12053                 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
12054                                         putPaddrLow(dmabuf->phys);
12055                 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
12056                                         putPaddrHigh(dmabuf->phys);
12057         }
12058         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12059
12060         /* The IOCTL status is embedded in the mailbox subheader. */
12061         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12062         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12063         if (shdr_status || shdr_add_status || rc) {
12064                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12065                                 "2501 CQ_CREATE mailbox failed with "
12066                                 "status x%x add_status x%x, mbx status x%x\n",
12067                                 shdr_status, shdr_add_status, rc);
12068                 status = -ENXIO;
12069                 goto out;
12070         }
12071         cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
12072         if (cq->queue_id == 0xFFFF) {
12073                 status = -ENXIO;
12074                 goto out;
12075         }
12076         /* link the cq onto the parent eq child list */
12077         list_add_tail(&cq->list, &eq->child_list);
12078         /* Set up completion queue's type and subtype */
12079         cq->type = type;
12080         cq->subtype = subtype;
12081         cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
12082         cq->assoc_qid = eq->queue_id;
12083         cq->host_index = 0;
12084         cq->hba_index = 0;
12085
12086 out:
12087         mempool_free(mbox, phba->mbox_mem_pool);
12088         return status;
12089 }
12090
12091 /**
12092  * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
12093  * @phba: HBA structure that indicates port to create a queue on.
12094  * @mq: The queue structure to use to create the mailbox queue.
12095  * @mbox: An allocated pointer to type LPFC_MBOXQ_t
12096  * @cq: The completion queue to associate with this cq.
12097  *
12098  * This function provides failback (fb) functionality when the
12099  * mq_create_ext fails on older FW generations.  It's purpose is identical
12100  * to mq_create_ext otherwise.
12101  *
12102  * This routine cannot fail as all attributes were previously accessed and
12103  * initialized in mq_create_ext.
12104  **/
12105 static void
12106 lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
12107                        LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
12108 {
12109         struct lpfc_mbx_mq_create *mq_create;
12110         struct lpfc_dmabuf *dmabuf;
12111         int length;
12112
12113         length = (sizeof(struct lpfc_mbx_mq_create) -
12114                   sizeof(struct lpfc_sli4_cfg_mhdr));
12115         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12116                          LPFC_MBOX_OPCODE_MQ_CREATE,
12117                          length, LPFC_SLI4_MBX_EMBED);
12118         mq_create = &mbox->u.mqe.un.mq_create;
12119         bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
12120                mq->page_count);
12121         bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
12122                cq->queue_id);
12123         bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
12124         switch (mq->entry_count) {
12125         case 16:
12126                 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
12127                        LPFC_MQ_RING_SIZE_16);
12128                 break;
12129         case 32:
12130                 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
12131                        LPFC_MQ_RING_SIZE_32);
12132                 break;
12133         case 64:
12134                 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
12135                        LPFC_MQ_RING_SIZE_64);
12136                 break;
12137         case 128:
12138                 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
12139                        LPFC_MQ_RING_SIZE_128);
12140                 break;
12141         }
12142         list_for_each_entry(dmabuf, &mq->page_list, list) {
12143                 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
12144                         putPaddrLow(dmabuf->phys);
12145                 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
12146                         putPaddrHigh(dmabuf->phys);
12147         }
12148 }
12149
12150 /**
12151  * lpfc_mq_create - Create a mailbox Queue on the HBA
12152  * @phba: HBA structure that indicates port to create a queue on.
12153  * @mq: The queue structure to use to create the mailbox queue.
12154  * @cq: The completion queue to associate with this cq.
12155  * @subtype: The queue's subtype.
12156  *
12157  * This function creates a mailbox queue, as detailed in @mq, on a port,
12158  * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
12159  *
12160  * The @phba struct is used to send mailbox command to HBA. The @cq struct
12161  * is used to get the entry count and entry size that are necessary to
12162  * determine the number of pages to allocate and use for this queue. This
12163  * function will send the MQ_CREATE mailbox command to the HBA to setup the
12164  * mailbox queue. This function is asynchronous and will wait for the mailbox
12165  * command to finish before continuing.
12166  *
12167  * On success this function will return a zero. If unable to allocate enough
12168  * memory this function will return -ENOMEM. If the queue create mailbox command
12169  * fails this function will return -ENXIO.
12170  **/
12171 int32_t
12172 lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
12173                struct lpfc_queue *cq, uint32_t subtype)
12174 {
12175         struct lpfc_mbx_mq_create *mq_create;
12176         struct lpfc_mbx_mq_create_ext *mq_create_ext;
12177         struct lpfc_dmabuf *dmabuf;
12178         LPFC_MBOXQ_t *mbox;
12179         int rc, length, status = 0;
12180         uint32_t shdr_status, shdr_add_status;
12181         union lpfc_sli4_cfg_shdr *shdr;
12182         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
12183
12184         /* sanity check on queue memory */
12185         if (!mq || !cq)
12186                 return -ENODEV;
12187         if (!phba->sli4_hba.pc_sli4_params.supported)
12188                 hw_page_size = SLI4_PAGE_SIZE;
12189
12190         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12191         if (!mbox)
12192                 return -ENOMEM;
12193         length = (sizeof(struct lpfc_mbx_mq_create_ext) -
12194                   sizeof(struct lpfc_sli4_cfg_mhdr));
12195         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12196                          LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
12197                          length, LPFC_SLI4_MBX_EMBED);
12198
12199         mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
12200         shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
12201         bf_set(lpfc_mbx_mq_create_ext_num_pages,
12202                &mq_create_ext->u.request, mq->page_count);
12203         bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
12204                &mq_create_ext->u.request, 1);
12205         bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
12206                &mq_create_ext->u.request, 1);
12207         bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
12208                &mq_create_ext->u.request, 1);
12209         bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
12210                &mq_create_ext->u.request, 1);
12211         bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
12212                &mq_create_ext->u.request, 1);
12213         bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
12214         bf_set(lpfc_mbox_hdr_version, &shdr->request,
12215                phba->sli4_hba.pc_sli4_params.mqv);
12216         if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
12217                 bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
12218                        cq->queue_id);
12219         else
12220                 bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
12221                        cq->queue_id);
12222         switch (mq->entry_count) {
12223         default:
12224                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12225                                 "0362 Unsupported MQ count. (%d)\n",
12226                                 mq->entry_count);
12227                 if (mq->entry_count < 16)
12228                         return -EINVAL;
12229                 /* otherwise default to smallest count (drop through) */
12230         case 16:
12231                 bf_set(lpfc_mq_context_ring_size,
12232                        &mq_create_ext->u.request.context,
12233                        LPFC_MQ_RING_SIZE_16);
12234                 break;
12235         case 32:
12236                 bf_set(lpfc_mq_context_ring_size,
12237                        &mq_create_ext->u.request.context,
12238                        LPFC_MQ_RING_SIZE_32);
12239                 break;
12240         case 64:
12241                 bf_set(lpfc_mq_context_ring_size,
12242                        &mq_create_ext->u.request.context,
12243                        LPFC_MQ_RING_SIZE_64);
12244                 break;
12245         case 128:
12246                 bf_set(lpfc_mq_context_ring_size,
12247                        &mq_create_ext->u.request.context,
12248                        LPFC_MQ_RING_SIZE_128);
12249                 break;
12250         }
12251         list_for_each_entry(dmabuf, &mq->page_list, list) {
12252                 memset(dmabuf->virt, 0, hw_page_size);
12253                 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
12254                                         putPaddrLow(dmabuf->phys);
12255                 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
12256                                         putPaddrHigh(dmabuf->phys);
12257         }
12258         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12259         mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
12260                               &mq_create_ext->u.response);
12261         if (rc != MBX_SUCCESS) {
12262                 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
12263                                 "2795 MQ_CREATE_EXT failed with "
12264                                 "status x%x. Failback to MQ_CREATE.\n",
12265                                 rc);
12266                 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
12267                 mq_create = &mbox->u.mqe.un.mq_create;
12268                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12269                 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
12270                 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
12271                                       &mq_create->u.response);
12272         }
12273
12274         /* The IOCTL status is embedded in the mailbox subheader. */
12275         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12276         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12277         if (shdr_status || shdr_add_status || rc) {
12278                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12279                                 "2502 MQ_CREATE mailbox failed with "
12280                                 "status x%x add_status x%x, mbx status x%x\n",
12281                                 shdr_status, shdr_add_status, rc);
12282                 status = -ENXIO;
12283                 goto out;
12284         }
12285         if (mq->queue_id == 0xFFFF) {
12286                 status = -ENXIO;
12287                 goto out;
12288         }
12289         mq->type = LPFC_MQ;
12290         mq->assoc_qid = cq->queue_id;
12291         mq->subtype = subtype;
12292         mq->host_index = 0;
12293         mq->hba_index = 0;
12294
12295         /* link the mq onto the parent cq child list */
12296         list_add_tail(&mq->list, &cq->child_list);
12297 out:
12298         mempool_free(mbox, phba->mbox_mem_pool);
12299         return status;
12300 }
12301
12302 /**
12303  * lpfc_wq_create - Create a Work Queue on the HBA
12304  * @phba: HBA structure that indicates port to create a queue on.
12305  * @wq: The queue structure to use to create the work queue.
12306  * @cq: The completion queue to bind this work queue to.
12307  * @subtype: The subtype of the work queue indicating its functionality.
12308  *
12309  * This function creates a work queue, as detailed in @wq, on a port, described
12310  * by @phba by sending a WQ_CREATE mailbox command to the HBA.
12311  *
12312  * The @phba struct is used to send mailbox command to HBA. The @wq struct
12313  * is used to get the entry count and entry size that are necessary to
12314  * determine the number of pages to allocate and use for this queue. The @cq
12315  * is used to indicate which completion queue to bind this work queue to. This
12316  * function will send the WQ_CREATE mailbox command to the HBA to setup the
12317  * work queue. This function is asynchronous and will wait for the mailbox
12318  * command to finish before continuing.
12319  *
12320  * On success this function will return a zero. If unable to allocate enough
12321  * memory this function will return -ENOMEM. If the queue create mailbox command
12322  * fails this function will return -ENXIO.
12323  **/
12324 uint32_t
12325 lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
12326                struct lpfc_queue *cq, uint32_t subtype)
12327 {
12328         struct lpfc_mbx_wq_create *wq_create;
12329         struct lpfc_dmabuf *dmabuf;
12330         LPFC_MBOXQ_t *mbox;
12331         int rc, length, status = 0;
12332         uint32_t shdr_status, shdr_add_status;
12333         union lpfc_sli4_cfg_shdr *shdr;
12334         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
12335         struct dma_address *page;
12336
12337         /* sanity check on queue memory */
12338         if (!wq || !cq)
12339                 return -ENODEV;
12340         if (!phba->sli4_hba.pc_sli4_params.supported)
12341                 hw_page_size = SLI4_PAGE_SIZE;
12342
12343         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12344         if (!mbox)
12345                 return -ENOMEM;
12346         length = (sizeof(struct lpfc_mbx_wq_create) -
12347                   sizeof(struct lpfc_sli4_cfg_mhdr));
12348         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12349                          LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
12350                          length, LPFC_SLI4_MBX_EMBED);
12351         wq_create = &mbox->u.mqe.un.wq_create;
12352         shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
12353         bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
12354                     wq->page_count);
12355         bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
12356                     cq->queue_id);
12357         bf_set(lpfc_mbox_hdr_version, &shdr->request,
12358                phba->sli4_hba.pc_sli4_params.wqv);
12359         if (phba->sli4_hba.pc_sli4_params.wqv == LPFC_Q_CREATE_VERSION_1) {
12360                 bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
12361                        wq->entry_count);
12362                 switch (wq->entry_size) {
12363                 default:
12364                 case 64:
12365                         bf_set(lpfc_mbx_wq_create_wqe_size,
12366                                &wq_create->u.request_1,
12367                                LPFC_WQ_WQE_SIZE_64);
12368                         break;
12369                 case 128:
12370                         bf_set(lpfc_mbx_wq_create_wqe_size,
12371                                &wq_create->u.request_1,
12372                                LPFC_WQ_WQE_SIZE_128);
12373                         break;
12374                 }
12375                 bf_set(lpfc_mbx_wq_create_page_size, &wq_create->u.request_1,
12376                        (PAGE_SIZE/SLI4_PAGE_SIZE));
12377                 page = wq_create->u.request_1.page;
12378         } else {
12379                 page = wq_create->u.request.page;
12380         }
12381         list_for_each_entry(dmabuf, &wq->page_list, list) {
12382                 memset(dmabuf->virt, 0, hw_page_size);
12383                 page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
12384                 page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
12385         }
12386         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12387         /* The IOCTL status is embedded in the mailbox subheader. */
12388         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12389         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12390         if (shdr_status || shdr_add_status || rc) {
12391                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12392                                 "2503 WQ_CREATE mailbox failed with "
12393                                 "status x%x add_status x%x, mbx status x%x\n",
12394                                 shdr_status, shdr_add_status, rc);
12395                 status = -ENXIO;
12396                 goto out;
12397         }
12398         wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
12399         if (wq->queue_id == 0xFFFF) {
12400                 status = -ENXIO;
12401                 goto out;
12402         }
12403         wq->type = LPFC_WQ;
12404         wq->assoc_qid = cq->queue_id;
12405         wq->subtype = subtype;
12406         wq->host_index = 0;
12407         wq->hba_index = 0;
12408         wq->entry_repost = LPFC_RELEASE_NOTIFICATION_INTERVAL;
12409
12410         /* link the wq onto the parent cq child list */
12411         list_add_tail(&wq->list, &cq->child_list);
12412 out:
12413         mempool_free(mbox, phba->mbox_mem_pool);
12414         return status;
12415 }
12416
12417 /**
12418  * lpfc_rq_adjust_repost - Adjust entry_repost for an RQ
12419  * @phba: HBA structure that indicates port to create a queue on.
12420  * @rq:   The queue structure to use for the receive queue.
12421  * @qno:  The associated HBQ number
12422  *
12423  *
12424  * For SLI4 we need to adjust the RQ repost value based on
12425  * the number of buffers that are initially posted to the RQ.
12426  */
12427 void
12428 lpfc_rq_adjust_repost(struct lpfc_hba *phba, struct lpfc_queue *rq, int qno)
12429 {
12430         uint32_t cnt;
12431
12432         /* sanity check on queue memory */
12433         if (!rq)
12434                 return;
12435         cnt = lpfc_hbq_defs[qno]->entry_count;
12436
12437         /* Recalc repost for RQs based on buffers initially posted */
12438         cnt = (cnt >> 3);
12439         if (cnt < LPFC_QUEUE_MIN_REPOST)
12440                 cnt = LPFC_QUEUE_MIN_REPOST;
12441
12442         rq->entry_repost = cnt;
12443 }
12444
12445 /**
12446  * lpfc_rq_create - Create a Receive Queue on the HBA
12447  * @phba: HBA structure that indicates port to create a queue on.
12448  * @hrq: The queue structure to use to create the header receive queue.
12449  * @drq: The queue structure to use to create the data receive queue.
12450  * @cq: The completion queue to bind this work queue to.
12451  *
12452  * This function creates a receive buffer queue pair , as detailed in @hrq and
12453  * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
12454  * to the HBA.
12455  *
12456  * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
12457  * struct is used to get the entry count that is necessary to determine the
12458  * number of pages to use for this queue. The @cq is used to indicate which
12459  * completion queue to bind received buffers that are posted to these queues to.
12460  * This function will send the RQ_CREATE mailbox command to the HBA to setup the
12461  * receive queue pair. This function is asynchronous and will wait for the
12462  * mailbox command to finish before continuing.
12463  *
12464  * On success this function will return a zero. If unable to allocate enough
12465  * memory this function will return -ENOMEM. If the queue create mailbox command
12466  * fails this function will return -ENXIO.
12467  **/
12468 uint32_t
12469 lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
12470                struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
12471 {
12472         struct lpfc_mbx_rq_create *rq_create;
12473         struct lpfc_dmabuf *dmabuf;
12474         LPFC_MBOXQ_t *mbox;
12475         int rc, length, status = 0;
12476         uint32_t shdr_status, shdr_add_status;
12477         union lpfc_sli4_cfg_shdr *shdr;
12478         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
12479
12480         /* sanity check on queue memory */
12481         if (!hrq || !drq || !cq)
12482                 return -ENODEV;
12483         if (!phba->sli4_hba.pc_sli4_params.supported)
12484                 hw_page_size = SLI4_PAGE_SIZE;
12485
12486         if (hrq->entry_count != drq->entry_count)
12487                 return -EINVAL;
12488         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12489         if (!mbox)
12490                 return -ENOMEM;
12491         length = (sizeof(struct lpfc_mbx_rq_create) -
12492                   sizeof(struct lpfc_sli4_cfg_mhdr));
12493         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12494                          LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
12495                          length, LPFC_SLI4_MBX_EMBED);
12496         rq_create = &mbox->u.mqe.un.rq_create;
12497         shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
12498         bf_set(lpfc_mbox_hdr_version, &shdr->request,
12499                phba->sli4_hba.pc_sli4_params.rqv);
12500         if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
12501                 bf_set(lpfc_rq_context_rqe_count_1,
12502                        &rq_create->u.request.context,
12503                        hrq->entry_count);
12504                 rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
12505                 bf_set(lpfc_rq_context_rqe_size,
12506                        &rq_create->u.request.context,
12507                        LPFC_RQE_SIZE_8);
12508                 bf_set(lpfc_rq_context_page_size,
12509                        &rq_create->u.request.context,
12510                        (PAGE_SIZE/SLI4_PAGE_SIZE));
12511         } else {
12512                 switch (hrq->entry_count) {
12513                 default:
12514                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12515                                         "2535 Unsupported RQ count. (%d)\n",
12516                                         hrq->entry_count);
12517                         if (hrq->entry_count < 512)
12518                                 return -EINVAL;
12519                         /* otherwise default to smallest count (drop through) */
12520                 case 512:
12521                         bf_set(lpfc_rq_context_rqe_count,
12522                                &rq_create->u.request.context,
12523                                LPFC_RQ_RING_SIZE_512);
12524                         break;
12525                 case 1024:
12526                         bf_set(lpfc_rq_context_rqe_count,
12527                                &rq_create->u.request.context,
12528                                LPFC_RQ_RING_SIZE_1024);
12529                         break;
12530                 case 2048:
12531                         bf_set(lpfc_rq_context_rqe_count,
12532                                &rq_create->u.request.context,
12533                                LPFC_RQ_RING_SIZE_2048);
12534                         break;
12535                 case 4096:
12536                         bf_set(lpfc_rq_context_rqe_count,
12537                                &rq_create->u.request.context,
12538                                LPFC_RQ_RING_SIZE_4096);
12539                         break;
12540                 }
12541                 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
12542                        LPFC_HDR_BUF_SIZE);
12543         }
12544         bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
12545                cq->queue_id);
12546         bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
12547                hrq->page_count);
12548         list_for_each_entry(dmabuf, &hrq->page_list, list) {
12549                 memset(dmabuf->virt, 0, hw_page_size);
12550                 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
12551                                         putPaddrLow(dmabuf->phys);
12552                 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
12553                                         putPaddrHigh(dmabuf->phys);
12554         }
12555         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12556         /* The IOCTL status is embedded in the mailbox subheader. */
12557         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12558         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12559         if (shdr_status || shdr_add_status || rc) {
12560                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12561                                 "2504 RQ_CREATE mailbox failed with "
12562                                 "status x%x add_status x%x, mbx status x%x\n",
12563                                 shdr_status, shdr_add_status, rc);
12564                 status = -ENXIO;
12565                 goto out;
12566         }
12567         hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
12568         if (hrq->queue_id == 0xFFFF) {
12569                 status = -ENXIO;
12570                 goto out;
12571         }
12572         hrq->type = LPFC_HRQ;
12573         hrq->assoc_qid = cq->queue_id;
12574         hrq->subtype = subtype;
12575         hrq->host_index = 0;
12576         hrq->hba_index = 0;
12577
12578         /* now create the data queue */
12579         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12580                          LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
12581                          length, LPFC_SLI4_MBX_EMBED);
12582         bf_set(lpfc_mbox_hdr_version, &shdr->request,
12583                phba->sli4_hba.pc_sli4_params.rqv);
12584         if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
12585                 bf_set(lpfc_rq_context_rqe_count_1,
12586                        &rq_create->u.request.context, hrq->entry_count);
12587                 rq_create->u.request.context.buffer_size = LPFC_DATA_BUF_SIZE;
12588                 bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
12589                        LPFC_RQE_SIZE_8);
12590                 bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
12591                        (PAGE_SIZE/SLI4_PAGE_SIZE));
12592         } else {
12593                 switch (drq->entry_count) {
12594                 default:
12595                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12596                                         "2536 Unsupported RQ count. (%d)\n",
12597                                         drq->entry_count);
12598                         if (drq->entry_count < 512)
12599                                 return -EINVAL;
12600                         /* otherwise default to smallest count (drop through) */
12601                 case 512:
12602                         bf_set(lpfc_rq_context_rqe_count,
12603                                &rq_create->u.request.context,
12604                                LPFC_RQ_RING_SIZE_512);
12605                         break;
12606                 case 1024:
12607                         bf_set(lpfc_rq_context_rqe_count,
12608                                &rq_create->u.request.context,
12609                                LPFC_RQ_RING_SIZE_1024);
12610                         break;
12611                 case 2048:
12612                         bf_set(lpfc_rq_context_rqe_count,
12613                                &rq_create->u.request.context,
12614                                LPFC_RQ_RING_SIZE_2048);
12615                         break;
12616                 case 4096:
12617                         bf_set(lpfc_rq_context_rqe_count,
12618                                &rq_create->u.request.context,
12619                                LPFC_RQ_RING_SIZE_4096);
12620                         break;
12621                 }
12622                 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
12623                        LPFC_DATA_BUF_SIZE);
12624         }
12625         bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
12626                cq->queue_id);
12627         bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
12628                drq->page_count);
12629         list_for_each_entry(dmabuf, &drq->page_list, list) {
12630                 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
12631                                         putPaddrLow(dmabuf->phys);
12632                 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
12633                                         putPaddrHigh(dmabuf->phys);
12634         }
12635         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12636         /* The IOCTL status is embedded in the mailbox subheader. */
12637         shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
12638         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12639         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12640         if (shdr_status || shdr_add_status || rc) {
12641                 status = -ENXIO;
12642                 goto out;
12643         }
12644         drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
12645         if (drq->queue_id == 0xFFFF) {
12646                 status = -ENXIO;
12647                 goto out;
12648         }
12649         drq->type = LPFC_DRQ;
12650         drq->assoc_qid = cq->queue_id;
12651         drq->subtype = subtype;
12652         drq->host_index = 0;
12653         drq->hba_index = 0;
12654
12655         /* link the header and data RQs onto the parent cq child list */
12656         list_add_tail(&hrq->list, &cq->child_list);
12657         list_add_tail(&drq->list, &cq->child_list);
12658
12659 out:
12660         mempool_free(mbox, phba->mbox_mem_pool);
12661         return status;
12662 }
12663
12664 /**
12665  * lpfc_eq_destroy - Destroy an event Queue on the HBA
12666  * @eq: The queue structure associated with the queue to destroy.
12667  *
12668  * This function destroys a queue, as detailed in @eq by sending an mailbox
12669  * command, specific to the type of queue, to the HBA.
12670  *
12671  * The @eq struct is used to get the queue ID of the queue to destroy.
12672  *
12673  * On success this function will return a zero. If the queue destroy mailbox
12674  * command fails this function will return -ENXIO.
12675  **/
12676 uint32_t
12677 lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
12678 {
12679         LPFC_MBOXQ_t *mbox;
12680         int rc, length, status = 0;
12681         uint32_t shdr_status, shdr_add_status;
12682         union lpfc_sli4_cfg_shdr *shdr;
12683
12684         /* sanity check on queue memory */
12685         if (!eq)
12686                 return -ENODEV;
12687         mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
12688         if (!mbox)
12689                 return -ENOMEM;
12690         length = (sizeof(struct lpfc_mbx_eq_destroy) -
12691                   sizeof(struct lpfc_sli4_cfg_mhdr));
12692         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12693                          LPFC_MBOX_OPCODE_EQ_DESTROY,
12694                          length, LPFC_SLI4_MBX_EMBED);
12695         bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
12696                eq->queue_id);
12697         mbox->vport = eq->phba->pport;
12698         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12699
12700         rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
12701         /* The IOCTL status is embedded in the mailbox subheader. */
12702         shdr = (union lpfc_sli4_cfg_shdr *)
12703                 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
12704         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12705         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12706         if (shdr_status || shdr_add_status || rc) {
12707                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12708                                 "2505 EQ_DESTROY mailbox failed with "
12709                                 "status x%x add_status x%x, mbx status x%x\n",
12710                                 shdr_status, shdr_add_status, rc);
12711                 status = -ENXIO;
12712         }
12713
12714         /* Remove eq from any list */
12715         list_del_init(&eq->list);
12716         mempool_free(mbox, eq->phba->mbox_mem_pool);
12717         return status;
12718 }
12719
12720 /**
12721  * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
12722  * @cq: The queue structure associated with the queue to destroy.
12723  *
12724  * This function destroys a queue, as detailed in @cq by sending an mailbox
12725  * command, specific to the type of queue, to the HBA.
12726  *
12727  * The @cq struct is used to get the queue ID of the queue to destroy.
12728  *
12729  * On success this function will return a zero. If the queue destroy mailbox
12730  * command fails this function will return -ENXIO.
12731  **/
12732 uint32_t
12733 lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
12734 {
12735         LPFC_MBOXQ_t *mbox;
12736         int rc, length, status = 0;
12737         uint32_t shdr_status, shdr_add_status;
12738         union lpfc_sli4_cfg_shdr *shdr;
12739
12740         /* sanity check on queue memory */
12741         if (!cq)
12742                 return -ENODEV;
12743         mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
12744         if (!mbox)
12745                 return -ENOMEM;
12746         length = (sizeof(struct lpfc_mbx_cq_destroy) -
12747                   sizeof(struct lpfc_sli4_cfg_mhdr));
12748         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12749                          LPFC_MBOX_OPCODE_CQ_DESTROY,
12750                          length, LPFC_SLI4_MBX_EMBED);
12751         bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
12752                cq->queue_id);
12753         mbox->vport = cq->phba->pport;
12754         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12755         rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
12756         /* The IOCTL status is embedded in the mailbox subheader. */
12757         shdr = (union lpfc_sli4_cfg_shdr *)
12758                 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
12759         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12760         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12761         if (shdr_status || shdr_add_status || rc) {
12762                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12763                                 "2506 CQ_DESTROY mailbox failed with "
12764                                 "status x%x add_status x%x, mbx status x%x\n",
12765                                 shdr_status, shdr_add_status, rc);
12766                 status = -ENXIO;
12767         }
12768         /* Remove cq from any list */
12769         list_del_init(&cq->list);
12770         mempool_free(mbox, cq->phba->mbox_mem_pool);
12771         return status;
12772 }
12773
12774 /**
12775  * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
12776  * @qm: The queue structure associated with the queue to destroy.
12777  *
12778  * This function destroys a queue, as detailed in @mq by sending an mailbox
12779  * command, specific to the type of queue, to the HBA.
12780  *
12781  * The @mq struct is used to get the queue ID of the queue to destroy.
12782  *
12783  * On success this function will return a zero. If the queue destroy mailbox
12784  * command fails this function will return -ENXIO.
12785  **/
12786 uint32_t
12787 lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
12788 {
12789         LPFC_MBOXQ_t *mbox;
12790         int rc, length, status = 0;
12791         uint32_t shdr_status, shdr_add_status;
12792         union lpfc_sli4_cfg_shdr *shdr;
12793
12794         /* sanity check on queue memory */
12795         if (!mq)
12796                 return -ENODEV;
12797         mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
12798         if (!mbox)
12799                 return -ENOMEM;
12800         length = (sizeof(struct lpfc_mbx_mq_destroy) -
12801                   sizeof(struct lpfc_sli4_cfg_mhdr));
12802         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12803                          LPFC_MBOX_OPCODE_MQ_DESTROY,
12804                          length, LPFC_SLI4_MBX_EMBED);
12805         bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
12806                mq->queue_id);
12807         mbox->vport = mq->phba->pport;
12808         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12809         rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
12810         /* The IOCTL status is embedded in the mailbox subheader. */
12811         shdr = (union lpfc_sli4_cfg_shdr *)
12812                 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
12813         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12814         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12815         if (shdr_status || shdr_add_status || rc) {
12816                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12817                                 "2507 MQ_DESTROY mailbox failed with "
12818                                 "status x%x add_status x%x, mbx status x%x\n",
12819                                 shdr_status, shdr_add_status, rc);
12820                 status = -ENXIO;
12821         }
12822         /* Remove mq from any list */
12823         list_del_init(&mq->list);
12824         mempool_free(mbox, mq->phba->mbox_mem_pool);
12825         return status;
12826 }
12827
12828 /**
12829  * lpfc_wq_destroy - Destroy a Work Queue on the HBA
12830  * @wq: The queue structure associated with the queue to destroy.
12831  *
12832  * This function destroys a queue, as detailed in @wq by sending an mailbox
12833  * command, specific to the type of queue, to the HBA.
12834  *
12835  * The @wq struct is used to get the queue ID of the queue to destroy.
12836  *
12837  * On success this function will return a zero. If the queue destroy mailbox
12838  * command fails this function will return -ENXIO.
12839  **/
12840 uint32_t
12841 lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
12842 {
12843         LPFC_MBOXQ_t *mbox;
12844         int rc, length, status = 0;
12845         uint32_t shdr_status, shdr_add_status;
12846         union lpfc_sli4_cfg_shdr *shdr;
12847
12848         /* sanity check on queue memory */
12849         if (!wq)
12850                 return -ENODEV;
12851         mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
12852         if (!mbox)
12853                 return -ENOMEM;
12854         length = (sizeof(struct lpfc_mbx_wq_destroy) -
12855                   sizeof(struct lpfc_sli4_cfg_mhdr));
12856         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12857                          LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
12858                          length, LPFC_SLI4_MBX_EMBED);
12859         bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
12860                wq->queue_id);
12861         mbox->vport = wq->phba->pport;
12862         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12863         rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
12864         shdr = (union lpfc_sli4_cfg_shdr *)
12865                 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
12866         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12867         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12868         if (shdr_status || shdr_add_status || rc) {
12869                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12870                                 "2508 WQ_DESTROY mailbox failed with "
12871                                 "status x%x add_status x%x, mbx status x%x\n",
12872                                 shdr_status, shdr_add_status, rc);
12873                 status = -ENXIO;
12874         }
12875         /* Remove wq from any list */
12876         list_del_init(&wq->list);
12877         mempool_free(mbox, wq->phba->mbox_mem_pool);
12878         return status;
12879 }
12880
12881 /**
12882  * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
12883  * @rq: The queue structure associated with the queue to destroy.
12884  *
12885  * This function destroys a queue, as detailed in @rq by sending an mailbox
12886  * command, specific to the type of queue, to the HBA.
12887  *
12888  * The @rq struct is used to get the queue ID of the queue to destroy.
12889  *
12890  * On success this function will return a zero. If the queue destroy mailbox
12891  * command fails this function will return -ENXIO.
12892  **/
12893 uint32_t
12894 lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
12895                 struct lpfc_queue *drq)
12896 {
12897         LPFC_MBOXQ_t *mbox;
12898         int rc, length, status = 0;
12899         uint32_t shdr_status, shdr_add_status;
12900         union lpfc_sli4_cfg_shdr *shdr;
12901
12902         /* sanity check on queue memory */
12903         if (!hrq || !drq)
12904                 return -ENODEV;
12905         mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
12906         if (!mbox)
12907                 return -ENOMEM;
12908         length = (sizeof(struct lpfc_mbx_rq_destroy) -
12909                   sizeof(struct lpfc_sli4_cfg_mhdr));
12910         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12911                          LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
12912                          length, LPFC_SLI4_MBX_EMBED);
12913         bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
12914                hrq->queue_id);
12915         mbox->vport = hrq->phba->pport;
12916         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12917         rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
12918         /* The IOCTL status is embedded in the mailbox subheader. */
12919         shdr = (union lpfc_sli4_cfg_shdr *)
12920                 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
12921         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12922         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12923         if (shdr_status || shdr_add_status || rc) {
12924                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12925                                 "2509 RQ_DESTROY mailbox failed with "
12926                                 "status x%x add_status x%x, mbx status x%x\n",
12927                                 shdr_status, shdr_add_status, rc);
12928                 if (rc != MBX_TIMEOUT)
12929                         mempool_free(mbox, hrq->phba->mbox_mem_pool);
12930                 return -ENXIO;
12931         }
12932         bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
12933                drq->queue_id);
12934         rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
12935         shdr = (union lpfc_sli4_cfg_shdr *)
12936                 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
12937         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12938         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12939         if (shdr_status || shdr_add_status || rc) {
12940                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12941                                 "2510 RQ_DESTROY mailbox failed with "
12942                                 "status x%x add_status x%x, mbx status x%x\n",
12943                                 shdr_status, shdr_add_status, rc);
12944                 status = -ENXIO;
12945         }
12946         list_del_init(&hrq->list);
12947         list_del_init(&drq->list);
12948         mempool_free(mbox, hrq->phba->mbox_mem_pool);
12949         return status;
12950 }
12951
12952 /**
12953  * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
12954  * @phba: The virtual port for which this call being executed.
12955  * @pdma_phys_addr0: Physical address of the 1st SGL page.
12956  * @pdma_phys_addr1: Physical address of the 2nd SGL page.
12957  * @xritag: the xritag that ties this io to the SGL pages.
12958  *
12959  * This routine will post the sgl pages for the IO that has the xritag
12960  * that is in the iocbq structure. The xritag is assigned during iocbq
12961  * creation and persists for as long as the driver is loaded.
12962  * if the caller has fewer than 256 scatter gather segments to map then
12963  * pdma_phys_addr1 should be 0.
12964  * If the caller needs to map more than 256 scatter gather segment then
12965  * pdma_phys_addr1 should be a valid physical address.
12966  * physical address for SGLs must be 64 byte aligned.
12967  * If you are going to map 2 SGL's then the first one must have 256 entries
12968  * the second sgl can have between 1 and 256 entries.
12969  *
12970  * Return codes:
12971  *      0 - Success
12972  *      -ENXIO, -ENOMEM - Failure
12973  **/
12974 int
12975 lpfc_sli4_post_sgl(struct lpfc_hba *phba,
12976                 dma_addr_t pdma_phys_addr0,
12977                 dma_addr_t pdma_phys_addr1,
12978                 uint16_t xritag)
12979 {
12980         struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
12981         LPFC_MBOXQ_t *mbox;
12982         int rc;
12983         uint32_t shdr_status, shdr_add_status;
12984         uint32_t mbox_tmo;
12985         union lpfc_sli4_cfg_shdr *shdr;
12986
12987         if (xritag == NO_XRI) {
12988                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12989                                 "0364 Invalid param:\n");
12990                 return -EINVAL;
12991         }
12992
12993         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12994         if (!mbox)
12995                 return -ENOMEM;
12996
12997         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12998                         LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
12999                         sizeof(struct lpfc_mbx_post_sgl_pages) -
13000                         sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
13001
13002         post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
13003                                 &mbox->u.mqe.un.post_sgl_pages;
13004         bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
13005         bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
13006
13007         post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
13008                                 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
13009         post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
13010                                 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
13011
13012         post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
13013                                 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
13014         post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
13015                                 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
13016         if (!phba->sli4_hba.intr_enable)
13017                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13018         else {
13019                 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
13020                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
13021         }
13022         /* The IOCTL status is embedded in the mailbox subheader. */
13023         shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
13024         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13025         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13026         if (rc != MBX_TIMEOUT)
13027                 mempool_free(mbox, phba->mbox_mem_pool);
13028         if (shdr_status || shdr_add_status || rc) {
13029                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13030                                 "2511 POST_SGL mailbox failed with "
13031                                 "status x%x add_status x%x, mbx status x%x\n",
13032                                 shdr_status, shdr_add_status, rc);
13033                 rc = -ENXIO;
13034         }
13035         return 0;
13036 }
13037
13038 /**
13039  * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
13040  * @phba: pointer to lpfc hba data structure.
13041  *
13042  * This routine is invoked to post rpi header templates to the
13043  * HBA consistent with the SLI-4 interface spec.  This routine
13044  * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
13045  * SLI4_PAGE_SIZE modulo 64 rpi context headers.
13046  *
13047  * Returns
13048  *      A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
13049  *      LPFC_RPI_ALLOC_ERROR if no rpis are available.
13050  **/
13051 uint16_t
13052 lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
13053 {
13054         unsigned long xri;
13055
13056         /*
13057          * Fetch the next logical xri.  Because this index is logical,
13058          * the driver starts at 0 each time.
13059          */
13060         spin_lock_irq(&phba->hbalock);
13061         xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
13062                                  phba->sli4_hba.max_cfg_param.max_xri, 0);
13063         if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
13064                 spin_unlock_irq(&phba->hbalock);
13065                 return NO_XRI;
13066         } else {
13067                 set_bit(xri, phba->sli4_hba.xri_bmask);
13068                 phba->sli4_hba.max_cfg_param.xri_used++;
13069                 phba->sli4_hba.xri_count++;
13070         }
13071
13072         spin_unlock_irq(&phba->hbalock);
13073         return xri;
13074 }
13075
13076 /**
13077  * lpfc_sli4_free_xri - Release an xri for reuse.
13078  * @phba: pointer to lpfc hba data structure.
13079  *
13080  * This routine is invoked to release an xri to the pool of
13081  * available rpis maintained by the driver.
13082  **/
13083 void
13084 __lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
13085 {
13086         if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
13087                 phba->sli4_hba.xri_count--;
13088                 phba->sli4_hba.max_cfg_param.xri_used--;
13089         }
13090 }
13091
13092 /**
13093  * lpfc_sli4_free_xri - Release an xri for reuse.
13094  * @phba: pointer to lpfc hba data structure.
13095  *
13096  * This routine is invoked to release an xri to the pool of
13097  * available rpis maintained by the driver.
13098  **/
13099 void
13100 lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
13101 {
13102         spin_lock_irq(&phba->hbalock);
13103         __lpfc_sli4_free_xri(phba, xri);
13104         spin_unlock_irq(&phba->hbalock);
13105 }
13106
13107 /**
13108  * lpfc_sli4_next_xritag - Get an xritag for the io
13109  * @phba: Pointer to HBA context object.
13110  *
13111  * This function gets an xritag for the iocb. If there is no unused xritag
13112  * it will return 0xffff.
13113  * The function returns the allocated xritag if successful, else returns zero.
13114  * Zero is not a valid xritag.
13115  * The caller is not required to hold any lock.
13116  **/
13117 uint16_t
13118 lpfc_sli4_next_xritag(struct lpfc_hba *phba)
13119 {
13120         uint16_t xri_index;
13121
13122         xri_index = lpfc_sli4_alloc_xri(phba);
13123         if (xri_index != NO_XRI)
13124                 return xri_index;
13125
13126         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13127                         "2004 Failed to allocate XRI.last XRITAG is %d"
13128                         " Max XRI is %d, Used XRI is %d\n",
13129                         xri_index,
13130                         phba->sli4_hba.max_cfg_param.max_xri,
13131                         phba->sli4_hba.max_cfg_param.xri_used);
13132         return NO_XRI;
13133 }
13134
13135 /**
13136  * lpfc_sli4_post_els_sgl_list - post a block of ELS sgls to the port.
13137  * @phba: pointer to lpfc hba data structure.
13138  *
13139  * This routine is invoked to post a block of driver's sgl pages to the
13140  * HBA using non-embedded mailbox command. No Lock is held. This routine
13141  * is only called when the driver is loading and after all IO has been
13142  * stopped.
13143  **/
13144 int
13145 lpfc_sli4_post_els_sgl_list(struct lpfc_hba *phba)
13146 {
13147         struct lpfc_sglq *sglq_entry;
13148         struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
13149         struct sgl_page_pairs *sgl_pg_pairs;
13150         void *viraddr;
13151         LPFC_MBOXQ_t *mbox;
13152         uint32_t reqlen, alloclen, pg_pairs;
13153         uint32_t mbox_tmo;
13154         uint16_t xritag_start = 0, lxri = 0;
13155         int els_xri_cnt, rc = 0;
13156         uint32_t shdr_status, shdr_add_status;
13157         union lpfc_sli4_cfg_shdr *shdr;
13158
13159         /* The number of sgls to be posted */
13160         els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
13161
13162         reqlen = els_xri_cnt * sizeof(struct sgl_page_pairs) +
13163                  sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
13164         if (reqlen > SLI4_PAGE_SIZE) {
13165                 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
13166                                 "2559 Block sgl registration required DMA "
13167                                 "size (%d) great than a page\n", reqlen);
13168                 return -ENOMEM;
13169         }
13170         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13171         if (!mbox)
13172                 return -ENOMEM;
13173
13174         /* Allocate DMA memory and set up the non-embedded mailbox command */
13175         alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13176                          LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
13177                          LPFC_SLI4_MBX_NEMBED);
13178
13179         if (alloclen < reqlen) {
13180                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13181                                 "0285 Allocated DMA memory size (%d) is "
13182                                 "less than the requested DMA memory "
13183                                 "size (%d)\n", alloclen, reqlen);
13184                 lpfc_sli4_mbox_cmd_free(phba, mbox);
13185                 return -ENOMEM;
13186         }
13187         /* Set up the SGL pages in the non-embedded DMA pages */
13188         viraddr = mbox->sge_array->addr[0];
13189         sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
13190         sgl_pg_pairs = &sgl->sgl_pg_pairs;
13191
13192         for (pg_pairs = 0; pg_pairs < els_xri_cnt; pg_pairs++) {
13193                 sglq_entry = phba->sli4_hba.lpfc_els_sgl_array[pg_pairs];
13194
13195                 /*
13196                  * Assign the sglq a physical xri only if the driver has not
13197                  * initialized those resources.  A port reset only needs
13198                  * the sglq's posted.
13199                  */
13200                 if (bf_get(lpfc_xri_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
13201                     LPFC_XRI_RSRC_RDY) {
13202                         lxri = lpfc_sli4_next_xritag(phba);
13203                         if (lxri == NO_XRI) {
13204                                 lpfc_sli4_mbox_cmd_free(phba, mbox);
13205                                 return -ENOMEM;
13206                         }
13207                         sglq_entry->sli4_lxritag = lxri;
13208                         sglq_entry->sli4_xritag = phba->sli4_hba.xri_ids[lxri];
13209                 }
13210
13211                 /* Set up the sge entry */
13212                 sgl_pg_pairs->sgl_pg0_addr_lo =
13213                                 cpu_to_le32(putPaddrLow(sglq_entry->phys));
13214                 sgl_pg_pairs->sgl_pg0_addr_hi =
13215                                 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
13216                 sgl_pg_pairs->sgl_pg1_addr_lo =
13217                                 cpu_to_le32(putPaddrLow(0));
13218                 sgl_pg_pairs->sgl_pg1_addr_hi =
13219                                 cpu_to_le32(putPaddrHigh(0));
13220
13221                 /* Keep the first xritag on the list */
13222                 if (pg_pairs == 0)
13223                         xritag_start = sglq_entry->sli4_xritag;
13224                 sgl_pg_pairs++;
13225         }
13226
13227         /* Complete initialization and perform endian conversion. */
13228         bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
13229         bf_set(lpfc_post_sgl_pages_xricnt, sgl, els_xri_cnt);
13230         sgl->word0 = cpu_to_le32(sgl->word0);
13231         if (!phba->sli4_hba.intr_enable)
13232                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13233         else {
13234                 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
13235                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
13236         }
13237         shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
13238         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13239         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13240         if (rc != MBX_TIMEOUT)
13241                 lpfc_sli4_mbox_cmd_free(phba, mbox);
13242         if (shdr_status || shdr_add_status || rc) {
13243                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13244                                 "2513 POST_SGL_BLOCK mailbox command failed "
13245                                 "status x%x add_status x%x mbx status x%x\n",
13246                                 shdr_status, shdr_add_status, rc);
13247                 rc = -ENXIO;
13248         }
13249
13250         if (rc == 0)
13251                 bf_set(lpfc_xri_rsrc_rdy, &phba->sli4_hba.sli4_flags,
13252                        LPFC_XRI_RSRC_RDY);
13253         return rc;
13254 }
13255
13256 /**
13257  * lpfc_sli4_post_els_sgl_list_ext - post a block of ELS sgls to the port.
13258  * @phba: pointer to lpfc hba data structure.
13259  *
13260  * This routine is invoked to post a block of driver's sgl pages to the
13261  * HBA using non-embedded mailbox command. No Lock is held. This routine
13262  * is only called when the driver is loading and after all IO has been
13263  * stopped.
13264  **/
13265 int
13266 lpfc_sli4_post_els_sgl_list_ext(struct lpfc_hba *phba)
13267 {
13268         struct lpfc_sglq *sglq_entry;
13269         struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
13270         struct sgl_page_pairs *sgl_pg_pairs;
13271         void *viraddr;
13272         LPFC_MBOXQ_t *mbox;
13273         uint32_t reqlen, alloclen, index;
13274         uint32_t mbox_tmo;
13275         uint16_t rsrc_start, rsrc_size, els_xri_cnt;
13276         uint16_t xritag_start = 0, lxri = 0;
13277         struct lpfc_rsrc_blks *rsrc_blk;
13278         int cnt, ttl_cnt, rc = 0;
13279         int loop_cnt;
13280         uint32_t shdr_status, shdr_add_status;
13281         union lpfc_sli4_cfg_shdr *shdr;
13282
13283         /* The number of sgls to be posted */
13284         els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
13285
13286         reqlen = els_xri_cnt * sizeof(struct sgl_page_pairs) +
13287                  sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
13288         if (reqlen > SLI4_PAGE_SIZE) {
13289                 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
13290                                 "2989 Block sgl registration required DMA "
13291                                 "size (%d) great than a page\n", reqlen);
13292                 return -ENOMEM;
13293         }
13294
13295         cnt = 0;
13296         ttl_cnt = 0;
13297         list_for_each_entry(rsrc_blk, &phba->sli4_hba.lpfc_xri_blk_list,
13298                             list) {
13299                 rsrc_start = rsrc_blk->rsrc_start;
13300                 rsrc_size = rsrc_blk->rsrc_size;
13301
13302                 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
13303                                 "3014 Working ELS Extent start %d, cnt %d\n",
13304                                 rsrc_start, rsrc_size);
13305
13306                 loop_cnt = min(els_xri_cnt, rsrc_size);
13307                 if (ttl_cnt + loop_cnt >= els_xri_cnt) {
13308                         loop_cnt = els_xri_cnt - ttl_cnt;
13309                         ttl_cnt = els_xri_cnt;
13310                 }
13311
13312                 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13313                 if (!mbox)
13314                         return -ENOMEM;
13315                 /*
13316                  * Allocate DMA memory and set up the non-embedded mailbox
13317                  * command.
13318                  */
13319                 alloclen = lpfc_sli4_config(phba, mbox,
13320                                         LPFC_MBOX_SUBSYSTEM_FCOE,
13321                                         LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
13322                                         reqlen, LPFC_SLI4_MBX_NEMBED);
13323                 if (alloclen < reqlen) {
13324                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13325                                         "2987 Allocated DMA memory size (%d) "
13326                                         "is less than the requested DMA memory "
13327                                         "size (%d)\n", alloclen, reqlen);
13328                         lpfc_sli4_mbox_cmd_free(phba, mbox);
13329                         return -ENOMEM;
13330                 }
13331
13332                 /* Set up the SGL pages in the non-embedded DMA pages */
13333                 viraddr = mbox->sge_array->addr[0];
13334                 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
13335                 sgl_pg_pairs = &sgl->sgl_pg_pairs;
13336
13337                 /*
13338                  * The starting resource may not begin at zero. Control
13339                  * the loop variants via the block resource parameters,
13340                  * but handle the sge pointers with a zero-based index
13341                  * that doesn't get reset per loop pass.
13342                  */
13343                 for (index = rsrc_start;
13344                      index < rsrc_start + loop_cnt;
13345                      index++) {
13346                         sglq_entry = phba->sli4_hba.lpfc_els_sgl_array[cnt];
13347
13348                         /*
13349                          * Assign the sglq a physical xri only if the driver
13350                          * has not initialized those resources.  A port reset
13351                          * only needs the sglq's posted.
13352                          */
13353                         if (bf_get(lpfc_xri_rsrc_rdy,
13354                                    &phba->sli4_hba.sli4_flags) !=
13355                                    LPFC_XRI_RSRC_RDY) {
13356                                 lxri = lpfc_sli4_next_xritag(phba);
13357                                 if (lxri == NO_XRI) {
13358                                         lpfc_sli4_mbox_cmd_free(phba, mbox);
13359                                         rc = -ENOMEM;
13360                                         goto err_exit;
13361                                 }
13362                                 sglq_entry->sli4_lxritag = lxri;
13363                                 sglq_entry->sli4_xritag =
13364                                                 phba->sli4_hba.xri_ids[lxri];
13365                         }
13366
13367                         /* Set up the sge entry */
13368                         sgl_pg_pairs->sgl_pg0_addr_lo =
13369                                 cpu_to_le32(putPaddrLow(sglq_entry->phys));
13370                         sgl_pg_pairs->sgl_pg0_addr_hi =
13371                                 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
13372                         sgl_pg_pairs->sgl_pg1_addr_lo =
13373                                 cpu_to_le32(putPaddrLow(0));
13374                         sgl_pg_pairs->sgl_pg1_addr_hi =
13375                                 cpu_to_le32(putPaddrHigh(0));
13376
13377                         /* Track the starting physical XRI for the mailbox. */
13378                         if (index == rsrc_start)
13379                                 xritag_start = sglq_entry->sli4_xritag;
13380                         sgl_pg_pairs++;
13381                         cnt++;
13382                 }
13383
13384                 /* Complete initialization and perform endian conversion. */
13385                 rsrc_blk->rsrc_used += loop_cnt;
13386                 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
13387                 bf_set(lpfc_post_sgl_pages_xricnt, sgl, loop_cnt);
13388                 sgl->word0 = cpu_to_le32(sgl->word0);
13389
13390                 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
13391                                 "3015 Post ELS Extent SGL, start %d, "
13392                                 "cnt %d, used %d\n",
13393                                 xritag_start, loop_cnt, rsrc_blk->rsrc_used);
13394                 if (!phba->sli4_hba.intr_enable)
13395                         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13396                 else {
13397                         mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
13398                         rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
13399                 }
13400                 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
13401                 shdr_status = bf_get(lpfc_mbox_hdr_status,
13402                                      &shdr->response);
13403                 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
13404                                          &shdr->response);
13405                 if (rc != MBX_TIMEOUT)
13406                         lpfc_sli4_mbox_cmd_free(phba, mbox);
13407                 if (shdr_status || shdr_add_status || rc) {
13408                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13409                                         "2988 POST_SGL_BLOCK mailbox "
13410                                         "command failed status x%x "
13411                                         "add_status x%x mbx status x%x\n",
13412                                         shdr_status, shdr_add_status, rc);
13413                         rc = -ENXIO;
13414                         goto err_exit;
13415                 }
13416                 if (ttl_cnt >= els_xri_cnt)
13417                         break;
13418         }
13419
13420  err_exit:
13421         if (rc == 0)
13422                 bf_set(lpfc_xri_rsrc_rdy, &phba->sli4_hba.sli4_flags,
13423                        LPFC_XRI_RSRC_RDY);
13424         return rc;
13425 }
13426
13427 /**
13428  * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
13429  * @phba: pointer to lpfc hba data structure.
13430  * @sblist: pointer to scsi buffer list.
13431  * @count: number of scsi buffers on the list.
13432  *
13433  * This routine is invoked to post a block of @count scsi sgl pages from a
13434  * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
13435  * No Lock is held.
13436  *
13437  **/
13438 int
13439 lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba, struct list_head *sblist,
13440                               int cnt)
13441 {
13442         struct lpfc_scsi_buf *psb;
13443         struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
13444         struct sgl_page_pairs *sgl_pg_pairs;
13445         void *viraddr;
13446         LPFC_MBOXQ_t *mbox;
13447         uint32_t reqlen, alloclen, pg_pairs;
13448         uint32_t mbox_tmo;
13449         uint16_t xritag_start = 0;
13450         int rc = 0;
13451         uint32_t shdr_status, shdr_add_status;
13452         dma_addr_t pdma_phys_bpl1;
13453         union lpfc_sli4_cfg_shdr *shdr;
13454
13455         /* Calculate the requested length of the dma memory */
13456         reqlen = cnt * sizeof(struct sgl_page_pairs) +
13457                  sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
13458         if (reqlen > SLI4_PAGE_SIZE) {
13459                 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
13460                                 "0217 Block sgl registration required DMA "
13461                                 "size (%d) great than a page\n", reqlen);
13462                 return -ENOMEM;
13463         }
13464         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13465         if (!mbox) {
13466                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13467                                 "0283 Failed to allocate mbox cmd memory\n");
13468                 return -ENOMEM;
13469         }
13470
13471         /* Allocate DMA memory and set up the non-embedded mailbox command */
13472         alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13473                                 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
13474                                 LPFC_SLI4_MBX_NEMBED);
13475
13476         if (alloclen < reqlen) {
13477                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13478                                 "2561 Allocated DMA memory size (%d) is "
13479                                 "less than the requested DMA memory "
13480                                 "size (%d)\n", alloclen, reqlen);
13481                 lpfc_sli4_mbox_cmd_free(phba, mbox);
13482                 return -ENOMEM;
13483         }
13484
13485         /* Get the first SGE entry from the non-embedded DMA memory */
13486         viraddr = mbox->sge_array->addr[0];
13487
13488         /* Set up the SGL pages in the non-embedded DMA pages */
13489         sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
13490         sgl_pg_pairs = &sgl->sgl_pg_pairs;
13491
13492         pg_pairs = 0;
13493         list_for_each_entry(psb, sblist, list) {
13494                 /* Set up the sge entry */
13495                 sgl_pg_pairs->sgl_pg0_addr_lo =
13496                         cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
13497                 sgl_pg_pairs->sgl_pg0_addr_hi =
13498                         cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
13499                 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
13500                         pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
13501                 else
13502                         pdma_phys_bpl1 = 0;
13503                 sgl_pg_pairs->sgl_pg1_addr_lo =
13504                         cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
13505                 sgl_pg_pairs->sgl_pg1_addr_hi =
13506                         cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
13507                 /* Keep the first xritag on the list */
13508                 if (pg_pairs == 0)
13509                         xritag_start = psb->cur_iocbq.sli4_xritag;
13510                 sgl_pg_pairs++;
13511                 pg_pairs++;
13512         }
13513         bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
13514         bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
13515         /* Perform endian conversion if necessary */
13516         sgl->word0 = cpu_to_le32(sgl->word0);
13517
13518         if (!phba->sli4_hba.intr_enable)
13519                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13520         else {
13521                 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
13522                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
13523         }
13524         shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
13525         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13526         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13527         if (rc != MBX_TIMEOUT)
13528                 lpfc_sli4_mbox_cmd_free(phba, mbox);
13529         if (shdr_status || shdr_add_status || rc) {
13530                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13531                                 "2564 POST_SGL_BLOCK mailbox command failed "
13532                                 "status x%x add_status x%x mbx status x%x\n",
13533                                 shdr_status, shdr_add_status, rc);
13534                 rc = -ENXIO;
13535         }
13536         return rc;
13537 }
13538
13539 /**
13540  * lpfc_sli4_post_scsi_sgl_blk_ext - post a block of scsi sgls to the port.
13541  * @phba: pointer to lpfc hba data structure.
13542  * @sblist: pointer to scsi buffer list.
13543  * @count: number of scsi buffers on the list.
13544  *
13545  * This routine is invoked to post a block of @count scsi sgl pages from a
13546  * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
13547  * No Lock is held.
13548  *
13549  **/
13550 int
13551 lpfc_sli4_post_scsi_sgl_blk_ext(struct lpfc_hba *phba, struct list_head *sblist,
13552                                 int cnt)
13553 {
13554         struct lpfc_scsi_buf *psb = NULL;
13555         struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
13556         struct sgl_page_pairs *sgl_pg_pairs;
13557         void *viraddr;
13558         LPFC_MBOXQ_t *mbox;
13559         uint32_t reqlen, alloclen, pg_pairs;
13560         uint32_t mbox_tmo;
13561         uint16_t xri_start = 0, scsi_xri_start;
13562         uint16_t rsrc_range;
13563         int rc = 0, avail_cnt;
13564         uint32_t shdr_status, shdr_add_status;
13565         dma_addr_t pdma_phys_bpl1;
13566         union lpfc_sli4_cfg_shdr *shdr;
13567         struct lpfc_rsrc_blks *rsrc_blk;
13568         uint32_t xri_cnt = 0;
13569
13570         /* Calculate the total requested length of the dma memory */
13571         reqlen = cnt * sizeof(struct sgl_page_pairs) +
13572                  sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
13573         if (reqlen > SLI4_PAGE_SIZE) {
13574                 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
13575                                 "2932 Block sgl registration required DMA "
13576                                 "size (%d) great than a page\n", reqlen);
13577                 return -ENOMEM;
13578         }
13579
13580         /*
13581          * The use of extents requires the driver to post the sgl headers
13582          * in multiple postings to meet the contiguous resource assignment.
13583          */
13584         psb = list_prepare_entry(psb, sblist, list);
13585         scsi_xri_start = phba->sli4_hba.scsi_xri_start;
13586         list_for_each_entry(rsrc_blk, &phba->sli4_hba.lpfc_xri_blk_list,
13587                             list) {
13588                 rsrc_range = rsrc_blk->rsrc_start + rsrc_blk->rsrc_size;
13589                 if (rsrc_range < scsi_xri_start)
13590                         continue;
13591                 else if (rsrc_blk->rsrc_used >= rsrc_blk->rsrc_size)
13592                         continue;
13593                 else
13594                         avail_cnt = rsrc_blk->rsrc_size - rsrc_blk->rsrc_used;
13595
13596                 reqlen = (avail_cnt * sizeof(struct sgl_page_pairs)) +
13597                         sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
13598                 /*
13599                  * Allocate DMA memory and set up the non-embedded mailbox
13600                  * command. The mbox is used to post an SGL page per loop
13601                  * but the DMA memory has a use-once semantic so the mailbox
13602                  * is used and freed per loop pass.
13603                  */
13604                 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13605                 if (!mbox) {
13606                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13607                                         "2933 Failed to allocate mbox cmd "
13608                                         "memory\n");
13609                         return -ENOMEM;
13610                 }
13611                 alloclen = lpfc_sli4_config(phba, mbox,
13612                                         LPFC_MBOX_SUBSYSTEM_FCOE,
13613                                         LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
13614                                         reqlen,
13615                                         LPFC_SLI4_MBX_NEMBED);
13616                 if (alloclen < reqlen) {
13617                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13618                                         "2934 Allocated DMA memory size (%d) "
13619                                         "is less than the requested DMA memory "
13620                                         "size (%d)\n", alloclen, reqlen);
13621                         lpfc_sli4_mbox_cmd_free(phba, mbox);
13622                         return -ENOMEM;
13623                 }
13624
13625                 /* Get the first SGE entry from the non-embedded DMA memory */
13626                 viraddr = mbox->sge_array->addr[0];
13627
13628                 /* Set up the SGL pages in the non-embedded DMA pages */
13629                 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
13630                 sgl_pg_pairs = &sgl->sgl_pg_pairs;
13631
13632                 /* pg_pairs tracks posted SGEs per loop iteration. */
13633                 pg_pairs = 0;
13634                 list_for_each_entry_continue(psb, sblist, list) {
13635                         /* Set up the sge entry */
13636                         sgl_pg_pairs->sgl_pg0_addr_lo =
13637                                 cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
13638                         sgl_pg_pairs->sgl_pg0_addr_hi =
13639                                 cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
13640                         if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
13641                                 pdma_phys_bpl1 = psb->dma_phys_bpl +
13642                                         SGL_PAGE_SIZE;
13643                         else
13644                                 pdma_phys_bpl1 = 0;
13645                         sgl_pg_pairs->sgl_pg1_addr_lo =
13646                                 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
13647                         sgl_pg_pairs->sgl_pg1_addr_hi =
13648                                 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
13649                         /* Keep the first xri for this extent. */
13650                         if (pg_pairs == 0)
13651                                 xri_start = psb->cur_iocbq.sli4_xritag;
13652                         sgl_pg_pairs++;
13653                         pg_pairs++;
13654                         xri_cnt++;
13655
13656                         /*
13657                          * Track two exit conditions - the loop has constructed
13658                          * all of the caller's SGE pairs or all available
13659                          * resource IDs in this extent are consumed.
13660                          */
13661                         if ((xri_cnt == cnt) || (pg_pairs >= avail_cnt))
13662                                 break;
13663                 }
13664                 rsrc_blk->rsrc_used += pg_pairs;
13665                 bf_set(lpfc_post_sgl_pages_xri, sgl, xri_start);
13666                 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
13667
13668                 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
13669                                 "3016 Post SCSI Extent SGL, start %d, cnt %d "
13670                                 "blk use %d\n",
13671                                 xri_start, pg_pairs, rsrc_blk->rsrc_used);
13672                 /* Perform endian conversion if necessary */
13673                 sgl->word0 = cpu_to_le32(sgl->word0);
13674                 if (!phba->sli4_hba.intr_enable)
13675                         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13676                 else {
13677                         mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
13678                         rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
13679                 }
13680                 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
13681                 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13682                 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
13683                                          &shdr->response);
13684                 if (rc != MBX_TIMEOUT)
13685                         lpfc_sli4_mbox_cmd_free(phba, mbox);
13686                 if (shdr_status || shdr_add_status || rc) {
13687                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13688                                         "2935 POST_SGL_BLOCK mailbox command "
13689                                         "failed status x%x add_status x%x "
13690                                         "mbx status x%x\n",
13691                                         shdr_status, shdr_add_status, rc);
13692                         return -ENXIO;
13693                 }
13694
13695                 /* Post only what is requested. */
13696                 if (xri_cnt >= cnt)
13697                         break;
13698         }
13699         return rc;
13700 }
13701
13702 /**
13703  * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
13704  * @phba: pointer to lpfc_hba struct that the frame was received on
13705  * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
13706  *
13707  * This function checks the fields in the @fc_hdr to see if the FC frame is a
13708  * valid type of frame that the LPFC driver will handle. This function will
13709  * return a zero if the frame is a valid frame or a non zero value when the
13710  * frame does not pass the check.
13711  **/
13712 static int
13713 lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
13714 {
13715         /*  make rctl_names static to save stack space */
13716         static char *rctl_names[] = FC_RCTL_NAMES_INIT;
13717         char *type_names[] = FC_TYPE_NAMES_INIT;
13718         struct fc_vft_header *fc_vft_hdr;
13719         uint32_t *header = (uint32_t *) fc_hdr;
13720
13721         switch (fc_hdr->fh_r_ctl) {
13722         case FC_RCTL_DD_UNCAT:          /* uncategorized information */
13723         case FC_RCTL_DD_SOL_DATA:       /* solicited data */
13724         case FC_RCTL_DD_UNSOL_CTL:      /* unsolicited control */
13725         case FC_RCTL_DD_SOL_CTL:        /* solicited control or reply */
13726         case FC_RCTL_DD_UNSOL_DATA:     /* unsolicited data */
13727         case FC_RCTL_DD_DATA_DESC:      /* data descriptor */
13728         case FC_RCTL_DD_UNSOL_CMD:      /* unsolicited command */
13729         case FC_RCTL_DD_CMD_STATUS:     /* command status */
13730         case FC_RCTL_ELS_REQ:   /* extended link services request */
13731         case FC_RCTL_ELS_REP:   /* extended link services reply */
13732         case FC_RCTL_ELS4_REQ:  /* FC-4 ELS request */
13733         case FC_RCTL_ELS4_REP:  /* FC-4 ELS reply */
13734         case FC_RCTL_BA_NOP:    /* basic link service NOP */
13735         case FC_RCTL_BA_ABTS:   /* basic link service abort */
13736         case FC_RCTL_BA_RMC:    /* remove connection */
13737         case FC_RCTL_BA_ACC:    /* basic accept */
13738         case FC_RCTL_BA_RJT:    /* basic reject */
13739         case FC_RCTL_BA_PRMT:
13740         case FC_RCTL_ACK_1:     /* acknowledge_1 */
13741         case FC_RCTL_ACK_0:     /* acknowledge_0 */
13742         case FC_RCTL_P_RJT:     /* port reject */
13743         case FC_RCTL_F_RJT:     /* fabric reject */
13744         case FC_RCTL_P_BSY:     /* port busy */
13745         case FC_RCTL_F_BSY:     /* fabric busy to data frame */
13746         case FC_RCTL_F_BSYL:    /* fabric busy to link control frame */
13747         case FC_RCTL_LCR:       /* link credit reset */
13748         case FC_RCTL_END:       /* end */
13749                 break;
13750         case FC_RCTL_VFTH:      /* Virtual Fabric tagging Header */
13751                 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
13752                 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
13753                 return lpfc_fc_frame_check(phba, fc_hdr);
13754         default:
13755                 goto drop;
13756         }
13757         switch (fc_hdr->fh_type) {
13758         case FC_TYPE_BLS:
13759         case FC_TYPE_ELS:
13760         case FC_TYPE_FCP:
13761         case FC_TYPE_CT:
13762                 break;
13763         case FC_TYPE_IP:
13764         case FC_TYPE_ILS:
13765         default:
13766                 goto drop;
13767         }
13768
13769         lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
13770                         "2538 Received frame rctl:%s type:%s "
13771                         "Frame Data:%08x %08x %08x %08x %08x %08x\n",
13772                         rctl_names[fc_hdr->fh_r_ctl],
13773                         type_names[fc_hdr->fh_type],
13774                         be32_to_cpu(header[0]), be32_to_cpu(header[1]),
13775                         be32_to_cpu(header[2]), be32_to_cpu(header[3]),
13776                         be32_to_cpu(header[4]), be32_to_cpu(header[5]));
13777         return 0;
13778 drop:
13779         lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
13780                         "2539 Dropped frame rctl:%s type:%s\n",
13781                         rctl_names[fc_hdr->fh_r_ctl],
13782                         type_names[fc_hdr->fh_type]);
13783         return 1;
13784 }
13785
13786 /**
13787  * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
13788  * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
13789  *
13790  * This function processes the FC header to retrieve the VFI from the VF
13791  * header, if one exists. This function will return the VFI if one exists
13792  * or 0 if no VSAN Header exists.
13793  **/
13794 static uint32_t
13795 lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
13796 {
13797         struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
13798
13799         if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
13800                 return 0;
13801         return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
13802 }
13803
13804 /**
13805  * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
13806  * @phba: Pointer to the HBA structure to search for the vport on
13807  * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
13808  * @fcfi: The FC Fabric ID that the frame came from
13809  *
13810  * This function searches the @phba for a vport that matches the content of the
13811  * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
13812  * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
13813  * returns the matching vport pointer or NULL if unable to match frame to a
13814  * vport.
13815  **/
13816 static struct lpfc_vport *
13817 lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
13818                        uint16_t fcfi)
13819 {
13820         struct lpfc_vport **vports;
13821         struct lpfc_vport *vport = NULL;
13822         int i;
13823         uint32_t did = (fc_hdr->fh_d_id[0] << 16 |
13824                         fc_hdr->fh_d_id[1] << 8 |
13825                         fc_hdr->fh_d_id[2]);
13826         if (did == Fabric_DID)
13827                 return phba->pport;
13828         vports = lpfc_create_vport_work_array(phba);
13829         if (vports != NULL)
13830                 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
13831                         if (phba->fcf.fcfi == fcfi &&
13832                             vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
13833                             vports[i]->fc_myDID == did) {
13834                                 vport = vports[i];
13835                                 break;
13836                         }
13837                 }
13838         lpfc_destroy_vport_work_array(phba, vports);
13839         return vport;
13840 }
13841
13842 /**
13843  * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
13844  * @vport: The vport to work on.
13845  *
13846  * This function updates the receive sequence time stamp for this vport. The
13847  * receive sequence time stamp indicates the time that the last frame of the
13848  * the sequence that has been idle for the longest amount of time was received.
13849  * the driver uses this time stamp to indicate if any received sequences have
13850  * timed out.
13851  **/
13852 void
13853 lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
13854 {
13855         struct lpfc_dmabuf *h_buf;
13856         struct hbq_dmabuf *dmabuf = NULL;
13857
13858         /* get the oldest sequence on the rcv list */
13859         h_buf = list_get_first(&vport->rcv_buffer_list,
13860                                struct lpfc_dmabuf, list);
13861         if (!h_buf)
13862                 return;
13863         dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
13864         vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
13865 }
13866
13867 /**
13868  * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
13869  * @vport: The vport that the received sequences were sent to.
13870  *
13871  * This function cleans up all outstanding received sequences. This is called
13872  * by the driver when a link event or user action invalidates all the received
13873  * sequences.
13874  **/
13875 void
13876 lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
13877 {
13878         struct lpfc_dmabuf *h_buf, *hnext;
13879         struct lpfc_dmabuf *d_buf, *dnext;
13880         struct hbq_dmabuf *dmabuf = NULL;
13881
13882         /* start with the oldest sequence on the rcv list */
13883         list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
13884                 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
13885                 list_del_init(&dmabuf->hbuf.list);
13886                 list_for_each_entry_safe(d_buf, dnext,
13887                                          &dmabuf->dbuf.list, list) {
13888                         list_del_init(&d_buf->list);
13889                         lpfc_in_buf_free(vport->phba, d_buf);
13890                 }
13891                 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
13892         }
13893 }
13894
13895 /**
13896  * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
13897  * @vport: The vport that the received sequences were sent to.
13898  *
13899  * This function determines whether any received sequences have timed out by
13900  * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
13901  * indicates that there is at least one timed out sequence this routine will
13902  * go through the received sequences one at a time from most inactive to most
13903  * active to determine which ones need to be cleaned up. Once it has determined
13904  * that a sequence needs to be cleaned up it will simply free up the resources
13905  * without sending an abort.
13906  **/
13907 void
13908 lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
13909 {
13910         struct lpfc_dmabuf *h_buf, *hnext;
13911         struct lpfc_dmabuf *d_buf, *dnext;
13912         struct hbq_dmabuf *dmabuf = NULL;
13913         unsigned long timeout;
13914         int abort_count = 0;
13915
13916         timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
13917                    vport->rcv_buffer_time_stamp);
13918         if (list_empty(&vport->rcv_buffer_list) ||
13919             time_before(jiffies, timeout))
13920                 return;
13921         /* start with the oldest sequence on the rcv list */
13922         list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
13923                 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
13924                 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
13925                            dmabuf->time_stamp);
13926                 if (time_before(jiffies, timeout))
13927                         break;
13928                 abort_count++;
13929                 list_del_init(&dmabuf->hbuf.list);
13930                 list_for_each_entry_safe(d_buf, dnext,
13931                                          &dmabuf->dbuf.list, list) {
13932                         list_del_init(&d_buf->list);
13933                         lpfc_in_buf_free(vport->phba, d_buf);
13934                 }
13935                 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
13936         }
13937         if (abort_count)
13938                 lpfc_update_rcv_time_stamp(vport);
13939 }
13940
13941 /**
13942  * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
13943  * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
13944  *
13945  * This function searches through the existing incomplete sequences that have
13946  * been sent to this @vport. If the frame matches one of the incomplete
13947  * sequences then the dbuf in the @dmabuf is added to the list of frames that
13948  * make up that sequence. If no sequence is found that matches this frame then
13949  * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
13950  * This function returns a pointer to the first dmabuf in the sequence list that
13951  * the frame was linked to.
13952  **/
13953 static struct hbq_dmabuf *
13954 lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
13955 {
13956         struct fc_frame_header *new_hdr;
13957         struct fc_frame_header *temp_hdr;
13958         struct lpfc_dmabuf *d_buf;
13959         struct lpfc_dmabuf *h_buf;
13960         struct hbq_dmabuf *seq_dmabuf = NULL;
13961         struct hbq_dmabuf *temp_dmabuf = NULL;
13962
13963         INIT_LIST_HEAD(&dmabuf->dbuf.list);
13964         dmabuf->time_stamp = jiffies;
13965         new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
13966         /* Use the hdr_buf to find the sequence that this frame belongs to */
13967         list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
13968                 temp_hdr = (struct fc_frame_header *)h_buf->virt;
13969                 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
13970                     (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
13971                     (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
13972                         continue;
13973                 /* found a pending sequence that matches this frame */
13974                 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
13975                 break;
13976         }
13977         if (!seq_dmabuf) {
13978                 /*
13979                  * This indicates first frame received for this sequence.
13980                  * Queue the buffer on the vport's rcv_buffer_list.
13981                  */
13982                 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
13983                 lpfc_update_rcv_time_stamp(vport);
13984                 return dmabuf;
13985         }
13986         temp_hdr = seq_dmabuf->hbuf.virt;
13987         if (be16_to_cpu(new_hdr->fh_seq_cnt) <
13988                 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
13989                 list_del_init(&seq_dmabuf->hbuf.list);
13990                 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
13991                 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
13992                 lpfc_update_rcv_time_stamp(vport);
13993                 return dmabuf;
13994         }
13995         /* move this sequence to the tail to indicate a young sequence */
13996         list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
13997         seq_dmabuf->time_stamp = jiffies;
13998         lpfc_update_rcv_time_stamp(vport);
13999         if (list_empty(&seq_dmabuf->dbuf.list)) {
14000                 temp_hdr = dmabuf->hbuf.virt;
14001                 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
14002                 return seq_dmabuf;
14003         }
14004         /* find the correct place in the sequence to insert this frame */
14005         list_for_each_entry_reverse(d_buf, &seq_dmabuf->dbuf.list, list) {
14006                 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
14007                 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
14008                 /*
14009                  * If the frame's sequence count is greater than the frame on
14010                  * the list then insert the frame right after this frame
14011                  */
14012                 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
14013                         be16_to_cpu(temp_hdr->fh_seq_cnt)) {
14014                         list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
14015                         return seq_dmabuf;
14016                 }
14017         }
14018         return NULL;
14019 }
14020
14021 /**
14022  * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
14023  * @vport: pointer to a vitural port
14024  * @dmabuf: pointer to a dmabuf that describes the FC sequence
14025  *
14026  * This function tries to abort from the partially assembed sequence, described
14027  * by the information from basic abbort @dmabuf. It checks to see whether such
14028  * partially assembled sequence held by the driver. If so, it shall free up all
14029  * the frames from the partially assembled sequence.
14030  *
14031  * Return
14032  * true  -- if there is matching partially assembled sequence present and all
14033  *          the frames freed with the sequence;
14034  * false -- if there is no matching partially assembled sequence present so
14035  *          nothing got aborted in the lower layer driver
14036  **/
14037 static bool
14038 lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
14039                             struct hbq_dmabuf *dmabuf)
14040 {
14041         struct fc_frame_header *new_hdr;
14042         struct fc_frame_header *temp_hdr;
14043         struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
14044         struct hbq_dmabuf *seq_dmabuf = NULL;
14045
14046         /* Use the hdr_buf to find the sequence that matches this frame */
14047         INIT_LIST_HEAD(&dmabuf->dbuf.list);
14048         INIT_LIST_HEAD(&dmabuf->hbuf.list);
14049         new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
14050         list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
14051                 temp_hdr = (struct fc_frame_header *)h_buf->virt;
14052                 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
14053                     (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
14054                     (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
14055                         continue;
14056                 /* found a pending sequence that matches this frame */
14057                 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
14058                 break;
14059         }
14060
14061         /* Free up all the frames from the partially assembled sequence */
14062         if (seq_dmabuf) {
14063                 list_for_each_entry_safe(d_buf, n_buf,
14064                                          &seq_dmabuf->dbuf.list, list) {
14065                         list_del_init(&d_buf->list);
14066                         lpfc_in_buf_free(vport->phba, d_buf);
14067                 }
14068                 return true;
14069         }
14070         return false;
14071 }
14072
14073 /**
14074  * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
14075  * @phba: Pointer to HBA context object.
14076  * @cmd_iocbq: pointer to the command iocbq structure.
14077  * @rsp_iocbq: pointer to the response iocbq structure.
14078  *
14079  * This function handles the sequence abort response iocb command complete
14080  * event. It properly releases the memory allocated to the sequence abort
14081  * accept iocb.
14082  **/
14083 static void
14084 lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
14085                              struct lpfc_iocbq *cmd_iocbq,
14086                              struct lpfc_iocbq *rsp_iocbq)
14087 {
14088         if (cmd_iocbq)
14089                 lpfc_sli_release_iocbq(phba, cmd_iocbq);
14090
14091         /* Failure means BLS ABORT RSP did not get delivered to remote node*/
14092         if (rsp_iocbq && rsp_iocbq->iocb.ulpStatus)
14093                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14094                         "3154 BLS ABORT RSP failed, data:  x%x/x%x\n",
14095                         rsp_iocbq->iocb.ulpStatus,
14096                         rsp_iocbq->iocb.un.ulpWord[4]);
14097 }
14098
14099 /**
14100  * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
14101  * @phba: Pointer to HBA context object.
14102  * @xri: xri id in transaction.
14103  *
14104  * This function validates the xri maps to the known range of XRIs allocated an
14105  * used by the driver.
14106  **/
14107 uint16_t
14108 lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
14109                       uint16_t xri)
14110 {
14111         int i;
14112
14113         for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
14114                 if (xri == phba->sli4_hba.xri_ids[i])
14115                         return i;
14116         }
14117         return NO_XRI;
14118 }
14119
14120
14121 /**
14122  * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
14123  * @phba: Pointer to HBA context object.
14124  * @fc_hdr: pointer to a FC frame header.
14125  *
14126  * This function sends a basic response to a previous unsol sequence abort
14127  * event after aborting the sequence handling.
14128  **/
14129 static void
14130 lpfc_sli4_seq_abort_rsp(struct lpfc_hba *phba,
14131                         struct fc_frame_header *fc_hdr)
14132 {
14133         struct lpfc_iocbq *ctiocb = NULL;
14134         struct lpfc_nodelist *ndlp;
14135         uint16_t oxid, rxid;
14136         uint32_t sid, fctl;
14137         IOCB_t *icmd;
14138         int rc;
14139
14140         if (!lpfc_is_link_up(phba))
14141                 return;
14142
14143         sid = sli4_sid_from_fc_hdr(fc_hdr);
14144         oxid = be16_to_cpu(fc_hdr->fh_ox_id);
14145         rxid = be16_to_cpu(fc_hdr->fh_rx_id);
14146
14147         ndlp = lpfc_findnode_did(phba->pport, sid);
14148         if (!ndlp) {
14149                 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
14150                                 "1268 Find ndlp returned NULL for oxid:x%x "
14151                                 "SID:x%x\n", oxid, sid);
14152                 return;
14153         }
14154         if (lpfc_sli4_xri_inrange(phba, rxid))
14155                 lpfc_set_rrq_active(phba, ndlp, rxid, oxid, 0);
14156
14157         /* Allocate buffer for rsp iocb */
14158         ctiocb = lpfc_sli_get_iocbq(phba);
14159         if (!ctiocb)
14160                 return;
14161
14162         /* Extract the F_CTL field from FC_HDR */
14163         fctl = sli4_fctl_from_fc_hdr(fc_hdr);
14164
14165         icmd = &ctiocb->iocb;
14166         icmd->un.xseq64.bdl.bdeSize = 0;
14167         icmd->un.xseq64.bdl.ulpIoTag32 = 0;
14168         icmd->un.xseq64.w5.hcsw.Dfctl = 0;
14169         icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
14170         icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
14171
14172         /* Fill in the rest of iocb fields */
14173         icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
14174         icmd->ulpBdeCount = 0;
14175         icmd->ulpLe = 1;
14176         icmd->ulpClass = CLASS3;
14177         icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
14178         ctiocb->context1 = ndlp;
14179
14180         ctiocb->iocb_cmpl = NULL;
14181         ctiocb->vport = phba->pport;
14182         ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
14183         ctiocb->sli4_lxritag = NO_XRI;
14184         ctiocb->sli4_xritag = NO_XRI;
14185
14186         /* If the oxid maps to the FCP XRI range or if it is out of range,
14187          * send a BLS_RJT.  The driver no longer has that exchange.
14188          * Override the IOCB for a BA_RJT.
14189          */
14190         if (oxid > (phba->sli4_hba.max_cfg_param.max_xri +
14191                     phba->sli4_hba.max_cfg_param.xri_base) ||
14192             oxid > (lpfc_sli4_get_els_iocb_cnt(phba) +
14193                     phba->sli4_hba.max_cfg_param.xri_base)) {
14194                 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
14195                 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
14196                 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
14197                 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
14198         }
14199
14200         if (fctl & FC_FC_EX_CTX) {
14201                 /* ABTS sent by responder to CT exchange, construction
14202                  * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
14203                  * field and RX_ID from ABTS for RX_ID field.
14204                  */
14205                 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
14206                 bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
14207         } else {
14208                 /* ABTS sent by initiator to CT exchange, construction
14209                  * of BA_ACC will need to allocate a new XRI as for the
14210                  * XRI_TAG and RX_ID fields.
14211                  */
14212                 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
14213                 bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, NO_XRI);
14214         }
14215         bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
14216
14217         /* Xmit CT abts response on exchange <xid> */
14218         lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
14219                         "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
14220                         icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
14221
14222         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
14223         if (rc == IOCB_ERROR) {
14224                 lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
14225                                 "2925 Failed to issue CT ABTS RSP x%x on "
14226                                 "xri x%x, Data x%x\n",
14227                                 icmd->un.xseq64.w5.hcsw.Rctl, oxid,
14228                                 phba->link_state);
14229                 lpfc_sli_release_iocbq(phba, ctiocb);
14230         }
14231 }
14232
14233 /**
14234  * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
14235  * @vport: Pointer to the vport on which this sequence was received
14236  * @dmabuf: pointer to a dmabuf that describes the FC sequence
14237  *
14238  * This function handles an SLI-4 unsolicited abort event. If the unsolicited
14239  * receive sequence is only partially assembed by the driver, it shall abort
14240  * the partially assembled frames for the sequence. Otherwise, if the
14241  * unsolicited receive sequence has been completely assembled and passed to
14242  * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
14243  * unsolicited sequence has been aborted. After that, it will issue a basic
14244  * accept to accept the abort.
14245  **/
14246 void
14247 lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
14248                              struct hbq_dmabuf *dmabuf)
14249 {
14250         struct lpfc_hba *phba = vport->phba;
14251         struct fc_frame_header fc_hdr;
14252         uint32_t fctl;
14253         bool abts_par;
14254
14255         /* Make a copy of fc_hdr before the dmabuf being released */
14256         memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
14257         fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
14258
14259         if (fctl & FC_FC_EX_CTX) {
14260                 /*
14261                  * ABTS sent by responder to exchange, just free the buffer
14262                  */
14263                 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14264         } else {
14265                 /*
14266                  * ABTS sent by initiator to exchange, need to do cleanup
14267                  */
14268                 /* Try to abort partially assembled seq */
14269                 abts_par = lpfc_sli4_abort_partial_seq(vport, dmabuf);
14270
14271                 /* Send abort to ULP if partially seq abort failed */
14272                 if (abts_par == false)
14273                         lpfc_sli4_send_seq_to_ulp(vport, dmabuf);
14274                 else
14275                         lpfc_in_buf_free(phba, &dmabuf->dbuf);
14276         }
14277         /* Send basic accept (BA_ACC) to the abort requester */
14278         lpfc_sli4_seq_abort_rsp(phba, &fc_hdr);
14279 }
14280
14281 /**
14282  * lpfc_seq_complete - Indicates if a sequence is complete
14283  * @dmabuf: pointer to a dmabuf that describes the FC sequence
14284  *
14285  * This function checks the sequence, starting with the frame described by
14286  * @dmabuf, to see if all the frames associated with this sequence are present.
14287  * the frames associated with this sequence are linked to the @dmabuf using the
14288  * dbuf list. This function looks for two major things. 1) That the first frame
14289  * has a sequence count of zero. 2) There is a frame with last frame of sequence
14290  * set. 3) That there are no holes in the sequence count. The function will
14291  * return 1 when the sequence is complete, otherwise it will return 0.
14292  **/
14293 static int
14294 lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
14295 {
14296         struct fc_frame_header *hdr;
14297         struct lpfc_dmabuf *d_buf;
14298         struct hbq_dmabuf *seq_dmabuf;
14299         uint32_t fctl;
14300         int seq_count = 0;
14301
14302         hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
14303         /* make sure first fame of sequence has a sequence count of zero */
14304         if (hdr->fh_seq_cnt != seq_count)
14305                 return 0;
14306         fctl = (hdr->fh_f_ctl[0] << 16 |
14307                 hdr->fh_f_ctl[1] << 8 |
14308                 hdr->fh_f_ctl[2]);
14309         /* If last frame of sequence we can return success. */
14310         if (fctl & FC_FC_END_SEQ)
14311                 return 1;
14312         list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
14313                 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
14314                 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
14315                 /* If there is a hole in the sequence count then fail. */
14316                 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
14317                         return 0;
14318                 fctl = (hdr->fh_f_ctl[0] << 16 |
14319                         hdr->fh_f_ctl[1] << 8 |
14320                         hdr->fh_f_ctl[2]);
14321                 /* If last frame of sequence we can return success. */
14322                 if (fctl & FC_FC_END_SEQ)
14323                         return 1;
14324         }
14325         return 0;
14326 }
14327
14328 /**
14329  * lpfc_prep_seq - Prep sequence for ULP processing
14330  * @vport: Pointer to the vport on which this sequence was received
14331  * @dmabuf: pointer to a dmabuf that describes the FC sequence
14332  *
14333  * This function takes a sequence, described by a list of frames, and creates
14334  * a list of iocbq structures to describe the sequence. This iocbq list will be
14335  * used to issue to the generic unsolicited sequence handler. This routine
14336  * returns a pointer to the first iocbq in the list. If the function is unable
14337  * to allocate an iocbq then it throw out the received frames that were not
14338  * able to be described and return a pointer to the first iocbq. If unable to
14339  * allocate any iocbqs (including the first) this function will return NULL.
14340  **/
14341 static struct lpfc_iocbq *
14342 lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
14343 {
14344         struct hbq_dmabuf *hbq_buf;
14345         struct lpfc_dmabuf *d_buf, *n_buf;
14346         struct lpfc_iocbq *first_iocbq, *iocbq;
14347         struct fc_frame_header *fc_hdr;
14348         uint32_t sid;
14349         uint32_t len, tot_len;
14350         struct ulp_bde64 *pbde;
14351
14352         fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
14353         /* remove from receive buffer list */
14354         list_del_init(&seq_dmabuf->hbuf.list);
14355         lpfc_update_rcv_time_stamp(vport);
14356         /* get the Remote Port's SID */
14357         sid = sli4_sid_from_fc_hdr(fc_hdr);
14358         tot_len = 0;
14359         /* Get an iocbq struct to fill in. */
14360         first_iocbq = lpfc_sli_get_iocbq(vport->phba);
14361         if (first_iocbq) {
14362                 /* Initialize the first IOCB. */
14363                 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
14364                 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
14365                 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
14366                 first_iocbq->iocb.ulpContext = NO_XRI;
14367                 first_iocbq->iocb.unsli3.rcvsli3.ox_id =
14368                         be16_to_cpu(fc_hdr->fh_ox_id);
14369                 /* iocbq is prepped for internal consumption.  Physical vpi. */
14370                 first_iocbq->iocb.unsli3.rcvsli3.vpi =
14371                         vport->phba->vpi_ids[vport->vpi];
14372                 /* put the first buffer into the first IOCBq */
14373                 first_iocbq->context2 = &seq_dmabuf->dbuf;
14374                 first_iocbq->context3 = NULL;
14375                 first_iocbq->iocb.ulpBdeCount = 1;
14376                 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
14377                                                         LPFC_DATA_BUF_SIZE;
14378                 first_iocbq->iocb.un.rcvels.remoteID = sid;
14379                 tot_len = bf_get(lpfc_rcqe_length,
14380                                        &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
14381                 first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
14382         }
14383         iocbq = first_iocbq;
14384         /*
14385          * Each IOCBq can have two Buffers assigned, so go through the list
14386          * of buffers for this sequence and save two buffers in each IOCBq
14387          */
14388         list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
14389                 if (!iocbq) {
14390                         lpfc_in_buf_free(vport->phba, d_buf);
14391                         continue;
14392                 }
14393                 if (!iocbq->context3) {
14394                         iocbq->context3 = d_buf;
14395                         iocbq->iocb.ulpBdeCount++;
14396                         pbde = (struct ulp_bde64 *)
14397                                         &iocbq->iocb.unsli3.sli3Words[4];
14398                         pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
14399
14400                         /* We need to get the size out of the right CQE */
14401                         hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
14402                         len = bf_get(lpfc_rcqe_length,
14403                                        &hbq_buf->cq_event.cqe.rcqe_cmpl);
14404                         iocbq->iocb.unsli3.rcvsli3.acc_len += len;
14405                         tot_len += len;
14406                 } else {
14407                         iocbq = lpfc_sli_get_iocbq(vport->phba);
14408                         if (!iocbq) {
14409                                 if (first_iocbq) {
14410                                         first_iocbq->iocb.ulpStatus =
14411                                                         IOSTAT_FCP_RSP_ERROR;
14412                                         first_iocbq->iocb.un.ulpWord[4] =
14413                                                         IOERR_NO_RESOURCES;
14414                                 }
14415                                 lpfc_in_buf_free(vport->phba, d_buf);
14416                                 continue;
14417                         }
14418                         iocbq->context2 = d_buf;
14419                         iocbq->context3 = NULL;
14420                         iocbq->iocb.ulpBdeCount = 1;
14421                         iocbq->iocb.un.cont64[0].tus.f.bdeSize =
14422                                                         LPFC_DATA_BUF_SIZE;
14423
14424                         /* We need to get the size out of the right CQE */
14425                         hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
14426                         len = bf_get(lpfc_rcqe_length,
14427                                        &hbq_buf->cq_event.cqe.rcqe_cmpl);
14428                         tot_len += len;
14429                         iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
14430
14431                         iocbq->iocb.un.rcvels.remoteID = sid;
14432                         list_add_tail(&iocbq->list, &first_iocbq->list);
14433                 }
14434         }
14435         return first_iocbq;
14436 }
14437
14438 static void
14439 lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
14440                           struct hbq_dmabuf *seq_dmabuf)
14441 {
14442         struct fc_frame_header *fc_hdr;
14443         struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
14444         struct lpfc_hba *phba = vport->phba;
14445
14446         fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
14447         iocbq = lpfc_prep_seq(vport, seq_dmabuf);
14448         if (!iocbq) {
14449                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14450                                 "2707 Ring %d handler: Failed to allocate "
14451                                 "iocb Rctl x%x Type x%x received\n",
14452                                 LPFC_ELS_RING,
14453                                 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
14454                 return;
14455         }
14456         if (!lpfc_complete_unsol_iocb(phba,
14457                                       &phba->sli.ring[LPFC_ELS_RING],
14458                                       iocbq, fc_hdr->fh_r_ctl,
14459                                       fc_hdr->fh_type))
14460                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14461                                 "2540 Ring %d handler: unexpected Rctl "
14462                                 "x%x Type x%x received\n",
14463                                 LPFC_ELS_RING,
14464                                 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
14465
14466         /* Free iocb created in lpfc_prep_seq */
14467         list_for_each_entry_safe(curr_iocb, next_iocb,
14468                 &iocbq->list, list) {
14469                 list_del_init(&curr_iocb->list);
14470                 lpfc_sli_release_iocbq(phba, curr_iocb);
14471         }
14472         lpfc_sli_release_iocbq(phba, iocbq);
14473 }
14474
14475 /**
14476  * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
14477  * @phba: Pointer to HBA context object.
14478  *
14479  * This function is called with no lock held. This function processes all
14480  * the received buffers and gives it to upper layers when a received buffer
14481  * indicates that it is the final frame in the sequence. The interrupt
14482  * service routine processes received buffers at interrupt contexts and adds
14483  * received dma buffers to the rb_pend_list queue and signals the worker thread.
14484  * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
14485  * appropriate receive function when the final frame in a sequence is received.
14486  **/
14487 void
14488 lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
14489                                  struct hbq_dmabuf *dmabuf)
14490 {
14491         struct hbq_dmabuf *seq_dmabuf;
14492         struct fc_frame_header *fc_hdr;
14493         struct lpfc_vport *vport;
14494         uint32_t fcfi;
14495
14496         /* Process each received buffer */
14497         fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
14498         /* check to see if this a valid type of frame */
14499         if (lpfc_fc_frame_check(phba, fc_hdr)) {
14500                 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14501                 return;
14502         }
14503         if ((bf_get(lpfc_cqe_code,
14504                     &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
14505                 fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
14506                               &dmabuf->cq_event.cqe.rcqe_cmpl);
14507         else
14508                 fcfi = bf_get(lpfc_rcqe_fcf_id,
14509                               &dmabuf->cq_event.cqe.rcqe_cmpl);
14510         vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi);
14511         if (!vport || !(vport->vpi_state & LPFC_VPI_REGISTERED)) {
14512                 /* throw out the frame */
14513                 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14514                 return;
14515         }
14516         /* Handle the basic abort sequence (BA_ABTS) event */
14517         if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
14518                 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
14519                 return;
14520         }
14521
14522         /* Link this frame */
14523         seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
14524         if (!seq_dmabuf) {
14525                 /* unable to add frame to vport - throw it out */
14526                 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14527                 return;
14528         }
14529         /* If not last frame in sequence continue processing frames. */
14530         if (!lpfc_seq_complete(seq_dmabuf))
14531                 return;
14532
14533         /* Send the complete sequence to the upper layer protocol */
14534         lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
14535 }
14536
14537 /**
14538  * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
14539  * @phba: pointer to lpfc hba data structure.
14540  *
14541  * This routine is invoked to post rpi header templates to the
14542  * HBA consistent with the SLI-4 interface spec.  This routine
14543  * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
14544  * SLI4_PAGE_SIZE modulo 64 rpi context headers.
14545  *
14546  * This routine does not require any locks.  It's usage is expected
14547  * to be driver load or reset recovery when the driver is
14548  * sequential.
14549  *
14550  * Return codes
14551  *      0 - successful
14552  *      -EIO - The mailbox failed to complete successfully.
14553  *      When this error occurs, the driver is not guaranteed
14554  *      to have any rpi regions posted to the device and
14555  *      must either attempt to repost the regions or take a
14556  *      fatal error.
14557  **/
14558 int
14559 lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
14560 {
14561         struct lpfc_rpi_hdr *rpi_page;
14562         uint32_t rc = 0;
14563         uint16_t lrpi = 0;
14564
14565         /* SLI4 ports that support extents do not require RPI headers. */
14566         if (!phba->sli4_hba.rpi_hdrs_in_use)
14567                 goto exit;
14568         if (phba->sli4_hba.extents_in_use)
14569                 return -EIO;
14570
14571         list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
14572                 /*
14573                  * Assign the rpi headers a physical rpi only if the driver
14574                  * has not initialized those resources.  A port reset only
14575                  * needs the headers posted.
14576                  */
14577                 if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
14578                     LPFC_RPI_RSRC_RDY)
14579                         rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
14580
14581                 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
14582                 if (rc != MBX_SUCCESS) {
14583                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14584                                         "2008 Error %d posting all rpi "
14585                                         "headers\n", rc);
14586                         rc = -EIO;
14587                         break;
14588                 }
14589         }
14590
14591  exit:
14592         bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
14593                LPFC_RPI_RSRC_RDY);
14594         return rc;
14595 }
14596
14597 /**
14598  * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
14599  * @phba: pointer to lpfc hba data structure.
14600  * @rpi_page:  pointer to the rpi memory region.
14601  *
14602  * This routine is invoked to post a single rpi header to the
14603  * HBA consistent with the SLI-4 interface spec.  This memory region
14604  * maps up to 64 rpi context regions.
14605  *
14606  * Return codes
14607  *      0 - successful
14608  *      -ENOMEM - No available memory
14609  *      -EIO - The mailbox failed to complete successfully.
14610  **/
14611 int
14612 lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
14613 {
14614         LPFC_MBOXQ_t *mboxq;
14615         struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
14616         uint32_t rc = 0;
14617         uint32_t shdr_status, shdr_add_status;
14618         union lpfc_sli4_cfg_shdr *shdr;
14619
14620         /* SLI4 ports that support extents do not require RPI headers. */
14621         if (!phba->sli4_hba.rpi_hdrs_in_use)
14622                 return rc;
14623         if (phba->sli4_hba.extents_in_use)
14624                 return -EIO;
14625
14626         /* The port is notified of the header region via a mailbox command. */
14627         mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14628         if (!mboxq) {
14629                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14630                                 "2001 Unable to allocate memory for issuing "
14631                                 "SLI_CONFIG_SPECIAL mailbox command\n");
14632                 return -ENOMEM;
14633         }
14634
14635         /* Post all rpi memory regions to the port. */
14636         hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
14637         lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
14638                          LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
14639                          sizeof(struct lpfc_mbx_post_hdr_tmpl) -
14640                          sizeof(struct lpfc_sli4_cfg_mhdr),
14641                          LPFC_SLI4_MBX_EMBED);
14642
14643
14644         /* Post the physical rpi to the port for this rpi header. */
14645         bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
14646                rpi_page->start_rpi);
14647         bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
14648                hdr_tmpl, rpi_page->page_count);
14649
14650         hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
14651         hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
14652         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
14653         shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
14654         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14655         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14656         if (rc != MBX_TIMEOUT)
14657                 mempool_free(mboxq, phba->mbox_mem_pool);
14658         if (shdr_status || shdr_add_status || rc) {
14659                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14660                                 "2514 POST_RPI_HDR mailbox failed with "
14661                                 "status x%x add_status x%x, mbx status x%x\n",
14662                                 shdr_status, shdr_add_status, rc);
14663                 rc = -ENXIO;
14664         }
14665         return rc;
14666 }
14667
14668 /**
14669  * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
14670  * @phba: pointer to lpfc hba data structure.
14671  *
14672  * This routine is invoked to post rpi header templates to the
14673  * HBA consistent with the SLI-4 interface spec.  This routine
14674  * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
14675  * SLI4_PAGE_SIZE modulo 64 rpi context headers.
14676  *
14677  * Returns
14678  *      A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
14679  *      LPFC_RPI_ALLOC_ERROR if no rpis are available.
14680  **/
14681 int
14682 lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
14683 {
14684         unsigned long rpi;
14685         uint16_t max_rpi, rpi_limit;
14686         uint16_t rpi_remaining, lrpi = 0;
14687         struct lpfc_rpi_hdr *rpi_hdr;
14688
14689         max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
14690         rpi_limit = phba->sli4_hba.next_rpi;
14691
14692         /*
14693          * Fetch the next logical rpi.  Because this index is logical,
14694          * the  driver starts at 0 each time.
14695          */
14696         spin_lock_irq(&phba->hbalock);
14697         rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
14698         if (rpi >= rpi_limit)
14699                 rpi = LPFC_RPI_ALLOC_ERROR;
14700         else {
14701                 set_bit(rpi, phba->sli4_hba.rpi_bmask);
14702                 phba->sli4_hba.max_cfg_param.rpi_used++;
14703                 phba->sli4_hba.rpi_count++;
14704         }
14705
14706         /*
14707          * Don't try to allocate more rpi header regions if the device limit
14708          * has been exhausted.
14709          */
14710         if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
14711             (phba->sli4_hba.rpi_count >= max_rpi)) {
14712                 spin_unlock_irq(&phba->hbalock);
14713                 return rpi;
14714         }
14715
14716         /*
14717          * RPI header postings are not required for SLI4 ports capable of
14718          * extents.
14719          */
14720         if (!phba->sli4_hba.rpi_hdrs_in_use) {
14721                 spin_unlock_irq(&phba->hbalock);
14722                 return rpi;
14723         }
14724
14725         /*
14726          * If the driver is running low on rpi resources, allocate another
14727          * page now.  Note that the next_rpi value is used because
14728          * it represents how many are actually in use whereas max_rpi notes
14729          * how many are supported max by the device.
14730          */
14731         rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
14732         spin_unlock_irq(&phba->hbalock);
14733         if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
14734                 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
14735                 if (!rpi_hdr) {
14736                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14737                                         "2002 Error Could not grow rpi "
14738                                         "count\n");
14739                 } else {
14740                         lrpi = rpi_hdr->start_rpi;
14741                         rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
14742                         lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
14743                 }
14744         }
14745
14746         return rpi;
14747 }
14748
14749 /**
14750  * lpfc_sli4_free_rpi - Release an rpi for reuse.
14751  * @phba: pointer to lpfc hba data structure.
14752  *
14753  * This routine is invoked to release an rpi to the pool of
14754  * available rpis maintained by the driver.
14755  **/
14756 void
14757 __lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
14758 {
14759         if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
14760                 phba->sli4_hba.rpi_count--;
14761                 phba->sli4_hba.max_cfg_param.rpi_used--;
14762         }
14763 }
14764
14765 /**
14766  * lpfc_sli4_free_rpi - Release an rpi for reuse.
14767  * @phba: pointer to lpfc hba data structure.
14768  *
14769  * This routine is invoked to release an rpi to the pool of
14770  * available rpis maintained by the driver.
14771  **/
14772 void
14773 lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
14774 {
14775         spin_lock_irq(&phba->hbalock);
14776         __lpfc_sli4_free_rpi(phba, rpi);
14777         spin_unlock_irq(&phba->hbalock);
14778 }
14779
14780 /**
14781  * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
14782  * @phba: pointer to lpfc hba data structure.
14783  *
14784  * This routine is invoked to remove the memory region that
14785  * provided rpi via a bitmask.
14786  **/
14787 void
14788 lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
14789 {
14790         kfree(phba->sli4_hba.rpi_bmask);
14791         kfree(phba->sli4_hba.rpi_ids);
14792         bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
14793 }
14794
14795 /**
14796  * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
14797  * @phba: pointer to lpfc hba data structure.
14798  *
14799  * This routine is invoked to remove the memory region that
14800  * provided rpi via a bitmask.
14801  **/
14802 int
14803 lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp,
14804         void (*cmpl)(struct lpfc_hba *, LPFC_MBOXQ_t *), void *arg)
14805 {
14806         LPFC_MBOXQ_t *mboxq;
14807         struct lpfc_hba *phba = ndlp->phba;
14808         int rc;
14809
14810         /* The port is notified of the header region via a mailbox command. */
14811         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14812         if (!mboxq)
14813                 return -ENOMEM;
14814
14815         /* Post all rpi memory regions to the port. */
14816         lpfc_resume_rpi(mboxq, ndlp);
14817         if (cmpl) {
14818                 mboxq->mbox_cmpl = cmpl;
14819                 mboxq->context1 = arg;
14820                 mboxq->context2 = ndlp;
14821         } else
14822                 mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14823         mboxq->vport = ndlp->vport;
14824         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
14825         if (rc == MBX_NOT_FINISHED) {
14826                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14827                                 "2010 Resume RPI Mailbox failed "
14828                                 "status %d, mbxStatus x%x\n", rc,
14829                                 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
14830                 mempool_free(mboxq, phba->mbox_mem_pool);
14831                 return -EIO;
14832         }
14833         return 0;
14834 }
14835
14836 /**
14837  * lpfc_sli4_init_vpi - Initialize a vpi with the port
14838  * @vport: Pointer to the vport for which the vpi is being initialized
14839  *
14840  * This routine is invoked to activate a vpi with the port.
14841  *
14842  * Returns:
14843  *    0 success
14844  *    -Evalue otherwise
14845  **/
14846 int
14847 lpfc_sli4_init_vpi(struct lpfc_vport *vport)
14848 {
14849         LPFC_MBOXQ_t *mboxq;
14850         int rc = 0;
14851         int retval = MBX_SUCCESS;
14852         uint32_t mbox_tmo;
14853         struct lpfc_hba *phba = vport->phba;
14854         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14855         if (!mboxq)
14856                 return -ENOMEM;
14857         lpfc_init_vpi(phba, mboxq, vport->vpi);
14858         mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
14859         rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
14860         if (rc != MBX_SUCCESS) {
14861                 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
14862                                 "2022 INIT VPI Mailbox failed "
14863                                 "status %d, mbxStatus x%x\n", rc,
14864                                 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
14865                 retval = -EIO;
14866         }
14867         if (rc != MBX_TIMEOUT)
14868                 mempool_free(mboxq, vport->phba->mbox_mem_pool);
14869
14870         return retval;
14871 }
14872
14873 /**
14874  * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
14875  * @phba: pointer to lpfc hba data structure.
14876  * @mboxq: Pointer to mailbox object.
14877  *
14878  * This routine is invoked to manually add a single FCF record. The caller
14879  * must pass a completely initialized FCF_Record.  This routine takes
14880  * care of the nonembedded mailbox operations.
14881  **/
14882 static void
14883 lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
14884 {
14885         void *virt_addr;
14886         union lpfc_sli4_cfg_shdr *shdr;
14887         uint32_t shdr_status, shdr_add_status;
14888
14889         virt_addr = mboxq->sge_array->addr[0];
14890         /* The IOCTL status is embedded in the mailbox subheader. */
14891         shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
14892         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14893         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14894
14895         if ((shdr_status || shdr_add_status) &&
14896                 (shdr_status != STATUS_FCF_IN_USE))
14897                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14898                         "2558 ADD_FCF_RECORD mailbox failed with "
14899                         "status x%x add_status x%x\n",
14900                         shdr_status, shdr_add_status);
14901
14902         lpfc_sli4_mbox_cmd_free(phba, mboxq);
14903 }
14904
14905 /**
14906  * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
14907  * @phba: pointer to lpfc hba data structure.
14908  * @fcf_record:  pointer to the initialized fcf record to add.
14909  *
14910  * This routine is invoked to manually add a single FCF record. The caller
14911  * must pass a completely initialized FCF_Record.  This routine takes
14912  * care of the nonembedded mailbox operations.
14913  **/
14914 int
14915 lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
14916 {
14917         int rc = 0;
14918         LPFC_MBOXQ_t *mboxq;
14919         uint8_t *bytep;
14920         void *virt_addr;
14921         dma_addr_t phys_addr;
14922         struct lpfc_mbx_sge sge;
14923         uint32_t alloc_len, req_len;
14924         uint32_t fcfindex;
14925
14926         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14927         if (!mboxq) {
14928                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14929                         "2009 Failed to allocate mbox for ADD_FCF cmd\n");
14930                 return -ENOMEM;
14931         }
14932
14933         req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
14934                   sizeof(uint32_t);
14935
14936         /* Allocate DMA memory and set up the non-embedded mailbox command */
14937         alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
14938                                      LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
14939                                      req_len, LPFC_SLI4_MBX_NEMBED);
14940         if (alloc_len < req_len) {
14941                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14942                         "2523 Allocated DMA memory size (x%x) is "
14943                         "less than the requested DMA memory "
14944                         "size (x%x)\n", alloc_len, req_len);
14945                 lpfc_sli4_mbox_cmd_free(phba, mboxq);
14946                 return -ENOMEM;
14947         }
14948
14949         /*
14950          * Get the first SGE entry from the non-embedded DMA memory.  This
14951          * routine only uses a single SGE.
14952          */
14953         lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
14954         phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
14955         virt_addr = mboxq->sge_array->addr[0];
14956         /*
14957          * Configure the FCF record for FCFI 0.  This is the driver's
14958          * hardcoded default and gets used in nonFIP mode.
14959          */
14960         fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
14961         bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
14962         lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
14963
14964         /*
14965          * Copy the fcf_index and the FCF Record Data. The data starts after
14966          * the FCoE header plus word10. The data copy needs to be endian
14967          * correct.
14968          */
14969         bytep += sizeof(uint32_t);
14970         lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
14971         mboxq->vport = phba->pport;
14972         mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
14973         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
14974         if (rc == MBX_NOT_FINISHED) {
14975                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14976                         "2515 ADD_FCF_RECORD mailbox failed with "
14977                         "status 0x%x\n", rc);
14978                 lpfc_sli4_mbox_cmd_free(phba, mboxq);
14979                 rc = -EIO;
14980         } else
14981                 rc = 0;
14982
14983         return rc;
14984 }
14985
14986 /**
14987  * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
14988  * @phba: pointer to lpfc hba data structure.
14989  * @fcf_record:  pointer to the fcf record to write the default data.
14990  * @fcf_index: FCF table entry index.
14991  *
14992  * This routine is invoked to build the driver's default FCF record.  The
14993  * values used are hardcoded.  This routine handles memory initialization.
14994  *
14995  **/
14996 void
14997 lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
14998                                 struct fcf_record *fcf_record,
14999                                 uint16_t fcf_index)
15000 {
15001         memset(fcf_record, 0, sizeof(struct fcf_record));
15002         fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
15003         fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
15004         fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
15005         bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
15006         bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
15007         bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
15008         bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
15009         bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
15010         bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
15011         bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
15012         bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
15013         bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
15014         bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
15015         bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
15016         bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
15017         bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
15018                 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
15019         /* Set the VLAN bit map */
15020         if (phba->valid_vlan) {
15021                 fcf_record->vlan_bitmap[phba->vlan_id / 8]
15022                         = 1 << (phba->vlan_id % 8);
15023         }
15024 }
15025
15026 /**
15027  * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
15028  * @phba: pointer to lpfc hba data structure.
15029  * @fcf_index: FCF table entry offset.
15030  *
15031  * This routine is invoked to scan the entire FCF table by reading FCF
15032  * record and processing it one at a time starting from the @fcf_index
15033  * for initial FCF discovery or fast FCF failover rediscovery.
15034  *
15035  * Return 0 if the mailbox command is submitted successfully, none 0
15036  * otherwise.
15037  **/
15038 int
15039 lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
15040 {
15041         int rc = 0, error;
15042         LPFC_MBOXQ_t *mboxq;
15043
15044         phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
15045         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15046         if (!mboxq) {
15047                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15048                                 "2000 Failed to allocate mbox for "
15049                                 "READ_FCF cmd\n");
15050                 error = -ENOMEM;
15051                 goto fail_fcf_scan;
15052         }
15053         /* Construct the read FCF record mailbox command */
15054         rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
15055         if (rc) {
15056                 error = -EINVAL;
15057                 goto fail_fcf_scan;
15058         }
15059         /* Issue the mailbox command asynchronously */
15060         mboxq->vport = phba->pport;
15061         mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
15062
15063         spin_lock_irq(&phba->hbalock);
15064         phba->hba_flag |= FCF_TS_INPROG;
15065         spin_unlock_irq(&phba->hbalock);
15066
15067         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
15068         if (rc == MBX_NOT_FINISHED)
15069                 error = -EIO;
15070         else {
15071                 /* Reset eligible FCF count for new scan */
15072                 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
15073                         phba->fcf.eligible_fcf_cnt = 0;
15074                 error = 0;
15075         }
15076 fail_fcf_scan:
15077         if (error) {
15078                 if (mboxq)
15079                         lpfc_sli4_mbox_cmd_free(phba, mboxq);
15080                 /* FCF scan failed, clear FCF_TS_INPROG flag */
15081                 spin_lock_irq(&phba->hbalock);
15082                 phba->hba_flag &= ~FCF_TS_INPROG;
15083                 spin_unlock_irq(&phba->hbalock);
15084         }
15085         return error;
15086 }
15087
15088 /**
15089  * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
15090  * @phba: pointer to lpfc hba data structure.
15091  * @fcf_index: FCF table entry offset.
15092  *
15093  * This routine is invoked to read an FCF record indicated by @fcf_index
15094  * and to use it for FLOGI roundrobin FCF failover.
15095  *
15096  * Return 0 if the mailbox command is submitted successfully, none 0
15097  * otherwise.
15098  **/
15099 int
15100 lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
15101 {
15102         int rc = 0, error;
15103         LPFC_MBOXQ_t *mboxq;
15104
15105         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15106         if (!mboxq) {
15107                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
15108                                 "2763 Failed to allocate mbox for "
15109                                 "READ_FCF cmd\n");
15110                 error = -ENOMEM;
15111                 goto fail_fcf_read;
15112         }
15113         /* Construct the read FCF record mailbox command */
15114         rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
15115         if (rc) {
15116                 error = -EINVAL;
15117                 goto fail_fcf_read;
15118         }
15119         /* Issue the mailbox command asynchronously */
15120         mboxq->vport = phba->pport;
15121         mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
15122         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
15123         if (rc == MBX_NOT_FINISHED)
15124                 error = -EIO;
15125         else
15126                 error = 0;
15127
15128 fail_fcf_read:
15129         if (error && mboxq)
15130                 lpfc_sli4_mbox_cmd_free(phba, mboxq);
15131         return error;
15132 }
15133
15134 /**
15135  * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
15136  * @phba: pointer to lpfc hba data structure.
15137  * @fcf_index: FCF table entry offset.
15138  *
15139  * This routine is invoked to read an FCF record indicated by @fcf_index to
15140  * determine whether it's eligible for FLOGI roundrobin failover list.
15141  *
15142  * Return 0 if the mailbox command is submitted successfully, none 0
15143  * otherwise.
15144  **/
15145 int
15146 lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
15147 {
15148         int rc = 0, error;
15149         LPFC_MBOXQ_t *mboxq;
15150
15151         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15152         if (!mboxq) {
15153                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
15154                                 "2758 Failed to allocate mbox for "
15155                                 "READ_FCF cmd\n");
15156                                 error = -ENOMEM;
15157                                 goto fail_fcf_read;
15158         }
15159         /* Construct the read FCF record mailbox command */
15160         rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
15161         if (rc) {
15162                 error = -EINVAL;
15163                 goto fail_fcf_read;
15164         }
15165         /* Issue the mailbox command asynchronously */
15166         mboxq->vport = phba->pport;
15167         mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
15168         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
15169         if (rc == MBX_NOT_FINISHED)
15170                 error = -EIO;
15171         else
15172                 error = 0;
15173
15174 fail_fcf_read:
15175         if (error && mboxq)
15176                 lpfc_sli4_mbox_cmd_free(phba, mboxq);
15177         return error;
15178 }
15179
15180 /**
15181  * lpfc_check_next_fcf_pri
15182  * phba pointer to the lpfc_hba struct for this port.
15183  * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
15184  * routine when the rr_bmask is empty. The FCF indecies are put into the
15185  * rr_bmask based on their priority level. Starting from the highest priority
15186  * to the lowest. The most likely FCF candidate will be in the highest
15187  * priority group. When this routine is called it searches the fcf_pri list for
15188  * next lowest priority group and repopulates the rr_bmask with only those
15189  * fcf_indexes.
15190  * returns:
15191  * 1=success 0=failure
15192  **/
15193 int
15194 lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
15195 {
15196         uint16_t next_fcf_pri;
15197         uint16_t last_index;
15198         struct lpfc_fcf_pri *fcf_pri;
15199         int rc;
15200         int ret = 0;
15201
15202         last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
15203                         LPFC_SLI4_FCF_TBL_INDX_MAX);
15204         lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
15205                         "3060 Last IDX %d\n", last_index);
15206         if (list_empty(&phba->fcf.fcf_pri_list)) {
15207                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
15208                         "3061 Last IDX %d\n", last_index);
15209                 return 0; /* Empty rr list */
15210         }
15211         next_fcf_pri = 0;
15212         /*
15213          * Clear the rr_bmask and set all of the bits that are at this
15214          * priority.
15215          */
15216         memset(phba->fcf.fcf_rr_bmask, 0,
15217                         sizeof(*phba->fcf.fcf_rr_bmask));
15218         spin_lock_irq(&phba->hbalock);
15219         list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
15220                 if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
15221                         continue;
15222                 /*
15223                  * the 1st priority that has not FLOGI failed
15224                  * will be the highest.
15225                  */
15226                 if (!next_fcf_pri)
15227                         next_fcf_pri = fcf_pri->fcf_rec.priority;
15228                 spin_unlock_irq(&phba->hbalock);
15229                 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
15230                         rc = lpfc_sli4_fcf_rr_index_set(phba,
15231                                                 fcf_pri->fcf_rec.fcf_index);
15232                         if (rc)
15233                                 return 0;
15234                 }
15235                 spin_lock_irq(&phba->hbalock);
15236         }
15237         /*
15238          * if next_fcf_pri was not set above and the list is not empty then
15239          * we have failed flogis on all of them. So reset flogi failed
15240          * and start at the begining.
15241          */
15242         if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
15243                 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
15244                         fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
15245                         /*
15246                          * the 1st priority that has not FLOGI failed
15247                          * will be the highest.
15248                          */
15249                         if (!next_fcf_pri)
15250                                 next_fcf_pri = fcf_pri->fcf_rec.priority;
15251                         spin_unlock_irq(&phba->hbalock);
15252                         if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
15253                                 rc = lpfc_sli4_fcf_rr_index_set(phba,
15254                                                 fcf_pri->fcf_rec.fcf_index);
15255                                 if (rc)
15256                                         return 0;
15257                         }
15258                         spin_lock_irq(&phba->hbalock);
15259                 }
15260         } else
15261                 ret = 1;
15262         spin_unlock_irq(&phba->hbalock);
15263
15264         return ret;
15265 }
15266 /**
15267  * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
15268  * @phba: pointer to lpfc hba data structure.
15269  *
15270  * This routine is to get the next eligible FCF record index in a round
15271  * robin fashion. If the next eligible FCF record index equals to the
15272  * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
15273  * shall be returned, otherwise, the next eligible FCF record's index
15274  * shall be returned.
15275  **/
15276 uint16_t
15277 lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
15278 {
15279         uint16_t next_fcf_index;
15280
15281         /* Search start from next bit of currently registered FCF index */
15282 next_priority:
15283         next_fcf_index = (phba->fcf.current_rec.fcf_indx + 1) %
15284                                         LPFC_SLI4_FCF_TBL_INDX_MAX;
15285         next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
15286                                        LPFC_SLI4_FCF_TBL_INDX_MAX,
15287                                        next_fcf_index);
15288
15289         /* Wrap around condition on phba->fcf.fcf_rr_bmask */
15290         if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
15291                 /*
15292                  * If we have wrapped then we need to clear the bits that
15293                  * have been tested so that we can detect when we should
15294                  * change the priority level.
15295                  */
15296                 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
15297                                                LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
15298         }
15299
15300
15301         /* Check roundrobin failover list empty condition */
15302         if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
15303                 next_fcf_index == phba->fcf.current_rec.fcf_indx) {
15304                 /*
15305                  * If next fcf index is not found check if there are lower
15306                  * Priority level fcf's in the fcf_priority list.
15307                  * Set up the rr_bmask with all of the avaiable fcf bits
15308                  * at that level and continue the selection process.
15309                  */
15310                 if (lpfc_check_next_fcf_pri_level(phba))
15311                         goto next_priority;
15312                 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
15313                                 "2844 No roundrobin failover FCF available\n");
15314                 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
15315                         return LPFC_FCOE_FCF_NEXT_NONE;
15316                 else {
15317                         lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
15318                                 "3063 Only FCF available idx %d, flag %x\n",
15319                                 next_fcf_index,
15320                         phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag);
15321                         return next_fcf_index;
15322                 }
15323         }
15324
15325         if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
15326                 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
15327                 LPFC_FCF_FLOGI_FAILED)
15328                 goto next_priority;
15329
15330         lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
15331                         "2845 Get next roundrobin failover FCF (x%x)\n",
15332                         next_fcf_index);
15333
15334         return next_fcf_index;
15335 }
15336
15337 /**
15338  * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
15339  * @phba: pointer to lpfc hba data structure.
15340  *
15341  * This routine sets the FCF record index in to the eligible bmask for
15342  * roundrobin failover search. It checks to make sure that the index
15343  * does not go beyond the range of the driver allocated bmask dimension
15344  * before setting the bit.
15345  *
15346  * Returns 0 if the index bit successfully set, otherwise, it returns
15347  * -EINVAL.
15348  **/
15349 int
15350 lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
15351 {
15352         if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
15353                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
15354                                 "2610 FCF (x%x) reached driver's book "
15355                                 "keeping dimension:x%x\n",
15356                                 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
15357                 return -EINVAL;
15358         }
15359         /* Set the eligible FCF record index bmask */
15360         set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
15361
15362         lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
15363                         "2790 Set FCF (x%x) to roundrobin FCF failover "
15364                         "bmask\n", fcf_index);
15365
15366         return 0;
15367 }
15368
15369 /**
15370  * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
15371  * @phba: pointer to lpfc hba data structure.
15372  *
15373  * This routine clears the FCF record index from the eligible bmask for
15374  * roundrobin failover search. It checks to make sure that the index
15375  * does not go beyond the range of the driver allocated bmask dimension
15376  * before clearing the bit.
15377  **/
15378 void
15379 lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
15380 {
15381         struct lpfc_fcf_pri *fcf_pri;
15382         if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
15383                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
15384                                 "2762 FCF (x%x) reached driver's book "
15385                                 "keeping dimension:x%x\n",
15386                                 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
15387                 return;
15388         }
15389         /* Clear the eligible FCF record index bmask */
15390         spin_lock_irq(&phba->hbalock);
15391         list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
15392                 if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
15393                         list_del_init(&fcf_pri->list);
15394                         break;
15395                 }
15396         }
15397         spin_unlock_irq(&phba->hbalock);
15398         clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
15399
15400         lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
15401                         "2791 Clear FCF (x%x) from roundrobin failover "
15402                         "bmask\n", fcf_index);
15403 }
15404
15405 /**
15406  * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
15407  * @phba: pointer to lpfc hba data structure.
15408  *
15409  * This routine is the completion routine for the rediscover FCF table mailbox
15410  * command. If the mailbox command returned failure, it will try to stop the
15411  * FCF rediscover wait timer.
15412  **/
15413 void
15414 lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
15415 {
15416         struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
15417         uint32_t shdr_status, shdr_add_status;
15418
15419         redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
15420
15421         shdr_status = bf_get(lpfc_mbox_hdr_status,
15422                              &redisc_fcf->header.cfg_shdr.response);
15423         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
15424                              &redisc_fcf->header.cfg_shdr.response);
15425         if (shdr_status || shdr_add_status) {
15426                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
15427                                 "2746 Requesting for FCF rediscovery failed "
15428                                 "status x%x add_status x%x\n",
15429                                 shdr_status, shdr_add_status);
15430                 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
15431                         spin_lock_irq(&phba->hbalock);
15432                         phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
15433                         spin_unlock_irq(&phba->hbalock);
15434                         /*
15435                          * CVL event triggered FCF rediscover request failed,
15436                          * last resort to re-try current registered FCF entry.
15437                          */
15438                         lpfc_retry_pport_discovery(phba);
15439                 } else {
15440                         spin_lock_irq(&phba->hbalock);
15441                         phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
15442                         spin_unlock_irq(&phba->hbalock);
15443                         /*
15444                          * DEAD FCF event triggered FCF rediscover request
15445                          * failed, last resort to fail over as a link down
15446                          * to FCF registration.
15447                          */
15448                         lpfc_sli4_fcf_dead_failthrough(phba);
15449                 }
15450         } else {
15451                 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
15452                                 "2775 Start FCF rediscover quiescent timer\n");
15453                 /*
15454                  * Start FCF rediscovery wait timer for pending FCF
15455                  * before rescan FCF record table.
15456                  */
15457                 lpfc_fcf_redisc_wait_start_timer(phba);
15458         }
15459
15460         mempool_free(mbox, phba->mbox_mem_pool);
15461 }
15462
15463 /**
15464  * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
15465  * @phba: pointer to lpfc hba data structure.
15466  *
15467  * This routine is invoked to request for rediscovery of the entire FCF table
15468  * by the port.
15469  **/
15470 int
15471 lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
15472 {
15473         LPFC_MBOXQ_t *mbox;
15474         struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
15475         int rc, length;
15476
15477         /* Cancel retry delay timers to all vports before FCF rediscover */
15478         lpfc_cancel_all_vport_retry_delay_timer(phba);
15479
15480         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15481         if (!mbox) {
15482                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15483                                 "2745 Failed to allocate mbox for "
15484                                 "requesting FCF rediscover.\n");
15485                 return -ENOMEM;
15486         }
15487
15488         length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
15489                   sizeof(struct lpfc_sli4_cfg_mhdr));
15490         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15491                          LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
15492                          length, LPFC_SLI4_MBX_EMBED);
15493
15494         redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
15495         /* Set count to 0 for invalidating the entire FCF database */
15496         bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
15497
15498         /* Issue the mailbox command asynchronously */
15499         mbox->vport = phba->pport;
15500         mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
15501         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
15502
15503         if (rc == MBX_NOT_FINISHED) {
15504                 mempool_free(mbox, phba->mbox_mem_pool);
15505                 return -EIO;
15506         }
15507         return 0;
15508 }
15509
15510 /**
15511  * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
15512  * @phba: pointer to lpfc hba data structure.
15513  *
15514  * This function is the failover routine as a last resort to the FCF DEAD
15515  * event when driver failed to perform fast FCF failover.
15516  **/
15517 void
15518 lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
15519 {
15520         uint32_t link_state;
15521
15522         /*
15523          * Last resort as FCF DEAD event failover will treat this as
15524          * a link down, but save the link state because we don't want
15525          * it to be changed to Link Down unless it is already down.
15526          */
15527         link_state = phba->link_state;
15528         lpfc_linkdown(phba);
15529         phba->link_state = link_state;
15530
15531         /* Unregister FCF if no devices connected to it */
15532         lpfc_unregister_unused_fcf(phba);
15533 }
15534
15535 /**
15536  * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
15537  * @phba: pointer to lpfc hba data structure.
15538  * @rgn23_data: pointer to configure region 23 data.
15539  *
15540  * This function gets SLI3 port configure region 23 data through memory dump
15541  * mailbox command. When it successfully retrieves data, the size of the data
15542  * will be returned, otherwise, 0 will be returned.
15543  **/
15544 static uint32_t
15545 lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
15546 {
15547         LPFC_MBOXQ_t *pmb = NULL;
15548         MAILBOX_t *mb;
15549         uint32_t offset = 0;
15550         int rc;
15551
15552         if (!rgn23_data)
15553                 return 0;
15554
15555         pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15556         if (!pmb) {
15557                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15558                                 "2600 failed to allocate mailbox memory\n");
15559                 return 0;
15560         }
15561         mb = &pmb->u.mb;
15562
15563         do {
15564                 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
15565                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
15566
15567                 if (rc != MBX_SUCCESS) {
15568                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
15569                                         "2601 failed to read config "
15570                                         "region 23, rc 0x%x Status 0x%x\n",
15571                                         rc, mb->mbxStatus);
15572                         mb->un.varDmp.word_cnt = 0;
15573                 }
15574                 /*
15575                  * dump mem may return a zero when finished or we got a
15576                  * mailbox error, either way we are done.
15577                  */
15578                 if (mb->un.varDmp.word_cnt == 0)
15579                         break;
15580                 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
15581                         mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
15582
15583                 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
15584                                        rgn23_data + offset,
15585                                        mb->un.varDmp.word_cnt);
15586                 offset += mb->un.varDmp.word_cnt;
15587         } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
15588
15589         mempool_free(pmb, phba->mbox_mem_pool);
15590         return offset;
15591 }
15592
15593 /**
15594  * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
15595  * @phba: pointer to lpfc hba data structure.
15596  * @rgn23_data: pointer to configure region 23 data.
15597  *
15598  * This function gets SLI4 port configure region 23 data through memory dump
15599  * mailbox command. When it successfully retrieves data, the size of the data
15600  * will be returned, otherwise, 0 will be returned.
15601  **/
15602 static uint32_t
15603 lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
15604 {
15605         LPFC_MBOXQ_t *mboxq = NULL;
15606         struct lpfc_dmabuf *mp = NULL;
15607         struct lpfc_mqe *mqe;
15608         uint32_t data_length = 0;
15609         int rc;
15610
15611         if (!rgn23_data)
15612                 return 0;
15613
15614         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15615         if (!mboxq) {
15616                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15617                                 "3105 failed to allocate mailbox memory\n");
15618                 return 0;
15619         }
15620
15621         if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
15622                 goto out;
15623         mqe = &mboxq->u.mqe;
15624         mp = (struct lpfc_dmabuf *) mboxq->context1;
15625         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
15626         if (rc)
15627                 goto out;
15628         data_length = mqe->un.mb_words[5];
15629         if (data_length == 0)
15630                 goto out;
15631         if (data_length > DMP_RGN23_SIZE) {
15632                 data_length = 0;
15633                 goto out;
15634         }
15635         lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
15636 out:
15637         mempool_free(mboxq, phba->mbox_mem_pool);
15638         if (mp) {
15639                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
15640                 kfree(mp);
15641         }
15642         return data_length;
15643 }
15644
15645 /**
15646  * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
15647  * @phba: pointer to lpfc hba data structure.
15648  *
15649  * This function read region 23 and parse TLV for port status to
15650  * decide if the user disaled the port. If the TLV indicates the
15651  * port is disabled, the hba_flag is set accordingly.
15652  **/
15653 void
15654 lpfc_sli_read_link_ste(struct lpfc_hba *phba)
15655 {
15656         uint8_t *rgn23_data = NULL;
15657         uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
15658         uint32_t offset = 0;
15659
15660         /* Get adapter Region 23 data */
15661         rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
15662         if (!rgn23_data)
15663                 goto out;
15664
15665         if (phba->sli_rev < LPFC_SLI_REV4)
15666                 data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
15667         else {
15668                 if_type = bf_get(lpfc_sli_intf_if_type,
15669                                  &phba->sli4_hba.sli_intf);
15670                 if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
15671                         goto out;
15672                 data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
15673         }
15674
15675         if (!data_size)
15676                 goto out;
15677
15678         /* Check the region signature first */
15679         if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
15680                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15681                         "2619 Config region 23 has bad signature\n");
15682                         goto out;
15683         }
15684         offset += 4;
15685
15686         /* Check the data structure version */
15687         if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
15688                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15689                         "2620 Config region 23 has bad version\n");
15690                 goto out;
15691         }
15692         offset += 4;
15693
15694         /* Parse TLV entries in the region */
15695         while (offset < data_size) {
15696                 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
15697                         break;
15698                 /*
15699                  * If the TLV is not driver specific TLV or driver id is
15700                  * not linux driver id, skip the record.
15701                  */
15702                 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
15703                     (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
15704                     (rgn23_data[offset + 3] != 0)) {
15705                         offset += rgn23_data[offset + 1] * 4 + 4;
15706                         continue;
15707                 }
15708
15709                 /* Driver found a driver specific TLV in the config region */
15710                 sub_tlv_len = rgn23_data[offset + 1] * 4;
15711                 offset += 4;
15712                 tlv_offset = 0;
15713
15714                 /*
15715                  * Search for configured port state sub-TLV.
15716                  */
15717                 while ((offset < data_size) &&
15718                         (tlv_offset < sub_tlv_len)) {
15719                         if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
15720                                 offset += 4;
15721                                 tlv_offset += 4;
15722                                 break;
15723                         }
15724                         if (rgn23_data[offset] != PORT_STE_TYPE) {
15725                                 offset += rgn23_data[offset + 1] * 4 + 4;
15726                                 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
15727                                 continue;
15728                         }
15729
15730                         /* This HBA contains PORT_STE configured */
15731                         if (!rgn23_data[offset + 2])
15732                                 phba->hba_flag |= LINK_DISABLED;
15733
15734                         goto out;
15735                 }
15736         }
15737
15738 out:
15739         kfree(rgn23_data);
15740         return;
15741 }
15742
15743 /**
15744  * lpfc_wr_object - write an object to the firmware
15745  * @phba: HBA structure that indicates port to create a queue on.
15746  * @dmabuf_list: list of dmabufs to write to the port.
15747  * @size: the total byte value of the objects to write to the port.
15748  * @offset: the current offset to be used to start the transfer.
15749  *
15750  * This routine will create a wr_object mailbox command to send to the port.
15751  * the mailbox command will be constructed using the dma buffers described in
15752  * @dmabuf_list to create a list of BDEs. This routine will fill in as many
15753  * BDEs that the imbedded mailbox can support. The @offset variable will be
15754  * used to indicate the starting offset of the transfer and will also return
15755  * the offset after the write object mailbox has completed. @size is used to
15756  * determine the end of the object and whether the eof bit should be set.
15757  *
15758  * Return 0 is successful and offset will contain the the new offset to use
15759  * for the next write.
15760  * Return negative value for error cases.
15761  **/
15762 int
15763 lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
15764                uint32_t size, uint32_t *offset)
15765 {
15766         struct lpfc_mbx_wr_object *wr_object;
15767         LPFC_MBOXQ_t *mbox;
15768         int rc = 0, i = 0;
15769         uint32_t shdr_status, shdr_add_status;
15770         uint32_t mbox_tmo;
15771         union lpfc_sli4_cfg_shdr *shdr;
15772         struct lpfc_dmabuf *dmabuf;
15773         uint32_t written = 0;
15774
15775         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15776         if (!mbox)
15777                 return -ENOMEM;
15778
15779         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15780                         LPFC_MBOX_OPCODE_WRITE_OBJECT,
15781                         sizeof(struct lpfc_mbx_wr_object) -
15782                         sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
15783
15784         wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
15785         wr_object->u.request.write_offset = *offset;
15786         sprintf((uint8_t *)wr_object->u.request.object_name, "/");
15787         wr_object->u.request.object_name[0] =
15788                 cpu_to_le32(wr_object->u.request.object_name[0]);
15789         bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
15790         list_for_each_entry(dmabuf, dmabuf_list, list) {
15791                 if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
15792                         break;
15793                 wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
15794                 wr_object->u.request.bde[i].addrHigh =
15795                         putPaddrHigh(dmabuf->phys);
15796                 if (written + SLI4_PAGE_SIZE >= size) {
15797                         wr_object->u.request.bde[i].tus.f.bdeSize =
15798                                 (size - written);
15799                         written += (size - written);
15800                         bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
15801                 } else {
15802                         wr_object->u.request.bde[i].tus.f.bdeSize =
15803                                 SLI4_PAGE_SIZE;
15804                         written += SLI4_PAGE_SIZE;
15805                 }
15806                 i++;
15807         }
15808         wr_object->u.request.bde_count = i;
15809         bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
15810         if (!phba->sli4_hba.intr_enable)
15811                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15812         else {
15813                 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
15814                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
15815         }
15816         /* The IOCTL status is embedded in the mailbox subheader. */
15817         shdr = (union lpfc_sli4_cfg_shdr *) &wr_object->header.cfg_shdr;
15818         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15819         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15820         if (rc != MBX_TIMEOUT)
15821                 mempool_free(mbox, phba->mbox_mem_pool);
15822         if (shdr_status || shdr_add_status || rc) {
15823                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15824                                 "3025 Write Object mailbox failed with "
15825                                 "status x%x add_status x%x, mbx status x%x\n",
15826                                 shdr_status, shdr_add_status, rc);
15827                 rc = -ENXIO;
15828         } else
15829                 *offset += wr_object->u.response.actual_write_length;
15830         return rc;
15831 }
15832
15833 /**
15834  * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
15835  * @vport: pointer to vport data structure.
15836  *
15837  * This function iterate through the mailboxq and clean up all REG_LOGIN
15838  * and REG_VPI mailbox commands associated with the vport. This function
15839  * is called when driver want to restart discovery of the vport due to
15840  * a Clear Virtual Link event.
15841  **/
15842 void
15843 lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
15844 {
15845         struct lpfc_hba *phba = vport->phba;
15846         LPFC_MBOXQ_t *mb, *nextmb;
15847         struct lpfc_dmabuf *mp;
15848         struct lpfc_nodelist *ndlp;
15849         struct lpfc_nodelist *act_mbx_ndlp = NULL;
15850         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
15851         LIST_HEAD(mbox_cmd_list);
15852         uint8_t restart_loop;
15853
15854         /* Clean up internally queued mailbox commands with the vport */
15855         spin_lock_irq(&phba->hbalock);
15856         list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
15857                 if (mb->vport != vport)
15858                         continue;
15859
15860                 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
15861                         (mb->u.mb.mbxCommand != MBX_REG_VPI))
15862                         continue;
15863
15864                 list_del(&mb->list);
15865                 list_add_tail(&mb->list, &mbox_cmd_list);
15866         }
15867         /* Clean up active mailbox command with the vport */
15868         mb = phba->sli.mbox_active;
15869         if (mb && (mb->vport == vport)) {
15870                 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
15871                         (mb->u.mb.mbxCommand == MBX_REG_VPI))
15872                         mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15873                 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
15874                         act_mbx_ndlp = (struct lpfc_nodelist *)mb->context2;
15875                         /* Put reference count for delayed processing */
15876                         act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
15877                         /* Unregister the RPI when mailbox complete */
15878                         mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
15879                 }
15880         }
15881         /* Cleanup any mailbox completions which are not yet processed */
15882         do {
15883                 restart_loop = 0;
15884                 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
15885                         /*
15886                          * If this mailox is already processed or it is
15887                          * for another vport ignore it.
15888                          */
15889                         if ((mb->vport != vport) ||
15890                                 (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
15891                                 continue;
15892
15893                         if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
15894                                 (mb->u.mb.mbxCommand != MBX_REG_VPI))
15895                                 continue;
15896
15897                         mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15898                         if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
15899                                 ndlp = (struct lpfc_nodelist *)mb->context2;
15900                                 /* Unregister the RPI when mailbox complete */
15901                                 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
15902                                 restart_loop = 1;
15903                                 spin_unlock_irq(&phba->hbalock);
15904                                 spin_lock(shost->host_lock);
15905                                 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
15906                                 spin_unlock(shost->host_lock);
15907                                 spin_lock_irq(&phba->hbalock);
15908                                 break;
15909                         }
15910                 }
15911         } while (restart_loop);
15912
15913         spin_unlock_irq(&phba->hbalock);
15914
15915         /* Release the cleaned-up mailbox commands */
15916         while (!list_empty(&mbox_cmd_list)) {
15917                 list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
15918                 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
15919                         mp = (struct lpfc_dmabuf *) (mb->context1);
15920                         if (mp) {
15921                                 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
15922                                 kfree(mp);
15923                         }
15924                         ndlp = (struct lpfc_nodelist *) mb->context2;
15925                         mb->context2 = NULL;
15926                         if (ndlp) {
15927                                 spin_lock(shost->host_lock);
15928                                 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
15929                                 spin_unlock(shost->host_lock);
15930                                 lpfc_nlp_put(ndlp);
15931                         }
15932                 }
15933                 mempool_free(mb, phba->mbox_mem_pool);
15934         }
15935
15936         /* Release the ndlp with the cleaned-up active mailbox command */
15937         if (act_mbx_ndlp) {
15938                 spin_lock(shost->host_lock);
15939                 act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
15940                 spin_unlock(shost->host_lock);
15941                 lpfc_nlp_put(act_mbx_ndlp);
15942         }
15943 }
15944
15945 /**
15946  * lpfc_drain_txq - Drain the txq
15947  * @phba: Pointer to HBA context object.
15948  *
15949  * This function attempt to submit IOCBs on the txq
15950  * to the adapter.  For SLI4 adapters, the txq contains
15951  * ELS IOCBs that have been deferred because the there
15952  * are no SGLs.  This congestion can occur with large
15953  * vport counts during node discovery.
15954  **/
15955
15956 uint32_t
15957 lpfc_drain_txq(struct lpfc_hba *phba)
15958 {
15959         LIST_HEAD(completions);
15960         struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
15961         struct lpfc_iocbq *piocbq = 0;
15962         unsigned long iflags = 0;
15963         char *fail_msg = NULL;
15964         struct lpfc_sglq *sglq;
15965         union lpfc_wqe wqe;
15966
15967         spin_lock_irqsave(&phba->hbalock, iflags);
15968         if (pring->txq_cnt > pring->txq_max)
15969                 pring->txq_max = pring->txq_cnt;
15970
15971         spin_unlock_irqrestore(&phba->hbalock, iflags);
15972
15973         while (pring->txq_cnt) {
15974                 spin_lock_irqsave(&phba->hbalock, iflags);
15975
15976                 piocbq = lpfc_sli_ringtx_get(phba, pring);
15977                 sglq = __lpfc_sli_get_sglq(phba, piocbq);
15978                 if (!sglq) {
15979                         __lpfc_sli_ringtx_put(phba, pring, piocbq);
15980                         spin_unlock_irqrestore(&phba->hbalock, iflags);
15981                         break;
15982                 } else {
15983                         if (!piocbq) {
15984                                 /* The txq_cnt out of sync. This should
15985                                  * never happen
15986                                  */
15987                                 sglq = __lpfc_clear_active_sglq(phba,
15988                                                  sglq->sli4_lxritag);
15989                                 spin_unlock_irqrestore(&phba->hbalock, iflags);
15990                                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15991                                         "2823 txq empty and txq_cnt is %d\n ",
15992                                         pring->txq_cnt);
15993                                 break;
15994                         }
15995                 }
15996
15997                 /* The xri and iocb resources secured,
15998                  * attempt to issue request
15999                  */
16000                 piocbq->sli4_lxritag = sglq->sli4_lxritag;
16001                 piocbq->sli4_xritag = sglq->sli4_xritag;
16002                 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
16003                         fail_msg = "to convert bpl to sgl";
16004                 else if (lpfc_sli4_iocb2wqe(phba, piocbq, &wqe))
16005                         fail_msg = "to convert iocb to wqe";
16006                 else if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
16007                         fail_msg = " - Wq is full";
16008                 else
16009                         lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
16010
16011                 if (fail_msg) {
16012                         /* Failed means we can't issue and need to cancel */
16013                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16014                                         "2822 IOCB failed %s iotag 0x%x "
16015                                         "xri 0x%x\n",
16016                                         fail_msg,
16017                                         piocbq->iotag, piocbq->sli4_xritag);
16018                         list_add_tail(&piocbq->list, &completions);
16019                 }
16020                 spin_unlock_irqrestore(&phba->hbalock, iflags);
16021         }
16022
16023         /* Cancel all the IOCBs that cannot be issued */
16024         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
16025                                 IOERR_SLI_ABORTED);
16026
16027         return pring->txq_cnt;
16028 }