[SCSI] lpfc 8.1.7 : Fix race condition between lpfc_sli_issue_mbox and lpfc_online
[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         /* Turn off parity checking and serr during the physical reset */
1715         pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
1716         pci_write_config_word(phba->pcidev, PCI_COMMAND,
1717                               (cfg_value &
1718                                ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
1719
1720         psli->sli_flag &= ~(LPFC_SLI2_ACTIVE | LPFC_PROCESS_LA);
1721         /* Now toggle INITFF bit in the Host Control Register */
1722         writel(HC_INITFF, phba->HCregaddr);
1723         mdelay(1);
1724         readl(phba->HCregaddr); /* flush */
1725         writel(0, phba->HCregaddr);
1726         readl(phba->HCregaddr); /* flush */
1727
1728         /* Restore PCI cmd register */
1729         pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
1730
1731         /* Initialize relevant SLI info */
1732         for (i = 0; i < psli->num_rings; i++) {
1733                 pring = &psli->ring[i];
1734                 pring->flag = 0;
1735                 pring->rspidx = 0;
1736                 pring->next_cmdidx  = 0;
1737                 pring->local_getidx = 0;
1738                 pring->cmdidx = 0;
1739                 pring->missbufcnt = 0;
1740         }
1741
1742         phba->hba_state = LPFC_WARM_START;
1743         return 0;
1744 }
1745
1746 int
1747 lpfc_sli_brdrestart(struct lpfc_hba * phba)
1748 {
1749         MAILBOX_t *mb;
1750         struct lpfc_sli *psli;
1751         uint16_t skip_post;
1752         volatile uint32_t word0;
1753         void __iomem *to_slim;
1754
1755         spin_lock_irq(phba->host->host_lock);
1756
1757         psli = &phba->sli;
1758
1759         /* Restart HBA */
1760         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
1761                         "%d:0328 Restart HBA Data: x%x x%x\n", phba->brd_no,
1762                         phba->hba_state, psli->sli_flag);
1763
1764         word0 = 0;
1765         mb = (MAILBOX_t *) &word0;
1766         mb->mbxCommand = MBX_RESTART;
1767         mb->mbxHc = 1;
1768
1769         lpfc_reset_barrier(phba);
1770
1771         to_slim = phba->MBslimaddr;
1772         writel(*(uint32_t *) mb, to_slim);
1773         readl(to_slim); /* flush */
1774
1775         /* Only skip post after fc_ffinit is completed */
1776         if (phba->hba_state) {
1777                 skip_post = 1;
1778                 word0 = 1;      /* This is really setting up word1 */
1779         } else {
1780                 skip_post = 0;
1781                 word0 = 0;      /* This is really setting up word1 */
1782         }
1783         to_slim = phba->MBslimaddr + sizeof (uint32_t);
1784         writel(*(uint32_t *) mb, to_slim);
1785         readl(to_slim); /* flush */
1786
1787         lpfc_sli_brdreset(phba);
1788         phba->stopped = 0;
1789         phba->hba_state = LPFC_INIT_START;
1790
1791         spin_unlock_irq(phba->host->host_lock);
1792
1793         memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
1794         psli->stats_start = get_seconds();
1795
1796         if (skip_post)
1797                 mdelay(100);
1798         else
1799                 mdelay(2000);
1800
1801         lpfc_hba_down_post(phba);
1802
1803         return 0;
1804 }
1805
1806 static int
1807 lpfc_sli_chipset_init(struct lpfc_hba *phba)
1808 {
1809         uint32_t status, i = 0;
1810
1811         /* Read the HBA Host Status Register */
1812         status = readl(phba->HSregaddr);
1813
1814         /* Check status register to see what current state is */
1815         i = 0;
1816         while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
1817
1818                 /* Check every 100ms for 5 retries, then every 500ms for 5, then
1819                  * every 2.5 sec for 5, then reset board and every 2.5 sec for
1820                  * 4.
1821                  */
1822                 if (i++ >= 20) {
1823                         /* Adapter failed to init, timeout, status reg
1824                            <status> */
1825                         lpfc_printf_log(phba,
1826                                         KERN_ERR,
1827                                         LOG_INIT,
1828                                         "%d:0436 Adapter failed to init, "
1829                                         "timeout, status reg x%x\n",
1830                                         phba->brd_no,
1831                                         status);
1832                         phba->hba_state = LPFC_HBA_ERROR;
1833                         return -ETIMEDOUT;
1834                 }
1835
1836                 /* Check to see if any errors occurred during init */
1837                 if (status & HS_FFERM) {
1838                         /* ERROR: During chipset initialization */
1839                         /* Adapter failed to init, chipset, status reg
1840                            <status> */
1841                         lpfc_printf_log(phba,
1842                                         KERN_ERR,
1843                                         LOG_INIT,
1844                                         "%d:0437 Adapter failed to init, "
1845                                         "chipset, status reg x%x\n",
1846                                         phba->brd_no,
1847                                         status);
1848                         phba->hba_state = LPFC_HBA_ERROR;
1849                         return -EIO;
1850                 }
1851
1852                 if (i <= 5) {
1853                         msleep(10);
1854                 } else if (i <= 10) {
1855                         msleep(500);
1856                 } else {
1857                         msleep(2500);
1858                 }
1859
1860                 if (i == 15) {
1861                         phba->hba_state = LPFC_STATE_UNKNOWN; /* Do post */
1862                         lpfc_sli_brdrestart(phba);
1863                 }
1864                 /* Read the HBA Host Status Register */
1865                 status = readl(phba->HSregaddr);
1866         }
1867
1868         /* Check to see if any errors occurred during init */
1869         if (status & HS_FFERM) {
1870                 /* ERROR: During chipset initialization */
1871                 /* Adapter failed to init, chipset, status reg <status> */
1872                 lpfc_printf_log(phba,
1873                                 KERN_ERR,
1874                                 LOG_INIT,
1875                                 "%d:0438 Adapter failed to init, chipset, "
1876                                 "status reg x%x\n",
1877                                 phba->brd_no,
1878                                 status);
1879                 phba->hba_state = LPFC_HBA_ERROR;
1880                 return -EIO;
1881         }
1882
1883         /* Clear all interrupt enable conditions */
1884         writel(0, phba->HCregaddr);
1885         readl(phba->HCregaddr); /* flush */
1886
1887         /* setup host attn register */
1888         writel(0xffffffff, phba->HAregaddr);
1889         readl(phba->HAregaddr); /* flush */
1890         return 0;
1891 }
1892
1893 int
1894 lpfc_sli_hba_setup(struct lpfc_hba * phba)
1895 {
1896         LPFC_MBOXQ_t *pmb;
1897         uint32_t resetcount = 0, rc = 0, done = 0;
1898
1899         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1900         if (!pmb) {
1901                 phba->hba_state = LPFC_HBA_ERROR;
1902                 return -ENOMEM;
1903         }
1904
1905         while (resetcount < 2 && !done) {
1906                 spin_lock_irq(phba->host->host_lock);
1907                 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
1908                 spin_unlock_irq(phba->host->host_lock);
1909                 phba->hba_state = LPFC_STATE_UNKNOWN;
1910                 lpfc_sli_brdrestart(phba);
1911                 msleep(2500);
1912                 rc = lpfc_sli_chipset_init(phba);
1913                 if (rc)
1914                         break;
1915
1916                 spin_lock_irq(phba->host->host_lock);
1917                 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
1918                 spin_unlock_irq(phba->host->host_lock);
1919                 resetcount++;
1920
1921         /* Call pre CONFIG_PORT mailbox command initialization.  A value of 0
1922          * means the call was successful.  Any other nonzero value is a failure,
1923          * but if ERESTART is returned, the driver may reset the HBA and try
1924          * again.
1925          */
1926                 rc = lpfc_config_port_prep(phba);
1927                 if (rc == -ERESTART) {
1928                         phba->hba_state = 0;
1929                         continue;
1930                 } else if (rc) {
1931                         break;
1932                 }
1933
1934                 phba->hba_state = LPFC_INIT_MBX_CMDS;
1935                 lpfc_config_port(phba, pmb);
1936                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
1937                 if (rc == MBX_SUCCESS)
1938                         done = 1;
1939                 else {
1940                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1941                                 "%d:0442 Adapter failed to init, mbxCmd x%x "
1942                                 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
1943                                 phba->brd_no, pmb->mb.mbxCommand,
1944                                 pmb->mb.mbxStatus, 0);
1945                         phba->sli.sli_flag &= ~LPFC_SLI2_ACTIVE;
1946                 }
1947         }
1948         if (!done)
1949                 goto lpfc_sli_hba_setup_error;
1950
1951         rc = lpfc_sli_ring_map(phba, pmb);
1952
1953         if (rc)
1954                 goto lpfc_sli_hba_setup_error;
1955
1956         phba->sli.sli_flag |= LPFC_PROCESS_LA;
1957
1958         rc = lpfc_config_port_post(phba);
1959         if (rc)
1960                 goto lpfc_sli_hba_setup_error;
1961
1962         goto lpfc_sli_hba_setup_exit;
1963 lpfc_sli_hba_setup_error:
1964         phba->hba_state = LPFC_HBA_ERROR;
1965 lpfc_sli_hba_setup_exit:
1966         mempool_free(pmb, phba->mbox_mem_pool);
1967         return rc;
1968 }
1969
1970 static void
1971 lpfc_mbox_abort(struct lpfc_hba * phba)
1972 {
1973         LPFC_MBOXQ_t *pmbox;
1974         MAILBOX_t *mb;
1975
1976         if (phba->sli.mbox_active) {
1977                 del_timer_sync(&phba->sli.mbox_tmo);
1978                 phba->work_hba_events &= ~WORKER_MBOX_TMO;
1979                 pmbox = phba->sli.mbox_active;
1980                 mb = &pmbox->mb;
1981                 phba->sli.mbox_active = NULL;
1982                 if (pmbox->mbox_cmpl) {
1983                         mb->mbxStatus = MBX_NOT_FINISHED;
1984                         (pmbox->mbox_cmpl) (phba, pmbox);
1985                 }
1986                 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
1987         }
1988
1989         /* Abort all the non active mailbox commands. */
1990         spin_lock_irq(phba->host->host_lock);
1991         pmbox = lpfc_mbox_get(phba);
1992         while (pmbox) {
1993                 mb = &pmbox->mb;
1994                 if (pmbox->mbox_cmpl) {
1995                         mb->mbxStatus = MBX_NOT_FINISHED;
1996                         spin_unlock_irq(phba->host->host_lock);
1997                         (pmbox->mbox_cmpl) (phba, pmbox);
1998                         spin_lock_irq(phba->host->host_lock);
1999                 }
2000                 pmbox = lpfc_mbox_get(phba);
2001         }
2002         spin_unlock_irq(phba->host->host_lock);
2003         return;
2004 }
2005
2006 /*! lpfc_mbox_timeout
2007  *
2008  * \pre
2009  * \post
2010  * \param hba Pointer to per struct lpfc_hba structure
2011  * \param l1  Pointer to the driver's mailbox queue.
2012  * \return
2013  *   void
2014  *
2015  * \b Description:
2016  *
2017  * This routine handles mailbox timeout events at timer interrupt context.
2018  */
2019 void
2020 lpfc_mbox_timeout(unsigned long ptr)
2021 {
2022         struct lpfc_hba *phba;
2023         unsigned long iflag;
2024
2025         phba = (struct lpfc_hba *)ptr;
2026         spin_lock_irqsave(phba->host->host_lock, iflag);
2027         if (!(phba->work_hba_events & WORKER_MBOX_TMO)) {
2028                 phba->work_hba_events |= WORKER_MBOX_TMO;
2029                 if (phba->work_wait)
2030                         wake_up(phba->work_wait);
2031         }
2032         spin_unlock_irqrestore(phba->host->host_lock, iflag);
2033 }
2034
2035 void
2036 lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
2037 {
2038         LPFC_MBOXQ_t *pmbox;
2039         MAILBOX_t *mb;
2040
2041         spin_lock_irq(phba->host->host_lock);
2042         if (!(phba->work_hba_events & WORKER_MBOX_TMO)) {
2043                 spin_unlock_irq(phba->host->host_lock);
2044                 return;
2045         }
2046
2047         phba->work_hba_events &= ~WORKER_MBOX_TMO;
2048
2049         pmbox = phba->sli.mbox_active;
2050         mb = &pmbox->mb;
2051
2052         /* Mbox cmd <mbxCommand> timeout */
2053         lpfc_printf_log(phba,
2054                 KERN_ERR,
2055                 LOG_MBOX | LOG_SLI,
2056                 "%d:0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
2057                 phba->brd_no,
2058                 mb->mbxCommand,
2059                 phba->hba_state,
2060                 phba->sli.sli_flag,
2061                 phba->sli.mbox_active);
2062
2063         phba->sli.mbox_active = NULL;
2064         if (pmbox->mbox_cmpl) {
2065                 mb->mbxStatus = MBX_NOT_FINISHED;
2066                 spin_unlock_irq(phba->host->host_lock);
2067                 (pmbox->mbox_cmpl) (phba, pmbox);
2068                 spin_lock_irq(phba->host->host_lock);
2069         }
2070         phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
2071
2072         spin_unlock_irq(phba->host->host_lock);
2073         lpfc_mbox_abort(phba);
2074         return;
2075 }
2076
2077 int
2078 lpfc_sli_issue_mbox(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmbox, uint32_t flag)
2079 {
2080         MAILBOX_t *mb;
2081         struct lpfc_sli *psli;
2082         uint32_t status, evtctr;
2083         uint32_t ha_copy;
2084         int i;
2085         unsigned long drvr_flag = 0;
2086         volatile uint32_t word0, ldata;
2087         void __iomem *to_slim;
2088
2089         psli = &phba->sli;
2090
2091         spin_lock_irqsave(phba->host->host_lock, drvr_flag);
2092
2093
2094         mb = &pmbox->mb;
2095         status = MBX_SUCCESS;
2096
2097         if (phba->hba_state == LPFC_HBA_ERROR) {
2098                 spin_unlock_irqrestore(phba->host->host_lock, drvr_flag);
2099
2100                 /* Mbox command <mbxCommand> cannot issue */
2101                 LOG_MBOX_CANNOT_ISSUE_DATA( phba, mb, psli, flag)
2102                 return (MBX_NOT_FINISHED);
2103         }
2104
2105         if (mb->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT &&
2106             !(readl(phba->HCregaddr) & HC_MBINT_ENA)) {
2107                 spin_unlock_irqrestore(phba->host->host_lock, drvr_flag);
2108                 LOG_MBOX_CANNOT_ISSUE_DATA( phba, mb, psli, flag)
2109                 return (MBX_NOT_FINISHED);
2110         }
2111
2112         if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
2113                 /* Polling for a mbox command when another one is already active
2114                  * is not allowed in SLI. Also, the driver must have established
2115                  * SLI2 mode to queue and process multiple mbox commands.
2116                  */
2117
2118                 if (flag & MBX_POLL) {
2119                         spin_unlock_irqrestore(phba->host->host_lock,
2120                                                drvr_flag);
2121
2122                         /* Mbox command <mbxCommand> cannot issue */
2123                         LOG_MBOX_CANNOT_ISSUE_DATA( phba, mb, psli, flag)
2124                         return (MBX_NOT_FINISHED);
2125                 }
2126
2127                 if (!(psli->sli_flag & LPFC_SLI2_ACTIVE)) {
2128                         spin_unlock_irqrestore(phba->host->host_lock,
2129                                                drvr_flag);
2130                         /* Mbox command <mbxCommand> cannot issue */
2131                         LOG_MBOX_CANNOT_ISSUE_DATA( phba, mb, psli, flag)
2132                         return (MBX_NOT_FINISHED);
2133                 }
2134
2135                 /* Handle STOP IOCB processing flag. This is only meaningful
2136                  * if we are not polling for mbox completion.
2137                  */
2138                 if (flag & MBX_STOP_IOCB) {
2139                         flag &= ~MBX_STOP_IOCB;
2140                         /* Now flag each ring */
2141                         for (i = 0; i < psli->num_rings; i++) {
2142                                 /* If the ring is active, flag it */
2143                                 if (psli->ring[i].cmdringaddr) {
2144                                         psli->ring[i].flag |=
2145                                             LPFC_STOP_IOCB_MBX;
2146                                 }
2147                         }
2148                 }
2149
2150                 /* Another mailbox command is still being processed, queue this
2151                  * command to be processed later.
2152                  */
2153                 lpfc_mbox_put(phba, pmbox);
2154
2155                 /* Mbox cmd issue - BUSY */
2156                 lpfc_printf_log(phba,
2157                         KERN_INFO,
2158                         LOG_MBOX | LOG_SLI,
2159                         "%d:0308 Mbox cmd issue - BUSY Data: x%x x%x x%x x%x\n",
2160                         phba->brd_no,
2161                         mb->mbxCommand,
2162                         phba->hba_state,
2163                         psli->sli_flag,
2164                         flag);
2165
2166                 psli->slistat.mbox_busy++;
2167                 spin_unlock_irqrestore(phba->host->host_lock,
2168                                        drvr_flag);
2169
2170                 return (MBX_BUSY);
2171         }
2172
2173         /* Handle STOP IOCB processing flag. This is only meaningful
2174          * if we are not polling for mbox completion.
2175          */
2176         if (flag & MBX_STOP_IOCB) {
2177                 flag &= ~MBX_STOP_IOCB;
2178                 if (flag == MBX_NOWAIT) {
2179                         /* Now flag each ring */
2180                         for (i = 0; i < psli->num_rings; i++) {
2181                                 /* If the ring is active, flag it */
2182                                 if (psli->ring[i].cmdringaddr) {
2183                                         psli->ring[i].flag |=
2184                                             LPFC_STOP_IOCB_MBX;
2185                                 }
2186                         }
2187                 }
2188         }
2189
2190         psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
2191
2192         /* If we are not polling, we MUST be in SLI2 mode */
2193         if (flag != MBX_POLL) {
2194                 if (!(psli->sli_flag & LPFC_SLI2_ACTIVE) &&
2195                     (mb->mbxCommand != MBX_KILL_BOARD)) {
2196                         psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
2197                         spin_unlock_irqrestore(phba->host->host_lock,
2198                                                drvr_flag);
2199                         /* Mbox command <mbxCommand> cannot issue */
2200                         LOG_MBOX_CANNOT_ISSUE_DATA( phba, mb, psli, flag);
2201                         return (MBX_NOT_FINISHED);
2202                 }
2203                 /* timeout active mbox command */
2204                 mod_timer(&psli->mbox_tmo, (jiffies +
2205                                (HZ * lpfc_mbox_tmo_val(phba, mb->mbxCommand))));
2206         }
2207
2208         /* Mailbox cmd <cmd> issue */
2209         lpfc_printf_log(phba,
2210                 KERN_INFO,
2211                 LOG_MBOX | LOG_SLI,
2212                 "%d:0309 Mailbox cmd x%x issue Data: x%x x%x x%x\n",
2213                 phba->brd_no,
2214                 mb->mbxCommand,
2215                 phba->hba_state,
2216                 psli->sli_flag,
2217                 flag);
2218
2219         psli->slistat.mbox_cmd++;
2220         evtctr = psli->slistat.mbox_event;
2221
2222         /* next set own bit for the adapter and copy over command word */
2223         mb->mbxOwner = OWN_CHIP;
2224
2225         if (psli->sli_flag & LPFC_SLI2_ACTIVE) {
2226                 /* First copy command data to host SLIM area */
2227                 lpfc_sli_pcimem_bcopy(mb, &phba->slim2p->mbx, MAILBOX_CMD_SIZE);
2228         } else {
2229                 if (mb->mbxCommand == MBX_CONFIG_PORT) {
2230                         /* copy command data into host mbox for cmpl */
2231                         lpfc_sli_pcimem_bcopy(mb, &phba->slim2p->mbx,
2232                                         MAILBOX_CMD_SIZE);
2233                 }
2234
2235                 /* First copy mbox command data to HBA SLIM, skip past first
2236                    word */
2237                 to_slim = phba->MBslimaddr + sizeof (uint32_t);
2238                 lpfc_memcpy_to_slim(to_slim, &mb->un.varWords[0],
2239                             MAILBOX_CMD_SIZE - sizeof (uint32_t));
2240
2241                 /* Next copy over first word, with mbxOwner set */
2242                 ldata = *((volatile uint32_t *)mb);
2243                 to_slim = phba->MBslimaddr;
2244                 writel(ldata, to_slim);
2245                 readl(to_slim); /* flush */
2246
2247                 if (mb->mbxCommand == MBX_CONFIG_PORT) {
2248                         /* switch over to host mailbox */
2249                         psli->sli_flag |= LPFC_SLI2_ACTIVE;
2250                 }
2251         }
2252
2253         wmb();
2254         /* interrupt board to doit right away */
2255         writel(CA_MBATT, phba->CAregaddr);
2256         readl(phba->CAregaddr); /* flush */
2257
2258         switch (flag) {
2259         case MBX_NOWAIT:
2260                 /* Don't wait for it to finish, just return */
2261                 psli->mbox_active = pmbox;
2262                 break;
2263
2264         case MBX_POLL:
2265                 psli->mbox_active = NULL;
2266                 if (psli->sli_flag & LPFC_SLI2_ACTIVE) {
2267                         /* First read mbox status word */
2268                         word0 = *((volatile uint32_t *)&phba->slim2p->mbx);
2269                         word0 = le32_to_cpu(word0);
2270                 } else {
2271                         /* First read mbox status word */
2272                         word0 = readl(phba->MBslimaddr);
2273                 }
2274
2275                 /* Read the HBA Host Attention Register */
2276                 ha_copy = readl(phba->HAregaddr);
2277
2278                 i = lpfc_mbox_tmo_val(phba, mb->mbxCommand);
2279                 i *= 1000; /* Convert to ms */
2280
2281                 /* Wait for command to complete */
2282                 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
2283                        (!(ha_copy & HA_MBATT) &&
2284                         (phba->hba_state > LPFC_WARM_START))) {
2285                         if (i-- <= 0) {
2286                                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
2287                                 spin_unlock_irqrestore(phba->host->host_lock,
2288                                                        drvr_flag);
2289                                 return (MBX_NOT_FINISHED);
2290                         }
2291
2292                         /* Check if we took a mbox interrupt while we were
2293                            polling */
2294                         if (((word0 & OWN_CHIP) != OWN_CHIP)
2295                             && (evtctr != psli->slistat.mbox_event))
2296                                 break;
2297
2298                         spin_unlock_irqrestore(phba->host->host_lock,
2299                                                drvr_flag);
2300
2301                         /* Can be in interrupt context, do not sleep */
2302                         /* (or might be called with interrupts disabled) */
2303                         mdelay(1);
2304
2305                         spin_lock_irqsave(phba->host->host_lock, drvr_flag);
2306
2307                         if (psli->sli_flag & LPFC_SLI2_ACTIVE) {
2308                                 /* First copy command data */
2309                                 word0 = *((volatile uint32_t *)
2310                                                 &phba->slim2p->mbx);
2311                                 word0 = le32_to_cpu(word0);
2312                                 if (mb->mbxCommand == MBX_CONFIG_PORT) {
2313                                         MAILBOX_t *slimmb;
2314                                         volatile uint32_t slimword0;
2315                                         /* Check real SLIM for any errors */
2316                                         slimword0 = readl(phba->MBslimaddr);
2317                                         slimmb = (MAILBOX_t *) & slimword0;
2318                                         if (((slimword0 & OWN_CHIP) != OWN_CHIP)
2319                                             && slimmb->mbxStatus) {
2320                                                 psli->sli_flag &=
2321                                                     ~LPFC_SLI2_ACTIVE;
2322                                                 word0 = slimword0;
2323                                         }
2324                                 }
2325                         } else {
2326                                 /* First copy command data */
2327                                 word0 = readl(phba->MBslimaddr);
2328                         }
2329                         /* Read the HBA Host Attention Register */
2330                         ha_copy = readl(phba->HAregaddr);
2331                 }
2332
2333                 if (psli->sli_flag & LPFC_SLI2_ACTIVE) {
2334                         /* copy results back to user */
2335                         lpfc_sli_pcimem_bcopy(&phba->slim2p->mbx, mb,
2336                                         MAILBOX_CMD_SIZE);
2337                 } else {
2338                         /* First copy command data */
2339                         lpfc_memcpy_from_slim(mb, phba->MBslimaddr,
2340                                                         MAILBOX_CMD_SIZE);
2341                         if ((mb->mbxCommand == MBX_DUMP_MEMORY) &&
2342                                 pmbox->context2) {
2343                                 lpfc_memcpy_from_slim((void *)pmbox->context2,
2344                                       phba->MBslimaddr + DMP_RSP_OFFSET,
2345                                                       mb->un.varDmp.word_cnt);
2346                         }
2347                 }
2348
2349                 writel(HA_MBATT, phba->HAregaddr);
2350                 readl(phba->HAregaddr); /* flush */
2351
2352                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
2353                 status = mb->mbxStatus;
2354         }
2355
2356         spin_unlock_irqrestore(phba->host->host_lock, drvr_flag);
2357         return (status);
2358 }
2359
2360 static int
2361 lpfc_sli_ringtx_put(struct lpfc_hba * phba, struct lpfc_sli_ring * pring,
2362                     struct lpfc_iocbq * piocb)
2363 {
2364         /* Insert the caller's iocb in the txq tail for later processing. */
2365         list_add_tail(&piocb->list, &pring->txq);
2366         pring->txq_cnt++;
2367         return (0);
2368 }
2369
2370 static struct lpfc_iocbq *
2371 lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2372                    struct lpfc_iocbq ** piocb)
2373 {
2374         struct lpfc_iocbq * nextiocb;
2375
2376         nextiocb = lpfc_sli_ringtx_get(phba, pring);
2377         if (!nextiocb) {
2378                 nextiocb = *piocb;
2379                 *piocb = NULL;
2380         }
2381
2382         return nextiocb;
2383 }
2384
2385 int
2386 lpfc_sli_issue_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2387                     struct lpfc_iocbq *piocb, uint32_t flag)
2388 {
2389         struct lpfc_iocbq *nextiocb;
2390         IOCB_t *iocb;
2391
2392         /*
2393          * We should never get an IOCB if we are in a < LINK_DOWN state
2394          */
2395         if (unlikely(phba->hba_state < LPFC_LINK_DOWN))
2396                 return IOCB_ERROR;
2397
2398         /*
2399          * Check to see if we are blocking IOCB processing because of a
2400          * outstanding mbox command.
2401          */
2402         if (unlikely(pring->flag & LPFC_STOP_IOCB_MBX))
2403                 goto iocb_busy;
2404
2405         if (unlikely(phba->hba_state == LPFC_LINK_DOWN)) {
2406                 /*
2407                  * Only CREATE_XRI, CLOSE_XRI, ABORT_XRI, and QUE_RING_BUF
2408                  * can be issued if the link is not up.
2409                  */
2410                 switch (piocb->iocb.ulpCommand) {
2411                 case CMD_QUE_RING_BUF_CN:
2412                 case CMD_QUE_RING_BUF64_CN:
2413                         /*
2414                          * For IOCBs, like QUE_RING_BUF, that have no rsp ring
2415                          * completion, iocb_cmpl MUST be 0.
2416                          */
2417                         if (piocb->iocb_cmpl)
2418                                 piocb->iocb_cmpl = NULL;
2419                         /*FALLTHROUGH*/
2420                 case CMD_CREATE_XRI_CR:
2421                         break;
2422                 default:
2423                         goto iocb_busy;
2424                 }
2425
2426         /*
2427          * For FCP commands, we must be in a state where we can process link
2428          * attention events.
2429          */
2430         } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
2431                    !(phba->sli.sli_flag & LPFC_PROCESS_LA)))
2432                 goto iocb_busy;
2433
2434         while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
2435                (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
2436                 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
2437
2438         if (iocb)
2439                 lpfc_sli_update_ring(phba, pring);
2440         else
2441                 lpfc_sli_update_full_ring(phba, pring);
2442
2443         if (!piocb)
2444                 return IOCB_SUCCESS;
2445
2446         goto out_busy;
2447
2448  iocb_busy:
2449         pring->stats.iocb_cmd_delay++;
2450
2451  out_busy:
2452
2453         if (!(flag & SLI_IOCB_RET_IOCB)) {
2454                 lpfc_sli_ringtx_put(phba, pring, piocb);
2455                 return IOCB_SUCCESS;
2456         }
2457
2458         return IOCB_BUSY;
2459 }
2460
2461 static int
2462 lpfc_extra_ring_setup( struct lpfc_hba *phba)
2463 {
2464         struct lpfc_sli *psli;
2465         struct lpfc_sli_ring *pring;
2466
2467         psli = &phba->sli;
2468
2469         /* Adjust cmd/rsp ring iocb entries more evenly */
2470         pring = &psli->ring[psli->fcp_ring];
2471         pring->numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
2472         pring->numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
2473         pring->numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
2474         pring->numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
2475
2476         pring = &psli->ring[1];
2477         pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
2478         pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
2479         pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
2480         pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
2481
2482         /* Setup default profile for this ring */
2483         pring->iotag_max = 4096;
2484         pring->num_mask = 1;
2485         pring->prt[0].profile = 0;      /* Mask 0 */
2486         pring->prt[0].rctl = FC_UNSOL_DATA;
2487         pring->prt[0].type = 5;
2488         pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
2489         return 0;
2490 }
2491
2492 int
2493 lpfc_sli_setup(struct lpfc_hba *phba)
2494 {
2495         int i, totiocb = 0;
2496         struct lpfc_sli *psli = &phba->sli;
2497         struct lpfc_sli_ring *pring;
2498
2499         psli->num_rings = MAX_CONFIGURED_RINGS;
2500         psli->sli_flag = 0;
2501         psli->fcp_ring = LPFC_FCP_RING;
2502         psli->next_ring = LPFC_FCP_NEXT_RING;
2503         psli->ip_ring = LPFC_IP_RING;
2504
2505         psli->iocbq_lookup = NULL;
2506         psli->iocbq_lookup_len = 0;
2507         psli->last_iotag = 0;
2508
2509         for (i = 0; i < psli->num_rings; i++) {
2510                 pring = &psli->ring[i];
2511                 switch (i) {
2512                 case LPFC_FCP_RING:     /* ring 0 - FCP */
2513                         /* numCiocb and numRiocb are used in config_port */
2514                         pring->numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
2515                         pring->numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
2516                         pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
2517                         pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
2518                         pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
2519                         pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
2520                         pring->iotag_ctr = 0;
2521                         pring->iotag_max =
2522                             (phba->cfg_hba_queue_depth * 2);
2523                         pring->fast_iotag = pring->iotag_max;
2524                         pring->num_mask = 0;
2525                         break;
2526                 case LPFC_IP_RING:      /* ring 1 - IP */
2527                         /* numCiocb and numRiocb are used in config_port */
2528                         pring->numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
2529                         pring->numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
2530                         pring->num_mask = 0;
2531                         break;
2532                 case LPFC_ELS_RING:     /* ring 2 - ELS / CT */
2533                         /* numCiocb and numRiocb are used in config_port */
2534                         pring->numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
2535                         pring->numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
2536                         pring->fast_iotag = 0;
2537                         pring->iotag_ctr = 0;
2538                         pring->iotag_max = 4096;
2539                         pring->num_mask = 4;
2540                         pring->prt[0].profile = 0;      /* Mask 0 */
2541                         pring->prt[0].rctl = FC_ELS_REQ;
2542                         pring->prt[0].type = FC_ELS_DATA;
2543                         pring->prt[0].lpfc_sli_rcv_unsol_event =
2544                             lpfc_els_unsol_event;
2545                         pring->prt[1].profile = 0;      /* Mask 1 */
2546                         pring->prt[1].rctl = FC_ELS_RSP;
2547                         pring->prt[1].type = FC_ELS_DATA;
2548                         pring->prt[1].lpfc_sli_rcv_unsol_event =
2549                             lpfc_els_unsol_event;
2550                         pring->prt[2].profile = 0;      /* Mask 2 */
2551                         /* NameServer Inquiry */
2552                         pring->prt[2].rctl = FC_UNSOL_CTL;
2553                         /* NameServer */
2554                         pring->prt[2].type = FC_COMMON_TRANSPORT_ULP;
2555                         pring->prt[2].lpfc_sli_rcv_unsol_event =
2556                             lpfc_ct_unsol_event;
2557                         pring->prt[3].profile = 0;      /* Mask 3 */
2558                         /* NameServer response */
2559                         pring->prt[3].rctl = FC_SOL_CTL;
2560                         /* NameServer */
2561                         pring->prt[3].type = FC_COMMON_TRANSPORT_ULP;
2562                         pring->prt[3].lpfc_sli_rcv_unsol_event =
2563                             lpfc_ct_unsol_event;
2564                         break;
2565                 }
2566                 totiocb += (pring->numCiocb + pring->numRiocb);
2567         }
2568         if (totiocb > MAX_SLI2_IOCB) {
2569                 /* Too many cmd / rsp ring entries in SLI2 SLIM */
2570                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2571                                 "%d:0462 Too many cmd / rsp ring entries in "
2572                                 "SLI2 SLIM Data: x%x x%x\n",
2573                                 phba->brd_no, totiocb, MAX_SLI2_IOCB);
2574         }
2575         if (phba->cfg_multi_ring_support == 2)
2576                 lpfc_extra_ring_setup(phba);
2577
2578         return 0;
2579 }
2580
2581 int
2582 lpfc_sli_queue_setup(struct lpfc_hba * phba)
2583 {
2584         struct lpfc_sli *psli;
2585         struct lpfc_sli_ring *pring;
2586         int i;
2587
2588         psli = &phba->sli;
2589         spin_lock_irq(phba->host->host_lock);
2590         INIT_LIST_HEAD(&psli->mboxq);
2591         /* Initialize list headers for txq and txcmplq as double linked lists */
2592         for (i = 0; i < psli->num_rings; i++) {
2593                 pring = &psli->ring[i];
2594                 pring->ringno = i;
2595                 pring->next_cmdidx  = 0;
2596                 pring->local_getidx = 0;
2597                 pring->cmdidx = 0;
2598                 INIT_LIST_HEAD(&pring->txq);
2599                 INIT_LIST_HEAD(&pring->txcmplq);
2600                 INIT_LIST_HEAD(&pring->iocb_continueq);
2601                 INIT_LIST_HEAD(&pring->postbufq);
2602         }
2603         spin_unlock_irq(phba->host->host_lock);
2604         return (1);
2605 }
2606
2607 int
2608 lpfc_sli_hba_down(struct lpfc_hba * phba)
2609 {
2610         struct lpfc_sli *psli;
2611         struct lpfc_sli_ring *pring;
2612         LPFC_MBOXQ_t *pmb;
2613         struct lpfc_iocbq *iocb, *next_iocb;
2614         IOCB_t *icmd = NULL;
2615         int i;
2616         unsigned long flags = 0;
2617
2618         psli = &phba->sli;
2619         lpfc_hba_down_prep(phba);
2620
2621         spin_lock_irqsave(phba->host->host_lock, flags);
2622
2623         for (i = 0; i < psli->num_rings; i++) {
2624                 pring = &psli->ring[i];
2625                 pring->flag |= LPFC_DEFERRED_RING_EVENT;
2626
2627                 /*
2628                  * Error everything on the txq since these iocbs have not been
2629                  * given to the FW yet.
2630                  */
2631                 pring->txq_cnt = 0;
2632
2633                 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
2634                         list_del_init(&iocb->list);
2635                         if (iocb->iocb_cmpl) {
2636                                 icmd = &iocb->iocb;
2637                                 icmd->ulpStatus = IOSTAT_LOCAL_REJECT;
2638                                 icmd->un.ulpWord[4] = IOERR_SLI_DOWN;
2639                                 spin_unlock_irqrestore(phba->host->host_lock,
2640                                                        flags);
2641                                 (iocb->iocb_cmpl) (phba, iocb, iocb);
2642                                 spin_lock_irqsave(phba->host->host_lock, flags);
2643                         } else
2644                                 lpfc_sli_release_iocbq(phba, iocb);
2645                 }
2646
2647                 INIT_LIST_HEAD(&(pring->txq));
2648
2649         }
2650
2651         spin_unlock_irqrestore(phba->host->host_lock, flags);
2652
2653         /* Return any active mbox cmds */
2654         del_timer_sync(&psli->mbox_tmo);
2655         spin_lock_irqsave(phba->host->host_lock, flags);
2656         phba->work_hba_events &= ~WORKER_MBOX_TMO;
2657         if (psli->mbox_active) {
2658                 pmb = psli->mbox_active;
2659                 pmb->mb.mbxStatus = MBX_NOT_FINISHED;
2660                 if (pmb->mbox_cmpl) {
2661                         spin_unlock_irqrestore(phba->host->host_lock, flags);
2662                         pmb->mbox_cmpl(phba,pmb);
2663                         spin_lock_irqsave(phba->host->host_lock, flags);
2664                 }
2665         }
2666         psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
2667         psli->mbox_active = NULL;
2668
2669         /* Return any pending mbox cmds */
2670         while ((pmb = lpfc_mbox_get(phba)) != NULL) {
2671                 pmb->mb.mbxStatus = MBX_NOT_FINISHED;
2672                 if (pmb->mbox_cmpl) {
2673                         spin_unlock_irqrestore(phba->host->host_lock, flags);
2674                         pmb->mbox_cmpl(phba,pmb);
2675                         spin_lock_irqsave(phba->host->host_lock, flags);
2676                 }
2677         }
2678
2679         INIT_LIST_HEAD(&psli->mboxq);
2680
2681         spin_unlock_irqrestore(phba->host->host_lock, flags);
2682
2683         return 1;
2684 }
2685
2686 void
2687 lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
2688 {
2689         uint32_t *src = srcp;
2690         uint32_t *dest = destp;
2691         uint32_t ldata;
2692         int i;
2693
2694         for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
2695                 ldata = *src;
2696                 ldata = le32_to_cpu(ldata);
2697                 *dest = ldata;
2698                 src++;
2699                 dest++;
2700         }
2701 }
2702
2703 int
2704 lpfc_sli_ringpostbuf_put(struct lpfc_hba * phba, struct lpfc_sli_ring * pring,
2705                          struct lpfc_dmabuf * mp)
2706 {
2707         /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
2708            later */
2709         list_add_tail(&mp->list, &pring->postbufq);
2710
2711         pring->postbufq_cnt++;
2712         return 0;
2713 }
2714
2715
2716 struct lpfc_dmabuf *
2717 lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2718                          dma_addr_t phys)
2719 {
2720         struct lpfc_dmabuf *mp, *next_mp;
2721         struct list_head *slp = &pring->postbufq;
2722
2723         /* Search postbufq, from the begining, looking for a match on phys */
2724         list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
2725                 if (mp->phys == phys) {
2726                         list_del_init(&mp->list);
2727                         pring->postbufq_cnt--;
2728                         return mp;
2729                 }
2730         }
2731
2732         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2733                         "%d:0410 Cannot find virtual addr for mapped buf on "
2734                         "ring %d Data x%llx x%p x%p x%x\n",
2735                         phba->brd_no, pring->ringno, (unsigned long long)phys,
2736                         slp->next, slp->prev, pring->postbufq_cnt);
2737         return NULL;
2738 }
2739
2740 static void
2741 lpfc_sli_abort_elsreq_cmpl(struct lpfc_hba * phba, struct lpfc_iocbq * cmdiocb,
2742                            struct lpfc_iocbq * rspiocb)
2743 {
2744         struct lpfc_dmabuf *buf_ptr, *buf_ptr1;
2745         /* Free the resources associated with the ELS_REQUEST64 IOCB the driver
2746          * just aborted.
2747          * In this case, context2  = cmd,  context2->next = rsp, context3 = bpl
2748          */
2749         if (cmdiocb->context2) {
2750                 buf_ptr1 = (struct lpfc_dmabuf *) cmdiocb->context2;
2751
2752                 /* Free the response IOCB before completing the abort
2753                    command.  */
2754                 buf_ptr = NULL;
2755                 list_remove_head((&buf_ptr1->list), buf_ptr,
2756                                  struct lpfc_dmabuf, list);
2757                 if (buf_ptr) {
2758                         lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
2759                         kfree(buf_ptr);
2760                 }
2761                 lpfc_mbuf_free(phba, buf_ptr1->virt, buf_ptr1->phys);
2762                 kfree(buf_ptr1);
2763         }
2764
2765         if (cmdiocb->context3) {
2766                 buf_ptr = (struct lpfc_dmabuf *) cmdiocb->context3;
2767                 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
2768                 kfree(buf_ptr);
2769         }
2770
2771         lpfc_sli_release_iocbq(phba, cmdiocb);
2772         return;
2773 }
2774
2775 int
2776 lpfc_sli_issue_abort_iotag32(struct lpfc_hba * phba,
2777                              struct lpfc_sli_ring * pring,
2778                              struct lpfc_iocbq * cmdiocb)
2779 {
2780         struct lpfc_iocbq *abtsiocbp;
2781         IOCB_t *icmd = NULL;
2782         IOCB_t *iabt = NULL;
2783
2784         /* issue ABTS for this IOCB based on iotag */
2785         abtsiocbp = lpfc_sli_get_iocbq(phba);
2786         if (abtsiocbp == NULL)
2787                 return 0;
2788
2789         iabt = &abtsiocbp->iocb;
2790         icmd = &cmdiocb->iocb;
2791         switch (icmd->ulpCommand) {
2792         case CMD_ELS_REQUEST64_CR:
2793                 /* Even though we abort the ELS command, the firmware may access
2794                  * the BPL or other resources before it processes our
2795                  * ABORT_MXRI64. Thus we must delay reusing the cmdiocb
2796                  * resources till the actual abort request completes.
2797                  */
2798                 abtsiocbp->context1 = (void *)((unsigned long)icmd->ulpCommand);
2799                 abtsiocbp->context2 = cmdiocb->context2;
2800                 abtsiocbp->context3 = cmdiocb->context3;
2801                 cmdiocb->context2 = NULL;
2802                 cmdiocb->context3 = NULL;
2803                 abtsiocbp->iocb_cmpl = lpfc_sli_abort_elsreq_cmpl;
2804                 break;
2805         default:
2806                 lpfc_sli_release_iocbq(phba, abtsiocbp);
2807                 return 0;
2808         }
2809
2810         iabt->un.amxri.abortType = ABORT_TYPE_ABTS;
2811         iabt->un.amxri.iotag32 = icmd->un.elsreq64.bdl.ulpIoTag32;
2812
2813         iabt->ulpLe = 1;
2814         iabt->ulpClass = CLASS3;
2815         iabt->ulpCommand = CMD_ABORT_MXRI64_CN;
2816
2817         if (lpfc_sli_issue_iocb(phba, pring, abtsiocbp, 0) == IOCB_ERROR) {
2818                 lpfc_sli_release_iocbq(phba, abtsiocbp);
2819                 return 0;
2820         }
2821
2822         return 1;
2823 }
2824
2825 static int
2826 lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, uint16_t tgt_id,
2827                            uint64_t lun_id, uint32_t ctx,
2828                            lpfc_ctx_cmd ctx_cmd)
2829 {
2830         struct lpfc_scsi_buf *lpfc_cmd;
2831         struct scsi_cmnd *cmnd;
2832         int rc = 1;
2833
2834         if (!(iocbq->iocb_flag &  LPFC_IO_FCP))
2835                 return rc;
2836
2837         lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
2838         cmnd = lpfc_cmd->pCmd;
2839
2840         if (cmnd == NULL)
2841                 return rc;
2842
2843         switch (ctx_cmd) {
2844         case LPFC_CTX_LUN:
2845                 if ((cmnd->device->id == tgt_id) &&
2846                     (cmnd->device->lun == lun_id))
2847                         rc = 0;
2848                 break;
2849         case LPFC_CTX_TGT:
2850                 if (cmnd->device->id == tgt_id)
2851                         rc = 0;
2852                 break;
2853         case LPFC_CTX_CTX:
2854                 if (iocbq->iocb.ulpContext == ctx)
2855                         rc = 0;
2856                 break;
2857         case LPFC_CTX_HOST:
2858                 rc = 0;
2859                 break;
2860         default:
2861                 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
2862                         __FUNCTION__, ctx_cmd);
2863                 break;
2864         }
2865
2866         return rc;
2867 }
2868
2869 int
2870 lpfc_sli_sum_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2871                 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd ctx_cmd)
2872 {
2873         struct lpfc_iocbq *iocbq;
2874         int sum, i;
2875
2876         for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
2877                 iocbq = phba->sli.iocbq_lookup[i];
2878
2879                 if (lpfc_sli_validate_fcp_iocb (iocbq, tgt_id, lun_id,
2880                                                 0, ctx_cmd) == 0)
2881                         sum++;
2882         }
2883
2884         return sum;
2885 }
2886
2887 void
2888 lpfc_sli_abort_fcp_cmpl(struct lpfc_hba * phba, struct lpfc_iocbq * cmdiocb,
2889                            struct lpfc_iocbq * rspiocb)
2890 {
2891         spin_lock_irq(phba->host->host_lock);
2892         lpfc_sli_release_iocbq(phba, cmdiocb);
2893         spin_unlock_irq(phba->host->host_lock);
2894         return;
2895 }
2896
2897 int
2898 lpfc_sli_abort_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2899                     uint16_t tgt_id, uint64_t lun_id, uint32_t ctx,
2900                     lpfc_ctx_cmd abort_cmd)
2901 {
2902         struct lpfc_iocbq *iocbq;
2903         struct lpfc_iocbq *abtsiocb;
2904         IOCB_t *cmd = NULL;
2905         int errcnt = 0, ret_val = 0;
2906         int i;
2907
2908         for (i = 1; i <= phba->sli.last_iotag; i++) {
2909                 iocbq = phba->sli.iocbq_lookup[i];
2910
2911                 if (lpfc_sli_validate_fcp_iocb (iocbq, tgt_id, lun_id,
2912                                                 0, abort_cmd) != 0)
2913                         continue;
2914
2915                 /* issue ABTS for this IOCB based on iotag */
2916                 abtsiocb = lpfc_sli_get_iocbq(phba);
2917                 if (abtsiocb == NULL) {
2918                         errcnt++;
2919                         continue;
2920                 }
2921
2922                 cmd = &iocbq->iocb;
2923                 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
2924                 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
2925                 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
2926                 abtsiocb->iocb.ulpLe = 1;
2927                 abtsiocb->iocb.ulpClass = cmd->ulpClass;
2928
2929                 if (phba->hba_state >= LPFC_LINK_UP)
2930                         abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
2931                 else
2932                         abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
2933
2934                 /* Setup callback routine and issue the command. */
2935                 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
2936                 ret_val = lpfc_sli_issue_iocb(phba, pring, abtsiocb, 0);
2937                 if (ret_val == IOCB_ERROR) {
2938                         lpfc_sli_release_iocbq(phba, abtsiocb);
2939                         errcnt++;
2940                         continue;
2941                 }
2942         }
2943
2944         return errcnt;
2945 }
2946
2947 static void
2948 lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
2949                         struct lpfc_iocbq *cmdiocbq,
2950                         struct lpfc_iocbq *rspiocbq)
2951 {
2952         wait_queue_head_t *pdone_q;
2953         unsigned long iflags;
2954
2955         spin_lock_irqsave(phba->host->host_lock, iflags);
2956         cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
2957         if (cmdiocbq->context2 && rspiocbq)
2958                 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
2959                        &rspiocbq->iocb, sizeof(IOCB_t));
2960
2961         pdone_q = cmdiocbq->context_un.wait_queue;
2962         spin_unlock_irqrestore(phba->host->host_lock, iflags);
2963         if (pdone_q)
2964                 wake_up(pdone_q);
2965         return;
2966 }
2967
2968 /*
2969  * Issue the caller's iocb and wait for its completion, but no longer than the
2970  * caller's timeout.  Note that iocb_flags is cleared before the
2971  * lpfc_sli_issue_call since the wake routine sets a unique value and by
2972  * definition this is a wait function.
2973  */
2974 int
2975 lpfc_sli_issue_iocb_wait(struct lpfc_hba * phba,
2976                          struct lpfc_sli_ring * pring,
2977                          struct lpfc_iocbq * piocb,
2978                          struct lpfc_iocbq * prspiocbq,
2979                          uint32_t timeout)
2980 {
2981         DECLARE_WAIT_QUEUE_HEAD(done_q);
2982         long timeleft, timeout_req = 0;
2983         int retval = IOCB_SUCCESS;
2984         uint32_t creg_val;
2985
2986         /*
2987          * If the caller has provided a response iocbq buffer, then context2
2988          * is NULL or its an error.
2989          */
2990         if (prspiocbq) {
2991                 if (piocb->context2)
2992                         return IOCB_ERROR;
2993                 piocb->context2 = prspiocbq;
2994         }
2995
2996         piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
2997         piocb->context_un.wait_queue = &done_q;
2998         piocb->iocb_flag &= ~LPFC_IO_WAKE;
2999
3000         if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
3001                 creg_val = readl(phba->HCregaddr);
3002                 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
3003                 writel(creg_val, phba->HCregaddr);
3004                 readl(phba->HCregaddr); /* flush */
3005         }
3006
3007         retval = lpfc_sli_issue_iocb(phba, pring, piocb, 0);
3008         if (retval == IOCB_SUCCESS) {
3009                 timeout_req = timeout * HZ;
3010                 spin_unlock_irq(phba->host->host_lock);
3011                 timeleft = wait_event_timeout(done_q,
3012                                 piocb->iocb_flag & LPFC_IO_WAKE,
3013                                 timeout_req);
3014                 spin_lock_irq(phba->host->host_lock);
3015
3016                 if (timeleft == 0) {
3017                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3018                                         "%d:0329 IOCB wait timeout error - no "
3019                                         "wake response Data x%x\n",
3020                                         phba->brd_no, timeout);
3021                         retval = IOCB_TIMEDOUT;
3022                 } else if (!(piocb->iocb_flag & LPFC_IO_WAKE)) {
3023                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3024                                         "%d:0330 IOCB wake NOT set, "
3025                                         "Data x%x x%lx\n", phba->brd_no,
3026                                         timeout, (timeleft / jiffies));
3027                         retval = IOCB_TIMEDOUT;
3028                 } else {
3029                         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3030                                         "%d:0331 IOCB wake signaled\n",
3031                                         phba->brd_no);
3032                 }
3033         } else {
3034                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3035                                 "%d:0332 IOCB wait issue failed, Data x%x\n",
3036                                 phba->brd_no, retval);
3037                 retval = IOCB_ERROR;
3038         }
3039
3040         if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
3041                 creg_val = readl(phba->HCregaddr);
3042                 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
3043                 writel(creg_val, phba->HCregaddr);
3044                 readl(phba->HCregaddr); /* flush */
3045         }
3046
3047         if (prspiocbq)
3048                 piocb->context2 = NULL;
3049
3050         piocb->context_un.wait_queue = NULL;
3051         piocb->iocb_cmpl = NULL;
3052         return retval;
3053 }
3054
3055 int
3056 lpfc_sli_issue_mbox_wait(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq,
3057                          uint32_t timeout)
3058 {
3059         DECLARE_WAIT_QUEUE_HEAD(done_q);
3060         DECLARE_WAITQUEUE(wq_entry, current);
3061         uint32_t timeleft = 0;
3062         int retval;
3063
3064         /* The caller must leave context1 empty. */
3065         if (pmboxq->context1 != 0) {
3066                 return (MBX_NOT_FINISHED);
3067         }
3068
3069         /* setup wake call as IOCB callback */
3070         pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
3071         /* setup context field to pass wait_queue pointer to wake function  */
3072         pmboxq->context1 = &done_q;
3073
3074         /* start to sleep before we wait, to avoid races */
3075         set_current_state(TASK_INTERRUPTIBLE);
3076         add_wait_queue(&done_q, &wq_entry);
3077
3078         /* now issue the command */
3079         retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
3080
3081         if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
3082                 timeleft = schedule_timeout(timeout * HZ);
3083                 pmboxq->context1 = NULL;
3084                 /* if schedule_timeout returns 0, we timed out and were not
3085                    woken up */
3086                 if ((timeleft == 0) || signal_pending(current))
3087                         retval = MBX_TIMEOUT;
3088                 else
3089                         retval = MBX_SUCCESS;
3090         }
3091
3092
3093         set_current_state(TASK_RUNNING);
3094         remove_wait_queue(&done_q, &wq_entry);
3095         return retval;
3096 }
3097
3098 int
3099 lpfc_sli_flush_mbox_queue(struct lpfc_hba * phba)
3100 {
3101         int i = 0;
3102
3103         while (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE && !phba->stopped) {
3104                 if (i++ > LPFC_MBOX_TMO * 1000)
3105                         return 1;
3106
3107                 if (lpfc_sli_handle_mb_event(phba) == 0)
3108                         i = 0;
3109
3110                 msleep(1);
3111         }
3112
3113         return (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) ? 1 : 0;
3114 }
3115
3116 irqreturn_t
3117 lpfc_intr_handler(int irq, void *dev_id, struct pt_regs * regs)
3118 {
3119         struct lpfc_hba *phba;
3120         uint32_t ha_copy;
3121         uint32_t work_ha_copy;
3122         unsigned long status;
3123         int i;
3124         uint32_t control;
3125
3126         /*
3127          * Get the driver's phba structure from the dev_id and
3128          * assume the HBA is not interrupting.
3129          */
3130         phba = (struct lpfc_hba *) dev_id;
3131
3132         if (unlikely(!phba))
3133                 return IRQ_NONE;
3134
3135         phba->sli.slistat.sli_intr++;
3136
3137         /*
3138          * Call the HBA to see if it is interrupting.  If not, don't claim
3139          * the interrupt
3140          */
3141
3142         /* Ignore all interrupts during initialization. */
3143         if (unlikely(phba->hba_state < LPFC_LINK_DOWN))
3144                 return IRQ_NONE;
3145
3146         /*
3147          * Read host attention register to determine interrupt source
3148          * Clear Attention Sources, except Error Attention (to
3149          * preserve status) and Link Attention
3150          */
3151         spin_lock(phba->host->host_lock);
3152         ha_copy = readl(phba->HAregaddr);
3153         writel((ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
3154         readl(phba->HAregaddr); /* flush */
3155         spin_unlock(phba->host->host_lock);
3156
3157         if (unlikely(!ha_copy))
3158                 return IRQ_NONE;
3159
3160         work_ha_copy = ha_copy & phba->work_ha_mask;
3161
3162         if (unlikely(work_ha_copy)) {
3163                 if (work_ha_copy & HA_LATT) {
3164                         if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
3165                                 /*
3166                                  * Turn off Link Attention interrupts
3167                                  * until CLEAR_LA done
3168                                  */
3169                                 spin_lock(phba->host->host_lock);
3170                                 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
3171                                 control = readl(phba->HCregaddr);
3172                                 control &= ~HC_LAINT_ENA;
3173                                 writel(control, phba->HCregaddr);
3174                                 readl(phba->HCregaddr); /* flush */
3175                                 spin_unlock(phba->host->host_lock);
3176                         }
3177                         else
3178                                 work_ha_copy &= ~HA_LATT;
3179                 }
3180
3181                 if (work_ha_copy & ~(HA_ERATT|HA_MBATT|HA_LATT)) {
3182                         for (i = 0; i < phba->sli.num_rings; i++) {
3183                                 if (work_ha_copy & (HA_RXATT << (4*i))) {
3184                                         /*
3185                                          * Turn off Slow Rings interrupts
3186                                          */
3187                                         spin_lock(phba->host->host_lock);
3188                                         control = readl(phba->HCregaddr);
3189                                         control &= ~(HC_R0INT_ENA << i);
3190                                         writel(control, phba->HCregaddr);
3191                                         readl(phba->HCregaddr); /* flush */
3192                                         spin_unlock(phba->host->host_lock);
3193                                 }
3194                         }
3195                 }
3196
3197                 if (work_ha_copy & HA_ERATT) {
3198                         phba->hba_state = LPFC_HBA_ERROR;
3199                         /*
3200                          * There was a link/board error.  Read the
3201                          * status register to retrieve the error event
3202                          * and process it.
3203                          */
3204                         phba->sli.slistat.err_attn_event++;
3205                         /* Save status info */
3206                         phba->work_hs = readl(phba->HSregaddr);
3207                         phba->work_status[0] = readl(phba->MBslimaddr + 0xa8);
3208                         phba->work_status[1] = readl(phba->MBslimaddr + 0xac);
3209
3210                         /* Clear Chip error bit */
3211                         writel(HA_ERATT, phba->HAregaddr);
3212                         readl(phba->HAregaddr); /* flush */
3213                         phba->stopped = 1;
3214                 }
3215
3216                 spin_lock(phba->host->host_lock);
3217                 phba->work_ha |= work_ha_copy;
3218                 if (phba->work_wait)
3219                         wake_up(phba->work_wait);
3220                 spin_unlock(phba->host->host_lock);
3221         }
3222
3223         ha_copy &= ~(phba->work_ha_mask);
3224
3225         /*
3226          * Process all events on FCP ring.  Take the optimized path for
3227          * FCP IO.  Any other IO is slow path and is handled by
3228          * the worker thread.
3229          */
3230         status = (ha_copy & (HA_RXMASK  << (4*LPFC_FCP_RING)));
3231         status >>= (4*LPFC_FCP_RING);
3232         if (status & HA_RXATT)
3233                 lpfc_sli_handle_fast_ring_event(phba,
3234                                                 &phba->sli.ring[LPFC_FCP_RING],
3235                                                 status);
3236         return IRQ_HANDLED;
3237
3238 } /* lpfc_intr_handler */