lpfc_init.c revision ffcde188a82497385139c62c6b6362aa4f29406f
1/*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for         *
3 * Fibre Channel Host Bus Adapters.                                *
4 * Copyright (C) 2004-2007 Emulex.  All rights reserved.           *
5 * EMULEX and SLI are trademarks of Emulex.                        *
6 * www.emulex.com                                                  *
7 * Portions Copyright (C) 2004-2005 Christoph Hellwig              *
8 *                                                                 *
9 * This program is free software; you can redistribute it and/or   *
10 * modify it under the terms of version 2 of the GNU General       *
11 * Public License as published by the Free Software Foundation.    *
12 * This program is distributed in the hope that it will be useful. *
13 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND          *
14 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,  *
15 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE      *
16 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
17 * TO BE LEGALLY INVALID.  See the GNU General Public License for  *
18 * more details, a copy of which can be found in the file COPYING  *
19 * included with this package.                                     *
20 *******************************************************************/
21
22#include <linux/blkdev.h>
23#include <linux/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>
31
32#include <scsi/scsi.h>
33#include <scsi/scsi_device.h>
34#include <scsi/scsi_host.h>
35#include <scsi/scsi_transport_fc.h>
36
37#include "lpfc_hw.h"
38#include "lpfc_sli.h"
39#include "lpfc_disc.h"
40#include "lpfc_scsi.h"
41#include "lpfc.h"
42#include "lpfc_logmsg.h"
43#include "lpfc_crtn.h"
44#include "lpfc_vport.h"
45#include "lpfc_version.h"
46
47static int lpfc_parse_vpd(struct lpfc_hba *, uint8_t *, int);
48static void lpfc_get_hba_model_desc(struct lpfc_hba *, uint8_t *, uint8_t *);
49static int lpfc_post_rcv_buf(struct lpfc_hba *);
50
51static struct scsi_transport_template *lpfc_transport_template = NULL;
52static struct scsi_transport_template *lpfc_vport_transport_template = NULL;
53static DEFINE_IDR(lpfc_hba_index);
54
55/************************************************************************/
56/*                                                                      */
57/*    lpfc_config_port_prep                                             */
58/*    This routine will do LPFC initialization prior to the             */
59/*    CONFIG_PORT mailbox command. This will be initialized             */
60/*    as a SLI layer callback routine.                                  */
61/*    This routine returns 0 on success or -ERESTART if it wants        */
62/*    the SLI layer to reset the HBA and try again. Any                 */
63/*    other return value indicates an error.                            */
64/*                                                                      */
65/************************************************************************/
66int
67lpfc_config_port_prep(struct lpfc_hba *phba)
68{
69	lpfc_vpd_t *vp = &phba->vpd;
70	int i = 0, rc;
71	LPFC_MBOXQ_t *pmb;
72	MAILBOX_t *mb;
73	char *lpfc_vpd_data = NULL;
74	uint16_t offset = 0;
75	static char licensed[56] =
76		    "key unlock for use with gnu public licensed code only\0";
77	static int init_key = 1;
78
79	pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
80	if (!pmb) {
81		phba->link_state = LPFC_HBA_ERROR;
82		return -ENOMEM;
83	}
84
85	mb = &pmb->mb;
86	phba->link_state = LPFC_INIT_MBX_CMDS;
87
88	if (lpfc_is_LC_HBA(phba->pcidev->device)) {
89		if (init_key) {
90			uint32_t *ptext = (uint32_t *) licensed;
91
92			for (i = 0; i < 56; i += sizeof (uint32_t), ptext++)
93				*ptext = cpu_to_be32(*ptext);
94			init_key = 0;
95		}
96
97		lpfc_read_nv(phba, pmb);
98		memset((char*)mb->un.varRDnvp.rsvd3, 0,
99			sizeof (mb->un.varRDnvp.rsvd3));
100		memcpy((char*)mb->un.varRDnvp.rsvd3, licensed,
101			 sizeof (licensed));
102
103		rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
104
105		if (rc != MBX_SUCCESS) {
106			lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
107					"0324 Config Port initialization "
108					"error, mbxCmd x%x READ_NVPARM, "
109					"mbxStatus x%x\n",
110					mb->mbxCommand, mb->mbxStatus);
111			mempool_free(pmb, phba->mbox_mem_pool);
112			return -ERESTART;
113		}
114		memcpy(phba->wwnn, (char *)mb->un.varRDnvp.nodename,
115		       sizeof(phba->wwnn));
116		memcpy(phba->wwpn, (char *)mb->un.varRDnvp.portname,
117		       sizeof(phba->wwpn));
118	}
119
120	phba->sli3_options = 0x0;
121
122	/* Setup and issue mailbox READ REV command */
123	lpfc_read_rev(phba, pmb);
124	rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
125	if (rc != MBX_SUCCESS) {
126		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
127				"0439 Adapter failed to init, mbxCmd x%x "
128				"READ_REV, mbxStatus x%x\n",
129				mb->mbxCommand, mb->mbxStatus);
130		mempool_free( pmb, phba->mbox_mem_pool);
131		return -ERESTART;
132	}
133
134
135	/*
136	 * The value of rr must be 1 since the driver set the cv field to 1.
137	 * This setting requires the FW to set all revision fields.
138	 */
139	if (mb->un.varRdRev.rr == 0) {
140		vp->rev.rBit = 0;
141		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
142				"0440 Adapter failed to init, READ_REV has "
143				"missing revision information.\n");
144		mempool_free(pmb, phba->mbox_mem_pool);
145		return -ERESTART;
146	}
147
148	if (phba->sli_rev == 3 && !mb->un.varRdRev.v3rsp)
149		return -EINVAL;
150
151	/* Save information as VPD data */
152	vp->rev.rBit = 1;
153	memcpy(&vp->sli3Feat, &mb->un.varRdRev.sli3Feat, sizeof(uint32_t));
154	vp->rev.sli1FwRev = mb->un.varRdRev.sli1FwRev;
155	memcpy(vp->rev.sli1FwName, (char*) mb->un.varRdRev.sli1FwName, 16);
156	vp->rev.sli2FwRev = mb->un.varRdRev.sli2FwRev;
157	memcpy(vp->rev.sli2FwName, (char *) mb->un.varRdRev.sli2FwName, 16);
158	vp->rev.biuRev = mb->un.varRdRev.biuRev;
159	vp->rev.smRev = mb->un.varRdRev.smRev;
160	vp->rev.smFwRev = mb->un.varRdRev.un.smFwRev;
161	vp->rev.endecRev = mb->un.varRdRev.endecRev;
162	vp->rev.fcphHigh = mb->un.varRdRev.fcphHigh;
163	vp->rev.fcphLow = mb->un.varRdRev.fcphLow;
164	vp->rev.feaLevelHigh = mb->un.varRdRev.feaLevelHigh;
165	vp->rev.feaLevelLow = mb->un.varRdRev.feaLevelLow;
166	vp->rev.postKernRev = mb->un.varRdRev.postKernRev;
167	vp->rev.opFwRev = mb->un.varRdRev.opFwRev;
168
169	/* If the sli feature level is less then 9, we must
170	 * tear down all RPIs and VPIs on link down if NPIV
171	 * is enabled.
172	 */
173	if (vp->rev.feaLevelHigh < 9)
174		phba->sli3_options |= LPFC_SLI3_VPORT_TEARDOWN;
175
176	if (lpfc_is_LC_HBA(phba->pcidev->device))
177		memcpy(phba->RandomData, (char *)&mb->un.varWords[24],
178						sizeof (phba->RandomData));
179
180	/* Get adapter VPD information */
181	pmb->context2 = kmalloc(DMP_RSP_SIZE, GFP_KERNEL);
182	if (!pmb->context2)
183		goto out_free_mbox;
184	lpfc_vpd_data = kmalloc(DMP_VPD_SIZE, GFP_KERNEL);
185	if (!lpfc_vpd_data)
186		goto out_free_context2;
187
188	do {
189		lpfc_dump_mem(phba, pmb, offset);
190		rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
191
192		if (rc != MBX_SUCCESS) {
193			lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
194					"0441 VPD not present on adapter, "
195					"mbxCmd x%x DUMP VPD, mbxStatus x%x\n",
196					mb->mbxCommand, mb->mbxStatus);
197			mb->un.varDmp.word_cnt = 0;
198		}
199		if (mb->un.varDmp.word_cnt > DMP_VPD_SIZE - offset)
200			mb->un.varDmp.word_cnt = DMP_VPD_SIZE - offset;
201		lpfc_sli_pcimem_bcopy(pmb->context2, lpfc_vpd_data + offset,
202				      mb->un.varDmp.word_cnt);
203		offset += mb->un.varDmp.word_cnt;
204	} while (mb->un.varDmp.word_cnt && offset < DMP_VPD_SIZE);
205	lpfc_parse_vpd(phba, lpfc_vpd_data, offset);
206
207	kfree(lpfc_vpd_data);
208out_free_context2:
209	kfree(pmb->context2);
210out_free_mbox:
211	mempool_free(pmb, phba->mbox_mem_pool);
212	return 0;
213}
214
215/************************************************************************/
216/*                                                                      */
217/*    lpfc_config_port_post                                             */
218/*    This routine will do LPFC initialization after the                */
219/*    CONFIG_PORT mailbox command. This will be initialized             */
220/*    as a SLI layer callback routine.                                  */
221/*    This routine returns 0 on success. Any other return value         */
222/*    indicates an error.                                               */
223/*                                                                      */
224/************************************************************************/
225int
226lpfc_config_port_post(struct lpfc_hba *phba)
227{
228	struct lpfc_vport *vport = phba->pport;
229	LPFC_MBOXQ_t *pmb;
230	MAILBOX_t *mb;
231	struct lpfc_dmabuf *mp;
232	struct lpfc_sli *psli = &phba->sli;
233	uint32_t status, timeout;
234	int i, j;
235	int rc;
236
237	pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
238	if (!pmb) {
239		phba->link_state = LPFC_HBA_ERROR;
240		return -ENOMEM;
241	}
242	mb = &pmb->mb;
243
244	/* Get login parameters for NID.  */
245	lpfc_read_sparam(phba, pmb, 0);
246	pmb->vport = vport;
247	if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
248		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
249				"0448 Adapter failed init, mbxCmd x%x "
250				"READ_SPARM mbxStatus x%x\n",
251				mb->mbxCommand, mb->mbxStatus);
252		phba->link_state = LPFC_HBA_ERROR;
253		mp = (struct lpfc_dmabuf *) pmb->context1;
254		mempool_free( pmb, phba->mbox_mem_pool);
255		lpfc_mbuf_free(phba, mp->virt, mp->phys);
256		kfree(mp);
257		return -EIO;
258	}
259
260	mp = (struct lpfc_dmabuf *) pmb->context1;
261
262	memcpy(&vport->fc_sparam, mp->virt, sizeof (struct serv_parm));
263	lpfc_mbuf_free(phba, mp->virt, mp->phys);
264	kfree(mp);
265	pmb->context1 = NULL;
266
267	if (phba->cfg_soft_wwnn)
268		u64_to_wwn(phba->cfg_soft_wwnn,
269			   vport->fc_sparam.nodeName.u.wwn);
270	if (phba->cfg_soft_wwpn)
271		u64_to_wwn(phba->cfg_soft_wwpn,
272			   vport->fc_sparam.portName.u.wwn);
273	memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
274	       sizeof (struct lpfc_name));
275	memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
276	       sizeof (struct lpfc_name));
277	/* If no serial number in VPD data, use low 6 bytes of WWNN */
278	/* This should be consolidated into parse_vpd ? - mr */
279	if (phba->SerialNumber[0] == 0) {
280		uint8_t *outptr;
281
282		outptr = &vport->fc_nodename.u.s.IEEE[0];
283		for (i = 0; i < 12; i++) {
284			status = *outptr++;
285			j = ((status & 0xf0) >> 4);
286			if (j <= 9)
287				phba->SerialNumber[i] =
288				    (char)((uint8_t) 0x30 + (uint8_t) j);
289			else
290				phba->SerialNumber[i] =
291				    (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
292			i++;
293			j = (status & 0xf);
294			if (j <= 9)
295				phba->SerialNumber[i] =
296				    (char)((uint8_t) 0x30 + (uint8_t) j);
297			else
298				phba->SerialNumber[i] =
299				    (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
300		}
301	}
302
303	lpfc_read_config(phba, pmb);
304	pmb->vport = vport;
305	if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
306		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
307				"0453 Adapter failed to init, mbxCmd x%x "
308				"READ_CONFIG, mbxStatus x%x\n",
309				mb->mbxCommand, mb->mbxStatus);
310		phba->link_state = LPFC_HBA_ERROR;
311		mempool_free( pmb, phba->mbox_mem_pool);
312		return -EIO;
313	}
314
315	/* Reset the DFT_HBA_Q_DEPTH to the max xri  */
316	if (phba->cfg_hba_queue_depth > (mb->un.varRdConfig.max_xri+1))
317		phba->cfg_hba_queue_depth =
318			mb->un.varRdConfig.max_xri + 1;
319
320	phba->lmt = mb->un.varRdConfig.lmt;
321
322	/* Get the default values for Model Name and Description */
323	lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
324
325	if ((phba->cfg_link_speed > LINK_SPEED_10G)
326	    || ((phba->cfg_link_speed == LINK_SPEED_1G)
327		&& !(phba->lmt & LMT_1Gb))
328	    || ((phba->cfg_link_speed == LINK_SPEED_2G)
329		&& !(phba->lmt & LMT_2Gb))
330	    || ((phba->cfg_link_speed == LINK_SPEED_4G)
331		&& !(phba->lmt & LMT_4Gb))
332	    || ((phba->cfg_link_speed == LINK_SPEED_8G)
333		&& !(phba->lmt & LMT_8Gb))
334	    || ((phba->cfg_link_speed == LINK_SPEED_10G)
335		&& !(phba->lmt & LMT_10Gb))) {
336		/* Reset link speed to auto */
337		lpfc_printf_log(phba, KERN_WARNING, LOG_LINK_EVENT,
338			"1302 Invalid speed for this board: "
339			"Reset link speed to auto: x%x\n",
340			phba->cfg_link_speed);
341			phba->cfg_link_speed = LINK_SPEED_AUTO;
342	}
343
344	phba->link_state = LPFC_LINK_DOWN;
345
346	/* Only process IOCBs on ring 0 till hba_state is READY */
347	if (psli->ring[psli->extra_ring].cmdringaddr)
348		psli->ring[psli->extra_ring].flag |= LPFC_STOP_IOCB_EVENT;
349	if (psli->ring[psli->fcp_ring].cmdringaddr)
350		psli->ring[psli->fcp_ring].flag |= LPFC_STOP_IOCB_EVENT;
351	if (psli->ring[psli->next_ring].cmdringaddr)
352		psli->ring[psli->next_ring].flag |= LPFC_STOP_IOCB_EVENT;
353
354	/* Post receive buffers for desired rings */
355	if (phba->sli_rev != 3)
356		lpfc_post_rcv_buf(phba);
357
358	/* Enable appropriate host interrupts */
359	spin_lock_irq(&phba->hbalock);
360	status = readl(phba->HCregaddr);
361	status |= HC_MBINT_ENA | HC_ERINT_ENA | HC_LAINT_ENA;
362	if (psli->num_rings > 0)
363		status |= HC_R0INT_ENA;
364	if (psli->num_rings > 1)
365		status |= HC_R1INT_ENA;
366	if (psli->num_rings > 2)
367		status |= HC_R2INT_ENA;
368	if (psli->num_rings > 3)
369		status |= HC_R3INT_ENA;
370
371	if ((phba->cfg_poll & ENABLE_FCP_RING_POLLING) &&
372	    (phba->cfg_poll & DISABLE_FCP_RING_INT))
373		status &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
374
375	writel(status, phba->HCregaddr);
376	readl(phba->HCregaddr); /* flush */
377	spin_unlock_irq(&phba->hbalock);
378
379	/*
380	 * Setup the ring 0 (els)  timeout handler
381	 */
382	timeout = phba->fc_ratov << 1;
383	mod_timer(&vport->els_tmofunc, jiffies + HZ * timeout);
384	mod_timer(&phba->hb_tmofunc, jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
385	phba->hb_outstanding = 0;
386	phba->last_completion_time = jiffies;
387
388	lpfc_init_link(phba, pmb, phba->cfg_topology, phba->cfg_link_speed);
389	pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
390	pmb->vport = vport;
391	rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
392	lpfc_set_loopback_flag(phba);
393	if (rc != MBX_SUCCESS) {
394		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
395				"0454 Adapter failed to init, mbxCmd x%x "
396				"INIT_LINK, mbxStatus x%x\n",
397				mb->mbxCommand, mb->mbxStatus);
398
399		/* Clear all interrupt enable conditions */
400		writel(0, phba->HCregaddr);
401		readl(phba->HCregaddr); /* flush */
402		/* Clear all pending interrupts */
403		writel(0xffffffff, phba->HAregaddr);
404		readl(phba->HAregaddr); /* flush */
405
406		phba->link_state = LPFC_HBA_ERROR;
407		if (rc != MBX_BUSY)
408			mempool_free(pmb, phba->mbox_mem_pool);
409		return -EIO;
410	}
411	/* MBOX buffer will be freed in mbox compl */
412
413	return (0);
414}
415
416/************************************************************************/
417/*                                                                      */
418/*    lpfc_hba_down_prep                                                */
419/*    This routine will do LPFC uninitialization before the             */
420/*    HBA is reset when bringing down the SLI Layer. This will be       */
421/*    initialized as a SLI layer callback routine.                      */
422/*    This routine returns 0 on success. Any other return value         */
423/*    indicates an error.                                               */
424/*                                                                      */
425/************************************************************************/
426int
427lpfc_hba_down_prep(struct lpfc_hba *phba)
428{
429	/* Disable interrupts */
430	writel(0, phba->HCregaddr);
431	readl(phba->HCregaddr); /* flush */
432
433	lpfc_cleanup_discovery_resources(phba->pport);
434	return 0;
435}
436
437/************************************************************************/
438/*                                                                      */
439/*    lpfc_hba_down_post                                                */
440/*    This routine will do uninitialization after the HBA is reset      */
441/*    when bringing down the SLI Layer.                                 */
442/*    This routine returns 0 on success. Any other return value         */
443/*    indicates an error.                                               */
444/*                                                                      */
445/************************************************************************/
446int
447lpfc_hba_down_post(struct lpfc_hba *phba)
448{
449	struct lpfc_sli *psli = &phba->sli;
450	struct lpfc_sli_ring *pring;
451	struct lpfc_dmabuf *mp, *next_mp;
452	int i;
453
454	if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)
455		lpfc_sli_hbqbuf_free_all(phba);
456	else {
457		/* Cleanup preposted buffers on the ELS ring */
458		pring = &psli->ring[LPFC_ELS_RING];
459		list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
460			list_del(&mp->list);
461			pring->postbufq_cnt--;
462			lpfc_mbuf_free(phba, mp->virt, mp->phys);
463			kfree(mp);
464		}
465	}
466
467	for (i = 0; i < psli->num_rings; i++) {
468		pring = &psli->ring[i];
469		lpfc_sli_abort_iocb_ring(phba, pring);
470	}
471
472	return 0;
473}
474
475/* HBA heart beat timeout handler */
476void
477lpfc_hb_timeout(unsigned long ptr)
478{
479	struct lpfc_hba *phba;
480	unsigned long iflag;
481
482	phba = (struct lpfc_hba *)ptr;
483	spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
484	if (!(phba->pport->work_port_events & WORKER_HB_TMO))
485		phba->pport->work_port_events |= WORKER_HB_TMO;
486	spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
487
488	if (phba->work_wait)
489		wake_up(phba->work_wait);
490	return;
491}
492
493static void
494lpfc_hb_mbox_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
495{
496	unsigned long drvr_flag;
497
498	spin_lock_irqsave(&phba->hbalock, drvr_flag);
499	phba->hb_outstanding = 0;
500	spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
501
502	mempool_free(pmboxq, phba->mbox_mem_pool);
503	if (!(phba->pport->fc_flag & FC_OFFLINE_MODE) &&
504		!(phba->link_state == LPFC_HBA_ERROR) &&
505		!(phba->pport->load_flag & FC_UNLOADING))
506		mod_timer(&phba->hb_tmofunc,
507			jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
508	return;
509}
510
511void
512lpfc_hb_timeout_handler(struct lpfc_hba *phba)
513{
514	LPFC_MBOXQ_t *pmboxq;
515	int retval;
516	struct lpfc_sli *psli = &phba->sli;
517
518	if ((phba->link_state == LPFC_HBA_ERROR) ||
519		(phba->pport->load_flag & FC_UNLOADING) ||
520		(phba->pport->fc_flag & FC_OFFLINE_MODE))
521		return;
522
523	spin_lock_irq(&phba->pport->work_port_lock);
524	/* If the timer is already canceled do nothing */
525	if (!(phba->pport->work_port_events & WORKER_HB_TMO)) {
526		spin_unlock_irq(&phba->pport->work_port_lock);
527		return;
528	}
529
530	if (time_after(phba->last_completion_time + LPFC_HB_MBOX_INTERVAL * HZ,
531		jiffies)) {
532		spin_unlock_irq(&phba->pport->work_port_lock);
533		if (!phba->hb_outstanding)
534			mod_timer(&phba->hb_tmofunc,
535				jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
536		else
537			mod_timer(&phba->hb_tmofunc,
538				jiffies + HZ * LPFC_HB_MBOX_TIMEOUT);
539		return;
540	}
541	spin_unlock_irq(&phba->pport->work_port_lock);
542
543	/* If there is no heart beat outstanding, issue a heartbeat command */
544	if (!phba->hb_outstanding) {
545		pmboxq = mempool_alloc(phba->mbox_mem_pool,GFP_KERNEL);
546		if (!pmboxq) {
547			mod_timer(&phba->hb_tmofunc,
548				jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
549			return;
550		}
551
552		lpfc_heart_beat(phba, pmboxq);
553		pmboxq->mbox_cmpl = lpfc_hb_mbox_cmpl;
554		pmboxq->vport = phba->pport;
555		retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
556
557		if (retval != MBX_BUSY && retval != MBX_SUCCESS) {
558			mempool_free(pmboxq, phba->mbox_mem_pool);
559			mod_timer(&phba->hb_tmofunc,
560				jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
561			return;
562		}
563		mod_timer(&phba->hb_tmofunc,
564			jiffies + HZ * LPFC_HB_MBOX_TIMEOUT);
565		phba->hb_outstanding = 1;
566		return;
567	} else {
568		/*
569		 * If heart beat timeout called with hb_outstanding set we
570		 * need to take the HBA offline.
571		 */
572		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
573				"0459 Adapter heartbeat failure, taking "
574				"this port offline.\n");
575
576		spin_lock_irq(&phba->hbalock);
577		psli->sli_flag &= ~LPFC_SLI2_ACTIVE;
578		spin_unlock_irq(&phba->hbalock);
579
580		lpfc_offline_prep(phba);
581		lpfc_offline(phba);
582		lpfc_unblock_mgmt_io(phba);
583		phba->link_state = LPFC_HBA_ERROR;
584		lpfc_hba_down_post(phba);
585	}
586}
587
588/************************************************************************/
589/*                                                                      */
590/*    lpfc_handle_eratt                                                 */
591/*    This routine will handle processing a Host Attention              */
592/*    Error Status event. This will be initialized                      */
593/*    as a SLI layer callback routine.                                  */
594/*                                                                      */
595/************************************************************************/
596void
597lpfc_handle_eratt(struct lpfc_hba *phba)
598{
599	struct lpfc_vport *vport = phba->pport;
600	struct lpfc_sli   *psli = &phba->sli;
601	struct lpfc_sli_ring  *pring;
602	struct lpfc_vport **vports;
603	uint32_t event_data;
604	struct Scsi_Host  *shost;
605	int i;
606
607	/* If the pci channel is offline, ignore possible errors,
608	 * since we cannot communicate with the pci card anyway. */
609	if (pci_channel_offline(phba->pcidev))
610		return;
611
612	if (phba->work_hs & HS_FFER6 ||
613	    phba->work_hs & HS_FFER5) {
614		/* Re-establishing Link */
615		lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
616				"1301 Re-establishing Link "
617				"Data: x%x x%x x%x\n",
618				phba->work_hs,
619				phba->work_status[0], phba->work_status[1]);
620		vports = lpfc_create_vport_work_array(phba);
621		if (vports != NULL)
622			for(i = 0;
623			    i < LPFC_MAX_VPORTS && vports[i] != NULL;
624			    i++){
625				shost = lpfc_shost_from_vport(vports[i]);
626				spin_lock_irq(shost->host_lock);
627				vports[i]->fc_flag |= FC_ESTABLISH_LINK;
628				spin_unlock_irq(shost->host_lock);
629			}
630		lpfc_destroy_vport_work_array(vports);
631		spin_lock_irq(&phba->hbalock);
632		psli->sli_flag &= ~LPFC_SLI2_ACTIVE;
633		spin_unlock_irq(&phba->hbalock);
634
635		/*
636		* Firmware stops when it triggled erratt with HS_FFER6.
637		* That could cause the I/Os dropped by the firmware.
638		* Error iocb (I/O) on txcmplq and let the SCSI layer
639		* retry it after re-establishing link.
640		*/
641		pring = &psli->ring[psli->fcp_ring];
642		lpfc_sli_abort_iocb_ring(phba, pring);
643
644
645		/*
646		 * There was a firmware error.  Take the hba offline and then
647		 * attempt to restart it.
648		 */
649		lpfc_offline_prep(phba);
650		lpfc_offline(phba);
651		lpfc_sli_brdrestart(phba);
652		if (lpfc_online(phba) == 0) {	/* Initialize the HBA */
653			mod_timer(&phba->fc_estabtmo, jiffies + HZ * 60);
654			lpfc_unblock_mgmt_io(phba);
655			return;
656		}
657		lpfc_unblock_mgmt_io(phba);
658	} else {
659		/* The if clause above forces this code path when the status
660		 * failure is a value other than FFER6.  Do not call the offline
661		 *  twice. This is the adapter hardware error path.
662		 */
663		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
664				"0457 Adapter Hardware Error "
665				"Data: x%x x%x x%x\n",
666				phba->work_hs,
667				phba->work_status[0], phba->work_status[1]);
668
669		event_data = FC_REG_DUMP_EVENT;
670		shost = lpfc_shost_from_vport(vport);
671		fc_host_post_vendor_event(shost, fc_get_event_number(),
672				sizeof(event_data), (char *) &event_data,
673				SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
674
675		spin_lock_irq(&phba->hbalock);
676		psli->sli_flag &= ~LPFC_SLI2_ACTIVE;
677		spin_unlock_irq(&phba->hbalock);
678		lpfc_offline_prep(phba);
679		lpfc_offline(phba);
680		lpfc_unblock_mgmt_io(phba);
681		phba->link_state = LPFC_HBA_ERROR;
682		lpfc_hba_down_post(phba);
683	}
684}
685
686/************************************************************************/
687/*                                                                      */
688/*    lpfc_handle_latt                                                  */
689/*    This routine will handle processing a Host Attention              */
690/*    Link Status event. This will be initialized                       */
691/*    as a SLI layer callback routine.                                  */
692/*                                                                      */
693/************************************************************************/
694void
695lpfc_handle_latt(struct lpfc_hba *phba)
696{
697	struct lpfc_vport *vport = phba->pport;
698	struct lpfc_sli   *psli = &phba->sli;
699	LPFC_MBOXQ_t *pmb;
700	volatile uint32_t control;
701	struct lpfc_dmabuf *mp;
702	int rc = -ENOMEM;
703
704	pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
705	if (!pmb)
706		goto lpfc_handle_latt_err_exit;
707
708	mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
709	if (!mp)
710		goto lpfc_handle_latt_free_pmb;
711
712	mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
713	if (!mp->virt)
714		goto lpfc_handle_latt_free_mp;
715
716	rc = -EIO;
717
718	/* Cleanup any outstanding ELS commands */
719	lpfc_els_flush_all_cmd(phba);
720
721	psli->slistat.link_event++;
722	lpfc_read_la(phba, pmb, mp);
723	pmb->mbox_cmpl = lpfc_mbx_cmpl_read_la;
724	pmb->vport = vport;
725	rc = lpfc_sli_issue_mbox (phba, pmb, (MBX_NOWAIT | MBX_STOP_IOCB));
726	if (rc == MBX_NOT_FINISHED)
727		goto lpfc_handle_latt_free_mbuf;
728
729	/* Clear Link Attention in HA REG */
730	spin_lock_irq(&phba->hbalock);
731	writel(HA_LATT, phba->HAregaddr);
732	readl(phba->HAregaddr); /* flush */
733	spin_unlock_irq(&phba->hbalock);
734
735	return;
736
737lpfc_handle_latt_free_mbuf:
738	lpfc_mbuf_free(phba, mp->virt, mp->phys);
739lpfc_handle_latt_free_mp:
740	kfree(mp);
741lpfc_handle_latt_free_pmb:
742	mempool_free(pmb, phba->mbox_mem_pool);
743lpfc_handle_latt_err_exit:
744	/* Enable Link attention interrupts */
745	spin_lock_irq(&phba->hbalock);
746	psli->sli_flag |= LPFC_PROCESS_LA;
747	control = readl(phba->HCregaddr);
748	control |= HC_LAINT_ENA;
749	writel(control, phba->HCregaddr);
750	readl(phba->HCregaddr); /* flush */
751
752	/* Clear Link Attention in HA REG */
753	writel(HA_LATT, phba->HAregaddr);
754	readl(phba->HAregaddr); /* flush */
755	spin_unlock_irq(&phba->hbalock);
756	lpfc_linkdown(phba);
757	phba->link_state = LPFC_HBA_ERROR;
758
759	/* The other case is an error from issue_mbox */
760	if (rc == -ENOMEM)
761		lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
762			        "0300 READ_LA: no buffers\n");
763
764	return;
765}
766
767/************************************************************************/
768/*                                                                      */
769/*   lpfc_parse_vpd                                                     */
770/*   This routine will parse the VPD data                               */
771/*                                                                      */
772/************************************************************************/
773static int
774lpfc_parse_vpd(struct lpfc_hba *phba, uint8_t *vpd, int len)
775{
776	uint8_t lenlo, lenhi;
777	int Length;
778	int i, j;
779	int finished = 0;
780	int index = 0;
781
782	if (!vpd)
783		return 0;
784
785	/* Vital Product */
786	lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
787			"0455 Vital Product Data: x%x x%x x%x x%x\n",
788			(uint32_t) vpd[0], (uint32_t) vpd[1], (uint32_t) vpd[2],
789			(uint32_t) vpd[3]);
790	while (!finished && (index < (len - 4))) {
791		switch (vpd[index]) {
792		case 0x82:
793		case 0x91:
794			index += 1;
795			lenlo = vpd[index];
796			index += 1;
797			lenhi = vpd[index];
798			index += 1;
799			i = ((((unsigned short)lenhi) << 8) + lenlo);
800			index += i;
801			break;
802		case 0x90:
803			index += 1;
804			lenlo = vpd[index];
805			index += 1;
806			lenhi = vpd[index];
807			index += 1;
808			Length = ((((unsigned short)lenhi) << 8) + lenlo);
809			if (Length > len - index)
810				Length = len - index;
811			while (Length > 0) {
812			/* Look for Serial Number */
813			if ((vpd[index] == 'S') && (vpd[index+1] == 'N')) {
814				index += 2;
815				i = vpd[index];
816				index += 1;
817				j = 0;
818				Length -= (3+i);
819				while(i--) {
820					phba->SerialNumber[j++] = vpd[index++];
821					if (j == 31)
822						break;
823				}
824				phba->SerialNumber[j] = 0;
825				continue;
826			}
827			else if ((vpd[index] == 'V') && (vpd[index+1] == '1')) {
828				phba->vpd_flag |= VPD_MODEL_DESC;
829				index += 2;
830				i = vpd[index];
831				index += 1;
832				j = 0;
833				Length -= (3+i);
834				while(i--) {
835					phba->ModelDesc[j++] = vpd[index++];
836					if (j == 255)
837						break;
838				}
839				phba->ModelDesc[j] = 0;
840				continue;
841			}
842			else if ((vpd[index] == 'V') && (vpd[index+1] == '2')) {
843				phba->vpd_flag |= VPD_MODEL_NAME;
844				index += 2;
845				i = vpd[index];
846				index += 1;
847				j = 0;
848				Length -= (3+i);
849				while(i--) {
850					phba->ModelName[j++] = vpd[index++];
851					if (j == 79)
852						break;
853				}
854				phba->ModelName[j] = 0;
855				continue;
856			}
857			else if ((vpd[index] == 'V') && (vpd[index+1] == '3')) {
858				phba->vpd_flag |= VPD_PROGRAM_TYPE;
859				index += 2;
860				i = vpd[index];
861				index += 1;
862				j = 0;
863				Length -= (3+i);
864				while(i--) {
865					phba->ProgramType[j++] = vpd[index++];
866					if (j == 255)
867						break;
868				}
869				phba->ProgramType[j] = 0;
870				continue;
871			}
872			else if ((vpd[index] == 'V') && (vpd[index+1] == '4')) {
873				phba->vpd_flag |= VPD_PORT;
874				index += 2;
875				i = vpd[index];
876				index += 1;
877				j = 0;
878				Length -= (3+i);
879				while(i--) {
880				phba->Port[j++] = vpd[index++];
881				if (j == 19)
882					break;
883				}
884				phba->Port[j] = 0;
885				continue;
886			}
887			else {
888				index += 2;
889				i = vpd[index];
890				index += 1;
891				index += i;
892				Length -= (3 + i);
893			}
894		}
895		finished = 0;
896		break;
897		case 0x78:
898			finished = 1;
899			break;
900		default:
901			index ++;
902			break;
903		}
904	}
905
906	return(1);
907}
908
909static void
910lpfc_get_hba_model_desc(struct lpfc_hba *phba, uint8_t *mdp, uint8_t *descp)
911{
912	lpfc_vpd_t *vp;
913	uint16_t dev_id = phba->pcidev->device;
914	int max_speed;
915	struct {
916		char * name;
917		int    max_speed;
918		char * bus;
919	} m = {"<Unknown>", 0, ""};
920
921	if (mdp && mdp[0] != '\0'
922		&& descp && descp[0] != '\0')
923		return;
924
925	if (phba->lmt & LMT_10Gb)
926		max_speed = 10;
927	else if (phba->lmt & LMT_8Gb)
928		max_speed = 8;
929	else if (phba->lmt & LMT_4Gb)
930		max_speed = 4;
931	else if (phba->lmt & LMT_2Gb)
932		max_speed = 2;
933	else
934		max_speed = 1;
935
936	vp = &phba->vpd;
937
938	switch (dev_id) {
939	case PCI_DEVICE_ID_FIREFLY:
940		m = (typeof(m)){"LP6000", max_speed, "PCI"};
941		break;
942	case PCI_DEVICE_ID_SUPERFLY:
943		if (vp->rev.biuRev >= 1 && vp->rev.biuRev <= 3)
944			m = (typeof(m)){"LP7000", max_speed,  "PCI"};
945		else
946			m = (typeof(m)){"LP7000E", max_speed, "PCI"};
947		break;
948	case PCI_DEVICE_ID_DRAGONFLY:
949		m = (typeof(m)){"LP8000", max_speed, "PCI"};
950		break;
951	case PCI_DEVICE_ID_CENTAUR:
952		if (FC_JEDEC_ID(vp->rev.biuRev) == CENTAUR_2G_JEDEC_ID)
953			m = (typeof(m)){"LP9002", max_speed, "PCI"};
954		else
955			m = (typeof(m)){"LP9000", max_speed, "PCI"};
956		break;
957	case PCI_DEVICE_ID_RFLY:
958		m = (typeof(m)){"LP952", max_speed, "PCI"};
959		break;
960	case PCI_DEVICE_ID_PEGASUS:
961		m = (typeof(m)){"LP9802", max_speed, "PCI-X"};
962		break;
963	case PCI_DEVICE_ID_THOR:
964		m = (typeof(m)){"LP10000", max_speed, "PCI-X"};
965		break;
966	case PCI_DEVICE_ID_VIPER:
967		m = (typeof(m)){"LPX1000", max_speed,  "PCI-X"};
968		break;
969	case PCI_DEVICE_ID_PFLY:
970		m = (typeof(m)){"LP982", max_speed, "PCI-X"};
971		break;
972	case PCI_DEVICE_ID_TFLY:
973		m = (typeof(m)){"LP1050", max_speed, "PCI-X"};
974		break;
975	case PCI_DEVICE_ID_HELIOS:
976		m = (typeof(m)){"LP11000", max_speed, "PCI-X2"};
977		break;
978	case PCI_DEVICE_ID_HELIOS_SCSP:
979		m = (typeof(m)){"LP11000-SP", max_speed, "PCI-X2"};
980		break;
981	case PCI_DEVICE_ID_HELIOS_DCSP:
982		m = (typeof(m)){"LP11002-SP", max_speed, "PCI-X2"};
983		break;
984	case PCI_DEVICE_ID_NEPTUNE:
985		m = (typeof(m)){"LPe1000", max_speed, "PCIe"};
986		break;
987	case PCI_DEVICE_ID_NEPTUNE_SCSP:
988		m = (typeof(m)){"LPe1000-SP", max_speed, "PCIe"};
989		break;
990	case PCI_DEVICE_ID_NEPTUNE_DCSP:
991		m = (typeof(m)){"LPe1002-SP", max_speed, "PCIe"};
992		break;
993	case PCI_DEVICE_ID_BMID:
994		m = (typeof(m)){"LP1150", max_speed, "PCI-X2"};
995		break;
996	case PCI_DEVICE_ID_BSMB:
997		m = (typeof(m)){"LP111", max_speed, "PCI-X2"};
998		break;
999	case PCI_DEVICE_ID_ZEPHYR:
1000		m = (typeof(m)){"LPe11000", max_speed, "PCIe"};
1001		break;
1002	case PCI_DEVICE_ID_ZEPHYR_SCSP:
1003		m = (typeof(m)){"LPe11000", max_speed, "PCIe"};
1004		break;
1005	case PCI_DEVICE_ID_ZEPHYR_DCSP:
1006		m = (typeof(m)){"LPe11002-SP", max_speed, "PCIe"};
1007		break;
1008	case PCI_DEVICE_ID_ZMID:
1009		m = (typeof(m)){"LPe1150", max_speed, "PCIe"};
1010		break;
1011	case PCI_DEVICE_ID_ZSMB:
1012		m = (typeof(m)){"LPe111", max_speed, "PCIe"};
1013		break;
1014	case PCI_DEVICE_ID_LP101:
1015		m = (typeof(m)){"LP101", max_speed, "PCI-X"};
1016		break;
1017	case PCI_DEVICE_ID_LP10000S:
1018		m = (typeof(m)){"LP10000-S", max_speed, "PCI"};
1019		break;
1020	case PCI_DEVICE_ID_LP11000S:
1021		m = (typeof(m)){"LP11000-S", max_speed,
1022			"PCI-X2"};
1023		break;
1024	case PCI_DEVICE_ID_LPE11000S:
1025		m = (typeof(m)){"LPe11000-S", max_speed,
1026			"PCIe"};
1027		break;
1028	case PCI_DEVICE_ID_SAT:
1029		m = (typeof(m)){"LPe12000", max_speed, "PCIe"};
1030		break;
1031	case PCI_DEVICE_ID_SAT_MID:
1032		m = (typeof(m)){"LPe1250", max_speed, "PCIe"};
1033		break;
1034	case PCI_DEVICE_ID_SAT_SMB:
1035		m = (typeof(m)){"LPe121", max_speed, "PCIe"};
1036		break;
1037	case PCI_DEVICE_ID_SAT_DCSP:
1038		m = (typeof(m)){"LPe12002-SP", max_speed, "PCIe"};
1039		break;
1040	case PCI_DEVICE_ID_SAT_SCSP:
1041		m = (typeof(m)){"LPe12000-SP", max_speed, "PCIe"};
1042		break;
1043	case PCI_DEVICE_ID_SAT_S:
1044		m = (typeof(m)){"LPe12000-S", max_speed, "PCIe"};
1045		break;
1046	default:
1047		m = (typeof(m)){ NULL };
1048		break;
1049	}
1050
1051	if (mdp && mdp[0] == '\0')
1052		snprintf(mdp, 79,"%s", m.name);
1053	if (descp && descp[0] == '\0')
1054		snprintf(descp, 255,
1055			 "Emulex %s %dGb %s Fibre Channel Adapter",
1056			 m.name, m.max_speed, m.bus);
1057}
1058
1059/**************************************************/
1060/*   lpfc_post_buffer                             */
1061/*                                                */
1062/*   This routine will post count buffers to the  */
1063/*   ring with the QUE_RING_BUF_CN command. This  */
1064/*   allows 3 buffers / command to be posted.     */
1065/*   Returns the number of buffers NOT posted.    */
1066/**************************************************/
1067int
1068lpfc_post_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring, int cnt,
1069		 int type)
1070{
1071	IOCB_t *icmd;
1072	struct lpfc_iocbq *iocb;
1073	struct lpfc_dmabuf *mp1, *mp2;
1074
1075	cnt += pring->missbufcnt;
1076
1077	/* While there are buffers to post */
1078	while (cnt > 0) {
1079		/* Allocate buffer for  command iocb */
1080		iocb = lpfc_sli_get_iocbq(phba);
1081		if (iocb == NULL) {
1082			pring->missbufcnt = cnt;
1083			return cnt;
1084		}
1085		icmd = &iocb->iocb;
1086
1087		/* 2 buffers can be posted per command */
1088		/* Allocate buffer to post */
1089		mp1 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
1090		if (mp1)
1091		    mp1->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
1092						&mp1->phys);
1093		if (mp1 == 0 || mp1->virt == 0) {
1094			kfree(mp1);
1095			lpfc_sli_release_iocbq(phba, iocb);
1096			pring->missbufcnt = cnt;
1097			return cnt;
1098		}
1099
1100		INIT_LIST_HEAD(&mp1->list);
1101		/* Allocate buffer to post */
1102		if (cnt > 1) {
1103			mp2 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
1104			if (mp2)
1105				mp2->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
1106							    &mp2->phys);
1107			if (mp2 == 0 || mp2->virt == 0) {
1108				kfree(mp2);
1109				lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
1110				kfree(mp1);
1111				lpfc_sli_release_iocbq(phba, iocb);
1112				pring->missbufcnt = cnt;
1113				return cnt;
1114			}
1115
1116			INIT_LIST_HEAD(&mp2->list);
1117		} else {
1118			mp2 = NULL;
1119		}
1120
1121		icmd->un.cont64[0].addrHigh = putPaddrHigh(mp1->phys);
1122		icmd->un.cont64[0].addrLow = putPaddrLow(mp1->phys);
1123		icmd->un.cont64[0].tus.f.bdeSize = FCELSSIZE;
1124		icmd->ulpBdeCount = 1;
1125		cnt--;
1126		if (mp2) {
1127			icmd->un.cont64[1].addrHigh = putPaddrHigh(mp2->phys);
1128			icmd->un.cont64[1].addrLow = putPaddrLow(mp2->phys);
1129			icmd->un.cont64[1].tus.f.bdeSize = FCELSSIZE;
1130			cnt--;
1131			icmd->ulpBdeCount = 2;
1132		}
1133
1134		icmd->ulpCommand = CMD_QUE_RING_BUF64_CN;
1135		icmd->ulpLe = 1;
1136
1137		if (lpfc_sli_issue_iocb(phba, pring, iocb, 0) == IOCB_ERROR) {
1138			lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
1139			kfree(mp1);
1140			cnt++;
1141			if (mp2) {
1142				lpfc_mbuf_free(phba, mp2->virt, mp2->phys);
1143				kfree(mp2);
1144				cnt++;
1145			}
1146			lpfc_sli_release_iocbq(phba, iocb);
1147			pring->missbufcnt = cnt;
1148			return cnt;
1149		}
1150		lpfc_sli_ringpostbuf_put(phba, pring, mp1);
1151		if (mp2)
1152			lpfc_sli_ringpostbuf_put(phba, pring, mp2);
1153	}
1154	pring->missbufcnt = 0;
1155	return 0;
1156}
1157
1158/************************************************************************/
1159/*                                                                      */
1160/*   lpfc_post_rcv_buf                                                  */
1161/*   This routine post initial rcv buffers to the configured rings      */
1162/*                                                                      */
1163/************************************************************************/
1164static int
1165lpfc_post_rcv_buf(struct lpfc_hba *phba)
1166{
1167	struct lpfc_sli *psli = &phba->sli;
1168
1169	/* Ring 0, ELS / CT buffers */
1170	lpfc_post_buffer(phba, &psli->ring[LPFC_ELS_RING], LPFC_BUF_RING0, 1);
1171	/* Ring 2 - FCP no buffers needed */
1172
1173	return 0;
1174}
1175
1176#define S(N,V) (((V)<<(N))|((V)>>(32-(N))))
1177
1178/************************************************************************/
1179/*                                                                      */
1180/*   lpfc_sha_init                                                      */
1181/*                                                                      */
1182/************************************************************************/
1183static void
1184lpfc_sha_init(uint32_t * HashResultPointer)
1185{
1186	HashResultPointer[0] = 0x67452301;
1187	HashResultPointer[1] = 0xEFCDAB89;
1188	HashResultPointer[2] = 0x98BADCFE;
1189	HashResultPointer[3] = 0x10325476;
1190	HashResultPointer[4] = 0xC3D2E1F0;
1191}
1192
1193/************************************************************************/
1194/*                                                                      */
1195/*   lpfc_sha_iterate                                                   */
1196/*                                                                      */
1197/************************************************************************/
1198static void
1199lpfc_sha_iterate(uint32_t * HashResultPointer, uint32_t * HashWorkingPointer)
1200{
1201	int t;
1202	uint32_t TEMP;
1203	uint32_t A, B, C, D, E;
1204	t = 16;
1205	do {
1206		HashWorkingPointer[t] =
1207		    S(1,
1208		      HashWorkingPointer[t - 3] ^ HashWorkingPointer[t -
1209								     8] ^
1210		      HashWorkingPointer[t - 14] ^ HashWorkingPointer[t - 16]);
1211	} while (++t <= 79);
1212	t = 0;
1213	A = HashResultPointer[0];
1214	B = HashResultPointer[1];
1215	C = HashResultPointer[2];
1216	D = HashResultPointer[3];
1217	E = HashResultPointer[4];
1218
1219	do {
1220		if (t < 20) {
1221			TEMP = ((B & C) | ((~B) & D)) + 0x5A827999;
1222		} else if (t < 40) {
1223			TEMP = (B ^ C ^ D) + 0x6ED9EBA1;
1224		} else if (t < 60) {
1225			TEMP = ((B & C) | (B & D) | (C & D)) + 0x8F1BBCDC;
1226		} else {
1227			TEMP = (B ^ C ^ D) + 0xCA62C1D6;
1228		}
1229		TEMP += S(5, A) + E + HashWorkingPointer[t];
1230		E = D;
1231		D = C;
1232		C = S(30, B);
1233		B = A;
1234		A = TEMP;
1235	} while (++t <= 79);
1236
1237	HashResultPointer[0] += A;
1238	HashResultPointer[1] += B;
1239	HashResultPointer[2] += C;
1240	HashResultPointer[3] += D;
1241	HashResultPointer[4] += E;
1242
1243}
1244
1245/************************************************************************/
1246/*                                                                      */
1247/*   lpfc_challenge_key                                                 */
1248/*                                                                      */
1249/************************************************************************/
1250static void
1251lpfc_challenge_key(uint32_t * RandomChallenge, uint32_t * HashWorking)
1252{
1253	*HashWorking = (*RandomChallenge ^ *HashWorking);
1254}
1255
1256/************************************************************************/
1257/*                                                                      */
1258/*   lpfc_hba_init                                                      */
1259/*                                                                      */
1260/************************************************************************/
1261void
1262lpfc_hba_init(struct lpfc_hba *phba, uint32_t *hbainit)
1263{
1264	int t;
1265	uint32_t *HashWorking;
1266	uint32_t *pwwnn = (uint32_t *) phba->wwnn;
1267
1268	HashWorking = kmalloc(80 * sizeof(uint32_t), GFP_KERNEL);
1269	if (!HashWorking)
1270		return;
1271
1272	memset(HashWorking, 0, (80 * sizeof(uint32_t)));
1273	HashWorking[0] = HashWorking[78] = *pwwnn++;
1274	HashWorking[1] = HashWorking[79] = *pwwnn;
1275
1276	for (t = 0; t < 7; t++)
1277		lpfc_challenge_key(phba->RandomData + t, HashWorking + t);
1278
1279	lpfc_sha_init(hbainit);
1280	lpfc_sha_iterate(hbainit, HashWorking);
1281	kfree(HashWorking);
1282}
1283
1284static void
1285lpfc_cleanup(struct lpfc_vport *vport)
1286{
1287	struct lpfc_nodelist *ndlp, *next_ndlp;
1288
1289	/* clean up phba - lpfc specific */
1290	lpfc_can_disctmo(vport);
1291	list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp)
1292		lpfc_nlp_put(ndlp);
1293	return;
1294}
1295
1296static void
1297lpfc_establish_link_tmo(unsigned long ptr)
1298{
1299	struct lpfc_hba   *phba = (struct lpfc_hba *) ptr;
1300	struct lpfc_vport **vports;
1301	unsigned long iflag;
1302	int i;
1303
1304	/* Re-establishing Link, timer expired */
1305	lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
1306			"1300 Re-establishing Link, timer expired "
1307			"Data: x%x x%x\n",
1308			phba->pport->fc_flag, phba->pport->port_state);
1309	vports = lpfc_create_vport_work_array(phba);
1310	if (vports != NULL)
1311		for(i = 0; i < LPFC_MAX_VPORTS && vports[i] != NULL; i++) {
1312			struct Scsi_Host *shost;
1313			shost = lpfc_shost_from_vport(vports[i]);
1314			spin_lock_irqsave(shost->host_lock, iflag);
1315			vports[i]->fc_flag &= ~FC_ESTABLISH_LINK;
1316			spin_unlock_irqrestore(shost->host_lock, iflag);
1317		}
1318	lpfc_destroy_vport_work_array(vports);
1319}
1320
1321void
1322lpfc_stop_vport_timers(struct lpfc_vport *vport)
1323{
1324	del_timer_sync(&vport->els_tmofunc);
1325	del_timer_sync(&vport->fc_fdmitmo);
1326	lpfc_can_disctmo(vport);
1327	return;
1328}
1329
1330static void
1331lpfc_stop_phba_timers(struct lpfc_hba *phba)
1332{
1333	del_timer_sync(&phba->fcp_poll_timer);
1334	del_timer_sync(&phba->fc_estabtmo);
1335	lpfc_stop_vport_timers(phba->pport);
1336	del_timer_sync(&phba->sli.mbox_tmo);
1337	del_timer_sync(&phba->fabric_block_timer);
1338	phba->hb_outstanding = 0;
1339	del_timer_sync(&phba->hb_tmofunc);
1340	return;
1341}
1342
1343int
1344lpfc_online(struct lpfc_hba *phba)
1345{
1346	struct lpfc_vport *vport = phba->pport;
1347	struct lpfc_vport **vports;
1348	int i;
1349
1350	if (!phba)
1351		return 0;
1352
1353	if (!(vport->fc_flag & FC_OFFLINE_MODE))
1354		return 0;
1355
1356	lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
1357			"0458 Bring Adapter online\n");
1358
1359	lpfc_block_mgmt_io(phba);
1360
1361	if (!lpfc_sli_queue_setup(phba)) {
1362		lpfc_unblock_mgmt_io(phba);
1363		return 1;
1364	}
1365
1366	if (lpfc_sli_hba_setup(phba)) {	/* Initialize the HBA */
1367		lpfc_unblock_mgmt_io(phba);
1368		return 1;
1369	}
1370
1371	vports = lpfc_create_vport_work_array(phba);
1372	if (vports != NULL)
1373		for(i = 0; i < LPFC_MAX_VPORTS && vports[i] != NULL; i++) {
1374			struct Scsi_Host *shost;
1375			shost = lpfc_shost_from_vport(vports[i]);
1376			spin_lock_irq(shost->host_lock);
1377			vports[i]->fc_flag &= ~FC_OFFLINE_MODE;
1378			if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
1379				vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
1380			spin_unlock_irq(shost->host_lock);
1381		}
1382		lpfc_destroy_vport_work_array(vports);
1383
1384	lpfc_unblock_mgmt_io(phba);
1385	return 0;
1386}
1387
1388void
1389lpfc_block_mgmt_io(struct lpfc_hba * phba)
1390{
1391	unsigned long iflag;
1392
1393	spin_lock_irqsave(&phba->hbalock, iflag);
1394	phba->sli.sli_flag |= LPFC_BLOCK_MGMT_IO;
1395	spin_unlock_irqrestore(&phba->hbalock, iflag);
1396}
1397
1398void
1399lpfc_unblock_mgmt_io(struct lpfc_hba * phba)
1400{
1401	unsigned long iflag;
1402
1403	spin_lock_irqsave(&phba->hbalock, iflag);
1404	phba->sli.sli_flag &= ~LPFC_BLOCK_MGMT_IO;
1405	spin_unlock_irqrestore(&phba->hbalock, iflag);
1406}
1407
1408void
1409lpfc_offline_prep(struct lpfc_hba * phba)
1410{
1411	struct lpfc_vport *vport = phba->pport;
1412	struct lpfc_nodelist  *ndlp, *next_ndlp;
1413
1414	if (vport->fc_flag & FC_OFFLINE_MODE)
1415		return;
1416
1417	lpfc_block_mgmt_io(phba);
1418
1419	lpfc_linkdown(phba);
1420
1421	/* Issue an unreg_login to all nodes */
1422	list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp)
1423		if (ndlp->nlp_state != NLP_STE_UNUSED_NODE)
1424			lpfc_unreg_rpi(vport, ndlp);
1425
1426	lpfc_sli_flush_mbox_queue(phba);
1427}
1428
1429void
1430lpfc_offline(struct lpfc_hba *phba)
1431{
1432	struct Scsi_Host  *shost;
1433	struct lpfc_vport **vports;
1434	int i;
1435
1436	if (phba->pport->fc_flag & FC_OFFLINE_MODE)
1437		return;
1438
1439	/* stop all timers associated with this hba */
1440	lpfc_stop_phba_timers(phba);
1441	vports = lpfc_create_vport_work_array(phba);
1442	if (vports != NULL)
1443		for(i = 0; i < LPFC_MAX_VPORTS && vports[i] != NULL; i++)
1444			lpfc_stop_vport_timers(vports[i]);
1445	lpfc_destroy_vport_work_array(vports);
1446	lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
1447			"0460 Bring Adapter offline\n");
1448	/* Bring down the SLI Layer and cleanup.  The HBA is offline
1449	   now.  */
1450	lpfc_sli_hba_down(phba);
1451	spin_lock_irq(&phba->hbalock);
1452	phba->work_ha = 0;
1453	spin_unlock_irq(&phba->hbalock);
1454	vports = lpfc_create_vport_work_array(phba);
1455	if (vports != NULL)
1456		for(i = 0; i < LPFC_MAX_VPORTS && vports[i] != NULL; i++) {
1457			shost = lpfc_shost_from_vport(vports[i]);
1458			lpfc_cleanup(vports[i]);
1459			spin_lock_irq(shost->host_lock);
1460			vports[i]->work_port_events = 0;
1461			vports[i]->fc_flag |= FC_OFFLINE_MODE;
1462			spin_unlock_irq(shost->host_lock);
1463		}
1464	lpfc_destroy_vport_work_array(vports);
1465}
1466
1467/******************************************************************************
1468* Function name: lpfc_scsi_free
1469*
1470* Description: Called from lpfc_pci_remove_one free internal driver resources
1471*
1472******************************************************************************/
1473static int
1474lpfc_scsi_free(struct lpfc_hba *phba)
1475{
1476	struct lpfc_scsi_buf *sb, *sb_next;
1477	struct lpfc_iocbq *io, *io_next;
1478
1479	spin_lock_irq(&phba->hbalock);
1480	/* Release all the lpfc_scsi_bufs maintained by this host. */
1481	list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list, list) {
1482		list_del(&sb->list);
1483		pci_pool_free(phba->lpfc_scsi_dma_buf_pool, sb->data,
1484			      sb->dma_handle);
1485		kfree(sb);
1486		phba->total_scsi_bufs--;
1487	}
1488
1489	/* Release all the lpfc_iocbq entries maintained by this host. */
1490	list_for_each_entry_safe(io, io_next, &phba->lpfc_iocb_list, list) {
1491		list_del(&io->list);
1492		kfree(io);
1493		phba->total_iocbq_bufs--;
1494	}
1495
1496	spin_unlock_irq(&phba->hbalock);
1497
1498	return 0;
1499}
1500
1501struct lpfc_vport *
1502lpfc_create_port(struct lpfc_hba *phba, int instance, struct device *dev)
1503{
1504	struct lpfc_vport *vport;
1505	struct Scsi_Host  *shost;
1506	int error = 0;
1507
1508	if (dev != &phba->pcidev->dev)
1509		shost = scsi_host_alloc(&lpfc_vport_template,
1510					sizeof(struct lpfc_vport));
1511	else
1512		shost = scsi_host_alloc(&lpfc_template,
1513					sizeof(struct lpfc_vport));
1514	if (!shost)
1515		goto out;
1516
1517	vport = (struct lpfc_vport *) shost->hostdata;
1518	vport->phba = phba;
1519
1520	vport->load_flag |= FC_LOADING;
1521	vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
1522
1523	lpfc_get_vport_cfgparam(vport);
1524	shost->unique_id = instance;
1525	shost->max_id = LPFC_MAX_TARGET;
1526	shost->max_lun = vport->cfg_max_luns;
1527	shost->this_id = -1;
1528	shost->max_cmd_len = 16;
1529	/*
1530	 * Set initial can_queue value since 0 is no longer supported and
1531	 * scsi_add_host will fail. This will be adjusted later based on the
1532	 * max xri value determined in hba setup.
1533	 */
1534	shost->can_queue = phba->cfg_hba_queue_depth - 10;
1535	if (dev != &phba->pcidev->dev) {
1536		shost->transportt = lpfc_vport_transport_template;
1537		vport->port_type = LPFC_NPIV_PORT;
1538	} else {
1539		shost->transportt = lpfc_transport_template;
1540		vport->port_type = LPFC_PHYSICAL_PORT;
1541	}
1542
1543	/* Initialize all internally managed lists. */
1544	INIT_LIST_HEAD(&vport->fc_nodes);
1545	spin_lock_init(&vport->work_port_lock);
1546
1547	init_timer(&vport->fc_disctmo);
1548	vport->fc_disctmo.function = lpfc_disc_timeout;
1549	vport->fc_disctmo.data = (unsigned long)vport;
1550
1551	init_timer(&vport->fc_fdmitmo);
1552	vport->fc_fdmitmo.function = lpfc_fdmi_tmo;
1553	vport->fc_fdmitmo.data = (unsigned long)vport;
1554
1555	init_timer(&vport->els_tmofunc);
1556	vport->els_tmofunc.function = lpfc_els_timeout;
1557	vport->els_tmofunc.data = (unsigned long)vport;
1558
1559	error = scsi_add_host(shost, dev);
1560	if (error)
1561		goto out_put_shost;
1562
1563	spin_lock_irq(&phba->hbalock);
1564	list_add_tail(&vport->listentry, &phba->port_list);
1565	spin_unlock_irq(&phba->hbalock);
1566	return vport;
1567
1568out_put_shost:
1569	scsi_host_put(shost);
1570out:
1571	return NULL;
1572}
1573
1574void
1575destroy_port(struct lpfc_vport *vport)
1576{
1577	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1578	struct lpfc_hba  *phba = vport->phba;
1579
1580	kfree(vport->vname);
1581
1582	lpfc_debugfs_terminate(vport);
1583	fc_remove_host(shost);
1584	scsi_remove_host(shost);
1585
1586	spin_lock_irq(&phba->hbalock);
1587	list_del_init(&vport->listentry);
1588	spin_unlock_irq(&phba->hbalock);
1589
1590	lpfc_cleanup(vport);
1591	return;
1592}
1593
1594int
1595lpfc_get_instance(void)
1596{
1597	int instance = 0;
1598
1599	/* Assign an unused number */
1600	if (!idr_pre_get(&lpfc_hba_index, GFP_KERNEL))
1601		return -1;
1602	if (idr_get_new(&lpfc_hba_index, NULL, &instance))
1603		return -1;
1604	return instance;
1605}
1606
1607/*
1608 * Note: there is no scan_start function as adapter initialization
1609 * will have asynchronously kicked off the link initialization.
1610 */
1611
1612int lpfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
1613{
1614	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1615	struct lpfc_hba   *phba = vport->phba;
1616	int stat = 0;
1617
1618	spin_lock_irq(shost->host_lock);
1619
1620	if (vport->load_flag & FC_UNLOADING) {
1621		stat = 1;
1622		goto finished;
1623	}
1624	if (time >= 30 * HZ) {
1625		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
1626				"0461 Scanning longer than 30 "
1627				"seconds.  Continuing initialization\n");
1628		stat = 1;
1629		goto finished;
1630	}
1631	if (time >= 15 * HZ && phba->link_state <= LPFC_LINK_DOWN) {
1632		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
1633				"0465 Link down longer than 15 "
1634				"seconds.  Continuing initialization\n");
1635		stat = 1;
1636		goto finished;
1637	}
1638
1639	if (vport->port_state != LPFC_VPORT_READY)
1640		goto finished;
1641	if (vport->num_disc_nodes || vport->fc_prli_sent)
1642		goto finished;
1643	if (vport->fc_map_cnt == 0 && time < 2 * HZ)
1644		goto finished;
1645	if ((phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) != 0)
1646		goto finished;
1647
1648	stat = 1;
1649
1650finished:
1651	spin_unlock_irq(shost->host_lock);
1652	return stat;
1653}
1654
1655void lpfc_host_attrib_init(struct Scsi_Host *shost)
1656{
1657	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1658	struct lpfc_hba   *phba = vport->phba;
1659	/*
1660	 * Set fixed host attributes.  Must done after lpfc_sli_hba_setup().
1661	 */
1662
1663	fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
1664	fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
1665	fc_host_supported_classes(shost) = FC_COS_CLASS3;
1666
1667	memset(fc_host_supported_fc4s(shost), 0,
1668	       sizeof(fc_host_supported_fc4s(shost)));
1669	fc_host_supported_fc4s(shost)[2] = 1;
1670	fc_host_supported_fc4s(shost)[7] = 1;
1671
1672	lpfc_vport_symbolic_node_name(vport, fc_host_symbolic_name(shost),
1673				 sizeof fc_host_symbolic_name(shost));
1674
1675	fc_host_supported_speeds(shost) = 0;
1676	if (phba->lmt & LMT_10Gb)
1677		fc_host_supported_speeds(shost) |= FC_PORTSPEED_10GBIT;
1678	if (phba->lmt & LMT_4Gb)
1679		fc_host_supported_speeds(shost) |= FC_PORTSPEED_4GBIT;
1680	if (phba->lmt & LMT_2Gb)
1681		fc_host_supported_speeds(shost) |= FC_PORTSPEED_2GBIT;
1682	if (phba->lmt & LMT_1Gb)
1683		fc_host_supported_speeds(shost) |= FC_PORTSPEED_1GBIT;
1684
1685	fc_host_maxframe_size(shost) =
1686		(((uint32_t) vport->fc_sparam.cmn.bbRcvSizeMsb & 0x0F) << 8) |
1687		(uint32_t) vport->fc_sparam.cmn.bbRcvSizeLsb;
1688
1689	/* This value is also unchanging */
1690	memset(fc_host_active_fc4s(shost), 0,
1691	       sizeof(fc_host_active_fc4s(shost)));
1692	fc_host_active_fc4s(shost)[2] = 1;
1693	fc_host_active_fc4s(shost)[7] = 1;
1694
1695	fc_host_max_npiv_vports(shost) = phba->max_vpi;
1696	spin_lock_irq(shost->host_lock);
1697	vport->load_flag &= ~FC_LOADING;
1698	spin_unlock_irq(shost->host_lock);
1699}
1700
1701static int __devinit
1702lpfc_pci_probe_one(struct pci_dev *pdev, const struct pci_device_id *pid)
1703{
1704	struct lpfc_vport *vport = NULL;
1705	struct lpfc_hba   *phba;
1706	struct lpfc_sli   *psli;
1707	struct lpfc_iocbq *iocbq_entry = NULL, *iocbq_next = NULL;
1708	struct Scsi_Host  *shost = NULL;
1709	void *ptr;
1710	unsigned long bar0map_len, bar2map_len;
1711	int error = -ENODEV;
1712	int  i, hbq_count;
1713	uint16_t iotag;
1714
1715	if (pci_enable_device(pdev))
1716		goto out;
1717	if (pci_request_regions(pdev, LPFC_DRIVER_NAME))
1718		goto out_disable_device;
1719
1720	phba = kzalloc(sizeof (struct lpfc_hba), GFP_KERNEL);
1721	if (!phba)
1722		goto out_release_regions;
1723
1724	spin_lock_init(&phba->hbalock);
1725
1726	phba->pcidev = pdev;
1727
1728	/* Assign an unused board number */
1729	if ((phba->brd_no = lpfc_get_instance()) < 0)
1730		goto out_free_phba;
1731
1732	INIT_LIST_HEAD(&phba->port_list);
1733	/*
1734	 * Get all the module params for configuring this host and then
1735	 * establish the host.
1736	 */
1737	lpfc_get_cfgparam(phba);
1738	phba->max_vpi = LPFC_MAX_VPI;
1739
1740	/* Initialize timers used by driver */
1741	init_timer(&phba->fc_estabtmo);
1742	phba->fc_estabtmo.function = lpfc_establish_link_tmo;
1743	phba->fc_estabtmo.data = (unsigned long)phba;
1744
1745	init_timer(&phba->hb_tmofunc);
1746	phba->hb_tmofunc.function = lpfc_hb_timeout;
1747	phba->hb_tmofunc.data = (unsigned long)phba;
1748
1749	psli = &phba->sli;
1750	init_timer(&psli->mbox_tmo);
1751	psli->mbox_tmo.function = lpfc_mbox_timeout;
1752	psli->mbox_tmo.data = (unsigned long) phba;
1753	init_timer(&phba->fcp_poll_timer);
1754	phba->fcp_poll_timer.function = lpfc_poll_timeout;
1755	phba->fcp_poll_timer.data = (unsigned long) phba;
1756	init_timer(&phba->fabric_block_timer);
1757	phba->fabric_block_timer.function = lpfc_fabric_block_timeout;
1758	phba->fabric_block_timer.data = (unsigned long) phba;
1759
1760	pci_set_master(pdev);
1761	pci_try_set_mwi(pdev);
1762
1763	if (pci_set_dma_mask(phba->pcidev, DMA_64BIT_MASK) != 0)
1764		if (pci_set_dma_mask(phba->pcidev, DMA_32BIT_MASK) != 0)
1765			goto out_idr_remove;
1766
1767	/*
1768	 * Get the bus address of Bar0 and Bar2 and the number of bytes
1769	 * required by each mapping.
1770	 */
1771	phba->pci_bar0_map = pci_resource_start(phba->pcidev, 0);
1772	bar0map_len        = pci_resource_len(phba->pcidev, 0);
1773
1774	phba->pci_bar2_map = pci_resource_start(phba->pcidev, 2);
1775	bar2map_len        = pci_resource_len(phba->pcidev, 2);
1776
1777	/* Map HBA SLIM to a kernel virtual address. */
1778	phba->slim_memmap_p      = ioremap(phba->pci_bar0_map, bar0map_len);
1779	if (!phba->slim_memmap_p) {
1780		error = -ENODEV;
1781		dev_printk(KERN_ERR, &pdev->dev,
1782			   "ioremap failed for SLIM memory.\n");
1783		goto out_idr_remove;
1784	}
1785
1786	/* Map HBA Control Registers to a kernel virtual address. */
1787	phba->ctrl_regs_memmap_p = ioremap(phba->pci_bar2_map, bar2map_len);
1788	if (!phba->ctrl_regs_memmap_p) {
1789		error = -ENODEV;
1790		dev_printk(KERN_ERR, &pdev->dev,
1791			   "ioremap failed for HBA control registers.\n");
1792		goto out_iounmap_slim;
1793	}
1794
1795	/* Allocate memory for SLI-2 structures */
1796	phba->slim2p = dma_alloc_coherent(&phba->pcidev->dev, SLI2_SLIM_SIZE,
1797					  &phba->slim2p_mapping, GFP_KERNEL);
1798	if (!phba->slim2p)
1799		goto out_iounmap;
1800
1801	memset(phba->slim2p, 0, SLI2_SLIM_SIZE);
1802
1803	phba->hbqslimp.virt = dma_alloc_coherent(&phba->pcidev->dev,
1804						 lpfc_sli_hbq_size(),
1805						 &phba->hbqslimp.phys,
1806						 GFP_KERNEL);
1807	if (!phba->hbqslimp.virt)
1808		goto out_free_slim;
1809
1810	hbq_count = lpfc_sli_hbq_count();
1811	ptr = phba->hbqslimp.virt;
1812	for (i = 0; i < hbq_count; ++i) {
1813		phba->hbqs[i].hbq_virt = ptr;
1814		INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
1815		ptr += (lpfc_hbq_defs[i]->entry_count *
1816			sizeof(struct lpfc_hbq_entry));
1817	}
1818	phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_els_hbq_alloc;
1819	phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer  = lpfc_els_hbq_free;
1820
1821	memset(phba->hbqslimp.virt, 0, lpfc_sli_hbq_size());
1822
1823	/* Initialize the SLI Layer to run with lpfc HBAs. */
1824	lpfc_sli_setup(phba);
1825	lpfc_sli_queue_setup(phba);
1826
1827	error = lpfc_mem_alloc(phba);
1828	if (error)
1829		goto out_free_hbqslimp;
1830
1831	/* Initialize and populate the iocb list per host.  */
1832	INIT_LIST_HEAD(&phba->lpfc_iocb_list);
1833	for (i = 0; i < LPFC_IOCB_LIST_CNT; i++) {
1834		iocbq_entry = kzalloc(sizeof(struct lpfc_iocbq), GFP_KERNEL);
1835		if (iocbq_entry == NULL) {
1836			printk(KERN_ERR "%s: only allocated %d iocbs of "
1837				"expected %d count. Unloading driver.\n",
1838				__FUNCTION__, i, LPFC_IOCB_LIST_CNT);
1839			error = -ENOMEM;
1840			goto out_free_iocbq;
1841		}
1842
1843		iotag = lpfc_sli_next_iotag(phba, iocbq_entry);
1844		if (iotag == 0) {
1845			kfree (iocbq_entry);
1846			printk(KERN_ERR "%s: failed to allocate IOTAG. "
1847			       "Unloading driver.\n",
1848				__FUNCTION__);
1849			error = -ENOMEM;
1850			goto out_free_iocbq;
1851		}
1852
1853		spin_lock_irq(&phba->hbalock);
1854		list_add(&iocbq_entry->list, &phba->lpfc_iocb_list);
1855		phba->total_iocbq_bufs++;
1856		spin_unlock_irq(&phba->hbalock);
1857	}
1858
1859	/* Initialize HBA structure */
1860	phba->fc_edtov = FF_DEF_EDTOV;
1861	phba->fc_ratov = FF_DEF_RATOV;
1862	phba->fc_altov = FF_DEF_ALTOV;
1863	phba->fc_arbtov = FF_DEF_ARBTOV;
1864
1865	INIT_LIST_HEAD(&phba->work_list);
1866	phba->work_ha_mask = (HA_ERATT|HA_MBATT|HA_LATT);
1867	phba->work_ha_mask |= (HA_RXMASK << (LPFC_ELS_RING * 4));
1868
1869	/* Startup the kernel thread for this host adapter. */
1870	phba->worker_thread = kthread_run(lpfc_do_work, phba,
1871				       "lpfc_worker_%d", phba->brd_no);
1872	if (IS_ERR(phba->worker_thread)) {
1873		error = PTR_ERR(phba->worker_thread);
1874		goto out_free_iocbq;
1875	}
1876
1877	/* Initialize the list of scsi buffers used by driver for scsi IO. */
1878	spin_lock_init(&phba->scsi_buf_list_lock);
1879	INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list);
1880
1881	/* Initialize list of fabric iocbs */
1882	INIT_LIST_HEAD(&phba->fabric_iocb_list);
1883
1884	vport = lpfc_create_port(phba, phba->brd_no, &phba->pcidev->dev);
1885	if (!vport)
1886		goto out_kthread_stop;
1887
1888	shost = lpfc_shost_from_vport(vport);
1889	phba->pport = vport;
1890	lpfc_debugfs_initialize(vport);
1891
1892	pci_set_drvdata(pdev, shost);
1893
1894	if (phba->cfg_use_msi) {
1895		error = pci_enable_msi(phba->pcidev);
1896		if (!error)
1897			phba->using_msi = 1;
1898		else
1899			lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
1900					"0452 Enable MSI failed, continuing "
1901					"with IRQ\n");
1902	}
1903
1904	error =	request_irq(phba->pcidev->irq, lpfc_intr_handler, IRQF_SHARED,
1905			    LPFC_DRIVER_NAME, phba);
1906	if (error) {
1907		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1908			"0451 Enable interrupt handler failed\n");
1909		goto out_disable_msi;
1910	}
1911
1912	phba->MBslimaddr = phba->slim_memmap_p;
1913	phba->HAregaddr = phba->ctrl_regs_memmap_p + HA_REG_OFFSET;
1914	phba->CAregaddr = phba->ctrl_regs_memmap_p + CA_REG_OFFSET;
1915	phba->HSregaddr = phba->ctrl_regs_memmap_p + HS_REG_OFFSET;
1916	phba->HCregaddr = phba->ctrl_regs_memmap_p + HC_REG_OFFSET;
1917
1918	if (lpfc_alloc_sysfs_attr(vport))
1919		goto out_free_irq;
1920
1921	if (lpfc_sli_hba_setup(phba))
1922		goto out_remove_device;
1923
1924	/*
1925	 * hba setup may have changed the hba_queue_depth so we need to adjust
1926	 * the value of can_queue.
1927	 */
1928	shost->can_queue = phba->cfg_hba_queue_depth - 10;
1929
1930	lpfc_host_attrib_init(shost);
1931
1932	if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
1933		spin_lock_irq(shost->host_lock);
1934		lpfc_poll_start_timer(phba);
1935		spin_unlock_irq(shost->host_lock);
1936	}
1937
1938	scsi_scan_host(shost);
1939
1940	return 0;
1941
1942out_remove_device:
1943	lpfc_free_sysfs_attr(vport);
1944	spin_lock_irq(shost->host_lock);
1945	vport->load_flag |= FC_UNLOADING;
1946	spin_unlock_irq(shost->host_lock);
1947out_free_irq:
1948	lpfc_stop_phba_timers(phba);
1949	phba->pport->work_port_events = 0;
1950	free_irq(phba->pcidev->irq, phba);
1951out_disable_msi:
1952	if (phba->using_msi)
1953		pci_disable_msi(phba->pcidev);
1954	destroy_port(vport);
1955out_kthread_stop:
1956	kthread_stop(phba->worker_thread);
1957out_free_iocbq:
1958	list_for_each_entry_safe(iocbq_entry, iocbq_next,
1959						&phba->lpfc_iocb_list, list) {
1960		kfree(iocbq_entry);
1961		phba->total_iocbq_bufs--;
1962	}
1963	lpfc_mem_free(phba);
1964out_free_hbqslimp:
1965	dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(), phba->hbqslimp.virt,
1966			  phba->hbqslimp.phys);
1967out_free_slim:
1968	dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE, phba->slim2p,
1969							phba->slim2p_mapping);
1970out_iounmap:
1971	iounmap(phba->ctrl_regs_memmap_p);
1972out_iounmap_slim:
1973	iounmap(phba->slim_memmap_p);
1974out_idr_remove:
1975	idr_remove(&lpfc_hba_index, phba->brd_no);
1976out_free_phba:
1977	kfree(phba);
1978out_release_regions:
1979	pci_release_regions(pdev);
1980out_disable_device:
1981	pci_disable_device(pdev);
1982out:
1983	pci_set_drvdata(pdev, NULL);
1984	if (shost)
1985		scsi_host_put(shost);
1986	return error;
1987}
1988
1989static void __devexit
1990lpfc_pci_remove_one(struct pci_dev *pdev)
1991{
1992	struct Scsi_Host  *shost = pci_get_drvdata(pdev);
1993	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1994	struct lpfc_hba   *phba = vport->phba;
1995	spin_lock_irq(&phba->hbalock);
1996	vport->load_flag |= FC_UNLOADING;
1997	spin_unlock_irq(&phba->hbalock);
1998
1999	kfree(vport->vname);
2000	lpfc_free_sysfs_attr(vport);
2001
2002	fc_remove_host(shost);
2003	scsi_remove_host(shost);
2004	/*
2005	 * Bring down the SLI Layer. This step disable all interrupts,
2006	 * clears the rings, discards all mailbox commands, and resets
2007	 * the HBA.
2008	 */
2009	lpfc_sli_hba_down(phba);
2010	lpfc_sli_brdrestart(phba);
2011
2012	lpfc_stop_phba_timers(phba);
2013	spin_lock_irq(&phba->hbalock);
2014	list_del_init(&vport->listentry);
2015	spin_unlock_irq(&phba->hbalock);
2016
2017	lpfc_debugfs_terminate(vport);
2018	lpfc_cleanup(vport);
2019
2020	kthread_stop(phba->worker_thread);
2021
2022	/* Release the irq reservation */
2023	free_irq(phba->pcidev->irq, phba);
2024	if (phba->using_msi)
2025		pci_disable_msi(phba->pcidev);
2026
2027	pci_set_drvdata(pdev, NULL);
2028	scsi_host_put(shost);
2029
2030	/*
2031	 * Call scsi_free before mem_free since scsi bufs are released to their
2032	 * corresponding pools here.
2033	 */
2034	lpfc_scsi_free(phba);
2035	lpfc_mem_free(phba);
2036
2037	dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(), phba->hbqslimp.virt,
2038			  phba->hbqslimp.phys);
2039
2040	/* Free resources associated with SLI2 interface */
2041	dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
2042			  phba->slim2p, phba->slim2p_mapping);
2043
2044	/* unmap adapter SLIM and Control Registers */
2045	iounmap(phba->ctrl_regs_memmap_p);
2046	iounmap(phba->slim_memmap_p);
2047
2048	idr_remove(&lpfc_hba_index, phba->brd_no);
2049
2050	kfree(phba);
2051
2052	pci_release_regions(pdev);
2053	pci_disable_device(pdev);
2054}
2055
2056/**
2057 * lpfc_io_error_detected - called when PCI error is detected
2058 * @pdev: Pointer to PCI device
2059 * @state: The current pci conneection state
2060 *
2061 * This function is called after a PCI bus error affecting
2062 * this device has been detected.
2063 */
2064static pci_ers_result_t lpfc_io_error_detected(struct pci_dev *pdev,
2065				pci_channel_state_t state)
2066{
2067	struct Scsi_Host *shost = pci_get_drvdata(pdev);
2068	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
2069	struct lpfc_sli *psli = &phba->sli;
2070	struct lpfc_sli_ring  *pring;
2071
2072	if (state == pci_channel_io_perm_failure)
2073		return PCI_ERS_RESULT_DISCONNECT;
2074
2075	pci_disable_device(pdev);
2076	/*
2077	 * There may be I/Os dropped by the firmware.
2078	 * Error iocb (I/O) on txcmplq and let the SCSI layer
2079	 * retry it after re-establishing link.
2080	 */
2081	pring = &psli->ring[psli->fcp_ring];
2082	lpfc_sli_abort_iocb_ring(phba, pring);
2083
2084	/* Release the irq reservation */
2085	free_irq(phba->pcidev->irq, phba);
2086	if (phba->using_msi)
2087		pci_disable_msi(phba->pcidev);
2088
2089	/* Request a slot reset. */
2090	return PCI_ERS_RESULT_NEED_RESET;
2091}
2092
2093/**
2094 * lpfc_io_slot_reset - called after the pci bus has been reset.
2095 * @pdev: Pointer to PCI device
2096 *
2097 * Restart the card from scratch, as if from a cold-boot.
2098 */
2099static pci_ers_result_t lpfc_io_slot_reset(struct pci_dev *pdev)
2100{
2101	struct Scsi_Host *shost = pci_get_drvdata(pdev);
2102	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
2103	struct lpfc_sli *psli = &phba->sli;
2104	int bars = pci_select_bars(pdev, IORESOURCE_MEM);
2105
2106	dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
2107	if (pci_enable_device_bars(pdev, bars)) {
2108		printk(KERN_ERR "lpfc: Cannot re-enable "
2109			"PCI device after reset.\n");
2110		return PCI_ERS_RESULT_DISCONNECT;
2111	}
2112
2113	pci_set_master(pdev);
2114
2115	/* Re-establishing Link */
2116	spin_lock_irq(shost->host_lock);
2117	phba->pport->fc_flag |= FC_ESTABLISH_LINK;
2118	spin_unlock_irq(shost->host_lock);
2119
2120	spin_lock_irq(&phba->hbalock);
2121	psli->sli_flag &= ~LPFC_SLI2_ACTIVE;
2122	spin_unlock_irq(&phba->hbalock);
2123
2124
2125	/* Take device offline; this will perform cleanup */
2126	lpfc_offline(phba);
2127	lpfc_sli_brdrestart(phba);
2128
2129	return PCI_ERS_RESULT_RECOVERED;
2130}
2131
2132/**
2133 * lpfc_io_resume - called when traffic can start flowing again.
2134 * @pdev: Pointer to PCI device
2135 *
2136 * This callback is called when the error recovery driver tells us that
2137 * its OK to resume normal operation.
2138 */
2139static void lpfc_io_resume(struct pci_dev *pdev)
2140{
2141	struct Scsi_Host *shost = pci_get_drvdata(pdev);
2142	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
2143
2144	if (lpfc_online(phba) == 0) {
2145		mod_timer(&phba->fc_estabtmo, jiffies + HZ * 60);
2146	}
2147}
2148
2149static struct pci_device_id lpfc_id_table[] = {
2150	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_VIPER,
2151		PCI_ANY_ID, PCI_ANY_ID, },
2152	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_FIREFLY,
2153		PCI_ANY_ID, PCI_ANY_ID, },
2154	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_THOR,
2155		PCI_ANY_ID, PCI_ANY_ID, },
2156	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PEGASUS,
2157		PCI_ANY_ID, PCI_ANY_ID, },
2158	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_CENTAUR,
2159		PCI_ANY_ID, PCI_ANY_ID, },
2160	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_DRAGONFLY,
2161		PCI_ANY_ID, PCI_ANY_ID, },
2162	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SUPERFLY,
2163		PCI_ANY_ID, PCI_ANY_ID, },
2164	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_RFLY,
2165		PCI_ANY_ID, PCI_ANY_ID, },
2166	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PFLY,
2167		PCI_ANY_ID, PCI_ANY_ID, },
2168	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE,
2169		PCI_ANY_ID, PCI_ANY_ID, },
2170	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_SCSP,
2171		PCI_ANY_ID, PCI_ANY_ID, },
2172	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_DCSP,
2173		PCI_ANY_ID, PCI_ANY_ID, },
2174	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS,
2175		PCI_ANY_ID, PCI_ANY_ID, },
2176	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_SCSP,
2177		PCI_ANY_ID, PCI_ANY_ID, },
2178	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_DCSP,
2179		PCI_ANY_ID, PCI_ANY_ID, },
2180	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BMID,
2181		PCI_ANY_ID, PCI_ANY_ID, },
2182	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BSMB,
2183		PCI_ANY_ID, PCI_ANY_ID, },
2184	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR,
2185		PCI_ANY_ID, PCI_ANY_ID, },
2186	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_SCSP,
2187		PCI_ANY_ID, PCI_ANY_ID, },
2188	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_DCSP,
2189		PCI_ANY_ID, PCI_ANY_ID, },
2190	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZMID,
2191		PCI_ANY_ID, PCI_ANY_ID, },
2192	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZSMB,
2193		PCI_ANY_ID, PCI_ANY_ID, },
2194	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_TFLY,
2195		PCI_ANY_ID, PCI_ANY_ID, },
2196	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP101,
2197		PCI_ANY_ID, PCI_ANY_ID, },
2198	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP10000S,
2199		PCI_ANY_ID, PCI_ANY_ID, },
2200	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP11000S,
2201		PCI_ANY_ID, PCI_ANY_ID, },
2202	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LPE11000S,
2203		PCI_ANY_ID, PCI_ANY_ID, },
2204	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT,
2205		PCI_ANY_ID, PCI_ANY_ID, },
2206	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_MID,
2207		PCI_ANY_ID, PCI_ANY_ID, },
2208	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SMB,
2209		PCI_ANY_ID, PCI_ANY_ID, },
2210	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_DCSP,
2211		PCI_ANY_ID, PCI_ANY_ID, },
2212	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SCSP,
2213		PCI_ANY_ID, PCI_ANY_ID, },
2214	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_S,
2215		PCI_ANY_ID, PCI_ANY_ID, },
2216	{ 0 }
2217};
2218
2219MODULE_DEVICE_TABLE(pci, lpfc_id_table);
2220
2221static struct pci_error_handlers lpfc_err_handler = {
2222	.error_detected = lpfc_io_error_detected,
2223	.slot_reset = lpfc_io_slot_reset,
2224	.resume = lpfc_io_resume,
2225};
2226
2227static struct pci_driver lpfc_driver = {
2228	.name		= LPFC_DRIVER_NAME,
2229	.id_table	= lpfc_id_table,
2230	.probe		= lpfc_pci_probe_one,
2231	.remove		= __devexit_p(lpfc_pci_remove_one),
2232	.err_handler    = &lpfc_err_handler,
2233};
2234
2235static int __init
2236lpfc_init(void)
2237{
2238	int error = 0;
2239
2240	printk(LPFC_MODULE_DESC "\n");
2241	printk(LPFC_COPYRIGHT "\n");
2242
2243	lpfc_transport_template =
2244				fc_attach_transport(&lpfc_transport_functions);
2245	lpfc_vport_transport_template =
2246			fc_attach_transport(&lpfc_vport_transport_functions);
2247	if (!lpfc_transport_template || !lpfc_vport_transport_template)
2248		return -ENOMEM;
2249	error = pci_register_driver(&lpfc_driver);
2250	if (error) {
2251		fc_release_transport(lpfc_transport_template);
2252		fc_release_transport(lpfc_vport_transport_template);
2253	}
2254
2255	return error;
2256}
2257
2258static void __exit
2259lpfc_exit(void)
2260{
2261	pci_unregister_driver(&lpfc_driver);
2262	fc_release_transport(lpfc_transport_template);
2263	fc_release_transport(lpfc_vport_transport_template);
2264}
2265
2266module_init(lpfc_init);
2267module_exit(lpfc_exit);
2268MODULE_LICENSE("GPL");
2269MODULE_DESCRIPTION(LPFC_MODULE_DESC);
2270MODULE_AUTHOR("Emulex Corporation - tech.support@emulex.com");
2271MODULE_VERSION("0:" LPFC_DRIVER_VERSION);
2272