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