1 /*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
3 * Enterprise Fibre Channel Host Bus Adapters. *
4 * Refer to the README file included with this package for *
5 * driver version and adapter support. *
6 * Copyright (C) 2004 Emulex Corporation. *
9 * This program is free software; you can redistribute it and/or *
10 * modify it under the terms of the GNU General Public License *
11 * as published by the Free Software Foundation; either version 2 *
12 * of the License, or (at your option) any later version. *
14 * This program is distributed in the hope that it will be useful, *
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
17 * GNU General Public License for more details, a copy of which *
18 * can be found in the file COPYING included with this package. *
19 *******************************************************************/
22 * $Id: lpfc_sli.c 1.232 2005/04/13 11:59:16EDT sf_support Exp $
25 #include <linux/blkdev.h>
26 #include <linux/pci.h>
27 #include <linux/interrupt.h>
28 #include <linux/delay.h>
30 #include <scsi/scsi_cmnd.h>
31 #include <scsi/scsi_device.h>
32 #include <scsi/scsi_host.h>
36 #include "lpfc_disc.h"
37 #include "lpfc_scsi.h"
39 #include "lpfc_crtn.h"
40 #include "lpfc_logmsg.h"
41 #include "lpfc_compat.h"
44 * Define macro to log: Mailbox command x%x cannot issue Data
45 * This allows multiple uses of lpfc_msgBlk0311
46 * w/o perturbing log msg utility.
48 #define LOG_MBOX_CANNOT_ISSUE_DATA( phba, mb, psli, flag) \
49 lpfc_printf_log(phba, \
52 "%d:0311 Mailbox command x%x cannot issue " \
53 "Data: x%x x%x x%x\n", \
61 /* There are only four IOCB completion types. */
62 typedef enum _lpfc_iocb_type {
70 * Translate the iocb command to an iocb command type used to decide the final
71 * disposition of each completed IOCB.
74 lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
76 lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
78 if (iocb_cmnd > CMD_MAX_IOCB_CMD)
82 case CMD_XMIT_SEQUENCE_CR:
83 case CMD_XMIT_SEQUENCE_CX:
84 case CMD_XMIT_BCAST_CN:
85 case CMD_XMIT_BCAST_CX:
86 case CMD_ELS_REQUEST_CR:
87 case CMD_ELS_REQUEST_CX:
88 case CMD_CREATE_XRI_CR:
89 case CMD_CREATE_XRI_CX:
91 case CMD_XMIT_ELS_RSP_CX:
93 case CMD_FCP_IWRITE_CR:
94 case CMD_FCP_IWRITE_CX:
95 case CMD_FCP_IREAD_CR:
96 case CMD_FCP_IREAD_CX:
97 case CMD_FCP_ICMND_CR:
98 case CMD_FCP_ICMND_CX:
100 case CMD_ADAPTER_DUMP:
101 case CMD_XMIT_SEQUENCE64_CR:
102 case CMD_XMIT_SEQUENCE64_CX:
103 case CMD_XMIT_BCAST64_CN:
104 case CMD_XMIT_BCAST64_CX:
105 case CMD_ELS_REQUEST64_CR:
106 case CMD_ELS_REQUEST64_CX:
107 case CMD_FCP_IWRITE64_CR:
108 case CMD_FCP_IWRITE64_CX:
109 case CMD_FCP_IREAD64_CR:
110 case CMD_FCP_IREAD64_CX:
111 case CMD_FCP_ICMND64_CR:
112 case CMD_FCP_ICMND64_CX:
113 case CMD_GEN_REQUEST64_CR:
114 case CMD_GEN_REQUEST64_CX:
115 case CMD_XMIT_ELS_RSP64_CX:
116 type = LPFC_SOL_IOCB;
118 case CMD_ABORT_XRI_CN:
119 case CMD_ABORT_XRI_CX:
120 case CMD_CLOSE_XRI_CN:
121 case CMD_CLOSE_XRI_CX:
122 case CMD_XRI_ABORTED_CX:
123 case CMD_ABORT_MXRI64_CN:
124 type = LPFC_ABORT_IOCB;
126 case CMD_RCV_SEQUENCE_CX:
127 case CMD_RCV_ELS_REQ_CX:
128 case CMD_RCV_SEQUENCE64_CX:
129 case CMD_RCV_ELS_REQ64_CX:
130 type = LPFC_UNSOL_IOCB;
133 type = LPFC_UNKNOWN_IOCB;
141 lpfc_sli_ring_map(struct lpfc_hba * phba, LPFC_MBOXQ_t *pmb)
143 struct lpfc_sli *psli = &phba->sli;
144 MAILBOX_t *pmbox = &pmb->mb;
147 for (i = 0; i < psli->num_rings; i++) {
148 phba->hba_state = LPFC_INIT_MBX_CMDS;
149 lpfc_config_ring(phba, i, pmb);
150 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
151 if (rc != MBX_SUCCESS) {
152 lpfc_printf_log(phba,
155 "%d:0446 Adapter failed to init, "
156 "mbxCmd x%x CFG_RING, mbxStatus x%x, "
162 phba->hba_state = LPFC_HBA_ERROR;
170 lpfc_sli_ringtxcmpl_put(struct lpfc_hba * phba,
171 struct lpfc_sli_ring * pring, struct lpfc_iocbq * piocb)
175 list_add_tail(&piocb->list, &pring->txcmplq);
176 pring->txcmplq_cnt++;
177 if (unlikely(pring->ringno == LPFC_ELS_RING))
178 mod_timer(&phba->els_tmofunc,
179 jiffies + HZ * (phba->fc_ratov << 1));
181 if (pring->fast_lookup) {
182 /* Setup fast lookup based on iotag for completion */
183 iotag = piocb->iocb.ulpIoTag;
184 if (iotag && (iotag < pring->fast_iotag))
185 *(pring->fast_lookup + iotag) = piocb;
188 /* Cmd ring <ringno> put: iotag <iotag> greater then
189 configured max <fast_iotag> wd0 <icmd> */
190 lpfc_printf_log(phba,
193 "%d:0316 Cmd ring %d put: iotag x%x "
194 "greater then configured max x%x "
197 pring->ringno, iotag,
199 *(((uint32_t *)(&piocb->iocb)) + 7));
205 static struct lpfc_iocbq *
206 lpfc_sli_ringtx_get(struct lpfc_hba * phba, struct lpfc_sli_ring * pring)
208 struct list_head *dlp;
209 struct lpfc_iocbq *cmd_iocb;
213 list_remove_head((&pring->txq), cmd_iocb,
217 /* If the first ptr is not equal to the list header,
218 * deque the IOCBQ_t and return it.
226 lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
228 struct lpfc_pgp *pgp = &phba->slim2p->mbx.us.s2.port[pring->ringno];
229 uint32_t max_cmd_idx = pring->numCiocb;
232 if ((pring->next_cmdidx == pring->cmdidx) &&
233 (++pring->next_cmdidx >= max_cmd_idx))
234 pring->next_cmdidx = 0;
236 if (unlikely(pring->local_getidx == pring->next_cmdidx)) {
238 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
240 if (unlikely(pring->local_getidx >= max_cmd_idx)) {
241 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
242 "%d:0315 Ring %d issue: portCmdGet %d "
243 "is bigger then cmd ring %d\n",
244 phba->brd_no, pring->ringno,
245 pring->local_getidx, max_cmd_idx);
247 phba->hba_state = LPFC_HBA_ERROR;
249 * All error attention handlers are posted to
252 phba->work_ha |= HA_ERATT;
253 phba->work_hs = HS_FFER3;
255 wake_up(phba->work_wait);
260 if (pring->local_getidx == pring->next_cmdidx)
264 iocb = IOCB_ENTRY(pring->cmdringaddr, pring->cmdidx);
270 lpfc_sli_next_iotag(struct lpfc_hba * phba, struct lpfc_sli_ring * pring)
272 uint32_t search_start;
274 if (pring->fast_lookup == NULL) {
276 if (pring->iotag_ctr >= pring->iotag_max)
277 pring->iotag_ctr = 1;
278 return pring->iotag_ctr;
281 search_start = pring->iotag_ctr;
285 if (pring->iotag_ctr >= pring->fast_iotag)
286 pring->iotag_ctr = 1;
288 if (*(pring->fast_lookup + pring->iotag_ctr) == NULL)
289 return pring->iotag_ctr;
291 } while (pring->iotag_ctr != search_start);
294 * Outstanding I/O count for ring <ringno> is at max <fast_iotag>
296 lpfc_printf_log(phba,
299 "%d:0318 Outstanding I/O count for ring %d is at max x%x\n",
307 lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
308 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
311 * Allocate and set up an iotag
313 nextiocb->iocb.ulpIoTag =
314 lpfc_sli_next_iotag(phba, &phba->sli.ring[phba->sli.fcp_ring]);
317 * Issue iocb command to adapter
319 lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, sizeof (IOCB_t));
321 pring->stats.iocb_cmd++;
324 * If there is no completion routine to call, we can release the
325 * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
326 * that have no rsp ring completion, iocb_cmpl MUST be NULL.
328 if (nextiocb->iocb_cmpl)
329 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
331 list_add_tail(&nextiocb->list, &phba->lpfc_iocb_list);
335 * Let the HBA know what IOCB slot will be the next one the
336 * driver will put a command into.
338 pring->cmdidx = pring->next_cmdidx;
339 writeb(pring->cmdidx, phba->MBslimaddr
340 + (SLIMOFF + (pring->ringno * 2)) * 4);
344 lpfc_sli_update_full_ring(struct lpfc_hba * phba,
345 struct lpfc_sli_ring *pring)
347 int ringno = pring->ringno;
349 pring->flag |= LPFC_CALL_RING_AVAILABLE;
354 * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
355 * The HBA will tell us when an IOCB entry is available.
357 writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
358 readl(phba->CAregaddr); /* flush */
360 pring->stats.iocb_cmd_full++;
364 lpfc_sli_update_ring(struct lpfc_hba * phba,
365 struct lpfc_sli_ring *pring)
367 int ringno = pring->ringno;
370 * Tell the HBA that there is work to do in this ring.
373 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
374 readl(phba->CAregaddr); /* flush */
378 lpfc_sli_resume_iocb(struct lpfc_hba * phba, struct lpfc_sli_ring * pring)
381 struct lpfc_iocbq *nextiocb;
385 * (a) there is anything on the txq to send
387 * (c) link attention events can be processed (fcp ring only)
388 * (d) IOCB processing is not blocked by the outstanding mbox command.
390 if (pring->txq_cnt &&
391 (phba->hba_state > LPFC_LINK_DOWN) &&
392 (pring->ringno != phba->sli.fcp_ring ||
393 phba->sli.sli_flag & LPFC_PROCESS_LA) &&
394 !(pring->flag & LPFC_STOP_IOCB_MBX)) {
396 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
397 (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
398 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
401 lpfc_sli_update_ring(phba, pring);
403 lpfc_sli_update_full_ring(phba, pring);
409 /* lpfc_sli_turn_on_ring is only called by lpfc_sli_handle_mb_event below */
411 lpfc_sli_turn_on_ring(struct lpfc_hba * phba, int ringno)
413 struct lpfc_pgp *pgp = &phba->slim2p->mbx.us.s2.port[ringno];
415 /* If the ring is active, flag it */
416 if (phba->sli.ring[ringno].cmdringaddr) {
417 if (phba->sli.ring[ringno].flag & LPFC_STOP_IOCB_MBX) {
418 phba->sli.ring[ringno].flag &= ~LPFC_STOP_IOCB_MBX;
420 * Force update of the local copy of cmdGetInx
422 phba->sli.ring[ringno].local_getidx
423 = le32_to_cpu(pgp->cmdGetInx);
424 spin_lock_irq(phba->host->host_lock);
425 lpfc_sli_resume_iocb(phba, &phba->sli.ring[ringno]);
426 spin_unlock_irq(phba->host->host_lock);
432 lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
436 switch (mbxCommand) {
440 case MBX_RUN_BIU_DIAG:
443 case MBX_CONFIG_LINK:
444 case MBX_CONFIG_RING:
446 case MBX_READ_CONFIG:
447 case MBX_READ_RCONFIG:
449 case MBX_READ_STATUS:
453 case MBX_READ_LNK_STAT:
455 case MBX_UNREG_LOGIN:
458 case MBX_DUMP_MEMORY:
459 case MBX_DUMP_CONTEXT:
464 case MBX_DEL_LD_ENTRY:
465 case MBX_RUN_PROGRAM:
469 case MBX_CONFIG_FARP:
471 case MBX_RUN_BIU_DIAG64:
472 case MBX_CONFIG_PORT:
473 case MBX_READ_SPARM64:
475 case MBX_REG_LOGIN64:
477 case MBX_FLASH_WR_ULA:
479 case MBX_LOAD_EXP_ROM:
489 lpfc_sli_wake_mbox_wait(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
491 wait_queue_head_t *pdone_q;
494 * If pdone_q is empty, the driver thread gave up waiting and
497 pdone_q = (wait_queue_head_t *) pmboxq->context1;
499 wake_up_interruptible(pdone_q);
504 lpfc_sli_def_mbox_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmb)
506 struct lpfc_dmabuf *mp;
507 mp = (struct lpfc_dmabuf *) (pmb->context1);
509 lpfc_mbuf_free(phba, mp->virt, mp->phys);
512 mempool_free( pmb, phba->mbox_mem_pool);
517 lpfc_sli_handle_mb_event(struct lpfc_hba * phba)
522 struct lpfc_sli *psli;
524 uint32_t process_next;
527 /* We should only get here if we are in SLI2 mode */
528 if (!(phba->sli.sli_flag & LPFC_SLI2_ACTIVE)) {
532 phba->sli.slistat.mbox_event++;
534 /* Get a Mailbox buffer to setup mailbox commands for callback */
535 if ((pmb = phba->sli.mbox_active)) {
537 mbox = &phba->slim2p->mbx;
539 /* First check out the status word */
540 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof (uint32_t));
542 /* Sanity check to ensure the host owns the mailbox */
543 if (pmbox->mbxOwner != OWN_HOST) {
544 /* Lets try for a while */
545 for (i = 0; i < 10240; i++) {
546 /* First copy command data */
547 lpfc_sli_pcimem_bcopy(mbox, pmbox,
549 if (pmbox->mbxOwner == OWN_HOST)
552 /* Stray Mailbox Interrupt, mbxCommand <cmd> mbxStatus
554 lpfc_printf_log(phba,
557 "%d:0304 Stray Mailbox Interrupt "
558 "mbxCommand x%x mbxStatus x%x\n",
563 spin_lock_irq(phba->host->host_lock);
564 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
565 spin_unlock_irq(phba->host->host_lock);
570 del_timer_sync(&phba->sli.mbox_tmo);
571 phba->work_hba_events &= ~WORKER_MBOX_TMO;
574 * It is a fatal error if unknown mbox command completion.
576 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
579 /* Unknow mailbox command compl */
580 lpfc_printf_log(phba,
583 "%d:0323 Unknown Mailbox command %x Cmpl\n",
586 phba->hba_state = LPFC_HBA_ERROR;
587 phba->work_hs = HS_FFER3;
588 lpfc_handle_eratt(phba);
592 phba->sli.mbox_active = NULL;
593 if (pmbox->mbxStatus) {
594 phba->sli.slistat.mbox_stat_err++;
595 if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
596 /* Mbox cmd cmpl error - RETRYing */
597 lpfc_printf_log(phba,
600 "%d:0305 Mbox cmd cmpl error - "
601 "RETRYing Data: x%x x%x x%x x%x\n",
605 pmbox->un.varWords[0],
607 pmbox->mbxStatus = 0;
608 pmbox->mbxOwner = OWN_HOST;
609 spin_lock_irq(phba->host->host_lock);
610 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
611 spin_unlock_irq(phba->host->host_lock);
612 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
613 if (rc == MBX_SUCCESS)
618 /* Mailbox cmd <cmd> Cmpl <cmpl> */
619 lpfc_printf_log(phba,
622 "%d:0307 Mailbox cmd x%x Cmpl x%p "
623 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x\n",
627 *((uint32_t *) pmbox),
628 pmbox->un.varWords[0],
629 pmbox->un.varWords[1],
630 pmbox->un.varWords[2],
631 pmbox->un.varWords[3],
632 pmbox->un.varWords[4],
633 pmbox->un.varWords[5],
634 pmbox->un.varWords[6],
635 pmbox->un.varWords[7]);
637 if (pmb->mbox_cmpl) {
638 lpfc_sli_pcimem_bcopy(mbox, pmbox, MAILBOX_CMD_SIZE);
639 pmb->mbox_cmpl(phba,pmb);
645 process_next = 0; /* by default don't loop */
646 spin_lock_irq(phba->host->host_lock);
647 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
649 /* Process next mailbox command if there is one */
650 if ((pmb = lpfc_mbox_get(phba))) {
651 spin_unlock_irq(phba->host->host_lock);
652 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
653 if (rc == MBX_NOT_FINISHED) {
654 pmb->mb.mbxStatus = MBX_NOT_FINISHED;
655 pmb->mbox_cmpl(phba,pmb);
657 continue; /* loop back */
660 spin_unlock_irq(phba->host->host_lock);
661 /* Turn on IOCB processing */
662 for (i = 0; i < phba->sli.num_rings; i++) {
663 lpfc_sli_turn_on_ring(phba, i);
666 /* Free any lpfc_dmabuf's waiting for mbox cmd cmpls */
667 while (!list_empty(&phba->freebufList)) {
668 struct lpfc_dmabuf *mp;
671 list_remove_head((&phba->freebufList),
676 lpfc_mbuf_free(phba, mp->virt,
683 } while (process_next);
688 lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
689 struct lpfc_iocbq *saveq)
697 irsp = &(saveq->iocb);
698 if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX)
699 || (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX)) {
704 (WORD5 *) & (saveq->iocb.un.
706 Rctl = w5p->hcsw.Rctl;
707 Type = w5p->hcsw.Type;
709 /* Firmware Workaround */
710 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
711 (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX)) {
714 w5p->hcsw.Rctl = Rctl;
715 w5p->hcsw.Type = Type;
718 /* unSolicited Responses */
719 if (pring->prt[0].profile) {
720 (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring, saveq);
723 /* We must search, based on rctl / type
724 for the right routine */
725 for (i = 0; i < pring->num_mask;
727 if ((pring->prt[i].rctl ==
731 (pring->prt[i].lpfc_sli_rcv_unsol_event)
732 (phba, pring, saveq);
739 /* Unexpected Rctl / Type received */
740 /* Ring <ringno> handler: unexpected
741 Rctl <Rctl> Type <Type> received */
742 lpfc_printf_log(phba,
745 "%d:0313 Ring %d handler: unexpected Rctl x%x "
746 "Type x%x received \n",
755 static struct lpfc_iocbq *
756 lpfc_sli_txcmpl_ring_search_slow(struct lpfc_sli_ring * pring,
757 struct lpfc_iocbq * prspiocb)
761 struct lpfc_iocbq *cmd_iocb;
762 struct lpfc_iocbq *iocb, *next_iocb;
765 irsp = &prspiocb->iocb;
766 iotag = irsp->ulpIoTag;
769 /* Search through txcmpl from the begining */
770 list_for_each_entry_safe(iocb, next_iocb, &(pring->txcmplq), list) {
772 if (iotag == icmd->ulpIoTag) {
775 list_del(&iocb->list);
776 pring->txcmplq_cnt--;
784 static struct lpfc_iocbq *
785 lpfc_sli_txcmpl_ring_iotag_lookup(struct lpfc_hba * phba,
786 struct lpfc_sli_ring * pring,
787 struct lpfc_iocbq * prspiocb)
790 struct lpfc_iocbq *cmd_iocb = NULL;
793 if (unlikely(pring->fast_lookup == NULL))
796 /* Use fast lookup based on iotag for completion */
797 irsp = &prspiocb->iocb;
798 iotag = irsp->ulpIoTag;
799 if (iotag < pring->fast_iotag) {
800 cmd_iocb = *(pring->fast_lookup + iotag);
801 *(pring->fast_lookup + iotag) = NULL;
803 list_del(&cmd_iocb->list);
804 pring->txcmplq_cnt--;
808 * This is clearly an error. A ring that uses iotags
809 * should never have a interrupt for a completion that
810 * is not on the ring. Return NULL and log a error.
812 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
813 "%d:0327 Rsp ring %d error - command "
814 "completion for iotag x%x not found\n",
815 phba->brd_no, pring->ringno, iotag);
821 * Rsp ring <ringno> get: iotag <iotag> greater then
822 * configured max <fast_iotag> wd0 <irsp>. This is an
823 * error. Just return NULL.
825 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
826 "%d:0317 Rsp ring %d get: iotag x%x greater then "
827 "configured max x%x wd0 x%x\n",
828 phba->brd_no, pring->ringno, iotag, pring->fast_iotag,
829 *(((uint32_t *) irsp) + 7));
834 lpfc_sli_process_sol_iocb(struct lpfc_hba * phba, struct lpfc_sli_ring * pring,
835 struct lpfc_iocbq *saveq)
837 struct lpfc_iocbq * cmdiocbp;
841 /* Based on the iotag field, get the cmd IOCB from the txcmplq */
842 spin_lock_irqsave(phba->host->host_lock, iflag);
843 cmdiocbp = lpfc_sli_txcmpl_ring_search_slow(pring, saveq);
845 if (cmdiocbp->iocb_cmpl) {
847 * Post all ELS completions to the worker thread.
848 * All other are passed to the completion callback.
850 if (pring->ringno == LPFC_ELS_RING) {
851 spin_unlock_irqrestore(phba->host->host_lock,
853 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
854 spin_lock_irqsave(phba->host->host_lock, iflag);
857 if (cmdiocbp->iocb_flag & LPFC_IO_POLL)
860 spin_unlock_irqrestore(phba->host->host_lock,
862 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
863 spin_lock_irqsave(phba->host->host_lock, iflag);
866 list_add_tail(&cmdiocbp->list, &phba->lpfc_iocb_list);
870 * Unknown initiating command based on the response iotag.
871 * This could be the case on the ELS ring because of
874 if (pring->ringno != LPFC_ELS_RING) {
876 * Ring <ringno> handler: unexpected completion IoTag
879 lpfc_printf_log(phba,
882 "%d:0322 Ring %d handler: unexpected "
883 "completion IoTag x%x Data: x%x x%x x%x x%x\n",
886 saveq->iocb.ulpIoTag,
887 saveq->iocb.ulpStatus,
888 saveq->iocb.un.ulpWord[4],
889 saveq->iocb.ulpCommand,
890 saveq->iocb.ulpContext);
893 spin_unlock_irqrestore(phba->host->host_lock, iflag);
898 * This routine presumes LPFC_FCP_RING handling and doesn't bother
899 * to check it explicitly.
902 lpfc_sli_handle_fast_ring_event(struct lpfc_hba * phba,
903 struct lpfc_sli_ring * pring, uint32_t mask)
905 struct lpfc_pgp *pgp = &phba->slim2p->mbx.us.s2.port[pring->ringno];
907 IOCB_t *entry = NULL;
908 struct lpfc_iocbq *cmdiocbq = NULL;
909 struct lpfc_iocbq rspiocbq;
911 uint32_t portRspPut, portRspMax;
915 uint32_t rsp_cmpl = 0;
916 void __iomem *to_slim;
918 spin_lock_irqsave(phba->host->host_lock, iflag);
919 pring->stats.iocb_event++;
922 * The next available response entry should never exceed the maximum
923 * entries. If it does, treat it as an adapter hardware error.
925 portRspMax = pring->numRiocb;
926 portRspPut = le32_to_cpu(pgp->rspPutInx);
927 if (unlikely(portRspPut >= portRspMax)) {
929 * Ring <ringno> handler: portRspPut <portRspPut> is bigger then
930 * rsp ring <portRspMax>
932 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
933 "%d:0312 Ring %d handler: portRspPut %d "
934 "is bigger then rsp ring %d\n",
935 phba->brd_no, pring->ringno, portRspPut,
938 phba->hba_state = LPFC_HBA_ERROR;
940 /* All error attention handlers are posted to worker thread */
941 phba->work_ha |= HA_ERATT;
942 phba->work_hs = HS_FFER3;
944 wake_up(phba->work_wait);
946 spin_unlock_irqrestore(phba->host->host_lock, iflag);
951 while (pring->rspidx != portRspPut) {
953 * Fetch an entry off the ring and copy it into a local data
954 * structure. The copy involves a byte-swap since the
955 * network byte order and pci byte orders are different.
957 entry = (IOCB_t *) IOCB_ENTRY(pring->rspringaddr, pring->rspidx);
958 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
959 (uint32_t *) &rspiocbq.iocb,
961 irsp = &rspiocbq.iocb;
963 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
964 pring->stats.iocb_rsp++;
967 if (unlikely(irsp->ulpStatus)) {
968 /* Rsp ring <ringno> error: IOCB */
969 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
970 "%d:0326 Rsp Ring %d error: IOCB Data: "
971 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
972 phba->brd_no, pring->ringno,
973 irsp->un.ulpWord[0], irsp->un.ulpWord[1],
974 irsp->un.ulpWord[2], irsp->un.ulpWord[3],
975 irsp->un.ulpWord[4], irsp->un.ulpWord[5],
976 *(((uint32_t *) irsp) + 6),
977 *(((uint32_t *) irsp) + 7));
981 case LPFC_ABORT_IOCB:
984 * Idle exchange closed via ABTS from port. No iocb
985 * resources need to be recovered.
987 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
988 printk(KERN_INFO "%s: IOCB cmd 0x%x processed. "
989 "Skipping completion\n", __FUNCTION__,
994 cmdiocbq = lpfc_sli_txcmpl_ring_iotag_lookup(phba,
997 if ((cmdiocbq) && (cmdiocbq->iocb_cmpl)) {
998 spin_unlock_irqrestore(
999 phba->host->host_lock, iflag);
1000 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
1002 spin_lock_irqsave(phba->host->host_lock,
1007 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
1008 char adaptermsg[LPFC_MAX_ADPTMSG];
1009 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
1010 memcpy(&adaptermsg[0], (uint8_t *) irsp,
1012 dev_warn(&((phba->pcidev)->dev), "lpfc%d: %s",
1013 phba->brd_no, adaptermsg);
1015 /* Unknown IOCB command */
1016 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
1017 "%d:0321 Unknown IOCB command "
1018 "Data: x%x, x%x x%x x%x x%x\n",
1019 phba->brd_no, type, irsp->ulpCommand,
1020 irsp->ulpStatus, irsp->ulpIoTag,
1027 * The response IOCB has been processed. Update the ring
1028 * pointer in SLIM. If the port response put pointer has not
1029 * been updated, sync the pgp->rspPutInx and fetch the new port
1030 * response put pointer.
1032 if (++pring->rspidx >= portRspMax)
1035 to_slim = phba->MBslimaddr +
1036 (SLIMOFF + (pring->ringno * 2) + 1) * 4;
1037 writeb(pring->rspidx, to_slim);
1039 if (pring->rspidx == portRspPut)
1040 portRspPut = le32_to_cpu(pgp->rspPutInx);
1043 if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
1044 pring->stats.iocb_rsp_full++;
1045 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
1046 writel(status, phba->CAregaddr);
1047 readl(phba->CAregaddr);
1049 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
1050 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
1051 pring->stats.iocb_cmd_empty++;
1053 /* Force update of the local copy of cmdGetInx */
1054 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
1055 lpfc_sli_resume_iocb(phba, pring);
1057 if ((pring->lpfc_sli_cmd_available))
1058 (pring->lpfc_sli_cmd_available) (phba, pring);
1062 spin_unlock_irqrestore(phba->host->host_lock, iflag);
1068 lpfc_sli_handle_slow_ring_event(struct lpfc_hba * phba,
1069 struct lpfc_sli_ring * pring, uint32_t mask)
1072 IOCB_t *irsp = NULL;
1073 struct lpfc_iocbq *rspiocbp = NULL;
1074 struct lpfc_iocbq *next_iocb;
1075 struct lpfc_iocbq *cmdiocbp;
1076 struct lpfc_iocbq *saveq;
1077 struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
1078 struct lpfc_pgp *pgp = &phba->slim2p->mbx.us.s2.port[pring->ringno];
1079 uint8_t iocb_cmd_type;
1080 lpfc_iocb_type type;
1081 uint32_t status, free_saveq;
1082 uint32_t portRspPut, portRspMax;
1084 unsigned long iflag;
1085 void __iomem *to_slim;
1087 spin_lock_irqsave(phba->host->host_lock, iflag);
1088 pring->stats.iocb_event++;
1091 * The next available response entry should never exceed the maximum
1092 * entries. If it does, treat it as an adapter hardware error.
1094 portRspMax = pring->numRiocb;
1095 portRspPut = le32_to_cpu(pgp->rspPutInx);
1096 if (portRspPut >= portRspMax) {
1098 * Ring <ringno> handler: portRspPut <portRspPut> is bigger then
1099 * rsp ring <portRspMax>
1101 lpfc_printf_log(phba,
1104 "%d:0312 Ring %d handler: portRspPut %d "
1105 "is bigger then rsp ring %d\n",
1107 pring->ringno, portRspPut, portRspMax);
1109 phba->hba_state = LPFC_HBA_ERROR;
1110 spin_unlock_irqrestore(phba->host->host_lock, iflag);
1112 phba->work_hs = HS_FFER3;
1113 lpfc_handle_eratt(phba);
1119 lpfc_iocb_list = &phba->lpfc_iocb_list;
1120 while (pring->rspidx != portRspPut) {
1122 * Build a completion list and call the appropriate handler.
1123 * The process is to get the next available response iocb, get
1124 * a free iocb from the list, copy the response data into the
1125 * free iocb, insert to the continuation list, and update the
1126 * next response index to slim. This process makes response
1127 * iocb's in the ring available to DMA as fast as possible but
1128 * pays a penalty for a copy operation. Since the iocb is
1129 * only 32 bytes, this penalty is considered small relative to
1130 * the PCI reads for register values and a slim write. When
1131 * the ulpLe field is set, the entire Command has been
1134 entry = IOCB_ENTRY(pring->rspringaddr, pring->rspidx);
1135 list_remove_head(lpfc_iocb_list, rspiocbp, struct lpfc_iocbq,
1137 if (rspiocbp == NULL) {
1138 printk(KERN_ERR "%s: out of buffers! Failing "
1139 "completion.\n", __FUNCTION__);
1143 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb, sizeof (IOCB_t));
1144 irsp = &rspiocbp->iocb;
1146 if (++pring->rspidx >= portRspMax)
1149 to_slim = phba->MBslimaddr + (SLIMOFF + (pring->ringno * 2)
1151 writeb(pring->rspidx, to_slim);
1153 if (list_empty(&(pring->iocb_continueq))) {
1154 list_add(&rspiocbp->list, &(pring->iocb_continueq));
1156 list_add_tail(&rspiocbp->list,
1157 &(pring->iocb_continueq));
1160 pring->iocb_continueq_cnt++;
1163 * By default, the driver expects to free all resources
1164 * associated with this iocb completion.
1167 saveq = list_get_first(&pring->iocb_continueq,
1168 struct lpfc_iocbq, list);
1169 irsp = &(saveq->iocb);
1170 list_del_init(&pring->iocb_continueq);
1171 pring->iocb_continueq_cnt = 0;
1173 pring->stats.iocb_rsp++;
1175 if (irsp->ulpStatus) {
1176 /* Rsp ring <ringno> error: IOCB */
1177 lpfc_printf_log(phba,
1180 "%d:0328 Rsp Ring %d error: IOCB Data: "
1181 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
1184 irsp->un.ulpWord[0],
1185 irsp->un.ulpWord[1],
1186 irsp->un.ulpWord[2],
1187 irsp->un.ulpWord[3],
1188 irsp->un.ulpWord[4],
1189 irsp->un.ulpWord[5],
1190 *(((uint32_t *) irsp) + 6),
1191 *(((uint32_t *) irsp) + 7));
1195 * Fetch the IOCB command type and call the correct
1196 * completion routine. Solicited and Unsolicited
1197 * IOCBs on the ELS ring get freed back to the
1198 * lpfc_iocb_list by the discovery kernel thread.
1200 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
1201 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
1202 if (type == LPFC_SOL_IOCB) {
1203 spin_unlock_irqrestore(phba->host->host_lock,
1205 rc = lpfc_sli_process_sol_iocb(phba, pring,
1207 spin_lock_irqsave(phba->host->host_lock, iflag);
1208 } else if (type == LPFC_UNSOL_IOCB) {
1209 spin_unlock_irqrestore(phba->host->host_lock,
1211 rc = lpfc_sli_process_unsol_iocb(phba, pring,
1213 spin_lock_irqsave(phba->host->host_lock, iflag);
1214 } else if (type == LPFC_ABORT_IOCB) {
1215 if ((irsp->ulpCommand != CMD_XRI_ABORTED_CX) &&
1217 lpfc_sli_txcmpl_ring_search_slow(pring,
1219 /* Call the specified completion
1221 if (cmdiocbp->iocb_cmpl) {
1222 spin_unlock_irqrestore(
1223 phba->host->host_lock,
1225 (cmdiocbp->iocb_cmpl) (phba,
1228 phba->host->host_lock,
1231 list_add_tail(&cmdiocbp->list,
1235 } else if (type == LPFC_UNKNOWN_IOCB) {
1236 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
1238 char adaptermsg[LPFC_MAX_ADPTMSG];
1240 memset(adaptermsg, 0,
1242 memcpy(&adaptermsg[0], (uint8_t *) irsp,
1244 dev_warn(&((phba->pcidev)->dev),
1246 phba->brd_no, adaptermsg);
1248 /* Unknown IOCB command */
1249 lpfc_printf_log(phba,
1252 "%d:0321 Unknown IOCB command "
1253 "Data: x%x x%x x%x x%x\n",
1263 if (!list_empty(&saveq->list)) {
1264 list_for_each_entry_safe(rspiocbp,
1268 list_add_tail(&rspiocbp->list,
1273 list_add_tail(&saveq->list, lpfc_iocb_list);
1278 * If the port response put pointer has not been updated, sync
1279 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
1280 * response put pointer.
1282 if (pring->rspidx == portRspPut) {
1283 portRspPut = le32_to_cpu(pgp->rspPutInx);
1285 } /* while (pring->rspidx != portRspPut) */
1287 if ((rspiocbp != 0) && (mask & HA_R0RE_REQ)) {
1288 /* At least one response entry has been freed */
1289 pring->stats.iocb_rsp_full++;
1290 /* SET RxRE_RSP in Chip Att register */
1291 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
1292 writel(status, phba->CAregaddr);
1293 readl(phba->CAregaddr); /* flush */
1295 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
1296 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
1297 pring->stats.iocb_cmd_empty++;
1299 /* Force update of the local copy of cmdGetInx */
1300 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
1301 lpfc_sli_resume_iocb(phba, pring);
1303 if ((pring->lpfc_sli_cmd_available))
1304 (pring->lpfc_sli_cmd_available) (phba, pring);
1308 spin_unlock_irqrestore(phba->host->host_lock, iflag);
1313 lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1315 struct lpfc_iocbq *iocb, *next_iocb;
1316 IOCB_t *icmd = NULL, *cmd = NULL;
1322 /* Error everything on txq and txcmplq
1325 spin_lock_irq(phba->host->host_lock);
1326 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
1327 list_del_init(&iocb->list);
1328 if (iocb->iocb_cmpl) {
1330 icmd->ulpStatus = IOSTAT_LOCAL_REJECT;
1331 icmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
1332 spin_unlock_irq(phba->host->host_lock);
1333 (iocb->iocb_cmpl) (phba, iocb, iocb);
1334 spin_lock_irq(phba->host->host_lock);
1336 list_add_tail(&iocb->list, &phba->lpfc_iocb_list);
1340 INIT_LIST_HEAD(&(pring->txq));
1342 /* Next issue ABTS for everything on the txcmplq */
1343 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
1347 * Imediate abort of IOCB, clear fast_lookup entry,
1348 * if any, deque and call compl
1350 iotag = cmd->ulpIoTag;
1351 if (iotag && pring->fast_lookup &&
1352 (iotag < pring->fast_iotag))
1353 pring->fast_lookup[iotag] = NULL;
1355 list_del_init(&iocb->list);
1356 pring->txcmplq_cnt--;
1358 if (iocb->iocb_cmpl) {
1359 cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
1360 cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
1361 spin_unlock_irq(phba->host->host_lock);
1362 (iocb->iocb_cmpl) (phba, iocb, iocb);
1363 spin_lock_irq(phba->host->host_lock);
1365 list_add_tail(&iocb->list, &phba->lpfc_iocb_list);
1369 INIT_LIST_HEAD(&pring->txcmplq);
1370 pring->txcmplq_cnt = 0;
1371 spin_unlock_irq(phba->host->host_lock);
1376 /******************************************************************************
1377 * lpfc_sli_send_reset
1379 * Note: After returning from this function, the HBA cannot be accessed for
1380 * 1 ms. Since we do not wish to delay in interrupt context, it is the
1381 * responsibility of the caller to perform the mdelay(1) and flush via readl().
1382 ******************************************************************************/
1384 lpfc_sli_send_reset(struct lpfc_hba * phba, uint16_t skip_post)
1387 volatile uint32_t word0;
1388 void __iomem *to_slim;
1389 unsigned long flags = 0;
1391 spin_lock_irqsave(phba->host->host_lock, flags);
1393 /* A board reset must use REAL SLIM. */
1394 phba->sli.sli_flag &= ~LPFC_SLI2_ACTIVE;
1397 swpmb = (MAILBOX_t *) & word0;
1398 swpmb->mbxCommand = MBX_RESTART;
1401 to_slim = phba->MBslimaddr;
1402 writel(*(uint32_t *) swpmb, to_slim);
1403 readl(to_slim); /* flush */
1405 /* Only skip post after fc_ffinit is completed */
1407 word0 = 1; /* This is really setting up word1 */
1409 word0 = 0; /* This is really setting up word1 */
1411 to_slim = phba->MBslimaddr + sizeof (uint32_t);
1412 writel(*(uint32_t *) swpmb, to_slim);
1413 readl(to_slim); /* flush */
1415 /* Turn off parity checking and serr during the physical reset */
1416 pci_read_config_word(phba->pcidev, PCI_COMMAND, &phba->pci_cfg_value);
1417 pci_write_config_word(phba->pcidev, PCI_COMMAND,
1418 (phba->pci_cfg_value &
1419 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
1421 writel(HC_INITFF, phba->HCregaddr);
1423 phba->hba_state = LPFC_INIT_START;
1424 spin_unlock_irqrestore(phba->host->host_lock, flags);
1430 lpfc_sli_brdreset(struct lpfc_hba * phba, uint16_t skip_post)
1432 struct lpfc_sli_ring *pring;
1434 struct lpfc_dmabuf *mp, *next_mp;
1435 unsigned long flags = 0;
1437 lpfc_sli_send_reset(phba, skip_post);
1440 spin_lock_irqsave(phba->host->host_lock, flags);
1441 /* Risk the write on flush case ie no delay after the readl */
1442 readl(phba->HCregaddr); /* flush */
1443 /* Now toggle INITFF bit set by lpfc_sli_send_reset */
1444 writel(0, phba->HCregaddr);
1445 readl(phba->HCregaddr); /* flush */
1447 /* Restore PCI cmd register */
1448 pci_write_config_word(phba->pcidev, PCI_COMMAND, phba->pci_cfg_value);
1450 /* perform board reset */
1451 phba->fc_eventTag = 0;
1453 phba->fc_prevDID = Mask_DID;
1456 lpfc_printf_log(phba,
1459 "%d:0325 Reset HBA Data: x%x x%x x%x\n",
1465 /* Initialize relevant SLI info */
1466 for (i = 0; i < phba->sli.num_rings; i++) {
1467 pring = &phba->sli.ring[i];
1470 pring->next_cmdidx = 0;
1471 pring->local_getidx = 0;
1473 pring->missbufcnt = 0;
1475 spin_unlock_irqrestore(phba->host->host_lock, flags);
1483 spin_lock_irqsave(phba->host->host_lock, flags);
1484 /* Cleanup preposted buffers on the ELS ring */
1485 pring = &phba->sli.ring[LPFC_ELS_RING];
1486 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
1487 list_del(&mp->list);
1488 pring->postbufq_cnt--;
1489 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1492 spin_unlock_irqrestore(phba->host->host_lock, flags);
1494 for (i = 0; i < phba->sli.num_rings; i++)
1495 lpfc_sli_abort_iocb_ring(phba, &phba->sli.ring[i]);
1501 lpfc_sli_chipset_init(struct lpfc_hba *phba)
1503 uint32_t status, i = 0;
1505 /* Read the HBA Host Status Register */
1506 status = readl(phba->HSregaddr);
1508 /* Check status register to see what current state is */
1510 while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
1512 /* Check every 100ms for 5 retries, then every 500ms for 5, then
1513 * every 2.5 sec for 5, then reset board and every 2.5 sec for
1517 /* Adapter failed to init, timeout, status reg
1519 lpfc_printf_log(phba,
1522 "%d:0436 Adapter failed to init, "
1523 "timeout, status reg x%x\n",
1526 phba->hba_state = LPFC_HBA_ERROR;
1530 /* Check to see if any errors occurred during init */
1531 if (status & HS_FFERM) {
1532 /* ERROR: During chipset initialization */
1533 /* Adapter failed to init, chipset, status reg
1535 lpfc_printf_log(phba,
1538 "%d:0437 Adapter failed to init, "
1539 "chipset, status reg x%x\n",
1542 phba->hba_state = LPFC_HBA_ERROR;
1548 } else if (i <= 10) {
1555 lpfc_sli_brdreset(phba, 0);
1557 /* Read the HBA Host Status Register */
1558 status = readl(phba->HSregaddr);
1561 /* Check to see if any errors occurred during init */
1562 if (status & HS_FFERM) {
1563 /* ERROR: During chipset initialization */
1564 /* Adapter failed to init, chipset, status reg <status> */
1565 lpfc_printf_log(phba,
1568 "%d:0438 Adapter failed to init, chipset, "
1572 phba->hba_state = LPFC_HBA_ERROR;
1576 /* Clear all interrupt enable conditions */
1577 writel(0, phba->HCregaddr);
1578 readl(phba->HCregaddr); /* flush */
1580 /* setup host attn register */
1581 writel(0xffffffff, phba->HAregaddr);
1582 readl(phba->HAregaddr); /* flush */
1587 lpfc_sli_hba_setup(struct lpfc_hba * phba)
1590 uint32_t resetcount = 0, rc = 0, done = 0;
1592 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1594 phba->hba_state = LPFC_HBA_ERROR;
1598 while (resetcount < 2 && !done) {
1599 phba->hba_state = 0;
1600 lpfc_sli_brdreset(phba, 0);
1602 rc = lpfc_sli_chipset_init(phba);
1608 /* Call pre CONFIG_PORT mailbox command initialization. A value of 0
1609 * means the call was successful. Any other nonzero value is a failure,
1610 * but if ERESTART is returned, the driver may reset the HBA and try
1613 rc = lpfc_config_port_prep(phba);
1614 if (rc == -ERESTART) {
1615 phba->hba_state = 0;
1621 phba->hba_state = LPFC_INIT_MBX_CMDS;
1622 lpfc_config_port(phba, pmb);
1623 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
1624 if (rc == MBX_SUCCESS)
1627 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1628 "%d:0442 Adapter failed to init, mbxCmd x%x "
1629 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
1630 phba->brd_no, pmb->mb.mbxCommand,
1631 pmb->mb.mbxStatus, 0);
1632 phba->sli.sli_flag &= ~LPFC_SLI2_ACTIVE;
1636 goto lpfc_sli_hba_setup_error;
1638 rc = lpfc_sli_ring_map(phba, pmb);
1641 goto lpfc_sli_hba_setup_error;
1643 phba->sli.sli_flag |= LPFC_PROCESS_LA;
1645 rc = lpfc_config_port_post(phba);
1647 goto lpfc_sli_hba_setup_error;
1649 goto lpfc_sli_hba_setup_exit;
1650 lpfc_sli_hba_setup_error:
1651 phba->hba_state = LPFC_HBA_ERROR;
1652 lpfc_sli_hba_setup_exit:
1653 mempool_free(pmb, phba->mbox_mem_pool);
1658 lpfc_mbox_abort(struct lpfc_hba * phba)
1660 LPFC_MBOXQ_t *pmbox;
1663 if (phba->sli.mbox_active) {
1664 del_timer_sync(&phba->sli.mbox_tmo);
1665 phba->work_hba_events &= ~WORKER_MBOX_TMO;
1666 pmbox = phba->sli.mbox_active;
1668 phba->sli.mbox_active = NULL;
1669 if (pmbox->mbox_cmpl) {
1670 mb->mbxStatus = MBX_NOT_FINISHED;
1671 (pmbox->mbox_cmpl) (phba, pmbox);
1673 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
1676 /* Abort all the non active mailbox commands. */
1677 spin_lock_irq(phba->host->host_lock);
1678 pmbox = lpfc_mbox_get(phba);
1681 if (pmbox->mbox_cmpl) {
1682 mb->mbxStatus = MBX_NOT_FINISHED;
1683 spin_unlock_irq(phba->host->host_lock);
1684 (pmbox->mbox_cmpl) (phba, pmbox);
1685 spin_lock_irq(phba->host->host_lock);
1687 pmbox = lpfc_mbox_get(phba);
1689 spin_unlock_irq(phba->host->host_lock);
1693 /*! lpfc_mbox_timeout
1697 * \param hba Pointer to per struct lpfc_hba structure
1698 * \param l1 Pointer to the driver's mailbox queue.
1704 * This routine handles mailbox timeout events at timer interrupt context.
1707 lpfc_mbox_timeout(unsigned long ptr)
1709 struct lpfc_hba *phba;
1710 unsigned long iflag;
1712 phba = (struct lpfc_hba *)ptr;
1713 spin_lock_irqsave(phba->host->host_lock, iflag);
1714 if (!(phba->work_hba_events & WORKER_MBOX_TMO)) {
1715 phba->work_hba_events |= WORKER_MBOX_TMO;
1716 if (phba->work_wait)
1717 wake_up(phba->work_wait);
1719 spin_unlock_irqrestore(phba->host->host_lock, iflag);
1723 lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
1725 LPFC_MBOXQ_t *pmbox;
1728 spin_lock_irq(phba->host->host_lock);
1729 if (!(phba->work_hba_events & WORKER_MBOX_TMO)) {
1730 spin_unlock_irq(phba->host->host_lock);
1734 phba->work_hba_events &= ~WORKER_MBOX_TMO;
1736 pmbox = phba->sli.mbox_active;
1739 /* Mbox cmd <mbxCommand> timeout */
1740 lpfc_printf_log(phba,
1743 "%d:0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
1748 phba->sli.mbox_active);
1750 phba->sli.mbox_active = NULL;
1751 if (pmbox->mbox_cmpl) {
1752 mb->mbxStatus = MBX_NOT_FINISHED;
1753 spin_unlock_irq(phba->host->host_lock);
1754 (pmbox->mbox_cmpl) (phba, pmbox);
1755 spin_lock_irq(phba->host->host_lock);
1757 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
1759 spin_unlock_irq(phba->host->host_lock);
1760 lpfc_mbox_abort(phba);
1765 lpfc_sli_issue_mbox(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmbox, uint32_t flag)
1768 struct lpfc_sli *psli;
1769 uint32_t status, evtctr;
1772 unsigned long drvr_flag = 0;
1773 volatile uint32_t word0, ldata;
1774 void __iomem *to_slim;
1778 spin_lock_irqsave(phba->host->host_lock, drvr_flag);
1782 status = MBX_SUCCESS;
1784 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
1785 /* Polling for a mbox command when another one is already active
1786 * is not allowed in SLI. Also, the driver must have established
1787 * SLI2 mode to queue and process multiple mbox commands.
1790 if (flag & MBX_POLL) {
1791 spin_unlock_irqrestore(phba->host->host_lock,
1794 /* Mbox command <mbxCommand> cannot issue */
1795 LOG_MBOX_CANNOT_ISSUE_DATA( phba, mb, psli, flag)
1796 return (MBX_NOT_FINISHED);
1799 if (!(psli->sli_flag & LPFC_SLI2_ACTIVE)) {
1800 spin_unlock_irqrestore(phba->host->host_lock,
1802 /* Mbox command <mbxCommand> cannot issue */
1803 LOG_MBOX_CANNOT_ISSUE_DATA( phba, mb, psli, flag)
1804 return (MBX_NOT_FINISHED);
1807 /* Handle STOP IOCB processing flag. This is only meaningful
1808 * if we are not polling for mbox completion.
1810 if (flag & MBX_STOP_IOCB) {
1811 flag &= ~MBX_STOP_IOCB;
1812 /* Now flag each ring */
1813 for (i = 0; i < psli->num_rings; i++) {
1814 /* If the ring is active, flag it */
1815 if (psli->ring[i].cmdringaddr) {
1816 psli->ring[i].flag |=
1822 /* Another mailbox command is still being processed, queue this
1823 * command to be processed later.
1825 lpfc_mbox_put(phba, pmbox);
1827 /* Mbox cmd issue - BUSY */
1828 lpfc_printf_log(phba,
1831 "%d:0308 Mbox cmd issue - BUSY Data: x%x x%x x%x x%x\n",
1838 psli->slistat.mbox_busy++;
1839 spin_unlock_irqrestore(phba->host->host_lock,
1845 /* Handle STOP IOCB processing flag. This is only meaningful
1846 * if we are not polling for mbox completion.
1848 if (flag & MBX_STOP_IOCB) {
1849 flag &= ~MBX_STOP_IOCB;
1850 if (flag == MBX_NOWAIT) {
1851 /* Now flag each ring */
1852 for (i = 0; i < psli->num_rings; i++) {
1853 /* If the ring is active, flag it */
1854 if (psli->ring[i].cmdringaddr) {
1855 psli->ring[i].flag |=
1862 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
1864 /* If we are not polling, we MUST be in SLI2 mode */
1865 if (flag != MBX_POLL) {
1866 if (!(psli->sli_flag & LPFC_SLI2_ACTIVE)) {
1867 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
1868 spin_unlock_irqrestore(phba->host->host_lock,
1870 /* Mbox command <mbxCommand> cannot issue */
1871 LOG_MBOX_CANNOT_ISSUE_DATA( phba, mb, psli, flag);
1872 return (MBX_NOT_FINISHED);
1874 /* timeout active mbox command */
1875 mod_timer(&psli->mbox_tmo, jiffies + HZ * LPFC_MBOX_TMO);
1878 /* Mailbox cmd <cmd> issue */
1879 lpfc_printf_log(phba,
1882 "%d:0309 Mailbox cmd x%x issue Data: x%x x%x x%x\n",
1889 psli->slistat.mbox_cmd++;
1890 evtctr = psli->slistat.mbox_event;
1892 /* next set own bit for the adapter and copy over command word */
1893 mb->mbxOwner = OWN_CHIP;
1895 if (psli->sli_flag & LPFC_SLI2_ACTIVE) {
1896 /* First copy command data to host SLIM area */
1897 lpfc_sli_pcimem_bcopy(mb, &phba->slim2p->mbx, MAILBOX_CMD_SIZE);
1899 if (mb->mbxCommand == MBX_CONFIG_PORT) {
1900 /* copy command data into host mbox for cmpl */
1901 lpfc_sli_pcimem_bcopy(mb, &phba->slim2p->mbx,
1905 /* First copy mbox command data to HBA SLIM, skip past first
1907 to_slim = phba->MBslimaddr + sizeof (uint32_t);
1908 lpfc_memcpy_to_slim(to_slim, &mb->un.varWords[0],
1909 MAILBOX_CMD_SIZE - sizeof (uint32_t));
1911 /* Next copy over first word, with mbxOwner set */
1912 ldata = *((volatile uint32_t *)mb);
1913 to_slim = phba->MBslimaddr;
1914 writel(ldata, to_slim);
1915 readl(to_slim); /* flush */
1917 if (mb->mbxCommand == MBX_CONFIG_PORT) {
1918 /* switch over to host mailbox */
1919 psli->sli_flag |= LPFC_SLI2_ACTIVE;
1924 /* interrupt board to doit right away */
1925 writel(CA_MBATT, phba->CAregaddr);
1926 readl(phba->CAregaddr); /* flush */
1930 /* Don't wait for it to finish, just return */
1931 psli->mbox_active = pmbox;
1936 psli->mbox_active = NULL;
1937 if (psli->sli_flag & LPFC_SLI2_ACTIVE) {
1938 /* First read mbox status word */
1939 word0 = *((volatile uint32_t *)&phba->slim2p->mbx);
1940 word0 = le32_to_cpu(word0);
1942 /* First read mbox status word */
1943 word0 = readl(phba->MBslimaddr);
1946 /* Read the HBA Host Attention Register */
1947 ha_copy = readl(phba->HAregaddr);
1949 /* Wait for command to complete */
1950 while (((word0 & OWN_CHIP) == OWN_CHIP)
1951 || !(ha_copy & HA_MBATT)) {
1953 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
1954 spin_unlock_irqrestore(phba->host->host_lock,
1956 return (MBX_NOT_FINISHED);
1959 /* Check if we took a mbox interrupt while we were
1961 if (((word0 & OWN_CHIP) != OWN_CHIP)
1962 && (evtctr != psli->slistat.mbox_event))
1965 spin_unlock_irqrestore(phba->host->host_lock,
1968 /* Can be in interrupt context, do not sleep */
1969 /* (or might be called with interrupts disabled) */
1972 spin_lock_irqsave(phba->host->host_lock, drvr_flag);
1974 if (psli->sli_flag & LPFC_SLI2_ACTIVE) {
1975 /* First copy command data */
1976 word0 = *((volatile uint32_t *)
1977 &phba->slim2p->mbx);
1978 word0 = le32_to_cpu(word0);
1979 if (mb->mbxCommand == MBX_CONFIG_PORT) {
1981 volatile uint32_t slimword0;
1982 /* Check real SLIM for any errors */
1983 slimword0 = readl(phba->MBslimaddr);
1984 slimmb = (MAILBOX_t *) & slimword0;
1985 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
1986 && slimmb->mbxStatus) {
1993 /* First copy command data */
1994 word0 = readl(phba->MBslimaddr);
1996 /* Read the HBA Host Attention Register */
1997 ha_copy = readl(phba->HAregaddr);
2000 if (psli->sli_flag & LPFC_SLI2_ACTIVE) {
2001 /* copy results back to user */
2002 lpfc_sli_pcimem_bcopy(&phba->slim2p->mbx, mb,
2005 /* First copy command data */
2006 lpfc_memcpy_from_slim(mb, phba->MBslimaddr,
2008 if ((mb->mbxCommand == MBX_DUMP_MEMORY) &&
2010 lpfc_memcpy_from_slim((void *)pmbox->context2,
2011 phba->MBslimaddr + DMP_RSP_OFFSET,
2012 mb->un.varDmp.word_cnt);
2016 writel(HA_MBATT, phba->HAregaddr);
2017 readl(phba->HAregaddr); /* flush */
2019 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
2020 status = mb->mbxStatus;
2023 spin_unlock_irqrestore(phba->host->host_lock, drvr_flag);
2028 lpfc_sli_ringtx_put(struct lpfc_hba * phba, struct lpfc_sli_ring * pring,
2029 struct lpfc_iocbq * piocb)
2031 /* Insert the caller's iocb in the txq tail for later processing. */
2032 list_add_tail(&piocb->list, &pring->txq);
2037 static struct lpfc_iocbq *
2038 lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2039 struct lpfc_iocbq ** piocb)
2041 struct lpfc_iocbq * nextiocb;
2043 nextiocb = lpfc_sli_ringtx_get(phba, pring);
2053 lpfc_sli_issue_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2054 struct lpfc_iocbq *piocb, uint32_t flag)
2056 struct lpfc_iocbq *nextiocb;
2060 * We should never get an IOCB if we are in a < LINK_DOWN state
2062 if (unlikely(phba->hba_state < LPFC_LINK_DOWN))
2066 * Check to see if we are blocking IOCB processing because of a
2067 * outstanding mbox command.
2069 if (unlikely(pring->flag & LPFC_STOP_IOCB_MBX))
2072 if (unlikely(phba->hba_state == LPFC_LINK_DOWN)) {
2074 * Only CREATE_XRI, CLOSE_XRI, ABORT_XRI, and QUE_RING_BUF
2075 * can be issued if the link is not up.
2077 switch (piocb->iocb.ulpCommand) {
2078 case CMD_QUE_RING_BUF_CN:
2079 case CMD_QUE_RING_BUF64_CN:
2080 case CMD_CLOSE_XRI_CN:
2081 case CMD_ABORT_XRI_CN:
2083 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
2084 * completion, iocb_cmpl MUST be 0.
2086 if (piocb->iocb_cmpl)
2087 piocb->iocb_cmpl = NULL;
2089 case CMD_CREATE_XRI_CR:
2096 * For FCP commands, we must be in a state where we can process link
2099 } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
2100 !(phba->sli.sli_flag & LPFC_PROCESS_LA)))
2104 * Check to see if this is a high priority command.
2105 * If so bypass tx queue processing.
2107 if (unlikely((flag & SLI_IOCB_HIGH_PRIORITY) &&
2108 (iocb = lpfc_sli_next_iocb_slot(phba, pring)))) {
2109 lpfc_sli_submit_iocb(phba, pring, iocb, piocb);
2113 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
2114 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
2115 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
2118 lpfc_sli_update_ring(phba, pring);
2120 lpfc_sli_update_full_ring(phba, pring);
2123 return IOCB_SUCCESS;
2128 pring->stats.iocb_cmd_delay++;
2132 if (!(flag & SLI_IOCB_RET_IOCB)) {
2133 lpfc_sli_ringtx_put(phba, pring, piocb);
2134 return IOCB_SUCCESS;
2141 lpfc_sli_setup(struct lpfc_hba *phba)
2144 struct lpfc_sli *psli = &phba->sli;
2145 struct lpfc_sli_ring *pring;
2147 psli->num_rings = MAX_CONFIGURED_RINGS;
2149 psli->fcp_ring = LPFC_FCP_RING;
2150 psli->next_ring = LPFC_FCP_NEXT_RING;
2151 psli->ip_ring = LPFC_IP_RING;
2153 for (i = 0; i < psli->num_rings; i++) {
2154 pring = &psli->ring[i];
2156 case LPFC_FCP_RING: /* ring 0 - FCP */
2157 /* numCiocb and numRiocb are used in config_port */
2158 pring->numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
2159 pring->numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
2160 pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
2161 pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
2162 pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
2163 pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
2164 pring->iotag_ctr = 0;
2166 (phba->cfg_hba_queue_depth * 2);
2167 pring->fast_iotag = pring->iotag_max;
2168 pring->num_mask = 0;
2170 case LPFC_IP_RING: /* ring 1 - IP */
2171 /* numCiocb and numRiocb are used in config_port */
2172 pring->numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
2173 pring->numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
2174 pring->num_mask = 0;
2176 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
2177 /* numCiocb and numRiocb are used in config_port */
2178 pring->numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
2179 pring->numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
2180 pring->fast_iotag = 0;
2181 pring->iotag_ctr = 0;
2182 pring->iotag_max = 4096;
2183 pring->num_mask = 4;
2184 pring->prt[0].profile = 0; /* Mask 0 */
2185 pring->prt[0].rctl = FC_ELS_REQ;
2186 pring->prt[0].type = FC_ELS_DATA;
2187 pring->prt[0].lpfc_sli_rcv_unsol_event =
2188 lpfc_els_unsol_event;
2189 pring->prt[1].profile = 0; /* Mask 1 */
2190 pring->prt[1].rctl = FC_ELS_RSP;
2191 pring->prt[1].type = FC_ELS_DATA;
2192 pring->prt[1].lpfc_sli_rcv_unsol_event =
2193 lpfc_els_unsol_event;
2194 pring->prt[2].profile = 0; /* Mask 2 */
2195 /* NameServer Inquiry */
2196 pring->prt[2].rctl = FC_UNSOL_CTL;
2198 pring->prt[2].type = FC_COMMON_TRANSPORT_ULP;
2199 pring->prt[2].lpfc_sli_rcv_unsol_event =
2200 lpfc_ct_unsol_event;
2201 pring->prt[3].profile = 0; /* Mask 3 */
2202 /* NameServer response */
2203 pring->prt[3].rctl = FC_SOL_CTL;
2205 pring->prt[3].type = FC_COMMON_TRANSPORT_ULP;
2206 pring->prt[3].lpfc_sli_rcv_unsol_event =
2207 lpfc_ct_unsol_event;
2210 totiocb += (pring->numCiocb + pring->numRiocb);
2212 if (totiocb > MAX_SLI2_IOCB) {
2213 /* Too many cmd / rsp ring entries in SLI2 SLIM */
2214 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2215 "%d:0462 Too many cmd / rsp ring entries in "
2216 "SLI2 SLIM Data: x%x x%x\n",
2217 phba->brd_no, totiocb, MAX_SLI2_IOCB);
2224 lpfc_sli_queue_setup(struct lpfc_hba * phba)
2226 struct lpfc_sli *psli;
2227 struct lpfc_sli_ring *pring;
2231 spin_lock_irq(phba->host->host_lock);
2232 INIT_LIST_HEAD(&psli->mboxq);
2233 /* Initialize list headers for txq and txcmplq as double linked lists */
2234 for (i = 0; i < psli->num_rings; i++) {
2235 pring = &psli->ring[i];
2237 pring->next_cmdidx = 0;
2238 pring->local_getidx = 0;
2240 INIT_LIST_HEAD(&pring->txq);
2241 INIT_LIST_HEAD(&pring->txcmplq);
2242 INIT_LIST_HEAD(&pring->iocb_continueq);
2243 INIT_LIST_HEAD(&pring->postbufq);
2244 cnt = pring->fast_iotag;
2245 spin_unlock_irq(phba->host->host_lock);
2247 pring->fast_lookup =
2248 kmalloc(cnt * sizeof (struct lpfc_iocbq *),
2250 if (pring->fast_lookup == 0) {
2253 memset((char *)pring->fast_lookup, 0,
2254 cnt * sizeof (struct lpfc_iocbq *));
2256 spin_lock_irq(phba->host->host_lock);
2258 spin_unlock_irq(phba->host->host_lock);
2263 lpfc_sli_hba_down(struct lpfc_hba * phba)
2265 struct lpfc_sli *psli;
2266 struct lpfc_sli_ring *pring;
2268 struct lpfc_iocbq *iocb, *next_iocb;
2269 IOCB_t *icmd = NULL;
2271 unsigned long flags = 0;
2274 lpfc_hba_down_prep(phba);
2276 spin_lock_irqsave(phba->host->host_lock, flags);
2278 for (i = 0; i < psli->num_rings; i++) {
2279 pring = &psli->ring[i];
2280 pring->flag |= LPFC_DEFERRED_RING_EVENT;
2283 * Error everything on the txq since these iocbs have not been
2284 * given to the FW yet.
2288 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
2289 list_del_init(&iocb->list);
2290 if (iocb->iocb_cmpl) {
2292 icmd->ulpStatus = IOSTAT_LOCAL_REJECT;
2293 icmd->un.ulpWord[4] = IOERR_SLI_DOWN;
2294 spin_unlock_irqrestore(phba->host->host_lock,
2296 (iocb->iocb_cmpl) (phba, iocb, iocb);
2297 spin_lock_irqsave(phba->host->host_lock, flags);
2299 list_add_tail(&iocb->list,
2300 &phba->lpfc_iocb_list);
2304 INIT_LIST_HEAD(&(pring->txq));
2306 if (pring->fast_lookup) {
2307 kfree(pring->fast_lookup);
2308 pring->fast_lookup = NULL;
2313 spin_unlock_irqrestore(phba->host->host_lock, flags);
2315 /* Return any active mbox cmds */
2316 del_timer_sync(&psli->mbox_tmo);
2317 spin_lock_irqsave(phba->host->host_lock, flags);
2318 phba->work_hba_events &= ~WORKER_MBOX_TMO;
2319 if (psli->mbox_active) {
2320 pmb = psli->mbox_active;
2321 pmb->mb.mbxStatus = MBX_NOT_FINISHED;
2322 if (pmb->mbox_cmpl) {
2323 spin_unlock_irqrestore(phba->host->host_lock, flags);
2324 pmb->mbox_cmpl(phba,pmb);
2325 spin_lock_irqsave(phba->host->host_lock, flags);
2328 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
2329 psli->mbox_active = NULL;
2331 /* Return any pending mbox cmds */
2332 while ((pmb = lpfc_mbox_get(phba)) != NULL) {
2333 pmb->mb.mbxStatus = MBX_NOT_FINISHED;
2334 if (pmb->mbox_cmpl) {
2335 spin_unlock_irqrestore(phba->host->host_lock, flags);
2336 pmb->mbox_cmpl(phba,pmb);
2337 spin_lock_irqsave(phba->host->host_lock, flags);
2341 INIT_LIST_HEAD(&psli->mboxq);
2343 spin_unlock_irqrestore(phba->host->host_lock, flags);
2346 * Provided the hba is not in an error state, reset it. It is not
2347 * capable of IO anymore.
2349 if (phba->hba_state != LPFC_HBA_ERROR) {
2350 phba->hba_state = LPFC_INIT_START;
2351 lpfc_sli_brdreset(phba, 1);
2358 lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
2360 uint32_t *src = srcp;
2361 uint32_t *dest = destp;
2365 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
2367 ldata = le32_to_cpu(ldata);
2375 lpfc_sli_ringpostbuf_put(struct lpfc_hba * phba, struct lpfc_sli_ring * pring,
2376 struct lpfc_dmabuf * mp)
2378 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
2380 list_add_tail(&mp->list, &pring->postbufq);
2382 pring->postbufq_cnt++;
2387 struct lpfc_dmabuf *
2388 lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2391 struct lpfc_dmabuf *mp, *next_mp;
2392 struct list_head *slp = &pring->postbufq;
2394 /* Search postbufq, from the begining, looking for a match on phys */
2395 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
2396 if (mp->phys == phys) {
2397 list_del_init(&mp->list);
2398 pring->postbufq_cnt--;
2403 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2404 "%d:0410 Cannot find virtual addr for mapped buf on "
2405 "ring %d Data x%llx x%p x%p x%x\n",
2406 phba->brd_no, pring->ringno, (unsigned long long)phys,
2407 slp->next, slp->prev, pring->postbufq_cnt);
2412 lpfc_sli_abort_elsreq_cmpl(struct lpfc_hba * phba, struct lpfc_iocbq * cmdiocb,
2413 struct lpfc_iocbq * rspiocb)
2415 struct lpfc_dmabuf *buf_ptr, *buf_ptr1;
2416 /* Free the resources associated with the ELS_REQUEST64 IOCB the driver
2418 * In this case, context2 = cmd, context2->next = rsp, context3 = bpl
2420 if (cmdiocb->context2) {
2421 buf_ptr1 = (struct lpfc_dmabuf *) cmdiocb->context2;
2423 /* Free the response IOCB before completing the abort
2426 list_remove_head((&buf_ptr1->list), buf_ptr,
2427 struct lpfc_dmabuf, list);
2429 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
2432 lpfc_mbuf_free(phba, buf_ptr1->virt, buf_ptr1->phys);
2436 if (cmdiocb->context3) {
2437 buf_ptr = (struct lpfc_dmabuf *) cmdiocb->context3;
2438 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
2442 list_add_tail(&cmdiocb->list, &phba->lpfc_iocb_list);
2447 lpfc_sli_issue_abort_iotag32(struct lpfc_hba * phba,
2448 struct lpfc_sli_ring * pring,
2449 struct lpfc_iocbq * cmdiocb)
2451 struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
2452 struct lpfc_iocbq *abtsiocbp = NULL;
2453 IOCB_t *icmd = NULL;
2454 IOCB_t *iabt = NULL;
2456 /* issue ABTS for this IOCB based on iotag */
2457 list_remove_head(lpfc_iocb_list, abtsiocbp, struct lpfc_iocbq, list);
2458 if (abtsiocbp == NULL)
2460 memset(abtsiocbp, 0, sizeof (struct lpfc_iocbq));
2462 iabt = &abtsiocbp->iocb;
2463 icmd = &cmdiocb->iocb;
2464 switch (icmd->ulpCommand) {
2465 case CMD_ELS_REQUEST64_CR:
2466 /* Even though we abort the ELS command, the firmware may access
2467 * the BPL or other resources before it processes our
2468 * ABORT_MXRI64. Thus we must delay reusing the cmdiocb
2469 * resources till the actual abort request completes.
2471 abtsiocbp->context1 = (void *)((unsigned long)icmd->ulpCommand);
2472 abtsiocbp->context2 = cmdiocb->context2;
2473 abtsiocbp->context3 = cmdiocb->context3;
2474 cmdiocb->context2 = NULL;
2475 cmdiocb->context3 = NULL;
2476 abtsiocbp->iocb_cmpl = lpfc_sli_abort_elsreq_cmpl;
2479 list_add_tail(&abtsiocbp->list, lpfc_iocb_list);
2483 iabt->un.amxri.abortType = ABORT_TYPE_ABTS;
2484 iabt->un.amxri.iotag32 = icmd->un.elsreq64.bdl.ulpIoTag32;
2487 iabt->ulpClass = CLASS3;
2488 iabt->ulpCommand = CMD_ABORT_MXRI64_CN;
2490 if (lpfc_sli_issue_iocb(phba, pring, abtsiocbp, 0) == IOCB_ERROR) {
2491 list_add_tail(&abtsiocbp->list, lpfc_iocb_list);
2499 lpfc_sli_validate_iocb_cmd(struct lpfc_scsi_buf *lpfc_cmd, uint16_t tgt_id,
2500 uint64_t lun_id, struct lpfc_iocbq *iocb,
2501 uint32_t ctx, lpfc_ctx_cmd ctx_cmd)
2505 if (lpfc_cmd == NULL)
2510 if ((lpfc_cmd->pCmd->device->id == tgt_id) &&
2511 (lpfc_cmd->pCmd->device->lun == lun_id))
2515 if (lpfc_cmd->pCmd->device->id == tgt_id)
2519 if (iocb->iocb.ulpContext == ctx)
2525 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
2526 __FUNCTION__, ctx_cmd);
2534 lpfc_sli_sum_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2535 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd ctx_cmd)
2537 struct lpfc_iocbq *iocb, *next_iocb;
2539 struct lpfc_scsi_buf *lpfc_cmd;
2540 int sum = 0, ret_val = 0;
2542 /* Next check the txcmplq */
2543 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
2546 /* Must be a FCP command */
2547 if ((cmd->ulpCommand != CMD_FCP_ICMND64_CR) &&
2548 (cmd->ulpCommand != CMD_FCP_IWRITE64_CR) &&
2549 (cmd->ulpCommand != CMD_FCP_IREAD64_CR)) {
2553 /* context1 MUST be a struct lpfc_scsi_buf */
2554 lpfc_cmd = (struct lpfc_scsi_buf *) (iocb->context1);
2555 ret_val = lpfc_sli_validate_iocb_cmd(lpfc_cmd, tgt_id, lun_id,
2565 lpfc_sli_abort_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2566 uint16_t tgt_id, uint64_t lun_id, uint32_t ctx,
2567 lpfc_ctx_cmd abort_cmd)
2569 struct lpfc_iocbq *iocb, *next_iocb;
2570 struct lpfc_iocbq *abtsiocb = NULL;
2571 struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
2573 struct lpfc_scsi_buf *lpfc_cmd;
2574 int errcnt = 0, ret_val = 0;
2576 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
2579 /* Must be a FCP command */
2580 if ((cmd->ulpCommand != CMD_FCP_ICMND64_CR) &&
2581 (cmd->ulpCommand != CMD_FCP_IWRITE64_CR) &&
2582 (cmd->ulpCommand != CMD_FCP_IREAD64_CR)) {
2586 /* context1 MUST be a struct lpfc_scsi_buf */
2587 lpfc_cmd = (struct lpfc_scsi_buf *) (iocb->context1);
2588 ret_val = lpfc_sli_validate_iocb_cmd(lpfc_cmd, tgt_id, lun_id,
2589 iocb, ctx, abort_cmd);
2593 /* issue ABTS for this IOCB based on iotag */
2594 list_remove_head(lpfc_iocb_list, abtsiocb, struct lpfc_iocbq,
2596 if (abtsiocb == NULL) {
2600 memset(abtsiocb, 0, sizeof (struct lpfc_iocbq));
2602 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
2603 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
2604 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
2605 abtsiocb->iocb.ulpLe = 1;
2606 abtsiocb->iocb.ulpClass = cmd->ulpClass;
2608 if (phba->hba_state >= LPFC_LINK_UP)
2609 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
2611 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
2613 ret_val = lpfc_sli_issue_iocb(phba, pring, abtsiocb, 0);
2614 if (ret_val == IOCB_ERROR) {
2615 list_add_tail(&abtsiocb->list, lpfc_iocb_list);
2625 lpfc_sli_wake_iocb_high_priority(struct lpfc_hba * phba,
2626 struct lpfc_iocbq * queue1,
2627 struct lpfc_iocbq * queue2)
2629 if (queue1->context2 && queue2)
2630 memcpy(queue1->context2, queue2, sizeof (struct lpfc_iocbq));
2632 /* The waiter is looking for LPFC_IO_HIPRI bit to be set
2633 as a signal to wake up */
2634 queue1->iocb_flag |= LPFC_IO_HIPRI;
2639 lpfc_sli_issue_iocb_wait_high_priority(struct lpfc_hba * phba,
2640 struct lpfc_sli_ring * pring,
2641 struct lpfc_iocbq * piocb,
2643 struct lpfc_iocbq * prspiocbq,
2646 int j, delay_time, retval = IOCB_ERROR;
2648 /* The caller must left context1 empty. */
2649 if (piocb->context_un.hipri_wait_queue != 0) {
2654 * If the caller has provided a response iocbq buffer, context2 must
2655 * be NULL or its an error.
2657 if (prspiocbq && piocb->context2) {
2661 piocb->context2 = prspiocbq;
2663 /* Setup callback routine and issue the command. */
2664 piocb->iocb_cmpl = lpfc_sli_wake_iocb_high_priority;
2665 retval = lpfc_sli_issue_iocb(phba, pring, piocb,
2666 flag | SLI_IOCB_HIGH_PRIORITY);
2667 if (retval != IOCB_SUCCESS) {
2668 piocb->context2 = NULL;
2673 * This high-priority iocb was sent out-of-band. Poll for its
2674 * completion rather than wait for a signal. Note that the host_lock
2675 * is held by the midlayer and must be released here to allow the
2676 * interrupt handlers to complete the IO and signal this routine via
2678 * Also, the delay_time is computed to be one second longer than
2679 * the scsi command timeout to give the FW time to abort on
2680 * timeout rather than the driver just giving up. Typically,
2681 * the midlayer does not specify a time for this command so the
2682 * driver is free to enforce its own timeout.
2685 delay_time = ((timeout + 1) * 1000) >> 6;
2686 retval = IOCB_ERROR;
2687 spin_unlock_irq(phba->host->host_lock);
2688 for (j = 0; j < 64; j++) {
2690 if (piocb->iocb_flag & LPFC_IO_HIPRI) {
2691 piocb->iocb_flag &= ~LPFC_IO_HIPRI;
2692 retval = IOCB_SUCCESS;
2697 spin_lock_irq(phba->host->host_lock);
2698 piocb->context2 = NULL;
2702 lpfc_sli_issue_mbox_wait(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq,
2705 DECLARE_WAIT_QUEUE_HEAD(done_q);
2706 DECLARE_WAITQUEUE(wq_entry, current);
2707 uint32_t timeleft = 0;
2710 /* The caller must leave context1 empty. */
2711 if (pmboxq->context1 != 0) {
2712 return (MBX_NOT_FINISHED);
2715 /* setup wake call as IOCB callback */
2716 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
2717 /* setup context field to pass wait_queue pointer to wake function */
2718 pmboxq->context1 = &done_q;
2720 /* start to sleep before we wait, to avoid races */
2721 set_current_state(TASK_INTERRUPTIBLE);
2722 add_wait_queue(&done_q, &wq_entry);
2724 /* now issue the command */
2725 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
2727 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
2728 timeleft = schedule_timeout(timeout * HZ);
2729 pmboxq->context1 = NULL;
2730 /* if schedule_timeout returns 0, we timed out and were not
2732 if (timeleft == 0) {
2733 retval = MBX_TIMEOUT;
2735 retval = MBX_SUCCESS;
2740 set_current_state(TASK_RUNNING);
2741 remove_wait_queue(&done_q, &wq_entry);
2746 lpfc_intr_handler(int irq, void *dev_id, struct pt_regs * regs)
2748 struct lpfc_hba *phba;
2750 uint32_t work_ha_copy;
2751 unsigned long status;
2756 * Get the driver's phba structure from the dev_id and
2757 * assume the HBA is not interrupting.
2759 phba = (struct lpfc_hba *) dev_id;
2761 if (unlikely(!phba))
2764 phba->sli.slistat.sli_intr++;
2767 * Call the HBA to see if it is interrupting. If not, don't claim
2771 /* Ignore all interrupts during initialization. */
2772 if (unlikely(phba->hba_state < LPFC_LINK_DOWN))
2776 * Read host attention register to determine interrupt source
2777 * Clear Attention Sources, except Error Attention (to
2778 * preserve status) and Link Attention
2780 spin_lock(phba->host->host_lock);
2781 ha_copy = readl(phba->HAregaddr);
2782 writel((ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
2783 readl(phba->HAregaddr); /* flush */
2784 spin_unlock(phba->host->host_lock);
2786 if (unlikely(!ha_copy))
2789 work_ha_copy = ha_copy & phba->work_ha_mask;
2791 if (unlikely(work_ha_copy)) {
2792 if (work_ha_copy & HA_LATT) {
2793 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
2795 * Turn off Link Attention interrupts
2796 * until CLEAR_LA done
2798 spin_lock(phba->host->host_lock);
2799 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
2800 control = readl(phba->HCregaddr);
2801 control &= ~HC_LAINT_ENA;
2802 writel(control, phba->HCregaddr);
2803 readl(phba->HCregaddr); /* flush */
2804 spin_unlock(phba->host->host_lock);
2807 work_ha_copy &= ~HA_LATT;
2810 if (work_ha_copy & ~(HA_ERATT|HA_MBATT|HA_LATT)) {
2811 for (i = 0; i < phba->sli.num_rings; i++) {
2812 if (work_ha_copy & (HA_RXATT << (4*i))) {
2814 * Turn off Slow Rings interrupts
2816 spin_lock(phba->host->host_lock);
2817 control = readl(phba->HCregaddr);
2818 control &= ~(HC_R0INT_ENA << i);
2819 writel(control, phba->HCregaddr);
2820 readl(phba->HCregaddr); /* flush */
2821 spin_unlock(phba->host->host_lock);
2826 if (work_ha_copy & HA_ERATT) {
2827 phba->hba_state = LPFC_HBA_ERROR;
2829 * There was a link/board error. Read the
2830 * status register to retrieve the error event
2833 phba->sli.slistat.err_attn_event++;
2834 /* Save status info */
2835 phba->work_hs = readl(phba->HSregaddr);
2836 phba->work_status[0] = readl(phba->MBslimaddr + 0xa8);
2837 phba->work_status[1] = readl(phba->MBslimaddr + 0xac);
2839 /* Clear Chip error bit */
2840 writel(HA_ERATT, phba->HAregaddr);
2841 readl(phba->HAregaddr); /* flush */
2844 * Reseting the HBA is the only reliable way
2845 * to shutdown interrupt when there is a
2848 lpfc_sli_send_reset(phba, phba->hba_state);
2851 spin_lock(phba->host->host_lock);
2852 phba->work_ha |= work_ha_copy;
2853 if (phba->work_wait)
2854 wake_up(phba->work_wait);
2855 spin_unlock(phba->host->host_lock);
2858 ha_copy &= ~(phba->work_ha_mask);
2861 * Process all events on FCP ring. Take the optimized path for
2862 * FCP IO. Any other IO is slow path and is handled by
2863 * the worker thread.
2865 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
2866 status >>= (4*LPFC_FCP_RING);
2867 if (status & HA_RXATT)
2868 lpfc_sli_handle_fast_ring_event(phba,
2869 &phba->sli.ring[LPFC_FCP_RING],
2873 } /* lpfc_intr_handler */