]> err.no Git - linux-2.6/blob - drivers/scsi/lpfc/lpfc_sli.c
[SCSI] lpfc 8.2.2 : Error messages and debugfs updates
[linux-2.6] / 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-2007 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
33 #include "lpfc_hw.h"
34 #include "lpfc_sli.h"
35 #include "lpfc_disc.h"
36 #include "lpfc_scsi.h"
37 #include "lpfc.h"
38 #include "lpfc_crtn.h"
39 #include "lpfc_logmsg.h"
40 #include "lpfc_compat.h"
41 #include "lpfc_debugfs.h"
42
43 /*
44  * Define macro to log: Mailbox command x%x cannot issue Data
45  * This allows multiple uses of lpfc_msgBlk0311
46  * w/o perturbing log msg utility.
47  */
48 #define LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag) \
49                         lpfc_printf_log(phba, \
50                                 KERN_INFO, \
51                                 LOG_MBOX | LOG_SLI, \
52                                 "%d (%d):0311 Mailbox command x%x cannot " \
53                                 "issue Data: x%x x%x x%x\n", \
54                                 phba->brd_no, \
55                                 pmbox->vport ? pmbox->vport->vpi : 0, \
56                                 pmbox->mb.mbxCommand,           \
57                                 phba->pport->port_state,        \
58                                 psli->sli_flag, \
59                                 flag)
60
61
62 /* There are only four IOCB completion types. */
63 typedef enum _lpfc_iocb_type {
64         LPFC_UNKNOWN_IOCB,
65         LPFC_UNSOL_IOCB,
66         LPFC_SOL_IOCB,
67         LPFC_ABORT_IOCB
68 } lpfc_iocb_type;
69
70                 /* SLI-2/SLI-3 provide different sized iocbs.  Given a pointer
71                  * to the start of the ring, and the slot number of the
72                  * desired iocb entry, calc a pointer to that entry.
73                  */
74 static inline IOCB_t *
75 lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
76 {
77         return (IOCB_t *) (((char *) pring->cmdringaddr) +
78                            pring->cmdidx * phba->iocb_cmd_size);
79 }
80
81 static inline IOCB_t *
82 lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
83 {
84         return (IOCB_t *) (((char *) pring->rspringaddr) +
85                            pring->rspidx * phba->iocb_rsp_size);
86 }
87
88 static struct lpfc_iocbq *
89 __lpfc_sli_get_iocbq(struct lpfc_hba *phba)
90 {
91         struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
92         struct lpfc_iocbq * iocbq = NULL;
93
94         list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
95         return iocbq;
96 }
97
98 struct lpfc_iocbq *
99 lpfc_sli_get_iocbq(struct lpfc_hba *phba)
100 {
101         struct lpfc_iocbq * iocbq = NULL;
102         unsigned long iflags;
103
104         spin_lock_irqsave(&phba->hbalock, iflags);
105         iocbq = __lpfc_sli_get_iocbq(phba);
106         spin_unlock_irqrestore(&phba->hbalock, iflags);
107         return iocbq;
108 }
109
110 void
111 __lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
112 {
113         size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
114
115         /*
116          * Clean all volatile data fields, preserve iotag and node struct.
117          */
118         memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
119         list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
120 }
121
122 void
123 lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
124 {
125         unsigned long iflags;
126
127         /*
128          * Clean all volatile data fields, preserve iotag and node struct.
129          */
130         spin_lock_irqsave(&phba->hbalock, iflags);
131         __lpfc_sli_release_iocbq(phba, iocbq);
132         spin_unlock_irqrestore(&phba->hbalock, iflags);
133 }
134
135 /*
136  * Translate the iocb command to an iocb command type used to decide the final
137  * disposition of each completed IOCB.
138  */
139 static lpfc_iocb_type
140 lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
141 {
142         lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
143
144         if (iocb_cmnd > CMD_MAX_IOCB_CMD)
145                 return 0;
146
147         switch (iocb_cmnd) {
148         case CMD_XMIT_SEQUENCE_CR:
149         case CMD_XMIT_SEQUENCE_CX:
150         case CMD_XMIT_BCAST_CN:
151         case CMD_XMIT_BCAST_CX:
152         case CMD_ELS_REQUEST_CR:
153         case CMD_ELS_REQUEST_CX:
154         case CMD_CREATE_XRI_CR:
155         case CMD_CREATE_XRI_CX:
156         case CMD_GET_RPI_CN:
157         case CMD_XMIT_ELS_RSP_CX:
158         case CMD_GET_RPI_CR:
159         case CMD_FCP_IWRITE_CR:
160         case CMD_FCP_IWRITE_CX:
161         case CMD_FCP_IREAD_CR:
162         case CMD_FCP_IREAD_CX:
163         case CMD_FCP_ICMND_CR:
164         case CMD_FCP_ICMND_CX:
165         case CMD_FCP_TSEND_CX:
166         case CMD_FCP_TRSP_CX:
167         case CMD_FCP_TRECEIVE_CX:
168         case CMD_FCP_AUTO_TRSP_CX:
169         case CMD_ADAPTER_MSG:
170         case CMD_ADAPTER_DUMP:
171         case CMD_XMIT_SEQUENCE64_CR:
172         case CMD_XMIT_SEQUENCE64_CX:
173         case CMD_XMIT_BCAST64_CN:
174         case CMD_XMIT_BCAST64_CX:
175         case CMD_ELS_REQUEST64_CR:
176         case CMD_ELS_REQUEST64_CX:
177         case CMD_FCP_IWRITE64_CR:
178         case CMD_FCP_IWRITE64_CX:
179         case CMD_FCP_IREAD64_CR:
180         case CMD_FCP_IREAD64_CX:
181         case CMD_FCP_ICMND64_CR:
182         case CMD_FCP_ICMND64_CX:
183         case CMD_FCP_TSEND64_CX:
184         case CMD_FCP_TRSP64_CX:
185         case CMD_FCP_TRECEIVE64_CX:
186         case CMD_GEN_REQUEST64_CR:
187         case CMD_GEN_REQUEST64_CX:
188         case CMD_XMIT_ELS_RSP64_CX:
189                 type = LPFC_SOL_IOCB;
190                 break;
191         case CMD_ABORT_XRI_CN:
192         case CMD_ABORT_XRI_CX:
193         case CMD_CLOSE_XRI_CN:
194         case CMD_CLOSE_XRI_CX:
195         case CMD_XRI_ABORTED_CX:
196         case CMD_ABORT_MXRI64_CN:
197                 type = LPFC_ABORT_IOCB;
198                 break;
199         case CMD_RCV_SEQUENCE_CX:
200         case CMD_RCV_ELS_REQ_CX:
201         case CMD_RCV_SEQUENCE64_CX:
202         case CMD_RCV_ELS_REQ64_CX:
203         case CMD_IOCB_RCV_SEQ64_CX:
204         case CMD_IOCB_RCV_ELS64_CX:
205         case CMD_IOCB_RCV_CONT64_CX:
206                 type = LPFC_UNSOL_IOCB;
207                 break;
208         default:
209                 type = LPFC_UNKNOWN_IOCB;
210                 break;
211         }
212
213         return type;
214 }
215
216 static int
217 lpfc_sli_ring_map(struct lpfc_hba *phba)
218 {
219         struct lpfc_sli *psli = &phba->sli;
220         LPFC_MBOXQ_t *pmb;
221         MAILBOX_t *pmbox;
222         int i, rc, ret = 0;
223
224         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
225         if (!pmb)
226                 return -ENOMEM;
227         pmbox = &pmb->mb;
228         phba->link_state = LPFC_INIT_MBX_CMDS;
229         for (i = 0; i < psli->num_rings; i++) {
230                 lpfc_config_ring(phba, i, pmb);
231                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
232                 if (rc != MBX_SUCCESS) {
233                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
234                                         "%d:0446 Adapter failed to init (%d), "
235                                         "mbxCmd x%x CFG_RING, mbxStatus x%x, "
236                                         "ring %d\n",
237                                         phba->brd_no, rc,
238                                         pmbox->mbxCommand,
239                                         pmbox->mbxStatus,
240                                         i);
241                         phba->link_state = LPFC_HBA_ERROR;
242                         ret = -ENXIO;
243                         break;
244                 }
245         }
246         mempool_free(pmb, phba->mbox_mem_pool);
247         return ret;
248 }
249
250 static int
251 lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
252                         struct lpfc_iocbq *piocb)
253 {
254         list_add_tail(&piocb->list, &pring->txcmplq);
255         pring->txcmplq_cnt++;
256         if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
257            (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
258            (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
259                 if (!piocb->vport)
260                         BUG();
261                 else
262                         mod_timer(&piocb->vport->els_tmofunc,
263                                   jiffies + HZ * (phba->fc_ratov << 1));
264         }
265
266
267         return 0;
268 }
269
270 static struct lpfc_iocbq *
271 lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
272 {
273         struct lpfc_iocbq *cmd_iocb;
274
275         list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
276         if (cmd_iocb != NULL)
277                 pring->txq_cnt--;
278         return cmd_iocb;
279 }
280
281 static IOCB_t *
282 lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
283 {
284         struct lpfc_pgp *pgp = (phba->sli_rev == 3) ?
285                 &phba->slim2p->mbx.us.s3_pgp.port[pring->ringno] :
286                 &phba->slim2p->mbx.us.s2.port[pring->ringno];
287         uint32_t  max_cmd_idx = pring->numCiocb;
288
289         if ((pring->next_cmdidx == pring->cmdidx) &&
290            (++pring->next_cmdidx >= max_cmd_idx))
291                 pring->next_cmdidx = 0;
292
293         if (unlikely(pring->local_getidx == pring->next_cmdidx)) {
294
295                 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
296
297                 if (unlikely(pring->local_getidx >= max_cmd_idx)) {
298                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
299                                         "%d:0315 Ring %d issue: portCmdGet %d "
300                                         "is bigger then cmd ring %d\n",
301                                         phba->brd_no, pring->ringno,
302                                         pring->local_getidx, max_cmd_idx);
303
304                         phba->link_state = LPFC_HBA_ERROR;
305                         /*
306                          * All error attention handlers are posted to
307                          * worker thread
308                          */
309                         phba->work_ha |= HA_ERATT;
310                         phba->work_hs = HS_FFER3;
311
312                         /* hbalock should already be held */
313                         if (phba->work_wait)
314                                 lpfc_worker_wake_up(phba);
315
316                         return NULL;
317                 }
318
319                 if (pring->local_getidx == pring->next_cmdidx)
320                         return NULL;
321         }
322
323         return lpfc_cmd_iocb(phba, pring);
324 }
325
326 uint16_t
327 lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
328 {
329         struct lpfc_iocbq **new_arr;
330         struct lpfc_iocbq **old_arr;
331         size_t new_len;
332         struct lpfc_sli *psli = &phba->sli;
333         uint16_t iotag;
334
335         spin_lock_irq(&phba->hbalock);
336         iotag = psli->last_iotag;
337         if(++iotag < psli->iocbq_lookup_len) {
338                 psli->last_iotag = iotag;
339                 psli->iocbq_lookup[iotag] = iocbq;
340                 spin_unlock_irq(&phba->hbalock);
341                 iocbq->iotag = iotag;
342                 return iotag;
343         } else if (psli->iocbq_lookup_len < (0xffff
344                                            - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
345                 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
346                 spin_unlock_irq(&phba->hbalock);
347                 new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
348                                   GFP_KERNEL);
349                 if (new_arr) {
350                         spin_lock_irq(&phba->hbalock);
351                         old_arr = psli->iocbq_lookup;
352                         if (new_len <= psli->iocbq_lookup_len) {
353                                 /* highly unprobable case */
354                                 kfree(new_arr);
355                                 iotag = psli->last_iotag;
356                                 if(++iotag < psli->iocbq_lookup_len) {
357                                         psli->last_iotag = iotag;
358                                         psli->iocbq_lookup[iotag] = iocbq;
359                                         spin_unlock_irq(&phba->hbalock);
360                                         iocbq->iotag = iotag;
361                                         return iotag;
362                                 }
363                                 spin_unlock_irq(&phba->hbalock);
364                                 return 0;
365                         }
366                         if (psli->iocbq_lookup)
367                                 memcpy(new_arr, old_arr,
368                                        ((psli->last_iotag  + 1) *
369                                         sizeof (struct lpfc_iocbq *)));
370                         psli->iocbq_lookup = new_arr;
371                         psli->iocbq_lookup_len = new_len;
372                         psli->last_iotag = iotag;
373                         psli->iocbq_lookup[iotag] = iocbq;
374                         spin_unlock_irq(&phba->hbalock);
375                         iocbq->iotag = iotag;
376                         kfree(old_arr);
377                         return iotag;
378                 }
379         } else
380                 spin_unlock_irq(&phba->hbalock);
381
382         lpfc_printf_log(phba, KERN_ERR,LOG_SLI,
383                         "%d:0318 Failed to allocate IOTAG.last IOTAG is %d\n",
384                         phba->brd_no, psli->last_iotag);
385
386         return 0;
387 }
388
389 static void
390 lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
391                 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
392 {
393         /*
394          * Set up an iotag
395          */
396         nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
397
398         if (pring->ringno == LPFC_ELS_RING) {
399                 lpfc_debugfs_slow_ring_trc(phba,
400                         "IOCB cmd ring:   wd4:x%08x wd6:x%08x wd7:x%08x",
401                         *(((uint32_t *) &nextiocb->iocb) + 4),
402                         *(((uint32_t *) &nextiocb->iocb) + 6),
403                         *(((uint32_t *) &nextiocb->iocb) + 7));
404         }
405
406         /*
407          * Issue iocb command to adapter
408          */
409         lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
410         wmb();
411         pring->stats.iocb_cmd++;
412
413         /*
414          * If there is no completion routine to call, we can release the
415          * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
416          * that have no rsp ring completion, iocb_cmpl MUST be NULL.
417          */
418         if (nextiocb->iocb_cmpl)
419                 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
420         else
421                 __lpfc_sli_release_iocbq(phba, nextiocb);
422
423         /*
424          * Let the HBA know what IOCB slot will be the next one the
425          * driver will put a command into.
426          */
427         pring->cmdidx = pring->next_cmdidx;
428         writel(pring->cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
429 }
430
431 static void
432 lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
433 {
434         int ringno = pring->ringno;
435
436         pring->flag |= LPFC_CALL_RING_AVAILABLE;
437
438         wmb();
439
440         /*
441          * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
442          * The HBA will tell us when an IOCB entry is available.
443          */
444         writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
445         readl(phba->CAregaddr); /* flush */
446
447         pring->stats.iocb_cmd_full++;
448 }
449
450 static void
451 lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
452 {
453         int ringno = pring->ringno;
454
455         /*
456          * Tell the HBA that there is work to do in this ring.
457          */
458         wmb();
459         writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
460         readl(phba->CAregaddr); /* flush */
461 }
462
463 static void
464 lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
465 {
466         IOCB_t *iocb;
467         struct lpfc_iocbq *nextiocb;
468
469         /*
470          * Check to see if:
471          *  (a) there is anything on the txq to send
472          *  (b) link is up
473          *  (c) link attention events can be processed (fcp ring only)
474          *  (d) IOCB processing is not blocked by the outstanding mbox command.
475          */
476         if (pring->txq_cnt &&
477             lpfc_is_link_up(phba) &&
478             (pring->ringno != phba->sli.fcp_ring ||
479              phba->sli.sli_flag & LPFC_PROCESS_LA) &&
480             !(pring->flag & LPFC_STOP_IOCB_MBX)) {
481
482                 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
483                        (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
484                         lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
485
486                 if (iocb)
487                         lpfc_sli_update_ring(phba, pring);
488                 else
489                         lpfc_sli_update_full_ring(phba, pring);
490         }
491
492         return;
493 }
494
495 /* lpfc_sli_turn_on_ring is only called by lpfc_sli_handle_mb_event below */
496 static void
497 lpfc_sli_turn_on_ring(struct lpfc_hba *phba, int ringno)
498 {
499         struct lpfc_pgp *pgp = (phba->sli_rev == 3) ?
500                 &phba->slim2p->mbx.us.s3_pgp.port[ringno] :
501                 &phba->slim2p->mbx.us.s2.port[ringno];
502         unsigned long iflags;
503
504         /* If the ring is active, flag it */
505         spin_lock_irqsave(&phba->hbalock, iflags);
506         if (phba->sli.ring[ringno].cmdringaddr) {
507                 if (phba->sli.ring[ringno].flag & LPFC_STOP_IOCB_MBX) {
508                         phba->sli.ring[ringno].flag &= ~LPFC_STOP_IOCB_MBX;
509                         /*
510                          * Force update of the local copy of cmdGetInx
511                          */
512                         phba->sli.ring[ringno].local_getidx
513                                 = le32_to_cpu(pgp->cmdGetInx);
514                         lpfc_sli_resume_iocb(phba, &phba->sli.ring[ringno]);
515                 }
516         }
517         spin_unlock_irqrestore(&phba->hbalock, iflags);
518 }
519
520 struct lpfc_hbq_entry *
521 lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
522 {
523         struct hbq_s *hbqp = &phba->hbqs[hbqno];
524
525         if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
526             ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
527                 hbqp->next_hbqPutIdx = 0;
528
529         if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
530                 uint32_t raw_index = phba->hbq_get[hbqno];
531                 uint32_t getidx = le32_to_cpu(raw_index);
532
533                 hbqp->local_hbqGetIdx = getidx;
534
535                 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
536                         lpfc_printf_log(phba, KERN_ERR,
537                                         LOG_SLI | LOG_VPORT,
538                                         "%d:1802 HBQ %d: local_hbqGetIdx "
539                                         "%u is > than hbqp->entry_count %u\n",
540                                         phba->brd_no, hbqno,
541                                         hbqp->local_hbqGetIdx,
542                                         hbqp->entry_count);
543
544                         phba->link_state = LPFC_HBA_ERROR;
545                         return NULL;
546                 }
547
548                 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
549                         return NULL;
550         }
551
552         return (struct lpfc_hbq_entry *) phba->hbqslimp.virt + hbqp->hbqPutIdx;
553 }
554
555 void
556 lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
557 {
558         struct lpfc_dmabuf *dmabuf, *next_dmabuf;
559         struct hbq_dmabuf *hbq_buf;
560
561         /* Return all memory used by all HBQs */
562         list_for_each_entry_safe(dmabuf, next_dmabuf,
563                                  &phba->hbq_buffer_list, list) {
564                 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
565                 list_del(&hbq_buf->dbuf.list);
566                 lpfc_hbq_free(phba, hbq_buf->dbuf.virt, hbq_buf->dbuf.phys);
567                 kfree(hbq_buf);
568         }
569 }
570
571 static void
572 lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
573                          struct hbq_dmabuf *hbq_buf)
574 {
575         struct lpfc_hbq_entry *hbqe;
576         dma_addr_t physaddr = hbq_buf->dbuf.phys;
577
578         /* Get next HBQ entry slot to use */
579         hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
580         if (hbqe) {
581                 struct hbq_s *hbqp = &phba->hbqs[hbqno];
582
583                 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
584                 hbqe->bde.addrLow  = le32_to_cpu(putPaddrLow(physaddr));
585                 hbqe->bde.tus.f.bdeSize = FCELSSIZE;
586                 hbqe->bde.tus.f.bdeFlags = 0;
587                 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
588                 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
589                                 /* Sync SLIM */
590                 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
591                 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
592                                 /* flush */
593                 readl(phba->hbq_put + hbqno);
594                 list_add_tail(&hbq_buf->dbuf.list, &phba->hbq_buffer_list);
595         }
596 }
597
598 static struct lpfc_hbq_init lpfc_els_hbq = {
599         .rn = 1,
600         .entry_count = 200,
601         .mask_count = 0,
602         .profile = 0,
603         .ring_mask = 1 << LPFC_ELS_RING,
604         .buffer_count = 0,
605         .init_count = 20,
606         .add_count = 5,
607 };
608
609 static struct lpfc_hbq_init *lpfc_hbq_defs[] = {
610         &lpfc_els_hbq,
611 };
612
613 int
614 lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
615 {
616         uint32_t i, start, end;
617         struct hbq_dmabuf *hbq_buffer;
618
619         start = lpfc_hbq_defs[hbqno]->buffer_count;
620         end = count + lpfc_hbq_defs[hbqno]->buffer_count;
621         if (end > lpfc_hbq_defs[hbqno]->entry_count) {
622                 end = lpfc_hbq_defs[hbqno]->entry_count;
623         }
624
625         /* Populate HBQ entries */
626         for (i = start; i < end; i++) {
627                 hbq_buffer = kmalloc(sizeof(struct hbq_dmabuf),
628                                      GFP_KERNEL);
629                 if (!hbq_buffer)
630                         return 1;
631                 hbq_buffer->dbuf.virt = lpfc_hbq_alloc(phba, MEM_PRI,
632                                                         &hbq_buffer->dbuf.phys);
633                 if (hbq_buffer->dbuf.virt == NULL)
634                         return 1;
635                 hbq_buffer->tag = (i | (hbqno << 16));
636                 lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer);
637                 lpfc_hbq_defs[hbqno]->buffer_count++;
638         }
639         return 0;
640 }
641
642 int
643 lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
644 {
645         return(lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
646                                          lpfc_hbq_defs[qno]->add_count));
647 }
648
649 int
650 lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
651 {
652         return(lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
653                                          lpfc_hbq_defs[qno]->init_count));
654 }
655
656 struct hbq_dmabuf *
657 lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
658 {
659         struct lpfc_dmabuf *d_buf;
660         struct hbq_dmabuf *hbq_buf;
661
662         list_for_each_entry(d_buf, &phba->hbq_buffer_list, list) {
663                 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
664                 if ((hbq_buf->tag & 0xffff) == tag) {
665                         return hbq_buf;
666                 }
667         }
668         lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
669                         "%d:1803 Bad hbq tag. Data: x%x x%x\n",
670                         phba->brd_no, tag,
671                         lpfc_hbq_defs[tag >> 16]->buffer_count);
672         return NULL;
673 }
674
675 void
676 lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *sp)
677 {
678         uint32_t hbqno;
679
680         if (sp) {
681                 hbqno = sp->tag >> 16;
682                 lpfc_sli_hbq_to_firmware(phba, hbqno, sp);
683         }
684 }
685
686 static int
687 lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
688 {
689         uint8_t ret;
690
691         switch (mbxCommand) {
692         case MBX_LOAD_SM:
693         case MBX_READ_NV:
694         case MBX_WRITE_NV:
695         case MBX_RUN_BIU_DIAG:
696         case MBX_INIT_LINK:
697         case MBX_DOWN_LINK:
698         case MBX_CONFIG_LINK:
699         case MBX_CONFIG_RING:
700         case MBX_RESET_RING:
701         case MBX_READ_CONFIG:
702         case MBX_READ_RCONFIG:
703         case MBX_READ_SPARM:
704         case MBX_READ_STATUS:
705         case MBX_READ_RPI:
706         case MBX_READ_XRI:
707         case MBX_READ_REV:
708         case MBX_READ_LNK_STAT:
709         case MBX_REG_LOGIN:
710         case MBX_UNREG_LOGIN:
711         case MBX_READ_LA:
712         case MBX_CLEAR_LA:
713         case MBX_DUMP_MEMORY:
714         case MBX_DUMP_CONTEXT:
715         case MBX_RUN_DIAGS:
716         case MBX_RESTART:
717         case MBX_UPDATE_CFG:
718         case MBX_DOWN_LOAD:
719         case MBX_DEL_LD_ENTRY:
720         case MBX_RUN_PROGRAM:
721         case MBX_SET_MASK:
722         case MBX_SET_SLIM:
723         case MBX_UNREG_D_ID:
724         case MBX_KILL_BOARD:
725         case MBX_CONFIG_FARP:
726         case MBX_BEACON:
727         case MBX_LOAD_AREA:
728         case MBX_RUN_BIU_DIAG64:
729         case MBX_CONFIG_PORT:
730         case MBX_READ_SPARM64:
731         case MBX_READ_RPI64:
732         case MBX_REG_LOGIN64:
733         case MBX_READ_LA64:
734         case MBX_FLASH_WR_ULA:
735         case MBX_SET_DEBUG:
736         case MBX_LOAD_EXP_ROM:
737         case MBX_REG_VPI:
738         case MBX_UNREG_VPI:
739         case MBX_HEARTBEAT:
740                 ret = mbxCommand;
741                 break;
742         default:
743                 ret = MBX_SHUTDOWN;
744                 break;
745         }
746         return ret;
747 }
748 static void
749 lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
750 {
751         wait_queue_head_t *pdone_q;
752         unsigned long drvr_flag;
753
754         /*
755          * If pdone_q is empty, the driver thread gave up waiting and
756          * continued running.
757          */
758         pmboxq->mbox_flag |= LPFC_MBX_WAKE;
759         spin_lock_irqsave(&phba->hbalock, drvr_flag);
760         pdone_q = (wait_queue_head_t *) pmboxq->context1;
761         if (pdone_q)
762                 wake_up_interruptible(pdone_q);
763         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
764         return;
765 }
766
767 void
768 lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
769 {
770         struct lpfc_dmabuf *mp;
771         uint16_t rpi;
772         int rc;
773
774         mp = (struct lpfc_dmabuf *) (pmb->context1);
775
776         if (mp) {
777                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
778                 kfree(mp);
779         }
780
781         /*
782          * If a REG_LOGIN succeeded  after node is destroyed or node
783          * is in re-discovery driver need to cleanup the RPI.
784          */
785         if (!(phba->pport->load_flag & FC_UNLOADING) &&
786             pmb->mb.mbxCommand == MBX_REG_LOGIN64 &&
787             !pmb->mb.mbxStatus) {
788
789                 rpi = pmb->mb.un.varWords[0];
790                 lpfc_unreg_login(phba, pmb->mb.un.varRegLogin.vpi, rpi, pmb);
791                 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
792                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
793                 if (rc != MBX_NOT_FINISHED)
794                         return;
795         }
796
797         mempool_free(pmb, phba->mbox_mem_pool);
798         return;
799 }
800
801 int
802 lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
803 {
804         MAILBOX_t *pmbox;
805         LPFC_MBOXQ_t *pmb;
806         int rc;
807         LIST_HEAD(cmplq);
808
809         phba->sli.slistat.mbox_event++;
810
811         /* Get all completed mailboxe buffers into the cmplq */
812         spin_lock_irq(&phba->hbalock);
813         list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
814         spin_unlock_irq(&phba->hbalock);
815
816         /* Get a Mailbox buffer to setup mailbox commands for callback */
817         do {
818                 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
819                 if (pmb == NULL)
820                         break;
821
822                 pmbox = &pmb->mb;
823
824                 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
825                         if (pmb->vport) {
826                                 lpfc_debugfs_disc_trc(pmb->vport,
827                                         LPFC_DISC_TRC_MBOX_VPORT,
828                                         "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
829                                         (uint32_t)pmbox->mbxCommand,
830                                         pmbox->un.varWords[0],
831                                         pmbox->un.varWords[1]);
832                         }
833                         else {
834                                 lpfc_debugfs_disc_trc(phba->pport,
835                                         LPFC_DISC_TRC_MBOX,
836                                         "MBOX cmpl:       cmd:x%x mb:x%x x%x",
837                                         (uint32_t)pmbox->mbxCommand,
838                                         pmbox->un.varWords[0],
839                                         pmbox->un.varWords[1]);
840                         }
841                 }
842
843                 /*
844                  * It is a fatal error if unknown mbox command completion.
845                  */
846                 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
847                     MBX_SHUTDOWN) {
848
849                         /* Unknow mailbox command compl */
850                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
851                                         "%d (%d):0323 Unknown Mailbox command "
852                                         "%x Cmpl\n",
853                                         phba->brd_no,
854                                         pmb->vport ? pmb->vport->vpi : 0,
855                                         pmbox->mbxCommand);
856                         phba->link_state = LPFC_HBA_ERROR;
857                         phba->work_hs = HS_FFER3;
858                         lpfc_handle_eratt(phba);
859                         continue;
860                 }
861
862                 if (pmbox->mbxStatus) {
863                         phba->sli.slistat.mbox_stat_err++;
864                         if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
865                                 /* Mbox cmd cmpl error - RETRYing */
866                                 lpfc_printf_log(phba, KERN_INFO,
867                                                 LOG_MBOX | LOG_SLI,
868                                                 "%d (%d):0305 Mbox cmd cmpl "
869                                                 "error - RETRYing Data: x%x "
870                                                 "x%x x%x x%x\n",
871                                                 phba->brd_no,
872                                                 pmb->vport ? pmb->vport->vpi :0,
873                                                 pmbox->mbxCommand,
874                                                 pmbox->mbxStatus,
875                                                 pmbox->un.varWords[0],
876                                                 pmb->vport->port_state);
877                                 pmbox->mbxStatus = 0;
878                                 pmbox->mbxOwner = OWN_HOST;
879                                 spin_lock_irq(&phba->hbalock);
880                                 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
881                                 spin_unlock_irq(&phba->hbalock);
882                                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
883                                 if (rc == MBX_SUCCESS)
884                                         continue;
885                         }
886                 }
887
888                 /* Mailbox cmd <cmd> Cmpl <cmpl> */
889                 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
890                                 "%d (%d):0307 Mailbox cmd x%x Cmpl x%p "
891                                 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x\n",
892                                 phba->brd_no,
893                                 pmb->vport ? pmb->vport->vpi : 0,
894                                 pmbox->mbxCommand,
895                                 pmb->mbox_cmpl,
896                                 *((uint32_t *) pmbox),
897                                 pmbox->un.varWords[0],
898                                 pmbox->un.varWords[1],
899                                 pmbox->un.varWords[2],
900                                 pmbox->un.varWords[3],
901                                 pmbox->un.varWords[4],
902                                 pmbox->un.varWords[5],
903                                 pmbox->un.varWords[6],
904                                 pmbox->un.varWords[7]);
905
906                 if (pmb->mbox_cmpl)
907                         pmb->mbox_cmpl(phba,pmb);
908         } while (1);
909         return 0;
910 }
911
912 static struct lpfc_dmabuf *
913 lpfc_sli_replace_hbqbuff(struct lpfc_hba *phba, uint32_t tag)
914 {
915         struct hbq_dmabuf *hbq_entry, *new_hbq_entry;
916
917         hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
918         if (hbq_entry == NULL)
919                 return NULL;
920         list_del(&hbq_entry->dbuf.list);
921         new_hbq_entry = kmalloc(sizeof(struct hbq_dmabuf), GFP_ATOMIC);
922         if (new_hbq_entry == NULL)
923                 return &hbq_entry->dbuf;
924         new_hbq_entry->dbuf = hbq_entry->dbuf;
925         new_hbq_entry->tag = -1;
926         hbq_entry->dbuf.virt = lpfc_hbq_alloc(phba, 0, &hbq_entry->dbuf.phys);
927         if (hbq_entry->dbuf.virt == NULL) {
928                 kfree(new_hbq_entry);
929                 return &hbq_entry->dbuf;
930         }
931         lpfc_sli_free_hbq(phba, hbq_entry);
932         return &new_hbq_entry->dbuf;
933 }
934
935 static int
936 lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
937                             struct lpfc_iocbq *saveq)
938 {
939         IOCB_t           * irsp;
940         WORD5            * w5p;
941         uint32_t           Rctl, Type;
942         uint32_t           match, i;
943
944         match = 0;
945         irsp = &(saveq->iocb);
946         if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX)
947             || (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX)
948             || (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)
949             || (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX)) {
950                 Rctl = FC_ELS_REQ;
951                 Type = FC_ELS_DATA;
952         } else {
953                 w5p =
954                     (WORD5 *) & (saveq->iocb.un.
955                                  ulpWord[5]);
956                 Rctl = w5p->hcsw.Rctl;
957                 Type = w5p->hcsw.Type;
958
959                 /* Firmware Workaround */
960                 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
961                         (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
962                          irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
963                         Rctl = FC_ELS_REQ;
964                         Type = FC_ELS_DATA;
965                         w5p->hcsw.Rctl = Rctl;
966                         w5p->hcsw.Type = Type;
967                 }
968         }
969
970         if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
971                 if (irsp->ulpBdeCount != 0)
972                         saveq->context2 = lpfc_sli_replace_hbqbuff(phba,
973                                                 irsp->un.ulpWord[3]);
974                 if (irsp->ulpBdeCount == 2)
975                         saveq->context3 = lpfc_sli_replace_hbqbuff(phba,
976                                                 irsp->un.ulpWord[15]);
977         }
978
979         /* unSolicited Responses */
980         if (pring->prt[0].profile) {
981                 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
982                         (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
983                                                                         saveq);
984                 match = 1;
985         } else {
986                 /* We must search, based on rctl / type
987                    for the right routine */
988                 for (i = 0; i < pring->num_mask;
989                      i++) {
990                         if ((pring->prt[i].rctl ==
991                              Rctl)
992                             && (pring->prt[i].
993                                 type == Type)) {
994                                 if (pring->prt[i].lpfc_sli_rcv_unsol_event)
995                                         (pring->prt[i].lpfc_sli_rcv_unsol_event)
996                                                         (phba, pring, saveq);
997                                 match = 1;
998                                 break;
999                         }
1000                 }
1001         }
1002         if (match == 0) {
1003                 /* Unexpected Rctl / Type received */
1004                 /* Ring <ringno> handler: unexpected
1005                    Rctl <Rctl> Type <Type> received */
1006                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
1007                                 "%d:0313 Ring %d handler: unexpected Rctl x%x "
1008                                 "Type x%x received\n",
1009                                 phba->brd_no,
1010                                 pring->ringno,
1011                                 Rctl,
1012                                 Type);
1013         }
1014         return 1;
1015 }
1016
1017 static struct lpfc_iocbq *
1018 lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
1019                       struct lpfc_sli_ring *pring,
1020                       struct lpfc_iocbq *prspiocb)
1021 {
1022         struct lpfc_iocbq *cmd_iocb = NULL;
1023         uint16_t iotag;
1024
1025         iotag = prspiocb->iocb.ulpIoTag;
1026
1027         if (iotag != 0 && iotag <= phba->sli.last_iotag) {
1028                 cmd_iocb = phba->sli.iocbq_lookup[iotag];
1029                 list_del_init(&cmd_iocb->list);
1030                 pring->txcmplq_cnt--;
1031                 return cmd_iocb;
1032         }
1033
1034         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
1035                         "%d:0317 iotag x%x is out off "
1036                         "range: max iotag x%x wd0 x%x\n",
1037                         phba->brd_no, iotag,
1038                         phba->sli.last_iotag,
1039                         *(((uint32_t *) &prspiocb->iocb) + 7));
1040         return NULL;
1041 }
1042
1043 static int
1044 lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1045                           struct lpfc_iocbq *saveq)
1046 {
1047         struct lpfc_iocbq *cmdiocbp;
1048         int rc = 1;
1049         unsigned long iflag;
1050
1051         /* Based on the iotag field, get the cmd IOCB from the txcmplq */
1052         spin_lock_irqsave(&phba->hbalock, iflag);
1053         cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
1054         spin_unlock_irqrestore(&phba->hbalock, iflag);
1055
1056         if (cmdiocbp) {
1057                 if (cmdiocbp->iocb_cmpl) {
1058                         /*
1059                          * Post all ELS completions to the worker thread.
1060                          * All other are passed to the completion callback.
1061                          */
1062                         if (pring->ringno == LPFC_ELS_RING) {
1063                                 if (cmdiocbp->iocb_flag & LPFC_DRIVER_ABORTED) {
1064                                         cmdiocbp->iocb_flag &=
1065                                                 ~LPFC_DRIVER_ABORTED;
1066                                         saveq->iocb.ulpStatus =
1067                                                 IOSTAT_LOCAL_REJECT;
1068                                         saveq->iocb.un.ulpWord[4] =
1069                                                 IOERR_SLI_ABORTED;
1070                                 }
1071                         }
1072                         (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
1073                 } else
1074                         lpfc_sli_release_iocbq(phba, cmdiocbp);
1075         } else {
1076                 /*
1077                  * Unknown initiating command based on the response iotag.
1078                  * This could be the case on the ELS ring because of
1079                  * lpfc_els_abort().
1080                  */
1081                 if (pring->ringno != LPFC_ELS_RING) {
1082                         /*
1083                          * Ring <ringno> handler: unexpected completion IoTag
1084                          * <IoTag>
1085                          */
1086                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
1087                                         "%d (%d):0322 Ring %d handler: "
1088                                         "unexpected completion IoTag x%x "
1089                                         "Data: x%x x%x x%x x%x\n",
1090                                         phba->brd_no,
1091                                         cmdiocbp->vport->vpi,
1092                                         pring->ringno,
1093                                         saveq->iocb.ulpIoTag,
1094                                         saveq->iocb.ulpStatus,
1095                                         saveq->iocb.un.ulpWord[4],
1096                                         saveq->iocb.ulpCommand,
1097                                         saveq->iocb.ulpContext);
1098                 }
1099         }
1100
1101         return rc;
1102 }
1103
1104 static void
1105 lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1106 {
1107         struct lpfc_pgp *pgp = (phba->sli_rev == 3) ?
1108                 &phba->slim2p->mbx.us.s3_pgp.port[pring->ringno] :
1109                 &phba->slim2p->mbx.us.s2.port[pring->ringno];
1110         /*
1111          * Ring <ringno> handler: portRspPut <portRspPut> is bigger then
1112          * rsp ring <portRspMax>
1113          */
1114         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
1115                         "%d:0312 Ring %d handler: portRspPut %d "
1116                         "is bigger then rsp ring %d\n",
1117                         phba->brd_no, pring->ringno,
1118                         le32_to_cpu(pgp->rspPutInx),
1119                         pring->numRiocb);
1120
1121         phba->link_state = LPFC_HBA_ERROR;
1122
1123         /*
1124          * All error attention handlers are posted to
1125          * worker thread
1126          */
1127         phba->work_ha |= HA_ERATT;
1128         phba->work_hs = HS_FFER3;
1129
1130         /* hbalock should already be held */
1131         if (phba->work_wait)
1132                 lpfc_worker_wake_up(phba);
1133
1134         return;
1135 }
1136
1137 void lpfc_sli_poll_fcp_ring(struct lpfc_hba *phba)
1138 {
1139         struct lpfc_sli      *psli  = &phba->sli;
1140         struct lpfc_sli_ring *pring = &psli->ring[LPFC_FCP_RING];
1141         IOCB_t *irsp = NULL;
1142         IOCB_t *entry = NULL;
1143         struct lpfc_iocbq *cmdiocbq = NULL;
1144         struct lpfc_iocbq rspiocbq;
1145         struct lpfc_pgp *pgp;
1146         uint32_t status;
1147         uint32_t portRspPut, portRspMax;
1148         int type;
1149         uint32_t rsp_cmpl = 0;
1150         uint32_t ha_copy;
1151         unsigned long iflags;
1152
1153         pring->stats.iocb_event++;
1154
1155         pgp = (phba->sli_rev == 3) ?
1156                 &phba->slim2p->mbx.us.s3_pgp.port[pring->ringno] :
1157                 &phba->slim2p->mbx.us.s2.port[pring->ringno];
1158
1159
1160         /*
1161          * The next available response entry should never exceed the maximum
1162          * entries.  If it does, treat it as an adapter hardware error.
1163          */
1164         portRspMax = pring->numRiocb;
1165         portRspPut = le32_to_cpu(pgp->rspPutInx);
1166         if (unlikely(portRspPut >= portRspMax)) {
1167                 lpfc_sli_rsp_pointers_error(phba, pring);
1168                 return;
1169         }
1170
1171         rmb();
1172         while (pring->rspidx != portRspPut) {
1173                 entry = lpfc_resp_iocb(phba, pring);
1174                 if (++pring->rspidx >= portRspMax)
1175                         pring->rspidx = 0;
1176
1177                 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
1178                                       (uint32_t *) &rspiocbq.iocb,
1179                                       phba->iocb_rsp_size);
1180                 irsp = &rspiocbq.iocb;
1181                 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
1182                 pring->stats.iocb_rsp++;
1183                 rsp_cmpl++;
1184
1185                 if (unlikely(irsp->ulpStatus)) {
1186                         /* Rsp ring <ringno> error: IOCB */
1187                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
1188                                         "%d:0326 Rsp Ring %d error: IOCB Data: "
1189                                         "x%x x%x x%x x%x x%x x%x x%x x%x\n",
1190                                         phba->brd_no, pring->ringno,
1191                                         irsp->un.ulpWord[0],
1192                                         irsp->un.ulpWord[1],
1193                                         irsp->un.ulpWord[2],
1194                                         irsp->un.ulpWord[3],
1195                                         irsp->un.ulpWord[4],
1196                                         irsp->un.ulpWord[5],
1197                                         *(((uint32_t *) irsp) + 6),
1198                                         *(((uint32_t *) irsp) + 7));
1199                 }
1200
1201                 switch (type) {
1202                 case LPFC_ABORT_IOCB:
1203                 case LPFC_SOL_IOCB:
1204                         /*
1205                          * Idle exchange closed via ABTS from port.  No iocb
1206                          * resources need to be recovered.
1207                          */
1208                         if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
1209                                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
1210                                                 "%d:0314 IOCB cmd 0x%x"
1211                                                 " processed. Skipping"
1212                                                 " completion", phba->brd_no,
1213                                                 irsp->ulpCommand);
1214                                 break;
1215                         }
1216
1217                         spin_lock_irqsave(&phba->hbalock, iflags);
1218                         cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
1219                                                          &rspiocbq);
1220                         spin_unlock_irqrestore(&phba->hbalock, iflags);
1221                         if ((cmdiocbq) && (cmdiocbq->iocb_cmpl)) {
1222                                 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
1223                                                       &rspiocbq);
1224                         }
1225                         break;
1226                 default:
1227                         if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
1228                                 char adaptermsg[LPFC_MAX_ADPTMSG];
1229                                 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
1230                                 memcpy(&adaptermsg[0], (uint8_t *) irsp,
1231                                        MAX_MSG_DATA);
1232                                 dev_warn(&((phba->pcidev)->dev), "lpfc%d: %s",
1233                                          phba->brd_no, adaptermsg);
1234                         } else {
1235                                 /* Unknown IOCB command */
1236                                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
1237                                                 "%d:0321 Unknown IOCB command "
1238                                                 "Data: x%x, x%x x%x x%x x%x\n",
1239                                                 phba->brd_no, type,
1240                                                 irsp->ulpCommand,
1241                                                 irsp->ulpStatus,
1242                                                 irsp->ulpIoTag,
1243                                                 irsp->ulpContext);
1244                         }
1245                         break;
1246                 }
1247
1248                 /*
1249                  * The response IOCB has been processed.  Update the ring
1250                  * pointer in SLIM.  If the port response put pointer has not
1251                  * been updated, sync the pgp->rspPutInx and fetch the new port
1252                  * response put pointer.
1253                  */
1254                 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
1255
1256                 if (pring->rspidx == portRspPut)
1257                         portRspPut = le32_to_cpu(pgp->rspPutInx);
1258         }
1259
1260         ha_copy = readl(phba->HAregaddr);
1261         ha_copy >>= (LPFC_FCP_RING * 4);
1262
1263         if ((rsp_cmpl > 0) && (ha_copy & HA_R0RE_REQ)) {
1264                 spin_lock_irqsave(&phba->hbalock, iflags);
1265                 pring->stats.iocb_rsp_full++;
1266                 status = ((CA_R0ATT | CA_R0RE_RSP) << (LPFC_FCP_RING * 4));
1267                 writel(status, phba->CAregaddr);
1268                 readl(phba->CAregaddr);
1269                 spin_unlock_irqrestore(&phba->hbalock, iflags);
1270         }
1271         if ((ha_copy & HA_R0CE_RSP) &&
1272             (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
1273                 spin_lock_irqsave(&phba->hbalock, iflags);
1274                 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
1275                 pring->stats.iocb_cmd_empty++;
1276
1277                 /* Force update of the local copy of cmdGetInx */
1278                 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
1279                 lpfc_sli_resume_iocb(phba, pring);
1280
1281                 if ((pring->lpfc_sli_cmd_available))
1282                         (pring->lpfc_sli_cmd_available) (phba, pring);
1283
1284                 spin_unlock_irqrestore(&phba->hbalock, iflags);
1285         }
1286
1287         return;
1288 }
1289
1290 /*
1291  * This routine presumes LPFC_FCP_RING handling and doesn't bother
1292  * to check it explicitly.
1293  */
1294 static int
1295 lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
1296                                 struct lpfc_sli_ring *pring, uint32_t mask)
1297 {
1298         struct lpfc_pgp *pgp = (phba->sli_rev == 3) ?
1299                 &phba->slim2p->mbx.us.s3_pgp.port[pring->ringno] :
1300                 &phba->slim2p->mbx.us.s2.port[pring->ringno];
1301         IOCB_t *irsp = NULL;
1302         IOCB_t *entry = NULL;
1303         struct lpfc_iocbq *cmdiocbq = NULL;
1304         struct lpfc_iocbq rspiocbq;
1305         uint32_t status;
1306         uint32_t portRspPut, portRspMax;
1307         int rc = 1;
1308         lpfc_iocb_type type;
1309         unsigned long iflag;
1310         uint32_t rsp_cmpl = 0;
1311
1312         spin_lock_irqsave(&phba->hbalock, iflag);
1313         pring->stats.iocb_event++;
1314
1315         /*
1316          * The next available response entry should never exceed the maximum
1317          * entries.  If it does, treat it as an adapter hardware error.
1318          */
1319         portRspMax = pring->numRiocb;
1320         portRspPut = le32_to_cpu(pgp->rspPutInx);
1321         if (unlikely(portRspPut >= portRspMax)) {
1322                 lpfc_sli_rsp_pointers_error(phba, pring);
1323                 spin_unlock_irqrestore(&phba->hbalock, iflag);
1324                 return 1;
1325         }
1326
1327         rmb();
1328         while (pring->rspidx != portRspPut) {
1329                 /*
1330                  * Fetch an entry off the ring and copy it into a local data
1331                  * structure.  The copy involves a byte-swap since the
1332                  * network byte order and pci byte orders are different.
1333                  */
1334                 entry = lpfc_resp_iocb(phba, pring);
1335                 phba->last_completion_time = jiffies;
1336
1337                 if (++pring->rspidx >= portRspMax)
1338                         pring->rspidx = 0;
1339
1340                 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
1341                                       (uint32_t *) &rspiocbq.iocb,
1342                                       phba->iocb_rsp_size);
1343                 INIT_LIST_HEAD(&(rspiocbq.list));
1344                 irsp = &rspiocbq.iocb;
1345
1346                 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
1347                 pring->stats.iocb_rsp++;
1348                 rsp_cmpl++;
1349
1350                 if (unlikely(irsp->ulpStatus)) {
1351                         /*
1352                          * If resource errors reported from HBA, reduce
1353                          * queuedepths of the SCSI device.
1354                          */
1355                         if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
1356                                 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
1357                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
1358                                 lpfc_adjust_queue_depth(phba);
1359                                 spin_lock_irqsave(&phba->hbalock, iflag);
1360                         }
1361
1362                         /* Rsp ring <ringno> error: IOCB */
1363                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
1364                                         "%d:0336 Rsp Ring %d error: IOCB Data: "
1365                                         "x%x x%x x%x x%x x%x x%x x%x x%x\n",
1366                                         phba->brd_no, pring->ringno,
1367                                         irsp->un.ulpWord[0],
1368                                         irsp->un.ulpWord[1],
1369                                         irsp->un.ulpWord[2],
1370                                         irsp->un.ulpWord[3],
1371                                         irsp->un.ulpWord[4],
1372                                         irsp->un.ulpWord[5],
1373                                         *(((uint32_t *) irsp) + 6),
1374                                         *(((uint32_t *) irsp) + 7));
1375                 }
1376
1377                 switch (type) {
1378                 case LPFC_ABORT_IOCB:
1379                 case LPFC_SOL_IOCB:
1380                         /*
1381                          * Idle exchange closed via ABTS from port.  No iocb
1382                          * resources need to be recovered.
1383                          */
1384                         if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
1385                                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
1386                                                 "%d:0333 IOCB cmd 0x%x"
1387                                                 " processed. Skipping"
1388                                                 " completion\n",
1389                                                 phba->brd_no,
1390                                                 irsp->ulpCommand);
1391                                 break;
1392                         }
1393
1394                         cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
1395                                                          &rspiocbq);
1396                         if ((cmdiocbq) && (cmdiocbq->iocb_cmpl)) {
1397                                 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
1398                                         (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
1399                                                               &rspiocbq);
1400                                 } else {
1401                                         spin_unlock_irqrestore(&phba->hbalock,
1402                                                                iflag);
1403                                         (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
1404                                                               &rspiocbq);
1405                                         spin_lock_irqsave(&phba->hbalock,
1406                                                           iflag);
1407                                 }
1408                         }
1409                         break;
1410                 case LPFC_UNSOL_IOCB:
1411                         spin_unlock_irqrestore(&phba->hbalock, iflag);
1412                         lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
1413                         spin_lock_irqsave(&phba->hbalock, iflag);
1414                         break;
1415                 default:
1416                         if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
1417                                 char adaptermsg[LPFC_MAX_ADPTMSG];
1418                                 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
1419                                 memcpy(&adaptermsg[0], (uint8_t *) irsp,
1420                                        MAX_MSG_DATA);
1421                                 dev_warn(&((phba->pcidev)->dev), "lpfc%d: %s",
1422                                          phba->brd_no, adaptermsg);
1423                         } else {
1424                                 /* Unknown IOCB command */
1425                                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
1426                                                 "%d:0334 Unknown IOCB command "
1427                                                 "Data: x%x, x%x x%x x%x x%x\n",
1428                                                 phba->brd_no, type,
1429                                                 irsp->ulpCommand,
1430                                                 irsp->ulpStatus,
1431                                                 irsp->ulpIoTag,
1432                                                 irsp->ulpContext);
1433                         }
1434                         break;
1435                 }
1436
1437                 /*
1438                  * The response IOCB has been processed.  Update the ring
1439                  * pointer in SLIM.  If the port response put pointer has not
1440                  * been updated, sync the pgp->rspPutInx and fetch the new port
1441                  * response put pointer.
1442                  */
1443                 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
1444
1445                 if (pring->rspidx == portRspPut)
1446                         portRspPut = le32_to_cpu(pgp->rspPutInx);
1447         }
1448
1449         if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
1450                 pring->stats.iocb_rsp_full++;
1451                 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
1452                 writel(status, phba->CAregaddr);
1453                 readl(phba->CAregaddr);
1454         }
1455         if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
1456                 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
1457                 pring->stats.iocb_cmd_empty++;
1458
1459                 /* Force update of the local copy of cmdGetInx */
1460                 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
1461                 lpfc_sli_resume_iocb(phba, pring);
1462
1463                 if ((pring->lpfc_sli_cmd_available))
1464                         (pring->lpfc_sli_cmd_available) (phba, pring);
1465
1466         }
1467
1468         spin_unlock_irqrestore(&phba->hbalock, iflag);
1469         return rc;
1470 }
1471
1472 int
1473 lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
1474                                 struct lpfc_sli_ring *pring, uint32_t mask)
1475 {
1476         struct lpfc_pgp *pgp = (phba->sli_rev == 3) ?
1477                 &phba->slim2p->mbx.us.s3_pgp.port[pring->ringno] :
1478                 &phba->slim2p->mbx.us.s2.port[pring->ringno];
1479         IOCB_t *entry;
1480         IOCB_t *irsp = NULL;
1481         struct lpfc_iocbq *rspiocbp = NULL;
1482         struct lpfc_iocbq *next_iocb;
1483         struct lpfc_iocbq *cmdiocbp;
1484         struct lpfc_iocbq *saveq;
1485         uint8_t iocb_cmd_type;
1486         lpfc_iocb_type type;
1487         uint32_t status, free_saveq;
1488         uint32_t portRspPut, portRspMax;
1489         int rc = 1;
1490         unsigned long iflag;
1491
1492         spin_lock_irqsave(&phba->hbalock, iflag);
1493         pring->stats.iocb_event++;
1494
1495         /*
1496          * The next available response entry should never exceed the maximum
1497          * entries.  If it does, treat it as an adapter hardware error.
1498          */
1499         portRspMax = pring->numRiocb;
1500         portRspPut = le32_to_cpu(pgp->rspPutInx);
1501         if (portRspPut >= portRspMax) {
1502                 /*
1503                  * Ring <ringno> handler: portRspPut <portRspPut> is bigger then
1504                  * rsp ring <portRspMax>
1505                  */
1506                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
1507                                 "%d:0303 Ring %d handler: portRspPut %d "
1508                                 "is bigger then rsp ring %d\n",
1509                                 phba->brd_no, pring->ringno, portRspPut,
1510                                 portRspMax);
1511
1512                 phba->link_state = LPFC_HBA_ERROR;
1513                 spin_unlock_irqrestore(&phba->hbalock, iflag);
1514
1515                 phba->work_hs = HS_FFER3;
1516                 lpfc_handle_eratt(phba);
1517
1518                 return 1;
1519         }
1520
1521         rmb();
1522         while (pring->rspidx != portRspPut) {
1523                 /*
1524                  * Build a completion list and call the appropriate handler.
1525                  * The process is to get the next available response iocb, get
1526                  * a free iocb from the list, copy the response data into the
1527                  * free iocb, insert to the continuation list, and update the
1528                  * next response index to slim.  This process makes response
1529                  * iocb's in the ring available to DMA as fast as possible but
1530                  * pays a penalty for a copy operation.  Since the iocb is
1531                  * only 32 bytes, this penalty is considered small relative to
1532                  * the PCI reads for register values and a slim write.  When
1533                  * the ulpLe field is set, the entire Command has been
1534                  * received.
1535                  */
1536                 entry = lpfc_resp_iocb(phba, pring);
1537
1538                 phba->last_completion_time = jiffies;
1539                 rspiocbp = __lpfc_sli_get_iocbq(phba);
1540                 if (rspiocbp == NULL) {
1541                         printk(KERN_ERR "%s: out of buffers! Failing "
1542                                "completion.\n", __FUNCTION__);
1543                         break;
1544                 }
1545
1546                 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
1547                                       phba->iocb_rsp_size);
1548                 irsp = &rspiocbp->iocb;
1549
1550                 if (++pring->rspidx >= portRspMax)
1551                         pring->rspidx = 0;
1552
1553                 if (pring->ringno == LPFC_ELS_RING) {
1554                         lpfc_debugfs_slow_ring_trc(phba,
1555                         "IOCB rsp ring:   wd4:x%08x wd6:x%08x wd7:x%08x",
1556                                 *(((uint32_t *) irsp) + 4),
1557                                 *(((uint32_t *) irsp) + 6),
1558                                 *(((uint32_t *) irsp) + 7));
1559                 }
1560
1561                 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
1562
1563                 if (list_empty(&(pring->iocb_continueq))) {
1564                         list_add(&rspiocbp->list, &(pring->iocb_continueq));
1565                 } else {
1566                         list_add_tail(&rspiocbp->list,
1567                                       &(pring->iocb_continueq));
1568                 }
1569
1570                 pring->iocb_continueq_cnt++;
1571                 if (irsp->ulpLe) {
1572                         /*
1573                          * By default, the driver expects to free all resources
1574                          * associated with this iocb completion.
1575                          */
1576                         free_saveq = 1;
1577                         saveq = list_get_first(&pring->iocb_continueq,
1578                                                struct lpfc_iocbq, list);
1579                         irsp = &(saveq->iocb);
1580                         list_del_init(&pring->iocb_continueq);
1581                         pring->iocb_continueq_cnt = 0;
1582
1583                         pring->stats.iocb_rsp++;
1584
1585                         /*
1586                          * If resource errors reported from HBA, reduce
1587                          * queuedepths of the SCSI device.
1588                          */
1589                         if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
1590                              (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
1591                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
1592                                 lpfc_adjust_queue_depth(phba);
1593                                 spin_lock_irqsave(&phba->hbalock, iflag);
1594                         }
1595
1596                         if (irsp->ulpStatus) {
1597                                 /* Rsp ring <ringno> error: IOCB */
1598                                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
1599                                                 "%d:0328 Rsp Ring %d error: "
1600                                                 "IOCB Data: "
1601                                                 "x%x x%x x%x x%x "
1602                                                 "x%x x%x x%x x%x "
1603                                                 "x%x x%x x%x x%x "
1604                                                 "x%x x%x x%x x%x\n",
1605                                                 phba->brd_no,
1606                                                 pring->ringno,
1607                                                 irsp->un.ulpWord[0],
1608                                                 irsp->un.ulpWord[1],
1609                                                 irsp->un.ulpWord[2],
1610                                                 irsp->un.ulpWord[3],
1611                                                 irsp->un.ulpWord[4],
1612                                                 irsp->un.ulpWord[5],
1613                                                 *(((uint32_t *) irsp) + 6),
1614                                                 *(((uint32_t *) irsp) + 7),
1615                                                 *(((uint32_t *) irsp) + 8),
1616                                                 *(((uint32_t *) irsp) + 9),
1617                                                 *(((uint32_t *) irsp) + 10),
1618                                                 *(((uint32_t *) irsp) + 11),
1619                                                 *(((uint32_t *) irsp) + 12),
1620                                                 *(((uint32_t *) irsp) + 13),
1621                                                 *(((uint32_t *) irsp) + 14),
1622                                                 *(((uint32_t *) irsp) + 15));
1623                         }
1624
1625                         /*
1626                          * Fetch the IOCB command type and call the correct
1627                          * completion routine.  Solicited and Unsolicited
1628                          * IOCBs on the ELS ring get freed back to the
1629                          * lpfc_iocb_list by the discovery kernel thread.
1630                          */
1631                         iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
1632                         type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
1633                         if (type == LPFC_SOL_IOCB) {
1634                                 spin_unlock_irqrestore(&phba->hbalock,
1635                                                        iflag);
1636                                 rc = lpfc_sli_process_sol_iocb(phba, pring,
1637                                                                saveq);
1638                                 spin_lock_irqsave(&phba->hbalock, iflag);
1639                         } else if (type == LPFC_UNSOL_IOCB) {
1640                                 spin_unlock_irqrestore(&phba->hbalock,
1641                                                        iflag);
1642                                 rc = lpfc_sli_process_unsol_iocb(phba, pring,
1643                                                                  saveq);
1644                                 spin_lock_irqsave(&phba->hbalock, iflag);
1645                         } else if (type == LPFC_ABORT_IOCB) {
1646                                 if ((irsp->ulpCommand != CMD_XRI_ABORTED_CX) &&
1647                                     ((cmdiocbp =
1648                                       lpfc_sli_iocbq_lookup(phba, pring,
1649                                                             saveq)))) {
1650                                         /* Call the specified completion
1651                                            routine */
1652                                         if (cmdiocbp->iocb_cmpl) {
1653                                                 spin_unlock_irqrestore(
1654                                                        &phba->hbalock,
1655                                                        iflag);
1656                                                 (cmdiocbp->iocb_cmpl) (phba,
1657                                                              cmdiocbp, saveq);
1658                                                 spin_lock_irqsave(
1659                                                           &phba->hbalock,
1660                                                           iflag);
1661                                         } else
1662                                                 __lpfc_sli_release_iocbq(phba,
1663                                                                       cmdiocbp);
1664                                 }
1665                         } else if (type == LPFC_UNKNOWN_IOCB) {
1666                                 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
1667
1668                                         char adaptermsg[LPFC_MAX_ADPTMSG];
1669
1670                                         memset(adaptermsg, 0,
1671                                                LPFC_MAX_ADPTMSG);
1672                                         memcpy(&adaptermsg[0], (uint8_t *) irsp,
1673                                                MAX_MSG_DATA);
1674                                         dev_warn(&((phba->pcidev)->dev),
1675                                                  "lpfc%d: %s",
1676                                                  phba->brd_no, adaptermsg);
1677                                 } else {
1678                                         /* Unknown IOCB command */
1679                                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
1680                                                         "%d:0335 Unknown IOCB "
1681                                                         "command Data: x%x "
1682                                                         "x%x x%x x%x\n",
1683                                                         phba->brd_no,
1684                                                         irsp->ulpCommand,
1685                                                         irsp->ulpStatus,
1686                                                         irsp->ulpIoTag,
1687                                                         irsp->ulpContext);
1688                                 }
1689                         }
1690
1691                         if (free_saveq) {
1692                                 list_for_each_entry_safe(rspiocbp, next_iocb,
1693                                                          &saveq->list, list) {
1694                                         list_del(&rspiocbp->list);
1695                                         __lpfc_sli_release_iocbq(phba,
1696                                                                  rspiocbp);
1697                                 }
1698                                 __lpfc_sli_release_iocbq(phba, saveq);
1699                         }
1700                         rspiocbp = NULL;
1701                 }
1702
1703                 /*
1704                  * If the port response put pointer has not been updated, sync
1705                  * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
1706                  * response put pointer.
1707                  */
1708                 if (pring->rspidx == portRspPut) {
1709                         portRspPut = le32_to_cpu(pgp->rspPutInx);
1710                 }
1711         } /* while (pring->rspidx != portRspPut) */
1712
1713         if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
1714                 /* At least one response entry has been freed */
1715                 pring->stats.iocb_rsp_full++;
1716                 /* SET RxRE_RSP in Chip Att register */
1717                 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
1718                 writel(status, phba->CAregaddr);
1719                 readl(phba->CAregaddr); /* flush */
1720         }
1721         if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
1722                 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
1723                 pring->stats.iocb_cmd_empty++;
1724
1725                 /* Force update of the local copy of cmdGetInx */
1726                 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
1727                 lpfc_sli_resume_iocb(phba, pring);
1728
1729                 if ((pring->lpfc_sli_cmd_available))
1730                         (pring->lpfc_sli_cmd_available) (phba, pring);
1731
1732         }
1733
1734         spin_unlock_irqrestore(&phba->hbalock, iflag);
1735         return rc;
1736 }
1737
1738 void
1739 lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1740 {
1741         LIST_HEAD(completions);
1742         struct lpfc_iocbq *iocb, *next_iocb;
1743         IOCB_t *cmd = NULL;
1744
1745         if (pring->ringno == LPFC_ELS_RING) {
1746                 lpfc_fabric_abort_hba(phba);
1747         }
1748
1749         /* Error everything on txq and txcmplq
1750          * First do the txq.
1751          */
1752         spin_lock_irq(&phba->hbalock);
1753         list_splice_init(&pring->txq, &completions);
1754         pring->txq_cnt = 0;
1755
1756         /* Next issue ABTS for everything on the txcmplq */
1757         list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
1758                 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
1759
1760         spin_unlock_irq(&phba->hbalock);
1761
1762         while (!list_empty(&completions)) {
1763                 iocb = list_get_first(&completions, struct lpfc_iocbq, list);
1764                 cmd = &iocb->iocb;
1765                 list_del_init(&iocb->list);
1766
1767                 if (!iocb->iocb_cmpl)
1768                         lpfc_sli_release_iocbq(phba, iocb);
1769                 else {
1770                         cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
1771                         cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
1772                         (iocb->iocb_cmpl) (phba, iocb, iocb);
1773                 }
1774         }
1775 }
1776
1777 int
1778 lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
1779 {
1780         uint32_t status;
1781         int i = 0;
1782         int retval = 0;
1783
1784         /* Read the HBA Host Status Register */
1785         status = readl(phba->HSregaddr);
1786
1787         /*
1788          * Check status register every 100ms for 5 retries, then every
1789          * 500ms for 5, then every 2.5 sec for 5, then reset board and
1790          * every 2.5 sec for 4.
1791          * Break our of the loop if errors occurred during init.
1792          */
1793         while (((status & mask) != mask) &&
1794                !(status & HS_FFERM) &&
1795                i++ < 20) {
1796
1797                 if (i <= 5)
1798                         msleep(10);
1799                 else if (i <= 10)
1800                         msleep(500);
1801                 else
1802                         msleep(2500);
1803
1804                 if (i == 15) {
1805                                 /* Do post */
1806                         phba->pport->port_state = LPFC_VPORT_UNKNOWN;
1807                         lpfc_sli_brdrestart(phba);
1808                 }
1809                 /* Read the HBA Host Status Register */
1810                 status = readl(phba->HSregaddr);
1811         }
1812
1813         /* Check to see if any errors occurred during init */
1814         if ((status & HS_FFERM) || (i >= 20)) {
1815                 phba->link_state = LPFC_HBA_ERROR;
1816                 retval = 1;
1817         }
1818
1819         return retval;
1820 }
1821
1822 #define BARRIER_TEST_PATTERN (0xdeadbeef)
1823
1824 void lpfc_reset_barrier(struct lpfc_hba *phba)
1825 {
1826         uint32_t __iomem *resp_buf;
1827         uint32_t __iomem *mbox_buf;
1828         volatile uint32_t mbox;
1829         uint32_t hc_copy;
1830         int  i;
1831         uint8_t hdrtype;
1832
1833         pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
1834         if (hdrtype != 0x80 ||
1835             (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
1836              FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
1837                 return;
1838
1839         /*
1840          * Tell the other part of the chip to suspend temporarily all
1841          * its DMA activity.
1842          */
1843         resp_buf = phba->MBslimaddr;
1844
1845         /* Disable the error attention */
1846         hc_copy = readl(phba->HCregaddr);
1847         writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
1848         readl(phba->HCregaddr); /* flush */
1849         phba->link_flag |= LS_IGNORE_ERATT;
1850
1851         if (readl(phba->HAregaddr) & HA_ERATT) {
1852                 /* Clear Chip error bit */
1853                 writel(HA_ERATT, phba->HAregaddr);
1854                 phba->pport->stopped = 1;
1855         }
1856
1857         mbox = 0;
1858         ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
1859         ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
1860
1861         writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
1862         mbox_buf = phba->MBslimaddr;
1863         writel(mbox, mbox_buf);
1864
1865         for (i = 0;
1866              readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN) && i < 50; i++)
1867                 mdelay(1);
1868
1869         if (readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN)) {
1870                 if (phba->sli.sli_flag & LPFC_SLI2_ACTIVE ||
1871                     phba->pport->stopped)
1872                         goto restore_hc;
1873                 else
1874                         goto clear_errat;
1875         }
1876
1877         ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
1878         for (i = 0; readl(resp_buf) != mbox &&  i < 500; i++)
1879                 mdelay(1);
1880
1881 clear_errat:
1882
1883         while (!(readl(phba->HAregaddr) & HA_ERATT) && ++i < 500)
1884                 mdelay(1);
1885
1886         if (readl(phba->HAregaddr) & HA_ERATT) {
1887                 writel(HA_ERATT, phba->HAregaddr);
1888                 phba->pport->stopped = 1;
1889         }
1890
1891 restore_hc:
1892         phba->link_flag &= ~LS_IGNORE_ERATT;
1893         writel(hc_copy, phba->HCregaddr);
1894         readl(phba->HCregaddr); /* flush */
1895 }
1896
1897 int
1898 lpfc_sli_brdkill(struct lpfc_hba *phba)
1899 {
1900         struct lpfc_sli *psli;
1901         LPFC_MBOXQ_t *pmb;
1902         uint32_t status;
1903         uint32_t ha_copy;
1904         int retval;
1905         int i = 0;
1906
1907         psli = &phba->sli;
1908
1909         /* Kill HBA */
1910         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
1911                         "%d:0329 Kill HBA Data: x%x x%x\n",
1912                         phba->brd_no, phba->pport->port_state, psli->sli_flag);
1913
1914         if ((pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
1915                                                   GFP_KERNEL)) == 0)
1916                 return 1;
1917
1918         /* Disable the error attention */
1919         spin_lock_irq(&phba->hbalock);
1920         status = readl(phba->HCregaddr);
1921         status &= ~HC_ERINT_ENA;
1922         writel(status, phba->HCregaddr);
1923         readl(phba->HCregaddr); /* flush */
1924         phba->link_flag |= LS_IGNORE_ERATT;
1925         spin_unlock_irq(&phba->hbalock);
1926
1927         lpfc_kill_board(phba, pmb);
1928         pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
1929         retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
1930
1931         if (retval != MBX_SUCCESS) {
1932                 if (retval != MBX_BUSY)
1933                         mempool_free(pmb, phba->mbox_mem_pool);
1934                 spin_lock_irq(&phba->hbalock);
1935                 phba->link_flag &= ~LS_IGNORE_ERATT;
1936                 spin_unlock_irq(&phba->hbalock);
1937                 return 1;
1938         }
1939
1940         psli->sli_flag &= ~LPFC_SLI2_ACTIVE;
1941
1942         mempool_free(pmb, phba->mbox_mem_pool);
1943
1944         /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
1945          * attention every 100ms for 3 seconds. If we don't get ERATT after
1946          * 3 seconds we still set HBA_ERROR state because the status of the
1947          * board is now undefined.
1948          */
1949         ha_copy = readl(phba->HAregaddr);
1950
1951         while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
1952                 mdelay(100);
1953                 ha_copy = readl(phba->HAregaddr);
1954         }
1955
1956         del_timer_sync(&psli->mbox_tmo);
1957         if (ha_copy & HA_ERATT) {
1958                 writel(HA_ERATT, phba->HAregaddr);
1959                 phba->pport->stopped = 1;
1960         }
1961         spin_lock_irq(&phba->hbalock);
1962         psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
1963         phba->link_flag &= ~LS_IGNORE_ERATT;
1964         spin_unlock_irq(&phba->hbalock);
1965
1966         psli->mbox_active = NULL;
1967         lpfc_hba_down_post(phba);
1968         phba->link_state = LPFC_HBA_ERROR;
1969
1970         return ha_copy & HA_ERATT ? 0 : 1;
1971 }
1972
1973 int
1974 lpfc_sli_brdreset(struct lpfc_hba *phba)
1975 {
1976         struct lpfc_sli *psli;
1977         struct lpfc_sli_ring *pring;
1978         uint16_t cfg_value;
1979         int i;
1980
1981         psli = &phba->sli;
1982
1983         /* Reset HBA */
1984         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
1985                         "%d:0325 Reset HBA Data: x%x x%x\n", phba->brd_no,
1986                         phba->pport->port_state, psli->sli_flag);
1987
1988         /* perform board reset */
1989         phba->fc_eventTag = 0;
1990         phba->pport->fc_myDID = 0;
1991         phba->pport->fc_prevDID = 0;
1992
1993         /* Turn off parity checking and serr during the physical reset */
1994         pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
1995         pci_write_config_word(phba->pcidev, PCI_COMMAND,
1996                               (cfg_value &
1997                                ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
1998
1999         psli->sli_flag &= ~(LPFC_SLI2_ACTIVE | LPFC_PROCESS_LA);
2000         /* Now toggle INITFF bit in the Host Control Register */
2001         writel(HC_INITFF, phba->HCregaddr);
2002         mdelay(1);
2003         readl(phba->HCregaddr); /* flush */
2004         writel(0, phba->HCregaddr);
2005         readl(phba->HCregaddr); /* flush */
2006
2007         /* Restore PCI cmd register */
2008         pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
2009
2010         /* Initialize relevant SLI info */
2011         for (i = 0; i < psli->num_rings; i++) {
2012                 pring = &psli->ring[i];
2013                 pring->flag = 0;
2014                 pring->rspidx = 0;
2015                 pring->next_cmdidx  = 0;
2016                 pring->local_getidx = 0;
2017                 pring->cmdidx = 0;
2018                 pring->missbufcnt = 0;
2019         }
2020
2021         phba->link_state = LPFC_WARM_START;
2022         return 0;
2023 }
2024
2025 int
2026 lpfc_sli_brdrestart(struct lpfc_hba *phba)
2027 {
2028         MAILBOX_t *mb;
2029         struct lpfc_sli *psli;
2030         uint16_t skip_post;
2031         volatile uint32_t word0;
2032         void __iomem *to_slim;
2033
2034         spin_lock_irq(&phba->hbalock);
2035
2036         psli = &phba->sli;
2037
2038         /* Restart HBA */
2039         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
2040                         "%d:0337 Restart HBA Data: x%x x%x\n", phba->brd_no,
2041                         phba->pport->port_state, psli->sli_flag);
2042
2043         word0 = 0;
2044         mb = (MAILBOX_t *) &word0;
2045         mb->mbxCommand = MBX_RESTART;
2046         mb->mbxHc = 1;
2047
2048         lpfc_reset_barrier(phba);
2049
2050         to_slim = phba->MBslimaddr;
2051         writel(*(uint32_t *) mb, to_slim);
2052         readl(to_slim); /* flush */
2053
2054         /* Only skip post after fc_ffinit is completed */
2055         if (phba->pport->port_state) {
2056                 skip_post = 1;
2057                 word0 = 1;      /* This is really setting up word1 */
2058         } else {
2059                 skip_post = 0;
2060                 word0 = 0;      /* This is really setting up word1 */
2061         }
2062         to_slim = phba->MBslimaddr + sizeof (uint32_t);
2063         writel(*(uint32_t *) mb, to_slim);
2064         readl(to_slim); /* flush */
2065
2066         lpfc_sli_brdreset(phba);
2067         phba->pport->stopped = 0;
2068         phba->link_state = LPFC_INIT_START;
2069
2070         spin_unlock_irq(&phba->hbalock);
2071
2072         memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
2073         psli->stats_start = get_seconds();
2074
2075         if (skip_post)
2076                 mdelay(100);
2077         else
2078                 mdelay(2000);
2079
2080         lpfc_hba_down_post(phba);
2081
2082         return 0;
2083 }
2084
2085 static int
2086 lpfc_sli_chipset_init(struct lpfc_hba *phba)
2087 {
2088         uint32_t status, i = 0;
2089
2090         /* Read the HBA Host Status Register */
2091         status = readl(phba->HSregaddr);
2092
2093         /* Check status register to see what current state is */
2094         i = 0;
2095         while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
2096
2097                 /* Check every 100ms for 5 retries, then every 500ms for 5, then
2098                  * every 2.5 sec for 5, then reset board and every 2.5 sec for
2099                  * 4.
2100                  */
2101                 if (i++ >= 20) {
2102                         /* Adapter failed to init, timeout, status reg
2103                            <status> */
2104                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2105                                         "%d:0436 Adapter failed to init, "
2106                                         "timeout, status reg x%x\n",
2107                                         phba->brd_no, status);
2108                         phba->link_state = LPFC_HBA_ERROR;
2109                         return -ETIMEDOUT;
2110                 }
2111
2112                 /* Check to see if any errors occurred during init */
2113                 if (status & HS_FFERM) {
2114                         /* ERROR: During chipset initialization */
2115                         /* Adapter failed to init, chipset, status reg
2116                            <status> */
2117                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2118                                         "%d:0437 Adapter failed to init, "
2119                                         "chipset, status reg x%x\n",
2120                                         phba->brd_no,
2121                                         status);
2122                         phba->link_state = LPFC_HBA_ERROR;
2123                         return -EIO;
2124                 }
2125
2126                 if (i <= 5) {
2127                         msleep(10);
2128                 } else if (i <= 10) {
2129                         msleep(500);
2130                 } else {
2131                         msleep(2500);
2132                 }
2133
2134                 if (i == 15) {
2135                                 /* Do post */
2136                         phba->pport->port_state = LPFC_VPORT_UNKNOWN;
2137                         lpfc_sli_brdrestart(phba);
2138                 }
2139                 /* Read the HBA Host Status Register */
2140                 status = readl(phba->HSregaddr);
2141         }
2142
2143         /* Check to see if any errors occurred during init */
2144         if (status & HS_FFERM) {
2145                 /* ERROR: During chipset initialization */
2146                 /* Adapter failed to init, chipset, status reg <status> */
2147                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2148                                 "%d:0438 Adapter failed to init, chipset, "
2149                                 "status reg x%x\n",
2150                                 phba->brd_no,
2151                                 status);
2152                 phba->link_state = LPFC_HBA_ERROR;
2153                 return -EIO;
2154         }
2155
2156         /* Clear all interrupt enable conditions */
2157         writel(0, phba->HCregaddr);
2158         readl(phba->HCregaddr); /* flush */
2159
2160         /* setup host attn register */
2161         writel(0xffffffff, phba->HAregaddr);
2162         readl(phba->HAregaddr); /* flush */
2163         return 0;
2164 }
2165
2166 static int
2167 lpfc_sli_hbq_count(void)
2168 {
2169         return ARRAY_SIZE(lpfc_hbq_defs);
2170 }
2171
2172 static int
2173 lpfc_sli_hbq_entry_count(void)
2174 {
2175         int  hbq_count = lpfc_sli_hbq_count();
2176         int  count = 0;
2177         int  i;
2178
2179         for (i = 0; i < hbq_count; ++i)
2180                 count += lpfc_hbq_defs[i]->entry_count;
2181         return count;
2182 }
2183
2184 int
2185 lpfc_sli_hbq_size(void)
2186 {
2187         return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
2188 }
2189
2190 static int
2191 lpfc_sli_hbq_setup(struct lpfc_hba *phba)
2192 {
2193         int  hbq_count = lpfc_sli_hbq_count();
2194         LPFC_MBOXQ_t *pmb;
2195         MAILBOX_t *pmbox;
2196         uint32_t hbqno;
2197         uint32_t hbq_entry_index;
2198
2199                                 /* Get a Mailbox buffer to setup mailbox
2200                                  * commands for HBA initialization
2201                                  */
2202         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2203
2204         if (!pmb)
2205                 return -ENOMEM;
2206
2207         pmbox = &pmb->mb;
2208
2209         /* Initialize the struct lpfc_sli_hbq structure for each hbq */
2210         phba->link_state = LPFC_INIT_MBX_CMDS;
2211
2212         hbq_entry_index = 0;
2213         for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
2214                 phba->hbqs[hbqno].next_hbqPutIdx = 0;
2215                 phba->hbqs[hbqno].hbqPutIdx      = 0;
2216                 phba->hbqs[hbqno].local_hbqGetIdx   = 0;
2217                 phba->hbqs[hbqno].entry_count =
2218                         lpfc_hbq_defs[hbqno]->entry_count;
2219                 lpfc_config_hbq(phba, lpfc_hbq_defs[hbqno], hbq_entry_index,
2220                                 pmb);
2221                 hbq_entry_index += phba->hbqs[hbqno].entry_count;
2222
2223                 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
2224                         /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
2225                            mbxStatus <status>, ring <num> */
2226
2227                         lpfc_printf_log(phba, KERN_ERR,
2228                                         LOG_SLI | LOG_VPORT,
2229                                         "%d:1805 Adapter failed to init. "
2230                                         "Data: x%x x%x x%x\n",
2231                                         phba->brd_no, pmbox->mbxCommand,
2232                                         pmbox->mbxStatus, hbqno);
2233
2234                         phba->link_state = LPFC_HBA_ERROR;
2235                         mempool_free(pmb, phba->mbox_mem_pool);
2236                         return ENXIO;
2237                 }
2238         }
2239         phba->hbq_count = hbq_count;
2240
2241         mempool_free(pmb, phba->mbox_mem_pool);
2242
2243         /* Initially populate or replenish the HBQs */
2244         for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
2245                 if (lpfc_sli_hbqbuf_init_hbqs(phba, hbqno))
2246                         return -ENOMEM;
2247         }
2248         return 0;
2249 }
2250
2251 static int
2252 lpfc_do_config_port(struct lpfc_hba *phba, int sli_mode)
2253 {
2254         LPFC_MBOXQ_t *pmb;
2255         uint32_t resetcount = 0, rc = 0, done = 0;
2256
2257         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2258         if (!pmb) {
2259                 phba->link_state = LPFC_HBA_ERROR;
2260                 return -ENOMEM;
2261         }
2262
2263         phba->sli_rev = sli_mode;
2264         while (resetcount < 2 && !done) {
2265                 spin_lock_irq(&phba->hbalock);
2266                 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
2267                 spin_unlock_irq(&phba->hbalock);
2268                 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
2269                 lpfc_sli_brdrestart(phba);
2270                 msleep(2500);
2271                 rc = lpfc_sli_chipset_init(phba);
2272                 if (rc)
2273                         break;
2274
2275                 spin_lock_irq(&phba->hbalock);
2276                 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
2277                 spin_unlock_irq(&phba->hbalock);
2278                 resetcount++;
2279
2280                 /* Call pre CONFIG_PORT mailbox command initialization.  A
2281                  * value of 0 means the call was successful.  Any other
2282                  * nonzero value is a failure, but if ERESTART is returned,
2283                  * the driver may reset the HBA and try again.
2284                  */
2285                 rc = lpfc_config_port_prep(phba);
2286                 if (rc == -ERESTART) {
2287                         phba->link_state = LPFC_LINK_UNKNOWN;
2288                         continue;
2289                 } else if (rc) {
2290                         break;
2291                 }
2292
2293                 phba->link_state = LPFC_INIT_MBX_CMDS;
2294                 lpfc_config_port(phba, pmb);
2295                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
2296                 if (rc != MBX_SUCCESS) {
2297                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2298                                 "%d:0442 Adapter failed to init, mbxCmd x%x "
2299                                 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
2300                                 phba->brd_no, pmb->mb.mbxCommand,
2301                                 pmb->mb.mbxStatus, 0);
2302                         spin_lock_irq(&phba->hbalock);
2303                         phba->sli.sli_flag &= ~LPFC_SLI2_ACTIVE;
2304                         spin_unlock_irq(&phba->hbalock);
2305                         rc = -ENXIO;
2306                 } else {
2307                         done = 1;
2308                         phba->max_vpi = (phba->max_vpi &&
2309                                          pmb->mb.un.varCfgPort.gmv) != 0
2310                                 ? pmb->mb.un.varCfgPort.max_vpi
2311                                 : 0;
2312                 }
2313         }
2314
2315         if (!done) {
2316                 rc = -EINVAL;
2317                 goto do_prep_failed;
2318         }
2319
2320         if ((pmb->mb.un.varCfgPort.sli_mode == 3) &&
2321                 (!pmb->mb.un.varCfgPort.cMA)) {
2322                 rc = -ENXIO;
2323                 goto do_prep_failed;
2324         }
2325         return rc;
2326
2327 do_prep_failed:
2328         mempool_free(pmb, phba->mbox_mem_pool);
2329         return rc;
2330 }
2331
2332 int
2333 lpfc_sli_hba_setup(struct lpfc_hba *phba)
2334 {
2335         uint32_t rc;
2336         int  mode = 3;
2337
2338         switch (lpfc_sli_mode) {
2339         case 2:
2340                 if (phba->cfg_npiv_enable) {
2341                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
2342                                 "%d:1824 NPIV enabled: Override lpfc_sli_mode "
2343                                 "parameter (%d) to auto (0).\n",
2344                                 phba->brd_no, lpfc_sli_mode);
2345                         break;
2346                 }
2347                 mode = 2;
2348                 break;
2349         case 0:
2350         case 3:
2351                 break;
2352         default:
2353                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
2354                                 "%d:1819 Unrecognized lpfc_sli_mode "
2355                                 "parameter: %d.\n",
2356                                 phba->brd_no, lpfc_sli_mode);
2357
2358                 break;
2359         }
2360
2361         rc = lpfc_do_config_port(phba, mode);
2362         if (rc && lpfc_sli_mode == 3)
2363                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
2364                                 "%d:1820 Unable to select SLI-3.  "
2365                                 "Not supported by adapter.\n",
2366                                 phba->brd_no);
2367         if (rc && mode != 2)
2368                 rc = lpfc_do_config_port(phba, 2);
2369         if (rc)
2370                 goto lpfc_sli_hba_setup_error;
2371
2372         if (phba->sli_rev == 3) {
2373                 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
2374                 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
2375                 phba->sli3_options |= LPFC_SLI3_ENABLED;
2376                 phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
2377
2378         } else {
2379                 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
2380                 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
2381                 phba->sli3_options = 0;
2382         }
2383
2384         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
2385                         "%d:0444 Firmware in SLI %x mode. Max_vpi %d\n",
2386                         phba->brd_no, phba->sli_rev, phba->max_vpi);
2387         rc = lpfc_sli_ring_map(phba);
2388
2389         if (rc)
2390                 goto lpfc_sli_hba_setup_error;
2391
2392                                 /* Init HBQs */
2393
2394         if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
2395                 rc = lpfc_sli_hbq_setup(phba);
2396                 if (rc)
2397                         goto lpfc_sli_hba_setup_error;
2398         }
2399
2400         phba->sli.sli_flag |= LPFC_PROCESS_LA;
2401
2402         rc = lpfc_config_port_post(phba);
2403         if (rc)
2404                 goto lpfc_sli_hba_setup_error;
2405
2406         return rc;
2407
2408 lpfc_sli_hba_setup_error:
2409         phba->link_state = LPFC_HBA_ERROR;
2410         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
2411                         "%d:0445 Firmware initialization failed\n",
2412                         phba->brd_no);
2413         return rc;
2414 }
2415
2416 /*! lpfc_mbox_timeout
2417  *
2418  * \pre
2419  * \post
2420  * \param hba Pointer to per struct lpfc_hba structure
2421  * \param l1  Pointer to the driver's mailbox queue.
2422  * \return
2423  *   void
2424  *
2425  * \b Description:
2426  *
2427  * This routine handles mailbox timeout events at timer interrupt context.
2428  */
2429 void
2430 lpfc_mbox_timeout(unsigned long ptr)
2431 {
2432         struct lpfc_hba  *phba = (struct lpfc_hba *) ptr;
2433         unsigned long iflag;
2434         uint32_t tmo_posted;
2435
2436         spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
2437         tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
2438         if (!tmo_posted)
2439                 phba->pport->work_port_events |= WORKER_MBOX_TMO;
2440         spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
2441
2442         if (!tmo_posted) {
2443                 spin_lock_irqsave(&phba->hbalock, iflag);
2444                 if (phba->work_wait)
2445                         lpfc_worker_wake_up(phba);
2446                 spin_unlock_irqrestore(&phba->hbalock, iflag);
2447         }
2448 }
2449
2450 void
2451 lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
2452 {
2453         LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
2454         MAILBOX_t *mb = &pmbox->mb;
2455         struct lpfc_sli *psli = &phba->sli;
2456         struct lpfc_sli_ring *pring;
2457
2458         if (!(phba->pport->work_port_events & WORKER_MBOX_TMO)) {
2459                 return;
2460         }
2461
2462         /* Mbox cmd <mbxCommand> timeout */
2463         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
2464                         "%d:0310 Mailbox command x%x timeout Data: x%x x%x "
2465                         "x%p\n",
2466                         phba->brd_no,
2467                         mb->mbxCommand,
2468                         phba->pport->port_state,
2469                         phba->sli.sli_flag,
2470                         phba->sli.mbox_active);
2471
2472         /* Setting state unknown so lpfc_sli_abort_iocb_ring
2473          * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
2474          * it to fail all oustanding SCSI IO.
2475          */
2476         spin_lock_irq(&phba->pport->work_port_lock);
2477         phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
2478         spin_unlock_irq(&phba->pport->work_port_lock);
2479         spin_lock_irq(&phba->hbalock);
2480         phba->link_state = LPFC_LINK_UNKNOWN;
2481         phba->pport->fc_flag |= FC_ESTABLISH_LINK;
2482         psli->sli_flag &= ~LPFC_SLI2_ACTIVE;
2483         spin_unlock_irq(&phba->hbalock);
2484
2485         pring = &psli->ring[psli->fcp_ring];
2486         lpfc_sli_abort_iocb_ring(phba, pring);
2487
2488         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
2489                         "%d:0316 Resetting board due to mailbox timeout\n",
2490                         phba->brd_no);
2491         /*
2492          * lpfc_offline calls lpfc_sli_hba_down which will clean up
2493          * on oustanding mailbox commands.
2494          */
2495         lpfc_offline_prep(phba);
2496         lpfc_offline(phba);
2497         lpfc_sli_brdrestart(phba);
2498         if (lpfc_online(phba) == 0)             /* Initialize the HBA */
2499                 mod_timer(&phba->fc_estabtmo, jiffies + HZ * 60);
2500         lpfc_unblock_mgmt_io(phba);
2501         return;
2502 }
2503
2504 int
2505 lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
2506 {
2507         MAILBOX_t *mb;
2508         struct lpfc_sli *psli = &phba->sli;
2509         uint32_t status, evtctr;
2510         uint32_t ha_copy;
2511         int i;
2512         unsigned long drvr_flag = 0;
2513         volatile uint32_t word0, ldata;
2514         void __iomem *to_slim;
2515
2516         if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
2517                 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
2518                 if(!pmbox->vport) {
2519                         lpfc_printf_log(phba, KERN_ERR,
2520                                         LOG_MBOX | LOG_VPORT,
2521                                         "%d:1806 Mbox x%x failed. No vport\n",
2522                                         phba->brd_no,
2523                                         pmbox->mb.mbxCommand);
2524                         dump_stack();
2525                         return MBXERR_ERROR;
2526                 }
2527         }
2528
2529
2530         /* If the PCI channel is in offline state, do not post mbox. */
2531         if (unlikely(pci_channel_offline(phba->pcidev)))
2532                 return MBX_NOT_FINISHED;
2533
2534         spin_lock_irqsave(&phba->hbalock, drvr_flag);
2535         psli = &phba->sli;
2536
2537
2538         mb = &pmbox->mb;
2539         status = MBX_SUCCESS;
2540
2541         if (phba->link_state == LPFC_HBA_ERROR) {
2542                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
2543
2544                 /* Mbox command <mbxCommand> cannot issue */
2545                 LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag)
2546                 return MBX_NOT_FINISHED;
2547         }
2548
2549         if (mb->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT &&
2550             !(readl(phba->HCregaddr) & HC_MBINT_ENA)) {
2551                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
2552                 LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag)
2553                 return MBX_NOT_FINISHED;
2554         }
2555
2556         if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
2557                 /* Polling for a mbox command when another one is already active
2558                  * is not allowed in SLI. Also, the driver must have established
2559                  * SLI2 mode to queue and process multiple mbox commands.
2560                  */
2561
2562                 if (flag & MBX_POLL) {
2563                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
2564
2565                         /* Mbox command <mbxCommand> cannot issue */
2566                         LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag);
2567                         return MBX_NOT_FINISHED;
2568                 }
2569
2570                 if (!(psli->sli_flag & LPFC_SLI2_ACTIVE)) {
2571                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
2572                         /* Mbox command <mbxCommand> cannot issue */
2573                         LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag);
2574                         return MBX_NOT_FINISHED;
2575                 }
2576
2577                 /* Handle STOP IOCB processing flag. This is only meaningful
2578                  * if we are not polling for mbox completion.
2579                  */
2580                 if (flag & MBX_STOP_IOCB) {
2581                         flag &= ~MBX_STOP_IOCB;
2582                         /* Now flag each ring */
2583                         for (i = 0; i < psli->num_rings; i++) {
2584                                 /* If the ring is active, flag it */
2585                                 if (psli->ring[i].cmdringaddr) {
2586                                         psli->ring[i].flag |=
2587                                             LPFC_STOP_IOCB_MBX;
2588                                 }
2589                         }
2590                 }
2591
2592                 /* Another mailbox command is still being processed, queue this
2593                  * command to be processed later.
2594                  */
2595                 lpfc_mbox_put(phba, pmbox);
2596
2597                 /* Mbox cmd issue - BUSY */
2598                 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
2599                                 "%d (%d):0308 Mbox cmd issue - BUSY Data: "
2600                                 "x%x x%x x%x x%x\n",
2601                                 phba->brd_no,
2602                                 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
2603                                 mb->mbxCommand, phba->pport->port_state,
2604                                 psli->sli_flag, flag);
2605
2606                 psli->slistat.mbox_busy++;
2607                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
2608
2609                 if (pmbox->vport) {
2610                         lpfc_debugfs_disc_trc(pmbox->vport,
2611                                 LPFC_DISC_TRC_MBOX_VPORT,
2612                                 "MBOX Bsy vport:  cmd:x%x mb:x%x x%x",
2613                                 (uint32_t)mb->mbxCommand,
2614                                 mb->un.varWords[0], mb->un.varWords[1]);
2615                 }
2616                 else {
2617                         lpfc_debugfs_disc_trc(phba->pport,
2618                                 LPFC_DISC_TRC_MBOX,
2619                                 "MBOX Bsy:        cmd:x%x mb:x%x x%x",
2620                                 (uint32_t)mb->mbxCommand,
2621                                 mb->un.varWords[0], mb->un.varWords[1]);
2622                 }
2623
2624                 return MBX_BUSY;
2625         }
2626
2627         /* Handle STOP IOCB processing flag. This is only meaningful
2628          * if we are not polling for mbox completion.
2629          */
2630         if (flag & MBX_STOP_IOCB) {
2631                 flag &= ~MBX_STOP_IOCB;
2632                 if (flag == MBX_NOWAIT) {
2633                         /* Now flag each ring */
2634                         for (i = 0; i < psli->num_rings; i++) {
2635                                 /* If the ring is active, flag it */
2636                                 if (psli->ring[i].cmdringaddr) {
2637                                         psli->ring[i].flag |=
2638                                             LPFC_STOP_IOCB_MBX;
2639                                 }
2640                         }
2641                 }
2642         }
2643
2644         psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
2645
2646         /* If we are not polling, we MUST be in SLI2 mode */
2647         if (flag != MBX_POLL) {
2648                 if (!(psli->sli_flag & LPFC_SLI2_ACTIVE) &&
2649                     (mb->mbxCommand != MBX_KILL_BOARD)) {
2650                         psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
2651                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
2652                         /* Mbox command <mbxCommand> cannot issue */
2653                         LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag);
2654                         return MBX_NOT_FINISHED;
2655                 }
2656                 /* timeout active mbox command */
2657                 mod_timer(&psli->mbox_tmo, (jiffies +
2658                                (HZ * lpfc_mbox_tmo_val(phba, mb->mbxCommand))));
2659         }
2660
2661         /* Mailbox cmd <cmd> issue */
2662         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
2663                         "%d (%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
2664                         "x%x\n",
2665                         phba->brd_no, pmbox->vport ? pmbox->vport->vpi : 0,
2666                         mb->mbxCommand, phba->pport->port_state,
2667                         psli->sli_flag, flag);
2668
2669         if (mb->mbxCommand != MBX_HEARTBEAT) {
2670                 if (pmbox->vport) {
2671                         lpfc_debugfs_disc_trc(pmbox->vport,
2672                                 LPFC_DISC_TRC_MBOX_VPORT,
2673                                 "MBOX Send vport: cmd:x%x mb:x%x x%x",
2674                                 (uint32_t)mb->mbxCommand,
2675                                 mb->un.varWords[0], mb->un.varWords[1]);
2676                 }
2677                 else {
2678                         lpfc_debugfs_disc_trc(phba->pport,
2679                                 LPFC_DISC_TRC_MBOX,
2680                                 "MBOX Send:       cmd:x%x mb:x%x x%x",
2681                                 (uint32_t)mb->mbxCommand,
2682                                 mb->un.varWords[0], mb->un.varWords[1]);
2683                 }
2684         }
2685
2686         psli->slistat.mbox_cmd++;
2687         evtctr = psli->slistat.mbox_event;
2688
2689         /* next set own bit for the adapter and copy over command word */
2690         mb->mbxOwner = OWN_CHIP;
2691
2692         if (psli->sli_flag & LPFC_SLI2_ACTIVE) {
2693                 /* First copy command data to host SLIM area */
2694                 lpfc_sli_pcimem_bcopy(mb, &phba->slim2p->mbx, MAILBOX_CMD_SIZE);
2695         } else {
2696                 if (mb->mbxCommand == MBX_CONFIG_PORT) {
2697                         /* copy command data into host mbox for cmpl */
2698                         lpfc_sli_pcimem_bcopy(mb, &phba->slim2p->mbx,
2699                                               MAILBOX_CMD_SIZE);
2700                 }
2701
2702                 /* First copy mbox command data to HBA SLIM, skip past first
2703                    word */
2704                 to_slim = phba->MBslimaddr + sizeof (uint32_t);
2705                 lpfc_memcpy_to_slim(to_slim, &mb->un.varWords[0],
2706                             MAILBOX_CMD_SIZE - sizeof (uint32_t));
2707
2708                 /* Next copy over first word, with mbxOwner set */
2709                 ldata = *((volatile uint32_t *)mb);
2710                 to_slim = phba->MBslimaddr;
2711                 writel(ldata, to_slim);
2712                 readl(to_slim); /* flush */
2713
2714                 if (mb->mbxCommand == MBX_CONFIG_PORT) {
2715                         /* switch over to host mailbox */
2716                         psli->sli_flag |= LPFC_SLI2_ACTIVE;
2717                 }
2718         }
2719
2720         wmb();
2721         /* interrupt board to doit right away */
2722         writel(CA_MBATT, phba->CAregaddr);
2723         readl(phba->CAregaddr); /* flush */
2724
2725         switch (flag) {
2726         case MBX_NOWAIT:
2727                 /* Don't wait for it to finish, just return */
2728                 psli->mbox_active = pmbox;
2729                 break;
2730
2731         case MBX_POLL:
2732                 psli->mbox_active = NULL;
2733                 if (psli->sli_flag & LPFC_SLI2_ACTIVE) {
2734                         /* First read mbox status word */
2735                         word0 = *((volatile uint32_t *)&phba->slim2p->mbx);
2736                         word0 = le32_to_cpu(word0);
2737                 } else {
2738                         /* First read mbox status word */
2739                         word0 = readl(phba->MBslimaddr);
2740                 }
2741
2742                 /* Read the HBA Host Attention Register */
2743                 ha_copy = readl(phba->HAregaddr);
2744
2745                 i = lpfc_mbox_tmo_val(phba, mb->mbxCommand);
2746                 i *= 1000; /* Convert to ms */
2747
2748                 /* Wait for command to complete */
2749                 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
2750                        (!(ha_copy & HA_MBATT) &&
2751                         (phba->link_state > LPFC_WARM_START))) {
2752                         if (i-- <= 0) {
2753                                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
2754                                 spin_unlock_irqrestore(&phba->hbalock,
2755                                                        drvr_flag);
2756                                 return MBX_NOT_FINISHED;
2757                         }
2758
2759                         /* Check if we took a mbox interrupt while we were
2760                            polling */
2761                         if (((word0 & OWN_CHIP) != OWN_CHIP)
2762                             && (evtctr != psli->slistat.mbox_event))
2763                                 break;
2764
2765                         spin_unlock_irqrestore(&phba->hbalock,
2766                                                drvr_flag);
2767
2768                         msleep(1);
2769
2770                         spin_lock_irqsave(&phba->hbalock, drvr_flag);
2771
2772                         if (psli->sli_flag & LPFC_SLI2_ACTIVE) {
2773                                 /* First copy command data */
2774                                 word0 = *((volatile uint32_t *)
2775                                                 &phba->slim2p->mbx);
2776                                 word0 = le32_to_cpu(word0);
2777                                 if (mb->mbxCommand == MBX_CONFIG_PORT) {
2778                                         MAILBOX_t *slimmb;
2779                                         volatile uint32_t slimword0;
2780                                         /* Check real SLIM for any errors */
2781                                         slimword0 = readl(phba->MBslimaddr);
2782                                         slimmb = (MAILBOX_t *) & slimword0;
2783                                         if (((slimword0 & OWN_CHIP) != OWN_CHIP)
2784                                             && slimmb->mbxStatus) {
2785                                                 psli->sli_flag &=
2786                                                     ~LPFC_SLI2_ACTIVE;
2787                                                 word0 = slimword0;
2788                                         }
2789                                 }
2790                         } else {
2791                                 /* First copy command data */
2792                                 word0 = readl(phba->MBslimaddr);
2793                         }
2794                         /* Read the HBA Host Attention Register */
2795                         ha_copy = readl(phba->HAregaddr);
2796                 }
2797
2798                 if (psli->sli_flag & LPFC_SLI2_ACTIVE) {
2799                         /* copy results back to user */
2800                         lpfc_sli_pcimem_bcopy(&phba->slim2p->mbx, mb,
2801                                               MAILBOX_CMD_SIZE);
2802                 } else {
2803                         /* First copy command data */
2804                         lpfc_memcpy_from_slim(mb, phba->MBslimaddr,
2805                                                         MAILBOX_CMD_SIZE);
2806                         if ((mb->mbxCommand == MBX_DUMP_MEMORY) &&
2807                                 pmbox->context2) {
2808                                 lpfc_memcpy_from_slim((void *)pmbox->context2,
2809                                       phba->MBslimaddr + DMP_RSP_OFFSET,
2810                                                       mb->un.varDmp.word_cnt);
2811                         }
2812                 }
2813
2814                 writel(HA_MBATT, phba->HAregaddr);
2815                 readl(phba->HAregaddr); /* flush */
2816
2817                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
2818                 status = mb->mbxStatus;
2819         }
2820
2821         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
2822         return status;
2823 }
2824
2825 /*
2826  * Caller needs to hold lock.
2827  */
2828 static void
2829 __lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2830                     struct lpfc_iocbq *piocb)
2831 {
2832         /* Insert the caller's iocb in the txq tail for later processing. */
2833         list_add_tail(&piocb->list, &pring->txq);
2834         pring->txq_cnt++;
2835 }
2836
2837 static struct lpfc_iocbq *
2838 lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2839                    struct lpfc_iocbq **piocb)
2840 {
2841         struct lpfc_iocbq * nextiocb;
2842
2843         nextiocb = lpfc_sli_ringtx_get(phba, pring);
2844         if (!nextiocb) {
2845                 nextiocb = *piocb;
2846                 *piocb = NULL;
2847         }
2848
2849         return nextiocb;
2850 }
2851
2852 /*
2853  * Lockless version of lpfc_sli_issue_iocb.
2854  */
2855 int
2856 __lpfc_sli_issue_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2857                     struct lpfc_iocbq *piocb, uint32_t flag)
2858 {
2859         struct lpfc_iocbq *nextiocb;
2860         IOCB_t *iocb;
2861
2862         if (piocb->iocb_cmpl && (!piocb->vport) &&
2863            (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
2864            (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
2865                 lpfc_printf_log(phba, KERN_ERR,
2866                                 LOG_SLI | LOG_VPORT,
2867                                 "%d:1807 IOCB x%x failed. No vport\n",
2868                                 phba->brd_no,
2869                                 piocb->iocb.ulpCommand);
2870                 dump_stack();
2871                 return IOCB_ERROR;
2872         }
2873
2874
2875         /* If the PCI channel is in offline state, do not post iocbs. */
2876         if (unlikely(pci_channel_offline(phba->pcidev)))
2877                 return IOCB_ERROR;
2878
2879         /*
2880          * We should never get an IOCB if we are in a < LINK_DOWN state
2881          */
2882         if (unlikely(phba->link_state < LPFC_LINK_DOWN))
2883                 return IOCB_ERROR;
2884
2885         /*
2886          * Check to see if we are blocking IOCB processing because of a
2887          * outstanding mbox command.
2888          */
2889         if (unlikely(pring->flag & LPFC_STOP_IOCB_MBX))
2890                 goto iocb_busy;
2891
2892         if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
2893                 /*
2894                  * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
2895                  * can be issued if the link is not up.
2896                  */
2897                 switch (piocb->iocb.ulpCommand) {
2898                 case CMD_QUE_RING_BUF_CN:
2899                 case CMD_QUE_RING_BUF64_CN:
2900                         /*
2901                          * For IOCBs, like QUE_RING_BUF, that have no rsp ring
2902                          * completion, iocb_cmpl MUST be 0.
2903                          */
2904                         if (piocb->iocb_cmpl)
2905                                 piocb->iocb_cmpl = NULL;
2906                         /*FALLTHROUGH*/
2907                 case CMD_CREATE_XRI_CR:
2908                 case CMD_CLOSE_XRI_CN:
2909                 case CMD_CLOSE_XRI_CX:
2910                         break;
2911                 default:
2912                         goto iocb_busy;
2913                 }
2914
2915         /*
2916          * For FCP commands, we must be in a state where we can process link
2917          * attention events.
2918          */
2919         } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
2920                             !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
2921                 goto iocb_busy;
2922         }
2923
2924         while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
2925                (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
2926                 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
2927
2928         if (iocb)
2929                 lpfc_sli_update_ring(phba, pring);
2930         else
2931                 lpfc_sli_update_full_ring(phba, pring);
2932
2933         if (!piocb)
2934                 return IOCB_SUCCESS;
2935
2936         goto out_busy;
2937
2938  iocb_busy:
2939         pring->stats.iocb_cmd_delay++;
2940
2941  out_busy:
2942
2943         if (!(flag & SLI_IOCB_RET_IOCB)) {
2944                 __lpfc_sli_ringtx_put(phba, pring, piocb);
2945                 return IOCB_SUCCESS;
2946         }
2947
2948         return IOCB_BUSY;
2949 }
2950
2951
2952 int
2953 lpfc_sli_issue_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2954                     struct lpfc_iocbq *piocb, uint32_t flag)
2955 {
2956         unsigned long iflags;
2957         int rc;
2958
2959         spin_lock_irqsave(&phba->hbalock, iflags);
2960         rc = __lpfc_sli_issue_iocb(phba, pring, piocb, flag);
2961         spin_unlock_irqrestore(&phba->hbalock, iflags);
2962
2963         return rc;
2964 }
2965
2966 static int
2967 lpfc_extra_ring_setup( struct lpfc_hba *phba)
2968 {
2969         struct lpfc_sli *psli;
2970         struct lpfc_sli_ring *pring;
2971
2972         psli = &phba->sli;
2973
2974         /* Adjust cmd/rsp ring iocb entries more evenly */
2975
2976         /* Take some away from the FCP ring */
2977         pring = &psli->ring[psli->fcp_ring];
2978         pring->numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
2979         pring->numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
2980         pring->numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
2981         pring->numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
2982
2983         /* and give them to the extra ring */
2984         pring = &psli->ring[psli->extra_ring];
2985
2986         pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
2987         pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
2988         pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
2989         pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
2990
2991         /* Setup default profile for this ring */
2992         pring->iotag_max = 4096;
2993         pring->num_mask = 1;
2994         pring->prt[0].profile = 0;      /* Mask 0 */
2995         pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
2996         pring->prt[0].type = phba->cfg_multi_ring_type;
2997         pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
2998         return 0;
2999 }
3000
3001 int
3002 lpfc_sli_setup(struct lpfc_hba *phba)
3003 {
3004         int i, totiocbsize = 0;
3005         struct lpfc_sli *psli = &phba->sli;
3006         struct lpfc_sli_ring *pring;
3007
3008         psli->num_rings = MAX_CONFIGURED_RINGS;
3009         psli->sli_flag = 0;
3010         psli->fcp_ring = LPFC_FCP_RING;
3011         psli->next_ring = LPFC_FCP_NEXT_RING;
3012         psli->extra_ring = LPFC_EXTRA_RING;
3013
3014         psli->iocbq_lookup = NULL;
3015         psli->iocbq_lookup_len = 0;
3016         psli->last_iotag = 0;
3017
3018         for (i = 0; i < psli->num_rings; i++) {
3019                 pring = &psli->ring[i];
3020                 switch (i) {
3021                 case LPFC_FCP_RING:     /* ring 0 - FCP */
3022                         /* numCiocb and numRiocb are used in config_port */
3023                         pring->numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
3024                         pring->numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
3025                         pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
3026                         pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
3027                         pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
3028                         pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
3029                         pring->sizeCiocb = (phba->sli_rev == 3) ?
3030                                                         SLI3_IOCB_CMD_SIZE :
3031                                                         SLI2_IOCB_CMD_SIZE;
3032                         pring->sizeRiocb = (phba->sli_rev == 3) ?
3033                                                         SLI3_IOCB_RSP_SIZE :
3034                                                         SLI2_IOCB_RSP_SIZE;
3035                         pring->iotag_ctr = 0;
3036                         pring->iotag_max =
3037                             (phba->cfg_hba_queue_depth * 2);
3038                         pring->fast_iotag = pring->iotag_max;
3039                         pring->num_mask = 0;
3040                         break;
3041                 case LPFC_EXTRA_RING:   /* ring 1 - EXTRA */
3042                         /* numCiocb and numRiocb are used in config_port */
3043                         pring->numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
3044                         pring->numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
3045                         pring->sizeCiocb = (phba->sli_rev == 3) ?
3046                                                         SLI3_IOCB_CMD_SIZE :
3047                                                         SLI2_IOCB_CMD_SIZE;
3048                         pring->sizeRiocb = (phba->sli_rev == 3) ?
3049                                                         SLI3_IOCB_RSP_SIZE :
3050                                                         SLI2_IOCB_RSP_SIZE;
3051                         pring->iotag_max = phba->cfg_hba_queue_depth;
3052                         pring->num_mask = 0;
3053                         break;
3054                 case LPFC_ELS_RING:     /* ring 2 - ELS / CT */
3055                         /* numCiocb and numRiocb are used in config_port */
3056                         pring->numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
3057                         pring->numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
3058                         pring->sizeCiocb = (phba->sli_rev == 3) ?
3059                                                         SLI3_IOCB_CMD_SIZE :
3060                                                         SLI2_IOCB_CMD_SIZE;
3061                         pring->sizeRiocb = (phba->sli_rev == 3) ?
3062                                                         SLI3_IOCB_RSP_SIZE :
3063                                                         SLI2_IOCB_RSP_SIZE;
3064                         pring->fast_iotag = 0;
3065                         pring->iotag_ctr = 0;
3066                         pring->iotag_max = 4096;
3067                         pring->num_mask = 4;
3068                         pring->prt[0].profile = 0;      /* Mask 0 */
3069                         pring->prt[0].rctl = FC_ELS_REQ;
3070                         pring->prt[0].type = FC_ELS_DATA;
3071                         pring->prt[0].lpfc_sli_rcv_unsol_event =
3072                             lpfc_els_unsol_event;
3073                         pring->prt[1].profile = 0;      /* Mask 1 */
3074                         pring->prt[1].rctl = FC_ELS_RSP;
3075                         pring->prt[1].type = FC_ELS_DATA;
3076                         pring->prt[1].lpfc_sli_rcv_unsol_event =
3077                             lpfc_els_unsol_event;
3078                         pring->prt[2].profile = 0;      /* Mask 2 */
3079                         /* NameServer Inquiry */
3080                         pring->prt[2].rctl = FC_UNSOL_CTL;
3081                         /* NameServer */
3082                         pring->prt[2].type = FC_COMMON_TRANSPORT_ULP;
3083                         pring->prt[2].lpfc_sli_rcv_unsol_event =
3084                             lpfc_ct_unsol_event;
3085                         pring->prt[3].profile = 0;      /* Mask 3 */
3086                         /* NameServer response */
3087                         pring->prt[3].rctl = FC_SOL_CTL;
3088                         /* NameServer */
3089                         pring->prt[3].type = FC_COMMON_TRANSPORT_ULP;
3090                         pring->prt[3].lpfc_sli_rcv_unsol_event =
3091                             lpfc_ct_unsol_event;
3092                         break;
3093                 }
3094                 totiocbsize += (pring->numCiocb * pring->sizeCiocb) +
3095                                 (pring->numRiocb * pring->sizeRiocb);
3096         }
3097         if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
3098                 /* Too many cmd / rsp ring entries in SLI2 SLIM */
3099                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3100                                 "%d:0462 Too many cmd / rsp ring entries in "
3101                                 "SLI2 SLIM Data: x%x x%lx\n",
3102                                 phba->brd_no, totiocbsize,
3103                                 (unsigned long) MAX_SLIM_IOCB_SIZE);
3104         }
3105         if (phba->cfg_multi_ring_support == 2)
3106                 lpfc_extra_ring_setup(phba);
3107
3108         return 0;
3109 }
3110
3111 int
3112 lpfc_sli_queue_setup(struct lpfc_hba *phba)
3113 {
3114         struct lpfc_sli *psli;
3115         struct lpfc_sli_ring *pring;
3116         int i;
3117
3118         psli = &phba->sli;
3119         spin_lock_irq(&phba->hbalock);
3120         INIT_LIST_HEAD(&psli->mboxq);
3121         INIT_LIST_HEAD(&psli->mboxq_cmpl);
3122         /* Initialize list headers for txq and txcmplq as double linked lists */
3123         for (i = 0; i < psli->num_rings; i++) {
3124                 pring = &psli->ring[i];
3125                 pring->ringno = i;
3126                 pring->next_cmdidx  = 0;
3127                 pring->local_getidx = 0;
3128                 pring->cmdidx = 0;
3129                 INIT_LIST_HEAD(&pring->txq);
3130                 INIT_LIST_HEAD(&pring->txcmplq);
3131                 INIT_LIST_HEAD(&pring->iocb_continueq);
3132                 INIT_LIST_HEAD(&pring->postbufq);
3133         }
3134         spin_unlock_irq(&phba->hbalock);
3135         return 1;
3136 }
3137
3138 int
3139 lpfc_sli_host_down(struct lpfc_vport *vport)
3140 {
3141         LIST_HEAD(completions);
3142         struct lpfc_hba *phba = vport->phba;
3143         struct lpfc_sli *psli = &phba->sli;
3144         struct lpfc_sli_ring *pring;
3145         struct lpfc_iocbq *iocb, *next_iocb;
3146         int i;
3147         unsigned long flags = 0;
3148         uint16_t prev_pring_flag;
3149
3150         lpfc_cleanup_discovery_resources(vport);
3151
3152         spin_lock_irqsave(&phba->hbalock, flags);
3153         for (i = 0; i < psli->num_rings; i++) {
3154                 pring = &psli->ring[i];
3155                 prev_pring_flag = pring->flag;
3156                 if (pring->ringno == LPFC_ELS_RING) /* Only slow rings */
3157                         pring->flag |= LPFC_DEFERRED_RING_EVENT;
3158                 /*
3159                  * Error everything on the txq since these iocbs have not been
3160                  * given to the FW yet.
3161                  */
3162                 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
3163                         if (iocb->vport != vport)
3164                                 continue;
3165                         list_move_tail(&iocb->list, &completions);
3166                         pring->txq_cnt--;
3167                 }
3168
3169                 /* Next issue ABTS for everything on the txcmplq */
3170                 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
3171                                                                         list) {
3172                         if (iocb->vport != vport)
3173                                 continue;
3174                         lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3175                 }
3176
3177                 pring->flag = prev_pring_flag;
3178         }
3179
3180         spin_unlock_irqrestore(&phba->hbalock, flags);
3181
3182         while (!list_empty(&completions)) {
3183                 list_remove_head(&completions, iocb, struct lpfc_iocbq, list);
3184
3185                 if (!iocb->iocb_cmpl)
3186                         lpfc_sli_release_iocbq(phba, iocb);
3187                 else {
3188                         iocb->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
3189                         iocb->iocb.un.ulpWord[4] = IOERR_SLI_DOWN;
3190                         (iocb->iocb_cmpl) (phba, iocb, iocb);
3191                 }
3192         }
3193         return 1;
3194 }
3195
3196 int
3197 lpfc_sli_hba_down(struct lpfc_hba *phba)
3198 {
3199         LIST_HEAD(completions);
3200         struct lpfc_sli *psli = &phba->sli;
3201         struct lpfc_sli_ring *pring;
3202         LPFC_MBOXQ_t *pmb;
3203         struct lpfc_iocbq *iocb;
3204         IOCB_t *cmd = NULL;
3205         int i;
3206         unsigned long flags = 0;
3207
3208         lpfc_hba_down_prep(phba);
3209
3210         lpfc_fabric_abort_hba(phba);
3211
3212         spin_lock_irqsave(&phba->hbalock, flags);
3213         for (i = 0; i < psli->num_rings; i++) {
3214                 pring = &psli->ring[i];
3215                 if (pring->ringno == LPFC_ELS_RING) /* Only slow rings */
3216                         pring->flag |= LPFC_DEFERRED_RING_EVENT;
3217
3218                 /*
3219                  * Error everything on the txq since these iocbs have not been
3220                  * given to the FW yet.
3221                  */
3222                 list_splice_init(&pring->txq, &completions);
3223                 pring->txq_cnt = 0;
3224
3225         }
3226         spin_unlock_irqrestore(&phba->hbalock, flags);
3227
3228         while (!list_empty(&completions)) {
3229                 list_remove_head(&completions, iocb, struct lpfc_iocbq, list);
3230                 cmd = &iocb->iocb;
3231
3232                 if (!iocb->iocb_cmpl)
3233                         lpfc_sli_release_iocbq(phba, iocb);
3234                 else {
3235                         cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
3236                         cmd->un.ulpWord[4] = IOERR_SLI_DOWN;
3237                         (iocb->iocb_cmpl) (phba, iocb, iocb);
3238                 }
3239         }
3240
3241         /* Return any active mbox cmds */
3242         del_timer_sync(&psli->mbox_tmo);
3243         spin_lock_irqsave(&phba->hbalock, flags);
3244
3245         spin_lock(&phba->pport->work_port_lock);
3246         phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
3247         spin_unlock(&phba->pport->work_port_lock);
3248
3249         if (psli->mbox_active) {
3250                 list_add_tail(&psli->mbox_active->list, &completions);
3251                 psli->mbox_active = NULL;
3252                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
3253         }
3254
3255         /* Return any pending or completed mbox cmds */
3256         list_splice_init(&phba->sli.mboxq, &completions);
3257         list_splice_init(&phba->sli.mboxq_cmpl, &completions);
3258         INIT_LIST_HEAD(&psli->mboxq);
3259         INIT_LIST_HEAD(&psli->mboxq_cmpl);
3260
3261         spin_unlock_irqrestore(&phba->hbalock, flags);
3262
3263         while (!list_empty(&completions)) {
3264                 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
3265                 pmb->mb.mbxStatus = MBX_NOT_FINISHED;
3266                 if (pmb->mbox_cmpl) {
3267                         pmb->mbox_cmpl(phba,pmb);
3268                 }
3269         }
3270         return 1;
3271 }
3272
3273 void
3274 lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
3275 {
3276         uint32_t *src = srcp;
3277         uint32_t *dest = destp;
3278         uint32_t ldata;
3279         int i;
3280
3281         for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
3282                 ldata = *src;
3283                 ldata = le32_to_cpu(ldata);
3284                 *dest = ldata;
3285                 src++;
3286                 dest++;
3287         }
3288 }
3289
3290 int
3291 lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3292                          struct lpfc_dmabuf *mp)
3293 {
3294         /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
3295            later */
3296         spin_lock_irq(&phba->hbalock);
3297         list_add_tail(&mp->list, &pring->postbufq);
3298         pring->postbufq_cnt++;
3299         spin_unlock_irq(&phba->hbalock);
3300         return 0;
3301 }
3302
3303
3304 struct lpfc_dmabuf *
3305 lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3306                          dma_addr_t phys)
3307 {
3308         struct lpfc_dmabuf *mp, *next_mp;
3309         struct list_head *slp = &pring->postbufq;
3310
3311         /* Search postbufq, from the begining, looking for a match on phys */
3312         spin_lock_irq(&phba->hbalock);
3313         list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
3314                 if (mp->phys == phys) {
3315                         list_del_init(&mp->list);
3316                         pring->postbufq_cnt--;
3317                         spin_unlock_irq(&phba->hbalock);
3318                         return mp;
3319                 }
3320         }
3321
3322         spin_unlock_irq(&phba->hbalock);
3323         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3324                         "%d:0410 Cannot find virtual addr for mapped buf on "
3325                         "ring %d Data x%llx x%p x%p x%x\n",
3326                         phba->brd_no, pring->ringno, (unsigned long long)phys,
3327                         slp->next, slp->prev, pring->postbufq_cnt);
3328         return NULL;
3329 }
3330
3331 static void
3332 lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
3333                         struct lpfc_iocbq *rspiocb)
3334 {
3335         IOCB_t *irsp = &rspiocb->iocb;
3336         uint16_t abort_iotag, abort_context;
3337         struct lpfc_iocbq *abort_iocb;
3338         struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
3339
3340         abort_iocb = NULL;
3341
3342         if (irsp->ulpStatus) {
3343                 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
3344                 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
3345
3346                 spin_lock_irq(&phba->hbalock);
3347                 if (abort_iotag != 0 && abort_iotag <= phba->sli.last_iotag)
3348                         abort_iocb = phba->sli.iocbq_lookup[abort_iotag];
3349
3350                 lpfc_printf_log(phba, KERN_INFO, LOG_ELS | LOG_SLI,
3351                                 "%d:0327 Cannot abort els iocb %p "
3352                                 "with tag %x context %x, abort status %x, "
3353                                 "abort code %x\n",
3354                                 phba->brd_no, abort_iocb, abort_iotag,
3355                                 abort_context, irsp->ulpStatus,
3356                                 irsp->un.ulpWord[4]);
3357
3358                 /*
3359                  * make sure we have the right iocbq before taking it
3360                  * off the txcmplq and try to call completion routine.
3361                  */
3362                 if (!abort_iocb ||
3363                     abort_iocb->iocb.ulpContext != abort_context ||
3364                     (abort_iocb->iocb_flag & LPFC_DRIVER_ABORTED) == 0)
3365                         spin_unlock_irq(&phba->hbalock);
3366                 else {
3367                         list_del_init(&abort_iocb->list);
3368                         pring->txcmplq_cnt--;
3369                         spin_unlock_irq(&phba->hbalock);
3370
3371                         abort_iocb->iocb_flag &= ~LPFC_DRIVER_ABORTED;
3372                         abort_iocb->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
3373                         abort_iocb->iocb.un.ulpWord[4] = IOERR_SLI_ABORTED;
3374                         (abort_iocb->iocb_cmpl)(phba, abort_iocb, abort_iocb);
3375                 }
3376         }
3377
3378         lpfc_sli_release_iocbq(phba, cmdiocb);
3379         return;
3380 }
3381
3382 static void
3383 lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
3384                      struct lpfc_iocbq *rspiocb)
3385 {
3386         IOCB_t *irsp = &rspiocb->iocb;
3387
3388         /* ELS cmd tag <ulpIoTag> completes */
3389         lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
3390                         "%d (X):0133 Ignoring ELS cmd tag x%x completion Data: "
3391                         "x%x x%x x%x\n",
3392                         phba->brd_no, irsp->ulpIoTag, irsp->ulpStatus,
3393                         irsp->un.ulpWord[4], irsp->ulpTimeout);
3394         if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
3395                 lpfc_ct_free_iocb(phba, cmdiocb);
3396         else
3397                 lpfc_els_free_iocb(phba, cmdiocb);
3398         return;
3399 }
3400
3401 int
3402 lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3403                            struct lpfc_iocbq *cmdiocb)
3404 {
3405         struct lpfc_vport *vport = cmdiocb->vport;
3406         struct lpfc_iocbq *abtsiocbp;
3407         IOCB_t *icmd = NULL;
3408         IOCB_t *iabt = NULL;
3409         int retval = IOCB_ERROR;
3410
3411         /*
3412          * There are certain command types we don't want to abort.  And we
3413          * don't want to abort commands that are already in the process of
3414          * being aborted.
3415          */
3416         icmd = &cmdiocb->iocb;
3417         if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
3418             icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
3419             (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
3420                 return 0;
3421
3422         /* If we're unloading, don't abort iocb on the ELS ring, but change the
3423          * callback so that nothing happens when it finishes.
3424          */
3425         if ((vport->load_flag & FC_UNLOADING) &&
3426             (pring->ringno == LPFC_ELS_RING)) {
3427                 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
3428                         cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
3429                 else
3430                         cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
3431                 goto abort_iotag_exit;
3432         }
3433
3434         /* issue ABTS for this IOCB based on iotag */
3435         abtsiocbp = __lpfc_sli_get_iocbq(phba);
3436         if (abtsiocbp == NULL)
3437                 return 0;
3438
3439         /* This signals the response to set the correct status
3440          * before calling the completion handler.
3441          */
3442         cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
3443
3444         iabt = &abtsiocbp->iocb;
3445         iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
3446         iabt->un.acxri.abortContextTag = icmd->ulpContext;
3447         iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
3448         iabt->ulpLe = 1;
3449         iabt->ulpClass = icmd->ulpClass;
3450
3451         if (phba->link_state >= LPFC_LINK_UP)
3452                 iabt->ulpCommand = CMD_ABORT_XRI_CN;
3453         else
3454                 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
3455
3456         abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
3457
3458         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3459                         "%d (%d):0339 Abort xri x%x, original iotag x%x, "
3460                         "abort cmd iotag x%x\n",
3461                         phba->brd_no, vport->vpi,
3462                         iabt->un.acxri.abortContextTag,
3463                         iabt->un.acxri.abortIoTag, abtsiocbp->iotag);
3464         retval = __lpfc_sli_issue_iocb(phba, pring, abtsiocbp, 0);
3465
3466 abort_iotag_exit:
3467         /*
3468          * Caller to this routine should check for IOCB_ERROR
3469          * and handle it properly.  This routine no longer removes
3470          * iocb off txcmplq and call compl in case of IOCB_ERROR.
3471          */
3472         return retval;
3473 }
3474
3475 static int
3476 lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, uint16_t tgt_id,
3477                            uint64_t lun_id, uint32_t ctx,
3478                            lpfc_ctx_cmd ctx_cmd)
3479 {
3480         struct lpfc_scsi_buf *lpfc_cmd;
3481         struct scsi_cmnd *cmnd;
3482         int rc = 1;
3483
3484         if (!(iocbq->iocb_flag &  LPFC_IO_FCP))
3485                 return rc;
3486
3487         lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
3488         cmnd = lpfc_cmd->pCmd;
3489
3490         if (cmnd == NULL)
3491                 return rc;
3492
3493         switch (ctx_cmd) {
3494         case LPFC_CTX_LUN:
3495                 if ((cmnd->device->id == tgt_id) &&
3496                     (cmnd->device->lun == lun_id))
3497                         rc = 0;
3498                 break;
3499         case LPFC_CTX_TGT:
3500                 if (cmnd->device->id == tgt_id)
3501                         rc = 0;
3502                 break;
3503         case LPFC_CTX_CTX:
3504                 if (iocbq->iocb.ulpContext == ctx)
3505                         rc = 0;
3506                 break;
3507         case LPFC_CTX_HOST:
3508                 rc = 0;
3509                 break;
3510         default:
3511                 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
3512                         __FUNCTION__, ctx_cmd);
3513                 break;
3514         }
3515
3516         return rc;
3517 }
3518
3519 int
3520 lpfc_sli_sum_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3521                   uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd ctx_cmd)
3522 {
3523         struct lpfc_iocbq *iocbq;
3524         int sum, i;
3525
3526         for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
3527                 iocbq = phba->sli.iocbq_lookup[i];
3528
3529                 if (lpfc_sli_validate_fcp_iocb (iocbq, tgt_id, lun_id,
3530                                                 0, ctx_cmd) == 0)
3531                         sum++;
3532         }
3533
3534         return sum;
3535 }
3536
3537 void
3538 lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
3539                         struct lpfc_iocbq *rspiocb)
3540 {
3541         lpfc_sli_release_iocbq(phba, cmdiocb);
3542         return;
3543 }
3544
3545 int
3546 lpfc_sli_abort_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3547                     uint16_t tgt_id, uint64_t lun_id, uint32_t ctx,
3548                     lpfc_ctx_cmd abort_cmd)
3549 {
3550         struct lpfc_iocbq *iocbq;
3551         struct lpfc_iocbq *abtsiocb;
3552         IOCB_t *cmd = NULL;
3553         int errcnt = 0, ret_val = 0;
3554         int i;
3555
3556         for (i = 1; i <= phba->sli.last_iotag; i++) {
3557                 iocbq = phba->sli.iocbq_lookup[i];
3558
3559                 if (lpfc_sli_validate_fcp_iocb(iocbq, tgt_id, lun_id, 0,
3560                                                abort_cmd) != 0)
3561                         continue;
3562
3563                 /* issue ABTS for this IOCB based on iotag */
3564                 abtsiocb = lpfc_sli_get_iocbq(phba);
3565                 if (abtsiocb == NULL) {
3566                         errcnt++;
3567                         continue;
3568                 }
3569
3570                 cmd = &iocbq->iocb;
3571                 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
3572                 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
3573                 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
3574                 abtsiocb->iocb.ulpLe = 1;
3575                 abtsiocb->iocb.ulpClass = cmd->ulpClass;
3576                 abtsiocb->vport = phba->pport;
3577
3578                 if (lpfc_is_link_up(phba))
3579                         abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
3580                 else
3581                         abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
3582
3583                 /* Setup callback routine and issue the command. */
3584                 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
3585                 ret_val = lpfc_sli_issue_iocb(phba, pring, abtsiocb, 0);
3586                 if (ret_val == IOCB_ERROR) {
3587                         lpfc_sli_release_iocbq(phba, abtsiocb);
3588                         errcnt++;
3589                         continue;
3590                 }
3591         }
3592
3593         return errcnt;
3594 }
3595
3596 static void
3597 lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
3598                         struct lpfc_iocbq *cmdiocbq,
3599                         struct lpfc_iocbq *rspiocbq)
3600 {
3601         wait_queue_head_t *pdone_q;
3602         unsigned long iflags;
3603
3604         spin_lock_irqsave(&phba->hbalock, iflags);
3605         cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
3606         if (cmdiocbq->context2 && rspiocbq)
3607                 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
3608                        &rspiocbq->iocb, sizeof(IOCB_t));
3609
3610         pdone_q = cmdiocbq->context_un.wait_queue;
3611         if (pdone_q)
3612                 wake_up(pdone_q);
3613         spin_unlock_irqrestore(&phba->hbalock, iflags);
3614         return;
3615 }
3616
3617 /*
3618  * Issue the caller's iocb and wait for its completion, but no longer than the
3619  * caller's timeout.  Note that iocb_flags is cleared before the
3620  * lpfc_sli_issue_call since the wake routine sets a unique value and by
3621  * definition this is a wait function.
3622  */
3623
3624 int
3625 lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
3626                          struct lpfc_sli_ring *pring,
3627                          struct lpfc_iocbq *piocb,
3628                          struct lpfc_iocbq *prspiocbq,
3629                          uint32_t timeout)
3630 {
3631         DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
3632         long timeleft, timeout_req = 0;
3633         int retval = IOCB_SUCCESS;
3634         uint32_t creg_val;
3635
3636         /*
3637          * If the caller has provided a response iocbq buffer, then context2
3638          * is NULL or its an error.
3639          */
3640         if (prspiocbq) {
3641                 if (piocb->context2)
3642                         return IOCB_ERROR;
3643                 piocb->context2 = prspiocbq;
3644         }
3645
3646         piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
3647         piocb->context_un.wait_queue = &done_q;
3648         piocb->iocb_flag &= ~LPFC_IO_WAKE;
3649
3650         if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
3651                 creg_val = readl(phba->HCregaddr);
3652                 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
3653                 writel(creg_val, phba->HCregaddr);
3654                 readl(phba->HCregaddr); /* flush */
3655         }
3656
3657         retval = lpfc_sli_issue_iocb(phba, pring, piocb, 0);
3658         if (retval == IOCB_SUCCESS) {
3659                 timeout_req = timeout * HZ;
3660                 timeleft = wait_event_timeout(done_q,
3661                                 piocb->iocb_flag & LPFC_IO_WAKE,
3662                                 timeout_req);
3663
3664                 if (piocb->iocb_flag & LPFC_IO_WAKE) {
3665                         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3666                                         "%d:0331 IOCB wake signaled\n",
3667                                         phba->brd_no);
3668                 } else if (timeleft == 0) {
3669                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3670                                         "%d:0338 IOCB wait timeout error - no "
3671                                         "wake response Data x%x\n",
3672                                         phba->brd_no, timeout);
3673                         retval = IOCB_TIMEDOUT;
3674                 } else {
3675                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3676                                         "%d:0330 IOCB wake NOT set, "
3677                                         "Data x%x x%lx\n", phba->brd_no,
3678                                         timeout, (timeleft / jiffies));
3679                         retval = IOCB_TIMEDOUT;
3680                 }
3681         } else {
3682                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3683                                 "%d:0332 IOCB wait issue failed, Data x%x\n",
3684                                 phba->brd_no, retval);
3685                 retval = IOCB_ERROR;
3686         }
3687
3688         if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
3689                 creg_val = readl(phba->HCregaddr);
3690                 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
3691                 writel(creg_val, phba->HCregaddr);
3692                 readl(phba->HCregaddr); /* flush */
3693         }
3694
3695         if (prspiocbq)
3696                 piocb->context2 = NULL;
3697
3698         piocb->context_un.wait_queue = NULL;
3699         piocb->iocb_cmpl = NULL;
3700         return retval;
3701 }
3702
3703 int
3704 lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
3705                          uint32_t timeout)
3706 {
3707         DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
3708         int retval;
3709         unsigned long flag;
3710
3711         /* The caller must leave context1 empty. */
3712         if (pmboxq->context1 != 0)
3713                 return MBX_NOT_FINISHED;
3714
3715         /* setup wake call as IOCB callback */
3716         pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
3717         /* setup context field to pass wait_queue pointer to wake function  */
3718         pmboxq->context1 = &done_q;
3719
3720         /* now issue the command */
3721         retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
3722
3723         if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
3724                 wait_event_interruptible_timeout(done_q,
3725                                 pmboxq->mbox_flag & LPFC_MBX_WAKE,
3726                                 timeout * HZ);
3727
3728                 spin_lock_irqsave(&phba->hbalock, flag);
3729                 pmboxq->context1 = NULL;
3730                 /*
3731                  * if LPFC_MBX_WAKE flag is set the mailbox is completed
3732                  * else do not free the resources.
3733                  */
3734                 if (pmboxq->mbox_flag & LPFC_MBX_WAKE)
3735                         retval = MBX_SUCCESS;
3736                 else {
3737                         retval = MBX_TIMEOUT;
3738                         pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3739                 }
3740                 spin_unlock_irqrestore(&phba->hbalock, flag);
3741         }
3742
3743         return retval;
3744 }
3745
3746 int
3747 lpfc_sli_flush_mbox_queue(struct lpfc_hba * phba)
3748 {
3749         struct lpfc_vport *vport = phba->pport;
3750         int i = 0;
3751         uint32_t ha_copy;
3752
3753         while (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE && !vport->stopped) {
3754                 if (i++ > LPFC_MBOX_TMO * 1000)
3755                         return 1;
3756
3757                 /*
3758                  * Call lpfc_sli_handle_mb_event only if a mailbox cmd
3759                  * did finish. This way we won't get the misleading
3760                  * "Stray Mailbox Interrupt" message.
3761                  */
3762                 spin_lock_irq(&phba->hbalock);
3763                 ha_copy = phba->work_ha;
3764                 phba->work_ha &= ~HA_MBATT;
3765                 spin_unlock_irq(&phba->hbalock);
3766
3767                 if (ha_copy & HA_MBATT)
3768                         if (lpfc_sli_handle_mb_event(phba) == 0)
3769                                 i = 0;
3770
3771                 msleep(1);
3772         }
3773
3774         return (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) ? 1 : 0;
3775 }
3776
3777 irqreturn_t
3778 lpfc_intr_handler(int irq, void *dev_id)
3779 {
3780         struct lpfc_hba  *phba;
3781         uint32_t ha_copy;
3782         uint32_t work_ha_copy;
3783         unsigned long status;
3784         int i;
3785         uint32_t control;
3786
3787         MAILBOX_t *mbox, *pmbox;
3788         struct lpfc_vport *vport;
3789         struct lpfc_nodelist *ndlp;
3790         struct lpfc_dmabuf *mp;
3791         LPFC_MBOXQ_t *pmb;
3792         int rc;
3793
3794         /*
3795          * Get the driver's phba structure from the dev_id and
3796          * assume the HBA is not interrupting.
3797          */
3798         phba = (struct lpfc_hba *) dev_id;
3799
3800         if (unlikely(!phba))
3801                 return IRQ_NONE;
3802
3803         /* If the pci channel is offline, ignore all the interrupts. */
3804         if (unlikely(pci_channel_offline(phba->pcidev)))
3805                 return IRQ_NONE;
3806
3807         phba->sli.slistat.sli_intr++;
3808
3809         /*
3810          * Call the HBA to see if it is interrupting.  If not, don't claim
3811          * the interrupt
3812          */
3813
3814         /* Ignore all interrupts during initialization. */
3815         if (unlikely(phba->link_state < LPFC_LINK_DOWN))
3816                 return IRQ_NONE;
3817
3818         /*
3819          * Read host attention register to determine interrupt source
3820          * Clear Attention Sources, except Error Attention (to
3821          * preserve status) and Link Attention
3822          */
3823         spin_lock(&phba->hbalock);
3824         ha_copy = readl(phba->HAregaddr);
3825         /* If somebody is waiting to handle an eratt don't process it
3826          * here.  The brdkill function will do this.
3827          */
3828         if (phba->link_flag & LS_IGNORE_ERATT)
3829                 ha_copy &= ~HA_ERATT;
3830         writel((ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
3831         readl(phba->HAregaddr); /* flush */
3832         spin_unlock(&phba->hbalock);
3833
3834         if (unlikely(!ha_copy))
3835                 return IRQ_NONE;
3836
3837         work_ha_copy = ha_copy & phba->work_ha_mask;
3838
3839         if (unlikely(work_ha_copy)) {
3840                 if (work_ha_copy & HA_LATT) {
3841                         if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
3842                                 /*
3843                                  * Turn off Link Attention interrupts
3844                                  * until CLEAR_LA done
3845                                  */
3846                                 spin_lock(&phba->hbalock);
3847                                 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
3848                                 control = readl(phba->HCregaddr);
3849                                 control &= ~HC_LAINT_ENA;
3850                                 writel(control, phba->HCregaddr);
3851                                 readl(phba->HCregaddr); /* flush */
3852                                 spin_unlock(&phba->hbalock);
3853                         }
3854                         else
3855                                 work_ha_copy &= ~HA_LATT;
3856                 }
3857
3858                 if (work_ha_copy & ~(HA_ERATT|HA_MBATT|HA_LATT)) {
3859                         /*
3860                          * Turn off Slow Rings interrupts, LPFC_ELS_RING is
3861                          * the only slow ring.
3862                          */
3863                         status = (work_ha_copy &
3864                                 (HA_RXMASK  << (4*LPFC_ELS_RING)));
3865                         status >>= (4*LPFC_ELS_RING);
3866                         if (status & HA_RXMASK) {
3867                                 spin_lock(&phba->hbalock);
3868                                 control = readl(phba->HCregaddr);
3869
3870                                 lpfc_debugfs_slow_ring_trc(phba,
3871                                 "ISR slow ring:   ctl:x%x stat:x%x isrcnt:x%x",
3872                                 control, status,
3873                                 (uint32_t)phba->sli.slistat.sli_intr);
3874
3875                                 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
3876                                         lpfc_debugfs_slow_ring_trc(phba,
3877                                                 "ISR Disable ring:"
3878                                                 "pwork:x%x hawork:x%x wait:x%x",
3879                                                 phba->work_ha, work_ha_copy,
3880                                                 (uint32_t)((unsigned long)
3881                                                 phba->work_wait));
3882
3883                                         control &=
3884                                             ~(HC_R0INT_ENA << LPFC_ELS_RING);
3885                                         writel(control, phba->HCregaddr);
3886                                         readl(phba->HCregaddr); /* flush */
3887                                 }
3888                                 else {
3889                                         lpfc_debugfs_slow_ring_trc(phba,
3890                                                 "ISR slow ring:   pwork:"
3891                                                 "x%x hawork:x%x wait:x%x",
3892                                                 phba->work_ha, work_ha_copy,
3893                                                 (uint32_t)((unsigned long)
3894                                                 phba->work_wait));
3895                                 }
3896                                 spin_unlock(&phba->hbalock);
3897                         }
3898                 }
3899
3900                 if (work_ha_copy & HA_ERATT) {
3901                         phba->link_state = LPFC_HBA_ERROR;
3902                         /*
3903                          * There was a link/board error.  Read the
3904                          * status register to retrieve the error event
3905                          * and process it.
3906                          */
3907                         phba->sli.slistat.err_attn_event++;
3908                         /* Save status info */
3909                         phba->work_hs = readl(phba->HSregaddr);
3910                         phba->work_status[0] = readl(phba->MBslimaddr + 0xa8);
3911                         phba->work_status[1] = readl(phba->MBslimaddr + 0xac);
3912
3913                         /* Clear Chip error bit */
3914                         writel(HA_ERATT, phba->HAregaddr);
3915                         readl(phba->HAregaddr); /* flush */
3916                         phba->pport->stopped = 1;
3917                 }
3918
3919                 if ((work_ha_copy & HA_MBATT) &&
3920                     (phba->sli.mbox_active)) {
3921                         pmb = phba->sli.mbox_active;
3922                         pmbox = &pmb->mb;
3923                         mbox = &phba->slim2p->mbx;
3924                         vport = pmb->vport;
3925
3926                         /* First check out the status word */
3927                         lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
3928                         if (pmbox->mbxOwner != OWN_HOST) {
3929                                 /*
3930                                  * Stray Mailbox Interrupt, mbxCommand <cmd>
3931                                  * mbxStatus <status>
3932                                  */
3933                                 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX |
3934                                                 LOG_SLI,
3935                                                 "%d (%d):0304 Stray Mailbox "
3936                                                 "Interrupt mbxCommand x%x "
3937                                                 "mbxStatus x%x\n",
3938                                                 phba->brd_no,
3939                                                 (vport
3940                                                  ? vport->vpi : 0),
3941                                                 pmbox->mbxCommand,
3942                                                 pmbox->mbxStatus);
3943                         }
3944                         phba->last_completion_time = jiffies;
3945                         del_timer_sync(&phba->sli.mbox_tmo);
3946
3947                         phba->sli.mbox_active = NULL;
3948                         if (pmb->mbox_cmpl) {
3949                                 lpfc_sli_pcimem_bcopy(mbox, pmbox,
3950                                                       MAILBOX_CMD_SIZE);
3951                         }
3952                         if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
3953                                 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
3954
3955                                 lpfc_debugfs_disc_trc(vport,
3956                                         LPFC_DISC_TRC_MBOX_VPORT,
3957                                         "MBOX dflt rpi: : status:x%x rpi:x%x",
3958                                         (uint32_t)pmbox->mbxStatus,
3959                                         pmbox->un.varWords[0], 0);
3960
3961                                 if ( !pmbox->mbxStatus) {
3962                                         mp = (struct lpfc_dmabuf *)
3963                                                 (pmb->context1);
3964                                         ndlp = (struct lpfc_nodelist *)
3965                                                 pmb->context2;
3966
3967                                         /* Reg_LOGIN of dflt RPI was successful.
3968                                          * new lets get rid of the RPI using the
3969                                          * same mbox buffer.
3970                                          */
3971                                         lpfc_unreg_login(phba, vport->vpi,
3972                                                 pmbox->un.varWords[0], pmb);
3973                                         pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
3974                                         pmb->context1 = mp;
3975                                         pmb->context2 = ndlp;
3976                                         pmb->vport = vport;
3977                                         spin_lock(&phba->hbalock);
3978                                         phba->sli.sli_flag &=
3979                                                 ~LPFC_SLI_MBOX_ACTIVE;
3980                                         spin_unlock(&phba->hbalock);
3981                                         goto send_current_mbox;
3982                                 }
3983                         }
3984                         spin_lock(&phba->pport->work_port_lock);
3985                         phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
3986                         spin_unlock(&phba->pport->work_port_lock);
3987                         lpfc_mbox_cmpl_put(phba, pmb);
3988                 }
3989                 if ((work_ha_copy & HA_MBATT) &&
3990                     (phba->sli.mbox_active == NULL)) {
3991 send_next_mbox:
3992                         spin_lock(&phba->hbalock);
3993                         phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
3994                         pmb = lpfc_mbox_get(phba);
3995                         spin_unlock(&phba->hbalock);
3996 send_current_mbox:
3997                         /* Process next mailbox command if there is one */
3998                         if (pmb != NULL) {
3999                                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
4000                                 if (rc == MBX_NOT_FINISHED) {
4001                                         pmb->mb.mbxStatus = MBX_NOT_FINISHED;
4002                                         lpfc_mbox_cmpl_put(phba, pmb);
4003                                         goto send_next_mbox;
4004                                 }
4005                         } else {
4006                                 /* Turn on IOCB processing */
4007                                 for (i = 0; i < phba->sli.num_rings; i++)
4008                                         lpfc_sli_turn_on_ring(phba, i);
4009                         }
4010
4011                 }
4012
4013                 spin_lock(&phba->hbalock);
4014                 phba->work_ha |= work_ha_copy;
4015                 if (phba->work_wait)
4016                         lpfc_worker_wake_up(phba);
4017                 spin_unlock(&phba->hbalock);
4018         }
4019
4020         ha_copy &= ~(phba->work_ha_mask);
4021
4022         /*
4023          * Process all events on FCP ring.  Take the optimized path for
4024          * FCP IO.  Any other IO is slow path and is handled by
4025          * the worker thread.
4026          */
4027         status = (ha_copy & (HA_RXMASK  << (4*LPFC_FCP_RING)));
4028         status >>= (4*LPFC_FCP_RING);
4029         if (status & HA_RXMASK)
4030                 lpfc_sli_handle_fast_ring_event(phba,
4031                                                 &phba->sli.ring[LPFC_FCP_RING],
4032                                                 status);
4033
4034         if (phba->cfg_multi_ring_support == 2) {
4035                 /*
4036                  * Process all events on extra ring.  Take the optimized path
4037                  * for extra ring IO.  Any other IO is slow path and is handled
4038                  * by the worker thread.
4039                  */
4040                 status = (ha_copy & (HA_RXMASK  << (4*LPFC_EXTRA_RING)));
4041                 status >>= (4*LPFC_EXTRA_RING);
4042                 if (status & HA_RXMASK) {
4043                         lpfc_sli_handle_fast_ring_event(phba,
4044                                         &phba->sli.ring[LPFC_EXTRA_RING],
4045                                         status);
4046                 }
4047         }
4048         return IRQ_HANDLED;
4049
4050 } /* lpfc_intr_handler */