[SCSI] lpfc 8.1.2: Add ERROR and WARM_START modes for diagnostic purposes.
[safe/jmp/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-2006 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
42 /*
43  * Define macro to log: Mailbox command x%x cannot issue Data
44  * This allows multiple uses of lpfc_msgBlk0311
45  * w/o perturbing log msg utility.
46  */
47 #define LOG_MBOX_CANNOT_ISSUE_DATA( phba, mb, psli, flag) \
48                         lpfc_printf_log(phba, \
49                                 KERN_INFO, \
50                                 LOG_MBOX | LOG_SLI, \
51                                 "%d:0311 Mailbox command x%x cannot issue " \
52                                 "Data: x%x x%x x%x\n", \
53                                 phba->brd_no, \
54                                 mb->mbxCommand,         \
55                                 phba->hba_state,        \
56                                 psli->sli_flag, \
57                                 flag);
58
59
60 /* There are only four IOCB completion types. */
61 typedef enum _lpfc_iocb_type {
62         LPFC_UNKNOWN_IOCB,
63         LPFC_UNSOL_IOCB,
64         LPFC_SOL_IOCB,
65         LPFC_ABORT_IOCB
66 } lpfc_iocb_type;
67
68 struct lpfc_iocbq *
69 lpfc_sli_get_iocbq(struct lpfc_hba * phba)
70 {
71         struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
72         struct lpfc_iocbq * iocbq = NULL;
73
74         list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
75         return iocbq;
76 }
77
78 void
79 lpfc_sli_release_iocbq(struct lpfc_hba * phba, struct lpfc_iocbq * iocbq)
80 {
81         size_t start_clean = (size_t)(&((struct lpfc_iocbq *)NULL)->iocb);
82
83         /*
84          * Clean all volatile data fields, preserve iotag and node struct.
85          */
86         memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
87         list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
88 }
89
90 /*
91  * Translate the iocb command to an iocb command type used to decide the final
92  * disposition of each completed IOCB.
93  */
94 static lpfc_iocb_type
95 lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
96 {
97         lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
98
99         if (iocb_cmnd > CMD_MAX_IOCB_CMD)
100                 return 0;
101
102         switch (iocb_cmnd) {
103         case CMD_XMIT_SEQUENCE_CR:
104         case CMD_XMIT_SEQUENCE_CX:
105         case CMD_XMIT_BCAST_CN:
106         case CMD_XMIT_BCAST_CX:
107         case CMD_ELS_REQUEST_CR:
108         case CMD_ELS_REQUEST_CX:
109         case CMD_CREATE_XRI_CR:
110         case CMD_CREATE_XRI_CX:
111         case CMD_GET_RPI_CN:
112         case CMD_XMIT_ELS_RSP_CX:
113         case CMD_GET_RPI_CR:
114         case CMD_FCP_IWRITE_CR:
115         case CMD_FCP_IWRITE_CX:
116         case CMD_FCP_IREAD_CR:
117         case CMD_FCP_IREAD_CX:
118         case CMD_FCP_ICMND_CR:
119         case CMD_FCP_ICMND_CX:
120         case CMD_ADAPTER_MSG:
121         case CMD_ADAPTER_DUMP:
122         case CMD_XMIT_SEQUENCE64_CR:
123         case CMD_XMIT_SEQUENCE64_CX:
124         case CMD_XMIT_BCAST64_CN:
125         case CMD_XMIT_BCAST64_CX:
126         case CMD_ELS_REQUEST64_CR:
127         case CMD_ELS_REQUEST64_CX:
128         case CMD_FCP_IWRITE64_CR:
129         case CMD_FCP_IWRITE64_CX:
130         case CMD_FCP_IREAD64_CR:
131         case CMD_FCP_IREAD64_CX:
132         case CMD_FCP_ICMND64_CR:
133         case CMD_FCP_ICMND64_CX:
134         case CMD_GEN_REQUEST64_CR:
135         case CMD_GEN_REQUEST64_CX:
136         case CMD_XMIT_ELS_RSP64_CX:
137                 type = LPFC_SOL_IOCB;
138                 break;
139         case CMD_ABORT_XRI_CN:
140         case CMD_ABORT_XRI_CX:
141         case CMD_CLOSE_XRI_CN:
142         case CMD_CLOSE_XRI_CX:
143         case CMD_XRI_ABORTED_CX:
144         case CMD_ABORT_MXRI64_CN:
145                 type = LPFC_ABORT_IOCB;
146                 break;
147         case CMD_RCV_SEQUENCE_CX:
148         case CMD_RCV_ELS_REQ_CX:
149         case CMD_RCV_SEQUENCE64_CX:
150         case CMD_RCV_ELS_REQ64_CX:
151                 type = LPFC_UNSOL_IOCB;
152                 break;
153         default:
154                 type = LPFC_UNKNOWN_IOCB;
155                 break;
156         }
157
158         return type;
159 }
160
161 static int
162 lpfc_sli_ring_map(struct lpfc_hba * phba, LPFC_MBOXQ_t *pmb)
163 {
164         struct lpfc_sli *psli = &phba->sli;
165         MAILBOX_t *pmbox = &pmb->mb;
166         int i, rc;
167
168         for (i = 0; i < psli->num_rings; i++) {
169                 phba->hba_state = LPFC_INIT_MBX_CMDS;
170                 lpfc_config_ring(phba, i, pmb);
171                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
172                 if (rc != MBX_SUCCESS) {
173                         lpfc_printf_log(phba,
174                                         KERN_ERR,
175                                         LOG_INIT,
176                                         "%d:0446 Adapter failed to init, "
177                                         "mbxCmd x%x CFG_RING, mbxStatus x%x, "
178                                         "ring %d\n",
179                                         phba->brd_no,
180                                         pmbox->mbxCommand,
181                                         pmbox->mbxStatus,
182                                         i);
183                         phba->hba_state = LPFC_HBA_ERROR;
184                         return -ENXIO;
185                 }
186         }
187         return 0;
188 }
189
190 static int
191 lpfc_sli_ringtxcmpl_put(struct lpfc_hba * phba,
192                         struct lpfc_sli_ring * pring, struct lpfc_iocbq * piocb)
193 {
194         uint16_t iotag;
195
196         list_add_tail(&piocb->list, &pring->txcmplq);
197         pring->txcmplq_cnt++;
198         if (unlikely(pring->ringno == LPFC_ELS_RING))
199                 mod_timer(&phba->els_tmofunc,
200                                         jiffies + HZ * (phba->fc_ratov << 1));
201
202         if (pring->fast_lookup) {
203                 /* Setup fast lookup based on iotag for completion */
204                 iotag = piocb->iocb.ulpIoTag;
205                 if (iotag && (iotag < pring->fast_iotag))
206                         *(pring->fast_lookup + iotag) = piocb;
207                 else {
208
209                         /* Cmd ring <ringno> put: iotag <iotag> greater then
210                            configured max <fast_iotag> wd0 <icmd> */
211                         lpfc_printf_log(phba,
212                                         KERN_ERR,
213                                         LOG_SLI,
214                                         "%d:0316 Cmd ring %d put: iotag x%x "
215                                         "greater then configured max x%x "
216                                         "wd0 x%x\n",
217                                         phba->brd_no,
218                                         pring->ringno, iotag,
219                                         pring->fast_iotag,
220                                         *(((uint32_t *)(&piocb->iocb)) + 7));
221                 }
222         }
223         return (0);
224 }
225
226 static struct lpfc_iocbq *
227 lpfc_sli_ringtx_get(struct lpfc_hba * phba, struct lpfc_sli_ring * pring)
228 {
229         struct list_head *dlp;
230         struct lpfc_iocbq *cmd_iocb;
231
232         dlp = &pring->txq;
233         cmd_iocb = NULL;
234         list_remove_head((&pring->txq), cmd_iocb,
235                          struct lpfc_iocbq,
236                          list);
237         if (cmd_iocb) {
238                 /* If the first ptr is not equal to the list header,
239                  * deque the IOCBQ_t and return it.
240                  */
241                 pring->txq_cnt--;
242         }
243         return (cmd_iocb);
244 }
245
246 static IOCB_t *
247 lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
248 {
249         struct lpfc_pgp *pgp = &phba->slim2p->mbx.us.s2.port[pring->ringno];
250         uint32_t  max_cmd_idx = pring->numCiocb;
251         IOCB_t *iocb = NULL;
252
253         if ((pring->next_cmdidx == pring->cmdidx) &&
254            (++pring->next_cmdidx >= max_cmd_idx))
255                 pring->next_cmdidx = 0;
256
257         if (unlikely(pring->local_getidx == pring->next_cmdidx)) {
258
259                 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
260
261                 if (unlikely(pring->local_getidx >= max_cmd_idx)) {
262                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
263                                         "%d:0315 Ring %d issue: portCmdGet %d "
264                                         "is bigger then cmd ring %d\n",
265                                         phba->brd_no, pring->ringno,
266                                         pring->local_getidx, max_cmd_idx);
267
268                         phba->hba_state = LPFC_HBA_ERROR;
269                         /*
270                          * All error attention handlers are posted to
271                          * worker thread
272                          */
273                         phba->work_ha |= HA_ERATT;
274                         phba->work_hs = HS_FFER3;
275                         if (phba->work_wait)
276                                 wake_up(phba->work_wait);
277
278                         return NULL;
279                 }
280
281                 if (pring->local_getidx == pring->next_cmdidx)
282                         return NULL;
283         }
284
285         iocb = IOCB_ENTRY(pring->cmdringaddr, pring->cmdidx);
286
287         return iocb;
288 }
289
290 uint16_t
291 lpfc_sli_next_iotag(struct lpfc_hba * phba, struct lpfc_iocbq * iocbq)
292 {
293         struct lpfc_iocbq ** new_arr;
294         struct lpfc_iocbq ** old_arr;
295         size_t new_len;
296         struct lpfc_sli *psli = &phba->sli;
297         uint16_t iotag;
298
299         spin_lock_irq(phba->host->host_lock);
300         iotag = psli->last_iotag;
301         if(++iotag < psli->iocbq_lookup_len) {
302                 psli->last_iotag = iotag;
303                 psli->iocbq_lookup[iotag] = iocbq;
304                 spin_unlock_irq(phba->host->host_lock);
305                 iocbq->iotag = iotag;
306                 return iotag;
307         }
308         else if (psli->iocbq_lookup_len < (0xffff
309                                            - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
310                 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
311                 spin_unlock_irq(phba->host->host_lock);
312                 new_arr = kmalloc(new_len * sizeof (struct lpfc_iocbq *),
313                                   GFP_KERNEL);
314                 if (new_arr) {
315                         memset((char *)new_arr, 0,
316                                new_len * sizeof (struct lpfc_iocbq *));
317                         spin_lock_irq(phba->host->host_lock);
318                         old_arr = psli->iocbq_lookup;
319                         if (new_len <= psli->iocbq_lookup_len) {
320                                 /* highly unprobable case */
321                                 kfree(new_arr);
322                                 iotag = psli->last_iotag;
323                                 if(++iotag < psli->iocbq_lookup_len) {
324                                         psli->last_iotag = iotag;
325                                         psli->iocbq_lookup[iotag] = iocbq;
326                                         spin_unlock_irq(phba->host->host_lock);
327                                         iocbq->iotag = iotag;
328                                         return iotag;
329                                 }
330                                 spin_unlock_irq(phba->host->host_lock);
331                                 return 0;
332                         }
333                         if (psli->iocbq_lookup)
334                                 memcpy(new_arr, old_arr,
335                                        ((psli->last_iotag  + 1) *
336                                         sizeof (struct lpfc_iocbq *)));
337                         psli->iocbq_lookup = new_arr;
338                         psli->iocbq_lookup_len = new_len;
339                         psli->last_iotag = iotag;
340                         psli->iocbq_lookup[iotag] = iocbq;
341                         spin_unlock_irq(phba->host->host_lock);
342                         iocbq->iotag = iotag;
343                         kfree(old_arr);
344                         return iotag;
345                 }
346         }
347
348         lpfc_printf_log(phba, KERN_ERR,LOG_SLI,
349                         "%d:0318 Failed to allocate IOTAG.last IOTAG is %d\n",
350                         phba->brd_no, psli->last_iotag);
351
352         return 0;
353 }
354
355 static void
356 lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
357                 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
358 {
359         /*
360          * Set up an iotag
361          */
362         nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
363
364         /*
365          * Issue iocb command to adapter
366          */
367         lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, sizeof (IOCB_t));
368         wmb();
369         pring->stats.iocb_cmd++;
370
371         /*
372          * If there is no completion routine to call, we can release the
373          * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
374          * that have no rsp ring completion, iocb_cmpl MUST be NULL.
375          */
376         if (nextiocb->iocb_cmpl)
377                 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
378         else
379                 lpfc_sli_release_iocbq(phba, nextiocb);
380
381         /*
382          * Let the HBA know what IOCB slot will be the next one the
383          * driver will put a command into.
384          */
385         pring->cmdidx = pring->next_cmdidx;
386         writel(pring->cmdidx, phba->MBslimaddr
387                + (SLIMOFF + (pring->ringno * 2)) * 4);
388 }
389
390 static void
391 lpfc_sli_update_full_ring(struct lpfc_hba * phba,
392                           struct lpfc_sli_ring *pring)
393 {
394         int ringno = pring->ringno;
395
396         pring->flag |= LPFC_CALL_RING_AVAILABLE;
397
398         wmb();
399
400         /*
401          * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
402          * The HBA will tell us when an IOCB entry is available.
403          */
404         writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
405         readl(phba->CAregaddr); /* flush */
406
407         pring->stats.iocb_cmd_full++;
408 }
409
410 static void
411 lpfc_sli_update_ring(struct lpfc_hba * phba,
412                      struct lpfc_sli_ring *pring)
413 {
414         int ringno = pring->ringno;
415
416         /*
417          * Tell the HBA that there is work to do in this ring.
418          */
419         wmb();
420         writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
421         readl(phba->CAregaddr); /* flush */
422 }
423
424 static void
425 lpfc_sli_resume_iocb(struct lpfc_hba * phba, struct lpfc_sli_ring * pring)
426 {
427         IOCB_t *iocb;
428         struct lpfc_iocbq *nextiocb;
429
430         /*
431          * Check to see if:
432          *  (a) there is anything on the txq to send
433          *  (b) link is up
434          *  (c) link attention events can be processed (fcp ring only)
435          *  (d) IOCB processing is not blocked by the outstanding mbox command.
436          */
437         if (pring->txq_cnt &&
438             (phba->hba_state > LPFC_LINK_DOWN) &&
439             (pring->ringno != phba->sli.fcp_ring ||
440              phba->sli.sli_flag & LPFC_PROCESS_LA) &&
441             !(pring->flag & LPFC_STOP_IOCB_MBX)) {
442
443                 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
444                        (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
445                         lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
446
447                 if (iocb)
448                         lpfc_sli_update_ring(phba, pring);
449                 else
450                         lpfc_sli_update_full_ring(phba, pring);
451         }
452
453         return;
454 }
455
456 /* lpfc_sli_turn_on_ring is only called by lpfc_sli_handle_mb_event below */
457 static void
458 lpfc_sli_turn_on_ring(struct lpfc_hba * phba, int ringno)
459 {
460         struct lpfc_pgp *pgp = &phba->slim2p->mbx.us.s2.port[ringno];
461
462         /* If the ring is active, flag it */
463         if (phba->sli.ring[ringno].cmdringaddr) {
464                 if (phba->sli.ring[ringno].flag & LPFC_STOP_IOCB_MBX) {
465                         phba->sli.ring[ringno].flag &= ~LPFC_STOP_IOCB_MBX;
466                         /*
467                          * Force update of the local copy of cmdGetInx
468                          */
469                         phba->sli.ring[ringno].local_getidx
470                                 = le32_to_cpu(pgp->cmdGetInx);
471                         spin_lock_irq(phba->host->host_lock);
472                         lpfc_sli_resume_iocb(phba, &phba->sli.ring[ringno]);
473                         spin_unlock_irq(phba->host->host_lock);
474                 }
475         }
476 }
477
478 static int
479 lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
480 {
481         uint8_t ret;
482
483         switch (mbxCommand) {
484         case MBX_LOAD_SM:
485         case MBX_READ_NV:
486         case MBX_WRITE_NV:
487         case MBX_RUN_BIU_DIAG:
488         case MBX_INIT_LINK:
489         case MBX_DOWN_LINK:
490         case MBX_CONFIG_LINK:
491         case MBX_CONFIG_RING:
492         case MBX_RESET_RING:
493         case MBX_READ_CONFIG:
494         case MBX_READ_RCONFIG:
495         case MBX_READ_SPARM:
496         case MBX_READ_STATUS:
497         case MBX_READ_RPI:
498         case MBX_READ_XRI:
499         case MBX_READ_REV:
500         case MBX_READ_LNK_STAT:
501         case MBX_REG_LOGIN:
502         case MBX_UNREG_LOGIN:
503         case MBX_READ_LA:
504         case MBX_CLEAR_LA:
505         case MBX_DUMP_MEMORY:
506         case MBX_DUMP_CONTEXT:
507         case MBX_RUN_DIAGS:
508         case MBX_RESTART:
509         case MBX_UPDATE_CFG:
510         case MBX_DOWN_LOAD:
511         case MBX_DEL_LD_ENTRY:
512         case MBX_RUN_PROGRAM:
513         case MBX_SET_MASK:
514         case MBX_SET_SLIM:
515         case MBX_UNREG_D_ID:
516         case MBX_KILL_BOARD:
517         case MBX_CONFIG_FARP:
518         case MBX_BEACON:
519         case MBX_LOAD_AREA:
520         case MBX_RUN_BIU_DIAG64:
521         case MBX_CONFIG_PORT:
522         case MBX_READ_SPARM64:
523         case MBX_READ_RPI64:
524         case MBX_REG_LOGIN64:
525         case MBX_READ_LA64:
526         case MBX_FLASH_WR_ULA:
527         case MBX_SET_DEBUG:
528         case MBX_LOAD_EXP_ROM:
529                 ret = mbxCommand;
530                 break;
531         default:
532                 ret = MBX_SHUTDOWN;
533                 break;
534         }
535         return (ret);
536 }
537 static void
538 lpfc_sli_wake_mbox_wait(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
539 {
540         wait_queue_head_t *pdone_q;
541
542         /*
543          * If pdone_q is empty, the driver thread gave up waiting and
544          * continued running.
545          */
546         pdone_q = (wait_queue_head_t *) pmboxq->context1;
547         if (pdone_q)
548                 wake_up_interruptible(pdone_q);
549         return;
550 }
551
552 void
553 lpfc_sli_def_mbox_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmb)
554 {
555         struct lpfc_dmabuf *mp;
556         mp = (struct lpfc_dmabuf *) (pmb->context1);
557         if (mp) {
558                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
559                 kfree(mp);
560         }
561         mempool_free( pmb, phba->mbox_mem_pool);
562         return;
563 }
564
565 int
566 lpfc_sli_handle_mb_event(struct lpfc_hba * phba)
567 {
568         MAILBOX_t *mbox;
569         MAILBOX_t *pmbox;
570         LPFC_MBOXQ_t *pmb;
571         struct lpfc_sli *psli;
572         int i, rc;
573         uint32_t process_next;
574
575         psli = &phba->sli;
576         /* We should only get here if we are in SLI2 mode */
577         if (!(phba->sli.sli_flag & LPFC_SLI2_ACTIVE)) {
578                 return (1);
579         }
580
581         phba->sli.slistat.mbox_event++;
582
583         /* Get a Mailbox buffer to setup mailbox commands for callback */
584         if ((pmb = phba->sli.mbox_active)) {
585                 pmbox = &pmb->mb;
586                 mbox = &phba->slim2p->mbx;
587
588                 /* First check out the status word */
589                 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof (uint32_t));
590
591                 /* Sanity check to ensure the host owns the mailbox */
592                 if (pmbox->mbxOwner != OWN_HOST) {
593                         /* Lets try for a while */
594                         for (i = 0; i < 10240; i++) {
595                                 /* First copy command data */
596                                 lpfc_sli_pcimem_bcopy(mbox, pmbox,
597                                                         sizeof (uint32_t));
598                                 if (pmbox->mbxOwner == OWN_HOST)
599                                         goto mbout;
600                         }
601                         /* Stray Mailbox Interrupt, mbxCommand <cmd> mbxStatus
602                            <status> */
603                         lpfc_printf_log(phba,
604                                         KERN_ERR,
605                                         LOG_MBOX | LOG_SLI,
606                                         "%d:0304 Stray Mailbox Interrupt "
607                                         "mbxCommand x%x mbxStatus x%x\n",
608                                         phba->brd_no,
609                                         pmbox->mbxCommand,
610                                         pmbox->mbxStatus);
611
612                         spin_lock_irq(phba->host->host_lock);
613                         phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
614                         spin_unlock_irq(phba->host->host_lock);
615                         return (1);
616                 }
617
618               mbout:
619                 del_timer_sync(&phba->sli.mbox_tmo);
620                 phba->work_hba_events &= ~WORKER_MBOX_TMO;
621
622                 /*
623                  * It is a fatal error if unknown mbox command completion.
624                  */
625                 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
626                     MBX_SHUTDOWN) {
627
628                         /* Unknow mailbox command compl */
629                         lpfc_printf_log(phba,
630                                 KERN_ERR,
631                                 LOG_MBOX | LOG_SLI,
632                                 "%d:0323 Unknown Mailbox command %x Cmpl\n",
633                                 phba->brd_no,
634                                 pmbox->mbxCommand);
635                         phba->hba_state = LPFC_HBA_ERROR;
636                         phba->work_hs = HS_FFER3;
637                         lpfc_handle_eratt(phba);
638                         return (0);
639                 }
640
641                 phba->sli.mbox_active = NULL;
642                 if (pmbox->mbxStatus) {
643                         phba->sli.slistat.mbox_stat_err++;
644                         if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
645                                 /* Mbox cmd cmpl error - RETRYing */
646                                 lpfc_printf_log(phba,
647                                         KERN_INFO,
648                                         LOG_MBOX | LOG_SLI,
649                                         "%d:0305 Mbox cmd cmpl error - "
650                                         "RETRYing Data: x%x x%x x%x x%x\n",
651                                         phba->brd_no,
652                                         pmbox->mbxCommand,
653                                         pmbox->mbxStatus,
654                                         pmbox->un.varWords[0],
655                                         phba->hba_state);
656                                 pmbox->mbxStatus = 0;
657                                 pmbox->mbxOwner = OWN_HOST;
658                                 spin_lock_irq(phba->host->host_lock);
659                                 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
660                                 spin_unlock_irq(phba->host->host_lock);
661                                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
662                                 if (rc == MBX_SUCCESS)
663                                         return (0);
664                         }
665                 }
666
667                 /* Mailbox cmd <cmd> Cmpl <cmpl> */
668                 lpfc_printf_log(phba,
669                                 KERN_INFO,
670                                 LOG_MBOX | LOG_SLI,
671                                 "%d:0307 Mailbox cmd x%x Cmpl x%p "
672                                 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x\n",
673                                 phba->brd_no,
674                                 pmbox->mbxCommand,
675                                 pmb->mbox_cmpl,
676                                 *((uint32_t *) pmbox),
677                                 pmbox->un.varWords[0],
678                                 pmbox->un.varWords[1],
679                                 pmbox->un.varWords[2],
680                                 pmbox->un.varWords[3],
681                                 pmbox->un.varWords[4],
682                                 pmbox->un.varWords[5],
683                                 pmbox->un.varWords[6],
684                                 pmbox->un.varWords[7]);
685
686                 if (pmb->mbox_cmpl) {
687                         lpfc_sli_pcimem_bcopy(mbox, pmbox, MAILBOX_CMD_SIZE);
688                         pmb->mbox_cmpl(phba,pmb);
689                 }
690         }
691
692
693         do {
694                 process_next = 0;       /* by default don't loop */
695                 spin_lock_irq(phba->host->host_lock);
696                 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
697
698                 /* Process next mailbox command if there is one */
699                 if ((pmb = lpfc_mbox_get(phba))) {
700                         spin_unlock_irq(phba->host->host_lock);
701                         rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
702                         if (rc == MBX_NOT_FINISHED) {
703                                 pmb->mb.mbxStatus = MBX_NOT_FINISHED;
704                                 pmb->mbox_cmpl(phba,pmb);
705                                 process_next = 1;
706                                 continue;       /* loop back */
707                         }
708                 } else {
709                         spin_unlock_irq(phba->host->host_lock);
710                         /* Turn on IOCB processing */
711                         for (i = 0; i < phba->sli.num_rings; i++) {
712                                 lpfc_sli_turn_on_ring(phba, i);
713                         }
714
715                         /* Free any lpfc_dmabuf's waiting for mbox cmd cmpls */
716                         while (!list_empty(&phba->freebufList)) {
717                                 struct lpfc_dmabuf *mp;
718
719                                 mp = NULL;
720                                 list_remove_head((&phba->freebufList),
721                                                  mp,
722                                                  struct lpfc_dmabuf,
723                                                  list);
724                                 if (mp) {
725                                         lpfc_mbuf_free(phba, mp->virt,
726                                                        mp->phys);
727                                         kfree(mp);
728                                 }
729                         }
730                 }
731
732         } while (process_next);
733
734         return (0);
735 }
736 static int
737 lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
738                             struct lpfc_iocbq *saveq)
739 {
740         IOCB_t           * irsp;
741         WORD5            * w5p;
742         uint32_t           Rctl, Type;
743         uint32_t           match, i;
744
745         match = 0;
746         irsp = &(saveq->iocb);
747         if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX)
748             || (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX)) {
749                 Rctl = FC_ELS_REQ;
750                 Type = FC_ELS_DATA;
751         } else {
752                 w5p =
753                     (WORD5 *) & (saveq->iocb.un.
754                                  ulpWord[5]);
755                 Rctl = w5p->hcsw.Rctl;
756                 Type = w5p->hcsw.Type;
757
758                 /* Firmware Workaround */
759                 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
760                         (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX)) {
761                         Rctl = FC_ELS_REQ;
762                         Type = FC_ELS_DATA;
763                         w5p->hcsw.Rctl = Rctl;
764                         w5p->hcsw.Type = Type;
765                 }
766         }
767         /* unSolicited Responses */
768         if (pring->prt[0].profile) {
769                 (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring, saveq);
770                 match = 1;
771         } else {
772                 /* We must search, based on rctl / type
773                    for the right routine */
774                 for (i = 0; i < pring->num_mask;
775                      i++) {
776                         if ((pring->prt[i].rctl ==
777                              Rctl)
778                             && (pring->prt[i].
779                                 type == Type)) {
780                                 (pring->prt[i].lpfc_sli_rcv_unsol_event)
781                                         (phba, pring, saveq);
782                                 match = 1;
783                                 break;
784                         }
785                 }
786         }
787         if (match == 0) {
788                 /* Unexpected Rctl / Type received */
789                 /* Ring <ringno> handler: unexpected
790                    Rctl <Rctl> Type <Type> received */
791                 lpfc_printf_log(phba,
792                                 KERN_WARNING,
793                                 LOG_SLI,
794                                 "%d:0313 Ring %d handler: unexpected Rctl x%x "
795                                 "Type x%x received \n",
796                                 phba->brd_no,
797                                 pring->ringno,
798                                 Rctl,
799                                 Type);
800         }
801         return(1);
802 }
803
804 static struct lpfc_iocbq *
805 lpfc_sli_iocbq_lookup(struct lpfc_hba * phba,
806                       struct lpfc_sli_ring * pring,
807                       struct lpfc_iocbq * prspiocb)
808 {
809         struct lpfc_iocbq *cmd_iocb = NULL;
810         uint16_t iotag;
811
812         iotag = prspiocb->iocb.ulpIoTag;
813
814         if (iotag != 0 && iotag <= phba->sli.last_iotag) {
815                 cmd_iocb = phba->sli.iocbq_lookup[iotag];
816                 list_del(&cmd_iocb->list);
817                 pring->txcmplq_cnt--;
818                 return cmd_iocb;
819         }
820
821         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
822                         "%d:0317 iotag x%x is out off "
823                         "range: max iotag x%x wd0 x%x\n",
824                         phba->brd_no, iotag,
825                         phba->sli.last_iotag,
826                         *(((uint32_t *) &prspiocb->iocb) + 7));
827         return NULL;
828 }
829
830 static int
831 lpfc_sli_process_sol_iocb(struct lpfc_hba * phba, struct lpfc_sli_ring * pring,
832                           struct lpfc_iocbq *saveq)
833 {
834         struct lpfc_iocbq * cmdiocbp;
835         int rc = 1;
836         unsigned long iflag;
837
838         /* Based on the iotag field, get the cmd IOCB from the txcmplq */
839         spin_lock_irqsave(phba->host->host_lock, iflag);
840         cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
841         if (cmdiocbp) {
842                 if (cmdiocbp->iocb_cmpl) {
843                         /*
844                          * Post all ELS completions to the worker thread.
845                          * All other are passed to the completion callback.
846                          */
847                         if (pring->ringno == LPFC_ELS_RING) {
848                                 spin_unlock_irqrestore(phba->host->host_lock,
849                                                        iflag);
850                                 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
851                                 spin_lock_irqsave(phba->host->host_lock, iflag);
852                         }
853                         else {
854                                 spin_unlock_irqrestore(phba->host->host_lock,
855                                                        iflag);
856                                 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
857                                 spin_lock_irqsave(phba->host->host_lock, iflag);
858                         }
859                 } else
860                         lpfc_sli_release_iocbq(phba, cmdiocbp);
861         } else {
862                 /*
863                  * Unknown initiating command based on the response iotag.
864                  * This could be the case on the ELS ring because of
865                  * lpfc_els_abort().
866                  */
867                 if (pring->ringno != LPFC_ELS_RING) {
868                         /*
869                          * Ring <ringno> handler: unexpected completion IoTag
870                          * <IoTag>
871                          */
872                         lpfc_printf_log(phba,
873                                 KERN_WARNING,
874                                 LOG_SLI,
875                                 "%d:0322 Ring %d handler: unexpected "
876                                 "completion IoTag x%x Data: x%x x%x x%x x%x\n",
877                                 phba->brd_no,
878                                 pring->ringno,
879                                 saveq->iocb.ulpIoTag,
880                                 saveq->iocb.ulpStatus,
881                                 saveq->iocb.un.ulpWord[4],
882                                 saveq->iocb.ulpCommand,
883                                 saveq->iocb.ulpContext);
884                 }
885         }
886
887         spin_unlock_irqrestore(phba->host->host_lock, iflag);
888         return rc;
889 }
890
891 static void lpfc_sli_rsp_pointers_error(struct lpfc_hba * phba,
892                                         struct lpfc_sli_ring * pring)
893 {
894         struct lpfc_pgp *pgp = &phba->slim2p->mbx.us.s2.port[pring->ringno];
895         /*
896          * Ring <ringno> handler: portRspPut <portRspPut> is bigger then
897          * rsp ring <portRspMax>
898          */
899         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
900                         "%d:0312 Ring %d handler: portRspPut %d "
901                         "is bigger then rsp ring %d\n",
902                         phba->brd_no, pring->ringno,
903                         le32_to_cpu(pgp->rspPutInx),
904                         pring->numRiocb);
905
906         phba->hba_state = LPFC_HBA_ERROR;
907
908         /*
909          * All error attention handlers are posted to
910          * worker thread
911          */
912         phba->work_ha |= HA_ERATT;
913         phba->work_hs = HS_FFER3;
914         if (phba->work_wait)
915                 wake_up(phba->work_wait);
916
917         return;
918 }
919
920 void lpfc_sli_poll_fcp_ring(struct lpfc_hba * phba)
921 {
922         struct lpfc_sli      * psli   = &phba->sli;
923         struct lpfc_sli_ring * pring = &psli->ring[LPFC_FCP_RING];
924         IOCB_t *irsp = NULL;
925         IOCB_t *entry = NULL;
926         struct lpfc_iocbq *cmdiocbq = NULL;
927         struct lpfc_iocbq rspiocbq;
928         struct lpfc_pgp *pgp;
929         uint32_t status;
930         uint32_t portRspPut, portRspMax;
931         int type;
932         uint32_t rsp_cmpl = 0;
933         void __iomem *to_slim;
934         uint32_t ha_copy;
935
936         pring->stats.iocb_event++;
937
938         /* The driver assumes SLI-2 mode */
939         pgp =  &phba->slim2p->mbx.us.s2.port[pring->ringno];
940
941         /*
942          * The next available response entry should never exceed the maximum
943          * entries.  If it does, treat it as an adapter hardware error.
944          */
945         portRspMax = pring->numRiocb;
946         portRspPut = le32_to_cpu(pgp->rspPutInx);
947         if (unlikely(portRspPut >= portRspMax)) {
948                 lpfc_sli_rsp_pointers_error(phba, pring);
949                 return;
950         }
951
952         rmb();
953         while (pring->rspidx != portRspPut) {
954
955                 entry = IOCB_ENTRY(pring->rspringaddr, pring->rspidx);
956
957                 if (++pring->rspidx >= portRspMax)
958                         pring->rspidx = 0;
959
960                 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
961                                       (uint32_t *) &rspiocbq.iocb,
962                                       sizeof (IOCB_t));
963                 irsp = &rspiocbq.iocb;
964                 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
965                 pring->stats.iocb_rsp++;
966                 rsp_cmpl++;
967
968                 if (unlikely(irsp->ulpStatus)) {
969                         /* Rsp ring <ringno> error: IOCB */
970                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
971                                         "%d:0326 Rsp Ring %d error: IOCB Data: "
972                                         "x%x x%x x%x x%x x%x x%x x%x x%x\n",
973                                         phba->brd_no, pring->ringno,
974                                         irsp->un.ulpWord[0],
975                                         irsp->un.ulpWord[1],
976                                         irsp->un.ulpWord[2],
977                                         irsp->un.ulpWord[3],
978                                         irsp->un.ulpWord[4],
979                                         irsp->un.ulpWord[5],
980                                         *(((uint32_t *) irsp) + 6),
981                                         *(((uint32_t *) irsp) + 7));
982                 }
983
984                 switch (type) {
985                 case LPFC_ABORT_IOCB:
986                 case LPFC_SOL_IOCB:
987                         /*
988                          * Idle exchange closed via ABTS from port.  No iocb
989                          * resources need to be recovered.
990                          */
991                         if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
992                                 printk(KERN_INFO "%s: IOCB cmd 0x%x processed."
993                                        " Skipping completion\n", __FUNCTION__,
994                                        irsp->ulpCommand);
995                                 break;
996                         }
997
998                         cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
999                                                          &rspiocbq);
1000                         if ((cmdiocbq) && (cmdiocbq->iocb_cmpl)) {
1001                                 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
1002                                                       &rspiocbq);
1003                         }
1004                         break;
1005                 default:
1006                         if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
1007                                 char adaptermsg[LPFC_MAX_ADPTMSG];
1008                                 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
1009                                 memcpy(&adaptermsg[0], (uint8_t *) irsp,
1010                                        MAX_MSG_DATA);
1011                                 dev_warn(&((phba->pcidev)->dev), "lpfc%d: %s",
1012                                          phba->brd_no, adaptermsg);
1013                         } else {
1014                                 /* Unknown IOCB command */
1015                                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
1016                                                 "%d:0321 Unknown IOCB command "
1017                                                 "Data: x%x, x%x x%x x%x x%x\n",
1018                                                 phba->brd_no, type,
1019                                                 irsp->ulpCommand,
1020                                                 irsp->ulpStatus,
1021                                                 irsp->ulpIoTag,
1022                                                 irsp->ulpContext);
1023                         }
1024                         break;
1025                 }
1026
1027                 /*
1028                  * The response IOCB has been processed.  Update the ring
1029                  * pointer in SLIM.  If the port response put pointer has not
1030                  * been updated, sync the pgp->rspPutInx and fetch the new port
1031                  * response put pointer.
1032                  */
1033                 to_slim = phba->MBslimaddr +
1034                         (SLIMOFF + (pring->ringno * 2) + 1) * 4;
1035                 writeb(pring->rspidx, to_slim);
1036
1037                 if (pring->rspidx == portRspPut)
1038                         portRspPut = le32_to_cpu(pgp->rspPutInx);
1039         }
1040
1041         ha_copy = readl(phba->HAregaddr);
1042         ha_copy >>= (LPFC_FCP_RING * 4);
1043
1044         if ((rsp_cmpl > 0) && (ha_copy & HA_R0RE_REQ)) {
1045                 pring->stats.iocb_rsp_full++;
1046                 status = ((CA_R0ATT | CA_R0RE_RSP) << (LPFC_FCP_RING * 4));
1047                 writel(status, phba->CAregaddr);
1048                 readl(phba->CAregaddr);
1049         }
1050         if ((ha_copy & HA_R0CE_RSP) &&
1051             (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
1052                 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
1053                 pring->stats.iocb_cmd_empty++;
1054
1055                 /* Force update of the local copy of cmdGetInx */
1056                 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
1057                 lpfc_sli_resume_iocb(phba, pring);
1058
1059                 if ((pring->lpfc_sli_cmd_available))
1060                         (pring->lpfc_sli_cmd_available) (phba, pring);
1061
1062         }
1063
1064         return;
1065 }
1066
1067 /*
1068  * This routine presumes LPFC_FCP_RING handling and doesn't bother
1069  * to check it explicitly.
1070  */
1071 static int
1072 lpfc_sli_handle_fast_ring_event(struct lpfc_hba * phba,
1073                                 struct lpfc_sli_ring * pring, uint32_t mask)
1074 {
1075         struct lpfc_pgp *pgp = &phba->slim2p->mbx.us.s2.port[pring->ringno];
1076         IOCB_t *irsp = NULL;
1077         IOCB_t *entry = NULL;
1078         struct lpfc_iocbq *cmdiocbq = NULL;
1079         struct lpfc_iocbq rspiocbq;
1080         uint32_t status;
1081         uint32_t portRspPut, portRspMax;
1082         int rc = 1;
1083         lpfc_iocb_type type;
1084         unsigned long iflag;
1085         uint32_t rsp_cmpl = 0;
1086         void __iomem  *to_slim;
1087
1088         spin_lock_irqsave(phba->host->host_lock, iflag);
1089         pring->stats.iocb_event++;
1090
1091         /*
1092          * The next available response entry should never exceed the maximum
1093          * entries.  If it does, treat it as an adapter hardware error.
1094          */
1095         portRspMax = pring->numRiocb;
1096         portRspPut = le32_to_cpu(pgp->rspPutInx);
1097         if (unlikely(portRspPut >= portRspMax)) {
1098                 lpfc_sli_rsp_pointers_error(phba, pring);
1099                 spin_unlock_irqrestore(phba->host->host_lock, iflag);
1100                 return 1;
1101         }
1102
1103         rmb();
1104         while (pring->rspidx != portRspPut) {
1105                 /*
1106                  * Fetch an entry off the ring and copy it into a local data
1107                  * structure.  The copy involves a byte-swap since the
1108                  * network byte order and pci byte orders are different.
1109                  */
1110                 entry = IOCB_ENTRY(pring->rspringaddr, pring->rspidx);
1111
1112                 if (++pring->rspidx >= portRspMax)
1113                         pring->rspidx = 0;
1114
1115                 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
1116                                       (uint32_t *) &rspiocbq.iocb,
1117                                       sizeof (IOCB_t));
1118                 irsp = &rspiocbq.iocb;
1119
1120                 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
1121                 pring->stats.iocb_rsp++;
1122                 rsp_cmpl++;
1123
1124                 if (unlikely(irsp->ulpStatus)) {
1125                         /* Rsp ring <ringno> error: IOCB */
1126                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
1127                                 "%d:0326 Rsp Ring %d error: IOCB Data: "
1128                                 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
1129                                 phba->brd_no, pring->ringno,
1130                                 irsp->un.ulpWord[0], irsp->un.ulpWord[1],
1131                                 irsp->un.ulpWord[2], irsp->un.ulpWord[3],
1132                                 irsp->un.ulpWord[4], irsp->un.ulpWord[5],
1133                                 *(((uint32_t *) irsp) + 6),
1134                                 *(((uint32_t *) irsp) + 7));
1135                 }
1136
1137                 switch (type) {
1138                 case LPFC_ABORT_IOCB:
1139                 case LPFC_SOL_IOCB:
1140                         /*
1141                          * Idle exchange closed via ABTS from port.  No iocb
1142                          * resources need to be recovered.
1143                          */
1144                         if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
1145                                 printk(KERN_INFO "%s: IOCB cmd 0x%x processed. "
1146                                        "Skipping completion\n", __FUNCTION__,
1147                                        irsp->ulpCommand);
1148                                 break;
1149                         }
1150
1151                         cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
1152                                                          &rspiocbq);
1153                         if ((cmdiocbq) && (cmdiocbq->iocb_cmpl)) {
1154                                 spin_unlock_irqrestore(
1155                                        phba->host->host_lock, iflag);
1156                                 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
1157                                                       &rspiocbq);
1158                                 spin_lock_irqsave(phba->host->host_lock,
1159                                                   iflag);
1160                         }
1161                         break;
1162                 default:
1163                         if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
1164                                 char adaptermsg[LPFC_MAX_ADPTMSG];
1165                                 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
1166                                 memcpy(&adaptermsg[0], (uint8_t *) irsp,
1167                                        MAX_MSG_DATA);
1168                                 dev_warn(&((phba->pcidev)->dev), "lpfc%d: %s",
1169                                          phba->brd_no, adaptermsg);
1170                         } else {
1171                                 /* Unknown IOCB command */
1172                                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
1173                                         "%d:0321 Unknown IOCB command "
1174                                         "Data: x%x, x%x x%x x%x x%x\n",
1175                                         phba->brd_no, type, irsp->ulpCommand,
1176                                         irsp->ulpStatus, irsp->ulpIoTag,
1177                                         irsp->ulpContext);
1178                         }
1179                         break;
1180                 }
1181
1182                 /*
1183                  * The response IOCB has been processed.  Update the ring
1184                  * pointer in SLIM.  If the port response put pointer has not
1185                  * been updated, sync the pgp->rspPutInx and fetch the new port
1186                  * response put pointer.
1187                  */
1188                 to_slim = phba->MBslimaddr +
1189                         (SLIMOFF + (pring->ringno * 2) + 1) * 4;
1190                 writel(pring->rspidx, to_slim);
1191
1192                 if (pring->rspidx == portRspPut)
1193                         portRspPut = le32_to_cpu(pgp->rspPutInx);
1194         }
1195
1196         if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
1197                 pring->stats.iocb_rsp_full++;
1198                 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
1199                 writel(status, phba->CAregaddr);
1200                 readl(phba->CAregaddr);
1201         }
1202         if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
1203                 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
1204                 pring->stats.iocb_cmd_empty++;
1205
1206                 /* Force update of the local copy of cmdGetInx */
1207                 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
1208                 lpfc_sli_resume_iocb(phba, pring);
1209
1210                 if ((pring->lpfc_sli_cmd_available))
1211                         (pring->lpfc_sli_cmd_available) (phba, pring);
1212
1213         }
1214
1215         spin_unlock_irqrestore(phba->host->host_lock, iflag);
1216         return rc;
1217 }
1218
1219
1220 int
1221 lpfc_sli_handle_slow_ring_event(struct lpfc_hba * phba,
1222                            struct lpfc_sli_ring * pring, uint32_t mask)
1223 {
1224         IOCB_t *entry;
1225         IOCB_t *irsp = NULL;
1226         struct lpfc_iocbq *rspiocbp = NULL;
1227         struct lpfc_iocbq *next_iocb;
1228         struct lpfc_iocbq *cmdiocbp;
1229         struct lpfc_iocbq *saveq;
1230         struct lpfc_pgp *pgp = &phba->slim2p->mbx.us.s2.port[pring->ringno];
1231         uint8_t iocb_cmd_type;
1232         lpfc_iocb_type type;
1233         uint32_t status, free_saveq;
1234         uint32_t portRspPut, portRspMax;
1235         int rc = 1;
1236         unsigned long iflag;
1237         void __iomem  *to_slim;
1238
1239         spin_lock_irqsave(phba->host->host_lock, iflag);
1240         pring->stats.iocb_event++;
1241
1242         /*
1243          * The next available response entry should never exceed the maximum
1244          * entries.  If it does, treat it as an adapter hardware error.
1245          */
1246         portRspMax = pring->numRiocb;
1247         portRspPut = le32_to_cpu(pgp->rspPutInx);
1248         if (portRspPut >= portRspMax) {
1249                 /*
1250                  * Ring <ringno> handler: portRspPut <portRspPut> is bigger then
1251                  * rsp ring <portRspMax>
1252                  */
1253                 lpfc_printf_log(phba,
1254                                 KERN_ERR,
1255                                 LOG_SLI,
1256                                 "%d:0312 Ring %d handler: portRspPut %d "
1257                                 "is bigger then rsp ring %d\n",
1258                                 phba->brd_no,
1259                                 pring->ringno, portRspPut, portRspMax);
1260
1261                 phba->hba_state = LPFC_HBA_ERROR;
1262                 spin_unlock_irqrestore(phba->host->host_lock, iflag);
1263
1264                 phba->work_hs = HS_FFER3;
1265                 lpfc_handle_eratt(phba);
1266
1267                 return 1;
1268         }
1269
1270         rmb();
1271         while (pring->rspidx != portRspPut) {
1272                 /*
1273                  * Build a completion list and call the appropriate handler.
1274                  * The process is to get the next available response iocb, get
1275                  * a free iocb from the list, copy the response data into the
1276                  * free iocb, insert to the continuation list, and update the
1277                  * next response index to slim.  This process makes response
1278                  * iocb's in the ring available to DMA as fast as possible but
1279                  * pays a penalty for a copy operation.  Since the iocb is
1280                  * only 32 bytes, this penalty is considered small relative to
1281                  * the PCI reads for register values and a slim write.  When
1282                  * the ulpLe field is set, the entire Command has been
1283                  * received.
1284                  */
1285                 entry = IOCB_ENTRY(pring->rspringaddr, pring->rspidx);
1286                 rspiocbp = lpfc_sli_get_iocbq(phba);
1287                 if (rspiocbp == NULL) {
1288                         printk(KERN_ERR "%s: out of buffers! Failing "
1289                                "completion.\n", __FUNCTION__);
1290                         break;
1291                 }
1292
1293                 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb, sizeof (IOCB_t));
1294                 irsp = &rspiocbp->iocb;
1295
1296                 if (++pring->rspidx >= portRspMax)
1297                         pring->rspidx = 0;
1298
1299                 to_slim = phba->MBslimaddr + (SLIMOFF + (pring->ringno * 2)
1300                                               + 1) * 4;
1301                 writel(pring->rspidx, to_slim);
1302
1303                 if (list_empty(&(pring->iocb_continueq))) {
1304                         list_add(&rspiocbp->list, &(pring->iocb_continueq));
1305                 } else {
1306                         list_add_tail(&rspiocbp->list,
1307                                       &(pring->iocb_continueq));
1308                 }
1309
1310                 pring->iocb_continueq_cnt++;
1311                 if (irsp->ulpLe) {
1312                         /*
1313                          * By default, the driver expects to free all resources
1314                          * associated with this iocb completion.
1315                          */
1316                         free_saveq = 1;
1317                         saveq = list_get_first(&pring->iocb_continueq,
1318                                                struct lpfc_iocbq, list);
1319                         irsp = &(saveq->iocb);
1320                         list_del_init(&pring->iocb_continueq);
1321                         pring->iocb_continueq_cnt = 0;
1322
1323                         pring->stats.iocb_rsp++;
1324
1325                         if (irsp->ulpStatus) {
1326                                 /* Rsp ring <ringno> error: IOCB */
1327                                 lpfc_printf_log(phba,
1328                                         KERN_WARNING,
1329                                         LOG_SLI,
1330                                         "%d:0328 Rsp Ring %d error: IOCB Data: "
1331                                         "x%x x%x x%x x%x x%x x%x x%x x%x\n",
1332                                         phba->brd_no,
1333                                         pring->ringno,
1334                                         irsp->un.ulpWord[0],
1335                                         irsp->un.ulpWord[1],
1336                                         irsp->un.ulpWord[2],
1337                                         irsp->un.ulpWord[3],
1338                                         irsp->un.ulpWord[4],
1339                                         irsp->un.ulpWord[5],
1340                                         *(((uint32_t *) irsp) + 6),
1341                                         *(((uint32_t *) irsp) + 7));
1342                         }
1343
1344                         /*
1345                          * Fetch the IOCB command type and call the correct
1346                          * completion routine.  Solicited and Unsolicited
1347                          * IOCBs on the ELS ring get freed back to the
1348                          * lpfc_iocb_list by the discovery kernel thread.
1349                          */
1350                         iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
1351                         type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
1352                         if (type == LPFC_SOL_IOCB) {
1353                                 spin_unlock_irqrestore(phba->host->host_lock,
1354                                                        iflag);
1355                                 rc = lpfc_sli_process_sol_iocb(phba, pring,
1356                                         saveq);
1357                                 spin_lock_irqsave(phba->host->host_lock, iflag);
1358                         } else if (type == LPFC_UNSOL_IOCB) {
1359                                 spin_unlock_irqrestore(phba->host->host_lock,
1360                                                        iflag);
1361                                 rc = lpfc_sli_process_unsol_iocb(phba, pring,
1362                                         saveq);
1363                                 spin_lock_irqsave(phba->host->host_lock, iflag);
1364                         } else if (type == LPFC_ABORT_IOCB) {
1365                                 if ((irsp->ulpCommand != CMD_XRI_ABORTED_CX) &&
1366                                     ((cmdiocbp =
1367                                       lpfc_sli_iocbq_lookup(phba, pring,
1368                                                             saveq)))) {
1369                                         /* Call the specified completion
1370                                            routine */
1371                                         if (cmdiocbp->iocb_cmpl) {
1372                                                 spin_unlock_irqrestore(
1373                                                        phba->host->host_lock,
1374                                                        iflag);
1375                                                 (cmdiocbp->iocb_cmpl) (phba,
1376                                                              cmdiocbp, saveq);
1377                                                 spin_lock_irqsave(
1378                                                           phba->host->host_lock,
1379                                                           iflag);
1380                                         } else
1381                                                 lpfc_sli_release_iocbq(phba,
1382                                                                       cmdiocbp);
1383                                 }
1384                         } else if (type == LPFC_UNKNOWN_IOCB) {
1385                                 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
1386
1387                                         char adaptermsg[LPFC_MAX_ADPTMSG];
1388
1389                                         memset(adaptermsg, 0,
1390                                                LPFC_MAX_ADPTMSG);
1391                                         memcpy(&adaptermsg[0], (uint8_t *) irsp,
1392                                                MAX_MSG_DATA);
1393                                         dev_warn(&((phba->pcidev)->dev),
1394                                                  "lpfc%d: %s",
1395                                                  phba->brd_no, adaptermsg);
1396                                 } else {
1397                                         /* Unknown IOCB command */
1398                                         lpfc_printf_log(phba,
1399                                                 KERN_ERR,
1400                                                 LOG_SLI,
1401                                                 "%d:0321 Unknown IOCB command "
1402                                                 "Data: x%x x%x x%x x%x\n",
1403                                                 phba->brd_no,
1404                                                 irsp->ulpCommand,
1405                                                 irsp->ulpStatus,
1406                                                 irsp->ulpIoTag,
1407                                                 irsp->ulpContext);
1408                                 }
1409                         }
1410
1411                         if (free_saveq) {
1412                                 if (!list_empty(&saveq->list)) {
1413                                         list_for_each_entry_safe(rspiocbp,
1414                                                                  next_iocb,
1415                                                                  &saveq->list,
1416                                                                  list) {
1417                                                 lpfc_sli_release_iocbq(phba,
1418                                                                      rspiocbp);
1419                                         }
1420                                 }
1421
1422                                 lpfc_sli_release_iocbq(phba, saveq);
1423                         }
1424                 }
1425
1426                 /*
1427                  * If the port response put pointer has not been updated, sync
1428                  * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
1429                  * response put pointer.
1430                  */
1431                 if (pring->rspidx == portRspPut) {
1432                         portRspPut = le32_to_cpu(pgp->rspPutInx);
1433                 }
1434         } /* while (pring->rspidx != portRspPut) */
1435
1436         if ((rspiocbp != 0) && (mask & HA_R0RE_REQ)) {
1437                 /* At least one response entry has been freed */
1438                 pring->stats.iocb_rsp_full++;
1439                 /* SET RxRE_RSP in Chip Att register */
1440                 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
1441                 writel(status, phba->CAregaddr);
1442                 readl(phba->CAregaddr); /* flush */
1443         }
1444         if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
1445                 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
1446                 pring->stats.iocb_cmd_empty++;
1447
1448                 /* Force update of the local copy of cmdGetInx */
1449                 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
1450                 lpfc_sli_resume_iocb(phba, pring);
1451
1452                 if ((pring->lpfc_sli_cmd_available))
1453                         (pring->lpfc_sli_cmd_available) (phba, pring);
1454
1455         }
1456
1457         spin_unlock_irqrestore(phba->host->host_lock, iflag);
1458         return rc;
1459 }
1460
1461 int
1462 lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1463 {
1464         struct lpfc_iocbq *iocb, *next_iocb;
1465         IOCB_t *icmd = NULL, *cmd = NULL;
1466         int errcnt;
1467
1468         errcnt = 0;
1469
1470         /* Error everything on txq and txcmplq
1471          * First do the txq.
1472          */
1473         spin_lock_irq(phba->host->host_lock);
1474         list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
1475                 list_del_init(&iocb->list);
1476                 if (iocb->iocb_cmpl) {
1477                         icmd = &iocb->iocb;
1478                         icmd->ulpStatus = IOSTAT_LOCAL_REJECT;
1479                         icmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
1480                         spin_unlock_irq(phba->host->host_lock);
1481                         (iocb->iocb_cmpl) (phba, iocb, iocb);
1482                         spin_lock_irq(phba->host->host_lock);
1483                 } else
1484                         lpfc_sli_release_iocbq(phba, iocb);
1485         }
1486         pring->txq_cnt = 0;
1487         INIT_LIST_HEAD(&(pring->txq));
1488
1489         /* Next issue ABTS for everything on the txcmplq */
1490         list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
1491                 cmd = &iocb->iocb;
1492
1493                 /*
1494                  * Imediate abort of IOCB, deque and call compl
1495                  */
1496
1497                 list_del_init(&iocb->list);
1498                 pring->txcmplq_cnt--;
1499
1500                 if (iocb->iocb_cmpl) {
1501                         cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
1502                         cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
1503                         spin_unlock_irq(phba->host->host_lock);
1504                         (iocb->iocb_cmpl) (phba, iocb, iocb);
1505                         spin_lock_irq(phba->host->host_lock);
1506                 } else
1507                         lpfc_sli_release_iocbq(phba, iocb);
1508         }
1509
1510         INIT_LIST_HEAD(&pring->txcmplq);
1511         pring->txcmplq_cnt = 0;
1512         spin_unlock_irq(phba->host->host_lock);
1513
1514         return errcnt;
1515 }
1516
1517 int
1518 lpfc_sli_brdready(struct lpfc_hba * phba, uint32_t mask)
1519 {
1520         uint32_t status;
1521         int i = 0;
1522         int retval = 0;
1523
1524         /* Read the HBA Host Status Register */
1525         status = readl(phba->HSregaddr);
1526
1527         /*
1528          * Check status register every 100ms for 5 retries, then every
1529          * 500ms for 5, then every 2.5 sec for 5, then reset board and
1530          * every 2.5 sec for 4.
1531          * Break our of the loop if errors occurred during init.
1532          */
1533         while (((status & mask) != mask) &&
1534                !(status & HS_FFERM) &&
1535                i++ < 20) {
1536
1537                 if (i <= 5)
1538                         msleep(10);
1539                 else if (i <= 10)
1540                         msleep(500);
1541                 else
1542                         msleep(2500);
1543
1544                 if (i == 15) {
1545                         phba->hba_state = LPFC_STATE_UNKNOWN; /* Do post */
1546                         lpfc_sli_brdrestart(phba);
1547                 }
1548                 /* Read the HBA Host Status Register */
1549                 status = readl(phba->HSregaddr);
1550         }
1551
1552         /* Check to see if any errors occurred during init */
1553         if ((status & HS_FFERM) || (i >= 20)) {
1554                 phba->hba_state = LPFC_HBA_ERROR;
1555                 retval = 1;
1556         }
1557
1558         return retval;
1559 }
1560
1561 int
1562 lpfc_sli_brdkill(struct lpfc_hba * phba)
1563 {
1564         struct lpfc_sli *psli;
1565         LPFC_MBOXQ_t *pmb;
1566         uint32_t status;
1567         uint32_t ha_copy;
1568         int retval;
1569         int i = 0;
1570
1571         psli = &phba->sli;
1572
1573         /* Kill HBA */
1574         lpfc_printf_log(phba,
1575                 KERN_INFO,
1576                 LOG_SLI,
1577                 "%d:0329 Kill HBA Data: x%x x%x\n",
1578                 phba->brd_no,
1579                 phba->hba_state,
1580                 psli->sli_flag);
1581
1582         if ((pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
1583                                                   GFP_ATOMIC)) == 0) {
1584                 return 1;
1585         }
1586
1587         /* Disable the error attention */
1588         spin_lock_irq(phba->host->host_lock);
1589         status = readl(phba->HCregaddr);
1590         status &= ~HC_ERINT_ENA;
1591         writel(status, phba->HCregaddr);
1592         readl(phba->HCregaddr); /* flush */
1593         spin_unlock_irq(phba->host->host_lock);
1594
1595         lpfc_kill_board(phba, pmb);
1596         pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
1597         retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
1598
1599         if (retval != MBX_SUCCESS) {
1600                 if (retval != MBX_BUSY)
1601                         mempool_free(pmb, phba->mbox_mem_pool);
1602                 return 1;
1603         }
1604
1605         mempool_free(pmb, phba->mbox_mem_pool);
1606
1607         /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
1608          * attention every 100ms for 3 seconds. If we don't get ERATT after
1609          * 3 seconds we still set HBA_ERROR state because the status of the
1610          * board is now undefined.
1611          */
1612         ha_copy = readl(phba->HAregaddr);
1613
1614         while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
1615                 mdelay(100);
1616                 ha_copy = readl(phba->HAregaddr);
1617         }
1618
1619         del_timer_sync(&psli->mbox_tmo);
1620
1621         spin_lock_irq(phba->host->host_lock);
1622         psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
1623         spin_unlock_irq(phba->host->host_lock);
1624
1625         psli->mbox_active = NULL;
1626         lpfc_hba_down_post(phba);
1627         phba->hba_state = LPFC_HBA_ERROR;
1628
1629         return (ha_copy & HA_ERATT ? 0 : 1);
1630 }
1631
1632 int
1633 lpfc_sli_brdreset(struct lpfc_hba * phba)
1634 {
1635         struct lpfc_sli *psli;
1636         struct lpfc_sli_ring *pring;
1637         uint16_t cfg_value;
1638         int i;
1639
1640         psli = &phba->sli;
1641
1642         /* Reset HBA */
1643         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
1644                         "%d:0325 Reset HBA Data: x%x x%x\n", phba->brd_no,
1645                         phba->hba_state, psli->sli_flag);
1646
1647         /* perform board reset */
1648         phba->fc_eventTag = 0;
1649         phba->fc_myDID = 0;
1650         phba->fc_prevDID = 0;
1651
1652         psli->sli_flag = 0;
1653
1654         /* Turn off parity checking and serr during the physical reset */
1655         pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
1656         pci_write_config_word(phba->pcidev, PCI_COMMAND,
1657                               (cfg_value &
1658                                ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
1659
1660         /* Now toggle INITFF bit in the Host Control Register */
1661         writel(HC_INITFF, phba->HCregaddr);
1662         mdelay(1);
1663         readl(phba->HCregaddr); /* flush */
1664         writel(0, phba->HCregaddr);
1665         readl(phba->HCregaddr); /* flush */
1666
1667         /* Restore PCI cmd register */
1668         pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
1669
1670         /* Initialize relevant SLI info */
1671         for (i = 0; i < psli->num_rings; i++) {
1672                 pring = &psli->ring[i];
1673                 pring->flag = 0;
1674                 pring->rspidx = 0;
1675                 pring->next_cmdidx  = 0;
1676                 pring->local_getidx = 0;
1677                 pring->cmdidx = 0;
1678                 pring->missbufcnt = 0;
1679         }
1680
1681         phba->hba_state = LPFC_WARM_START;
1682         return 0;
1683 }
1684
1685 int
1686 lpfc_sli_brdrestart(struct lpfc_hba * phba)
1687 {
1688         MAILBOX_t *mb;
1689         struct lpfc_sli *psli;
1690         uint16_t skip_post;
1691         volatile uint32_t word0;
1692         void __iomem *to_slim;
1693
1694         spin_lock_irq(phba->host->host_lock);
1695
1696         psli = &phba->sli;
1697
1698         /* Restart HBA */
1699         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
1700                         "%d:0328 Restart HBA Data: x%x x%x\n", phba->brd_no,
1701                         phba->hba_state, psli->sli_flag);
1702
1703         word0 = 0;
1704         mb = (MAILBOX_t *) &word0;
1705         mb->mbxCommand = MBX_RESTART;
1706         mb->mbxHc = 1;
1707
1708         to_slim = phba->MBslimaddr;
1709         writel(*(uint32_t *) mb, to_slim);
1710         readl(to_slim); /* flush */
1711
1712         /* Only skip post after fc_ffinit is completed */
1713         if (phba->hba_state) {
1714                 skip_post = 1;
1715                 word0 = 1;      /* This is really setting up word1 */
1716         } else {
1717                 skip_post = 0;
1718                 word0 = 0;      /* This is really setting up word1 */
1719         }
1720         to_slim = (uint8_t *) phba->MBslimaddr + sizeof (uint32_t);
1721         writel(*(uint32_t *) mb, to_slim);
1722         readl(to_slim); /* flush */
1723
1724         lpfc_sli_brdreset(phba);
1725
1726         phba->hba_state = LPFC_INIT_START;
1727
1728         spin_unlock_irq(phba->host->host_lock);
1729
1730         if (skip_post)
1731                 mdelay(100);
1732         else
1733                 mdelay(2000);
1734
1735         lpfc_hba_down_post(phba);
1736
1737         return 0;
1738 }
1739
1740 static int
1741 lpfc_sli_chipset_init(struct lpfc_hba *phba)
1742 {
1743         uint32_t status, i = 0;
1744
1745         /* Read the HBA Host Status Register */
1746         status = readl(phba->HSregaddr);
1747
1748         /* Check status register to see what current state is */
1749         i = 0;
1750         while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
1751
1752                 /* Check every 100ms for 5 retries, then every 500ms for 5, then
1753                  * every 2.5 sec for 5, then reset board and every 2.5 sec for
1754                  * 4.
1755                  */
1756                 if (i++ >= 20) {
1757                         /* Adapter failed to init, timeout, status reg
1758                            <status> */
1759                         lpfc_printf_log(phba,
1760                                         KERN_ERR,
1761                                         LOG_INIT,
1762                                         "%d:0436 Adapter failed to init, "
1763                                         "timeout, status reg x%x\n",
1764                                         phba->brd_no,
1765                                         status);
1766                         phba->hba_state = LPFC_HBA_ERROR;
1767                         return -ETIMEDOUT;
1768                 }
1769
1770                 /* Check to see if any errors occurred during init */
1771                 if (status & HS_FFERM) {
1772                         /* ERROR: During chipset initialization */
1773                         /* Adapter failed to init, chipset, status reg
1774                            <status> */
1775                         lpfc_printf_log(phba,
1776                                         KERN_ERR,
1777                                         LOG_INIT,
1778                                         "%d:0437 Adapter failed to init, "
1779                                         "chipset, status reg x%x\n",
1780                                         phba->brd_no,
1781                                         status);
1782                         phba->hba_state = LPFC_HBA_ERROR;
1783                         return -EIO;
1784                 }
1785
1786                 if (i <= 5) {
1787                         msleep(10);
1788                 } else if (i <= 10) {
1789                         msleep(500);
1790                 } else {
1791                         msleep(2500);
1792                 }
1793
1794                 if (i == 15) {
1795                         phba->hba_state = LPFC_STATE_UNKNOWN; /* Do post */
1796                         lpfc_sli_brdrestart(phba);
1797                 }
1798                 /* Read the HBA Host Status Register */
1799                 status = readl(phba->HSregaddr);
1800         }
1801
1802         /* Check to see if any errors occurred during init */
1803         if (status & HS_FFERM) {
1804                 /* ERROR: During chipset initialization */
1805                 /* Adapter failed to init, chipset, status reg <status> */
1806                 lpfc_printf_log(phba,
1807                                 KERN_ERR,
1808                                 LOG_INIT,
1809                                 "%d:0438 Adapter failed to init, chipset, "
1810                                 "status reg x%x\n",
1811                                 phba->brd_no,
1812                                 status);
1813                 phba->hba_state = LPFC_HBA_ERROR;
1814                 return -EIO;
1815         }
1816
1817         /* Clear all interrupt enable conditions */
1818         writel(0, phba->HCregaddr);
1819         readl(phba->HCregaddr); /* flush */
1820
1821         /* setup host attn register */
1822         writel(0xffffffff, phba->HAregaddr);
1823         readl(phba->HAregaddr); /* flush */
1824         return 0;
1825 }
1826
1827 int
1828 lpfc_sli_hba_setup(struct lpfc_hba * phba)
1829 {
1830         LPFC_MBOXQ_t *pmb;
1831         uint32_t resetcount = 0, rc = 0, done = 0;
1832
1833         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1834         if (!pmb) {
1835                 phba->hba_state = LPFC_HBA_ERROR;
1836                 return -ENOMEM;
1837         }
1838
1839         while (resetcount < 2 && !done) {
1840                 phba->hba_state = LPFC_STATE_UNKNOWN;
1841                 lpfc_sli_brdrestart(phba);
1842                 msleep(2500);
1843                 rc = lpfc_sli_chipset_init(phba);
1844                 if (rc)
1845                         break;
1846
1847                 resetcount++;
1848
1849         /* Call pre CONFIG_PORT mailbox command initialization.  A value of 0
1850          * means the call was successful.  Any other nonzero value is a failure,
1851          * but if ERESTART is returned, the driver may reset the HBA and try
1852          * again.
1853          */
1854                 rc = lpfc_config_port_prep(phba);
1855                 if (rc == -ERESTART) {
1856                         phba->hba_state = 0;
1857                         continue;
1858                 } else if (rc) {
1859                         break;
1860                 }
1861
1862                 phba->hba_state = LPFC_INIT_MBX_CMDS;
1863                 lpfc_config_port(phba, pmb);
1864                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
1865                 if (rc == MBX_SUCCESS)
1866                         done = 1;
1867                 else {
1868                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1869                                 "%d:0442 Adapter failed to init, mbxCmd x%x "
1870                                 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
1871                                 phba->brd_no, pmb->mb.mbxCommand,
1872                                 pmb->mb.mbxStatus, 0);
1873                         phba->sli.sli_flag &= ~LPFC_SLI2_ACTIVE;
1874                 }
1875         }
1876         if (!done)
1877                 goto lpfc_sli_hba_setup_error;
1878
1879         rc = lpfc_sli_ring_map(phba, pmb);
1880
1881         if (rc)
1882                 goto lpfc_sli_hba_setup_error;
1883
1884         phba->sli.sli_flag |= LPFC_PROCESS_LA;
1885
1886         rc = lpfc_config_port_post(phba);
1887         if (rc)
1888                 goto lpfc_sli_hba_setup_error;
1889
1890         goto lpfc_sli_hba_setup_exit;
1891 lpfc_sli_hba_setup_error:
1892         phba->hba_state = LPFC_HBA_ERROR;
1893 lpfc_sli_hba_setup_exit:
1894         mempool_free(pmb, phba->mbox_mem_pool);
1895         return rc;
1896 }
1897
1898 static void
1899 lpfc_mbox_abort(struct lpfc_hba * phba)
1900 {
1901         LPFC_MBOXQ_t *pmbox;
1902         MAILBOX_t *mb;
1903
1904         if (phba->sli.mbox_active) {
1905                 del_timer_sync(&phba->sli.mbox_tmo);
1906                 phba->work_hba_events &= ~WORKER_MBOX_TMO;
1907                 pmbox = phba->sli.mbox_active;
1908                 mb = &pmbox->mb;
1909                 phba->sli.mbox_active = NULL;
1910                 if (pmbox->mbox_cmpl) {
1911                         mb->mbxStatus = MBX_NOT_FINISHED;
1912                         (pmbox->mbox_cmpl) (phba, pmbox);
1913                 }
1914                 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
1915         }
1916
1917         /* Abort all the non active mailbox commands. */
1918         spin_lock_irq(phba->host->host_lock);
1919         pmbox = lpfc_mbox_get(phba);
1920         while (pmbox) {
1921                 mb = &pmbox->mb;
1922                 if (pmbox->mbox_cmpl) {
1923                         mb->mbxStatus = MBX_NOT_FINISHED;
1924                         spin_unlock_irq(phba->host->host_lock);
1925                         (pmbox->mbox_cmpl) (phba, pmbox);
1926                         spin_lock_irq(phba->host->host_lock);
1927                 }
1928                 pmbox = lpfc_mbox_get(phba);
1929         }
1930         spin_unlock_irq(phba->host->host_lock);
1931         return;
1932 }
1933
1934 /*! lpfc_mbox_timeout
1935  *
1936  * \pre
1937  * \post
1938  * \param hba Pointer to per struct lpfc_hba structure
1939  * \param l1  Pointer to the driver's mailbox queue.
1940  * \return
1941  *   void
1942  *
1943  * \b Description:
1944  *
1945  * This routine handles mailbox timeout events at timer interrupt context.
1946  */
1947 void
1948 lpfc_mbox_timeout(unsigned long ptr)
1949 {
1950         struct lpfc_hba *phba;
1951         unsigned long iflag;
1952
1953         phba = (struct lpfc_hba *)ptr;
1954         spin_lock_irqsave(phba->host->host_lock, iflag);
1955         if (!(phba->work_hba_events & WORKER_MBOX_TMO)) {
1956                 phba->work_hba_events |= WORKER_MBOX_TMO;
1957                 if (phba->work_wait)
1958                         wake_up(phba->work_wait);
1959         }
1960         spin_unlock_irqrestore(phba->host->host_lock, iflag);
1961 }
1962
1963 void
1964 lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
1965 {
1966         LPFC_MBOXQ_t *pmbox;
1967         MAILBOX_t *mb;
1968
1969         spin_lock_irq(phba->host->host_lock);
1970         if (!(phba->work_hba_events & WORKER_MBOX_TMO)) {
1971                 spin_unlock_irq(phba->host->host_lock);
1972                 return;
1973         }
1974
1975         phba->work_hba_events &= ~WORKER_MBOX_TMO;
1976
1977         pmbox = phba->sli.mbox_active;
1978         mb = &pmbox->mb;
1979
1980         /* Mbox cmd <mbxCommand> timeout */
1981         lpfc_printf_log(phba,
1982                 KERN_ERR,
1983                 LOG_MBOX | LOG_SLI,
1984                 "%d:0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
1985                 phba->brd_no,
1986                 mb->mbxCommand,
1987                 phba->hba_state,
1988                 phba->sli.sli_flag,
1989                 phba->sli.mbox_active);
1990
1991         phba->sli.mbox_active = NULL;
1992         if (pmbox->mbox_cmpl) {
1993                 mb->mbxStatus = MBX_NOT_FINISHED;
1994                 spin_unlock_irq(phba->host->host_lock);
1995                 (pmbox->mbox_cmpl) (phba, pmbox);
1996                 spin_lock_irq(phba->host->host_lock);
1997         }
1998         phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
1999
2000         spin_unlock_irq(phba->host->host_lock);
2001         lpfc_mbox_abort(phba);
2002         return;
2003 }
2004
2005 int
2006 lpfc_sli_issue_mbox(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmbox, uint32_t flag)
2007 {
2008         MAILBOX_t *mb;
2009         struct lpfc_sli *psli;
2010         uint32_t status, evtctr;
2011         uint32_t ha_copy;
2012         int i;
2013         unsigned long drvr_flag = 0;
2014         volatile uint32_t word0, ldata;
2015         void __iomem *to_slim;
2016
2017         psli = &phba->sli;
2018
2019         spin_lock_irqsave(phba->host->host_lock, drvr_flag);
2020
2021
2022         mb = &pmbox->mb;
2023         status = MBX_SUCCESS;
2024
2025         if (phba->hba_state == LPFC_HBA_ERROR) {
2026                 spin_unlock_irqrestore(phba->host->host_lock, drvr_flag);
2027
2028                 /* Mbox command <mbxCommand> cannot issue */
2029                 LOG_MBOX_CANNOT_ISSUE_DATA( phba, mb, psli, flag)
2030                 return (MBX_NOT_FINISHED);
2031         }
2032
2033         if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
2034                 /* Polling for a mbox command when another one is already active
2035                  * is not allowed in SLI. Also, the driver must have established
2036                  * SLI2 mode to queue and process multiple mbox commands.
2037                  */
2038
2039                 if (flag & MBX_POLL) {
2040                         spin_unlock_irqrestore(phba->host->host_lock,
2041                                                drvr_flag);
2042
2043                         /* Mbox command <mbxCommand> cannot issue */
2044                         LOG_MBOX_CANNOT_ISSUE_DATA( phba, mb, psli, flag)
2045                         return (MBX_NOT_FINISHED);
2046                 }
2047
2048                 if (!(psli->sli_flag & LPFC_SLI2_ACTIVE)) {
2049                         spin_unlock_irqrestore(phba->host->host_lock,
2050                                                drvr_flag);
2051                         /* Mbox command <mbxCommand> cannot issue */
2052                         LOG_MBOX_CANNOT_ISSUE_DATA( phba, mb, psli, flag)
2053                         return (MBX_NOT_FINISHED);
2054                 }
2055
2056                 /* Handle STOP IOCB processing flag. This is only meaningful
2057                  * if we are not polling for mbox completion.
2058                  */
2059                 if (flag & MBX_STOP_IOCB) {
2060                         flag &= ~MBX_STOP_IOCB;
2061                         /* Now flag each ring */
2062                         for (i = 0; i < psli->num_rings; i++) {
2063                                 /* If the ring is active, flag it */
2064                                 if (psli->ring[i].cmdringaddr) {
2065                                         psli->ring[i].flag |=
2066                                             LPFC_STOP_IOCB_MBX;
2067                                 }
2068                         }
2069                 }
2070
2071                 /* Another mailbox command is still being processed, queue this
2072                  * command to be processed later.
2073                  */
2074                 lpfc_mbox_put(phba, pmbox);
2075
2076                 /* Mbox cmd issue - BUSY */
2077                 lpfc_printf_log(phba,
2078                         KERN_INFO,
2079                         LOG_MBOX | LOG_SLI,
2080                         "%d:0308 Mbox cmd issue - BUSY Data: x%x x%x x%x x%x\n",
2081                         phba->brd_no,
2082                         mb->mbxCommand,
2083                         phba->hba_state,
2084                         psli->sli_flag,
2085                         flag);
2086
2087                 psli->slistat.mbox_busy++;
2088                 spin_unlock_irqrestore(phba->host->host_lock,
2089                                        drvr_flag);
2090
2091                 return (MBX_BUSY);
2092         }
2093
2094         /* Handle STOP IOCB processing flag. This is only meaningful
2095          * if we are not polling for mbox completion.
2096          */
2097         if (flag & MBX_STOP_IOCB) {
2098                 flag &= ~MBX_STOP_IOCB;
2099                 if (flag == MBX_NOWAIT) {
2100                         /* Now flag each ring */
2101                         for (i = 0; i < psli->num_rings; i++) {
2102                                 /* If the ring is active, flag it */
2103                                 if (psli->ring[i].cmdringaddr) {
2104                                         psli->ring[i].flag |=
2105                                             LPFC_STOP_IOCB_MBX;
2106                                 }
2107                         }
2108                 }
2109         }
2110
2111         psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
2112
2113         /* If we are not polling, we MUST be in SLI2 mode */
2114         if (flag != MBX_POLL) {
2115                 if (!(psli->sli_flag & LPFC_SLI2_ACTIVE) &&
2116                     (mb->mbxCommand != MBX_KILL_BOARD)) {
2117                         psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
2118                         spin_unlock_irqrestore(phba->host->host_lock,
2119                                                drvr_flag);
2120                         /* Mbox command <mbxCommand> cannot issue */
2121                         LOG_MBOX_CANNOT_ISSUE_DATA( phba, mb, psli, flag);
2122                         return (MBX_NOT_FINISHED);
2123                 }
2124                 /* timeout active mbox command */
2125                 mod_timer(&psli->mbox_tmo, jiffies + HZ * LPFC_MBOX_TMO);
2126         }
2127
2128         /* Mailbox cmd <cmd> issue */
2129         lpfc_printf_log(phba,
2130                 KERN_INFO,
2131                 LOG_MBOX | LOG_SLI,
2132                 "%d:0309 Mailbox cmd x%x issue Data: x%x x%x x%x\n",
2133                 phba->brd_no,
2134                 mb->mbxCommand,
2135                 phba->hba_state,
2136                 psli->sli_flag,
2137                 flag);
2138
2139         psli->slistat.mbox_cmd++;
2140         evtctr = psli->slistat.mbox_event;
2141
2142         /* next set own bit for the adapter and copy over command word */
2143         mb->mbxOwner = OWN_CHIP;
2144
2145         if (psli->sli_flag & LPFC_SLI2_ACTIVE) {
2146                 /* First copy command data to host SLIM area */
2147                 lpfc_sli_pcimem_bcopy(mb, &phba->slim2p->mbx, MAILBOX_CMD_SIZE);
2148         } else {
2149                 if (mb->mbxCommand == MBX_CONFIG_PORT ||
2150                     mb->mbxCommand == MBX_KILL_BOARD) {
2151                         /* copy command data into host mbox for cmpl */
2152                         lpfc_sli_pcimem_bcopy(mb, &phba->slim2p->mbx,
2153                                         MAILBOX_CMD_SIZE);
2154                 }
2155
2156                 /* First copy mbox command data to HBA SLIM, skip past first
2157                    word */
2158                 to_slim = phba->MBslimaddr + sizeof (uint32_t);
2159                 lpfc_memcpy_to_slim(to_slim, &mb->un.varWords[0],
2160                             MAILBOX_CMD_SIZE - sizeof (uint32_t));
2161
2162                 /* Next copy over first word, with mbxOwner set */
2163                 ldata = *((volatile uint32_t *)mb);
2164                 to_slim = phba->MBslimaddr;
2165                 writel(ldata, to_slim);
2166                 readl(to_slim); /* flush */
2167
2168                 if (mb->mbxCommand == MBX_CONFIG_PORT) {
2169                         /* switch over to host mailbox */
2170                         psli->sli_flag |= LPFC_SLI2_ACTIVE;
2171                 }
2172         }
2173
2174         wmb();
2175         /* interrupt board to doit right away */
2176         writel(CA_MBATT, phba->CAregaddr);
2177         readl(phba->CAregaddr); /* flush */
2178
2179         switch (flag) {
2180         case MBX_NOWAIT:
2181                 /* Don't wait for it to finish, just return */
2182                 psli->mbox_active = pmbox;
2183                 break;
2184
2185         case MBX_POLL:
2186                 i = 0;
2187                 psli->mbox_active = NULL;
2188                 if (psli->sli_flag & LPFC_SLI2_ACTIVE) {
2189                         /* First read mbox status word */
2190                         word0 = *((volatile uint32_t *)&phba->slim2p->mbx);
2191                         word0 = le32_to_cpu(word0);
2192                 } else {
2193                         /* First read mbox status word */
2194                         word0 = readl(phba->MBslimaddr);
2195                 }
2196
2197                 /* Read the HBA Host Attention Register */
2198                 ha_copy = readl(phba->HAregaddr);
2199
2200                 /* Wait for command to complete */
2201                 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
2202                        (!(ha_copy & HA_MBATT) &&
2203                         (phba->hba_state > LPFC_WARM_START))) {
2204                         if (i++ >= 100) {
2205                                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
2206                                 spin_unlock_irqrestore(phba->host->host_lock,
2207                                                        drvr_flag);
2208                                 return (MBX_NOT_FINISHED);
2209                         }
2210
2211                         /* Check if we took a mbox interrupt while we were
2212                            polling */
2213                         if (((word0 & OWN_CHIP) != OWN_CHIP)
2214                             && (evtctr != psli->slistat.mbox_event))
2215                                 break;
2216
2217                         spin_unlock_irqrestore(phba->host->host_lock,
2218                                                drvr_flag);
2219
2220                         /* Can be in interrupt context, do not sleep */
2221                         /* (or might be called with interrupts disabled) */
2222                         mdelay(i);
2223
2224                         spin_lock_irqsave(phba->host->host_lock, drvr_flag);
2225
2226                         if (psli->sli_flag & LPFC_SLI2_ACTIVE) {
2227                                 /* First copy command data */
2228                                 word0 = *((volatile uint32_t *)
2229                                                 &phba->slim2p->mbx);
2230                                 word0 = le32_to_cpu(word0);
2231                                 if (mb->mbxCommand == MBX_CONFIG_PORT) {
2232                                         MAILBOX_t *slimmb;
2233                                         volatile uint32_t slimword0;
2234                                         /* Check real SLIM for any errors */
2235                                         slimword0 = readl(phba->MBslimaddr);
2236                                         slimmb = (MAILBOX_t *) & slimword0;
2237                                         if (((slimword0 & OWN_CHIP) != OWN_CHIP)
2238                                             && slimmb->mbxStatus) {
2239                                                 psli->sli_flag &=
2240                                                     ~LPFC_SLI2_ACTIVE;
2241                                                 word0 = slimword0;
2242                                         }
2243                                 }
2244                         } else {
2245                                 /* First copy command data */
2246                                 word0 = readl(phba->MBslimaddr);
2247                         }
2248                         /* Read the HBA Host Attention Register */
2249                         ha_copy = readl(phba->HAregaddr);
2250                 }
2251
2252                 if (psli->sli_flag & LPFC_SLI2_ACTIVE) {
2253                         /* copy results back to user */
2254                         lpfc_sli_pcimem_bcopy(&phba->slim2p->mbx, mb,
2255                                         MAILBOX_CMD_SIZE);
2256                 } else {
2257                         /* First copy command data */
2258                         lpfc_memcpy_from_slim(mb, phba->MBslimaddr,
2259                                                         MAILBOX_CMD_SIZE);
2260                         if ((mb->mbxCommand == MBX_DUMP_MEMORY) &&
2261                                 pmbox->context2) {
2262                                 lpfc_memcpy_from_slim((void *)pmbox->context2,
2263                                       phba->MBslimaddr + DMP_RSP_OFFSET,
2264                                                       mb->un.varDmp.word_cnt);
2265                         }
2266                 }
2267
2268                 writel(HA_MBATT, phba->HAregaddr);
2269                 readl(phba->HAregaddr); /* flush */
2270
2271                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
2272                 status = mb->mbxStatus;
2273         }
2274
2275         spin_unlock_irqrestore(phba->host->host_lock, drvr_flag);
2276         return (status);
2277 }
2278
2279 static int
2280 lpfc_sli_ringtx_put(struct lpfc_hba * phba, struct lpfc_sli_ring * pring,
2281                     struct lpfc_iocbq * piocb)
2282 {
2283         /* Insert the caller's iocb in the txq tail for later processing. */
2284         list_add_tail(&piocb->list, &pring->txq);
2285         pring->txq_cnt++;
2286         return (0);
2287 }
2288
2289 static struct lpfc_iocbq *
2290 lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2291                    struct lpfc_iocbq ** piocb)
2292 {
2293         struct lpfc_iocbq * nextiocb;
2294
2295         nextiocb = lpfc_sli_ringtx_get(phba, pring);
2296         if (!nextiocb) {
2297                 nextiocb = *piocb;
2298                 *piocb = NULL;
2299         }
2300
2301         return nextiocb;
2302 }
2303
2304 int
2305 lpfc_sli_issue_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2306                     struct lpfc_iocbq *piocb, uint32_t flag)
2307 {
2308         struct lpfc_iocbq *nextiocb;
2309         IOCB_t *iocb;
2310
2311         /*
2312          * We should never get an IOCB if we are in a < LINK_DOWN state
2313          */
2314         if (unlikely(phba->hba_state < LPFC_LINK_DOWN))
2315                 return IOCB_ERROR;
2316
2317         /*
2318          * Check to see if we are blocking IOCB processing because of a
2319          * outstanding mbox command.
2320          */
2321         if (unlikely(pring->flag & LPFC_STOP_IOCB_MBX))
2322                 goto iocb_busy;
2323
2324         if (unlikely(phba->hba_state == LPFC_LINK_DOWN)) {
2325                 /*
2326                  * Only CREATE_XRI, CLOSE_XRI, ABORT_XRI, and QUE_RING_BUF
2327                  * can be issued if the link is not up.
2328                  */
2329                 switch (piocb->iocb.ulpCommand) {
2330                 case CMD_QUE_RING_BUF_CN:
2331                 case CMD_QUE_RING_BUF64_CN:
2332                         /*
2333                          * For IOCBs, like QUE_RING_BUF, that have no rsp ring
2334                          * completion, iocb_cmpl MUST be 0.
2335                          */
2336                         if (piocb->iocb_cmpl)
2337                                 piocb->iocb_cmpl = NULL;
2338                         /*FALLTHROUGH*/
2339                 case CMD_CREATE_XRI_CR:
2340                         break;
2341                 default:
2342                         goto iocb_busy;
2343                 }
2344
2345         /*
2346          * For FCP commands, we must be in a state where we can process link
2347          * attention events.
2348          */
2349         } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
2350                    !(phba->sli.sli_flag & LPFC_PROCESS_LA)))
2351                 goto iocb_busy;
2352
2353         while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
2354                (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
2355                 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
2356
2357         if (iocb)
2358                 lpfc_sli_update_ring(phba, pring);
2359         else
2360                 lpfc_sli_update_full_ring(phba, pring);
2361
2362         if (!piocb)
2363                 return IOCB_SUCCESS;
2364
2365         goto out_busy;
2366
2367  iocb_busy:
2368         pring->stats.iocb_cmd_delay++;
2369
2370  out_busy:
2371
2372         if (!(flag & SLI_IOCB_RET_IOCB)) {
2373                 lpfc_sli_ringtx_put(phba, pring, piocb);
2374                 return IOCB_SUCCESS;
2375         }
2376
2377         return IOCB_BUSY;
2378 }
2379
2380 int
2381 lpfc_sli_setup(struct lpfc_hba *phba)
2382 {
2383         int i, totiocb = 0;
2384         struct lpfc_sli *psli = &phba->sli;
2385         struct lpfc_sli_ring *pring;
2386
2387         psli->num_rings = MAX_CONFIGURED_RINGS;
2388         psli->sli_flag = 0;
2389         psli->fcp_ring = LPFC_FCP_RING;
2390         psli->next_ring = LPFC_FCP_NEXT_RING;
2391         psli->ip_ring = LPFC_IP_RING;
2392
2393         psli->iocbq_lookup = NULL;
2394         psli->iocbq_lookup_len = 0;
2395         psli->last_iotag = 0;
2396
2397         for (i = 0; i < psli->num_rings; i++) {
2398                 pring = &psli->ring[i];
2399                 switch (i) {
2400                 case LPFC_FCP_RING:     /* ring 0 - FCP */
2401                         /* numCiocb and numRiocb are used in config_port */
2402                         pring->numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
2403                         pring->numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
2404                         pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
2405                         pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
2406                         pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
2407                         pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
2408                         pring->iotag_ctr = 0;
2409                         pring->iotag_max =
2410                             (phba->cfg_hba_queue_depth * 2);
2411                         pring->fast_iotag = pring->iotag_max;
2412                         pring->num_mask = 0;
2413                         break;
2414                 case LPFC_IP_RING:      /* ring 1 - IP */
2415                         /* numCiocb and numRiocb are used in config_port */
2416                         pring->numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
2417                         pring->numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
2418                         pring->num_mask = 0;
2419                         break;
2420                 case LPFC_ELS_RING:     /* ring 2 - ELS / CT */
2421                         /* numCiocb and numRiocb are used in config_port */
2422                         pring->numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
2423                         pring->numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
2424                         pring->fast_iotag = 0;
2425                         pring->iotag_ctr = 0;
2426                         pring->iotag_max = 4096;
2427                         pring->num_mask = 4;
2428                         pring->prt[0].profile = 0;      /* Mask 0 */
2429                         pring->prt[0].rctl = FC_ELS_REQ;
2430                         pring->prt[0].type = FC_ELS_DATA;
2431                         pring->prt[0].lpfc_sli_rcv_unsol_event =
2432                             lpfc_els_unsol_event;
2433                         pring->prt[1].profile = 0;      /* Mask 1 */
2434                         pring->prt[1].rctl = FC_ELS_RSP;
2435                         pring->prt[1].type = FC_ELS_DATA;
2436                         pring->prt[1].lpfc_sli_rcv_unsol_event =
2437                             lpfc_els_unsol_event;
2438                         pring->prt[2].profile = 0;      /* Mask 2 */
2439                         /* NameServer Inquiry */
2440                         pring->prt[2].rctl = FC_UNSOL_CTL;
2441                         /* NameServer */
2442                         pring->prt[2].type = FC_COMMON_TRANSPORT_ULP;
2443                         pring->prt[2].lpfc_sli_rcv_unsol_event =
2444                             lpfc_ct_unsol_event;
2445                         pring->prt[3].profile = 0;      /* Mask 3 */
2446                         /* NameServer response */
2447                         pring->prt[3].rctl = FC_SOL_CTL;
2448                         /* NameServer */
2449                         pring->prt[3].type = FC_COMMON_TRANSPORT_ULP;
2450                         pring->prt[3].lpfc_sli_rcv_unsol_event =
2451                             lpfc_ct_unsol_event;
2452                         break;
2453                 }
2454                 totiocb += (pring->numCiocb + pring->numRiocb);
2455         }
2456         if (totiocb > MAX_SLI2_IOCB) {
2457                 /* Too many cmd / rsp ring entries in SLI2 SLIM */
2458                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2459                                 "%d:0462 Too many cmd / rsp ring entries in "
2460                                 "SLI2 SLIM Data: x%x x%x\n",
2461                                 phba->brd_no, totiocb, MAX_SLI2_IOCB);
2462         }
2463
2464         return 0;
2465 }
2466
2467 int
2468 lpfc_sli_queue_setup(struct lpfc_hba * phba)
2469 {
2470         struct lpfc_sli *psli;
2471         struct lpfc_sli_ring *pring;
2472         int i;
2473
2474         psli = &phba->sli;
2475         spin_lock_irq(phba->host->host_lock);
2476         INIT_LIST_HEAD(&psli->mboxq);
2477         /* Initialize list headers for txq and txcmplq as double linked lists */
2478         for (i = 0; i < psli->num_rings; i++) {
2479                 pring = &psli->ring[i];
2480                 pring->ringno = i;
2481                 pring->next_cmdidx  = 0;
2482                 pring->local_getidx = 0;
2483                 pring->cmdidx = 0;
2484                 INIT_LIST_HEAD(&pring->txq);
2485                 INIT_LIST_HEAD(&pring->txcmplq);
2486                 INIT_LIST_HEAD(&pring->iocb_continueq);
2487                 INIT_LIST_HEAD(&pring->postbufq);
2488         }
2489         spin_unlock_irq(phba->host->host_lock);
2490         return (1);
2491 }
2492
2493 int
2494 lpfc_sli_hba_down(struct lpfc_hba * phba)
2495 {
2496         struct lpfc_sli *psli;
2497         struct lpfc_sli_ring *pring;
2498         LPFC_MBOXQ_t *pmb;
2499         struct lpfc_iocbq *iocb, *next_iocb;
2500         IOCB_t *icmd = NULL;
2501         int i;
2502         unsigned long flags = 0;
2503
2504         psli = &phba->sli;
2505         lpfc_hba_down_prep(phba);
2506
2507         spin_lock_irqsave(phba->host->host_lock, flags);
2508
2509         for (i = 0; i < psli->num_rings; i++) {
2510                 pring = &psli->ring[i];
2511                 pring->flag |= LPFC_DEFERRED_RING_EVENT;
2512
2513                 /*
2514                  * Error everything on the txq since these iocbs have not been
2515                  * given to the FW yet.
2516                  */
2517                 pring->txq_cnt = 0;
2518
2519                 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
2520                         list_del_init(&iocb->list);
2521                         if (iocb->iocb_cmpl) {
2522                                 icmd = &iocb->iocb;
2523                                 icmd->ulpStatus = IOSTAT_LOCAL_REJECT;
2524                                 icmd->un.ulpWord[4] = IOERR_SLI_DOWN;
2525                                 spin_unlock_irqrestore(phba->host->host_lock,
2526                                                        flags);
2527                                 (iocb->iocb_cmpl) (phba, iocb, iocb);
2528                                 spin_lock_irqsave(phba->host->host_lock, flags);
2529                         } else
2530                                 lpfc_sli_release_iocbq(phba, iocb);
2531                 }
2532
2533                 INIT_LIST_HEAD(&(pring->txq));
2534
2535                 kfree(pring->fast_lookup);
2536                 pring->fast_lookup = NULL;
2537         }
2538
2539         spin_unlock_irqrestore(phba->host->host_lock, flags);
2540
2541         /* Return any active mbox cmds */
2542         del_timer_sync(&psli->mbox_tmo);
2543         spin_lock_irqsave(phba->host->host_lock, flags);
2544         phba->work_hba_events &= ~WORKER_MBOX_TMO;
2545         if (psli->mbox_active) {
2546                 pmb = psli->mbox_active;
2547                 pmb->mb.mbxStatus = MBX_NOT_FINISHED;
2548                 if (pmb->mbox_cmpl) {
2549                         spin_unlock_irqrestore(phba->host->host_lock, flags);
2550                         pmb->mbox_cmpl(phba,pmb);
2551                         spin_lock_irqsave(phba->host->host_lock, flags);
2552                 }
2553         }
2554         psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
2555         psli->mbox_active = NULL;
2556
2557         /* Return any pending mbox cmds */
2558         while ((pmb = lpfc_mbox_get(phba)) != NULL) {
2559                 pmb->mb.mbxStatus = MBX_NOT_FINISHED;
2560                 if (pmb->mbox_cmpl) {
2561                         spin_unlock_irqrestore(phba->host->host_lock, flags);
2562                         pmb->mbox_cmpl(phba,pmb);
2563                         spin_lock_irqsave(phba->host->host_lock, flags);
2564                 }
2565         }
2566
2567         INIT_LIST_HEAD(&psli->mboxq);
2568
2569         spin_unlock_irqrestore(phba->host->host_lock, flags);
2570
2571         return 1;
2572 }
2573
2574 void
2575 lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
2576 {
2577         uint32_t *src = srcp;
2578         uint32_t *dest = destp;
2579         uint32_t ldata;
2580         int i;
2581
2582         for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
2583                 ldata = *src;
2584                 ldata = le32_to_cpu(ldata);
2585                 *dest = ldata;
2586                 src++;
2587                 dest++;
2588         }
2589 }
2590
2591 int
2592 lpfc_sli_ringpostbuf_put(struct lpfc_hba * phba, struct lpfc_sli_ring * pring,
2593                          struct lpfc_dmabuf * mp)
2594 {
2595         /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
2596            later */
2597         list_add_tail(&mp->list, &pring->postbufq);
2598
2599         pring->postbufq_cnt++;
2600         return 0;
2601 }
2602
2603
2604 struct lpfc_dmabuf *
2605 lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2606                          dma_addr_t phys)
2607 {
2608         struct lpfc_dmabuf *mp, *next_mp;
2609         struct list_head *slp = &pring->postbufq;
2610
2611         /* Search postbufq, from the begining, looking for a match on phys */
2612         list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
2613                 if (mp->phys == phys) {
2614                         list_del_init(&mp->list);
2615                         pring->postbufq_cnt--;
2616                         return mp;
2617                 }
2618         }
2619
2620         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2621                         "%d:0410 Cannot find virtual addr for mapped buf on "
2622                         "ring %d Data x%llx x%p x%p x%x\n",
2623                         phba->brd_no, pring->ringno, (unsigned long long)phys,
2624                         slp->next, slp->prev, pring->postbufq_cnt);
2625         return NULL;
2626 }
2627
2628 static void
2629 lpfc_sli_abort_elsreq_cmpl(struct lpfc_hba * phba, struct lpfc_iocbq * cmdiocb,
2630                            struct lpfc_iocbq * rspiocb)
2631 {
2632         struct lpfc_dmabuf *buf_ptr, *buf_ptr1;
2633         /* Free the resources associated with the ELS_REQUEST64 IOCB the driver
2634          * just aborted.
2635          * In this case, context2  = cmd,  context2->next = rsp, context3 = bpl
2636          */
2637         if (cmdiocb->context2) {
2638                 buf_ptr1 = (struct lpfc_dmabuf *) cmdiocb->context2;
2639
2640                 /* Free the response IOCB before completing the abort
2641                    command.  */
2642                 buf_ptr = NULL;
2643                 list_remove_head((&buf_ptr1->list), buf_ptr,
2644                                  struct lpfc_dmabuf, list);
2645                 if (buf_ptr) {
2646                         lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
2647                         kfree(buf_ptr);
2648                 }
2649                 lpfc_mbuf_free(phba, buf_ptr1->virt, buf_ptr1->phys);
2650                 kfree(buf_ptr1);
2651         }
2652
2653         if (cmdiocb->context3) {
2654                 buf_ptr = (struct lpfc_dmabuf *) cmdiocb->context3;
2655                 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
2656                 kfree(buf_ptr);
2657         }
2658
2659         lpfc_sli_release_iocbq(phba, cmdiocb);
2660         return;
2661 }
2662
2663 int
2664 lpfc_sli_issue_abort_iotag32(struct lpfc_hba * phba,
2665                              struct lpfc_sli_ring * pring,
2666                              struct lpfc_iocbq * cmdiocb)
2667 {
2668         struct lpfc_iocbq *abtsiocbp;
2669         IOCB_t *icmd = NULL;
2670         IOCB_t *iabt = NULL;
2671
2672         /* issue ABTS for this IOCB based on iotag */
2673         abtsiocbp = lpfc_sli_get_iocbq(phba);
2674         if (abtsiocbp == NULL)
2675                 return 0;
2676
2677         iabt = &abtsiocbp->iocb;
2678         icmd = &cmdiocb->iocb;
2679         switch (icmd->ulpCommand) {
2680         case CMD_ELS_REQUEST64_CR:
2681                 /* Even though we abort the ELS command, the firmware may access
2682                  * the BPL or other resources before it processes our
2683                  * ABORT_MXRI64. Thus we must delay reusing the cmdiocb
2684                  * resources till the actual abort request completes.
2685                  */
2686                 abtsiocbp->context1 = (void *)((unsigned long)icmd->ulpCommand);
2687                 abtsiocbp->context2 = cmdiocb->context2;
2688                 abtsiocbp->context3 = cmdiocb->context3;
2689                 cmdiocb->context2 = NULL;
2690                 cmdiocb->context3 = NULL;
2691                 abtsiocbp->iocb_cmpl = lpfc_sli_abort_elsreq_cmpl;
2692                 break;
2693         default:
2694                 lpfc_sli_release_iocbq(phba, abtsiocbp);
2695                 return 0;
2696         }
2697
2698         iabt->un.amxri.abortType = ABORT_TYPE_ABTS;
2699         iabt->un.amxri.iotag32 = icmd->un.elsreq64.bdl.ulpIoTag32;
2700
2701         iabt->ulpLe = 1;
2702         iabt->ulpClass = CLASS3;
2703         iabt->ulpCommand = CMD_ABORT_MXRI64_CN;
2704
2705         if (lpfc_sli_issue_iocb(phba, pring, abtsiocbp, 0) == IOCB_ERROR) {
2706                 lpfc_sli_release_iocbq(phba, abtsiocbp);
2707                 return 0;
2708         }
2709
2710         return 1;
2711 }
2712
2713 static int
2714 lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, uint16_t tgt_id,
2715                            uint64_t lun_id, uint32_t ctx,
2716                            lpfc_ctx_cmd ctx_cmd)
2717 {
2718         struct lpfc_scsi_buf *lpfc_cmd;
2719         struct scsi_cmnd *cmnd;
2720         int rc = 1;
2721
2722         if (!(iocbq->iocb_flag &  LPFC_IO_FCP))
2723                 return rc;
2724
2725         lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
2726         cmnd = lpfc_cmd->pCmd;
2727
2728         if (cmnd == NULL)
2729                 return rc;
2730
2731         switch (ctx_cmd) {
2732         case LPFC_CTX_LUN:
2733                 if ((cmnd->device->id == tgt_id) &&
2734                     (cmnd->device->lun == lun_id))
2735                         rc = 0;
2736                 break;
2737         case LPFC_CTX_TGT:
2738                 if (cmnd->device->id == tgt_id)
2739                         rc = 0;
2740                 break;
2741         case LPFC_CTX_CTX:
2742                 if (iocbq->iocb.ulpContext == ctx)
2743                         rc = 0;
2744                 break;
2745         case LPFC_CTX_HOST:
2746                 rc = 0;
2747                 break;
2748         default:
2749                 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
2750                         __FUNCTION__, ctx_cmd);
2751                 break;
2752         }
2753
2754         return rc;
2755 }
2756
2757 int
2758 lpfc_sli_sum_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2759                 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd ctx_cmd)
2760 {
2761         struct lpfc_iocbq *iocbq;
2762         int sum, i;
2763
2764         for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
2765                 iocbq = phba->sli.iocbq_lookup[i];
2766
2767                 if (lpfc_sli_validate_fcp_iocb (iocbq, tgt_id, lun_id,
2768                                                 0, ctx_cmd) == 0)
2769                         sum++;
2770         }
2771
2772         return sum;
2773 }
2774
2775 void
2776 lpfc_sli_abort_fcp_cmpl(struct lpfc_hba * phba, struct lpfc_iocbq * cmdiocb,
2777                            struct lpfc_iocbq * rspiocb)
2778 {
2779         spin_lock_irq(phba->host->host_lock);
2780         lpfc_sli_release_iocbq(phba, cmdiocb);
2781         spin_unlock_irq(phba->host->host_lock);
2782         return;
2783 }
2784
2785 int
2786 lpfc_sli_abort_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2787                     uint16_t tgt_id, uint64_t lun_id, uint32_t ctx,
2788                     lpfc_ctx_cmd abort_cmd)
2789 {
2790         struct lpfc_iocbq *iocbq;
2791         struct lpfc_iocbq *abtsiocb;
2792         IOCB_t *cmd = NULL;
2793         int errcnt = 0, ret_val = 0;
2794         int i;
2795
2796         for (i = 1; i <= phba->sli.last_iotag; i++) {
2797                 iocbq = phba->sli.iocbq_lookup[i];
2798
2799                 if (lpfc_sli_validate_fcp_iocb (iocbq, tgt_id, lun_id,
2800                                                 0, abort_cmd) != 0)
2801                         continue;
2802
2803                 /* issue ABTS for this IOCB based on iotag */
2804                 abtsiocb = lpfc_sli_get_iocbq(phba);
2805                 if (abtsiocb == NULL) {
2806                         errcnt++;
2807                         continue;
2808                 }
2809
2810                 cmd = &iocbq->iocb;
2811                 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
2812                 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
2813                 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
2814                 abtsiocb->iocb.ulpLe = 1;
2815                 abtsiocb->iocb.ulpClass = cmd->ulpClass;
2816
2817                 if (phba->hba_state >= LPFC_LINK_UP)
2818                         abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
2819                 else
2820                         abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
2821
2822                 /* Setup callback routine and issue the command. */
2823                 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
2824                 ret_val = lpfc_sli_issue_iocb(phba, pring, abtsiocb, 0);
2825                 if (ret_val == IOCB_ERROR) {
2826                         lpfc_sli_release_iocbq(phba, abtsiocb);
2827                         errcnt++;
2828                         continue;
2829                 }
2830         }
2831
2832         return errcnt;
2833 }
2834
2835 static void
2836 lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
2837                         struct lpfc_iocbq *cmdiocbq,
2838                         struct lpfc_iocbq *rspiocbq)
2839 {
2840         wait_queue_head_t *pdone_q;
2841         unsigned long iflags;
2842
2843         spin_lock_irqsave(phba->host->host_lock, iflags);
2844         cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
2845         if (cmdiocbq->context2 && rspiocbq)
2846                 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
2847                        &rspiocbq->iocb, sizeof(IOCB_t));
2848
2849         pdone_q = cmdiocbq->context_un.wait_queue;
2850         spin_unlock_irqrestore(phba->host->host_lock, iflags);
2851         if (pdone_q)
2852                 wake_up(pdone_q);
2853         return;
2854 }
2855
2856 /*
2857  * Issue the caller's iocb and wait for its completion, but no longer than the
2858  * caller's timeout.  Note that iocb_flags is cleared before the
2859  * lpfc_sli_issue_call since the wake routine sets a unique value and by
2860  * definition this is a wait function.
2861  */
2862 int
2863 lpfc_sli_issue_iocb_wait(struct lpfc_hba * phba,
2864                          struct lpfc_sli_ring * pring,
2865                          struct lpfc_iocbq * piocb,
2866                          struct lpfc_iocbq * prspiocbq,
2867                          uint32_t timeout)
2868 {
2869         DECLARE_WAIT_QUEUE_HEAD(done_q);
2870         long timeleft, timeout_req = 0;
2871         int retval = IOCB_SUCCESS;
2872         uint32_t creg_val;
2873
2874         /*
2875          * If the caller has provided a response iocbq buffer, then context2
2876          * is NULL or its an error.
2877          */
2878         if (prspiocbq) {
2879                 if (piocb->context2)
2880                         return IOCB_ERROR;
2881                 piocb->context2 = prspiocbq;
2882         }
2883
2884         piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
2885         piocb->context_un.wait_queue = &done_q;
2886         piocb->iocb_flag &= ~LPFC_IO_WAKE;
2887
2888         if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
2889                 creg_val = readl(phba->HCregaddr);
2890                 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
2891                 writel(creg_val, phba->HCregaddr);
2892                 readl(phba->HCregaddr); /* flush */
2893         }
2894
2895         retval = lpfc_sli_issue_iocb(phba, pring, piocb, 0);
2896         if (retval == IOCB_SUCCESS) {
2897                 timeout_req = timeout * HZ;
2898                 spin_unlock_irq(phba->host->host_lock);
2899                 timeleft = wait_event_timeout(done_q,
2900                                 piocb->iocb_flag & LPFC_IO_WAKE,
2901                                 timeout_req);
2902                 spin_lock_irq(phba->host->host_lock);
2903
2904                 if (timeleft == 0) {
2905                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2906                                         "%d:0329 IOCB wait timeout error - no "
2907                                         "wake response Data x%x\n",
2908                                         phba->brd_no, timeout);
2909                         retval = IOCB_TIMEDOUT;
2910                 } else if (!(piocb->iocb_flag & LPFC_IO_WAKE)) {
2911                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2912                                         "%d:0330 IOCB wake NOT set, "
2913                                         "Data x%x x%lx\n", phba->brd_no,
2914                                         timeout, (timeleft / jiffies));
2915                         retval = IOCB_TIMEDOUT;
2916                 } else {
2917                         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
2918                                         "%d:0331 IOCB wake signaled\n",
2919                                         phba->brd_no);
2920                 }
2921         } else {
2922                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
2923                                 "%d:0332 IOCB wait issue failed, Data x%x\n",
2924                                 phba->brd_no, retval);
2925                 retval = IOCB_ERROR;
2926         }
2927
2928         if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
2929                 creg_val = readl(phba->HCregaddr);
2930                 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
2931                 writel(creg_val, phba->HCregaddr);
2932                 readl(phba->HCregaddr); /* flush */
2933         }
2934
2935         if (prspiocbq)
2936                 piocb->context2 = NULL;
2937
2938         piocb->context_un.wait_queue = NULL;
2939         piocb->iocb_cmpl = NULL;
2940         return retval;
2941 }
2942
2943 int
2944 lpfc_sli_issue_mbox_wait(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq,
2945                          uint32_t timeout)
2946 {
2947         DECLARE_WAIT_QUEUE_HEAD(done_q);
2948         DECLARE_WAITQUEUE(wq_entry, current);
2949         uint32_t timeleft = 0;
2950         int retval;
2951
2952         /* The caller must leave context1 empty. */
2953         if (pmboxq->context1 != 0) {
2954                 return (MBX_NOT_FINISHED);
2955         }
2956
2957         /* setup wake call as IOCB callback */
2958         pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
2959         /* setup context field to pass wait_queue pointer to wake function  */
2960         pmboxq->context1 = &done_q;
2961
2962         /* start to sleep before we wait, to avoid races */
2963         set_current_state(TASK_INTERRUPTIBLE);
2964         add_wait_queue(&done_q, &wq_entry);
2965
2966         /* now issue the command */
2967         retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
2968
2969         if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
2970                 timeleft = schedule_timeout(timeout * HZ);
2971                 pmboxq->context1 = NULL;
2972                 /* if schedule_timeout returns 0, we timed out and were not
2973                    woken up */
2974                 if ((timeleft == 0) || signal_pending(current))
2975                         retval = MBX_TIMEOUT;
2976                 else
2977                         retval = MBX_SUCCESS;
2978         }
2979
2980
2981         set_current_state(TASK_RUNNING);
2982         remove_wait_queue(&done_q, &wq_entry);
2983         return retval;
2984 }
2985
2986 irqreturn_t
2987 lpfc_intr_handler(int irq, void *dev_id, struct pt_regs * regs)
2988 {
2989         struct lpfc_hba *phba;
2990         uint32_t ha_copy;
2991         uint32_t work_ha_copy;
2992         unsigned long status;
2993         int i;
2994         uint32_t control;
2995
2996         /*
2997          * Get the driver's phba structure from the dev_id and
2998          * assume the HBA is not interrupting.
2999          */
3000         phba = (struct lpfc_hba *) dev_id;
3001
3002         if (unlikely(!phba))
3003                 return IRQ_NONE;
3004
3005         phba->sli.slistat.sli_intr++;
3006
3007         /*
3008          * Call the HBA to see if it is interrupting.  If not, don't claim
3009          * the interrupt
3010          */
3011
3012         /* Ignore all interrupts during initialization. */
3013         if (unlikely(phba->hba_state < LPFC_LINK_DOWN))
3014                 return IRQ_NONE;
3015
3016         /*
3017          * Read host attention register to determine interrupt source
3018          * Clear Attention Sources, except Error Attention (to
3019          * preserve status) and Link Attention
3020          */
3021         spin_lock(phba->host->host_lock);
3022         ha_copy = readl(phba->HAregaddr);
3023         writel((ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
3024         readl(phba->HAregaddr); /* flush */
3025         spin_unlock(phba->host->host_lock);
3026
3027         if (unlikely(!ha_copy))
3028                 return IRQ_NONE;
3029
3030         work_ha_copy = ha_copy & phba->work_ha_mask;
3031
3032         if (unlikely(work_ha_copy)) {
3033                 if (work_ha_copy & HA_LATT) {
3034                         if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
3035                                 /*
3036                                  * Turn off Link Attention interrupts
3037                                  * until CLEAR_LA done
3038                                  */
3039                                 spin_lock(phba->host->host_lock);
3040                                 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
3041                                 control = readl(phba->HCregaddr);
3042                                 control &= ~HC_LAINT_ENA;
3043                                 writel(control, phba->HCregaddr);
3044                                 readl(phba->HCregaddr); /* flush */
3045                                 spin_unlock(phba->host->host_lock);
3046                         }
3047                         else
3048                                 work_ha_copy &= ~HA_LATT;
3049                 }
3050
3051                 if (work_ha_copy & ~(HA_ERATT|HA_MBATT|HA_LATT)) {
3052                         for (i = 0; i < phba->sli.num_rings; i++) {
3053                                 if (work_ha_copy & (HA_RXATT << (4*i))) {
3054                                         /*
3055                                          * Turn off Slow Rings interrupts
3056                                          */
3057                                         spin_lock(phba->host->host_lock);
3058                                         control = readl(phba->HCregaddr);
3059                                         control &= ~(HC_R0INT_ENA << i);
3060                                         writel(control, phba->HCregaddr);
3061                                         readl(phba->HCregaddr); /* flush */
3062                                         spin_unlock(phba->host->host_lock);
3063                                 }
3064                         }
3065                 }
3066
3067                 if (work_ha_copy & HA_ERATT) {
3068                         phba->hba_state = LPFC_HBA_ERROR;
3069                         /*
3070                          * There was a link/board error.  Read the
3071                          * status register to retrieve the error event
3072                          * and process it.
3073                          */
3074                         phba->sli.slistat.err_attn_event++;
3075                         /* Save status info */
3076                         phba->work_hs = readl(phba->HSregaddr);
3077                         phba->work_status[0] = readl(phba->MBslimaddr + 0xa8);
3078                         phba->work_status[1] = readl(phba->MBslimaddr + 0xac);
3079
3080                         /* Clear Chip error bit */
3081                         writel(HA_ERATT, phba->HAregaddr);
3082                         readl(phba->HAregaddr); /* flush */
3083                 }
3084
3085                 spin_lock(phba->host->host_lock);
3086                 phba->work_ha |= work_ha_copy;
3087                 if (phba->work_wait)
3088                         wake_up(phba->work_wait);
3089                 spin_unlock(phba->host->host_lock);
3090         }
3091
3092         ha_copy &= ~(phba->work_ha_mask);
3093
3094         /*
3095          * Process all events on FCP ring.  Take the optimized path for
3096          * FCP IO.  Any other IO is slow path and is handled by
3097          * the worker thread.
3098          */
3099         status = (ha_copy & (HA_RXMASK  << (4*LPFC_FCP_RING)));
3100         status >>= (4*LPFC_FCP_RING);
3101         if (status & HA_RXATT)
3102                 lpfc_sli_handle_fast_ring_event(phba,
3103                                                 &phba->sli.ring[LPFC_FCP_RING],
3104                                                 status);
3105         return IRQ_HANDLED;
3106
3107 } /* lpfc_intr_handler */