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_init.c 1.168 2004/11/15 11:01:33EST sf_support Exp $
25 #include <linux/version.h>
26 #include <linux/blkdev.h>
27 #include <linux/ctype.h>
28 #include <linux/dma-mapping.h>
29 #include <linux/pci.h>
30 #include <linux/spinlock.h>
32 #include <scsi/scsi_device.h>
33 #include <scsi/scsi_host.h>
36 #include "lpfc_disc.h"
37 #include "lpfc_scsi.h"
39 #include "lpfc_crtn.h"
41 #include "lpfc_logmsg.h"
43 #include "lpfc_version.h"
44 #include "lpfc_compat.h"
46 static int lpfc_parse_vpd(struct lpfc_hba *, uint8_t *);
47 static int lpfc_post_rcv_buf(struct lpfc_hba *);
48 static int lpfc_rdrev_wd30 = 0;
50 /************************************************************************/
52 /* lpfc_config_port_prep */
53 /* This routine will do LPFC initialization prior to the */
54 /* CONFIG_PORT mailbox command. This will be initialized */
55 /* as a SLI layer callback routine. */
56 /* This routine returns 0 on success or -ERESTART if it wants */
57 /* the SLI layer to reset the HBA and try again. Any */
58 /* other return value indicates an error. */
60 /************************************************************************/
62 lpfc_config_port_prep(struct lpfc_hba * phba)
64 lpfc_vpd_t *vp = &phba->vpd;
70 /* Get a Mailbox buffer to setup mailbox commands for HBA
72 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_ATOMIC);
74 phba->hba_state = LPFC_HBA_ERROR;
79 phba->hba_state = LPFC_INIT_MBX_CMDS;
81 /* special handling for LC HBAs */
82 if (lpfc_is_LC_HBA(phba->pcidev->device)) {
84 "key unlock for use with gnu public licensed code only\0";
85 uint32_t *ptext = (uint32_t *) licensed;
87 for (i = 0; i < 56; i += sizeof (uint32_t), ptext++)
88 *ptext = cpu_to_be32(*ptext);
90 /* Setup and issue mailbox READ NVPARAMS command */
91 lpfc_read_nv(phba, pmb);
92 memset((char*)mb->un.varRDnvp.rsvd3, 0,
93 sizeof (mb->un.varRDnvp.rsvd3));
94 memcpy((char*)mb->un.varRDnvp.rsvd3, licensed,
97 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
98 /* Adapter initialization error, mbxCmd <cmd>
99 READ_NVPARM, mbxStatus <status> */
100 lpfc_printf_log(phba,
103 "%d:0324 Config Port initialization "
104 "error, mbxCmd x%x READ_NVPARM, "
107 mb->mbxCommand, mb->mbxStatus);
110 memcpy(phba->wwnn, (char *)mb->un.varRDnvp.nodename,
111 sizeof (mb->un.varRDnvp.nodename));
114 /* Setup and issue mailbox READ REV command */
115 lpfc_read_rev(phba, pmb);
116 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
117 /* Adapter failed to init, mbxCmd <mbxCmd> READ_REV, mbxStatus
119 lpfc_printf_log(phba,
122 "%d:0439 Adapter failed to init, mbxCmd x%x "
123 "READ_REV, mbxStatus x%x\n",
125 mb->mbxCommand, mb->mbxStatus);
126 mempool_free( pmb, phba->mbox_mem_pool);
130 /* The HBA's current state is provided by the ProgType and rr fields.
131 * Read and check the value of these fields before continuing to config
134 if (mb->un.varRdRev.rr == 0 || mb->un.varRdRev.un.b.ProgType != 2) {
137 /* Adapter failed to init, mbxCmd <cmd> READ_REV detected
139 lpfc_printf_log(phba,
142 "%d:0440 Adapter failed to init, mbxCmd x%x "
143 "READ_REV detected outdated firmware"
147 mempool_free(pmb, phba->mbox_mem_pool);
151 vp->rev.sli1FwRev = mb->un.varRdRev.sli1FwRev;
152 memcpy(vp->rev.sli1FwName,
153 (char*)mb->un.varRdRev.sli1FwName, 16);
154 vp->rev.sli2FwRev = mb->un.varRdRev.sli2FwRev;
155 memcpy(vp->rev.sli2FwName,
156 (char *)mb->un.varRdRev.sli2FwName, 16);
159 /* Save information as VPD data */
160 vp->rev.biuRev = mb->un.varRdRev.biuRev;
161 vp->rev.smRev = mb->un.varRdRev.smRev;
162 vp->rev.smFwRev = mb->un.varRdRev.un.smFwRev;
163 vp->rev.endecRev = mb->un.varRdRev.endecRev;
164 vp->rev.fcphHigh = mb->un.varRdRev.fcphHigh;
165 vp->rev.fcphLow = mb->un.varRdRev.fcphLow;
166 vp->rev.feaLevelHigh = mb->un.varRdRev.feaLevelHigh;
167 vp->rev.feaLevelLow = mb->un.varRdRev.feaLevelLow;
168 vp->rev.postKernRev = mb->un.varRdRev.postKernRev;
169 vp->rev.opFwRev = mb->un.varRdRev.opFwRev;
170 lpfc_rdrev_wd30 = mb->un.varWords[30];
172 if (lpfc_is_LC_HBA(phba->pcidev->device))
173 memcpy(phba->RandomData, (char *)&mb->un.varWords[24],
174 sizeof (phba->RandomData));
176 /* Get adapter VPD information */
177 lpfc_dump_mem(phba, pmb);
178 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
179 /* Let it go through even if failed. */
180 /* Adapter failed to init, mbxCmd <cmd> DUMP VPD,
181 mbxStatus <status> */
182 lpfc_printf_log(phba,
185 "%d:0441 VPD not present on adapter, mbxCmd "
186 "x%x DUMP VPD, mbxStatus x%x\n",
188 mb->mbxCommand, mb->mbxStatus);
189 } else if (mb->un.varDmp.ra == 1) {
190 lpfc_parse_vpd(phba, (uint8_t *)&mb->un.varDmp.resp_offset);
192 mempool_free(pmb, phba->mbox_mem_pool);
196 /************************************************************************/
198 /* lpfc_config_port_post */
199 /* This routine will do LPFC initialization after the */
200 /* CONFIG_PORT mailbox command. This will be initialized */
201 /* as a SLI layer callback routine. */
202 /* This routine returns 0 on success. Any other return value */
203 /* indicates an error. */
205 /************************************************************************/
207 lpfc_config_port_post(struct lpfc_hba * phba)
211 struct lpfc_dmabuf *mp;
212 struct lpfc_sli *psli = &phba->sli;
213 uint32_t status, timeout;
214 int i, j, flogi_sent;
215 unsigned long isr_cnt, clk_cnt;
218 /* Get a Mailbox buffer to setup mailbox commands for HBA
220 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_ATOMIC);
222 phba->hba_state = LPFC_HBA_ERROR;
227 /* Setup link timers */
228 lpfc_config_link(phba, pmb);
229 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
230 lpfc_printf_log(phba,
233 "%d:0447 Adapter failed init, mbxCmd x%x "
234 "CONFIG_LINK mbxStatus x%x\n",
236 mb->mbxCommand, mb->mbxStatus);
237 phba->hba_state = LPFC_HBA_ERROR;
238 mempool_free( pmb, phba->mbox_mem_pool);
242 /* Get login parameters for NID. */
243 lpfc_read_sparam(phba, pmb);
244 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
245 lpfc_printf_log(phba,
248 "%d:0448 Adapter failed init, mbxCmd x%x "
249 "READ_SPARM mbxStatus x%x\n",
251 mb->mbxCommand, mb->mbxStatus);
252 phba->hba_state = LPFC_HBA_ERROR;
253 mempool_free( pmb, phba->mbox_mem_pool);
257 mp = (struct lpfc_dmabuf *) pmb->context1;
259 /* The mailbox was populated by the HBA. Flush it to main store for the
260 * driver. Note that all context buffers are from the driver's
261 * dma pool and have length LPFC_BPL_SIZE.
263 pci_dma_sync_single_for_cpu(phba->pcidev, mp->phys, LPFC_BPL_SIZE,
266 memcpy(&phba->fc_sparam, mp->virt, sizeof (struct serv_parm));
267 lpfc_mbuf_free(phba, mp->virt, mp->phys);
269 pmb->context1 = NULL;
271 memcpy(&phba->fc_nodename, &phba->fc_sparam.nodeName,
272 sizeof (struct lpfc_name));
273 memcpy(&phba->fc_portname, &phba->fc_sparam.portName,
274 sizeof (struct lpfc_name));
275 /* If no serial number in VPD data, use low 6 bytes of WWNN */
276 /* This should be consolidated into parse_vpd ? - mr */
277 if (phba->SerialNumber[0] == 0) {
280 outptr = (uint8_t *) & phba->fc_nodename.IEEE[0];
281 for (i = 0; i < 12; i++) {
283 j = ((status & 0xf0) >> 4);
285 phba->SerialNumber[i] =
286 (char)((uint8_t) 0x30 + (uint8_t) j);
288 phba->SerialNumber[i] =
289 (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
293 phba->SerialNumber[i] =
294 (char)((uint8_t) 0x30 + (uint8_t) j);
296 phba->SerialNumber[i] =
297 (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
301 /* This should turn on DELAYED ABTS for ELS timeouts */
302 lpfc_set_slim(phba, pmb, 0x052198, 0x1);
303 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
304 phba->hba_state = LPFC_HBA_ERROR;
305 mempool_free( pmb, phba->mbox_mem_pool);
310 lpfc_read_config(phba, pmb);
311 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
312 lpfc_printf_log(phba,
315 "%d:0453 Adapter failed to init, mbxCmd x%x "
316 "READ_CONFIG, mbxStatus x%x\n",
318 mb->mbxCommand, mb->mbxStatus);
319 phba->hba_state = LPFC_HBA_ERROR;
320 mempool_free( pmb, phba->mbox_mem_pool);
324 /* Reset the DFT_HBA_Q_DEPTH to the max xri */
325 if (phba->cfg_hba_queue_depth > (mb->un.varRdConfig.max_xri+1))
326 phba->cfg_hba_queue_depth =
327 mb->un.varRdConfig.max_xri + 1;
329 phba->lmt = mb->un.varRdConfig.lmt;
330 /* HBA is not 4GB capable, or HBA is not 2GB capable,
331 don't let link speed ask for it */
332 if ((((phba->lmt & LMT_4250_10bit) != LMT_4250_10bit) &&
333 (phba->cfg_link_speed > LINK_SPEED_2G)) ||
334 (((phba->lmt & LMT_2125_10bit) != LMT_2125_10bit) &&
335 (phba->cfg_link_speed > LINK_SPEED_1G))) {
336 /* Reset link speed to auto. 1G/2GB HBA cfg'd for 4G */
337 lpfc_printf_log(phba,
340 "%d:1302 Invalid speed for this board: "
341 "Reset link speed to auto: x%x\n",
343 phba->cfg_link_speed);
344 phba->cfg_link_speed = LINK_SPEED_AUTO;
347 if (!phba->intr_inited) {
348 /* Add our interrupt routine to kernel's interrupt chain &
351 if (request_irq(phba->pcidev->irq,
356 /* Enable interrupt handler failed */
357 lpfc_printf_log(phba,
360 "%d:0451 Enable interrupt handler "
363 phba->hba_state = LPFC_HBA_ERROR;
364 mempool_free(pmb, phba->mbox_mem_pool);
368 (HC_MBINT_ENA | HC_ERINT_ENA | HC_LAINT_ENA);
371 phba->hba_state = LPFC_LINK_DOWN;
373 /* Only process IOCBs on ring 0 till hba_state is READY */
374 if (psli->ring[psli->ip_ring].cmdringaddr)
375 psli->ring[psli->ip_ring].flag |= LPFC_STOP_IOCB_EVENT;
376 if (psli->ring[psli->fcp_ring].cmdringaddr)
377 psli->ring[psli->fcp_ring].flag |= LPFC_STOP_IOCB_EVENT;
378 if (psli->ring[psli->next_ring].cmdringaddr)
379 psli->ring[psli->next_ring].flag |= LPFC_STOP_IOCB_EVENT;
381 /* Post receive buffers for desired rings */
382 lpfc_post_rcv_buf(phba);
384 /* Enable appropriate host interrupts */
385 status = readl(phba->HCregaddr);
386 status |= phba->intr_inited;
387 if (psli->sliinit.num_rings > 0)
388 status |= HC_R0INT_ENA;
389 if (psli->sliinit.num_rings > 1)
390 status |= HC_R1INT_ENA;
391 if (psli->sliinit.num_rings > 2)
392 status |= HC_R2INT_ENA;
393 if (psli->sliinit.num_rings > 3)
394 status |= HC_R3INT_ENA;
396 writel(status, phba->HCregaddr);
397 readl(phba->HCregaddr); /* flush */
399 /* Setup and issue mailbox INITIALIZE LINK command */
400 lpfc_init_link(phba, pmb, phba->cfg_topology,
401 phba->cfg_link_speed);
403 isr_cnt = psli->slistat.sliIntr;
406 if (lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT) != MBX_SUCCESS) {
407 lpfc_printf_log(phba,
410 "%d:0454 Adapter failed to init, mbxCmd x%x "
411 "INIT_LINK, mbxStatus x%x\n",
413 mb->mbxCommand, mb->mbxStatus);
415 /* Clear all interrupt enable conditions */
416 writel(0, phba->HCregaddr);
417 readl(phba->HCregaddr); /* flush */
418 /* Clear all pending interrupts */
419 writel(0xffffffff, phba->HAregaddr);
420 readl(phba->HAregaddr); /* flush */
422 free_irq(phba->pcidev->irq, phba);
423 phba->hba_state = LPFC_HBA_ERROR;
424 mempool_free(pmb, phba->mbox_mem_pool);
427 /* MBOX buffer will be freed in mbox compl */
430 * Setup the ring 0 (els) timeout handler
432 timeout = phba->fc_ratov << 1;
434 phba->els_tmofunc.expires = jiffies + HZ * timeout;
435 add_timer(&phba->els_tmofunc);
437 phba->fc_prevDID = Mask_DID;
440 while ((phba->hba_state != LPFC_HBA_READY) ||
441 (phba->num_disc_nodes) || (phba->fc_prli_sent) ||
442 ((phba->fc_map_cnt == 0) && (i<2)) ||
443 (psli->sliinit.sli_flag & LPFC_SLI_MBOX_ACTIVE)) {
444 /* Check every second for 30 retries. */
449 if ((i >= 15) && (phba->hba_state <= LPFC_LINK_DOWN)) {
450 /* The link is down. Set linkdown timeout */
454 /* Delay for 1 second to give discovery time to complete. */
455 for (j = 0; j < 20; j++) {
456 /* On some systems, the driver's attach/detect routines
457 * are uninterruptible. Since the driver cannot predict
458 * when this is true, just manually call the ISR every
459 * 50 ms to service any interrupts.
462 if (isr_cnt == psli->slistat.sliIntr) {
464 isr_cnt = psli->slistat.sliIntr;
467 isr_cnt = psli->slistat.sliIntr;
469 if (clk_cnt == jiffies) {
470 /* REMOVE: IF THIS HAPPENS, SYSTEM CLOCK IS NOT RUNNING.
471 * WE HAVE TO MANUALLY CALL OUR TIMEOUT ROUTINES.
477 /* Since num_disc_nodes keys off of PLOGI, delay a bit to let
478 * any potential PRLIs to flush thru the SLI sub-system.
481 if (isr_cnt == psli->slistat.sliIntr) {
488 /************************************************************************/
490 /* lpfc_hba_down_prep */
491 /* This routine will do LPFC uninitialization before the */
492 /* HBA is reset when bringing down the SLI Layer. This will be */
493 /* initialized as a SLI layer callback routine. */
494 /* This routine returns 0 on success. Any other return value */
495 /* indicates an error. */
497 /************************************************************************/
499 lpfc_hba_down_prep(struct lpfc_hba * phba)
501 /* Disable interrupts */
502 writel(0, phba->HCregaddr);
503 readl(phba->HCregaddr); /* flush */
505 /* Cleanup potential discovery resources */
506 lpfc_els_flush_rscn(phba);
507 lpfc_els_flush_cmd(phba);
508 lpfc_disc_flush_list(phba);
513 /************************************************************************/
515 /* lpfc_handle_eratt */
516 /* This routine will handle processing a Host Attention */
517 /* Error Status event. This will be initialized */
518 /* as a SLI layer callback routine. */
520 /************************************************************************/
522 lpfc_handle_eratt(struct lpfc_hba * phba, uint32_t status)
524 struct lpfc_sli *psli;
525 struct lpfc_sli_ring *pring;
526 struct lpfc_iocbq *iocb, *next_iocb;
527 IOCB_t *icmd = NULL, *cmd = NULL;
528 struct lpfc_scsi_buf *lpfc_cmd;
529 volatile uint32_t status1, status2;
534 from_slim = ((uint8_t *)phba->MBslimaddr + 0xa8);
535 status1 = readl( from_slim);
536 from_slim = ((uint8_t *)phba->MBslimaddr + 0xac);
537 status2 = readl( from_slim);
539 if (status & HS_FFER6) {
540 /* Re-establishing Link */
541 spin_lock_irqsave(phba->host->host_lock, iflag);
542 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
543 "%d:1301 Re-establishing Link "
544 "Data: x%x x%x x%x\n",
545 phba->brd_no, status, status1, status2);
546 phba->fc_flag |= FC_ESTABLISH_LINK;
549 * Firmware stops when it triggled erratt with HS_FFER6.
550 * That could cause the I/Os dropped by the firmware.
551 * Error iocb (I/O) on txcmplq and let the SCSI layer
552 * retry it after re-establishing link.
554 pring = &psli->ring[psli->fcp_ring];
556 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
560 /* Must be a FCP command */
561 if ((cmd->ulpCommand != CMD_FCP_ICMND64_CR) &&
562 (cmd->ulpCommand != CMD_FCP_IWRITE64_CR) &&
563 (cmd->ulpCommand != CMD_FCP_IREAD64_CR)) {
567 /* context1 MUST be a struct lpfc_scsi_buf */
568 lpfc_cmd = (struct lpfc_scsi_buf *)(iocb->context1);
573 list_del(&iocb->list);
574 pring->txcmplq_cnt--;
576 if (iocb->iocb_cmpl) {
578 icmd->ulpStatus = IOSTAT_LOCAL_REJECT;
579 icmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
580 (iocb->iocb_cmpl)(phba, iocb, iocb);
582 mempool_free( iocb, phba->iocb_mem_pool);
587 * There was a firmware error. Take the hba offline and then
588 * attempt to restart it.
590 spin_unlock_irqrestore(phba->host->host_lock, iflag);
592 if (lpfc_online(phba) == 0) { /* Initialize the HBA */
593 mod_timer(&phba->fc_estabtmo, jiffies + HZ * 60);
597 /* The if clause above forces this code path when the status
598 * failure is a value other than FFER6. Do not call the offline
599 * twice. This is the adapter hardware error path.
601 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
602 "%d:0457 Adapter Hardware Error "
603 "Data: x%x x%x x%x\n",
604 phba->brd_no, status, status1, status2);
609 * Restart all traffic to this host. Since the fc_transport
610 * block functions (future) were not called in lpfc_offline,
611 * don't call them here.
613 scsi_unblock_requests(phba->host);
618 /************************************************************************/
620 /* lpfc_handle_latt */
621 /* This routine will handle processing a Host Attention */
622 /* Link Status event. This will be initialized */
623 /* as a SLI layer callback routine. */
625 /************************************************************************/
627 lpfc_handle_latt(struct lpfc_hba * phba)
629 struct lpfc_sli *psli;
631 volatile uint32_t control;
635 spin_lock_irqsave(phba->host->host_lock, iflag);
637 /* called from host_interrupt, to process LATT */
639 psli->slistat.linkEvent++;
641 /* Get a buffer which will be used for mailbox commands */
642 if ((pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
644 if (lpfc_read_la(phba, pmb) == 0) {
645 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_la;
646 if (lpfc_sli_issue_mbox
647 (phba, pmb, (MBX_NOWAIT | MBX_STOP_IOCB))
648 != MBX_NOT_FINISHED) {
649 /* Turn off Link Attention interrupts until
651 psli->sliinit.sli_flag &= ~LPFC_PROCESS_LA;
652 control = readl(phba->HCregaddr);
653 control &= ~HC_LAINT_ENA;
654 writel(control, phba->HCregaddr);
655 readl(phba->HCregaddr); /* flush */
657 /* Clear Link Attention in HA REG */
658 writel(HA_LATT, phba->HAregaddr);
659 readl(phba->HAregaddr); /* flush */
660 spin_unlock_irqrestore(phba->host->host_lock,
664 mempool_free(pmb, phba->mbox_mem_pool);
667 mempool_free(pmb, phba->mbox_mem_pool);
671 /* Clear Link Attention in HA REG */
672 writel(HA_LATT, phba->HAregaddr);
673 readl(phba->HAregaddr); /* flush */
675 phba->hba_state = LPFC_HBA_ERROR;
676 spin_unlock_irqrestore(phba->host->host_lock, iflag);
680 /************************************************************************/
683 /* This routine will parse the VPD data */
685 /************************************************************************/
687 lpfc_parse_vpd(struct lpfc_hba * phba, uint8_t * vpd)
689 uint8_t lenlo, lenhi;
696 lpfc_printf_log(phba,
699 "%d:0455 Vital Product Data: x%x x%x x%x x%x\n",
701 (uint32_t) vpd[0], (uint32_t) vpd[1], (uint32_t) vpd[2],
704 switch (vpd[index]) {
711 i = ((((unsigned short)lenhi) << 8) + lenlo);
720 i = ((((unsigned short)lenhi) << 8) + lenlo);
722 /* Look for Serial Number */
723 if ((vpd[index] == 'S')
724 && (vpd[index + 1] == 'N')) {
726 Length = &vpd[index];
731 phba->SerialNumber[j++] =
736 phba->SerialNumber[j] = 0;
740 Length = &vpd[index];
759 /**************************************************/
760 /* lpfc_post_buffer */
762 /* This routine will post count buffers to the */
763 /* ring with the QUE_RING_BUF_CN command. This */
764 /* allows 3 buffers / command to be posted. */
765 /* Returns the number of buffers NOT posted. */
766 /**************************************************/
768 lpfc_post_buffer(struct lpfc_hba * phba, struct lpfc_sli_ring * pring, int cnt,
772 struct lpfc_iocbq *iocb;
773 struct lpfc_dmabuf *mp1, *mp2;
775 cnt += pring->missbufcnt;
777 /* While there are buffers to post */
779 /* Allocate buffer for command iocb */
780 if ((iocb = mempool_alloc(phba->iocb_mem_pool, GFP_ATOMIC))
782 pring->missbufcnt = cnt;
785 memset(iocb, 0, sizeof (struct lpfc_iocbq));
788 /* 2 buffers can be posted per command */
789 /* Allocate buffer to post */
790 mp1 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_ATOMIC);
792 mp1->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
794 if (mp1 == 0 || mp1->virt == 0) {
798 mempool_free( iocb, phba->iocb_mem_pool);
799 pring->missbufcnt = cnt;
803 INIT_LIST_HEAD(&mp1->list);
804 /* Allocate buffer to post */
806 mp2 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
808 mp2->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
810 if (mp2 == 0 || mp2->virt == 0) {
813 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
815 mempool_free( iocb, phba->iocb_mem_pool);
816 pring->missbufcnt = cnt;
820 INIT_LIST_HEAD(&mp2->list);
825 icmd->un.cont64[0].addrHigh = putPaddrHigh(mp1->phys);
826 icmd->un.cont64[0].addrLow = putPaddrLow(mp1->phys);
827 icmd->un.cont64[0].tus.f.bdeSize = FCELSSIZE;
828 icmd->ulpBdeCount = 1;
831 icmd->un.cont64[1].addrHigh = putPaddrHigh(mp2->phys);
832 icmd->un.cont64[1].addrLow = putPaddrLow(mp2->phys);
833 icmd->un.cont64[1].tus.f.bdeSize = FCELSSIZE;
835 icmd->ulpBdeCount = 2;
838 icmd->ulpCommand = CMD_QUE_RING_BUF64_CN;
839 icmd->ulpIoTag = lpfc_sli_next_iotag(phba, pring);
842 if (lpfc_sli_issue_iocb(phba, pring, iocb, 0) == IOCB_ERROR) {
843 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
846 lpfc_mbuf_free(phba, mp2->virt, mp2->phys);
849 mempool_free( iocb, phba->iocb_mem_pool);
850 pring->missbufcnt = cnt;
853 lpfc_sli_ringpostbuf_put(phba, pring, mp1);
855 lpfc_sli_ringpostbuf_put(phba, pring, mp2);
858 pring->missbufcnt = 0;
862 /************************************************************************/
864 /* lpfc_post_rcv_buf */
865 /* This routine post initial rcv buffers to the configured rings */
867 /************************************************************************/
869 lpfc_post_rcv_buf(struct lpfc_hba * phba)
871 struct lpfc_sli *psli = &phba->sli;
873 /* Ring 0, ELS / CT buffers */
874 lpfc_post_buffer(phba, &psli->ring[LPFC_ELS_RING], LPFC_BUF_RING0, 1);
875 /* Ring 2 - FCP no buffers needed */
880 #define S(N,V) (((V)<<(N))|((V)>>(32-(N))))
882 /************************************************************************/
886 /************************************************************************/
888 lpfc_sha_init(uint32_t * HashResultPointer)
890 HashResultPointer[0] = 0x67452301;
891 HashResultPointer[1] = 0xEFCDAB89;
892 HashResultPointer[2] = 0x98BADCFE;
893 HashResultPointer[3] = 0x10325476;
894 HashResultPointer[4] = 0xC3D2E1F0;
897 /************************************************************************/
899 /* lpfc_sha_iterate */
901 /************************************************************************/
903 lpfc_sha_iterate(uint32_t * HashResultPointer, uint32_t * HashWorkingPointer)
907 uint32_t A, B, C, D, E;
910 HashWorkingPointer[t] =
912 HashWorkingPointer[t - 3] ^ HashWorkingPointer[t -
914 HashWorkingPointer[t - 14] ^ HashWorkingPointer[t - 16]);
917 A = HashResultPointer[0];
918 B = HashResultPointer[1];
919 C = HashResultPointer[2];
920 D = HashResultPointer[3];
921 E = HashResultPointer[4];
925 TEMP = ((B & C) | ((~B) & D)) + 0x5A827999;
927 TEMP = (B ^ C ^ D) + 0x6ED9EBA1;
929 TEMP = ((B & C) | (B & D) | (C & D)) + 0x8F1BBCDC;
931 TEMP = (B ^ C ^ D) + 0xCA62C1D6;
933 TEMP += S(5, A) + E + HashWorkingPointer[t];
941 HashResultPointer[0] += A;
942 HashResultPointer[1] += B;
943 HashResultPointer[2] += C;
944 HashResultPointer[3] += D;
945 HashResultPointer[4] += E;
949 /************************************************************************/
951 /* lpfc_challenge_key */
953 /************************************************************************/
955 lpfc_challenge_key(uint32_t * RandomChallenge, uint32_t * HashWorking)
957 *HashWorking = (*RandomChallenge ^ *HashWorking);
960 /************************************************************************/
964 /************************************************************************/
966 lpfc_hba_init(struct lpfc_hba *phba, uint32_t *hbainit)
969 uint32_t *HashWorking;
970 uint32_t *pwwnn = phba->wwnn;
972 HashWorking = kmalloc(80 * sizeof(uint32_t), GFP_ATOMIC);
976 memset(HashWorking, 0, (80 * sizeof(uint32_t)));
977 HashWorking[0] = HashWorking[78] = *pwwnn++;
978 HashWorking[1] = HashWorking[79] = *pwwnn;
980 for (t = 0; t < 7; t++)
981 lpfc_challenge_key(phba->RandomData + t, HashWorking + t);
983 lpfc_sha_init(hbainit);
984 lpfc_sha_iterate(hbainit, HashWorking);
989 lpfc_consistent_bind_cleanup(struct lpfc_hba * phba)
991 struct lpfc_bindlist *bdlp, *next_bdlp;
993 list_for_each_entry_safe(bdlp, next_bdlp,
994 &phba->fc_nlpbind_list, nlp_listp) {
995 list_del(&bdlp->nlp_listp);
996 mempool_free( bdlp, phba->bind_mem_pool);
998 phba->fc_bind_cnt = 0;
1002 lpfc_cleanup(struct lpfc_hba * phba, uint32_t save_bind)
1004 struct lpfc_nodelist *ndlp, *next_ndlp;
1006 /* clean up phba - lpfc specific */
1007 lpfc_can_disctmo(phba);
1008 list_for_each_entry_safe(ndlp, next_ndlp, &phba->fc_nlpunmap_list,
1010 lpfc_nlp_remove(phba, ndlp);
1013 list_for_each_entry_safe(ndlp, next_ndlp, &phba->fc_nlpmap_list,
1015 lpfc_nlp_remove(phba, ndlp);
1018 list_for_each_entry_safe(ndlp, next_ndlp, &phba->fc_unused_list,
1020 lpfc_nlp_list(phba, ndlp, NLP_NO_LIST);
1023 list_for_each_entry_safe(ndlp, next_ndlp, &phba->fc_plogi_list,
1025 lpfc_nlp_remove(phba, ndlp);
1028 list_for_each_entry_safe(ndlp, next_ndlp, &phba->fc_adisc_list,
1030 lpfc_nlp_remove(phba, ndlp);
1033 list_for_each_entry_safe(ndlp, next_ndlp, &phba->fc_reglogin_list,
1035 lpfc_nlp_remove(phba, ndlp);
1038 list_for_each_entry_safe(ndlp, next_ndlp, &phba->fc_prli_list,
1040 lpfc_nlp_remove(phba, ndlp);
1043 list_for_each_entry_safe(ndlp, next_ndlp, &phba->fc_npr_list,
1045 lpfc_nlp_remove(phba, ndlp);
1048 if (save_bind == 0) {
1049 lpfc_consistent_bind_cleanup(phba);
1052 INIT_LIST_HEAD(&phba->fc_nlpmap_list);
1053 INIT_LIST_HEAD(&phba->fc_nlpunmap_list);
1054 INIT_LIST_HEAD(&phba->fc_unused_list);
1055 INIT_LIST_HEAD(&phba->fc_plogi_list);
1056 INIT_LIST_HEAD(&phba->fc_adisc_list);
1057 INIT_LIST_HEAD(&phba->fc_reglogin_list);
1058 INIT_LIST_HEAD(&phba->fc_prli_list);
1059 INIT_LIST_HEAD(&phba->fc_npr_list);
1061 phba->fc_map_cnt = 0;
1062 phba->fc_unmap_cnt = 0;
1063 phba->fc_plogi_cnt = 0;
1064 phba->fc_adisc_cnt = 0;
1065 phba->fc_reglogin_cnt = 0;
1066 phba->fc_prli_cnt = 0;
1067 phba->fc_npr_cnt = 0;
1068 phba->fc_unused_cnt= 0;
1073 lpfc_establish_link_tmo(unsigned long ptr)
1075 struct lpfc_hba *phba = (struct lpfc_hba *)ptr;
1076 unsigned long iflag;
1078 spin_lock_irqsave(phba->host->host_lock, iflag);
1080 /* Re-establishing Link, timer expired */
1081 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
1082 "%d:1300 Re-establishing Link, timer expired "
1084 phba->brd_no, phba->fc_flag, phba->hba_state);
1085 phba->fc_flag &= ~FC_ESTABLISH_LINK;
1086 spin_unlock_irqrestore(phba->host->host_lock, iflag);
1090 lpfc_online(struct lpfc_hba * phba)
1095 if (!(phba->fc_flag & FC_OFFLINE_MODE))
1098 lpfc_printf_log(phba,
1101 "%d:0458 Bring Adapter online\n",
1104 if (!lpfc_sli_queue_setup(phba))
1107 if (lpfc_sli_hba_setup(phba)) /* Initialize the HBA */
1110 phba->fc_flag &= ~FC_OFFLINE_MODE;
1113 * Restart all traffic to this host. Since the fc_transport block
1114 * functions (future) were not called in lpfc_offline, don't call them
1117 scsi_unblock_requests(phba->host);
1122 lpfc_offline(struct lpfc_hba * phba)
1124 struct lpfc_sli_ring *pring;
1125 struct lpfc_sli *psli;
1126 unsigned long iflag;
1132 if (phba->fc_flag & FC_OFFLINE_MODE)
1136 * Don't call the fc_transport block api (future). The device is
1137 * going offline and causing a timer to fire in the midlayer is
1138 * unproductive. Just block all new requests until the driver
1139 * comes back online.
1141 scsi_block_requests(phba->host);
1143 pring = &psli->ring[psli->fcp_ring];
1145 lpfc_linkdown(phba);
1147 /* The linkdown event takes 30 seconds to timeout. */
1148 while (pring->txcmplq_cnt) {
1154 /* stop all timers associated with this hba */
1155 spin_lock_irqsave(phba->host->host_lock, iflag);
1156 lpfc_stop_timer(phba);
1157 spin_unlock_irqrestore(phba->host->host_lock, iflag);
1159 lpfc_printf_log(phba,
1162 "%d:0460 Bring Adapter offline\n",
1165 /* Bring down the SLI Layer and cleanup. The HBA is offline
1167 lpfc_sli_hba_down(phba);
1168 lpfc_cleanup(phba, 1);
1169 phba->fc_flag |= FC_OFFLINE_MODE;
1173 /******************************************************************************
1174 * Function name : lpfc_scsi_free
1176 * Description : Called from fc_detach to free scsi tgt / lun resources
1178 ******************************************************************************/
1180 lpfc_scsi_free(struct lpfc_hba * phba)
1182 struct lpfc_target *targetp;
1185 for (i = 0; i < MAX_FCP_TARGET; i++) {
1186 targetp = phba->device_queue_hash[i];
1189 phba->device_queue_hash[i] = NULL;
1196 lpfc_wakeup_event(struct lpfc_hba * phba, fcEVTHDR_t * ep)
1198 ep->e_mode &= ~E_SLEEPING_MODE;
1199 switch (ep->e_mask) {
1200 case FC_REG_LINK_EVENT:
1201 wake_up_interruptible(&phba->linkevtwq);
1203 case FC_REG_RSCN_EVENT:
1204 wake_up_interruptible(&phba->rscnevtwq);
1206 case FC_REG_CT_EVENT:
1207 wake_up_interruptible(&phba->ctevtwq);
1214 lpfc_put_event(struct lpfc_hba * phba, uint32_t evcode, uint32_t evdata0,
1215 void * evdata1, uint32_t evdata2, uint32_t evdata3)
1218 fcEVTHDR_t *ehp = phba->fc_evt_head;
1220 void *fstype = NULL;
1221 struct lpfc_dmabuf *mp;
1222 struct lpfc_sli_ct_request *ctp;
1223 struct lpfc_hba_event *rec;
1227 case HBA_EVENT_RSCN:
1228 evtype = FC_REG_RSCN_EVENT;
1230 case HBA_EVENT_LINK_DOWN:
1231 case HBA_EVENT_LINK_UP:
1232 evtype = FC_REG_LINK_EVENT;
1235 evtype = FC_REG_CT_EVENT;
1238 if (evtype == FC_REG_RSCN_EVENT || evtype == FC_REG_LINK_EVENT) {
1239 rec = &phba->hbaevt[phba->hba_event_put];
1240 rec->fc_eventcode = evcode;
1241 rec->fc_evdata1 = evdata0;
1242 rec->fc_evdata2 = (uint32_t)(unsigned long)evdata1;
1243 rec->fc_evdata3 = evdata2;
1244 rec->fc_evdata4 = evdata3;
1246 phba->hba_event_put++;
1247 if (phba->hba_event_put >= MAX_HBAEVT)
1248 phba->hba_event_put = 0;
1250 if (phba->hba_event_put == phba->hba_event_get) {
1251 phba->hba_event_missed++;
1252 phba->hba_event_get++;
1253 if (phba->hba_event_get >= MAX_HBAEVT)
1254 phba->hba_event_get = 0;
1258 if (evtype == FC_REG_CT_EVENT) {
1259 mp = (struct lpfc_dmabuf *) evdata1;
1260 ctp = (struct lpfc_sli_ct_request *) mp->virt;
1261 fstype = (void *)(ulong) (ctp->FsType);
1264 while (ehp && ((ehp->e_mask != evtype) || (ehp->e_type != fstype)))
1265 ehp = (fcEVTHDR_t *) ehp->e_next_header;
1272 while (ep && !(found)) {
1273 if (ep->evt_sleep) {
1275 case FC_REG_CT_EVENT:
1276 if ((ep->evt_type ==
1277 (void *)(ulong) FC_FSTYPE_ALL)
1278 || (ep->evt_type == fstype)) {
1280 ep->evt_data0 = evdata0; /* tag */
1281 ep->evt_data1 = evdata1; /* buffer
1283 ep->evt_data2 = evdata2; /* count */
1285 if (ehp->e_mode & E_SLEEPING_MODE) {
1288 lpfc_wakeup_event(phba, ehp);
1290 /* For FC_REG_CT_EVENT just give it to
1296 ep->evt_data0 = evdata0;
1297 ep->evt_data1 = evdata1;
1298 ep->evt_data2 = evdata2;
1300 if ((ehp->e_mode & E_SLEEPING_MODE)
1301 && !(ehp->e_flag & E_GET_EVENT_ACTIVE)) {
1302 ehp->e_flag |= E_GET_EVENT_ACTIVE;
1303 lpfc_wakeup_event(phba, ehp);
1305 /* For all other events, give it to every one
1312 if (evtype == FC_REG_LINK_EVENT)
1313 phba->nport_event_cnt++;
1319 lpfc_stop_timer(struct lpfc_hba * phba)
1321 struct lpfc_sli *psli = &phba->sli;
1323 /* Instead of a timer, this has been converted to a
1324 * deferred procedding list.
1326 while (!list_empty(&phba->freebufList)) {
1327 struct lpfc_dmabuf *mp;
1329 mp = (struct lpfc_dmabuf *)(phba->freebufList.next);
1331 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1332 list_del(&mp->list);
1337 del_timer_sync(&phba->fc_estabtmo);
1338 del_timer_sync(&phba->fc_disctmo);
1339 del_timer_sync(&phba->fc_scantmo);
1340 del_timer_sync(&phba->fc_fdmitmo);
1341 del_timer_sync(&phba->els_tmofunc);
1343 del_timer_sync(&psli->mbox_tmo);