1 /*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
3 * Fibre Channel Host Bus Adapters. *
4 * Copyright (C) 2004-2008 Emulex. All rights reserved. *
5 * EMULEX and SLI are trademarks of Emulex. *
7 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
9 * This program is free software; you can redistribute it and/or *
10 * modify it under the terms of version 2 of the GNU General *
11 * Public License as published by the Free Software Foundation. *
12 * This program is distributed in the hope that it will be useful. *
13 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
14 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
15 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
16 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
17 * TO BE LEGALLY INVALID. See the GNU General Public License for *
18 * more details, a copy of which can be found in the file COPYING *
19 * included with this package. *
20 *******************************************************************/
22 #include <linux/blkdev.h>
23 #include <linux/delay.h>
24 #include <linux/dma-mapping.h>
25 #include <linux/idr.h>
26 #include <linux/interrupt.h>
27 #include <linux/kthread.h>
28 #include <linux/pci.h>
29 #include <linux/spinlock.h>
30 #include <linux/ctype.h>
32 #include <scsi/scsi.h>
33 #include <scsi/scsi_device.h>
34 #include <scsi/scsi_host.h>
35 #include <scsi/scsi_transport_fc.h>
40 #include "lpfc_disc.h"
41 #include "lpfc_scsi.h"
43 #include "lpfc_logmsg.h"
44 #include "lpfc_crtn.h"
45 #include "lpfc_vport.h"
46 #include "lpfc_version.h"
48 static int lpfc_parse_vpd(struct lpfc_hba *, uint8_t *, int);
49 static void lpfc_get_hba_model_desc(struct lpfc_hba *, uint8_t *, uint8_t *);
50 static int lpfc_post_rcv_buf(struct lpfc_hba *);
52 static struct scsi_transport_template *lpfc_transport_template = NULL;
53 static struct scsi_transport_template *lpfc_vport_transport_template = NULL;
54 static DEFINE_IDR(lpfc_hba_index);
57 * lpfc_config_port_prep: Perform lpfc initialization prior to config port.
58 * @phba: pointer to lpfc hba data structure.
60 * This routine will do LPFC initialization prior to issuing the CONFIG_PORT
61 * mailbox command. It retrieves the revision information from the HBA and
62 * collects the Vital Product Data (VPD) about the HBA for preparing the
63 * configuration of the HBA.
67 * -ERESTART - requests the SLI layer to reset the HBA and try again.
68 * Any other value - indicates an error.
71 lpfc_config_port_prep(struct lpfc_hba *phba)
73 lpfc_vpd_t *vp = &phba->vpd;
77 char *lpfc_vpd_data = NULL;
79 static char licensed[56] =
80 "key unlock for use with gnu public licensed code only\0";
81 static int init_key = 1;
83 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
85 phba->link_state = LPFC_HBA_ERROR;
90 phba->link_state = LPFC_INIT_MBX_CMDS;
92 if (lpfc_is_LC_HBA(phba->pcidev->device)) {
94 uint32_t *ptext = (uint32_t *) licensed;
96 for (i = 0; i < 56; i += sizeof (uint32_t), ptext++)
97 *ptext = cpu_to_be32(*ptext);
101 lpfc_read_nv(phba, pmb);
102 memset((char*)mb->un.varRDnvp.rsvd3, 0,
103 sizeof (mb->un.varRDnvp.rsvd3));
104 memcpy((char*)mb->un.varRDnvp.rsvd3, licensed,
107 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
109 if (rc != MBX_SUCCESS) {
110 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
111 "0324 Config Port initialization "
112 "error, mbxCmd x%x READ_NVPARM, "
114 mb->mbxCommand, mb->mbxStatus);
115 mempool_free(pmb, phba->mbox_mem_pool);
118 memcpy(phba->wwnn, (char *)mb->un.varRDnvp.nodename,
120 memcpy(phba->wwpn, (char *)mb->un.varRDnvp.portname,
124 phba->sli3_options = 0x0;
126 /* Setup and issue mailbox READ REV command */
127 lpfc_read_rev(phba, pmb);
128 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
129 if (rc != MBX_SUCCESS) {
130 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
131 "0439 Adapter failed to init, mbxCmd x%x "
132 "READ_REV, mbxStatus x%x\n",
133 mb->mbxCommand, mb->mbxStatus);
134 mempool_free( pmb, phba->mbox_mem_pool);
140 * The value of rr must be 1 since the driver set the cv field to 1.
141 * This setting requires the FW to set all revision fields.
143 if (mb->un.varRdRev.rr == 0) {
145 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
146 "0440 Adapter failed to init, READ_REV has "
147 "missing revision information.\n");
148 mempool_free(pmb, phba->mbox_mem_pool);
152 if (phba->sli_rev == 3 && !mb->un.varRdRev.v3rsp) {
153 mempool_free(pmb, phba->mbox_mem_pool);
157 /* Save information as VPD data */
159 memcpy(&vp->sli3Feat, &mb->un.varRdRev.sli3Feat, sizeof(uint32_t));
160 vp->rev.sli1FwRev = mb->un.varRdRev.sli1FwRev;
161 memcpy(vp->rev.sli1FwName, (char*) mb->un.varRdRev.sli1FwName, 16);
162 vp->rev.sli2FwRev = mb->un.varRdRev.sli2FwRev;
163 memcpy(vp->rev.sli2FwName, (char *) mb->un.varRdRev.sli2FwName, 16);
164 vp->rev.biuRev = mb->un.varRdRev.biuRev;
165 vp->rev.smRev = mb->un.varRdRev.smRev;
166 vp->rev.smFwRev = mb->un.varRdRev.un.smFwRev;
167 vp->rev.endecRev = mb->un.varRdRev.endecRev;
168 vp->rev.fcphHigh = mb->un.varRdRev.fcphHigh;
169 vp->rev.fcphLow = mb->un.varRdRev.fcphLow;
170 vp->rev.feaLevelHigh = mb->un.varRdRev.feaLevelHigh;
171 vp->rev.feaLevelLow = mb->un.varRdRev.feaLevelLow;
172 vp->rev.postKernRev = mb->un.varRdRev.postKernRev;
173 vp->rev.opFwRev = mb->un.varRdRev.opFwRev;
175 /* If the sli feature level is less then 9, we must
176 * tear down all RPIs and VPIs on link down if NPIV
179 if (vp->rev.feaLevelHigh < 9)
180 phba->sli3_options |= LPFC_SLI3_VPORT_TEARDOWN;
182 if (lpfc_is_LC_HBA(phba->pcidev->device))
183 memcpy(phba->RandomData, (char *)&mb->un.varWords[24],
184 sizeof (phba->RandomData));
186 /* Get adapter VPD information */
187 lpfc_vpd_data = kmalloc(DMP_VPD_SIZE, GFP_KERNEL);
192 lpfc_dump_mem(phba, pmb, offset);
193 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
195 if (rc != MBX_SUCCESS) {
196 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
197 "0441 VPD not present on adapter, "
198 "mbxCmd x%x DUMP VPD, mbxStatus x%x\n",
199 mb->mbxCommand, mb->mbxStatus);
200 mb->un.varDmp.word_cnt = 0;
202 if (mb->un.varDmp.word_cnt > DMP_VPD_SIZE - offset)
203 mb->un.varDmp.word_cnt = DMP_VPD_SIZE - offset;
204 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
205 lpfc_vpd_data + offset,
206 mb->un.varDmp.word_cnt);
207 offset += mb->un.varDmp.word_cnt;
208 } while (mb->un.varDmp.word_cnt && offset < DMP_VPD_SIZE);
209 lpfc_parse_vpd(phba, lpfc_vpd_data, offset);
211 kfree(lpfc_vpd_data);
213 mempool_free(pmb, phba->mbox_mem_pool);
218 * lpfc_config_async_cmpl: Completion handler for config async event mbox cmd.
219 * @phba: pointer to lpfc hba data structure.
220 * @pmboxq: pointer to the driver internal queue element for mailbox command.
222 * This is the completion handler for driver's configuring asynchronous event
223 * mailbox command to the device. If the mailbox command returns successfully,
224 * it will set internal async event support flag to 1; otherwise, it will
225 * set internal async event support flag to 0.
228 lpfc_config_async_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
230 if (pmboxq->mb.mbxStatus == MBX_SUCCESS)
231 phba->temp_sensor_support = 1;
233 phba->temp_sensor_support = 0;
234 mempool_free(pmboxq, phba->mbox_mem_pool);
239 * lpfc_config_port_post: Perform lpfc initialization after config port.
240 * @phba: pointer to lpfc hba data structure.
242 * This routine will do LPFC initialization after the CONFIG_PORT mailbox
243 * command call. It performs all internal resource and state setups on the
244 * port: post IOCB buffers, enable appropriate host interrupt attentions,
245 * ELS ring timers, etc.
249 * Any other value - error.
252 lpfc_config_port_post(struct lpfc_hba *phba)
254 struct lpfc_vport *vport = phba->pport;
257 struct lpfc_dmabuf *mp;
258 struct lpfc_sli *psli = &phba->sli;
259 uint32_t status, timeout;
263 spin_lock_irq(&phba->hbalock);
265 * If the Config port completed correctly the HBA is not
266 * over heated any more.
268 if (phba->over_temp_state == HBA_OVER_TEMP)
269 phba->over_temp_state = HBA_NORMAL_TEMP;
270 spin_unlock_irq(&phba->hbalock);
272 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
274 phba->link_state = LPFC_HBA_ERROR;
279 /* Get login parameters for NID. */
280 lpfc_read_sparam(phba, pmb, 0);
282 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
283 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
284 "0448 Adapter failed init, mbxCmd x%x "
285 "READ_SPARM mbxStatus x%x\n",
286 mb->mbxCommand, mb->mbxStatus);
287 phba->link_state = LPFC_HBA_ERROR;
288 mp = (struct lpfc_dmabuf *) pmb->context1;
289 mempool_free( pmb, phba->mbox_mem_pool);
290 lpfc_mbuf_free(phba, mp->virt, mp->phys);
295 mp = (struct lpfc_dmabuf *) pmb->context1;
297 memcpy(&vport->fc_sparam, mp->virt, sizeof (struct serv_parm));
298 lpfc_mbuf_free(phba, mp->virt, mp->phys);
300 pmb->context1 = NULL;
302 if (phba->cfg_soft_wwnn)
303 u64_to_wwn(phba->cfg_soft_wwnn,
304 vport->fc_sparam.nodeName.u.wwn);
305 if (phba->cfg_soft_wwpn)
306 u64_to_wwn(phba->cfg_soft_wwpn,
307 vport->fc_sparam.portName.u.wwn);
308 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
309 sizeof (struct lpfc_name));
310 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
311 sizeof (struct lpfc_name));
312 /* If no serial number in VPD data, use low 6 bytes of WWNN */
313 /* This should be consolidated into parse_vpd ? - mr */
314 if (phba->SerialNumber[0] == 0) {
317 outptr = &vport->fc_nodename.u.s.IEEE[0];
318 for (i = 0; i < 12; i++) {
320 j = ((status & 0xf0) >> 4);
322 phba->SerialNumber[i] =
323 (char)((uint8_t) 0x30 + (uint8_t) j);
325 phba->SerialNumber[i] =
326 (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
330 phba->SerialNumber[i] =
331 (char)((uint8_t) 0x30 + (uint8_t) j);
333 phba->SerialNumber[i] =
334 (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
338 lpfc_read_config(phba, pmb);
340 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
341 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
342 "0453 Adapter failed to init, mbxCmd x%x "
343 "READ_CONFIG, mbxStatus x%x\n",
344 mb->mbxCommand, mb->mbxStatus);
345 phba->link_state = LPFC_HBA_ERROR;
346 mempool_free( pmb, phba->mbox_mem_pool);
350 /* Reset the DFT_HBA_Q_DEPTH to the max xri */
351 if (phba->cfg_hba_queue_depth > (mb->un.varRdConfig.max_xri+1))
352 phba->cfg_hba_queue_depth =
353 mb->un.varRdConfig.max_xri + 1;
355 phba->lmt = mb->un.varRdConfig.lmt;
357 /* Get the default values for Model Name and Description */
358 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
360 if ((phba->cfg_link_speed > LINK_SPEED_10G)
361 || ((phba->cfg_link_speed == LINK_SPEED_1G)
362 && !(phba->lmt & LMT_1Gb))
363 || ((phba->cfg_link_speed == LINK_SPEED_2G)
364 && !(phba->lmt & LMT_2Gb))
365 || ((phba->cfg_link_speed == LINK_SPEED_4G)
366 && !(phba->lmt & LMT_4Gb))
367 || ((phba->cfg_link_speed == LINK_SPEED_8G)
368 && !(phba->lmt & LMT_8Gb))
369 || ((phba->cfg_link_speed == LINK_SPEED_10G)
370 && !(phba->lmt & LMT_10Gb))) {
371 /* Reset link speed to auto */
372 lpfc_printf_log(phba, KERN_WARNING, LOG_LINK_EVENT,
373 "1302 Invalid speed for this board: "
374 "Reset link speed to auto: x%x\n",
375 phba->cfg_link_speed);
376 phba->cfg_link_speed = LINK_SPEED_AUTO;
379 phba->link_state = LPFC_LINK_DOWN;
381 /* Only process IOCBs on ELS ring till hba_state is READY */
382 if (psli->ring[psli->extra_ring].cmdringaddr)
383 psli->ring[psli->extra_ring].flag |= LPFC_STOP_IOCB_EVENT;
384 if (psli->ring[psli->fcp_ring].cmdringaddr)
385 psli->ring[psli->fcp_ring].flag |= LPFC_STOP_IOCB_EVENT;
386 if (psli->ring[psli->next_ring].cmdringaddr)
387 psli->ring[psli->next_ring].flag |= LPFC_STOP_IOCB_EVENT;
389 /* Post receive buffers for desired rings */
390 if (phba->sli_rev != 3)
391 lpfc_post_rcv_buf(phba);
394 * Configure HBA MSI-X attention conditions to messages if MSI-X mode
396 if (phba->intr_type == MSIX) {
397 rc = lpfc_config_msi(phba, pmb);
399 mempool_free(pmb, phba->mbox_mem_pool);
402 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
403 if (rc != MBX_SUCCESS) {
404 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
405 "0352 Config MSI mailbox command "
406 "failed, mbxCmd x%x, mbxStatus x%x\n",
407 pmb->mb.mbxCommand, pmb->mb.mbxStatus);
408 mempool_free(pmb, phba->mbox_mem_pool);
413 /* Initialize ERATT handling flag */
414 phba->hba_flag &= ~HBA_ERATT_HANDLED;
416 /* Enable appropriate host interrupts */
417 spin_lock_irq(&phba->hbalock);
418 status = readl(phba->HCregaddr);
419 status |= HC_MBINT_ENA | HC_ERINT_ENA | HC_LAINT_ENA;
420 if (psli->num_rings > 0)
421 status |= HC_R0INT_ENA;
422 if (psli->num_rings > 1)
423 status |= HC_R1INT_ENA;
424 if (psli->num_rings > 2)
425 status |= HC_R2INT_ENA;
426 if (psli->num_rings > 3)
427 status |= HC_R3INT_ENA;
429 if ((phba->cfg_poll & ENABLE_FCP_RING_POLLING) &&
430 (phba->cfg_poll & DISABLE_FCP_RING_INT))
431 status &= ~(HC_R0INT_ENA);
433 writel(status, phba->HCregaddr);
434 readl(phba->HCregaddr); /* flush */
435 spin_unlock_irq(&phba->hbalock);
437 /* Set up ring-0 (ELS) timer */
438 timeout = phba->fc_ratov * 2;
439 mod_timer(&vport->els_tmofunc, jiffies + HZ * timeout);
440 /* Set up heart beat (HB) timer */
441 mod_timer(&phba->hb_tmofunc, jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
442 phba->hb_outstanding = 0;
443 phba->last_completion_time = jiffies;
444 /* Set up error attention (ERATT) polling timer */
445 mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
447 lpfc_init_link(phba, pmb, phba->cfg_topology, phba->cfg_link_speed);
448 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
449 lpfc_set_loopback_flag(phba);
450 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
451 if (rc != MBX_SUCCESS) {
452 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
453 "0454 Adapter failed to init, mbxCmd x%x "
454 "INIT_LINK, mbxStatus x%x\n",
455 mb->mbxCommand, mb->mbxStatus);
457 /* Clear all interrupt enable conditions */
458 writel(0, phba->HCregaddr);
459 readl(phba->HCregaddr); /* flush */
460 /* Clear all pending interrupts */
461 writel(0xffffffff, phba->HAregaddr);
462 readl(phba->HAregaddr); /* flush */
464 phba->link_state = LPFC_HBA_ERROR;
466 mempool_free(pmb, phba->mbox_mem_pool);
469 /* MBOX buffer will be freed in mbox compl */
470 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
471 lpfc_config_async(phba, pmb, LPFC_ELS_RING);
472 pmb->mbox_cmpl = lpfc_config_async_cmpl;
473 pmb->vport = phba->pport;
474 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
476 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
477 lpfc_printf_log(phba,
480 "0456 Adapter failed to issue "
481 "ASYNCEVT_ENABLE mbox status x%x \n.",
483 mempool_free(pmb, phba->mbox_mem_pool);
489 * lpfc_hba_down_prep: Perform lpfc uninitialization prior to HBA reset.
490 * @phba: pointer to lpfc HBA data structure.
492 * This routine will do LPFC uninitialization before the HBA is reset when
493 * bringing down the SLI Layer.
497 * Any other value - error.
500 lpfc_hba_down_prep(struct lpfc_hba *phba)
502 struct lpfc_vport **vports;
504 /* Disable interrupts */
505 writel(0, phba->HCregaddr);
506 readl(phba->HCregaddr); /* flush */
508 if (phba->pport->load_flag & FC_UNLOADING)
509 lpfc_cleanup_discovery_resources(phba->pport);
511 vports = lpfc_create_vport_work_array(phba);
513 for(i = 0; i <= phba->max_vpi && vports[i] != NULL; i++)
514 lpfc_cleanup_discovery_resources(vports[i]);
515 lpfc_destroy_vport_work_array(phba, vports);
521 * lpfc_hba_down_post: Perform lpfc uninitialization after HBA reset.
522 * @phba: pointer to lpfc HBA data structure.
524 * This routine will do uninitialization after the HBA is reset when bring
525 * down the SLI Layer.
529 * Any other value - error.
532 lpfc_hba_down_post(struct lpfc_hba *phba)
534 struct lpfc_sli *psli = &phba->sli;
535 struct lpfc_sli_ring *pring;
536 struct lpfc_dmabuf *mp, *next_mp;
537 struct lpfc_iocbq *iocb;
539 LIST_HEAD(completions);
542 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)
543 lpfc_sli_hbqbuf_free_all(phba);
545 /* Cleanup preposted buffers on the ELS ring */
546 pring = &psli->ring[LPFC_ELS_RING];
547 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
549 pring->postbufq_cnt--;
550 lpfc_mbuf_free(phba, mp->virt, mp->phys);
555 spin_lock_irq(&phba->hbalock);
556 for (i = 0; i < psli->num_rings; i++) {
557 pring = &psli->ring[i];
559 /* At this point in time the HBA is either reset or DOA. Either
560 * way, nothing should be on txcmplq as it will NEVER complete.
562 list_splice_init(&pring->txcmplq, &completions);
563 pring->txcmplq_cnt = 0;
564 spin_unlock_irq(&phba->hbalock);
566 while (!list_empty(&completions)) {
567 iocb = list_get_first(&completions, struct lpfc_iocbq,
570 list_del_init(&iocb->list);
572 if (!iocb->iocb_cmpl)
573 lpfc_sli_release_iocbq(phba, iocb);
575 cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
576 cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
577 (iocb->iocb_cmpl) (phba, iocb, iocb);
581 lpfc_sli_abort_iocb_ring(phba, pring);
582 spin_lock_irq(&phba->hbalock);
584 spin_unlock_irq(&phba->hbalock);
590 * lpfc_hb_timeout: The HBA-timer timeout handler.
591 * @ptr: unsigned long holds the pointer to lpfc hba data structure.
593 * This is the HBA-timer timeout handler registered to the lpfc driver. When
594 * this timer fires, a HBA timeout event shall be posted to the lpfc driver
595 * work-port-events bitmap and the worker thread is notified. This timeout
596 * event will be used by the worker thread to invoke the actual timeout
597 * handler routine, lpfc_hb_timeout_handler. Any periodical operations will
598 * be performed in the timeout handler and the HBA timeout event bit shall
599 * be cleared by the worker thread after it has taken the event bitmap out.
602 lpfc_hb_timeout(unsigned long ptr)
604 struct lpfc_hba *phba;
608 phba = (struct lpfc_hba *)ptr;
610 /* Check for heart beat timeout conditions */
611 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
612 tmo_posted = phba->pport->work_port_events & WORKER_HB_TMO;
614 phba->pport->work_port_events |= WORKER_HB_TMO;
615 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
617 /* Tell the worker thread there is work to do */
619 lpfc_worker_wake_up(phba);
624 * lpfc_hb_mbox_cmpl: The lpfc heart-beat mailbox command callback function.
625 * @phba: pointer to lpfc hba data structure.
626 * @pmboxq: pointer to the driver internal queue element for mailbox command.
628 * This is the callback function to the lpfc heart-beat mailbox command.
629 * If configured, the lpfc driver issues the heart-beat mailbox command to
630 * the HBA every LPFC_HB_MBOX_INTERVAL (current 5) seconds. At the time the
631 * heart-beat mailbox command is issued, the driver shall set up heart-beat
632 * timeout timer to LPFC_HB_MBOX_TIMEOUT (current 30) seconds and marks
633 * heart-beat outstanding state. Once the mailbox command comes back and
634 * no error conditions detected, the heart-beat mailbox command timer is
635 * reset to LPFC_HB_MBOX_INTERVAL seconds and the heart-beat outstanding
636 * state is cleared for the next heart-beat. If the timer expired with the
637 * heart-beat outstanding state set, the driver will put the HBA offline.
640 lpfc_hb_mbox_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
642 unsigned long drvr_flag;
644 spin_lock_irqsave(&phba->hbalock, drvr_flag);
645 phba->hb_outstanding = 0;
646 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
648 /* Check and reset heart-beat timer is necessary */
649 mempool_free(pmboxq, phba->mbox_mem_pool);
650 if (!(phba->pport->fc_flag & FC_OFFLINE_MODE) &&
651 !(phba->link_state == LPFC_HBA_ERROR) &&
652 !(phba->pport->load_flag & FC_UNLOADING))
653 mod_timer(&phba->hb_tmofunc,
654 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
659 * lpfc_hb_timeout_handler: The HBA-timer timeout handler.
660 * @phba: pointer to lpfc hba data structure.
662 * This is the actual HBA-timer timeout handler to be invoked by the worker
663 * thread whenever the HBA timer fired and HBA-timeout event posted. This
664 * handler performs any periodic operations needed for the device. If such
665 * periodic event has already been attended to either in the interrupt handler
666 * or by processing slow-ring or fast-ring events within the HBA-timer
667 * timeout window (LPFC_HB_MBOX_INTERVAL), this handler just simply resets
668 * the timer for the next timeout period. If lpfc heart-beat mailbox command
669 * is configured and there is no heart-beat mailbox command outstanding, a
670 * heart-beat mailbox is issued and timer set properly. Otherwise, if there
671 * has been a heart-beat mailbox command outstanding, the HBA shall be put
675 lpfc_hb_timeout_handler(struct lpfc_hba *phba)
677 LPFC_MBOXQ_t *pmboxq;
678 struct lpfc_dmabuf *buf_ptr;
680 struct lpfc_sli *psli = &phba->sli;
681 LIST_HEAD(completions);
683 if ((phba->link_state == LPFC_HBA_ERROR) ||
684 (phba->pport->load_flag & FC_UNLOADING) ||
685 (phba->pport->fc_flag & FC_OFFLINE_MODE))
688 spin_lock_irq(&phba->pport->work_port_lock);
689 /* If the timer is already canceled do nothing */
690 if (!(phba->pport->work_port_events & WORKER_HB_TMO)) {
691 spin_unlock_irq(&phba->pport->work_port_lock);
695 if (time_after(phba->last_completion_time + LPFC_HB_MBOX_INTERVAL * HZ,
697 spin_unlock_irq(&phba->pport->work_port_lock);
698 if (!phba->hb_outstanding)
699 mod_timer(&phba->hb_tmofunc,
700 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
702 mod_timer(&phba->hb_tmofunc,
703 jiffies + HZ * LPFC_HB_MBOX_TIMEOUT);
706 spin_unlock_irq(&phba->pport->work_port_lock);
708 if (phba->elsbuf_cnt &&
709 (phba->elsbuf_cnt == phba->elsbuf_prev_cnt)) {
710 spin_lock_irq(&phba->hbalock);
711 list_splice_init(&phba->elsbuf, &completions);
712 phba->elsbuf_cnt = 0;
713 phba->elsbuf_prev_cnt = 0;
714 spin_unlock_irq(&phba->hbalock);
716 while (!list_empty(&completions)) {
717 list_remove_head(&completions, buf_ptr,
718 struct lpfc_dmabuf, list);
719 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
723 phba->elsbuf_prev_cnt = phba->elsbuf_cnt;
725 /* If there is no heart beat outstanding, issue a heartbeat command */
726 if (phba->cfg_enable_hba_heartbeat) {
727 if (!phba->hb_outstanding) {
728 pmboxq = mempool_alloc(phba->mbox_mem_pool,GFP_KERNEL);
730 mod_timer(&phba->hb_tmofunc,
731 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
735 lpfc_heart_beat(phba, pmboxq);
736 pmboxq->mbox_cmpl = lpfc_hb_mbox_cmpl;
737 pmboxq->vport = phba->pport;
738 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
740 if (retval != MBX_BUSY && retval != MBX_SUCCESS) {
741 mempool_free(pmboxq, phba->mbox_mem_pool);
742 mod_timer(&phba->hb_tmofunc,
743 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
746 mod_timer(&phba->hb_tmofunc,
747 jiffies + HZ * LPFC_HB_MBOX_TIMEOUT);
748 phba->hb_outstanding = 1;
752 * If heart beat timeout called with hb_outstanding set
753 * we need to take the HBA offline.
755 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
756 "0459 Adapter heartbeat failure, "
757 "taking this port offline.\n");
759 spin_lock_irq(&phba->hbalock);
760 psli->sli_flag &= ~LPFC_SLI2_ACTIVE;
761 spin_unlock_irq(&phba->hbalock);
763 lpfc_offline_prep(phba);
765 lpfc_unblock_mgmt_io(phba);
766 phba->link_state = LPFC_HBA_ERROR;
767 lpfc_hba_down_post(phba);
773 * lpfc_offline_eratt: Bring lpfc offline on hardware error attention.
774 * @phba: pointer to lpfc hba data structure.
776 * This routine is called to bring the HBA offline when HBA hardware error
777 * other than Port Error 6 has been detected.
780 lpfc_offline_eratt(struct lpfc_hba *phba)
782 struct lpfc_sli *psli = &phba->sli;
784 spin_lock_irq(&phba->hbalock);
785 psli->sli_flag &= ~LPFC_SLI2_ACTIVE;
786 spin_unlock_irq(&phba->hbalock);
787 lpfc_offline_prep(phba);
790 lpfc_reset_barrier(phba);
791 lpfc_sli_brdreset(phba);
792 lpfc_hba_down_post(phba);
793 lpfc_sli_brdready(phba, HS_MBRDY);
794 lpfc_unblock_mgmt_io(phba);
795 phba->link_state = LPFC_HBA_ERROR;
800 * lpfc_handle_eratt: The HBA hardware error handler.
801 * @phba: pointer to lpfc hba data structure.
803 * This routine is invoked to handle the following HBA hardware error
805 * 1 - HBA error attention interrupt
806 * 2 - DMA ring index out of range
807 * 3 - Mailbox command came back as unknown
810 lpfc_handle_eratt(struct lpfc_hba *phba)
812 struct lpfc_vport *vport = phba->pport;
813 struct lpfc_sli *psli = &phba->sli;
814 struct lpfc_sli_ring *pring;
816 unsigned long temperature;
817 struct temp_event temp_event_data;
818 struct Scsi_Host *shost;
819 struct lpfc_board_event_header board_event;
821 /* If the pci channel is offline, ignore possible errors,
822 * since we cannot communicate with the pci card anyway. */
823 if (pci_channel_offline(phba->pcidev))
825 /* If resets are disabled then leave the HBA alone and return */
826 if (!phba->cfg_enable_hba_reset)
829 /* Send an internal error event to mgmt application */
830 board_event.event_type = FC_REG_BOARD_EVENT;
831 board_event.subcategory = LPFC_EVENT_PORTINTERR;
832 shost = lpfc_shost_from_vport(phba->pport);
833 fc_host_post_vendor_event(shost, fc_get_event_number(),
835 (char *) &board_event,
838 if (phba->work_hs & HS_FFER6) {
839 /* Re-establishing Link */
840 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
841 "1301 Re-establishing Link "
842 "Data: x%x x%x x%x\n",
844 phba->work_status[0], phba->work_status[1]);
846 spin_lock_irq(&phba->hbalock);
847 psli->sli_flag &= ~LPFC_SLI2_ACTIVE;
848 spin_unlock_irq(&phba->hbalock);
851 * Firmware stops when it triggled erratt with HS_FFER6.
852 * That could cause the I/Os dropped by the firmware.
853 * Error iocb (I/O) on txcmplq and let the SCSI layer
854 * retry it after re-establishing link.
856 pring = &psli->ring[psli->fcp_ring];
857 lpfc_sli_abort_iocb_ring(phba, pring);
860 * There was a firmware error. Take the hba offline and then
861 * attempt to restart it.
863 lpfc_offline_prep(phba);
865 lpfc_sli_brdrestart(phba);
866 if (lpfc_online(phba) == 0) { /* Initialize the HBA */
867 lpfc_unblock_mgmt_io(phba);
870 lpfc_unblock_mgmt_io(phba);
871 } else if (phba->work_hs & HS_CRIT_TEMP) {
872 temperature = readl(phba->MBslimaddr + TEMPERATURE_OFFSET);
873 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
874 temp_event_data.event_code = LPFC_CRIT_TEMP;
875 temp_event_data.data = (uint32_t)temperature;
877 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
878 "0406 Adapter maximum temperature exceeded "
879 "(%ld), taking this port offline "
880 "Data: x%x x%x x%x\n",
881 temperature, phba->work_hs,
882 phba->work_status[0], phba->work_status[1]);
884 shost = lpfc_shost_from_vport(phba->pport);
885 fc_host_post_vendor_event(shost, fc_get_event_number(),
886 sizeof(temp_event_data),
887 (char *) &temp_event_data,
889 | PCI_VENDOR_ID_EMULEX);
891 spin_lock_irq(&phba->hbalock);
892 phba->over_temp_state = HBA_OVER_TEMP;
893 spin_unlock_irq(&phba->hbalock);
894 lpfc_offline_eratt(phba);
897 /* The if clause above forces this code path when the status
898 * failure is a value other than FFER6. Do not call the offline
899 * twice. This is the adapter hardware error path.
901 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
902 "0457 Adapter Hardware Error "
903 "Data: x%x x%x x%x\n",
905 phba->work_status[0], phba->work_status[1]);
907 event_data = FC_REG_DUMP_EVENT;
908 shost = lpfc_shost_from_vport(vport);
909 fc_host_post_vendor_event(shost, fc_get_event_number(),
910 sizeof(event_data), (char *) &event_data,
911 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
913 lpfc_offline_eratt(phba);
919 * lpfc_handle_latt: The HBA link event handler.
920 * @phba: pointer to lpfc hba data structure.
922 * This routine is invoked from the worker thread to handle a HBA host
923 * attention link event.
926 lpfc_handle_latt(struct lpfc_hba *phba)
928 struct lpfc_vport *vport = phba->pport;
929 struct lpfc_sli *psli = &phba->sli;
931 volatile uint32_t control;
932 struct lpfc_dmabuf *mp;
935 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
938 goto lpfc_handle_latt_err_exit;
941 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
944 goto lpfc_handle_latt_free_pmb;
947 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
950 goto lpfc_handle_latt_free_mp;
953 /* Cleanup any outstanding ELS commands */
954 lpfc_els_flush_all_cmd(phba);
956 psli->slistat.link_event++;
957 lpfc_read_la(phba, pmb, mp);
958 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_la;
960 /* Block ELS IOCBs until we have processed this mbox command */
961 phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
962 rc = lpfc_sli_issue_mbox (phba, pmb, MBX_NOWAIT);
963 if (rc == MBX_NOT_FINISHED) {
965 goto lpfc_handle_latt_free_mbuf;
968 /* Clear Link Attention in HA REG */
969 spin_lock_irq(&phba->hbalock);
970 writel(HA_LATT, phba->HAregaddr);
971 readl(phba->HAregaddr); /* flush */
972 spin_unlock_irq(&phba->hbalock);
976 lpfc_handle_latt_free_mbuf:
977 phba->sli.ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
978 lpfc_mbuf_free(phba, mp->virt, mp->phys);
979 lpfc_handle_latt_free_mp:
981 lpfc_handle_latt_free_pmb:
982 mempool_free(pmb, phba->mbox_mem_pool);
983 lpfc_handle_latt_err_exit:
984 /* Enable Link attention interrupts */
985 spin_lock_irq(&phba->hbalock);
986 psli->sli_flag |= LPFC_PROCESS_LA;
987 control = readl(phba->HCregaddr);
988 control |= HC_LAINT_ENA;
989 writel(control, phba->HCregaddr);
990 readl(phba->HCregaddr); /* flush */
992 /* Clear Link Attention in HA REG */
993 writel(HA_LATT, phba->HAregaddr);
994 readl(phba->HAregaddr); /* flush */
995 spin_unlock_irq(&phba->hbalock);
997 phba->link_state = LPFC_HBA_ERROR;
999 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
1000 "0300 LATT: Cannot issue READ_LA: Data:%d\n", rc);
1006 * lpfc_parse_vpd: Parse VPD (Vital Product Data).
1007 * @phba: pointer to lpfc hba data structure.
1008 * @vpd: pointer to the vital product data.
1009 * @len: length of the vital product data in bytes.
1011 * This routine parses the Vital Product Data (VPD). The VPD is treated as
1012 * an array of characters. In this routine, the ModelName, ProgramType, and
1013 * ModelDesc, etc. fields of the phba data structure will be populated.
1016 * 0 - pointer to the VPD passed in is NULL
1020 lpfc_parse_vpd(struct lpfc_hba *phba, uint8_t *vpd, int len)
1022 uint8_t lenlo, lenhi;
1032 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
1033 "0455 Vital Product Data: x%x x%x x%x x%x\n",
1034 (uint32_t) vpd[0], (uint32_t) vpd[1], (uint32_t) vpd[2],
1036 while (!finished && (index < (len - 4))) {
1037 switch (vpd[index]) {
1045 i = ((((unsigned short)lenhi) << 8) + lenlo);
1054 Length = ((((unsigned short)lenhi) << 8) + lenlo);
1055 if (Length > len - index)
1056 Length = len - index;
1057 while (Length > 0) {
1058 /* Look for Serial Number */
1059 if ((vpd[index] == 'S') && (vpd[index+1] == 'N')) {
1066 phba->SerialNumber[j++] = vpd[index++];
1070 phba->SerialNumber[j] = 0;
1073 else if ((vpd[index] == 'V') && (vpd[index+1] == '1')) {
1074 phba->vpd_flag |= VPD_MODEL_DESC;
1081 phba->ModelDesc[j++] = vpd[index++];
1085 phba->ModelDesc[j] = 0;
1088 else if ((vpd[index] == 'V') && (vpd[index+1] == '2')) {
1089 phba->vpd_flag |= VPD_MODEL_NAME;
1096 phba->ModelName[j++] = vpd[index++];
1100 phba->ModelName[j] = 0;
1103 else if ((vpd[index] == 'V') && (vpd[index+1] == '3')) {
1104 phba->vpd_flag |= VPD_PROGRAM_TYPE;
1111 phba->ProgramType[j++] = vpd[index++];
1115 phba->ProgramType[j] = 0;
1118 else if ((vpd[index] == 'V') && (vpd[index+1] == '4')) {
1119 phba->vpd_flag |= VPD_PORT;
1126 phba->Port[j++] = vpd[index++];
1156 * lpfc_get_hba_model_desc: Retrieve HBA device model name and description.
1157 * @phba: pointer to lpfc hba data structure.
1158 * @mdp: pointer to the data structure to hold the derived model name.
1159 * @descp: pointer to the data structure to hold the derived description.
1161 * This routine retrieves HBA's description based on its registered PCI device
1162 * ID. The @descp passed into this function points to an array of 256 chars. It
1163 * shall be returned with the model name, maximum speed, and the host bus type.
1164 * The @mdp passed into this function points to an array of 80 chars. When the
1165 * function returns, the @mdp will be filled with the model name.
1168 lpfc_get_hba_model_desc(struct lpfc_hba *phba, uint8_t *mdp, uint8_t *descp)
1171 uint16_t dev_id = phba->pcidev->device;
1178 } m = {"<Unknown>", 0, ""};
1180 if (mdp && mdp[0] != '\0'
1181 && descp && descp[0] != '\0')
1184 if (phba->lmt & LMT_10Gb)
1186 else if (phba->lmt & LMT_8Gb)
1188 else if (phba->lmt & LMT_4Gb)
1190 else if (phba->lmt & LMT_2Gb)
1198 case PCI_DEVICE_ID_FIREFLY:
1199 m = (typeof(m)){"LP6000", max_speed, "PCI"};
1201 case PCI_DEVICE_ID_SUPERFLY:
1202 if (vp->rev.biuRev >= 1 && vp->rev.biuRev <= 3)
1203 m = (typeof(m)){"LP7000", max_speed, "PCI"};
1205 m = (typeof(m)){"LP7000E", max_speed, "PCI"};
1207 case PCI_DEVICE_ID_DRAGONFLY:
1208 m = (typeof(m)){"LP8000", max_speed, "PCI"};
1210 case PCI_DEVICE_ID_CENTAUR:
1211 if (FC_JEDEC_ID(vp->rev.biuRev) == CENTAUR_2G_JEDEC_ID)
1212 m = (typeof(m)){"LP9002", max_speed, "PCI"};
1214 m = (typeof(m)){"LP9000", max_speed, "PCI"};
1216 case PCI_DEVICE_ID_RFLY:
1217 m = (typeof(m)){"LP952", max_speed, "PCI"};
1219 case PCI_DEVICE_ID_PEGASUS:
1220 m = (typeof(m)){"LP9802", max_speed, "PCI-X"};
1222 case PCI_DEVICE_ID_THOR:
1223 m = (typeof(m)){"LP10000", max_speed, "PCI-X"};
1225 case PCI_DEVICE_ID_VIPER:
1226 m = (typeof(m)){"LPX1000", max_speed, "PCI-X"};
1228 case PCI_DEVICE_ID_PFLY:
1229 m = (typeof(m)){"LP982", max_speed, "PCI-X"};
1231 case PCI_DEVICE_ID_TFLY:
1232 m = (typeof(m)){"LP1050", max_speed, "PCI-X"};
1234 case PCI_DEVICE_ID_HELIOS:
1235 m = (typeof(m)){"LP11000", max_speed, "PCI-X2"};
1237 case PCI_DEVICE_ID_HELIOS_SCSP:
1238 m = (typeof(m)){"LP11000-SP", max_speed, "PCI-X2"};
1240 case PCI_DEVICE_ID_HELIOS_DCSP:
1241 m = (typeof(m)){"LP11002-SP", max_speed, "PCI-X2"};
1243 case PCI_DEVICE_ID_NEPTUNE:
1244 m = (typeof(m)){"LPe1000", max_speed, "PCIe"};
1246 case PCI_DEVICE_ID_NEPTUNE_SCSP:
1247 m = (typeof(m)){"LPe1000-SP", max_speed, "PCIe"};
1249 case PCI_DEVICE_ID_NEPTUNE_DCSP:
1250 m = (typeof(m)){"LPe1002-SP", max_speed, "PCIe"};
1252 case PCI_DEVICE_ID_BMID:
1253 m = (typeof(m)){"LP1150", max_speed, "PCI-X2"};
1255 case PCI_DEVICE_ID_BSMB:
1256 m = (typeof(m)){"LP111", max_speed, "PCI-X2"};
1258 case PCI_DEVICE_ID_ZEPHYR:
1259 m = (typeof(m)){"LPe11000", max_speed, "PCIe"};
1261 case PCI_DEVICE_ID_ZEPHYR_SCSP:
1262 m = (typeof(m)){"LPe11000", max_speed, "PCIe"};
1264 case PCI_DEVICE_ID_ZEPHYR_DCSP:
1265 m = (typeof(m)){"LPe11002-SP", max_speed, "PCIe"};
1267 case PCI_DEVICE_ID_ZMID:
1268 m = (typeof(m)){"LPe1150", max_speed, "PCIe"};
1270 case PCI_DEVICE_ID_ZSMB:
1271 m = (typeof(m)){"LPe111", max_speed, "PCIe"};
1273 case PCI_DEVICE_ID_LP101:
1274 m = (typeof(m)){"LP101", max_speed, "PCI-X"};
1276 case PCI_DEVICE_ID_LP10000S:
1277 m = (typeof(m)){"LP10000-S", max_speed, "PCI"};
1279 case PCI_DEVICE_ID_LP11000S:
1280 m = (typeof(m)){"LP11000-S", max_speed,
1283 case PCI_DEVICE_ID_LPE11000S:
1284 m = (typeof(m)){"LPe11000-S", max_speed,
1287 case PCI_DEVICE_ID_SAT:
1288 m = (typeof(m)){"LPe12000", max_speed, "PCIe"};
1290 case PCI_DEVICE_ID_SAT_MID:
1291 m = (typeof(m)){"LPe1250", max_speed, "PCIe"};
1293 case PCI_DEVICE_ID_SAT_SMB:
1294 m = (typeof(m)){"LPe121", max_speed, "PCIe"};
1296 case PCI_DEVICE_ID_SAT_DCSP:
1297 m = (typeof(m)){"LPe12002-SP", max_speed, "PCIe"};
1299 case PCI_DEVICE_ID_SAT_SCSP:
1300 m = (typeof(m)){"LPe12000-SP", max_speed, "PCIe"};
1302 case PCI_DEVICE_ID_SAT_S:
1303 m = (typeof(m)){"LPe12000-S", max_speed, "PCIe"};
1305 case PCI_DEVICE_ID_HORNET:
1306 m = (typeof(m)){"LP21000", max_speed, "PCIe"};
1309 case PCI_DEVICE_ID_PROTEUS_VF:
1310 m = (typeof(m)) {"LPev12000", max_speed, "PCIe IOV"};
1312 case PCI_DEVICE_ID_PROTEUS_PF:
1313 m = (typeof(m)) {"LPev12000", max_speed, "PCIe IOV"};
1315 case PCI_DEVICE_ID_PROTEUS_S:
1316 m = (typeof(m)) {"LPemv12002-S", max_speed, "PCIe IOV"};
1319 m = (typeof(m)){ NULL };
1323 if (mdp && mdp[0] == '\0')
1324 snprintf(mdp, 79,"%s", m.name);
1325 if (descp && descp[0] == '\0')
1326 snprintf(descp, 255,
1327 "Emulex %s %d%s %s %s",
1328 m.name, m.max_speed,
1331 (GE) ? "FCoE Adapter" : "Fibre Channel Adapter");
1335 * lpfc_post_buffer: Post IOCB(s) with DMA buffer descriptor(s) to a IOCB ring.
1336 * @phba: pointer to lpfc hba data structure.
1337 * @pring: pointer to a IOCB ring.
1338 * @cnt: the number of IOCBs to be posted to the IOCB ring.
1340 * This routine posts a given number of IOCBs with the associated DMA buffer
1341 * descriptors specified by the cnt argument to the given IOCB ring.
1344 * The number of IOCBs NOT able to be posted to the IOCB ring.
1347 lpfc_post_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring, int cnt)
1350 struct lpfc_iocbq *iocb;
1351 struct lpfc_dmabuf *mp1, *mp2;
1353 cnt += pring->missbufcnt;
1355 /* While there are buffers to post */
1357 /* Allocate buffer for command iocb */
1358 iocb = lpfc_sli_get_iocbq(phba);
1360 pring->missbufcnt = cnt;
1365 /* 2 buffers can be posted per command */
1366 /* Allocate buffer to post */
1367 mp1 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
1369 mp1->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &mp1->phys);
1370 if (!mp1 || !mp1->virt) {
1372 lpfc_sli_release_iocbq(phba, iocb);
1373 pring->missbufcnt = cnt;
1377 INIT_LIST_HEAD(&mp1->list);
1378 /* Allocate buffer to post */
1380 mp2 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
1382 mp2->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
1384 if (!mp2 || !mp2->virt) {
1386 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
1388 lpfc_sli_release_iocbq(phba, iocb);
1389 pring->missbufcnt = cnt;
1393 INIT_LIST_HEAD(&mp2->list);
1398 icmd->un.cont64[0].addrHigh = putPaddrHigh(mp1->phys);
1399 icmd->un.cont64[0].addrLow = putPaddrLow(mp1->phys);
1400 icmd->un.cont64[0].tus.f.bdeSize = FCELSSIZE;
1401 icmd->ulpBdeCount = 1;
1404 icmd->un.cont64[1].addrHigh = putPaddrHigh(mp2->phys);
1405 icmd->un.cont64[1].addrLow = putPaddrLow(mp2->phys);
1406 icmd->un.cont64[1].tus.f.bdeSize = FCELSSIZE;
1408 icmd->ulpBdeCount = 2;
1411 icmd->ulpCommand = CMD_QUE_RING_BUF64_CN;
1414 if (lpfc_sli_issue_iocb(phba, pring, iocb, 0) == IOCB_ERROR) {
1415 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
1419 lpfc_mbuf_free(phba, mp2->virt, mp2->phys);
1423 lpfc_sli_release_iocbq(phba, iocb);
1424 pring->missbufcnt = cnt;
1427 lpfc_sli_ringpostbuf_put(phba, pring, mp1);
1429 lpfc_sli_ringpostbuf_put(phba, pring, mp2);
1431 pring->missbufcnt = 0;
1436 * lpfc_post_rcv_buf: Post the initial receive IOCB buffers to ELS ring.
1437 * @phba: pointer to lpfc hba data structure.
1439 * This routine posts initial receive IOCB buffers to the ELS ring. The
1440 * current number of initial IOCB buffers specified by LPFC_BUF_RING0 is
1444 * 0 - success (currently always success)
1447 lpfc_post_rcv_buf(struct lpfc_hba *phba)
1449 struct lpfc_sli *psli = &phba->sli;
1451 /* Ring 0, ELS / CT buffers */
1452 lpfc_post_buffer(phba, &psli->ring[LPFC_ELS_RING], LPFC_BUF_RING0);
1453 /* Ring 2 - FCP no buffers needed */
1458 #define S(N,V) (((V)<<(N))|((V)>>(32-(N))))
1461 * lpfc_sha_init: Set up initial array of hash table entries.
1462 * @HashResultPointer: pointer to an array as hash table.
1464 * This routine sets up the initial values to the array of hash table entries
1468 lpfc_sha_init(uint32_t * HashResultPointer)
1470 HashResultPointer[0] = 0x67452301;
1471 HashResultPointer[1] = 0xEFCDAB89;
1472 HashResultPointer[2] = 0x98BADCFE;
1473 HashResultPointer[3] = 0x10325476;
1474 HashResultPointer[4] = 0xC3D2E1F0;
1478 * lpfc_sha_iterate: Iterate initial hash table with the working hash table.
1479 * @HashResultPointer: pointer to an initial/result hash table.
1480 * @HashWorkingPointer: pointer to an working hash table.
1482 * This routine iterates an initial hash table pointed by @HashResultPointer
1483 * with the values from the working hash table pointeed by @HashWorkingPointer.
1484 * The results are putting back to the initial hash table, returned through
1485 * the @HashResultPointer as the result hash table.
1488 lpfc_sha_iterate(uint32_t * HashResultPointer, uint32_t * HashWorkingPointer)
1492 uint32_t A, B, C, D, E;
1495 HashWorkingPointer[t] =
1497 HashWorkingPointer[t - 3] ^ HashWorkingPointer[t -
1499 HashWorkingPointer[t - 14] ^ HashWorkingPointer[t - 16]);
1500 } while (++t <= 79);
1502 A = HashResultPointer[0];
1503 B = HashResultPointer[1];
1504 C = HashResultPointer[2];
1505 D = HashResultPointer[3];
1506 E = HashResultPointer[4];
1510 TEMP = ((B & C) | ((~B) & D)) + 0x5A827999;
1511 } else if (t < 40) {
1512 TEMP = (B ^ C ^ D) + 0x6ED9EBA1;
1513 } else if (t < 60) {
1514 TEMP = ((B & C) | (B & D) | (C & D)) + 0x8F1BBCDC;
1516 TEMP = (B ^ C ^ D) + 0xCA62C1D6;
1518 TEMP += S(5, A) + E + HashWorkingPointer[t];
1524 } while (++t <= 79);
1526 HashResultPointer[0] += A;
1527 HashResultPointer[1] += B;
1528 HashResultPointer[2] += C;
1529 HashResultPointer[3] += D;
1530 HashResultPointer[4] += E;
1535 * lpfc_challenge_key: Create challenge key based on WWPN of the HBA.
1536 * @RandomChallenge: pointer to the entry of host challenge random number array.
1537 * @HashWorking: pointer to the entry of the working hash array.
1539 * This routine calculates the working hash array referred by @HashWorking
1540 * from the challenge random numbers associated with the host, referred by
1541 * @RandomChallenge. The result is put into the entry of the working hash
1542 * array and returned by reference through @HashWorking.
1545 lpfc_challenge_key(uint32_t * RandomChallenge, uint32_t * HashWorking)
1547 *HashWorking = (*RandomChallenge ^ *HashWorking);
1551 * lpfc_hba_init: Perform special handling for LC HBA initialization.
1552 * @phba: pointer to lpfc hba data structure.
1553 * @hbainit: pointer to an array of unsigned 32-bit integers.
1555 * This routine performs the special handling for LC HBA initialization.
1558 lpfc_hba_init(struct lpfc_hba *phba, uint32_t *hbainit)
1561 uint32_t *HashWorking;
1562 uint32_t *pwwnn = (uint32_t *) phba->wwnn;
1564 HashWorking = kcalloc(80, sizeof(uint32_t), GFP_KERNEL);
1568 HashWorking[0] = HashWorking[78] = *pwwnn++;
1569 HashWorking[1] = HashWorking[79] = *pwwnn;
1571 for (t = 0; t < 7; t++)
1572 lpfc_challenge_key(phba->RandomData + t, HashWorking + t);
1574 lpfc_sha_init(hbainit);
1575 lpfc_sha_iterate(hbainit, HashWorking);
1580 * lpfc_cleanup: Performs vport cleanups before deleting a vport.
1581 * @vport: pointer to a virtual N_Port data structure.
1583 * This routine performs the necessary cleanups before deleting the @vport.
1584 * It invokes the discovery state machine to perform necessary state
1585 * transitions and to release the ndlps associated with the @vport. Note,
1586 * the physical port is treated as @vport 0.
1589 lpfc_cleanup(struct lpfc_vport *vport)
1591 struct lpfc_hba *phba = vport->phba;
1592 struct lpfc_nodelist *ndlp, *next_ndlp;
1595 if (phba->link_state > LPFC_LINK_DOWN)
1596 lpfc_port_link_failure(vport);
1598 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
1599 if (!NLP_CHK_NODE_ACT(ndlp)) {
1600 ndlp = lpfc_enable_node(vport, ndlp,
1601 NLP_STE_UNUSED_NODE);
1604 spin_lock_irq(&phba->ndlp_lock);
1605 NLP_SET_FREE_REQ(ndlp);
1606 spin_unlock_irq(&phba->ndlp_lock);
1607 /* Trigger the release of the ndlp memory */
1611 spin_lock_irq(&phba->ndlp_lock);
1612 if (NLP_CHK_FREE_REQ(ndlp)) {
1613 /* The ndlp should not be in memory free mode already */
1614 spin_unlock_irq(&phba->ndlp_lock);
1617 /* Indicate request for freeing ndlp memory */
1618 NLP_SET_FREE_REQ(ndlp);
1619 spin_unlock_irq(&phba->ndlp_lock);
1621 if (vport->port_type != LPFC_PHYSICAL_PORT &&
1622 ndlp->nlp_DID == Fabric_DID) {
1623 /* Just free up ndlp with Fabric_DID for vports */
1628 if (ndlp->nlp_type & NLP_FABRIC)
1629 lpfc_disc_state_machine(vport, ndlp, NULL,
1630 NLP_EVT_DEVICE_RECOVERY);
1632 lpfc_disc_state_machine(vport, ndlp, NULL,
1637 /* At this point, ALL ndlp's should be gone
1638 * because of the previous NLP_EVT_DEVICE_RM.
1639 * Lets wait for this to happen, if needed.
1641 while (!list_empty(&vport->fc_nodes)) {
1644 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
1645 "0233 Nodelist not empty\n");
1646 list_for_each_entry_safe(ndlp, next_ndlp,
1647 &vport->fc_nodes, nlp_listp) {
1648 lpfc_printf_vlog(ndlp->vport, KERN_ERR,
1650 "0282 did:x%x ndlp:x%p "
1651 "usgmap:x%x refcnt:%d\n",
1652 ndlp->nlp_DID, (void *)ndlp,
1655 &ndlp->kref.refcount));
1660 /* Wait for any activity on ndlps to settle */
1667 * lpfc_stop_vport_timers: Stop all the timers associated with a vport.
1668 * @vport: pointer to a virtual N_Port data structure.
1670 * This routine stops all the timers associated with a @vport. This function
1671 * is invoked before disabling or deleting a @vport. Note that the physical
1672 * port is treated as @vport 0.
1675 lpfc_stop_vport_timers(struct lpfc_vport *vport)
1677 del_timer_sync(&vport->els_tmofunc);
1678 del_timer_sync(&vport->fc_fdmitmo);
1679 lpfc_can_disctmo(vport);
1684 * lpfc_stop_phba_timers: Stop all the timers associated with an HBA.
1685 * @phba: pointer to lpfc hba data structure.
1687 * This routine stops all the timers associated with a HBA. This function is
1688 * invoked before either putting a HBA offline or unloading the driver.
1691 lpfc_stop_phba_timers(struct lpfc_hba *phba)
1693 del_timer_sync(&phba->fcp_poll_timer);
1694 lpfc_stop_vport_timers(phba->pport);
1695 del_timer_sync(&phba->sli.mbox_tmo);
1696 del_timer_sync(&phba->fabric_block_timer);
1697 phba->hb_outstanding = 0;
1698 del_timer_sync(&phba->hb_tmofunc);
1699 del_timer_sync(&phba->eratt_poll);
1704 * lpfc_block_mgmt_io: Mark a HBA's management interface as blocked.
1705 * @phba: pointer to lpfc hba data structure.
1707 * This routine marks a HBA's management interface as blocked. Once the HBA's
1708 * management interface is marked as blocked, all the user space access to
1709 * the HBA, whether they are from sysfs interface or libdfc interface will
1710 * all be blocked. The HBA is set to block the management interface when the
1711 * driver prepares the HBA interface for online or offline.
1714 lpfc_block_mgmt_io(struct lpfc_hba * phba)
1716 unsigned long iflag;
1718 spin_lock_irqsave(&phba->hbalock, iflag);
1719 phba->sli.sli_flag |= LPFC_BLOCK_MGMT_IO;
1720 spin_unlock_irqrestore(&phba->hbalock, iflag);
1724 * lpfc_online: Initialize and bring a HBA online.
1725 * @phba: pointer to lpfc hba data structure.
1727 * This routine initializes the HBA and brings a HBA online. During this
1728 * process, the management interface is blocked to prevent user space access
1729 * to the HBA interfering with the driver initialization.
1736 lpfc_online(struct lpfc_hba *phba)
1738 struct lpfc_vport *vport = phba->pport;
1739 struct lpfc_vport **vports;
1745 if (!(vport->fc_flag & FC_OFFLINE_MODE))
1748 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
1749 "0458 Bring Adapter online\n");
1751 lpfc_block_mgmt_io(phba);
1753 if (!lpfc_sli_queue_setup(phba)) {
1754 lpfc_unblock_mgmt_io(phba);
1758 if (lpfc_sli_hba_setup(phba)) { /* Initialize the HBA */
1759 lpfc_unblock_mgmt_io(phba);
1763 vports = lpfc_create_vport_work_array(phba);
1765 for(i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
1766 struct Scsi_Host *shost;
1767 shost = lpfc_shost_from_vport(vports[i]);
1768 spin_lock_irq(shost->host_lock);
1769 vports[i]->fc_flag &= ~FC_OFFLINE_MODE;
1770 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
1771 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
1772 spin_unlock_irq(shost->host_lock);
1774 lpfc_destroy_vport_work_array(phba, vports);
1776 lpfc_unblock_mgmt_io(phba);
1781 * lpfc_unblock_mgmt_io: Mark a HBA's management interface to be not blocked.
1782 * @phba: pointer to lpfc hba data structure.
1784 * This routine marks a HBA's management interface as not blocked. Once the
1785 * HBA's management interface is marked as not blocked, all the user space
1786 * access to the HBA, whether they are from sysfs interface or libdfc
1787 * interface will be allowed. The HBA is set to block the management interface
1788 * when the driver prepares the HBA interface for online or offline and then
1789 * set to unblock the management interface afterwards.
1792 lpfc_unblock_mgmt_io(struct lpfc_hba * phba)
1794 unsigned long iflag;
1796 spin_lock_irqsave(&phba->hbalock, iflag);
1797 phba->sli.sli_flag &= ~LPFC_BLOCK_MGMT_IO;
1798 spin_unlock_irqrestore(&phba->hbalock, iflag);
1802 * lpfc_offline_prep: Prepare a HBA to be brought offline.
1803 * @phba: pointer to lpfc hba data structure.
1805 * This routine is invoked to prepare a HBA to be brought offline. It performs
1806 * unregistration login to all the nodes on all vports and flushes the mailbox
1807 * queue to make it ready to be brought offline.
1810 lpfc_offline_prep(struct lpfc_hba * phba)
1812 struct lpfc_vport *vport = phba->pport;
1813 struct lpfc_nodelist *ndlp, *next_ndlp;
1814 struct lpfc_vport **vports;
1817 if (vport->fc_flag & FC_OFFLINE_MODE)
1820 lpfc_block_mgmt_io(phba);
1822 lpfc_linkdown(phba);
1824 /* Issue an unreg_login to all nodes on all vports */
1825 vports = lpfc_create_vport_work_array(phba);
1826 if (vports != NULL) {
1827 for(i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
1828 struct Scsi_Host *shost;
1830 if (vports[i]->load_flag & FC_UNLOADING)
1832 shost = lpfc_shost_from_vport(vports[i]);
1833 list_for_each_entry_safe(ndlp, next_ndlp,
1834 &vports[i]->fc_nodes,
1836 if (!NLP_CHK_NODE_ACT(ndlp))
1838 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
1840 if (ndlp->nlp_type & NLP_FABRIC) {
1841 lpfc_disc_state_machine(vports[i], ndlp,
1842 NULL, NLP_EVT_DEVICE_RECOVERY);
1843 lpfc_disc_state_machine(vports[i], ndlp,
1844 NULL, NLP_EVT_DEVICE_RM);
1846 spin_lock_irq(shost->host_lock);
1847 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
1848 spin_unlock_irq(shost->host_lock);
1849 lpfc_unreg_rpi(vports[i], ndlp);
1853 lpfc_destroy_vport_work_array(phba, vports);
1855 lpfc_sli_flush_mbox_queue(phba);
1859 * lpfc_offline: Bring a HBA offline.
1860 * @phba: pointer to lpfc hba data structure.
1862 * This routine actually brings a HBA offline. It stops all the timers
1863 * associated with the HBA, brings down the SLI layer, and eventually
1864 * marks the HBA as in offline state for the upper layer protocol.
1867 lpfc_offline(struct lpfc_hba *phba)
1869 struct Scsi_Host *shost;
1870 struct lpfc_vport **vports;
1873 if (phba->pport->fc_flag & FC_OFFLINE_MODE)
1876 /* stop all timers associated with this hba */
1877 lpfc_stop_phba_timers(phba);
1878 vports = lpfc_create_vport_work_array(phba);
1880 for(i = 0; i <= phba->max_vpi && vports[i] != NULL; i++)
1881 lpfc_stop_vport_timers(vports[i]);
1882 lpfc_destroy_vport_work_array(phba, vports);
1883 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
1884 "0460 Bring Adapter offline\n");
1885 /* Bring down the SLI Layer and cleanup. The HBA is offline
1887 lpfc_sli_hba_down(phba);
1888 spin_lock_irq(&phba->hbalock);
1890 spin_unlock_irq(&phba->hbalock);
1891 vports = lpfc_create_vport_work_array(phba);
1893 for(i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
1894 shost = lpfc_shost_from_vport(vports[i]);
1895 spin_lock_irq(shost->host_lock);
1896 vports[i]->work_port_events = 0;
1897 vports[i]->fc_flag |= FC_OFFLINE_MODE;
1898 spin_unlock_irq(shost->host_lock);
1900 lpfc_destroy_vport_work_array(phba, vports);
1904 * lpfc_scsi_free: Free all the SCSI buffers and IOCBs from driver lists.
1905 * @phba: pointer to lpfc hba data structure.
1907 * This routine is to free all the SCSI buffers and IOCBs from the driver
1908 * list back to kernel. It is called from lpfc_pci_remove_one to free
1909 * the internal resources before the device is removed from the system.
1912 * 0 - successful (for now, it always returns 0)
1915 lpfc_scsi_free(struct lpfc_hba *phba)
1917 struct lpfc_scsi_buf *sb, *sb_next;
1918 struct lpfc_iocbq *io, *io_next;
1920 spin_lock_irq(&phba->hbalock);
1921 /* Release all the lpfc_scsi_bufs maintained by this host. */
1922 list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list, list) {
1923 list_del(&sb->list);
1924 pci_pool_free(phba->lpfc_scsi_dma_buf_pool, sb->data,
1927 phba->total_scsi_bufs--;
1930 /* Release all the lpfc_iocbq entries maintained by this host. */
1931 list_for_each_entry_safe(io, io_next, &phba->lpfc_iocb_list, list) {
1932 list_del(&io->list);
1934 phba->total_iocbq_bufs--;
1937 spin_unlock_irq(&phba->hbalock);
1943 * lpfc_create_port: Create an FC port.
1944 * @phba: pointer to lpfc hba data structure.
1945 * @instance: a unique integer ID to this FC port.
1946 * @dev: pointer to the device data structure.
1948 * This routine creates a FC port for the upper layer protocol. The FC port
1949 * can be created on top of either a physical port or a virtual port provided
1950 * by the HBA. This routine also allocates a SCSI host data structure (shost)
1951 * and associates the FC port created before adding the shost into the SCSI
1955 * @vport - pointer to the virtual N_Port data structure.
1956 * NULL - port create failed.
1959 lpfc_create_port(struct lpfc_hba *phba, int instance, struct device *dev)
1961 struct lpfc_vport *vport;
1962 struct Scsi_Host *shost;
1965 if (dev != &phba->pcidev->dev)
1966 shost = scsi_host_alloc(&lpfc_vport_template,
1967 sizeof(struct lpfc_vport));
1969 shost = scsi_host_alloc(&lpfc_template,
1970 sizeof(struct lpfc_vport));
1974 vport = (struct lpfc_vport *) shost->hostdata;
1976 vport->load_flag |= FC_LOADING;
1977 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
1978 vport->fc_rscn_flush = 0;
1980 lpfc_get_vport_cfgparam(vport);
1981 shost->unique_id = instance;
1982 shost->max_id = LPFC_MAX_TARGET;
1983 shost->max_lun = vport->cfg_max_luns;
1984 shost->this_id = -1;
1985 shost->max_cmd_len = 16;
1987 * Set initial can_queue value since 0 is no longer supported and
1988 * scsi_add_host will fail. This will be adjusted later based on the
1989 * max xri value determined in hba setup.
1991 shost->can_queue = phba->cfg_hba_queue_depth - 10;
1992 if (dev != &phba->pcidev->dev) {
1993 shost->transportt = lpfc_vport_transport_template;
1994 vport->port_type = LPFC_NPIV_PORT;
1996 shost->transportt = lpfc_transport_template;
1997 vport->port_type = LPFC_PHYSICAL_PORT;
2000 /* Initialize all internally managed lists. */
2001 INIT_LIST_HEAD(&vport->fc_nodes);
2002 spin_lock_init(&vport->work_port_lock);
2004 init_timer(&vport->fc_disctmo);
2005 vport->fc_disctmo.function = lpfc_disc_timeout;
2006 vport->fc_disctmo.data = (unsigned long)vport;
2008 init_timer(&vport->fc_fdmitmo);
2009 vport->fc_fdmitmo.function = lpfc_fdmi_tmo;
2010 vport->fc_fdmitmo.data = (unsigned long)vport;
2012 init_timer(&vport->els_tmofunc);
2013 vport->els_tmofunc.function = lpfc_els_timeout;
2014 vport->els_tmofunc.data = (unsigned long)vport;
2016 error = scsi_add_host(shost, dev);
2020 spin_lock_irq(&phba->hbalock);
2021 list_add_tail(&vport->listentry, &phba->port_list);
2022 spin_unlock_irq(&phba->hbalock);
2026 scsi_host_put(shost);
2032 * destroy_port: Destroy an FC port.
2033 * @vport: pointer to an lpfc virtual N_Port data structure.
2035 * This routine destroys a FC port from the upper layer protocol. All the
2036 * resources associated with the port are released.
2039 destroy_port(struct lpfc_vport *vport)
2041 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2042 struct lpfc_hba *phba = vport->phba;
2044 kfree(vport->vname);
2046 lpfc_debugfs_terminate(vport);
2047 fc_remove_host(shost);
2048 scsi_remove_host(shost);
2050 spin_lock_irq(&phba->hbalock);
2051 list_del_init(&vport->listentry);
2052 spin_unlock_irq(&phba->hbalock);
2054 lpfc_cleanup(vport);
2059 * lpfc_get_instance: Get a unique integer ID.
2061 * This routine allocates a unique integer ID from lpfc_hba_index pool. It
2062 * uses the kernel idr facility to perform the task.
2065 * instance - a unique integer ID allocated as the new instance.
2066 * -1 - lpfc get instance failed.
2069 lpfc_get_instance(void)
2073 /* Assign an unused number */
2074 if (!idr_pre_get(&lpfc_hba_index, GFP_KERNEL))
2076 if (idr_get_new(&lpfc_hba_index, NULL, &instance))
2082 * lpfc_scan_finished: method for SCSI layer to detect whether scan is done.
2083 * @shost: pointer to SCSI host data structure.
2084 * @time: elapsed time of the scan in jiffies.
2086 * This routine is called by the SCSI layer with a SCSI host to determine
2087 * whether the scan host is finished.
2089 * Note: there is no scan_start function as adapter initialization will have
2090 * asynchronously kicked off the link initialization.
2093 * 0 - SCSI host scan is not over yet.
2094 * 1 - SCSI host scan is over.
2096 int lpfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
2098 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2099 struct lpfc_hba *phba = vport->phba;
2102 spin_lock_irq(shost->host_lock);
2104 if (vport->load_flag & FC_UNLOADING) {
2108 if (time >= 30 * HZ) {
2109 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
2110 "0461 Scanning longer than 30 "
2111 "seconds. Continuing initialization\n");
2115 if (time >= 15 * HZ && phba->link_state <= LPFC_LINK_DOWN) {
2116 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
2117 "0465 Link down longer than 15 "
2118 "seconds. Continuing initialization\n");
2123 if (vport->port_state != LPFC_VPORT_READY)
2125 if (vport->num_disc_nodes || vport->fc_prli_sent)
2127 if (vport->fc_map_cnt == 0 && time < 2 * HZ)
2129 if ((phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) != 0)
2135 spin_unlock_irq(shost->host_lock);
2140 * lpfc_host_attrib_init: Initialize SCSI host attributes on a FC port.
2141 * @shost: pointer to SCSI host data structure.
2143 * This routine initializes a given SCSI host attributes on a FC port. The
2144 * SCSI host can be either on top of a physical port or a virtual port.
2146 void lpfc_host_attrib_init(struct Scsi_Host *shost)
2148 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2149 struct lpfc_hba *phba = vport->phba;
2151 * Set fixed host attributes. Must done after lpfc_sli_hba_setup().
2154 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
2155 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
2156 fc_host_supported_classes(shost) = FC_COS_CLASS3;
2158 memset(fc_host_supported_fc4s(shost), 0,
2159 sizeof(fc_host_supported_fc4s(shost)));
2160 fc_host_supported_fc4s(shost)[2] = 1;
2161 fc_host_supported_fc4s(shost)[7] = 1;
2163 lpfc_vport_symbolic_node_name(vport, fc_host_symbolic_name(shost),
2164 sizeof fc_host_symbolic_name(shost));
2166 fc_host_supported_speeds(shost) = 0;
2167 if (phba->lmt & LMT_10Gb)
2168 fc_host_supported_speeds(shost) |= FC_PORTSPEED_10GBIT;
2169 if (phba->lmt & LMT_8Gb)
2170 fc_host_supported_speeds(shost) |= FC_PORTSPEED_8GBIT;
2171 if (phba->lmt & LMT_4Gb)
2172 fc_host_supported_speeds(shost) |= FC_PORTSPEED_4GBIT;
2173 if (phba->lmt & LMT_2Gb)
2174 fc_host_supported_speeds(shost) |= FC_PORTSPEED_2GBIT;
2175 if (phba->lmt & LMT_1Gb)
2176 fc_host_supported_speeds(shost) |= FC_PORTSPEED_1GBIT;
2178 fc_host_maxframe_size(shost) =
2179 (((uint32_t) vport->fc_sparam.cmn.bbRcvSizeMsb & 0x0F) << 8) |
2180 (uint32_t) vport->fc_sparam.cmn.bbRcvSizeLsb;
2182 /* This value is also unchanging */
2183 memset(fc_host_active_fc4s(shost), 0,
2184 sizeof(fc_host_active_fc4s(shost)));
2185 fc_host_active_fc4s(shost)[2] = 1;
2186 fc_host_active_fc4s(shost)[7] = 1;
2188 fc_host_max_npiv_vports(shost) = phba->max_vpi;
2189 spin_lock_irq(shost->host_lock);
2190 vport->load_flag &= ~FC_LOADING;
2191 spin_unlock_irq(shost->host_lock);
2195 * lpfc_enable_msix: Enable MSI-X interrupt mode.
2196 * @phba: pointer to lpfc hba data structure.
2198 * This routine is invoked to enable the MSI-X interrupt vectors. The kernel
2199 * function pci_enable_msix() is called to enable the MSI-X vectors. Note that
2200 * pci_enable_msix(), once invoked, enables either all or nothing, depending
2201 * on the current availability of PCI vector resources. The device driver is
2202 * responsible for calling the individual request_irq() to register each MSI-X
2203 * vector with a interrupt handler, which is done in this function. Note that
2204 * later when device is unloading, the driver should always call free_irq()
2205 * on all MSI-X vectors it has done request_irq() on before calling
2206 * pci_disable_msix(). Failure to do so results in a BUG_ON() and a device
2207 * will be left with MSI-X enabled and leaks its vectors.
2211 * other values - error
2214 lpfc_enable_msix(struct lpfc_hba *phba)
2219 /* Set up MSI-X multi-message vectors */
2220 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
2221 phba->msix_entries[i].entry = i;
2223 /* Configure MSI-X capability structure */
2224 rc = pci_enable_msix(phba->pcidev, phba->msix_entries,
2225 ARRAY_SIZE(phba->msix_entries));
2227 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
2228 "0420 Enable MSI-X failed (%d), continuing "
2232 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
2233 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
2234 "0477 MSI-X entry[%d]: vector=x%x "
2236 phba->msix_entries[i].vector,
2237 phba->msix_entries[i].entry);
2239 * Assign MSI-X vectors to interrupt handlers
2242 /* vector-0 is associated to slow-path handler */
2243 rc = request_irq(phba->msix_entries[0].vector, &lpfc_sp_intr_handler,
2244 IRQF_SHARED, LPFC_SP_DRIVER_HANDLER_NAME, phba);
2246 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2247 "0421 MSI-X slow-path request_irq failed "
2248 "(%d), continuing with MSI\n", rc);
2252 /* vector-1 is associated to fast-path handler */
2253 rc = request_irq(phba->msix_entries[1].vector, &lpfc_fp_intr_handler,
2254 IRQF_SHARED, LPFC_FP_DRIVER_HANDLER_NAME, phba);
2257 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2258 "0429 MSI-X fast-path request_irq failed "
2259 "(%d), continuing with MSI\n", rc);
2264 * Configure HBA MSI-X attention conditions to messages
2266 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2270 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2271 "0474 Unable to allocate memory for issuing "
2272 "MBOX_CONFIG_MSI command\n");
2275 rc = lpfc_config_msi(phba, pmb);
2278 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
2279 if (rc != MBX_SUCCESS) {
2280 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
2281 "0351 Config MSI mailbox command failed, "
2282 "mbxCmd x%x, mbxStatus x%x\n",
2283 pmb->mb.mbxCommand, pmb->mb.mbxStatus);
2287 /* Free memory allocated for mailbox command */
2288 mempool_free(pmb, phba->mbox_mem_pool);
2292 /* Free memory allocated for mailbox command */
2293 mempool_free(pmb, phba->mbox_mem_pool);
2296 /* free the irq already requested */
2297 free_irq(phba->msix_entries[1].vector, phba);
2300 /* free the irq already requested */
2301 free_irq(phba->msix_entries[0].vector, phba);
2304 /* Unconfigure MSI-X capability structure */
2305 pci_disable_msix(phba->pcidev);
2310 * lpfc_disable_msix: Disable MSI-X interrupt mode.
2311 * @phba: pointer to lpfc hba data structure.
2313 * This routine is invoked to release the MSI-X vectors and then disable the
2314 * MSI-X interrupt mode.
2317 lpfc_disable_msix(struct lpfc_hba *phba)
2321 /* Free up MSI-X multi-message vectors */
2322 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
2323 free_irq(phba->msix_entries[i].vector, phba);
2325 pci_disable_msix(phba->pcidev);
2329 * lpfc_pci_probe_one: lpfc PCI probe func to register device to PCI subsystem.
2330 * @pdev: pointer to PCI device
2331 * @pid: pointer to PCI device identifier
2333 * This routine is to be registered to the kernel's PCI subsystem. When an
2334 * Emulex HBA is presented in PCI bus, the kernel PCI subsystem looks at
2335 * PCI device-specific information of the device and driver to see if the
2336 * driver state that it can support this kind of device. If the match is
2337 * successful, the driver core invokes this routine. If this routine
2338 * determines it can claim the HBA, it does all the initialization that it
2339 * needs to do to handle the HBA properly.
2342 * 0 - driver can claim the device
2343 * negative value - driver can not claim the device
2345 static int __devinit
2346 lpfc_pci_probe_one(struct pci_dev *pdev, const struct pci_device_id *pid)
2348 struct lpfc_vport *vport = NULL;
2349 struct lpfc_hba *phba;
2350 struct lpfc_sli *psli;
2351 struct lpfc_iocbq *iocbq_entry = NULL, *iocbq_next = NULL;
2352 struct Scsi_Host *shost = NULL;
2354 unsigned long bar0map_len, bar2map_len;
2355 int error = -ENODEV, retval;
2358 int bars = pci_select_bars(pdev, IORESOURCE_MEM);
2359 struct lpfc_adapter_event_header adapter_event;
2361 if (pci_enable_device_mem(pdev))
2363 if (pci_request_selected_regions(pdev, bars, LPFC_DRIVER_NAME))
2364 goto out_disable_device;
2366 phba = kzalloc(sizeof (struct lpfc_hba), GFP_KERNEL);
2368 goto out_release_regions;
2370 atomic_set(&phba->fast_event_count, 0);
2371 spin_lock_init(&phba->hbalock);
2373 /* Initialize ndlp management spinlock */
2374 spin_lock_init(&phba->ndlp_lock);
2376 phba->pcidev = pdev;
2378 /* Assign an unused board number */
2379 if ((phba->brd_no = lpfc_get_instance()) < 0)
2382 INIT_LIST_HEAD(&phba->port_list);
2383 init_waitqueue_head(&phba->wait_4_mlo_m_q);
2385 * Get all the module params for configuring this host and then
2386 * establish the host.
2388 lpfc_get_cfgparam(phba);
2389 phba->max_vpi = LPFC_MAX_VPI;
2391 /* Initialize timers used by driver */
2392 init_timer(&phba->hb_tmofunc);
2393 phba->hb_tmofunc.function = lpfc_hb_timeout;
2394 phba->hb_tmofunc.data = (unsigned long)phba;
2397 init_timer(&psli->mbox_tmo);
2398 psli->mbox_tmo.function = lpfc_mbox_timeout;
2399 psli->mbox_tmo.data = (unsigned long) phba;
2400 init_timer(&phba->fcp_poll_timer);
2401 phba->fcp_poll_timer.function = lpfc_poll_timeout;
2402 phba->fcp_poll_timer.data = (unsigned long) phba;
2403 init_timer(&phba->fabric_block_timer);
2404 phba->fabric_block_timer.function = lpfc_fabric_block_timeout;
2405 phba->fabric_block_timer.data = (unsigned long) phba;
2406 init_timer(&phba->eratt_poll);
2407 phba->eratt_poll.function = lpfc_poll_eratt;
2408 phba->eratt_poll.data = (unsigned long) phba;
2410 pci_set_master(pdev);
2411 pci_try_set_mwi(pdev);
2413 if (pci_set_dma_mask(phba->pcidev, DMA_64BIT_MASK) != 0)
2414 if (pci_set_dma_mask(phba->pcidev, DMA_32BIT_MASK) != 0)
2415 goto out_idr_remove;
2418 * Get the bus address of Bar0 and Bar2 and the number of bytes
2419 * required by each mapping.
2421 phba->pci_bar0_map = pci_resource_start(phba->pcidev, 0);
2422 bar0map_len = pci_resource_len(phba->pcidev, 0);
2424 phba->pci_bar2_map = pci_resource_start(phba->pcidev, 2);
2425 bar2map_len = pci_resource_len(phba->pcidev, 2);
2427 /* Map HBA SLIM to a kernel virtual address. */
2428 phba->slim_memmap_p = ioremap(phba->pci_bar0_map, bar0map_len);
2429 if (!phba->slim_memmap_p) {
2431 dev_printk(KERN_ERR, &pdev->dev,
2432 "ioremap failed for SLIM memory.\n");
2433 goto out_idr_remove;
2436 /* Map HBA Control Registers to a kernel virtual address. */
2437 phba->ctrl_regs_memmap_p = ioremap(phba->pci_bar2_map, bar2map_len);
2438 if (!phba->ctrl_regs_memmap_p) {
2440 dev_printk(KERN_ERR, &pdev->dev,
2441 "ioremap failed for HBA control registers.\n");
2442 goto out_iounmap_slim;
2445 /* Allocate memory for SLI-2 structures */
2446 phba->slim2p.virt = dma_alloc_coherent(&phba->pcidev->dev,
2450 if (!phba->slim2p.virt)
2453 memset(phba->slim2p.virt, 0, SLI2_SLIM_SIZE);
2454 phba->mbox = phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, mbx);
2455 phba->pcb = (phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, pcb));
2456 phba->IOCBs = (phba->slim2p.virt +
2457 offsetof(struct lpfc_sli2_slim, IOCBs));
2459 phba->hbqslimp.virt = dma_alloc_coherent(&phba->pcidev->dev,
2460 lpfc_sli_hbq_size(),
2461 &phba->hbqslimp.phys,
2463 if (!phba->hbqslimp.virt)
2466 hbq_count = lpfc_sli_hbq_count();
2467 ptr = phba->hbqslimp.virt;
2468 for (i = 0; i < hbq_count; ++i) {
2469 phba->hbqs[i].hbq_virt = ptr;
2470 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
2471 ptr += (lpfc_hbq_defs[i]->entry_count *
2472 sizeof(struct lpfc_hbq_entry));
2474 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_els_hbq_alloc;
2475 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_els_hbq_free;
2477 memset(phba->hbqslimp.virt, 0, lpfc_sli_hbq_size());
2479 INIT_LIST_HEAD(&phba->hbqbuf_in_list);
2481 /* Initialize the SLI Layer to run with lpfc HBAs. */
2482 lpfc_sli_setup(phba);
2483 lpfc_sli_queue_setup(phba);
2485 retval = lpfc_mem_alloc(phba);
2488 goto out_free_hbqslimp;
2491 /* Initialize and populate the iocb list per host. */
2492 INIT_LIST_HEAD(&phba->lpfc_iocb_list);
2493 for (i = 0; i < LPFC_IOCB_LIST_CNT; i++) {
2494 iocbq_entry = kzalloc(sizeof(struct lpfc_iocbq), GFP_KERNEL);
2495 if (iocbq_entry == NULL) {
2496 printk(KERN_ERR "%s: only allocated %d iocbs of "
2497 "expected %d count. Unloading driver.\n",
2498 __func__, i, LPFC_IOCB_LIST_CNT);
2500 goto out_free_iocbq;
2503 iotag = lpfc_sli_next_iotag(phba, iocbq_entry);
2505 kfree (iocbq_entry);
2506 printk(KERN_ERR "%s: failed to allocate IOTAG. "
2507 "Unloading driver.\n",
2510 goto out_free_iocbq;
2513 spin_lock_irq(&phba->hbalock);
2514 list_add(&iocbq_entry->list, &phba->lpfc_iocb_list);
2515 phba->total_iocbq_bufs++;
2516 spin_unlock_irq(&phba->hbalock);
2519 /* Initialize HBA structure */
2520 phba->fc_edtov = FF_DEF_EDTOV;
2521 phba->fc_ratov = FF_DEF_RATOV;
2522 phba->fc_altov = FF_DEF_ALTOV;
2523 phba->fc_arbtov = FF_DEF_ARBTOV;
2525 INIT_LIST_HEAD(&phba->work_list);
2526 phba->work_ha_mask = (HA_ERATT | HA_MBATT | HA_LATT);
2527 phba->work_ha_mask |= (HA_RXMASK << (LPFC_ELS_RING * 4));
2529 /* Initialize the wait queue head for the kernel thread */
2530 init_waitqueue_head(&phba->work_waitq);
2532 /* Startup the kernel thread for this host adapter. */
2533 phba->worker_thread = kthread_run(lpfc_do_work, phba,
2534 "lpfc_worker_%d", phba->brd_no);
2535 if (IS_ERR(phba->worker_thread)) {
2536 error = PTR_ERR(phba->worker_thread);
2537 goto out_free_iocbq;
2540 /* Initialize the list of scsi buffers used by driver for scsi IO. */
2541 spin_lock_init(&phba->scsi_buf_list_lock);
2542 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list);
2544 /* Initialize list of fabric iocbs */
2545 INIT_LIST_HEAD(&phba->fabric_iocb_list);
2547 /* Initialize list to save ELS buffers */
2548 INIT_LIST_HEAD(&phba->elsbuf);
2550 vport = lpfc_create_port(phba, phba->brd_no, &phba->pcidev->dev);
2552 goto out_kthread_stop;
2554 shost = lpfc_shost_from_vport(vport);
2555 phba->pport = vport;
2556 lpfc_debugfs_initialize(vport);
2558 pci_set_drvdata(pdev, shost);
2559 phba->intr_type = NONE;
2561 phba->MBslimaddr = phba->slim_memmap_p;
2562 phba->HAregaddr = phba->ctrl_regs_memmap_p + HA_REG_OFFSET;
2563 phba->CAregaddr = phba->ctrl_regs_memmap_p + CA_REG_OFFSET;
2564 phba->HSregaddr = phba->ctrl_regs_memmap_p + HS_REG_OFFSET;
2565 phba->HCregaddr = phba->ctrl_regs_memmap_p + HC_REG_OFFSET;
2567 /* Configure and enable interrupt */
2568 if (phba->cfg_use_msi == 2) {
2569 /* Need to issue conf_port mbox cmd before conf_msi mbox cmd */
2570 error = lpfc_sli_config_port(phba, 3);
2572 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
2573 "0427 Firmware not capable of SLI 3 mode.\n");
2575 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
2576 "0426 Firmware capable of SLI 3 mode.\n");
2577 /* Now, try to enable MSI-X interrupt mode */
2578 error = lpfc_enable_msix(phba);
2580 phba->intr_type = MSIX;
2581 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
2582 "0430 enable MSI-X mode.\n");
2587 /* Fallback to MSI if MSI-X initialization failed */
2588 if (phba->cfg_use_msi >= 1 && phba->intr_type == NONE) {
2589 retval = pci_enable_msi(phba->pcidev);
2591 phba->intr_type = MSI;
2592 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
2593 "0473 enable MSI mode.\n");
2595 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
2596 "0452 enable IRQ mode.\n");
2599 /* MSI-X is the only case the doesn't need to call request_irq */
2600 if (phba->intr_type != MSIX) {
2601 retval = request_irq(phba->pcidev->irq, lpfc_intr_handler,
2602 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
2604 lpfc_printf_log(phba, KERN_ERR, LOG_INIT, "0451 Enable "
2605 "interrupt handler failed\n");
2607 goto out_disable_msi;
2608 } else if (phba->intr_type != MSI)
2609 phba->intr_type = INTx;
2612 if (lpfc_alloc_sysfs_attr(vport)) {
2613 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2614 "1476 Failed to allocate sysfs attr\n");
2619 if (lpfc_sli_hba_setup(phba)) {
2620 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2621 "1477 Failed to set up hba\n");
2623 goto out_remove_device;
2627 * hba setup may have changed the hba_queue_depth so we need to adjust
2628 * the value of can_queue.
2630 shost->can_queue = phba->cfg_hba_queue_depth - 10;
2632 lpfc_host_attrib_init(shost);
2634 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
2635 spin_lock_irq(shost->host_lock);
2636 lpfc_poll_start_timer(phba);
2637 spin_unlock_irq(shost->host_lock);
2640 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
2641 "0428 Perform SCSI scan\n");
2642 /* Send board arrival event to upper layer */
2643 adapter_event.event_type = FC_REG_ADAPTER_EVENT;
2644 adapter_event.subcategory = LPFC_EVENT_ARRIVAL;
2645 fc_host_post_vendor_event(shost, fc_get_event_number(),
2646 sizeof(adapter_event),
2647 (char *) &adapter_event,
2650 scsi_scan_host(shost);
2655 lpfc_free_sysfs_attr(vport);
2656 spin_lock_irq(shost->host_lock);
2657 vport->load_flag |= FC_UNLOADING;
2658 spin_unlock_irq(shost->host_lock);
2660 lpfc_stop_phba_timers(phba);
2661 phba->pport->work_port_events = 0;
2663 if (phba->intr_type == MSIX)
2664 lpfc_disable_msix(phba);
2666 free_irq(phba->pcidev->irq, phba);
2669 if (phba->intr_type == MSI)
2670 pci_disable_msi(phba->pcidev);
2671 destroy_port(vport);
2673 kthread_stop(phba->worker_thread);
2675 list_for_each_entry_safe(iocbq_entry, iocbq_next,
2676 &phba->lpfc_iocb_list, list) {
2678 phba->total_iocbq_bufs--;
2680 lpfc_mem_free(phba);
2682 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
2683 phba->hbqslimp.virt, phba->hbqslimp.phys);
2685 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
2686 phba->slim2p.virt, phba->slim2p.phys);
2688 iounmap(phba->ctrl_regs_memmap_p);
2690 iounmap(phba->slim_memmap_p);
2692 idr_remove(&lpfc_hba_index, phba->brd_no);
2695 out_release_regions:
2696 pci_release_selected_regions(pdev, bars);
2698 pci_disable_device(pdev);
2700 pci_set_drvdata(pdev, NULL);
2702 scsi_host_put(shost);
2707 * lpfc_pci_remove_one: lpfc PCI func to unregister device from PCI subsystem.
2708 * @pdev: pointer to PCI device
2710 * This routine is to be registered to the kernel's PCI subsystem. When an
2711 * Emulex HBA is removed from PCI bus. It perform all the necessary cleanup
2712 * for the HBA device to be removed from the PCI subsystem properly.
2714 static void __devexit
2715 lpfc_pci_remove_one(struct pci_dev *pdev)
2717 struct Scsi_Host *shost = pci_get_drvdata(pdev);
2718 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2719 struct lpfc_hba *phba = vport->phba;
2720 int bars = pci_select_bars(pdev, IORESOURCE_MEM);
2722 spin_lock_irq(&phba->hbalock);
2723 vport->load_flag |= FC_UNLOADING;
2724 spin_unlock_irq(&phba->hbalock);
2726 kfree(vport->vname);
2727 lpfc_free_sysfs_attr(vport);
2729 kthread_stop(phba->worker_thread);
2731 fc_remove_host(shost);
2732 scsi_remove_host(shost);
2733 lpfc_cleanup(vport);
2736 * Bring down the SLI Layer. This step disable all interrupts,
2737 * clears the rings, discards all mailbox commands, and resets
2740 lpfc_sli_hba_down(phba);
2741 lpfc_sli_brdrestart(phba);
2743 lpfc_stop_phba_timers(phba);
2744 spin_lock_irq(&phba->hbalock);
2745 list_del_init(&vport->listentry);
2746 spin_unlock_irq(&phba->hbalock);
2748 lpfc_debugfs_terminate(vport);
2750 if (phba->intr_type == MSIX)
2751 lpfc_disable_msix(phba);
2753 free_irq(phba->pcidev->irq, phba);
2754 if (phba->intr_type == MSI)
2755 pci_disable_msi(phba->pcidev);
2758 pci_set_drvdata(pdev, NULL);
2759 scsi_host_put(shost);
2762 * Call scsi_free before mem_free since scsi bufs are released to their
2763 * corresponding pools here.
2765 lpfc_scsi_free(phba);
2766 lpfc_mem_free(phba);
2768 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
2769 phba->hbqslimp.virt, phba->hbqslimp.phys);
2771 /* Free resources associated with SLI2 interface */
2772 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
2773 phba->slim2p.virt, phba->slim2p.phys);
2775 /* unmap adapter SLIM and Control Registers */
2776 iounmap(phba->ctrl_regs_memmap_p);
2777 iounmap(phba->slim_memmap_p);
2779 idr_remove(&lpfc_hba_index, phba->brd_no);
2783 pci_release_selected_regions(pdev, bars);
2784 pci_disable_device(pdev);
2788 * lpfc_io_error_detected: Driver method for handling PCI I/O error detected.
2789 * @pdev: pointer to PCI device.
2790 * @state: the current PCI connection state.
2792 * This routine is registered to the PCI subsystem for error handling. This
2793 * function is called by the PCI subsystem after a PCI bus error affecting
2794 * this device has been detected. When this function is invoked, it will
2795 * need to stop all the I/Os and interrupt(s) to the device. Once that is
2796 * done, it will return PCI_ERS_RESULT_NEED_RESET for the PCI subsystem to
2797 * perform proper recovery as desired.
2800 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
2801 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
2803 static pci_ers_result_t lpfc_io_error_detected(struct pci_dev *pdev,
2804 pci_channel_state_t state)
2806 struct Scsi_Host *shost = pci_get_drvdata(pdev);
2807 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
2808 struct lpfc_sli *psli = &phba->sli;
2809 struct lpfc_sli_ring *pring;
2811 if (state == pci_channel_io_perm_failure) {
2812 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2813 "0472 PCI channel I/O permanent failure\n");
2814 /* Block all SCSI devices' I/Os on the host */
2815 lpfc_scsi_dev_block(phba);
2816 /* Clean up all driver's outstanding SCSI I/Os */
2817 lpfc_sli_flush_fcp_rings(phba);
2818 return PCI_ERS_RESULT_DISCONNECT;
2821 pci_disable_device(pdev);
2823 * There may be I/Os dropped by the firmware.
2824 * Error iocb (I/O) on txcmplq and let the SCSI layer
2825 * retry it after re-establishing link.
2827 pring = &psli->ring[psli->fcp_ring];
2828 lpfc_sli_abort_iocb_ring(phba, pring);
2830 if (phba->intr_type == MSIX)
2831 lpfc_disable_msix(phba);
2833 free_irq(phba->pcidev->irq, phba);
2834 if (phba->intr_type == MSI)
2835 pci_disable_msi(phba->pcidev);
2838 /* Request a slot reset. */
2839 return PCI_ERS_RESULT_NEED_RESET;
2843 * lpfc_io_slot_reset: Restart a PCI device from scratch.
2844 * @pdev: pointer to PCI device.
2846 * This routine is registered to the PCI subsystem for error handling. This is
2847 * called after PCI bus has been reset to restart the PCI card from scratch,
2848 * as if from a cold-boot. During the PCI subsystem error recovery, after the
2849 * driver returns PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform
2850 * proper error recovery and then call this routine before calling the .resume
2851 * method to recover the device. This function will initialize the HBA device,
2852 * enable the interrupt, but it will just put the HBA to offline state without
2853 * passing any I/O traffic.
2856 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
2857 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
2859 static pci_ers_result_t lpfc_io_slot_reset(struct pci_dev *pdev)
2861 struct Scsi_Host *shost = pci_get_drvdata(pdev);
2862 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
2863 struct lpfc_sli *psli = &phba->sli;
2866 dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
2867 if (pci_enable_device_mem(pdev)) {
2868 printk(KERN_ERR "lpfc: Cannot re-enable "
2869 "PCI device after reset.\n");
2870 return PCI_ERS_RESULT_DISCONNECT;
2873 pci_set_master(pdev);
2875 spin_lock_irq(&phba->hbalock);
2876 psli->sli_flag &= ~LPFC_SLI2_ACTIVE;
2877 spin_unlock_irq(&phba->hbalock);
2879 /* Enable configured interrupt method */
2880 phba->intr_type = NONE;
2881 if (phba->cfg_use_msi == 2) {
2882 /* Need to issue conf_port mbox cmd before conf_msi mbox cmd */
2883 error = lpfc_sli_config_port(phba, 3);
2885 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
2886 "0478 Firmware not capable of SLI 3 mode.\n");
2888 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
2889 "0479 Firmware capable of SLI 3 mode.\n");
2890 /* Now, try to enable MSI-X interrupt mode */
2891 error = lpfc_enable_msix(phba);
2893 phba->intr_type = MSIX;
2894 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
2895 "0480 enable MSI-X mode.\n");
2900 /* Fallback to MSI if MSI-X initialization failed */
2901 if (phba->cfg_use_msi >= 1 && phba->intr_type == NONE) {
2902 retval = pci_enable_msi(phba->pcidev);
2904 phba->intr_type = MSI;
2905 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
2906 "0481 enable MSI mode.\n");
2908 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
2909 "0470 enable IRQ mode.\n");
2912 /* MSI-X is the only case the doesn't need to call request_irq */
2913 if (phba->intr_type != MSIX) {
2914 retval = request_irq(phba->pcidev->irq, lpfc_intr_handler,
2915 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
2917 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2918 "0471 Enable interrupt handler "
2920 } else if (phba->intr_type != MSI)
2921 phba->intr_type = INTx;
2924 /* Take device offline; this will perform cleanup */
2926 lpfc_sli_brdrestart(phba);
2928 return PCI_ERS_RESULT_RECOVERED;
2932 * lpfc_io_resume: Resume PCI I/O operation.
2933 * @pdev: pointer to PCI device
2935 * This routine is registered to the PCI subsystem for error handling. It is
2936 * called when kernel error recovery tells the lpfc driver that it is ok to
2937 * resume normal PCI operation after PCI bus error recovery. After this call,
2938 * traffic can start to flow from this device again.
2940 static void lpfc_io_resume(struct pci_dev *pdev)
2942 struct Scsi_Host *shost = pci_get_drvdata(pdev);
2943 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
2948 static struct pci_device_id lpfc_id_table[] = {
2949 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_VIPER,
2950 PCI_ANY_ID, PCI_ANY_ID, },
2951 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_FIREFLY,
2952 PCI_ANY_ID, PCI_ANY_ID, },
2953 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_THOR,
2954 PCI_ANY_ID, PCI_ANY_ID, },
2955 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PEGASUS,
2956 PCI_ANY_ID, PCI_ANY_ID, },
2957 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_CENTAUR,
2958 PCI_ANY_ID, PCI_ANY_ID, },
2959 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_DRAGONFLY,
2960 PCI_ANY_ID, PCI_ANY_ID, },
2961 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SUPERFLY,
2962 PCI_ANY_ID, PCI_ANY_ID, },
2963 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_RFLY,
2964 PCI_ANY_ID, PCI_ANY_ID, },
2965 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PFLY,
2966 PCI_ANY_ID, PCI_ANY_ID, },
2967 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE,
2968 PCI_ANY_ID, PCI_ANY_ID, },
2969 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_SCSP,
2970 PCI_ANY_ID, PCI_ANY_ID, },
2971 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_DCSP,
2972 PCI_ANY_ID, PCI_ANY_ID, },
2973 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS,
2974 PCI_ANY_ID, PCI_ANY_ID, },
2975 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_SCSP,
2976 PCI_ANY_ID, PCI_ANY_ID, },
2977 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_DCSP,
2978 PCI_ANY_ID, PCI_ANY_ID, },
2979 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BMID,
2980 PCI_ANY_ID, PCI_ANY_ID, },
2981 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BSMB,
2982 PCI_ANY_ID, PCI_ANY_ID, },
2983 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR,
2984 PCI_ANY_ID, PCI_ANY_ID, },
2985 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HORNET,
2986 PCI_ANY_ID, PCI_ANY_ID, },
2987 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_SCSP,
2988 PCI_ANY_ID, PCI_ANY_ID, },
2989 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_DCSP,
2990 PCI_ANY_ID, PCI_ANY_ID, },
2991 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZMID,
2992 PCI_ANY_ID, PCI_ANY_ID, },
2993 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZSMB,
2994 PCI_ANY_ID, PCI_ANY_ID, },
2995 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_TFLY,
2996 PCI_ANY_ID, PCI_ANY_ID, },
2997 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP101,
2998 PCI_ANY_ID, PCI_ANY_ID, },
2999 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP10000S,
3000 PCI_ANY_ID, PCI_ANY_ID, },
3001 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP11000S,
3002 PCI_ANY_ID, PCI_ANY_ID, },
3003 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LPE11000S,
3004 PCI_ANY_ID, PCI_ANY_ID, },
3005 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT,
3006 PCI_ANY_ID, PCI_ANY_ID, },
3007 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_MID,
3008 PCI_ANY_ID, PCI_ANY_ID, },
3009 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SMB,
3010 PCI_ANY_ID, PCI_ANY_ID, },
3011 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_DCSP,
3012 PCI_ANY_ID, PCI_ANY_ID, },
3013 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SCSP,
3014 PCI_ANY_ID, PCI_ANY_ID, },
3015 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_S,
3016 PCI_ANY_ID, PCI_ANY_ID, },
3017 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_VF,
3018 PCI_ANY_ID, PCI_ANY_ID, },
3019 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_PF,
3020 PCI_ANY_ID, PCI_ANY_ID, },
3021 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_S,
3022 PCI_ANY_ID, PCI_ANY_ID, },
3026 MODULE_DEVICE_TABLE(pci, lpfc_id_table);
3028 static struct pci_error_handlers lpfc_err_handler = {
3029 .error_detected = lpfc_io_error_detected,
3030 .slot_reset = lpfc_io_slot_reset,
3031 .resume = lpfc_io_resume,
3034 static struct pci_driver lpfc_driver = {
3035 .name = LPFC_DRIVER_NAME,
3036 .id_table = lpfc_id_table,
3037 .probe = lpfc_pci_probe_one,
3038 .remove = __devexit_p(lpfc_pci_remove_one),
3039 .err_handler = &lpfc_err_handler,
3043 * lpfc_init: lpfc module initialization routine.
3045 * This routine is to be invoked when the lpfc module is loaded into the
3046 * kernel. The special kernel macro module_init() is used to indicate the
3047 * role of this routine to the kernel as lpfc module entry point.
3051 * -ENOMEM - FC attach transport failed
3052 * all others - failed
3059 printk(LPFC_MODULE_DESC "\n");
3060 printk(LPFC_COPYRIGHT "\n");
3062 if (lpfc_enable_npiv) {
3063 lpfc_transport_functions.vport_create = lpfc_vport_create;
3064 lpfc_transport_functions.vport_delete = lpfc_vport_delete;
3066 lpfc_transport_template =
3067 fc_attach_transport(&lpfc_transport_functions);
3068 if (lpfc_transport_template == NULL)
3070 if (lpfc_enable_npiv) {
3071 lpfc_vport_transport_template =
3072 fc_attach_transport(&lpfc_vport_transport_functions);
3073 if (lpfc_vport_transport_template == NULL) {
3074 fc_release_transport(lpfc_transport_template);
3078 error = pci_register_driver(&lpfc_driver);
3080 fc_release_transport(lpfc_transport_template);
3081 if (lpfc_enable_npiv)
3082 fc_release_transport(lpfc_vport_transport_template);
3089 * lpfc_exit: lpfc module removal routine.
3091 * This routine is invoked when the lpfc module is removed from the kernel.
3092 * The special kernel macro module_exit() is used to indicate the role of
3093 * this routine to the kernel as lpfc module exit point.
3098 pci_unregister_driver(&lpfc_driver);
3099 fc_release_transport(lpfc_transport_template);
3100 if (lpfc_enable_npiv)
3101 fc_release_transport(lpfc_vport_transport_template);
3104 module_init(lpfc_init);
3105 module_exit(lpfc_exit);
3106 MODULE_LICENSE("GPL");
3107 MODULE_DESCRIPTION(LPFC_MODULE_DESC);
3108 MODULE_AUTHOR("Emulex Corporation - tech.support@emulex.com");
3109 MODULE_VERSION("0:" LPFC_DRIVER_VERSION);