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