]> nv-tegra.nvidia Code Review - linux-2.6.git/blob - drivers/scsi/lpfc/lpfc_sli.c
Merge git://git.kernel.org/pub/scm/linux/kernel/git/jejb/scsi-rc-fixes-2.6
[linux-2.6.git] / drivers / scsi / lpfc / lpfc_sli.c
1 /*******************************************************************
2  * This file is part of the Emulex Linux Device Driver for         *
3  * Fibre Channel Host Bus Adapters.                                *
4  * Copyright (C) 2004-2009 Emulex.  All rights reserved.           *
5  * EMULEX and SLI are trademarks of Emulex.                        *
6  * www.emulex.com                                                  *
7  * Portions Copyright (C) 2004-2005 Christoph Hellwig              *
8  *                                                                 *
9  * This program is free software; you can redistribute it and/or   *
10  * modify it under the terms of version 2 of the GNU General       *
11  * Public License as published by the Free Software Foundation.    *
12  * This program is distributed in the hope that it will be useful. *
13  * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND          *
14  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,  *
15  * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE      *
16  * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
17  * TO BE LEGALLY INVALID.  See the GNU General Public License for  *
18  * more details, a copy of which can be found in the file COPYING  *
19  * included with this package.                                     *
20  *******************************************************************/
21
22 #include <linux/blkdev.h>
23 #include <linux/pci.h>
24 #include <linux/interrupt.h>
25 #include <linux/delay.h>
26
27 #include <scsi/scsi.h>
28 #include <scsi/scsi_cmnd.h>
29 #include <scsi/scsi_device.h>
30 #include <scsi/scsi_host.h>
31 #include <scsi/scsi_transport_fc.h>
32 #include <scsi/fc/fc_fs.h>
33 #include <linux/aer.h>
34
35 #include "lpfc_hw4.h"
36 #include "lpfc_hw.h"
37 #include "lpfc_sli.h"
38 #include "lpfc_sli4.h"
39 #include "lpfc_nl.h"
40 #include "lpfc_disc.h"
41 #include "lpfc_scsi.h"
42 #include "lpfc.h"
43 #include "lpfc_crtn.h"
44 #include "lpfc_logmsg.h"
45 #include "lpfc_compat.h"
46 #include "lpfc_debugfs.h"
47 #include "lpfc_vport.h"
48
49 /* There are only four IOCB completion types. */
50 typedef enum _lpfc_iocb_type {
51         LPFC_UNKNOWN_IOCB,
52         LPFC_UNSOL_IOCB,
53         LPFC_SOL_IOCB,
54         LPFC_ABORT_IOCB
55 } lpfc_iocb_type;
56
57
58 /* Provide function prototypes local to this module. */
59 static int lpfc_sli_issue_mbox_s4(struct lpfc_hba *, LPFC_MBOXQ_t *,
60                                   uint32_t);
61 static int lpfc_sli4_read_rev(struct lpfc_hba *, LPFC_MBOXQ_t *,
62                               uint8_t *, uint32_t *);
63 static struct lpfc_iocbq *lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *,
64                                                          struct lpfc_iocbq *);
65 static void lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *,
66                                       struct hbq_dmabuf *);
67 static IOCB_t *
68 lpfc_get_iocb_from_iocbq(struct lpfc_iocbq *iocbq)
69 {
70         return &iocbq->iocb;
71 }
72
73 /**
74  * lpfc_sli4_wq_put - Put a Work Queue Entry on an Work Queue
75  * @q: The Work Queue to operate on.
76  * @wqe: The work Queue Entry to put on the Work queue.
77  *
78  * This routine will copy the contents of @wqe to the next available entry on
79  * the @q. This function will then ring the Work Queue Doorbell to signal the
80  * HBA to start processing the Work Queue Entry. This function returns 0 if
81  * successful. If no entries are available on @q then this function will return
82  * -ENOMEM.
83  * The caller is expected to hold the hbalock when calling this routine.
84  **/
85 static uint32_t
86 lpfc_sli4_wq_put(struct lpfc_queue *q, union lpfc_wqe *wqe)
87 {
88         union lpfc_wqe *temp_wqe = q->qe[q->host_index].wqe;
89         struct lpfc_register doorbell;
90         uint32_t host_index;
91
92         /* If the host has not yet processed the next entry then we are done */
93         if (((q->host_index + 1) % q->entry_count) == q->hba_index)
94                 return -ENOMEM;
95         /* set consumption flag every once in a while */
96         if (!((q->host_index + 1) % LPFC_RELEASE_NOTIFICATION_INTERVAL))
97                 bf_set(lpfc_wqe_gen_wqec, &wqe->generic, 1);
98
99         lpfc_sli_pcimem_bcopy(wqe, temp_wqe, q->entry_size);
100
101         /* Update the host index before invoking device */
102         host_index = q->host_index;
103         q->host_index = ((q->host_index + 1) % q->entry_count);
104
105         /* Ring Doorbell */
106         doorbell.word0 = 0;
107         bf_set(lpfc_wq_doorbell_num_posted, &doorbell, 1);
108         bf_set(lpfc_wq_doorbell_index, &doorbell, host_index);
109         bf_set(lpfc_wq_doorbell_id, &doorbell, q->queue_id);
110         writel(doorbell.word0, q->phba->sli4_hba.WQDBregaddr);
111         readl(q->phba->sli4_hba.WQDBregaddr); /* Flush */
112
113         return 0;
114 }
115
116 /**
117  * lpfc_sli4_wq_release - Updates internal hba index for WQ
118  * @q: The Work Queue to operate on.
119  * @index: The index to advance the hba index to.
120  *
121  * This routine will update the HBA index of a queue to reflect consumption of
122  * Work Queue Entries by the HBA. When the HBA indicates that it has consumed
123  * an entry the host calls this function to update the queue's internal
124  * pointers. This routine returns the number of entries that were consumed by
125  * the HBA.
126  **/
127 static uint32_t
128 lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)
129 {
130         uint32_t released = 0;
131
132         if (q->hba_index == index)
133                 return 0;
134         do {
135                 q->hba_index = ((q->hba_index + 1) % q->entry_count);
136                 released++;
137         } while (q->hba_index != index);
138         return released;
139 }
140
141 /**
142  * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue
143  * @q: The Mailbox Queue to operate on.
144  * @wqe: The Mailbox Queue Entry to put on the Work queue.
145  *
146  * This routine will copy the contents of @mqe to the next available entry on
147  * the @q. This function will then ring the Work Queue Doorbell to signal the
148  * HBA to start processing the Work Queue Entry. This function returns 0 if
149  * successful. If no entries are available on @q then this function will return
150  * -ENOMEM.
151  * The caller is expected to hold the hbalock when calling this routine.
152  **/
153 static uint32_t
154 lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)
155 {
156         struct lpfc_mqe *temp_mqe = q->qe[q->host_index].mqe;
157         struct lpfc_register doorbell;
158         uint32_t host_index;
159
160         /* If the host has not yet processed the next entry then we are done */
161         if (((q->host_index + 1) % q->entry_count) == q->hba_index)
162                 return -ENOMEM;
163         lpfc_sli_pcimem_bcopy(mqe, temp_mqe, q->entry_size);
164         /* Save off the mailbox pointer for completion */
165         q->phba->mbox = (MAILBOX_t *)temp_mqe;
166
167         /* Update the host index before invoking device */
168         host_index = q->host_index;
169         q->host_index = ((q->host_index + 1) % q->entry_count);
170
171         /* Ring Doorbell */
172         doorbell.word0 = 0;
173         bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);
174         bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);
175         writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);
176         readl(q->phba->sli4_hba.MQDBregaddr); /* Flush */
177         return 0;
178 }
179
180 /**
181  * lpfc_sli4_mq_release - Updates internal hba index for MQ
182  * @q: The Mailbox Queue to operate on.
183  *
184  * This routine will update the HBA index of a queue to reflect consumption of
185  * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed
186  * an entry the host calls this function to update the queue's internal
187  * pointers. This routine returns the number of entries that were consumed by
188  * the HBA.
189  **/
190 static uint32_t
191 lpfc_sli4_mq_release(struct lpfc_queue *q)
192 {
193         /* Clear the mailbox pointer for completion */
194         q->phba->mbox = NULL;
195         q->hba_index = ((q->hba_index + 1) % q->entry_count);
196         return 1;
197 }
198
199 /**
200  * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ
201  * @q: The Event Queue to get the first valid EQE from
202  *
203  * This routine will get the first valid Event Queue Entry from @q, update
204  * the queue's internal hba index, and return the EQE. If no valid EQEs are in
205  * the Queue (no more work to do), or the Queue is full of EQEs that have been
206  * processed, but not popped back to the HBA then this routine will return NULL.
207  **/
208 static struct lpfc_eqe *
209 lpfc_sli4_eq_get(struct lpfc_queue *q)
210 {
211         struct lpfc_eqe *eqe = q->qe[q->hba_index].eqe;
212
213         /* If the next EQE is not valid then we are done */
214         if (!bf_get(lpfc_eqe_valid, eqe))
215                 return NULL;
216         /* If the host has not yet processed the next entry then we are done */
217         if (((q->hba_index + 1) % q->entry_count) == q->host_index)
218                 return NULL;
219
220         q->hba_index = ((q->hba_index + 1) % q->entry_count);
221         return eqe;
222 }
223
224 /**
225  * lpfc_sli4_eq_release - Indicates the host has finished processing an EQ
226  * @q: The Event Queue that the host has completed processing for.
227  * @arm: Indicates whether the host wants to arms this CQ.
228  *
229  * This routine will mark all Event Queue Entries on @q, from the last
230  * known completed entry to the last entry that was processed, as completed
231  * by clearing the valid bit for each completion queue entry. Then it will
232  * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
233  * The internal host index in the @q will be updated by this routine to indicate
234  * that the host has finished processing the entries. The @arm parameter
235  * indicates that the queue should be rearmed when ringing the doorbell.
236  *
237  * This function will return the number of EQEs that were popped.
238  **/
239 uint32_t
240 lpfc_sli4_eq_release(struct lpfc_queue *q, bool arm)
241 {
242         uint32_t released = 0;
243         struct lpfc_eqe *temp_eqe;
244         struct lpfc_register doorbell;
245
246         /* while there are valid entries */
247         while (q->hba_index != q->host_index) {
248                 temp_eqe = q->qe[q->host_index].eqe;
249                 bf_set(lpfc_eqe_valid, temp_eqe, 0);
250                 released++;
251                 q->host_index = ((q->host_index + 1) % q->entry_count);
252         }
253         if (unlikely(released == 0 && !arm))
254                 return 0;
255
256         /* ring doorbell for number popped */
257         doorbell.word0 = 0;
258         if (arm) {
259                 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
260                 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
261         }
262         bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
263         bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
264         bf_set(lpfc_eqcq_doorbell_eqid, &doorbell, q->queue_id);
265         writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
266         /* PCI read to flush PCI pipeline on re-arming for INTx mode */
267         if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
268                 readl(q->phba->sli4_hba.EQCQDBregaddr);
269         return released;
270 }
271
272 /**
273  * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
274  * @q: The Completion Queue to get the first valid CQE from
275  *
276  * This routine will get the first valid Completion Queue Entry from @q, update
277  * the queue's internal hba index, and return the CQE. If no valid CQEs are in
278  * the Queue (no more work to do), or the Queue is full of CQEs that have been
279  * processed, but not popped back to the HBA then this routine will return NULL.
280  **/
281 static struct lpfc_cqe *
282 lpfc_sli4_cq_get(struct lpfc_queue *q)
283 {
284         struct lpfc_cqe *cqe;
285
286         /* If the next CQE is not valid then we are done */
287         if (!bf_get(lpfc_cqe_valid, q->qe[q->hba_index].cqe))
288                 return NULL;
289         /* If the host has not yet processed the next entry then we are done */
290         if (((q->hba_index + 1) % q->entry_count) == q->host_index)
291                 return NULL;
292
293         cqe = q->qe[q->hba_index].cqe;
294         q->hba_index = ((q->hba_index + 1) % q->entry_count);
295         return cqe;
296 }
297
298 /**
299  * lpfc_sli4_cq_release - Indicates the host has finished processing a CQ
300  * @q: The Completion Queue that the host has completed processing for.
301  * @arm: Indicates whether the host wants to arms this CQ.
302  *
303  * This routine will mark all Completion queue entries on @q, from the last
304  * known completed entry to the last entry that was processed, as completed
305  * by clearing the valid bit for each completion queue entry. Then it will
306  * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
307  * The internal host index in the @q will be updated by this routine to indicate
308  * that the host has finished processing the entries. The @arm parameter
309  * indicates that the queue should be rearmed when ringing the doorbell.
310  *
311  * This function will return the number of CQEs that were released.
312  **/
313 uint32_t
314 lpfc_sli4_cq_release(struct lpfc_queue *q, bool arm)
315 {
316         uint32_t released = 0;
317         struct lpfc_cqe *temp_qe;
318         struct lpfc_register doorbell;
319
320         /* while there are valid entries */
321         while (q->hba_index != q->host_index) {
322                 temp_qe = q->qe[q->host_index].cqe;
323                 bf_set(lpfc_cqe_valid, temp_qe, 0);
324                 released++;
325                 q->host_index = ((q->host_index + 1) % q->entry_count);
326         }
327         if (unlikely(released == 0 && !arm))
328                 return 0;
329
330         /* ring doorbell for number popped */
331         doorbell.word0 = 0;
332         if (arm)
333                 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
334         bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
335         bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
336         bf_set(lpfc_eqcq_doorbell_cqid, &doorbell, q->queue_id);
337         writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
338         return released;
339 }
340
341 /**
342  * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
343  * @q: The Header Receive Queue to operate on.
344  * @wqe: The Receive Queue Entry to put on the Receive queue.
345  *
346  * This routine will copy the contents of @wqe to the next available entry on
347  * the @q. This function will then ring the Receive Queue Doorbell to signal the
348  * HBA to start processing the Receive Queue Entry. This function returns the
349  * index that the rqe was copied to if successful. If no entries are available
350  * on @q then this function will return -ENOMEM.
351  * The caller is expected to hold the hbalock when calling this routine.
352  **/
353 static int
354 lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
355                  struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
356 {
357         struct lpfc_rqe *temp_hrqe = hq->qe[hq->host_index].rqe;
358         struct lpfc_rqe *temp_drqe = dq->qe[dq->host_index].rqe;
359         struct lpfc_register doorbell;
360         int put_index = hq->host_index;
361
362         if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
363                 return -EINVAL;
364         if (hq->host_index != dq->host_index)
365                 return -EINVAL;
366         /* If the host has not yet processed the next entry then we are done */
367         if (((hq->host_index + 1) % hq->entry_count) == hq->hba_index)
368                 return -EBUSY;
369         lpfc_sli_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
370         lpfc_sli_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
371
372         /* Update the host index to point to the next slot */
373         hq->host_index = ((hq->host_index + 1) % hq->entry_count);
374         dq->host_index = ((dq->host_index + 1) % dq->entry_count);
375
376         /* Ring The Header Receive Queue Doorbell */
377         if (!(hq->host_index % LPFC_RQ_POST_BATCH)) {
378                 doorbell.word0 = 0;
379                 bf_set(lpfc_rq_doorbell_num_posted, &doorbell,
380                        LPFC_RQ_POST_BATCH);
381                 bf_set(lpfc_rq_doorbell_id, &doorbell, hq->queue_id);
382                 writel(doorbell.word0, hq->phba->sli4_hba.RQDBregaddr);
383         }
384         return put_index;
385 }
386
387 /**
388  * lpfc_sli4_rq_release - Updates internal hba index for RQ
389  * @q: The Header Receive Queue to operate on.
390  *
391  * This routine will update the HBA index of a queue to reflect consumption of
392  * one Receive Queue Entry by the HBA. When the HBA indicates that it has
393  * consumed an entry the host calls this function to update the queue's
394  * internal pointers. This routine returns the number of entries that were
395  * consumed by the HBA.
396  **/
397 static uint32_t
398 lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
399 {
400         if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
401                 return 0;
402         hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
403         dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
404         return 1;
405 }
406
407 /**
408  * lpfc_cmd_iocb - Get next command iocb entry in the ring
409  * @phba: Pointer to HBA context object.
410  * @pring: Pointer to driver SLI ring object.
411  *
412  * This function returns pointer to next command iocb entry
413  * in the command ring. The caller must hold hbalock to prevent
414  * other threads consume the next command iocb.
415  * SLI-2/SLI-3 provide different sized iocbs.
416  **/
417 static inline IOCB_t *
418 lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
419 {
420         return (IOCB_t *) (((char *) pring->cmdringaddr) +
421                            pring->cmdidx * phba->iocb_cmd_size);
422 }
423
424 /**
425  * lpfc_resp_iocb - Get next response iocb entry in the ring
426  * @phba: Pointer to HBA context object.
427  * @pring: Pointer to driver SLI ring object.
428  *
429  * This function returns pointer to next response iocb entry
430  * in the response ring. The caller must hold hbalock to make sure
431  * that no other thread consume the next response iocb.
432  * SLI-2/SLI-3 provide different sized iocbs.
433  **/
434 static inline IOCB_t *
435 lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
436 {
437         return (IOCB_t *) (((char *) pring->rspringaddr) +
438                            pring->rspidx * phba->iocb_rsp_size);
439 }
440
441 /**
442  * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
443  * @phba: Pointer to HBA context object.
444  *
445  * This function is called with hbalock held. This function
446  * allocates a new driver iocb object from the iocb pool. If the
447  * allocation is successful, it returns pointer to the newly
448  * allocated iocb object else it returns NULL.
449  **/
450 static struct lpfc_iocbq *
451 __lpfc_sli_get_iocbq(struct lpfc_hba *phba)
452 {
453         struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
454         struct lpfc_iocbq * iocbq = NULL;
455
456         list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
457         return iocbq;
458 }
459
460 /**
461  * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
462  * @phba: Pointer to HBA context object.
463  * @xritag: XRI value.
464  *
465  * This function clears the sglq pointer from the array of acive
466  * sglq's. The xritag that is passed in is used to index into the
467  * array. Before the xritag can be used it needs to be adjusted
468  * by subtracting the xribase.
469  *
470  * Returns sglq ponter = success, NULL = Failure.
471  **/
472 static struct lpfc_sglq *
473 __lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
474 {
475         uint16_t adj_xri;
476         struct lpfc_sglq *sglq;
477         adj_xri = xritag - phba->sli4_hba.max_cfg_param.xri_base;
478         if (adj_xri > phba->sli4_hba.max_cfg_param.max_xri)
479                 return NULL;
480         sglq = phba->sli4_hba.lpfc_sglq_active_list[adj_xri];
481         phba->sli4_hba.lpfc_sglq_active_list[adj_xri] = NULL;
482         return sglq;
483 }
484
485 /**
486  * __lpfc_get_active_sglq - Get the active sglq for this XRI.
487  * @phba: Pointer to HBA context object.
488  * @xritag: XRI value.
489  *
490  * This function returns the sglq pointer from the array of acive
491  * sglq's. The xritag that is passed in is used to index into the
492  * array. Before the xritag can be used it needs to be adjusted
493  * by subtracting the xribase.
494  *
495  * Returns sglq ponter = success, NULL = Failure.
496  **/
497 static struct lpfc_sglq *
498 __lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
499 {
500         uint16_t adj_xri;
501         struct lpfc_sglq *sglq;
502         adj_xri = xritag - phba->sli4_hba.max_cfg_param.xri_base;
503         if (adj_xri > phba->sli4_hba.max_cfg_param.max_xri)
504                 return NULL;
505         sglq =  phba->sli4_hba.lpfc_sglq_active_list[adj_xri];
506         return sglq;
507 }
508
509 /**
510  * __lpfc_sli_get_sglq - Allocates an iocb object from sgl pool
511  * @phba: Pointer to HBA context object.
512  *
513  * This function is called with hbalock held. This function
514  * Gets a new driver sglq object from the sglq list. If the
515  * list is not empty then it is successful, it returns pointer to the newly
516  * allocated sglq object else it returns NULL.
517  **/
518 static struct lpfc_sglq *
519 __lpfc_sli_get_sglq(struct lpfc_hba *phba)
520 {
521         struct list_head *lpfc_sgl_list = &phba->sli4_hba.lpfc_sgl_list;
522         struct lpfc_sglq *sglq = NULL;
523         uint16_t adj_xri;
524         list_remove_head(lpfc_sgl_list, sglq, struct lpfc_sglq, list);
525         if (!sglq)
526                 return NULL;
527         adj_xri = sglq->sli4_xritag - phba->sli4_hba.max_cfg_param.xri_base;
528         phba->sli4_hba.lpfc_sglq_active_list[adj_xri] = sglq;
529         return sglq;
530 }
531
532 /**
533  * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
534  * @phba: Pointer to HBA context object.
535  *
536  * This function is called with no lock held. This function
537  * allocates a new driver iocb object from the iocb pool. If the
538  * allocation is successful, it returns pointer to the newly
539  * allocated iocb object else it returns NULL.
540  **/
541 struct lpfc_iocbq *
542 lpfc_sli_get_iocbq(struct lpfc_hba *phba)
543 {
544         struct lpfc_iocbq * iocbq = NULL;
545         unsigned long iflags;
546
547         spin_lock_irqsave(&phba->hbalock, iflags);
548         iocbq = __lpfc_sli_get_iocbq(phba);
549         spin_unlock_irqrestore(&phba->hbalock, iflags);
550         return iocbq;
551 }
552
553 /**
554  * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
555  * @phba: Pointer to HBA context object.
556  * @iocbq: Pointer to driver iocb object.
557  *
558  * This function is called with hbalock held to release driver
559  * iocb object to the iocb pool. The iotag in the iocb object
560  * does not change for each use of the iocb object. This function
561  * clears all other fields of the iocb object when it is freed.
562  * The sqlq structure that holds the xritag and phys and virtual
563  * mappings for the scatter gather list is retrieved from the
564  * active array of sglq. The get of the sglq pointer also clears
565  * the entry in the array. If the status of the IO indiactes that
566  * this IO was aborted then the sglq entry it put on the
567  * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
568  * IO has good status or fails for any other reason then the sglq
569  * entry is added to the free list (lpfc_sgl_list).
570  **/
571 static void
572 __lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
573 {
574         struct lpfc_sglq *sglq;
575         size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
576         unsigned long iflag;
577
578         if (iocbq->sli4_xritag == NO_XRI)
579                 sglq = NULL;
580         else
581                 sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_xritag);
582         if (sglq)  {
583                 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED
584                         && ((iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
585                         && (iocbq->iocb.un.ulpWord[4]
586                                 == IOERR_ABORT_REQUESTED))) {
587                         spin_lock_irqsave(&phba->sli4_hba.abts_sgl_list_lock,
588                                         iflag);
589                         list_add(&sglq->list,
590                                 &phba->sli4_hba.lpfc_abts_els_sgl_list);
591                         spin_unlock_irqrestore(
592                                 &phba->sli4_hba.abts_sgl_list_lock, iflag);
593                 } else
594                         list_add(&sglq->list, &phba->sli4_hba.lpfc_sgl_list);
595         }
596
597
598         /*
599          * Clean all volatile data fields, preserve iotag and node struct.
600          */
601         memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
602         iocbq->sli4_xritag = NO_XRI;
603         list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
604 }
605
606 /**
607  * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
608  * @phba: Pointer to HBA context object.
609  * @iocbq: Pointer to driver iocb object.
610  *
611  * This function is called with hbalock held to release driver
612  * iocb object to the iocb pool. The iotag in the iocb object
613  * does not change for each use of the iocb object. This function
614  * clears all other fields of the iocb object when it is freed.
615  **/
616 static void
617 __lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
618 {
619         size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
620
621         /*
622          * Clean all volatile data fields, preserve iotag and node struct.
623          */
624         memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
625         iocbq->sli4_xritag = NO_XRI;
626         list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
627 }
628
629 /**
630  * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
631  * @phba: Pointer to HBA context object.
632  * @iocbq: Pointer to driver iocb object.
633  *
634  * This function is called with hbalock held to release driver
635  * iocb object to the iocb pool. The iotag in the iocb object
636  * does not change for each use of the iocb object. This function
637  * clears all other fields of the iocb object when it is freed.
638  **/
639 static void
640 __lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
641 {
642         phba->__lpfc_sli_release_iocbq(phba, iocbq);
643 }
644
645 /**
646  * lpfc_sli_release_iocbq - Release iocb to the iocb pool
647  * @phba: Pointer to HBA context object.
648  * @iocbq: Pointer to driver iocb object.
649  *
650  * This function is called with no lock held to release the iocb to
651  * iocb pool.
652  **/
653 void
654 lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
655 {
656         unsigned long iflags;
657
658         /*
659          * Clean all volatile data fields, preserve iotag and node struct.
660          */
661         spin_lock_irqsave(&phba->hbalock, iflags);
662         __lpfc_sli_release_iocbq(phba, iocbq);
663         spin_unlock_irqrestore(&phba->hbalock, iflags);
664 }
665
666 /**
667  * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
668  * @phba: Pointer to HBA context object.
669  * @iocblist: List of IOCBs.
670  * @ulpstatus: ULP status in IOCB command field.
671  * @ulpWord4: ULP word-4 in IOCB command field.
672  *
673  * This function is called with a list of IOCBs to cancel. It cancels the IOCB
674  * on the list by invoking the complete callback function associated with the
675  * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
676  * fields.
677  **/
678 void
679 lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
680                       uint32_t ulpstatus, uint32_t ulpWord4)
681 {
682         struct lpfc_iocbq *piocb;
683
684         while (!list_empty(iocblist)) {
685                 list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
686
687                 if (!piocb->iocb_cmpl)
688                         lpfc_sli_release_iocbq(phba, piocb);
689                 else {
690                         piocb->iocb.ulpStatus = ulpstatus;
691                         piocb->iocb.un.ulpWord[4] = ulpWord4;
692                         (piocb->iocb_cmpl) (phba, piocb, piocb);
693                 }
694         }
695         return;
696 }
697
698 /**
699  * lpfc_sli_iocb_cmd_type - Get the iocb type
700  * @iocb_cmnd: iocb command code.
701  *
702  * This function is called by ring event handler function to get the iocb type.
703  * This function translates the iocb command to an iocb command type used to
704  * decide the final disposition of each completed IOCB.
705  * The function returns
706  * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
707  * LPFC_SOL_IOCB     if it is a solicited iocb completion
708  * LPFC_ABORT_IOCB   if it is an abort iocb
709  * LPFC_UNSOL_IOCB   if it is an unsolicited iocb
710  *
711  * The caller is not required to hold any lock.
712  **/
713 static lpfc_iocb_type
714 lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
715 {
716         lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
717
718         if (iocb_cmnd > CMD_MAX_IOCB_CMD)
719                 return 0;
720
721         switch (iocb_cmnd) {
722         case CMD_XMIT_SEQUENCE_CR:
723         case CMD_XMIT_SEQUENCE_CX:
724         case CMD_XMIT_BCAST_CN:
725         case CMD_XMIT_BCAST_CX:
726         case CMD_ELS_REQUEST_CR:
727         case CMD_ELS_REQUEST_CX:
728         case CMD_CREATE_XRI_CR:
729         case CMD_CREATE_XRI_CX:
730         case CMD_GET_RPI_CN:
731         case CMD_XMIT_ELS_RSP_CX:
732         case CMD_GET_RPI_CR:
733         case CMD_FCP_IWRITE_CR:
734         case CMD_FCP_IWRITE_CX:
735         case CMD_FCP_IREAD_CR:
736         case CMD_FCP_IREAD_CX:
737         case CMD_FCP_ICMND_CR:
738         case CMD_FCP_ICMND_CX:
739         case CMD_FCP_TSEND_CX:
740         case CMD_FCP_TRSP_CX:
741         case CMD_FCP_TRECEIVE_CX:
742         case CMD_FCP_AUTO_TRSP_CX:
743         case CMD_ADAPTER_MSG:
744         case CMD_ADAPTER_DUMP:
745         case CMD_XMIT_SEQUENCE64_CR:
746         case CMD_XMIT_SEQUENCE64_CX:
747         case CMD_XMIT_BCAST64_CN:
748         case CMD_XMIT_BCAST64_CX:
749         case CMD_ELS_REQUEST64_CR:
750         case CMD_ELS_REQUEST64_CX:
751         case CMD_FCP_IWRITE64_CR:
752         case CMD_FCP_IWRITE64_CX:
753         case CMD_FCP_IREAD64_CR:
754         case CMD_FCP_IREAD64_CX:
755         case CMD_FCP_ICMND64_CR:
756         case CMD_FCP_ICMND64_CX:
757         case CMD_FCP_TSEND64_CX:
758         case CMD_FCP_TRSP64_CX:
759         case CMD_FCP_TRECEIVE64_CX:
760         case CMD_GEN_REQUEST64_CR:
761         case CMD_GEN_REQUEST64_CX:
762         case CMD_XMIT_ELS_RSP64_CX:
763         case DSSCMD_IWRITE64_CR:
764         case DSSCMD_IWRITE64_CX:
765         case DSSCMD_IREAD64_CR:
766         case DSSCMD_IREAD64_CX:
767         case DSSCMD_INVALIDATE_DEK:
768         case DSSCMD_SET_KEK:
769         case DSSCMD_GET_KEK_ID:
770         case DSSCMD_GEN_XFER:
771                 type = LPFC_SOL_IOCB;
772                 break;
773         case CMD_ABORT_XRI_CN:
774         case CMD_ABORT_XRI_CX:
775         case CMD_CLOSE_XRI_CN:
776         case CMD_CLOSE_XRI_CX:
777         case CMD_XRI_ABORTED_CX:
778         case CMD_ABORT_MXRI64_CN:
779         case CMD_XMIT_BLS_RSP64_CX:
780                 type = LPFC_ABORT_IOCB;
781                 break;
782         case CMD_RCV_SEQUENCE_CX:
783         case CMD_RCV_ELS_REQ_CX:
784         case CMD_RCV_SEQUENCE64_CX:
785         case CMD_RCV_ELS_REQ64_CX:
786         case CMD_ASYNC_STATUS:
787         case CMD_IOCB_RCV_SEQ64_CX:
788         case CMD_IOCB_RCV_ELS64_CX:
789         case CMD_IOCB_RCV_CONT64_CX:
790         case CMD_IOCB_RET_XRI64_CX:
791                 type = LPFC_UNSOL_IOCB;
792                 break;
793         case CMD_IOCB_XMIT_MSEQ64_CR:
794         case CMD_IOCB_XMIT_MSEQ64_CX:
795         case CMD_IOCB_RCV_SEQ_LIST64_CX:
796         case CMD_IOCB_RCV_ELS_LIST64_CX:
797         case CMD_IOCB_CLOSE_EXTENDED_CN:
798         case CMD_IOCB_ABORT_EXTENDED_CN:
799         case CMD_IOCB_RET_HBQE64_CN:
800         case CMD_IOCB_FCP_IBIDIR64_CR:
801         case CMD_IOCB_FCP_IBIDIR64_CX:
802         case CMD_IOCB_FCP_ITASKMGT64_CX:
803         case CMD_IOCB_LOGENTRY_CN:
804         case CMD_IOCB_LOGENTRY_ASYNC_CN:
805                 printk("%s - Unhandled SLI-3 Command x%x\n",
806                                 __func__, iocb_cmnd);
807                 type = LPFC_UNKNOWN_IOCB;
808                 break;
809         default:
810                 type = LPFC_UNKNOWN_IOCB;
811                 break;
812         }
813
814         return type;
815 }
816
817 /**
818  * lpfc_sli_ring_map - Issue config_ring mbox for all rings
819  * @phba: Pointer to HBA context object.
820  *
821  * This function is called from SLI initialization code
822  * to configure every ring of the HBA's SLI interface. The
823  * caller is not required to hold any lock. This function issues
824  * a config_ring mailbox command for each ring.
825  * This function returns zero if successful else returns a negative
826  * error code.
827  **/
828 static int
829 lpfc_sli_ring_map(struct lpfc_hba *phba)
830 {
831         struct lpfc_sli *psli = &phba->sli;
832         LPFC_MBOXQ_t *pmb;
833         MAILBOX_t *pmbox;
834         int i, rc, ret = 0;
835
836         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
837         if (!pmb)
838                 return -ENOMEM;
839         pmbox = &pmb->u.mb;
840         phba->link_state = LPFC_INIT_MBX_CMDS;
841         for (i = 0; i < psli->num_rings; i++) {
842                 lpfc_config_ring(phba, i, pmb);
843                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
844                 if (rc != MBX_SUCCESS) {
845                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
846                                         "0446 Adapter failed to init (%d), "
847                                         "mbxCmd x%x CFG_RING, mbxStatus x%x, "
848                                         "ring %d\n",
849                                         rc, pmbox->mbxCommand,
850                                         pmbox->mbxStatus, i);
851                         phba->link_state = LPFC_HBA_ERROR;
852                         ret = -ENXIO;
853                         break;
854                 }
855         }
856         mempool_free(pmb, phba->mbox_mem_pool);
857         return ret;
858 }
859
860 /**
861  * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
862  * @phba: Pointer to HBA context object.
863  * @pring: Pointer to driver SLI ring object.
864  * @piocb: Pointer to the driver iocb object.
865  *
866  * This function is called with hbalock held. The function adds the
867  * new iocb to txcmplq of the given ring. This function always returns
868  * 0. If this function is called for ELS ring, this function checks if
869  * there is a vport associated with the ELS command. This function also
870  * starts els_tmofunc timer if this is an ELS command.
871  **/
872 static int
873 lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
874                         struct lpfc_iocbq *piocb)
875 {
876         list_add_tail(&piocb->list, &pring->txcmplq);
877         pring->txcmplq_cnt++;
878         if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
879            (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
880            (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
881                 if (!piocb->vport)
882                         BUG();
883                 else
884                         mod_timer(&piocb->vport->els_tmofunc,
885                                   jiffies + HZ * (phba->fc_ratov << 1));
886         }
887
888
889         return 0;
890 }
891
892 /**
893  * lpfc_sli_ringtx_get - Get first element of the txq
894  * @phba: Pointer to HBA context object.
895  * @pring: Pointer to driver SLI ring object.
896  *
897  * This function is called with hbalock held to get next
898  * iocb in txq of the given ring. If there is any iocb in
899  * the txq, the function returns first iocb in the list after
900  * removing the iocb from the list, else it returns NULL.
901  **/
902 static struct lpfc_iocbq *
903 lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
904 {
905         struct lpfc_iocbq *cmd_iocb;
906
907         list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
908         if (cmd_iocb != NULL)
909                 pring->txq_cnt--;
910         return cmd_iocb;
911 }
912
913 /**
914  * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
915  * @phba: Pointer to HBA context object.
916  * @pring: Pointer to driver SLI ring object.
917  *
918  * This function is called with hbalock held and the caller must post the
919  * iocb without releasing the lock. If the caller releases the lock,
920  * iocb slot returned by the function is not guaranteed to be available.
921  * The function returns pointer to the next available iocb slot if there
922  * is available slot in the ring, else it returns NULL.
923  * If the get index of the ring is ahead of the put index, the function
924  * will post an error attention event to the worker thread to take the
925  * HBA to offline state.
926  **/
927 static IOCB_t *
928 lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
929 {
930         struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
931         uint32_t  max_cmd_idx = pring->numCiocb;
932         if ((pring->next_cmdidx == pring->cmdidx) &&
933            (++pring->next_cmdidx >= max_cmd_idx))
934                 pring->next_cmdidx = 0;
935
936         if (unlikely(pring->local_getidx == pring->next_cmdidx)) {
937
938                 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
939
940                 if (unlikely(pring->local_getidx >= max_cmd_idx)) {
941                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
942                                         "0315 Ring %d issue: portCmdGet %d "
943                                         "is bigger than cmd ring %d\n",
944                                         pring->ringno,
945                                         pring->local_getidx, max_cmd_idx);
946
947                         phba->link_state = LPFC_HBA_ERROR;
948                         /*
949                          * All error attention handlers are posted to
950                          * worker thread
951                          */
952                         phba->work_ha |= HA_ERATT;
953                         phba->work_hs = HS_FFER3;
954
955                         lpfc_worker_wake_up(phba);
956
957                         return NULL;
958                 }
959
960                 if (pring->local_getidx == pring->next_cmdidx)
961                         return NULL;
962         }
963
964         return lpfc_cmd_iocb(phba, pring);
965 }
966
967 /**
968  * lpfc_sli_next_iotag - Get an iotag for the iocb
969  * @phba: Pointer to HBA context object.
970  * @iocbq: Pointer to driver iocb object.
971  *
972  * This function gets an iotag for the iocb. If there is no unused iotag and
973  * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
974  * array and assigns a new iotag.
975  * The function returns the allocated iotag if successful, else returns zero.
976  * Zero is not a valid iotag.
977  * The caller is not required to hold any lock.
978  **/
979 uint16_t
980 lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
981 {
982         struct lpfc_iocbq **new_arr;
983         struct lpfc_iocbq **old_arr;
984         size_t new_len;
985         struct lpfc_sli *psli = &phba->sli;
986         uint16_t iotag;
987
988         spin_lock_irq(&phba->hbalock);
989         iotag = psli->last_iotag;
990         if(++iotag < psli->iocbq_lookup_len) {
991                 psli->last_iotag = iotag;
992                 psli->iocbq_lookup[iotag] = iocbq;
993                 spin_unlock_irq(&phba->hbalock);
994                 iocbq->iotag = iotag;
995                 return iotag;
996         } else if (psli->iocbq_lookup_len < (0xffff
997                                            - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
998                 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
999                 spin_unlock_irq(&phba->hbalock);
1000                 new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
1001                                   GFP_KERNEL);
1002                 if (new_arr) {
1003                         spin_lock_irq(&phba->hbalock);
1004                         old_arr = psli->iocbq_lookup;
1005                         if (new_len <= psli->iocbq_lookup_len) {
1006                                 /* highly unprobable case */
1007                                 kfree(new_arr);
1008                                 iotag = psli->last_iotag;
1009                                 if(++iotag < psli->iocbq_lookup_len) {
1010                                         psli->last_iotag = iotag;
1011                                         psli->iocbq_lookup[iotag] = iocbq;
1012                                         spin_unlock_irq(&phba->hbalock);
1013                                         iocbq->iotag = iotag;
1014                                         return iotag;
1015                                 }
1016                                 spin_unlock_irq(&phba->hbalock);
1017                                 return 0;
1018                         }
1019                         if (psli->iocbq_lookup)
1020                                 memcpy(new_arr, old_arr,
1021                                        ((psli->last_iotag  + 1) *
1022                                         sizeof (struct lpfc_iocbq *)));
1023                         psli->iocbq_lookup = new_arr;
1024                         psli->iocbq_lookup_len = new_len;
1025                         psli->last_iotag = iotag;
1026                         psli->iocbq_lookup[iotag] = iocbq;
1027                         spin_unlock_irq(&phba->hbalock);
1028                         iocbq->iotag = iotag;
1029                         kfree(old_arr);
1030                         return iotag;
1031                 }
1032         } else
1033                 spin_unlock_irq(&phba->hbalock);
1034
1035         lpfc_printf_log(phba, KERN_ERR,LOG_SLI,
1036                         "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
1037                         psli->last_iotag);
1038
1039         return 0;
1040 }
1041
1042 /**
1043  * lpfc_sli_submit_iocb - Submit an iocb to the firmware
1044  * @phba: Pointer to HBA context object.
1045  * @pring: Pointer to driver SLI ring object.
1046  * @iocb: Pointer to iocb slot in the ring.
1047  * @nextiocb: Pointer to driver iocb object which need to be
1048  *            posted to firmware.
1049  *
1050  * This function is called with hbalock held to post a new iocb to
1051  * the firmware. This function copies the new iocb to ring iocb slot and
1052  * updates the ring pointers. It adds the new iocb to txcmplq if there is
1053  * a completion call back for this iocb else the function will free the
1054  * iocb object.
1055  **/
1056 static void
1057 lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1058                 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
1059 {
1060         /*
1061          * Set up an iotag
1062          */
1063         nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
1064
1065
1066         if (pring->ringno == LPFC_ELS_RING) {
1067                 lpfc_debugfs_slow_ring_trc(phba,
1068                         "IOCB cmd ring:   wd4:x%08x wd6:x%08x wd7:x%08x",
1069                         *(((uint32_t *) &nextiocb->iocb) + 4),
1070                         *(((uint32_t *) &nextiocb->iocb) + 6),
1071                         *(((uint32_t *) &nextiocb->iocb) + 7));
1072         }
1073
1074         /*
1075          * Issue iocb command to adapter
1076          */
1077         lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
1078         wmb();
1079         pring->stats.iocb_cmd++;
1080
1081         /*
1082          * If there is no completion routine to call, we can release the
1083          * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
1084          * that have no rsp ring completion, iocb_cmpl MUST be NULL.
1085          */
1086         if (nextiocb->iocb_cmpl)
1087                 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
1088         else
1089                 __lpfc_sli_release_iocbq(phba, nextiocb);
1090
1091         /*
1092          * Let the HBA know what IOCB slot will be the next one the
1093          * driver will put a command into.
1094          */
1095         pring->cmdidx = pring->next_cmdidx;
1096         writel(pring->cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
1097 }
1098
1099 /**
1100  * lpfc_sli_update_full_ring - Update the chip attention register
1101  * @phba: Pointer to HBA context object.
1102  * @pring: Pointer to driver SLI ring object.
1103  *
1104  * The caller is not required to hold any lock for calling this function.
1105  * This function updates the chip attention bits for the ring to inform firmware
1106  * that there are pending work to be done for this ring and requests an
1107  * interrupt when there is space available in the ring. This function is
1108  * called when the driver is unable to post more iocbs to the ring due
1109  * to unavailability of space in the ring.
1110  **/
1111 static void
1112 lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1113 {
1114         int ringno = pring->ringno;
1115
1116         pring->flag |= LPFC_CALL_RING_AVAILABLE;
1117
1118         wmb();
1119
1120         /*
1121          * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
1122          * The HBA will tell us when an IOCB entry is available.
1123          */
1124         writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
1125         readl(phba->CAregaddr); /* flush */
1126
1127         pring->stats.iocb_cmd_full++;
1128 }
1129
1130 /**
1131  * lpfc_sli_update_ring - Update chip attention register
1132  * @phba: Pointer to HBA context object.
1133  * @pring: Pointer to driver SLI ring object.
1134  *
1135  * This function updates the chip attention register bit for the
1136  * given ring to inform HBA that there is more work to be done
1137  * in this ring. The caller is not required to hold any lock.
1138  **/
1139 static void
1140 lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1141 {
1142         int ringno = pring->ringno;
1143
1144         /*
1145          * Tell the HBA that there is work to do in this ring.
1146          */
1147         if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
1148                 wmb();
1149                 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
1150                 readl(phba->CAregaddr); /* flush */
1151         }
1152 }
1153
1154 /**
1155  * lpfc_sli_resume_iocb - Process iocbs in the txq
1156  * @phba: Pointer to HBA context object.
1157  * @pring: Pointer to driver SLI ring object.
1158  *
1159  * This function is called with hbalock held to post pending iocbs
1160  * in the txq to the firmware. This function is called when driver
1161  * detects space available in the ring.
1162  **/
1163 static void
1164 lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1165 {
1166         IOCB_t *iocb;
1167         struct lpfc_iocbq *nextiocb;
1168
1169         /*
1170          * Check to see if:
1171          *  (a) there is anything on the txq to send
1172          *  (b) link is up
1173          *  (c) link attention events can be processed (fcp ring only)
1174          *  (d) IOCB processing is not blocked by the outstanding mbox command.
1175          */
1176         if (pring->txq_cnt &&
1177             lpfc_is_link_up(phba) &&
1178             (pring->ringno != phba->sli.fcp_ring ||
1179              phba->sli.sli_flag & LPFC_PROCESS_LA)) {
1180
1181                 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
1182                        (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
1183                         lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
1184
1185                 if (iocb)
1186                         lpfc_sli_update_ring(phba, pring);
1187                 else
1188                         lpfc_sli_update_full_ring(phba, pring);
1189         }
1190
1191         return;
1192 }
1193
1194 /**
1195  * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
1196  * @phba: Pointer to HBA context object.
1197  * @hbqno: HBQ number.
1198  *
1199  * This function is called with hbalock held to get the next
1200  * available slot for the given HBQ. If there is free slot
1201  * available for the HBQ it will return pointer to the next available
1202  * HBQ entry else it will return NULL.
1203  **/
1204 static struct lpfc_hbq_entry *
1205 lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
1206 {
1207         struct hbq_s *hbqp = &phba->hbqs[hbqno];
1208
1209         if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
1210             ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
1211                 hbqp->next_hbqPutIdx = 0;
1212
1213         if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
1214                 uint32_t raw_index = phba->hbq_get[hbqno];
1215                 uint32_t getidx = le32_to_cpu(raw_index);
1216
1217                 hbqp->local_hbqGetIdx = getidx;
1218
1219                 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
1220                         lpfc_printf_log(phba, KERN_ERR,
1221                                         LOG_SLI | LOG_VPORT,
1222                                         "1802 HBQ %d: local_hbqGetIdx "
1223                                         "%u is > than hbqp->entry_count %u\n",
1224                                         hbqno, hbqp->local_hbqGetIdx,
1225                                         hbqp->entry_count);
1226
1227                         phba->link_state = LPFC_HBA_ERROR;
1228                         return NULL;
1229                 }
1230
1231                 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
1232                         return NULL;
1233         }
1234
1235         return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
1236                         hbqp->hbqPutIdx;
1237 }
1238
1239 /**
1240  * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
1241  * @phba: Pointer to HBA context object.
1242  *
1243  * This function is called with no lock held to free all the
1244  * hbq buffers while uninitializing the SLI interface. It also
1245  * frees the HBQ buffers returned by the firmware but not yet
1246  * processed by the upper layers.
1247  **/
1248 void
1249 lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
1250 {
1251         struct lpfc_dmabuf *dmabuf, *next_dmabuf;
1252         struct hbq_dmabuf *hbq_buf;
1253         unsigned long flags;
1254         int i, hbq_count;
1255         uint32_t hbqno;
1256
1257         hbq_count = lpfc_sli_hbq_count();
1258         /* Return all memory used by all HBQs */
1259         spin_lock_irqsave(&phba->hbalock, flags);
1260         for (i = 0; i < hbq_count; ++i) {
1261                 list_for_each_entry_safe(dmabuf, next_dmabuf,
1262                                 &phba->hbqs[i].hbq_buffer_list, list) {
1263                         hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1264                         list_del(&hbq_buf->dbuf.list);
1265                         (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
1266                 }
1267                 phba->hbqs[i].buffer_count = 0;
1268         }
1269         /* Return all HBQ buffer that are in-fly */
1270         list_for_each_entry_safe(dmabuf, next_dmabuf, &phba->rb_pend_list,
1271                                  list) {
1272                 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1273                 list_del(&hbq_buf->dbuf.list);
1274                 if (hbq_buf->tag == -1) {
1275                         (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1276                                 (phba, hbq_buf);
1277                 } else {
1278                         hbqno = hbq_buf->tag >> 16;
1279                         if (hbqno >= LPFC_MAX_HBQS)
1280                                 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1281                                         (phba, hbq_buf);
1282                         else
1283                                 (phba->hbqs[hbqno].hbq_free_buffer)(phba,
1284                                         hbq_buf);
1285                 }
1286         }
1287
1288         /* Mark the HBQs not in use */
1289         phba->hbq_in_use = 0;
1290         spin_unlock_irqrestore(&phba->hbalock, flags);
1291 }
1292
1293 /**
1294  * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
1295  * @phba: Pointer to HBA context object.
1296  * @hbqno: HBQ number.
1297  * @hbq_buf: Pointer to HBQ buffer.
1298  *
1299  * This function is called with the hbalock held to post a
1300  * hbq buffer to the firmware. If the function finds an empty
1301  * slot in the HBQ, it will post the buffer. The function will return
1302  * pointer to the hbq entry if it successfully post the buffer
1303  * else it will return NULL.
1304  **/
1305 static int
1306 lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
1307                          struct hbq_dmabuf *hbq_buf)
1308 {
1309         return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
1310 }
1311
1312 /**
1313  * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
1314  * @phba: Pointer to HBA context object.
1315  * @hbqno: HBQ number.
1316  * @hbq_buf: Pointer to HBQ buffer.
1317  *
1318  * This function is called with the hbalock held to post a hbq buffer to the
1319  * firmware. If the function finds an empty slot in the HBQ, it will post the
1320  * buffer and place it on the hbq_buffer_list. The function will return zero if
1321  * it successfully post the buffer else it will return an error.
1322  **/
1323 static int
1324 lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
1325                             struct hbq_dmabuf *hbq_buf)
1326 {
1327         struct lpfc_hbq_entry *hbqe;
1328         dma_addr_t physaddr = hbq_buf->dbuf.phys;
1329
1330         /* Get next HBQ entry slot to use */
1331         hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
1332         if (hbqe) {
1333                 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1334
1335                 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1336                 hbqe->bde.addrLow  = le32_to_cpu(putPaddrLow(physaddr));
1337                 hbqe->bde.tus.f.bdeSize = hbq_buf->size;
1338                 hbqe->bde.tus.f.bdeFlags = 0;
1339                 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
1340                 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
1341                                 /* Sync SLIM */
1342                 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
1343                 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
1344                                 /* flush */
1345                 readl(phba->hbq_put + hbqno);
1346                 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
1347                 return 0;
1348         } else
1349                 return -ENOMEM;
1350 }
1351
1352 /**
1353  * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
1354  * @phba: Pointer to HBA context object.
1355  * @hbqno: HBQ number.
1356  * @hbq_buf: Pointer to HBQ buffer.
1357  *
1358  * This function is called with the hbalock held to post an RQE to the SLI4
1359  * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
1360  * the hbq_buffer_list and return zero, otherwise it will return an error.
1361  **/
1362 static int
1363 lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
1364                             struct hbq_dmabuf *hbq_buf)
1365 {
1366         int rc;
1367         struct lpfc_rqe hrqe;
1368         struct lpfc_rqe drqe;
1369
1370         hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
1371         hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
1372         drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
1373         drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
1374         rc = lpfc_sli4_rq_put(phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
1375                               &hrqe, &drqe);
1376         if (rc < 0)
1377                 return rc;
1378         hbq_buf->tag = rc;
1379         list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
1380         return 0;
1381 }
1382
1383 /* HBQ for ELS and CT traffic. */
1384 static struct lpfc_hbq_init lpfc_els_hbq = {
1385         .rn = 1,
1386         .entry_count = 256,
1387         .mask_count = 0,
1388         .profile = 0,
1389         .ring_mask = (1 << LPFC_ELS_RING),
1390         .buffer_count = 0,
1391         .init_count = 40,
1392         .add_count = 40,
1393 };
1394
1395 /* HBQ for the extra ring if needed */
1396 static struct lpfc_hbq_init lpfc_extra_hbq = {
1397         .rn = 1,
1398         .entry_count = 200,
1399         .mask_count = 0,
1400         .profile = 0,
1401         .ring_mask = (1 << LPFC_EXTRA_RING),
1402         .buffer_count = 0,
1403         .init_count = 0,
1404         .add_count = 5,
1405 };
1406
1407 /* Array of HBQs */
1408 struct lpfc_hbq_init *lpfc_hbq_defs[] = {
1409         &lpfc_els_hbq,
1410         &lpfc_extra_hbq,
1411 };
1412
1413 /**
1414  * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
1415  * @phba: Pointer to HBA context object.
1416  * @hbqno: HBQ number.
1417  * @count: Number of HBQ buffers to be posted.
1418  *
1419  * This function is called with no lock held to post more hbq buffers to the
1420  * given HBQ. The function returns the number of HBQ buffers successfully
1421  * posted.
1422  **/
1423 static int
1424 lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
1425 {
1426         uint32_t i, posted = 0;
1427         unsigned long flags;
1428         struct hbq_dmabuf *hbq_buffer;
1429         LIST_HEAD(hbq_buf_list);
1430         if (!phba->hbqs[hbqno].hbq_alloc_buffer)
1431                 return 0;
1432
1433         if ((phba->hbqs[hbqno].buffer_count + count) >
1434             lpfc_hbq_defs[hbqno]->entry_count)
1435                 count = lpfc_hbq_defs[hbqno]->entry_count -
1436                                         phba->hbqs[hbqno].buffer_count;
1437         if (!count)
1438                 return 0;
1439         /* Allocate HBQ entries */
1440         for (i = 0; i < count; i++) {
1441                 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
1442                 if (!hbq_buffer)
1443                         break;
1444                 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
1445         }
1446         /* Check whether HBQ is still in use */
1447         spin_lock_irqsave(&phba->hbalock, flags);
1448         if (!phba->hbq_in_use)
1449                 goto err;
1450         while (!list_empty(&hbq_buf_list)) {
1451                 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1452                                  dbuf.list);
1453                 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
1454                                       (hbqno << 16));
1455                 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
1456                         phba->hbqs[hbqno].buffer_count++;
1457                         posted++;
1458                 } else
1459                         (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1460         }
1461         spin_unlock_irqrestore(&phba->hbalock, flags);
1462         return posted;
1463 err:
1464         spin_unlock_irqrestore(&phba->hbalock, flags);
1465         while (!list_empty(&hbq_buf_list)) {
1466                 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1467                                  dbuf.list);
1468                 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1469         }
1470         return 0;
1471 }
1472
1473 /**
1474  * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
1475  * @phba: Pointer to HBA context object.
1476  * @qno: HBQ number.
1477  *
1478  * This function posts more buffers to the HBQ. This function
1479  * is called with no lock held. The function returns the number of HBQ entries
1480  * successfully allocated.
1481  **/
1482 int
1483 lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
1484 {
1485         if (phba->sli_rev == LPFC_SLI_REV4)
1486                 return 0;
1487         else
1488                 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1489                                          lpfc_hbq_defs[qno]->add_count);
1490 }
1491
1492 /**
1493  * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
1494  * @phba: Pointer to HBA context object.
1495  * @qno:  HBQ queue number.
1496  *
1497  * This function is called from SLI initialization code path with
1498  * no lock held to post initial HBQ buffers to firmware. The
1499  * function returns the number of HBQ entries successfully allocated.
1500  **/
1501 static int
1502 lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
1503 {
1504         if (phba->sli_rev == LPFC_SLI_REV4)
1505                 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1506                                          lpfc_hbq_defs[qno]->entry_count);
1507         else
1508                 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1509                                          lpfc_hbq_defs[qno]->init_count);
1510 }
1511
1512 /**
1513  * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
1514  * @phba: Pointer to HBA context object.
1515  * @hbqno: HBQ number.
1516  *
1517  * This function removes the first hbq buffer on an hbq list and returns a
1518  * pointer to that buffer. If it finds no buffers on the list it returns NULL.
1519  **/
1520 static struct hbq_dmabuf *
1521 lpfc_sli_hbqbuf_get(struct list_head *rb_list)
1522 {
1523         struct lpfc_dmabuf *d_buf;
1524
1525         list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
1526         if (!d_buf)
1527                 return NULL;
1528         return container_of(d_buf, struct hbq_dmabuf, dbuf);
1529 }
1530
1531 /**
1532  * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
1533  * @phba: Pointer to HBA context object.
1534  * @tag: Tag of the hbq buffer.
1535  *
1536  * This function is called with hbalock held. This function searches
1537  * for the hbq buffer associated with the given tag in the hbq buffer
1538  * list. If it finds the hbq buffer, it returns the hbq_buffer other wise
1539  * it returns NULL.
1540  **/
1541 static struct hbq_dmabuf *
1542 lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
1543 {
1544         struct lpfc_dmabuf *d_buf;
1545         struct hbq_dmabuf *hbq_buf;
1546         uint32_t hbqno;
1547
1548         hbqno = tag >> 16;
1549         if (hbqno >= LPFC_MAX_HBQS)
1550                 return NULL;
1551
1552         spin_lock_irq(&phba->hbalock);
1553         list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
1554                 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
1555                 if (hbq_buf->tag == tag) {
1556                         spin_unlock_irq(&phba->hbalock);
1557                         return hbq_buf;
1558                 }
1559         }
1560         spin_unlock_irq(&phba->hbalock);
1561         lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
1562                         "1803 Bad hbq tag. Data: x%x x%x\n",
1563                         tag, phba->hbqs[tag >> 16].buffer_count);
1564         return NULL;
1565 }
1566
1567 /**
1568  * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
1569  * @phba: Pointer to HBA context object.
1570  * @hbq_buffer: Pointer to HBQ buffer.
1571  *
1572  * This function is called with hbalock. This function gives back
1573  * the hbq buffer to firmware. If the HBQ does not have space to
1574  * post the buffer, it will free the buffer.
1575  **/
1576 void
1577 lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
1578 {
1579         uint32_t hbqno;
1580
1581         if (hbq_buffer) {
1582                 hbqno = hbq_buffer->tag >> 16;
1583                 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
1584                         (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1585         }
1586 }
1587
1588 /**
1589  * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
1590  * @mbxCommand: mailbox command code.
1591  *
1592  * This function is called by the mailbox event handler function to verify
1593  * that the completed mailbox command is a legitimate mailbox command. If the
1594  * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
1595  * and the mailbox event handler will take the HBA offline.
1596  **/
1597 static int
1598 lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
1599 {
1600         uint8_t ret;
1601
1602         switch (mbxCommand) {
1603         case MBX_LOAD_SM:
1604         case MBX_READ_NV:
1605         case MBX_WRITE_NV:
1606         case MBX_WRITE_VPARMS:
1607         case MBX_RUN_BIU_DIAG:
1608         case MBX_INIT_LINK:
1609         case MBX_DOWN_LINK:
1610         case MBX_CONFIG_LINK:
1611         case MBX_CONFIG_RING:
1612         case MBX_RESET_RING:
1613         case MBX_READ_CONFIG:
1614         case MBX_READ_RCONFIG:
1615         case MBX_READ_SPARM:
1616         case MBX_READ_STATUS:
1617         case MBX_READ_RPI:
1618         case MBX_READ_XRI:
1619         case MBX_READ_REV:
1620         case MBX_READ_LNK_STAT:
1621         case MBX_REG_LOGIN:
1622         case MBX_UNREG_LOGIN:
1623         case MBX_READ_LA:
1624         case MBX_CLEAR_LA:
1625         case MBX_DUMP_MEMORY:
1626         case MBX_DUMP_CONTEXT:
1627         case MBX_RUN_DIAGS:
1628         case MBX_RESTART:
1629         case MBX_UPDATE_CFG:
1630         case MBX_DOWN_LOAD:
1631         case MBX_DEL_LD_ENTRY:
1632         case MBX_RUN_PROGRAM:
1633         case MBX_SET_MASK:
1634         case MBX_SET_VARIABLE:
1635         case MBX_UNREG_D_ID:
1636         case MBX_KILL_BOARD:
1637         case MBX_CONFIG_FARP:
1638         case MBX_BEACON:
1639         case MBX_LOAD_AREA:
1640         case MBX_RUN_BIU_DIAG64:
1641         case MBX_CONFIG_PORT:
1642         case MBX_READ_SPARM64:
1643         case MBX_READ_RPI64:
1644         case MBX_REG_LOGIN64:
1645         case MBX_READ_LA64:
1646         case MBX_WRITE_WWN:
1647         case MBX_SET_DEBUG:
1648         case MBX_LOAD_EXP_ROM:
1649         case MBX_ASYNCEVT_ENABLE:
1650         case MBX_REG_VPI:
1651         case MBX_UNREG_VPI:
1652         case MBX_HEARTBEAT:
1653         case MBX_PORT_CAPABILITIES:
1654         case MBX_PORT_IOV_CONTROL:
1655         case MBX_SLI4_CONFIG:
1656         case MBX_SLI4_REQ_FTRS:
1657         case MBX_REG_FCFI:
1658         case MBX_UNREG_FCFI:
1659         case MBX_REG_VFI:
1660         case MBX_UNREG_VFI:
1661         case MBX_INIT_VPI:
1662         case MBX_INIT_VFI:
1663         case MBX_RESUME_RPI:
1664                 ret = mbxCommand;
1665                 break;
1666         default:
1667                 ret = MBX_SHUTDOWN;
1668                 break;
1669         }
1670         return ret;
1671 }
1672
1673 /**
1674  * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
1675  * @phba: Pointer to HBA context object.
1676  * @pmboxq: Pointer to mailbox command.
1677  *
1678  * This is completion handler function for mailbox commands issued from
1679  * lpfc_sli_issue_mbox_wait function. This function is called by the
1680  * mailbox event handler function with no lock held. This function
1681  * will wake up thread waiting on the wait queue pointed by context1
1682  * of the mailbox.
1683  **/
1684 void
1685 lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
1686 {
1687         wait_queue_head_t *pdone_q;
1688         unsigned long drvr_flag;
1689
1690         /*
1691          * If pdone_q is empty, the driver thread gave up waiting and
1692          * continued running.
1693          */
1694         pmboxq->mbox_flag |= LPFC_MBX_WAKE;
1695         spin_lock_irqsave(&phba->hbalock, drvr_flag);
1696         pdone_q = (wait_queue_head_t *) pmboxq->context1;
1697         if (pdone_q)
1698                 wake_up_interruptible(pdone_q);
1699         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
1700         return;
1701 }
1702
1703
1704 /**
1705  * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
1706  * @phba: Pointer to HBA context object.
1707  * @pmb: Pointer to mailbox object.
1708  *
1709  * This function is the default mailbox completion handler. It
1710  * frees the memory resources associated with the completed mailbox
1711  * command. If the completed command is a REG_LOGIN mailbox command,
1712  * this function will issue a UREG_LOGIN to re-claim the RPI.
1713  **/
1714 void
1715 lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
1716 {
1717         struct lpfc_dmabuf *mp;
1718         uint16_t rpi, vpi;
1719         int rc;
1720
1721         mp = (struct lpfc_dmabuf *) (pmb->context1);
1722
1723         if (mp) {
1724                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1725                 kfree(mp);
1726         }
1727
1728         if ((pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) &&
1729             (phba->sli_rev == LPFC_SLI_REV4))
1730                 lpfc_sli4_free_rpi(phba, pmb->u.mb.un.varUnregLogin.rpi);
1731
1732         /*
1733          * If a REG_LOGIN succeeded  after node is destroyed or node
1734          * is in re-discovery driver need to cleanup the RPI.
1735          */
1736         if (!(phba->pport->load_flag & FC_UNLOADING) &&
1737             pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
1738             !pmb->u.mb.mbxStatus) {
1739                 rpi = pmb->u.mb.un.varWords[0];
1740                 vpi = pmb->u.mb.un.varRegLogin.vpi - phba->vpi_base;
1741                 lpfc_unreg_login(phba, vpi, rpi, pmb);
1742                 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
1743                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
1744                 if (rc != MBX_NOT_FINISHED)
1745                         return;
1746         }
1747
1748         if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
1749                 lpfc_sli4_mbox_cmd_free(phba, pmb);
1750         else
1751                 mempool_free(pmb, phba->mbox_mem_pool);
1752 }
1753
1754 /**
1755  * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
1756  * @phba: Pointer to HBA context object.
1757  *
1758  * This function is called with no lock held. This function processes all
1759  * the completed mailbox commands and gives it to upper layers. The interrupt
1760  * service routine processes mailbox completion interrupt and adds completed
1761  * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
1762  * Worker thread call lpfc_sli_handle_mb_event, which will return the
1763  * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
1764  * function returns the mailbox commands to the upper layer by calling the
1765  * completion handler function of each mailbox.
1766  **/
1767 int
1768 lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
1769 {
1770         MAILBOX_t *pmbox;
1771         LPFC_MBOXQ_t *pmb;
1772         int rc;
1773         LIST_HEAD(cmplq);
1774
1775         phba->sli.slistat.mbox_event++;
1776
1777         /* Get all completed mailboxe buffers into the cmplq */
1778         spin_lock_irq(&phba->hbalock);
1779         list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
1780         spin_unlock_irq(&phba->hbalock);
1781
1782         /* Get a Mailbox buffer to setup mailbox commands for callback */
1783         do {
1784                 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
1785                 if (pmb == NULL)
1786                         break;
1787
1788                 pmbox = &pmb->u.mb;
1789
1790                 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
1791                         if (pmb->vport) {
1792                                 lpfc_debugfs_disc_trc(pmb->vport,
1793                                         LPFC_DISC_TRC_MBOX_VPORT,
1794                                         "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
1795                                         (uint32_t)pmbox->mbxCommand,
1796                                         pmbox->un.varWords[0],
1797                                         pmbox->un.varWords[1]);
1798                         }
1799                         else {
1800                                 lpfc_debugfs_disc_trc(phba->pport,
1801                                         LPFC_DISC_TRC_MBOX,
1802                                         "MBOX cmpl:       cmd:x%x mb:x%x x%x",
1803                                         (uint32_t)pmbox->mbxCommand,
1804                                         pmbox->un.varWords[0],
1805                                         pmbox->un.varWords[1]);
1806                         }
1807                 }
1808
1809                 /*
1810                  * It is a fatal error if unknown mbox command completion.
1811                  */
1812                 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
1813                     MBX_SHUTDOWN) {
1814                         /* Unknown mailbox command compl */
1815                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
1816                                         "(%d):0323 Unknown Mailbox command "
1817                                         "x%x (x%x) Cmpl\n",
1818                                         pmb->vport ? pmb->vport->vpi : 0,
1819                                         pmbox->mbxCommand,
1820                                         lpfc_sli4_mbox_opcode_get(phba, pmb));
1821                         phba->link_state = LPFC_HBA_ERROR;
1822                         phba->work_hs = HS_FFER3;
1823                         lpfc_handle_eratt(phba);
1824                         continue;
1825                 }
1826
1827                 if (pmbox->mbxStatus) {
1828                         phba->sli.slistat.mbox_stat_err++;
1829                         if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
1830                                 /* Mbox cmd cmpl error - RETRYing */
1831                                 lpfc_printf_log(phba, KERN_INFO,
1832                                                 LOG_MBOX | LOG_SLI,
1833                                                 "(%d):0305 Mbox cmd cmpl "
1834                                                 "error - RETRYing Data: x%x "
1835                                                 "(x%x) x%x x%x x%x\n",
1836                                                 pmb->vport ? pmb->vport->vpi :0,
1837                                                 pmbox->mbxCommand,
1838                                                 lpfc_sli4_mbox_opcode_get(phba,
1839                                                                           pmb),
1840                                                 pmbox->mbxStatus,
1841                                                 pmbox->un.varWords[0],
1842                                                 pmb->vport->port_state);
1843                                 pmbox->mbxStatus = 0;
1844                                 pmbox->mbxOwner = OWN_HOST;
1845                                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
1846                                 if (rc != MBX_NOT_FINISHED)
1847                                         continue;
1848                         }
1849                 }
1850
1851                 /* Mailbox cmd <cmd> Cmpl <cmpl> */
1852                 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
1853                                 "(%d):0307 Mailbox cmd x%x (x%x) Cmpl x%p "
1854                                 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x\n",
1855                                 pmb->vport ? pmb->vport->vpi : 0,
1856                                 pmbox->mbxCommand,
1857                                 lpfc_sli4_mbox_opcode_get(phba, pmb),
1858                                 pmb->mbox_cmpl,
1859                                 *((uint32_t *) pmbox),
1860                                 pmbox->un.varWords[0],
1861                                 pmbox->un.varWords[1],
1862                                 pmbox->un.varWords[2],
1863                                 pmbox->un.varWords[3],
1864                                 pmbox->un.varWords[4],
1865                                 pmbox->un.varWords[5],
1866                                 pmbox->un.varWords[6],
1867                                 pmbox->un.varWords[7]);
1868
1869                 if (pmb->mbox_cmpl)
1870                         pmb->mbox_cmpl(phba,pmb);
1871         } while (1);
1872         return 0;
1873 }
1874
1875 /**
1876  * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
1877  * @phba: Pointer to HBA context object.
1878  * @pring: Pointer to driver SLI ring object.
1879  * @tag: buffer tag.
1880  *
1881  * This function is called with no lock held. When QUE_BUFTAG_BIT bit
1882  * is set in the tag the buffer is posted for a particular exchange,
1883  * the function will return the buffer without replacing the buffer.
1884  * If the buffer is for unsolicited ELS or CT traffic, this function
1885  * returns the buffer and also posts another buffer to the firmware.
1886  **/
1887 static struct lpfc_dmabuf *
1888 lpfc_sli_get_buff(struct lpfc_hba *phba,
1889                   struct lpfc_sli_ring *pring,
1890                   uint32_t tag)
1891 {
1892         struct hbq_dmabuf *hbq_entry;
1893
1894         if (tag & QUE_BUFTAG_BIT)
1895                 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
1896         hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
1897         if (!hbq_entry)
1898                 return NULL;
1899         return &hbq_entry->dbuf;
1900 }
1901
1902 /**
1903  * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
1904  * @phba: Pointer to HBA context object.
1905  * @pring: Pointer to driver SLI ring object.
1906  * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
1907  * @fch_r_ctl: the r_ctl for the first frame of the sequence.
1908  * @fch_type: the type for the first frame of the sequence.
1909  *
1910  * This function is called with no lock held. This function uses the r_ctl and
1911  * type of the received sequence to find the correct callback function to call
1912  * to process the sequence.
1913  **/
1914 static int
1915 lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1916                          struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
1917                          uint32_t fch_type)
1918 {
1919         int i;
1920
1921         /* unSolicited Responses */
1922         if (pring->prt[0].profile) {
1923                 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
1924                         (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
1925                                                                         saveq);
1926                 return 1;
1927         }
1928         /* We must search, based on rctl / type
1929            for the right routine */
1930         for (i = 0; i < pring->num_mask; i++) {
1931                 if ((pring->prt[i].rctl == fch_r_ctl) &&
1932                     (pring->prt[i].type == fch_type)) {
1933                         if (pring->prt[i].lpfc_sli_rcv_unsol_event)
1934                                 (pring->prt[i].lpfc_sli_rcv_unsol_event)
1935                                                 (phba, pring, saveq);
1936                         return 1;
1937                 }
1938         }
1939         return 0;
1940 }
1941
1942 /**
1943  * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
1944  * @phba: Pointer to HBA context object.
1945  * @pring: Pointer to driver SLI ring object.
1946  * @saveq: Pointer to the unsolicited iocb.
1947  *
1948  * This function is called with no lock held by the ring event handler
1949  * when there is an unsolicited iocb posted to the response ring by the
1950  * firmware. This function gets the buffer associated with the iocbs
1951  * and calls the event handler for the ring. This function handles both
1952  * qring buffers and hbq buffers.
1953  * When the function returns 1 the caller can free the iocb object otherwise
1954  * upper layer functions will free the iocb objects.
1955  **/
1956 static int
1957 lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1958                             struct lpfc_iocbq *saveq)
1959 {
1960         IOCB_t           * irsp;
1961         WORD5            * w5p;
1962         uint32_t           Rctl, Type;
1963         uint32_t           match;
1964         struct lpfc_iocbq *iocbq;
1965         struct lpfc_dmabuf *dmzbuf;
1966
1967         match = 0;
1968         irsp = &(saveq->iocb);
1969
1970         if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
1971                 if (pring->lpfc_sli_rcv_async_status)
1972                         pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
1973                 else
1974                         lpfc_printf_log(phba,
1975                                         KERN_WARNING,
1976                                         LOG_SLI,
1977                                         "0316 Ring %d handler: unexpected "
1978                                         "ASYNC_STATUS iocb received evt_code "
1979                                         "0x%x\n",
1980                                         pring->ringno,
1981                                         irsp->un.asyncstat.evt_code);
1982                 return 1;
1983         }
1984
1985         if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
1986                 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
1987                 if (irsp->ulpBdeCount > 0) {
1988                         dmzbuf = lpfc_sli_get_buff(phba, pring,
1989                                         irsp->un.ulpWord[3]);
1990                         lpfc_in_buf_free(phba, dmzbuf);
1991                 }
1992
1993                 if (irsp->ulpBdeCount > 1) {
1994                         dmzbuf = lpfc_sli_get_buff(phba, pring,
1995                                         irsp->unsli3.sli3Words[3]);
1996                         lpfc_in_buf_free(phba, dmzbuf);
1997                 }
1998
1999                 if (irsp->ulpBdeCount > 2) {
2000                         dmzbuf = lpfc_sli_get_buff(phba, pring,
2001                                 irsp->unsli3.sli3Words[7]);
2002                         lpfc_in_buf_free(phba, dmzbuf);
2003                 }
2004
2005                 return 1;
2006         }
2007
2008         if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
2009                 if (irsp->ulpBdeCount != 0) {
2010                         saveq->context2 = lpfc_sli_get_buff(phba, pring,
2011                                                 irsp->un.ulpWord[3]);
2012                         if (!saveq->context2)
2013                                 lpfc_printf_log(phba,
2014                                         KERN_ERR,
2015                                         LOG_SLI,
2016                                         "0341 Ring %d Cannot find buffer for "
2017                                         "an unsolicited iocb. tag 0x%x\n",
2018                                         pring->ringno,
2019                                         irsp->un.ulpWord[3]);
2020                 }
2021                 if (irsp->ulpBdeCount == 2) {
2022                         saveq->context3 = lpfc_sli_get_buff(phba, pring,
2023                                                 irsp->unsli3.sli3Words[7]);
2024                         if (!saveq->context3)
2025                                 lpfc_printf_log(phba,
2026                                         KERN_ERR,
2027                                         LOG_SLI,
2028                                         "0342 Ring %d Cannot find buffer for an"
2029                                         " unsolicited iocb. tag 0x%x\n",
2030                                         pring->ringno,
2031                                         irsp->unsli3.sli3Words[7]);
2032                 }
2033                 list_for_each_entry(iocbq, &saveq->list, list) {
2034                         irsp = &(iocbq->iocb);
2035                         if (irsp->ulpBdeCount != 0) {
2036                                 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
2037                                                         irsp->un.ulpWord[3]);
2038                                 if (!iocbq->context2)
2039                                         lpfc_printf_log(phba,
2040                                                 KERN_ERR,
2041                                                 LOG_SLI,
2042                                                 "0343 Ring %d Cannot find "
2043                                                 "buffer for an unsolicited iocb"
2044                                                 ". tag 0x%x\n", pring->ringno,
2045                                                 irsp->un.ulpWord[3]);
2046                         }
2047                         if (irsp->ulpBdeCount == 2) {
2048                                 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
2049                                                 irsp->unsli3.sli3Words[7]);
2050                                 if (!iocbq->context3)
2051                                         lpfc_printf_log(phba,
2052                                                 KERN_ERR,
2053                                                 LOG_SLI,
2054                                                 "0344 Ring %d Cannot find "
2055                                                 "buffer for an unsolicited "
2056                                                 "iocb. tag 0x%x\n",
2057                                                 pring->ringno,
2058                                                 irsp->unsli3.sli3Words[7]);
2059                         }
2060                 }
2061         }
2062         if (irsp->ulpBdeCount != 0 &&
2063             (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
2064              irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
2065                 int found = 0;
2066
2067                 /* search continue save q for same XRI */
2068                 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
2069                         if (iocbq->iocb.ulpContext == saveq->iocb.ulpContext) {
2070                                 list_add_tail(&saveq->list, &iocbq->list);
2071                                 found = 1;
2072                                 break;
2073                         }
2074                 }
2075                 if (!found)
2076                         list_add_tail(&saveq->clist,
2077                                       &pring->iocb_continue_saveq);
2078                 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
2079                         list_del_init(&iocbq->clist);
2080                         saveq = iocbq;
2081                         irsp = &(saveq->iocb);
2082                 } else
2083                         return 0;
2084         }
2085         if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
2086             (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
2087             (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
2088                 Rctl = FC_RCTL_ELS_REQ;
2089                 Type = FC_TYPE_ELS;
2090         } else {
2091                 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
2092                 Rctl = w5p->hcsw.Rctl;
2093                 Type = w5p->hcsw.Type;
2094
2095                 /* Firmware Workaround */
2096                 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
2097                         (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
2098                          irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
2099                         Rctl = FC_RCTL_ELS_REQ;
2100                         Type = FC_TYPE_ELS;
2101                         w5p->hcsw.Rctl = Rctl;
2102                         w5p->hcsw.Type = Type;
2103                 }
2104         }
2105
2106         if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
2107                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
2108                                 "0313 Ring %d handler: unexpected Rctl x%x "
2109                                 "Type x%x received\n",
2110                                 pring->ringno, Rctl, Type);
2111
2112         return 1;
2113 }
2114
2115 /**
2116  * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
2117  * @phba: Pointer to HBA context object.
2118  * @pring: Pointer to driver SLI ring object.
2119  * @prspiocb: Pointer to response iocb object.
2120  *
2121  * This function looks up the iocb_lookup table to get the command iocb
2122  * corresponding to the given response iocb using the iotag of the
2123  * response iocb. This function is called with the hbalock held.
2124  * This function returns the command iocb object if it finds the command
2125  * iocb else returns NULL.
2126  **/
2127 static struct lpfc_iocbq *
2128 lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
2129                       struct lpfc_sli_ring *pring,
2130                       struct lpfc_iocbq *prspiocb)
2131 {
2132         struct lpfc_iocbq *cmd_iocb = NULL;
2133         uint16_t iotag;
2134
2135         iotag = prspiocb->iocb.ulpIoTag;
2136
2137         if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2138                 cmd_iocb = phba->sli.iocbq_lookup[iotag];
2139                 list_del_init(&cmd_iocb->list);
2140                 pring->txcmplq_cnt--;
2141                 return cmd_iocb;
2142         }
2143
2144         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2145                         "0317 iotag x%x is out off "
2146                         "range: max iotag x%x wd0 x%x\n",
2147                         iotag, phba->sli.last_iotag,
2148                         *(((uint32_t *) &prspiocb->iocb) + 7));
2149         return NULL;
2150 }
2151
2152 /**
2153  * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
2154  * @phba: Pointer to HBA context object.
2155  * @pring: Pointer to driver SLI ring object.
2156  * @iotag: IOCB tag.
2157  *
2158  * This function looks up the iocb_lookup table to get the command iocb
2159  * corresponding to the given iotag. This function is called with the
2160  * hbalock held.
2161  * This function returns the command iocb object if it finds the command
2162  * iocb else returns NULL.
2163  **/
2164 static struct lpfc_iocbq *
2165 lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
2166                              struct lpfc_sli_ring *pring, uint16_t iotag)
2167 {
2168         struct lpfc_iocbq *cmd_iocb;
2169
2170         if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2171                 cmd_iocb = phba->sli.iocbq_lookup[iotag];
2172                 list_del_init(&cmd_iocb->list);
2173                 pring->txcmplq_cnt--;
2174                 return cmd_iocb;
2175         }
2176
2177         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2178                         "0372 iotag x%x is out off range: max iotag (x%x)\n",
2179                         iotag, phba->sli.last_iotag);
2180         return NULL;
2181 }
2182
2183 /**
2184  * lpfc_sli_process_sol_iocb - process solicited iocb completion
2185  * @phba: Pointer to HBA context object.
2186  * @pring: Pointer to driver SLI ring object.
2187  * @saveq: Pointer to the response iocb to be processed.
2188  *
2189  * This function is called by the ring event handler for non-fcp
2190  * rings when there is a new response iocb in the response ring.
2191  * The caller is not required to hold any locks. This function
2192  * gets the command iocb associated with the response iocb and
2193  * calls the completion handler for the command iocb. If there
2194  * is no completion handler, the function will free the resources
2195  * associated with command iocb. If the response iocb is for
2196  * an already aborted command iocb, the status of the completion
2197  * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
2198  * This function always returns 1.
2199  **/
2200 static int
2201 lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2202                           struct lpfc_iocbq *saveq)
2203 {
2204         struct lpfc_iocbq *cmdiocbp;
2205         int rc = 1;
2206         unsigned long iflag;
2207
2208         /* Based on the iotag field, get the cmd IOCB from the txcmplq */
2209         spin_lock_irqsave(&phba->hbalock, iflag);
2210         cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
2211         spin_unlock_irqrestore(&phba->hbalock, iflag);
2212
2213         if (cmdiocbp) {
2214                 if (cmdiocbp->iocb_cmpl) {
2215                         /*
2216                          * If an ELS command failed send an event to mgmt
2217                          * application.
2218                          */
2219                         if (saveq->iocb.ulpStatus &&
2220                              (pring->ringno == LPFC_ELS_RING) &&
2221                              (cmdiocbp->iocb.ulpCommand ==
2222                                 CMD_ELS_REQUEST64_CR))
2223                                 lpfc_send_els_failure_event(phba,
2224                                         cmdiocbp, saveq);
2225
2226                         /*
2227                          * Post all ELS completions to the worker thread.
2228                          * All other are passed to the completion callback.
2229                          */
2230                         if (pring->ringno == LPFC_ELS_RING) {
2231                                 if (cmdiocbp->iocb_flag & LPFC_DRIVER_ABORTED) {
2232                                         cmdiocbp->iocb_flag &=
2233                                                 ~LPFC_DRIVER_ABORTED;
2234                                         saveq->iocb.ulpStatus =
2235                                                 IOSTAT_LOCAL_REJECT;
2236                                         saveq->iocb.un.ulpWord[4] =
2237                                                 IOERR_SLI_ABORTED;
2238
2239                                         /* Firmware could still be in progress
2240                                          * of DMAing payload, so don't free data
2241                                          * buffer till after a hbeat.
2242                                          */
2243                                         saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
2244                                 }
2245                         }
2246                         (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
2247                 } else
2248                         lpfc_sli_release_iocbq(phba, cmdiocbp);
2249         } else {
2250                 /*
2251                  * Unknown initiating command based on the response iotag.
2252                  * This could be the case on the ELS ring because of
2253                  * lpfc_els_abort().
2254                  */
2255                 if (pring->ringno != LPFC_ELS_RING) {
2256                         /*
2257                          * Ring <ringno> handler: unexpected completion IoTag
2258                          * <IoTag>
2259                          */
2260                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
2261                                          "0322 Ring %d handler: "
2262                                          "unexpected completion IoTag x%x "
2263                                          "Data: x%x x%x x%x x%x\n",
2264                                          pring->ringno,
2265                                          saveq->iocb.ulpIoTag,
2266                                          saveq->iocb.ulpStatus,
2267                                          saveq->iocb.un.ulpWord[4],
2268                                          saveq->iocb.ulpCommand,
2269                                          saveq->iocb.ulpContext);
2270                 }
2271         }
2272
2273         return rc;
2274 }
2275
2276 /**
2277  * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
2278  * @phba: Pointer to HBA context object.
2279  * @pring: Pointer to driver SLI ring object.
2280  *
2281  * This function is called from the iocb ring event handlers when
2282  * put pointer is ahead of the get pointer for a ring. This function signal
2283  * an error attention condition to the worker thread and the worker
2284  * thread will transition the HBA to offline state.
2285  **/
2286 static void
2287 lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
2288 {
2289         struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
2290         /*
2291          * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
2292          * rsp ring <portRspMax>
2293          */
2294         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2295                         "0312 Ring %d handler: portRspPut %d "
2296                         "is bigger than rsp ring %d\n",
2297                         pring->ringno, le32_to_cpu(pgp->rspPutInx),
2298                         pring->numRiocb);
2299
2300         phba->link_state = LPFC_HBA_ERROR;
2301
2302         /*
2303          * All error attention handlers are posted to
2304          * worker thread
2305          */
2306         phba->work_ha |= HA_ERATT;
2307         phba->work_hs = HS_FFER3;
2308
2309         lpfc_worker_wake_up(phba);
2310
2311         return;
2312 }
2313
2314 /**
2315  * lpfc_poll_eratt - Error attention polling timer timeout handler
2316  * @ptr: Pointer to address of HBA context object.
2317  *
2318  * This function is invoked by the Error Attention polling timer when the
2319  * timer times out. It will check the SLI Error Attention register for
2320  * possible attention events. If so, it will post an Error Attention event
2321  * and wake up worker thread to process it. Otherwise, it will set up the
2322  * Error Attention polling timer for the next poll.
2323  **/
2324 void lpfc_poll_eratt(unsigned long ptr)
2325 {
2326         struct lpfc_hba *phba;
2327         uint32_t eratt = 0;
2328
2329         phba = (struct lpfc_hba *)ptr;
2330
2331         /* Check chip HA register for error event */
2332         eratt = lpfc_sli_check_eratt(phba);
2333
2334         if (eratt)
2335                 /* Tell the worker thread there is work to do */
2336                 lpfc_worker_wake_up(phba);
2337         else
2338                 /* Restart the timer for next eratt poll */
2339                 mod_timer(&phba->eratt_poll, jiffies +
2340                                         HZ * LPFC_ERATT_POLL_INTERVAL);
2341         return;
2342 }
2343
2344
2345 /**
2346  * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
2347  * @phba: Pointer to HBA context object.
2348  * @pring: Pointer to driver SLI ring object.
2349  * @mask: Host attention register mask for this ring.
2350  *
2351  * This function is called from the interrupt context when there is a ring
2352  * event for the fcp ring. The caller does not hold any lock.
2353  * The function processes each response iocb in the response ring until it
2354  * finds an iocb with LE bit set and chains all the iocbs upto the iocb with
2355  * LE bit set. The function will call the completion handler of the command iocb
2356  * if the response iocb indicates a completion for a command iocb or it is
2357  * an abort completion. The function will call lpfc_sli_process_unsol_iocb
2358  * function if this is an unsolicited iocb.
2359  * This routine presumes LPFC_FCP_RING handling and doesn't bother
2360  * to check it explicitly.
2361  */
2362 int
2363 lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
2364                                 struct lpfc_sli_ring *pring, uint32_t mask)
2365 {
2366         struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
2367         IOCB_t *irsp = NULL;
2368         IOCB_t *entry = NULL;
2369         struct lpfc_iocbq *cmdiocbq = NULL;
2370         struct lpfc_iocbq rspiocbq;
2371         uint32_t status;
2372         uint32_t portRspPut, portRspMax;
2373         int rc = 1;
2374         lpfc_iocb_type type;
2375         unsigned long iflag;
2376         uint32_t rsp_cmpl = 0;
2377
2378         spin_lock_irqsave(&phba->hbalock, iflag);
2379         pring->stats.iocb_event++;
2380
2381         /*
2382          * The next available response entry should never exceed the maximum
2383          * entries.  If it does, treat it as an adapter hardware error.
2384          */
2385         portRspMax = pring->numRiocb;
2386         portRspPut = le32_to_cpu(pgp->rspPutInx);
2387         if (unlikely(portRspPut >= portRspMax)) {
2388                 lpfc_sli_rsp_pointers_error(phba, pring);
2389                 spin_unlock_irqrestore(&phba->hbalock, iflag);
2390                 return 1;
2391         }
2392         if (phba->fcp_ring_in_use) {
2393                 spin_unlock_irqrestore(&phba->hbalock, iflag);
2394                 return 1;
2395         } else
2396                 phba->fcp_ring_in_use = 1;
2397
2398         rmb();
2399         while (pring->rspidx != portRspPut) {
2400                 /*
2401                  * Fetch an entry off the ring and copy it into a local data
2402                  * structure.  The copy involves a byte-swap since the
2403                  * network byte order and pci byte orders are different.
2404                  */
2405                 entry = lpfc_resp_iocb(phba, pring);
2406                 phba->last_completion_time = jiffies;
2407
2408                 if (++pring->rspidx >= portRspMax)
2409                         pring->rspidx = 0;
2410
2411                 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
2412                                       (uint32_t *) &rspiocbq.iocb,
2413                                       phba->iocb_rsp_size);
2414                 INIT_LIST_HEAD(&(rspiocbq.list));
2415                 irsp = &rspiocbq.iocb;
2416
2417                 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
2418                 pring->stats.iocb_rsp++;
2419                 rsp_cmpl++;
2420
2421                 if (unlikely(irsp->ulpStatus)) {
2422                         /*
2423                          * If resource errors reported from HBA, reduce
2424                          * queuedepths of the SCSI device.
2425                          */
2426                         if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
2427                                 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
2428                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
2429                                 phba->lpfc_rampdown_queue_depth(phba);
2430                                 spin_lock_irqsave(&phba->hbalock, iflag);
2431                         }
2432
2433                         /* Rsp ring <ringno> error: IOCB */
2434                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
2435                                         "0336 Rsp Ring %d error: IOCB Data: "
2436                                         "x%x x%x x%x x%x x%x x%x x%x x%x\n",
2437                                         pring->ringno,
2438                                         irsp->un.ulpWord[0],
2439                                         irsp->un.ulpWord[1],
2440                                         irsp->un.ulpWord[2],
2441                                         irsp->un.ulpWord[3],
2442                                         irsp->un.ulpWord[4],
2443                                         irsp->un.ulpWord[5],
2444                                         *(uint32_t *)&irsp->un1,
2445                                         *((uint32_t *)&irsp->un1 + 1));
2446                 }
2447
2448                 switch (type) {
2449                 case LPFC_ABORT_IOCB:
2450                 case LPFC_SOL_IOCB:
2451                         /*
2452                          * Idle exchange closed via ABTS from port.  No iocb
2453                          * resources need to be recovered.
2454                          */
2455                         if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
2456                                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
2457                                                 "0333 IOCB cmd 0x%x"
2458                                                 " processed. Skipping"
2459                                                 " completion\n",
2460                                                 irsp->ulpCommand);
2461                                 break;
2462                         }
2463
2464                         cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
2465                                                          &rspiocbq);
2466                         if ((cmdiocbq) && (cmdiocbq->iocb_cmpl)) {
2467                                         spin_unlock_irqrestore(&phba->hbalock,
2468                                                                iflag);
2469                                         (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
2470                                                               &rspiocbq);
2471                                         spin_lock_irqsave(&phba->hbalock,
2472                                                           iflag);
2473                                 }
2474                         break;
2475                 case LPFC_UNSOL_IOCB:
2476                         spin_unlock_irqrestore(&phba->hbalock, iflag);
2477                         lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
2478                         spin_lock_irqsave(&phba->hbalock, iflag);
2479                         break;
2480                 default:
2481                         if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
2482                                 char adaptermsg[LPFC_MAX_ADPTMSG];
2483                                 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
2484                                 memcpy(&adaptermsg[0], (uint8_t *) irsp,
2485                                        MAX_MSG_DATA);
2486                                 dev_warn(&((phba->pcidev)->dev),
2487                                          "lpfc%d: %s\n",
2488                                          phba->brd_no, adaptermsg);
2489                         } else {
2490                                 /* Unknown IOCB command */
2491                                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2492                                                 "0334 Unknown IOCB command "
2493                                                 "Data: x%x, x%x x%x x%x x%x\n",
2494                                                 type, irsp->ulpCommand,
2495                                                 irsp->ulpStatus,
2496                                                 irsp->ulpIoTag,
2497                                                 irsp->ulpContext);
2498                         }
2499                         break;
2500                 }
2501
2502                 /*
2503                  * The response IOCB has been processed.  Update the ring
2504                  * pointer in SLIM.  If the port response put pointer has not
2505                  * been updated, sync the pgp->rspPutInx and fetch the new port
2506                  * response put pointer.
2507                  */
2508                 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
2509
2510                 if (pring->rspidx == portRspPut)
2511                         portRspPut = le32_to_cpu(pgp->rspPutInx);
2512         }
2513
2514         if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
2515                 pring->stats.iocb_rsp_full++;
2516                 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
2517                 writel(status, phba->CAregaddr);
2518                 readl(phba->CAregaddr);
2519         }
2520         if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
2521                 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
2522                 pring->stats.iocb_cmd_empty++;
2523
2524                 /* Force update of the local copy of cmdGetInx */
2525                 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
2526                 lpfc_sli_resume_iocb(phba, pring);
2527
2528                 if ((pring->lpfc_sli_cmd_available))
2529                         (pring->lpfc_sli_cmd_available) (phba, pring);
2530
2531         }
2532
2533         phba->fcp_ring_in_use = 0;
2534         spin_unlock_irqrestore(&phba->hbalock, iflag);
2535         return rc;
2536 }
2537
2538 /**
2539  * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
2540  * @phba: Pointer to HBA context object.
2541  * @pring: Pointer to driver SLI ring object.
2542  * @rspiocbp: Pointer to driver response IOCB object.
2543  *
2544  * This function is called from the worker thread when there is a slow-path
2545  * response IOCB to process. This function chains all the response iocbs until
2546  * seeing the iocb with the LE bit set. The function will call
2547  * lpfc_sli_process_sol_iocb function if the response iocb indicates a
2548  * completion of a command iocb. The function will call the
2549  * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
2550  * The function frees the resources or calls the completion handler if this
2551  * iocb is an abort completion. The function returns NULL when the response
2552  * iocb has the LE bit set and all the chained iocbs are processed, otherwise
2553  * this function shall chain the iocb on to the iocb_continueq and return the
2554  * response iocb passed in.
2555  **/
2556 static struct lpfc_iocbq *
2557 lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2558                         struct lpfc_iocbq *rspiocbp)
2559 {
2560         struct lpfc_iocbq *saveq;
2561         struct lpfc_iocbq *cmdiocbp;
2562         struct lpfc_iocbq *next_iocb;
2563         IOCB_t *irsp = NULL;
2564         uint32_t free_saveq;
2565         uint8_t iocb_cmd_type;
2566         lpfc_iocb_type type;
2567         unsigned long iflag;
2568         int rc;
2569
2570         spin_lock_irqsave(&phba->hbalock, iflag);
2571         /* First add the response iocb to the countinueq list */
2572         list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
2573         pring->iocb_continueq_cnt++;
2574
2575         /* Now, determine whetehr the list is completed for processing */
2576         irsp = &rspiocbp->iocb;
2577         if (irsp->ulpLe) {
2578                 /*
2579                  * By default, the driver expects to free all resources
2580                  * associated with this iocb completion.
2581                  */
2582                 free_saveq = 1;
2583                 saveq = list_get_first(&pring->iocb_continueq,
2584                                        struct lpfc_iocbq, list);
2585                 irsp = &(saveq->iocb);
2586                 list_del_init(&pring->iocb_continueq);
2587                 pring->iocb_continueq_cnt = 0;
2588
2589                 pring->stats.iocb_rsp++;
2590
2591                 /*
2592                  * If resource errors reported from HBA, reduce
2593                  * queuedepths of the SCSI device.
2594                  */
2595                 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
2596                     (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
2597                         spin_unlock_irqrestore(&phba->hbalock, iflag);
2598                         phba->lpfc_rampdown_queue_depth(phba);
2599                         spin_lock_irqsave(&phba->hbalock, iflag);
2600                 }
2601
2602                 if (irsp->ulpStatus) {
2603                         /* Rsp ring <ringno> error: IOCB */
2604                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
2605                                         "0328 Rsp Ring %d error: "
2606                                         "IOCB Data: "
2607                                         "x%x x%x x%x x%x "
2608                                         "x%x x%x x%x x%x "
2609                                         "x%x x%x x%x x%x "
2610                                         "x%x x%x x%x x%x\n",
2611                                         pring->ringno,
2612                                         irsp->un.ulpWord[0],
2613                                         irsp->un.ulpWord[1],
2614                                         irsp->un.ulpWord[2],
2615                                         irsp->un.ulpWord[3],
2616                                         irsp->un.ulpWord[4],
2617                                         irsp->un.ulpWord[5],
2618                                         *(((uint32_t *) irsp) + 6),
2619                                         *(((uint32_t *) irsp) + 7),
2620                                         *(((uint32_t *) irsp) + 8),
2621                                         *(((uint32_t *) irsp) + 9),
2622                                         *(((uint32_t *) irsp) + 10),
2623                                         *(((uint32_t *) irsp) + 11),
2624                                         *(((uint32_t *) irsp) + 12),
2625                                         *(((uint32_t *) irsp) + 13),
2626                                         *(((uint32_t *) irsp) + 14),
2627                                         *(((uint32_t *) irsp) + 15));
2628                 }
2629
2630                 /*
2631                  * Fetch the IOCB command type and call the correct completion
2632                  * routine. Solicited and Unsolicited IOCBs on the ELS ring
2633                  * get freed back to the lpfc_iocb_list by the discovery
2634                  * kernel thread.
2635                  */
2636                 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
2637                 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
2638                 switch (type) {
2639                 case LPFC_SOL_IOCB:
2640                         spin_unlock_irqrestore(&phba->hbalock, iflag);
2641                         rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
2642                         spin_lock_irqsave(&phba->hbalock, iflag);
2643                         break;
2644
2645                 case LPFC_UNSOL_IOCB:
2646                         spin_unlock_irqrestore(&phba->hbalock, iflag);
2647                         rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
2648                         spin_lock_irqsave(&phba->hbalock, iflag);
2649                         if (!rc)
2650                                 free_saveq = 0;
2651                         break;
2652
2653                 case LPFC_ABORT_IOCB:
2654                         cmdiocbp = NULL;
2655                         if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
2656                                 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
2657                                                                  saveq);
2658                         if (cmdiocbp) {
2659                                 /* Call the specified completion routine */
2660                                 if (cmdiocbp->iocb_cmpl) {
2661                                         spin_unlock_irqrestore(&phba->hbalock,
2662                                                                iflag);
2663                                         (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
2664                                                               saveq);
2665                                         spin_lock_irqsave(&phba->hbalock,
2666                                                           iflag);
2667                                 } else
2668                                         __lpfc_sli_release_iocbq(phba,
2669                                                                  cmdiocbp);
2670                         }
2671                         break;
2672
2673                 case LPFC_UNKNOWN_IOCB:
2674                         if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
2675                                 char adaptermsg[LPFC_MAX_ADPTMSG];
2676                                 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
2677                                 memcpy(&adaptermsg[0], (uint8_t *)irsp,
2678                                        MAX_MSG_DATA);
2679                                 dev_warn(&((phba->pcidev)->dev),
2680                                          "lpfc%d: %s\n",
2681                                          phba->brd_no, adaptermsg);
2682                         } else {
2683                                 /* Unknown IOCB command */
2684                                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2685                                                 "0335 Unknown IOCB "
2686                                                 "command Data: x%x "
2687                                                 "x%x x%x x%x\n",
2688                                                 irsp->ulpCommand,
2689                                                 irsp->ulpStatus,
2690                                                 irsp->ulpIoTag,
2691                                                 irsp->ulpContext);
2692                         }
2693                         break;
2694                 }
2695
2696                 if (free_saveq) {
2697                         list_for_each_entry_safe(rspiocbp, next_iocb,
2698                                                  &saveq->list, list) {
2699                                 list_del(&rspiocbp->list);
2700                                 __lpfc_sli_release_iocbq(phba, rspiocbp);
2701                         }
2702                         __lpfc_sli_release_iocbq(phba, saveq);
2703                 }
2704                 rspiocbp = NULL;
2705         }
2706         spin_unlock_irqrestore(&phba->hbalock, iflag);
2707         return rspiocbp;
2708 }
2709
2710 /**
2711  * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
2712  * @phba: Pointer to HBA context object.
2713  * @pring: Pointer to driver SLI ring object.
2714  * @mask: Host attention register mask for this ring.
2715  *
2716  * This routine wraps the actual slow_ring event process routine from the
2717  * API jump table function pointer from the lpfc_hba struct.
2718  **/
2719 void
2720 lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
2721                                 struct lpfc_sli_ring *pring, uint32_t mask)
2722 {
2723         phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
2724 }
2725
2726 /**
2727  * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
2728  * @phba: Pointer to HBA context object.
2729  * @pring: Pointer to driver SLI ring object.
2730  * @mask: Host attention register mask for this ring.
2731  *
2732  * This function is called from the worker thread when there is a ring event
2733  * for non-fcp rings. The caller does not hold any lock. The function will
2734  * remove each response iocb in the response ring and calls the handle
2735  * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
2736  **/
2737 static void
2738 lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
2739                                    struct lpfc_sli_ring *pring, uint32_t mask)
2740 {
2741         struct lpfc_pgp *pgp;
2742         IOCB_t *entry;
2743         IOCB_t *irsp = NULL;
2744         struct lpfc_iocbq *rspiocbp = NULL;
2745         uint32_t portRspPut, portRspMax;
2746         unsigned long iflag;
2747         uint32_t status;
2748
2749         pgp = &phba->port_gp[pring->ringno];
2750         spin_lock_irqsave(&phba->hbalock, iflag);
2751         pring->stats.iocb_event++;
2752
2753         /*
2754          * The next available response entry should never exceed the maximum
2755          * entries.  If it does, treat it as an adapter hardware error.
2756          */
2757         portRspMax = pring->numRiocb;
2758         portRspPut = le32_to_cpu(pgp->rspPutInx);
2759         if (portRspPut >= portRspMax) {
2760                 /*
2761                  * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
2762                  * rsp ring <portRspMax>
2763                  */
2764                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2765                                 "0303 Ring %d handler: portRspPut %d "
2766                                 "is bigger than rsp ring %d\n",
2767                                 pring->ringno, portRspPut, portRspMax);
2768
2769                 phba->link_state = LPFC_HBA_ERROR;
2770                 spin_unlock_irqrestore(&phba->hbalock, iflag);
2771
2772                 phba->work_hs = HS_FFER3;
2773                 lpfc_handle_eratt(phba);
2774
2775                 return;
2776         }
2777
2778         rmb();
2779         while (pring->rspidx != portRspPut) {
2780                 /*
2781                  * Build a completion list and call the appropriate handler.
2782                  * The process is to get the next available response iocb, get
2783                  * a free iocb from the list, copy the response data into the
2784                  * free iocb, insert to the continuation list, and update the
2785                  * next response index to slim.  This process makes response
2786                  * iocb's in the ring available to DMA as fast as possible but
2787                  * pays a penalty for a copy operation.  Since the iocb is
2788                  * only 32 bytes, this penalty is considered small relative to
2789                  * the PCI reads for register values and a slim write.  When
2790                  * the ulpLe field is set, the entire Command has been
2791                  * received.
2792                  */
2793                 entry = lpfc_resp_iocb(phba, pring);
2794
2795                 phba->last_completion_time = jiffies;
2796                 rspiocbp = __lpfc_sli_get_iocbq(phba);
2797                 if (rspiocbp == NULL) {
2798                         printk(KERN_ERR "%s: out of buffers! Failing "
2799                                "completion.\n", __func__);
2800                         break;
2801                 }
2802
2803                 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
2804                                       phba->iocb_rsp_size);
2805                 irsp = &rspiocbp->iocb;
2806
2807                 if (++pring->rspidx >= portRspMax)
2808                         pring->rspidx = 0;
2809
2810                 if (pring->ringno == LPFC_ELS_RING) {
2811                         lpfc_debugfs_slow_ring_trc(phba,
2812                         "IOCB rsp ring:   wd4:x%08x wd6:x%08x wd7:x%08x",
2813                                 *(((uint32_t *) irsp) + 4),
2814                                 *(((uint32_t *) irsp) + 6),
2815                                 *(((uint32_t *) irsp) + 7));
2816                 }
2817
2818                 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
2819
2820                 spin_unlock_irqrestore(&phba->hbalock, iflag);
2821                 /* Handle the response IOCB */
2822                 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
2823                 spin_lock_irqsave(&phba->hbalock, iflag);
2824
2825                 /*
2826                  * If the port response put pointer has not been updated, sync
2827                  * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
2828                  * response put pointer.
2829                  */
2830                 if (pring->rspidx == portRspPut) {
2831                         portRspPut = le32_to_cpu(pgp->rspPutInx);
2832                 }
2833         } /* while (pring->rspidx != portRspPut) */
2834
2835         if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
2836                 /* At least one response entry has been freed */
2837                 pring->stats.iocb_rsp_full++;
2838                 /* SET RxRE_RSP in Chip Att register */
2839                 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
2840                 writel(status, phba->CAregaddr);
2841                 readl(phba->CAregaddr); /* flush */
2842         }
2843         if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
2844                 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
2845                 pring->stats.iocb_cmd_empty++;
2846
2847                 /* Force update of the local copy of cmdGetInx */
2848                 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
2849                 lpfc_sli_resume_iocb(phba, pring);
2850
2851                 if ((pring->lpfc_sli_cmd_available))
2852                         (pring->lpfc_sli_cmd_available) (phba, pring);
2853
2854         }
2855
2856         spin_unlock_irqrestore(&phba->hbalock, iflag);
2857         return;
2858 }
2859
2860 /**
2861  * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
2862  * @phba: Pointer to HBA context object.
2863  * @pring: Pointer to driver SLI ring object.
2864  * @mask: Host attention register mask for this ring.
2865  *
2866  * This function is called from the worker thread when there is a pending
2867  * ELS response iocb on the driver internal slow-path response iocb worker
2868  * queue. The caller does not hold any lock. The function will remove each
2869  * response iocb from the response worker queue and calls the handle
2870  * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
2871  **/
2872 static void
2873 lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
2874                                    struct lpfc_sli_ring *pring, uint32_t mask)
2875 {
2876         struct lpfc_iocbq *irspiocbq;
2877         struct hbq_dmabuf *dmabuf;
2878         struct lpfc_cq_event *cq_event;
2879         unsigned long iflag;
2880
2881         spin_lock_irqsave(&phba->hbalock, iflag);
2882         phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
2883         spin_unlock_irqrestore(&phba->hbalock, iflag);
2884         while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
2885                 /* Get the response iocb from the head of work queue */
2886                 spin_lock_irqsave(&phba->hbalock, iflag);
2887                 list_remove_head(&phba->sli4_hba.sp_queue_event,
2888                                  cq_event, struct lpfc_cq_event, list);
2889                 spin_unlock_irqrestore(&phba->hbalock, iflag);
2890
2891                 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
2892                 case CQE_CODE_COMPL_WQE:
2893                         irspiocbq = container_of(cq_event, struct lpfc_iocbq,
2894                                                  cq_event);
2895                         /* Translate ELS WCQE to response IOCBQ */
2896                         irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
2897                                                                    irspiocbq);
2898                         if (irspiocbq)
2899                                 lpfc_sli_sp_handle_rspiocb(phba, pring,
2900                                                            irspiocbq);
2901                         break;
2902                 case CQE_CODE_RECEIVE:
2903                         dmabuf = container_of(cq_event, struct hbq_dmabuf,
2904                                               cq_event);
2905                         lpfc_sli4_handle_received_buffer(phba, dmabuf);
2906                         break;
2907                 default:
2908                         break;
2909                 }
2910         }
2911 }
2912
2913 /**
2914  * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
2915  * @phba: Pointer to HBA context object.
2916  * @pring: Pointer to driver SLI ring object.
2917  *
2918  * This function aborts all iocbs in the given ring and frees all the iocb
2919  * objects in txq. This function issues an abort iocb for all the iocb commands
2920  * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
2921  * the return of this function. The caller is not required to hold any locks.
2922  **/
2923 void
2924 lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
2925 {
2926         LIST_HEAD(completions);
2927         struct lpfc_iocbq *iocb, *next_iocb;
2928
2929         if (pring->ringno == LPFC_ELS_RING) {
2930                 lpfc_fabric_abort_hba(phba);
2931         }
2932
2933         /* Error everything on txq and txcmplq
2934          * First do the txq.
2935          */
2936         spin_lock_irq(&phba->hbalock);
2937         list_splice_init(&pring->txq, &completions);
2938         pring->txq_cnt = 0;
2939
2940         /* Next issue ABTS for everything on the txcmplq */
2941         list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
2942                 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
2943
2944         spin_unlock_irq(&phba->hbalock);
2945
2946         /* Cancel all the IOCBs from the completions list */
2947         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
2948                               IOERR_SLI_ABORTED);
2949 }
2950
2951 /**
2952  * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
2953  * @phba: Pointer to HBA context object.
2954  *
2955  * This function flushes all iocbs in the fcp ring and frees all the iocb
2956  * objects in txq and txcmplq. This function will not issue abort iocbs
2957  * for all the iocb commands in txcmplq, they will just be returned with
2958  * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
2959  * slot has been permanently disabled.
2960  **/
2961 void
2962 lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
2963 {
2964         LIST_HEAD(txq);
2965         LIST_HEAD(txcmplq);
2966         struct lpfc_sli *psli = &phba->sli;
2967         struct lpfc_sli_ring  *pring;
2968
2969         /* Currently, only one fcp ring */
2970         pring = &psli->ring[psli->fcp_ring];
2971
2972         spin_lock_irq(&phba->hbalock);
2973         /* Retrieve everything on txq */
2974         list_splice_init(&pring->txq, &txq);
2975         pring->txq_cnt = 0;
2976
2977         /* Retrieve everything on the txcmplq */
2978         list_splice_init(&pring->txcmplq, &txcmplq);
2979         pring->txcmplq_cnt = 0;
2980         spin_unlock_irq(&phba->hbalock);
2981
2982         /* Flush the txq */
2983         lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
2984                               IOERR_SLI_DOWN);
2985
2986         /* Flush the txcmpq */
2987         lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
2988                               IOERR_SLI_DOWN);
2989 }
2990
2991 /**
2992  * lpfc_sli_brdready_s3 - Check for sli3 host ready status
2993  * @phba: Pointer to HBA context object.
2994  * @mask: Bit mask to be checked.
2995  *
2996  * This function reads the host status register and compares
2997  * with the provided bit mask to check if HBA completed
2998  * the restart. This function will wait in a loop for the
2999  * HBA to complete restart. If the HBA does not restart within
3000  * 15 iterations, the function will reset the HBA again. The
3001  * function returns 1 when HBA fail to restart otherwise returns
3002  * zero.
3003  **/
3004 static int
3005 lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
3006 {
3007         uint32_t status;
3008         int i = 0;
3009         int retval = 0;
3010
3011         /* Read the HBA Host Status Register */
3012         status = readl(phba->HSregaddr);
3013
3014         /*
3015          * Check status register every 100ms for 5 retries, then every
3016          * 500ms for 5, then every 2.5 sec for 5, then reset board and
3017          * every 2.5 sec for 4.
3018          * Break our of the loop if errors occurred during init.
3019          */
3020         while (((status & mask) != mask) &&
3021                !(status & HS_FFERM) &&
3022                i++ < 20) {
3023
3024                 if (i <= 5)
3025                         msleep(10);
3026                 else if (i <= 10)
3027                         msleep(500);
3028                 else
3029                         msleep(2500);
3030
3031                 if (i == 15) {
3032                                 /* Do post */
3033                         phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3034                         lpfc_sli_brdrestart(phba);
3035                 }
3036                 /* Read the HBA Host Status Register */
3037                 status = readl(phba->HSregaddr);
3038         }
3039
3040         /* Check to see if any errors occurred during init */
3041         if ((status & HS_FFERM) || (i >= 20)) {
3042                 phba->link_state = LPFC_HBA_ERROR;
3043                 retval = 1;
3044         }
3045
3046         return retval;
3047 }
3048
3049 /**
3050  * lpfc_sli_brdready_s4 - Check for sli4 host ready status
3051  * @phba: Pointer to HBA context object.
3052  * @mask: Bit mask to be checked.
3053  *
3054  * This function checks the host status register to check if HBA is
3055  * ready. This function will wait in a loop for the HBA to be ready
3056  * If the HBA is not ready , the function will will reset the HBA PCI
3057  * function again. The function returns 1 when HBA fail to be ready
3058  * otherwise returns zero.
3059  **/
3060 static int
3061 lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
3062 {
3063         uint32_t status;
3064         int retval = 0;
3065
3066         /* Read the HBA Host Status Register */
3067         status = lpfc_sli4_post_status_check(phba);
3068
3069         if (status) {
3070                 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3071                 lpfc_sli_brdrestart(phba);
3072                 status = lpfc_sli4_post_status_check(phba);
3073         }
3074
3075         /* Check to see if any errors occurred during init */
3076         if (status) {
3077                 phba->link_state = LPFC_HBA_ERROR;
3078                 retval = 1;
3079         } else
3080                 phba->sli4_hba.intr_enable = 0;
3081
3082         return retval;
3083 }
3084
3085 /**
3086  * lpfc_sli_brdready - Wrapper func for checking the hba readyness
3087  * @phba: Pointer to HBA context object.
3088  * @mask: Bit mask to be checked.
3089  *
3090  * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
3091  * from the API jump table function pointer from the lpfc_hba struct.
3092  **/
3093 int
3094 lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
3095 {
3096         return phba->lpfc_sli_brdready(phba, mask);
3097 }
3098
3099 #define BARRIER_TEST_PATTERN (0xdeadbeef)
3100
3101 /**
3102  * lpfc_reset_barrier - Make HBA ready for HBA reset
3103  * @phba: Pointer to HBA context object.
3104  *
3105  * This function is called before resetting an HBA. This
3106  * function requests HBA to quiesce DMAs before a reset.
3107  **/
3108 void lpfc_reset_barrier(struct lpfc_hba *phba)
3109 {
3110         uint32_t __iomem *resp_buf;
3111         uint32_t __iomem *mbox_buf;
3112         volatile uint32_t mbox;
3113         uint32_t hc_copy;
3114         int  i;
3115         uint8_t hdrtype;
3116
3117         pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
3118         if (hdrtype != 0x80 ||
3119             (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
3120              FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
3121                 return;
3122
3123         /*
3124          * Tell the other part of the chip to suspend temporarily all
3125          * its DMA activity.
3126          */
3127         resp_buf = phba->MBslimaddr;
3128
3129         /* Disable the error attention */
3130         hc_copy = readl(phba->HCregaddr);
3131         writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
3132         readl(phba->HCregaddr); /* flush */
3133         phba->link_flag |= LS_IGNORE_ERATT;
3134
3135         if (readl(phba->HAregaddr) & HA_ERATT) {
3136                 /* Clear Chip error bit */
3137                 writel(HA_ERATT, phba->HAregaddr);
3138            &nb