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