lpfc_init.c revision dea3101e0a5c897d2c9351a7444e139db9f40247
1/*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for         *
3 * Enterprise Fibre Channel Host Bus Adapters.                     *
4 * Refer to the README file included with this package for         *
5 * driver version and adapter support.                             *
6 * Copyright (C) 2004 Emulex Corporation.                          *
7 * www.emulex.com                                                  *
8 *                                                                 *
9 * This program is free software; you can redistribute it and/or   *
10 * modify it under the terms of the GNU General Public License     *
11 * as published by the Free Software Foundation; either version 2  *
12 * of the License, or (at your option) any later version.          *
13 *                                                                 *
14 * This program is distributed in the hope that it will be useful, *
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of  *
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the   *
17 * GNU General Public License for more details, a copy of which    *
18 * can be found in the file COPYING included with this package.    *
19 *******************************************************************/
20
21/*
22 * $Id: lpfc_init.c 1.233 2005/04/13 11:59:09EDT sf_support Exp  $
23 */
24
25#include <linux/blkdev.h>
26#include <linux/delay.h>
27#include <linux/dma-mapping.h>
28#include <linux/idr.h>
29#include <linux/interrupt.h>
30#include <linux/kthread.h>
31#include <linux/pci.h>
32#include <linux/spinlock.h>
33
34#include <scsi/scsi_device.h>
35#include <scsi/scsi_host.h>
36#include <scsi/scsi_transport_fc.h>
37
38#include "lpfc_hw.h"
39#include "lpfc_sli.h"
40#include "lpfc_disc.h"
41#include "lpfc_scsi.h"
42#include "lpfc.h"
43#include "lpfc_logmsg.h"
44#include "lpfc_crtn.h"
45#include "lpfc_version.h"
46
47static int lpfc_parse_vpd(struct lpfc_hba *, uint8_t *);
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 DEFINE_IDR(lpfc_hba_index);
53
54/************************************************************************/
55/*                                                                      */
56/*    lpfc_config_port_prep                                             */
57/*    This routine will do LPFC initialization prior to the             */
58/*    CONFIG_PORT mailbox command. This will be initialized             */
59/*    as a SLI layer callback routine.                                  */
60/*    This routine returns 0 on success or -ERESTART if it wants        */
61/*    the SLI layer to reset the HBA and try again. Any                 */
62/*    other return value indicates an error.                            */
63/*                                                                      */
64/************************************************************************/
65int
66lpfc_config_port_prep(struct lpfc_hba * phba)
67{
68	lpfc_vpd_t *vp = &phba->vpd;
69	int i = 0, rc;
70	LPFC_MBOXQ_t *pmb;
71	MAILBOX_t *mb;
72	char *lpfc_vpd_data = NULL;
73	uint16_t offset = 0;
74	static char licensed[56] =
75		    "key unlock for use with gnu public licensed code only\0";
76
77	pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
78	if (!pmb) {
79		phba->hba_state = LPFC_HBA_ERROR;
80		return -ENOMEM;
81	}
82
83	mb = &pmb->mb;
84	phba->hba_state = LPFC_INIT_MBX_CMDS;
85
86	if (lpfc_is_LC_HBA(phba->pcidev->device)) {
87		uint32_t *ptext = (uint32_t *) licensed;
88
89		for (i = 0; i < 56; i += sizeof (uint32_t), ptext++)
90			*ptext = cpu_to_be32(*ptext);
91
92		lpfc_read_nv(phba, pmb);
93		memset((char*)mb->un.varRDnvp.rsvd3, 0,
94			sizeof (mb->un.varRDnvp.rsvd3));
95		memcpy((char*)mb->un.varRDnvp.rsvd3, licensed,
96			 sizeof (licensed));
97
98		rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
99
100		if (rc != MBX_SUCCESS) {
101			lpfc_printf_log(phba,
102					KERN_ERR,
103					LOG_MBOX,
104					"%d:0324 Config Port initialization "
105					"error, mbxCmd x%x READ_NVPARM, "
106					"mbxStatus x%x\n",
107					phba->brd_no,
108					mb->mbxCommand, mb->mbxStatus);
109			mempool_free(pmb, phba->mbox_mem_pool);
110			return -ERESTART;
111		}
112		memcpy(phba->wwnn, (char *)mb->un.varRDnvp.nodename,
113		       sizeof (mb->un.varRDnvp.nodename));
114	}
115
116	/* Setup and issue mailbox READ REV command */
117	lpfc_read_rev(phba, pmb);
118	rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
119	if (rc != MBX_SUCCESS) {
120		lpfc_printf_log(phba,
121				KERN_ERR,
122				LOG_INIT,
123				"%d:0439 Adapter failed to init, mbxCmd x%x "
124				"READ_REV, mbxStatus x%x\n",
125				phba->brd_no,
126				mb->mbxCommand, mb->mbxStatus);
127		mempool_free( pmb, phba->mbox_mem_pool);
128		return -ERESTART;
129	}
130
131	/* The HBA's current state is provided by the ProgType and rr fields.
132	 * Read and check the value of these fields before continuing to config
133	 * this port.
134	 */
135	if (mb->un.varRdRev.rr == 0 || mb->un.varRdRev.un.b.ProgType != 2) {
136		/* Old firmware */
137		vp->rev.rBit = 0;
138		lpfc_printf_log(phba,
139				KERN_ERR,
140				LOG_INIT,
141				"%d:0440 Adapter failed to init, mbxCmd x%x "
142				"READ_REV detected outdated firmware"
143				"Data: x%x\n",
144				phba->brd_no,
145				mb->mbxCommand, 0);
146		mempool_free(pmb, phba->mbox_mem_pool);
147		return -ERESTART;
148	} else {
149		vp->rev.rBit = 1;
150		vp->rev.sli1FwRev = mb->un.varRdRev.sli1FwRev;
151		memcpy(vp->rev.sli1FwName,
152			(char*)mb->un.varRdRev.sli1FwName, 16);
153		vp->rev.sli2FwRev = mb->un.varRdRev.sli2FwRev;
154		memcpy(vp->rev.sli2FwName,
155					(char *)mb->un.varRdRev.sli2FwName, 16);
156	}
157
158	/* Save information as VPD data */
159	vp->rev.biuRev = mb->un.varRdRev.biuRev;
160	vp->rev.smRev = mb->un.varRdRev.smRev;
161	vp->rev.smFwRev = mb->un.varRdRev.un.smFwRev;
162	vp->rev.endecRev = mb->un.varRdRev.endecRev;
163	vp->rev.fcphHigh = mb->un.varRdRev.fcphHigh;
164	vp->rev.fcphLow = mb->un.varRdRev.fcphLow;
165	vp->rev.feaLevelHigh = mb->un.varRdRev.feaLevelHigh;
166	vp->rev.feaLevelLow = mb->un.varRdRev.feaLevelLow;
167	vp->rev.postKernRev = mb->un.varRdRev.postKernRev;
168	vp->rev.opFwRev = mb->un.varRdRev.opFwRev;
169
170	if (lpfc_is_LC_HBA(phba->pcidev->device))
171		memcpy(phba->RandomData, (char *)&mb->un.varWords[24],
172						sizeof (phba->RandomData));
173
174	/* Get the default values for Model Name and Description */
175	lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
176
177	/* Get adapter VPD information */
178	pmb->context2 = kmalloc(DMP_RSP_SIZE, GFP_KERNEL);
179	if (!pmb->context2)
180		goto out_free_mbox;
181	lpfc_vpd_data = kmalloc(DMP_VPD_SIZE, GFP_KERNEL);
182	if (!lpfc_vpd_data)
183		goto out_free_context2;
184
185	do {
186		lpfc_dump_mem(phba, pmb, offset);
187		rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
188
189		if (rc != MBX_SUCCESS) {
190			lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
191					"%d:0441 VPD not present on adapter, "
192					"mbxCmd x%x DUMP VPD, mbxStatus x%x\n",
193					phba->brd_no,
194					mb->mbxCommand, mb->mbxStatus);
195			kfree(lpfc_vpd_data);
196			lpfc_vpd_data = NULL;
197			break;
198		}
199
200		lpfc_sli_pcimem_bcopy(pmb->context2, lpfc_vpd_data + offset,
201							mb->un.varDmp.word_cnt);
202		offset += mb->un.varDmp.word_cnt;
203	} while (mb->un.varDmp.word_cnt);
204	lpfc_parse_vpd(phba, lpfc_vpd_data);
205
206	kfree(lpfc_vpd_data);
207out_free_context2:
208	kfree(pmb->context2);
209out_free_mbox:
210	mempool_free(pmb, phba->mbox_mem_pool);
211	return 0;
212}
213
214/************************************************************************/
215/*                                                                      */
216/*    lpfc_config_port_post                                             */
217/*    This routine will do LPFC initialization after the                */
218/*    CONFIG_PORT mailbox command. This will be initialized             */
219/*    as a SLI layer callback routine.                                  */
220/*    This routine returns 0 on success. Any other return value         */
221/*    indicates an error.                                               */
222/*                                                                      */
223/************************************************************************/
224int
225lpfc_config_port_post(struct lpfc_hba * phba)
226{
227	LPFC_MBOXQ_t *pmb;
228	MAILBOX_t *mb;
229	struct lpfc_dmabuf *mp;
230	struct lpfc_sli *psli = &phba->sli;
231	uint32_t status, timeout;
232	int i, j, rc;
233
234	pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
235	if (!pmb) {
236		phba->hba_state = LPFC_HBA_ERROR;
237		return -ENOMEM;
238	}
239	mb = &pmb->mb;
240
241	lpfc_config_link(phba, pmb);
242	rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
243	if (rc != MBX_SUCCESS) {
244		lpfc_printf_log(phba,
245				KERN_ERR,
246				LOG_INIT,
247				"%d:0447 Adapter failed init, mbxCmd x%x "
248				"CONFIG_LINK mbxStatus x%x\n",
249				phba->brd_no,
250				mb->mbxCommand, mb->mbxStatus);
251		phba->hba_state = LPFC_HBA_ERROR;
252		mempool_free( pmb, phba->mbox_mem_pool);
253		return -EIO;
254	}
255
256	/* Get login parameters for NID.  */
257	lpfc_read_sparam(phba, pmb);
258	if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
259		lpfc_printf_log(phba,
260				KERN_ERR,
261				LOG_INIT,
262				"%d:0448 Adapter failed init, mbxCmd x%x "
263				"READ_SPARM mbxStatus x%x\n",
264				phba->brd_no,
265				mb->mbxCommand, mb->mbxStatus);
266		phba->hba_state = LPFC_HBA_ERROR;
267		mp = (struct lpfc_dmabuf *) pmb->context1;
268		mempool_free( pmb, phba->mbox_mem_pool);
269		lpfc_mbuf_free(phba, mp->virt, mp->phys);
270		kfree(mp);
271		return -EIO;
272	}
273
274	mp = (struct lpfc_dmabuf *) pmb->context1;
275
276	memcpy(&phba->fc_sparam, mp->virt, sizeof (struct serv_parm));
277	lpfc_mbuf_free(phba, mp->virt, mp->phys);
278	kfree(mp);
279	pmb->context1 = NULL;
280
281	memcpy(&phba->fc_nodename, &phba->fc_sparam.nodeName,
282	       sizeof (struct lpfc_name));
283	memcpy(&phba->fc_portname, &phba->fc_sparam.portName,
284	       sizeof (struct lpfc_name));
285	/* If no serial number in VPD data, use low 6 bytes of WWNN */
286	/* This should be consolidated into parse_vpd ? - mr */
287	if (phba->SerialNumber[0] == 0) {
288		uint8_t *outptr;
289
290		outptr = (uint8_t *) & phba->fc_nodename.IEEE[0];
291		for (i = 0; i < 12; i++) {
292			status = *outptr++;
293			j = ((status & 0xf0) >> 4);
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			i++;
301			j = (status & 0xf);
302			if (j <= 9)
303				phba->SerialNumber[i] =
304				    (char)((uint8_t) 0x30 + (uint8_t) j);
305			else
306				phba->SerialNumber[i] =
307				    (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
308		}
309	}
310
311	/* This should turn on DELAYED ABTS for ELS timeouts */
312	lpfc_set_slim(phba, pmb, 0x052198, 0x1);
313	if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
314		phba->hba_state = LPFC_HBA_ERROR;
315		mempool_free( pmb, phba->mbox_mem_pool);
316		return -EIO;
317	}
318
319
320	lpfc_read_config(phba, pmb);
321	if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
322		lpfc_printf_log(phba,
323				KERN_ERR,
324				LOG_INIT,
325				"%d:0453 Adapter failed to init, mbxCmd x%x "
326				"READ_CONFIG, mbxStatus x%x\n",
327				phba->brd_no,
328				mb->mbxCommand, mb->mbxStatus);
329		phba->hba_state = LPFC_HBA_ERROR;
330		mempool_free( pmb, phba->mbox_mem_pool);
331		return -EIO;
332	}
333
334	/* Reset the DFT_HBA_Q_DEPTH to the max xri  */
335	if (phba->cfg_hba_queue_depth > (mb->un.varRdConfig.max_xri+1))
336		phba->cfg_hba_queue_depth =
337			mb->un.varRdConfig.max_xri + 1;
338
339	phba->lmt = mb->un.varRdConfig.lmt;
340	/* HBA is not 4GB capable, or HBA is not 2GB capable,
341	don't let link speed ask for it */
342	if ((((phba->lmt & LMT_4250_10bit) != LMT_4250_10bit) &&
343		(phba->cfg_link_speed > LINK_SPEED_2G)) ||
344		(((phba->lmt & LMT_2125_10bit) != LMT_2125_10bit) &&
345		(phba->cfg_link_speed > LINK_SPEED_1G))) {
346		/* Reset link speed to auto. 1G/2GB HBA cfg'd for 4G */
347		lpfc_printf_log(phba,
348			KERN_WARNING,
349			LOG_LINK_EVENT,
350			"%d:1302 Invalid speed for this board: "
351			"Reset link speed to auto: x%x\n",
352			phba->brd_no,
353			phba->cfg_link_speed);
354			phba->cfg_link_speed = LINK_SPEED_AUTO;
355	}
356
357	phba->hba_state = LPFC_LINK_DOWN;
358
359	/* Only process IOCBs on ring 0 till hba_state is READY */
360	if (psli->ring[psli->ip_ring].cmdringaddr)
361		psli->ring[psli->ip_ring].flag |= LPFC_STOP_IOCB_EVENT;
362	if (psli->ring[psli->fcp_ring].cmdringaddr)
363		psli->ring[psli->fcp_ring].flag |= LPFC_STOP_IOCB_EVENT;
364	if (psli->ring[psli->next_ring].cmdringaddr)
365		psli->ring[psli->next_ring].flag |= LPFC_STOP_IOCB_EVENT;
366
367	/* Post receive buffers for desired rings */
368	lpfc_post_rcv_buf(phba);
369
370	/* Enable appropriate host interrupts */
371	spin_lock_irq(phba->host->host_lock);
372	status = readl(phba->HCregaddr);
373	status |= HC_MBINT_ENA | HC_ERINT_ENA | HC_LAINT_ENA;
374	if (psli->num_rings > 0)
375		status |= HC_R0INT_ENA;
376	if (psli->num_rings > 1)
377		status |= HC_R1INT_ENA;
378	if (psli->num_rings > 2)
379		status |= HC_R2INT_ENA;
380	if (psli->num_rings > 3)
381		status |= HC_R3INT_ENA;
382
383	writel(status, phba->HCregaddr);
384	readl(phba->HCregaddr); /* flush */
385	spin_unlock_irq(phba->host->host_lock);
386
387	/*
388	 * Setup the ring 0 (els)  timeout handler
389	 */
390	timeout = phba->fc_ratov << 1;
391	phba->els_tmofunc.expires = jiffies + HZ * timeout;
392	add_timer(&phba->els_tmofunc);
393
394	lpfc_init_link(phba, pmb, phba->cfg_topology, phba->cfg_link_speed);
395	pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
396	if (lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT) != MBX_SUCCESS) {
397		lpfc_printf_log(phba,
398				KERN_ERR,
399				LOG_INIT,
400				"%d:0454 Adapter failed to init, mbxCmd x%x "
401				"INIT_LINK, mbxStatus x%x\n",
402				phba->brd_no,
403				mb->mbxCommand, mb->mbxStatus);
404
405		/* Clear all interrupt enable conditions */
406		writel(0, phba->HCregaddr);
407		readl(phba->HCregaddr); /* flush */
408		/* Clear all pending interrupts */
409		writel(0xffffffff, phba->HAregaddr);
410		readl(phba->HAregaddr); /* flush */
411
412		phba->hba_state = LPFC_HBA_ERROR;
413		mempool_free(pmb, phba->mbox_mem_pool);
414		return -EIO;
415	}
416	/* MBOX buffer will be freed in mbox compl */
417
418	i = 0;
419	while ((phba->hba_state != LPFC_HBA_READY) ||
420	       (phba->num_disc_nodes) || (phba->fc_prli_sent) ||
421	       ((phba->fc_map_cnt == 0) && (i<2)) ||
422	       (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE)) {
423		/* Check every second for 30 retries. */
424		i++;
425		if (i > 30) {
426			break;
427		}
428		if ((i >= 15) && (phba->hba_state <= LPFC_LINK_DOWN)) {
429			/* The link is down.  Set linkdown timeout */
430			break;
431		}
432
433		/* Delay for 1 second to give discovery time to complete. */
434		msleep(1000);
435
436	}
437
438	/* Since num_disc_nodes keys off of PLOGI, delay a bit to let
439	 * any potential PRLIs to flush thru the SLI sub-system.
440	 */
441	msleep(50);
442
443	return (0);
444}
445
446/************************************************************************/
447/*                                                                      */
448/*    lpfc_hba_down_prep                                                */
449/*    This routine will do LPFC uninitialization before the             */
450/*    HBA is reset when bringing down the SLI Layer. This will be       */
451/*    initialized as a SLI layer callback routine.                      */
452/*    This routine returns 0 on success. Any other return value         */
453/*    indicates an error.                                               */
454/*                                                                      */
455/************************************************************************/
456int
457lpfc_hba_down_prep(struct lpfc_hba * phba)
458{
459	/* Disable interrupts */
460	writel(0, phba->HCregaddr);
461	readl(phba->HCregaddr); /* flush */
462
463	/* Cleanup potential discovery resources */
464	lpfc_els_flush_rscn(phba);
465	lpfc_els_flush_cmd(phba);
466	lpfc_disc_flush_list(phba);
467
468	return (0);
469}
470
471/************************************************************************/
472/*                                                                      */
473/*    lpfc_handle_eratt                                                 */
474/*    This routine will handle processing a Host Attention              */
475/*    Error Status event. This will be initialized                      */
476/*    as a SLI layer callback routine.                                  */
477/*                                                                      */
478/************************************************************************/
479void
480lpfc_handle_eratt(struct lpfc_hba * phba)
481{
482	struct lpfc_sli *psli = &phba->sli;
483	struct lpfc_sli_ring  *pring;
484
485	/*
486	 * If a reset is sent to the HBA restore PCI configuration registers.
487	 */
488	if ( phba->hba_state == LPFC_INIT_START ) {
489		mdelay(1);
490		readl(phba->HCregaddr); /* flush */
491		writel(0, phba->HCregaddr);
492		readl(phba->HCregaddr); /* flush */
493
494		/* Restore PCI cmd register */
495		pci_write_config_word(phba->pcidev,
496				      PCI_COMMAND, phba->pci_cfg_value);
497	}
498
499	if (phba->work_hs & HS_FFER6) {
500		/* Re-establishing Link */
501		lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
502				"%d:1301 Re-establishing Link "
503				"Data: x%x x%x x%x\n",
504				phba->brd_no, phba->work_hs,
505				phba->work_status[0], phba->work_status[1]);
506		spin_lock_irq(phba->host->host_lock);
507		phba->fc_flag |= FC_ESTABLISH_LINK;
508		spin_unlock_irq(phba->host->host_lock);
509
510		/*
511		* Firmware stops when it triggled erratt with HS_FFER6.
512		* That could cause the I/Os dropped by the firmware.
513		* Error iocb (I/O) on txcmplq and let the SCSI layer
514		* retry it after re-establishing link.
515		*/
516		pring = &psli->ring[psli->fcp_ring];
517		lpfc_sli_abort_iocb_ring(phba, pring);
518
519
520		/*
521		 * There was a firmware error.  Take the hba offline and then
522		 * attempt to restart it.
523		 */
524		lpfc_offline(phba);
525		if (lpfc_online(phba) == 0) {	/* Initialize the HBA */
526			mod_timer(&phba->fc_estabtmo, jiffies + HZ * 60);
527			return;
528		}
529	} else {
530		/* The if clause above forces this code path when the status
531		 * failure is a value other than FFER6.  Do not call the offline
532		 *  twice. This is the adapter hardware error path.
533		 */
534		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
535				"%d:0457 Adapter Hardware Error "
536				"Data: x%x x%x x%x\n",
537				phba->brd_no, phba->work_hs,
538				phba->work_status[0], phba->work_status[1]);
539
540		lpfc_offline(phba);
541
542		/*
543		 * Restart all traffic to this host.  Since the fc_transport
544		 * block functions (future) were not called in lpfc_offline,
545		 * don't call them here.
546		 */
547		scsi_unblock_requests(phba->host);
548	}
549}
550
551/************************************************************************/
552/*                                                                      */
553/*    lpfc_handle_latt                                                  */
554/*    This routine will handle processing a Host Attention              */
555/*    Link Status event. This will be initialized                       */
556/*    as a SLI layer callback routine.                                  */
557/*                                                                      */
558/************************************************************************/
559void
560lpfc_handle_latt(struct lpfc_hba * phba)
561{
562	struct lpfc_sli *psli = &phba->sli;
563	LPFC_MBOXQ_t *pmb;
564	volatile uint32_t control;
565	struct lpfc_dmabuf *mp;
566	int rc = -ENOMEM;
567
568	pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
569	if (!pmb)
570		goto lpfc_handle_latt_err_exit;
571
572	mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
573	if (!mp)
574		goto lpfc_handle_latt_free_pmb;
575
576	mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
577	if (!mp->virt)
578		goto lpfc_handle_latt_free_mp;
579
580	rc = -EIO;
581
582
583	psli->slistat.link_event++;
584	lpfc_read_la(phba, pmb, mp);
585	pmb->mbox_cmpl = lpfc_mbx_cmpl_read_la;
586	rc = lpfc_sli_issue_mbox (phba, pmb, (MBX_NOWAIT | MBX_STOP_IOCB));
587	if (rc == MBX_NOT_FINISHED)
588		goto lpfc_handle_latt_free_mp;
589
590	/* Clear Link Attention in HA REG */
591	spin_lock_irq(phba->host->host_lock);
592	writel(HA_LATT, phba->HAregaddr);
593	readl(phba->HAregaddr); /* flush */
594	spin_unlock_irq(phba->host->host_lock);
595
596	return;
597
598lpfc_handle_latt_free_mp:
599	kfree(mp);
600lpfc_handle_latt_free_pmb:
601	kfree(pmb);
602lpfc_handle_latt_err_exit:
603	/* Enable Link attention interrupts */
604	spin_lock_irq(phba->host->host_lock);
605	psli->sli_flag |= LPFC_PROCESS_LA;
606	control = readl(phba->HCregaddr);
607	control |= HC_LAINT_ENA;
608	writel(control, phba->HCregaddr);
609	readl(phba->HCregaddr); /* flush */
610
611	/* Clear Link Attention in HA REG */
612	writel(HA_LATT, phba->HAregaddr);
613	readl(phba->HAregaddr); /* flush */
614	spin_unlock_irq(phba->host->host_lock);
615	lpfc_linkdown(phba);
616	phba->hba_state = LPFC_HBA_ERROR;
617
618	/* The other case is an error from issue_mbox */
619	if (rc == -ENOMEM)
620		lpfc_printf_log(phba,
621				KERN_WARNING,
622				LOG_MBOX,
623			        "%d:0300 READ_LA: no buffers\n",
624				phba->brd_no);
625
626	return;
627}
628
629/************************************************************************/
630/*                                                                      */
631/*   lpfc_parse_vpd                                                     */
632/*   This routine will parse the VPD data                               */
633/*                                                                      */
634/************************************************************************/
635static int
636lpfc_parse_vpd(struct lpfc_hba * phba, uint8_t * vpd)
637{
638	uint8_t lenlo, lenhi;
639	uint32_t Length;
640	int i, j;
641	int finished = 0;
642	int index = 0;
643
644	if (!vpd)
645		return 0;
646
647	/* Vital Product */
648	lpfc_printf_log(phba,
649			KERN_INFO,
650			LOG_INIT,
651			"%d:0455 Vital Product Data: x%x x%x x%x x%x\n",
652			phba->brd_no,
653			(uint32_t) vpd[0], (uint32_t) vpd[1], (uint32_t) vpd[2],
654			(uint32_t) vpd[3]);
655	do {
656		switch (vpd[index]) {
657		case 0x82:
658			index += 1;
659			lenlo = vpd[index];
660			index += 1;
661			lenhi = vpd[index];
662			index += 1;
663			i = ((((unsigned short)lenhi) << 8) + lenlo);
664			index += i;
665			break;
666		case 0x90:
667			index += 1;
668			lenlo = vpd[index];
669			index += 1;
670			lenhi = vpd[index];
671			index += 1;
672			Length = ((((unsigned short)lenhi) << 8) + lenlo);
673
674			while (Length > 0) {
675			/* Look for Serial Number */
676			if ((vpd[index] == 'S') && (vpd[index+1] == 'N')) {
677				index += 2;
678				i = vpd[index];
679				index += 1;
680				j = 0;
681				Length -= (3+i);
682				while(i--) {
683					phba->SerialNumber[j++] = vpd[index++];
684					if (j == 31)
685						break;
686				}
687				phba->SerialNumber[j] = 0;
688				continue;
689			}
690			else if ((vpd[index] == 'V') && (vpd[index+1] == '1')) {
691				phba->vpd_flag |= VPD_MODEL_DESC;
692				index += 2;
693				i = vpd[index];
694				index += 1;
695				j = 0;
696				Length -= (3+i);
697				while(i--) {
698					phba->ModelDesc[j++] = vpd[index++];
699					if (j == 255)
700						break;
701				}
702				phba->ModelDesc[j] = 0;
703				continue;
704			}
705			else if ((vpd[index] == 'V') && (vpd[index+1] == '2')) {
706				phba->vpd_flag |= VPD_MODEL_NAME;
707				index += 2;
708				i = vpd[index];
709				index += 1;
710				j = 0;
711				Length -= (3+i);
712				while(i--) {
713					phba->ModelName[j++] = vpd[index++];
714					if (j == 79)
715						break;
716				}
717				phba->ModelName[j] = 0;
718				continue;
719			}
720			else if ((vpd[index] == 'V') && (vpd[index+1] == '3')) {
721				phba->vpd_flag |= VPD_PROGRAM_TYPE;
722				index += 2;
723				i = vpd[index];
724				index += 1;
725				j = 0;
726				Length -= (3+i);
727				while(i--) {
728					phba->ProgramType[j++] = vpd[index++];
729					if (j == 255)
730						break;
731				}
732				phba->ProgramType[j] = 0;
733				continue;
734			}
735			else if ((vpd[index] == 'V') && (vpd[index+1] == '4')) {
736				phba->vpd_flag |= VPD_PORT;
737				index += 2;
738				i = vpd[index];
739				index += 1;
740				j = 0;
741				Length -= (3+i);
742				while(i--) {
743				phba->Port[j++] = vpd[index++];
744				if (j == 19)
745					break;
746				}
747				phba->Port[j] = 0;
748				continue;
749			}
750			else {
751				index += 2;
752				i = vpd[index];
753				index += 1;
754				index += i;
755				Length -= (3 + i);
756			}
757		}
758		finished = 0;
759		break;
760		case 0x78:
761			finished = 1;
762			break;
763		default:
764			index ++;
765			break;
766		}
767	} while (!finished && (index < 108));
768
769	return(1);
770}
771
772static void
773lpfc_get_hba_model_desc(struct lpfc_hba * phba, uint8_t * mdp, uint8_t * descp)
774{
775	lpfc_vpd_t *vp;
776	uint32_t id;
777	char str[16];
778
779	vp = &phba->vpd;
780	pci_read_config_dword(phba->pcidev, PCI_VENDOR_ID, &id);
781
782	switch ((id >> 16) & 0xffff) {
783	case PCI_DEVICE_ID_SUPERFLY:
784		if (vp->rev.biuRev >= 1 && vp->rev.biuRev <= 3)
785			strcpy(str, "LP7000 1");
786		else
787			strcpy(str, "LP7000E 1");
788		break;
789	case PCI_DEVICE_ID_DRAGONFLY:
790		strcpy(str, "LP8000 1");
791		break;
792	case PCI_DEVICE_ID_CENTAUR:
793		if (FC_JEDEC_ID(vp->rev.biuRev) == CENTAUR_2G_JEDEC_ID)
794			strcpy(str, "LP9002 2");
795		else
796			strcpy(str, "LP9000 1");
797		break;
798	case PCI_DEVICE_ID_RFLY:
799		strcpy(str, "LP952 2");
800		break;
801	case PCI_DEVICE_ID_PEGASUS:
802		strcpy(str, "LP9802 2");
803		break;
804	case PCI_DEVICE_ID_THOR:
805		strcpy(str, "LP10000 2");
806		break;
807	case PCI_DEVICE_ID_VIPER:
808		strcpy(str, "LPX1000 10");
809		break;
810	case PCI_DEVICE_ID_PFLY:
811		strcpy(str, "LP982 2");
812		break;
813	case PCI_DEVICE_ID_TFLY:
814		strcpy(str, "LP1050 2");
815		break;
816	case PCI_DEVICE_ID_HELIOS:
817		strcpy(str, "LP11000 4");
818		break;
819	case PCI_DEVICE_ID_BMID:
820		strcpy(str, "LP1150 4");
821		break;
822	case PCI_DEVICE_ID_BSMB:
823		strcpy(str, "LP111 4");
824		break;
825	case PCI_DEVICE_ID_ZEPHYR:
826		strcpy(str, "LP11000e 4");
827		break;
828	case PCI_DEVICE_ID_ZMID:
829		strcpy(str, "LP1150e 4");
830		break;
831	case PCI_DEVICE_ID_ZSMB:
832		strcpy(str, "LP111e 4");
833		break;
834	case PCI_DEVICE_ID_LP101:
835		strcpy(str, "LP101 2");
836		break;
837	case PCI_DEVICE_ID_LP10000S:
838		strcpy(str, "LP10000-S 2");
839		break;
840	}
841	if (mdp)
842		sscanf(str, "%s", mdp);
843	if (descp)
844		sprintf(descp, "Emulex LightPulse %s Gigabit PCI Fibre "
845			"Channel Adapter", str);
846}
847
848/**************************************************/
849/*   lpfc_post_buffer                             */
850/*                                                */
851/*   This routine will post count buffers to the  */
852/*   ring with the QUE_RING_BUF_CN command. This  */
853/*   allows 3 buffers / command to be posted.     */
854/*   Returns the number of buffers NOT posted.    */
855/**************************************************/
856int
857lpfc_post_buffer(struct lpfc_hba * phba, struct lpfc_sli_ring * pring, int cnt,
858		 int type)
859{
860	IOCB_t *icmd;
861	struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
862	struct lpfc_iocbq *iocb = NULL;
863	struct lpfc_dmabuf *mp1, *mp2;
864
865	cnt += pring->missbufcnt;
866
867	/* While there are buffers to post */
868	while (cnt > 0) {
869		/* Allocate buffer for  command iocb */
870		spin_lock_irq(phba->host->host_lock);
871		list_remove_head(lpfc_iocb_list, iocb, struct lpfc_iocbq, list);
872		spin_unlock_irq(phba->host->host_lock);
873		if (iocb == NULL) {
874			pring->missbufcnt = cnt;
875			return cnt;
876		}
877		memset(iocb, 0, sizeof (struct lpfc_iocbq));
878		icmd = &iocb->iocb;
879
880		/* 2 buffers can be posted per command */
881		/* Allocate buffer to post */
882		mp1 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
883		if (mp1)
884		    mp1->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
885						&mp1->phys);
886		if (mp1 == 0 || mp1->virt == 0) {
887			if (mp1)
888				kfree(mp1);
889			spin_lock_irq(phba->host->host_lock);
890			list_add_tail(&iocb->list, lpfc_iocb_list);
891			spin_unlock_irq(phba->host->host_lock);
892			pring->missbufcnt = cnt;
893			return cnt;
894		}
895
896		INIT_LIST_HEAD(&mp1->list);
897		/* Allocate buffer to post */
898		if (cnt > 1) {
899			mp2 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
900			if (mp2)
901				mp2->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
902							    &mp2->phys);
903			if (mp2 == 0 || mp2->virt == 0) {
904				if (mp2)
905					kfree(mp2);
906				lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
907				kfree(mp1);
908				spin_lock_irq(phba->host->host_lock);
909				list_add_tail(&iocb->list, lpfc_iocb_list);
910				spin_unlock_irq(phba->host->host_lock);
911				pring->missbufcnt = cnt;
912				return cnt;
913			}
914
915			INIT_LIST_HEAD(&mp2->list);
916		} else {
917			mp2 = NULL;
918		}
919
920		icmd->un.cont64[0].addrHigh = putPaddrHigh(mp1->phys);
921		icmd->un.cont64[0].addrLow = putPaddrLow(mp1->phys);
922		icmd->un.cont64[0].tus.f.bdeSize = FCELSSIZE;
923		icmd->ulpBdeCount = 1;
924		cnt--;
925		if (mp2) {
926			icmd->un.cont64[1].addrHigh = putPaddrHigh(mp2->phys);
927			icmd->un.cont64[1].addrLow = putPaddrLow(mp2->phys);
928			icmd->un.cont64[1].tus.f.bdeSize = FCELSSIZE;
929			cnt--;
930			icmd->ulpBdeCount = 2;
931		}
932
933		icmd->ulpCommand = CMD_QUE_RING_BUF64_CN;
934		icmd->ulpLe = 1;
935
936		spin_lock_irq(phba->host->host_lock);
937		if (lpfc_sli_issue_iocb(phba, pring, iocb, 0) == IOCB_ERROR) {
938			lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
939			kfree(mp1);
940			cnt++;
941			if (mp2) {
942				lpfc_mbuf_free(phba, mp2->virt, mp2->phys);
943				kfree(mp2);
944				cnt++;
945			}
946			list_add_tail(&iocb->list, lpfc_iocb_list);
947			pring->missbufcnt = cnt;
948			spin_unlock_irq(phba->host->host_lock);
949			return cnt;
950		}
951		spin_unlock_irq(phba->host->host_lock);
952		lpfc_sli_ringpostbuf_put(phba, pring, mp1);
953		if (mp2) {
954			lpfc_sli_ringpostbuf_put(phba, pring, mp2);
955		}
956	}
957	pring->missbufcnt = 0;
958	return 0;
959}
960
961/************************************************************************/
962/*                                                                      */
963/*   lpfc_post_rcv_buf                                                  */
964/*   This routine post initial rcv buffers to the configured rings      */
965/*                                                                      */
966/************************************************************************/
967static int
968lpfc_post_rcv_buf(struct lpfc_hba * phba)
969{
970	struct lpfc_sli *psli = &phba->sli;
971
972	/* Ring 0, ELS / CT buffers */
973	lpfc_post_buffer(phba, &psli->ring[LPFC_ELS_RING], LPFC_BUF_RING0, 1);
974	/* Ring 2 - FCP no buffers needed */
975
976	return 0;
977}
978
979#define S(N,V) (((V)<<(N))|((V)>>(32-(N))))
980
981/************************************************************************/
982/*                                                                      */
983/*   lpfc_sha_init                                                      */
984/*                                                                      */
985/************************************************************************/
986static void
987lpfc_sha_init(uint32_t * HashResultPointer)
988{
989	HashResultPointer[0] = 0x67452301;
990	HashResultPointer[1] = 0xEFCDAB89;
991	HashResultPointer[2] = 0x98BADCFE;
992	HashResultPointer[3] = 0x10325476;
993	HashResultPointer[4] = 0xC3D2E1F0;
994}
995
996/************************************************************************/
997/*                                                                      */
998/*   lpfc_sha_iterate                                                   */
999/*                                                                      */
1000/************************************************************************/
1001static void
1002lpfc_sha_iterate(uint32_t * HashResultPointer, uint32_t * HashWorkingPointer)
1003{
1004	int t;
1005	uint32_t TEMP;
1006	uint32_t A, B, C, D, E;
1007	t = 16;
1008	do {
1009		HashWorkingPointer[t] =
1010		    S(1,
1011		      HashWorkingPointer[t - 3] ^ HashWorkingPointer[t -
1012								     8] ^
1013		      HashWorkingPointer[t - 14] ^ HashWorkingPointer[t - 16]);
1014	} while (++t <= 79);
1015	t = 0;
1016	A = HashResultPointer[0];
1017	B = HashResultPointer[1];
1018	C = HashResultPointer[2];
1019	D = HashResultPointer[3];
1020	E = HashResultPointer[4];
1021
1022	do {
1023		if (t < 20) {
1024			TEMP = ((B & C) | ((~B) & D)) + 0x5A827999;
1025		} else if (t < 40) {
1026			TEMP = (B ^ C ^ D) + 0x6ED9EBA1;
1027		} else if (t < 60) {
1028			TEMP = ((B & C) | (B & D) | (C & D)) + 0x8F1BBCDC;
1029		} else {
1030			TEMP = (B ^ C ^ D) + 0xCA62C1D6;
1031		}
1032		TEMP += S(5, A) + E + HashWorkingPointer[t];
1033		E = D;
1034		D = C;
1035		C = S(30, B);
1036		B = A;
1037		A = TEMP;
1038	} while (++t <= 79);
1039
1040	HashResultPointer[0] += A;
1041	HashResultPointer[1] += B;
1042	HashResultPointer[2] += C;
1043	HashResultPointer[3] += D;
1044	HashResultPointer[4] += E;
1045
1046}
1047
1048/************************************************************************/
1049/*                                                                      */
1050/*   lpfc_challenge_key                                                 */
1051/*                                                                      */
1052/************************************************************************/
1053static void
1054lpfc_challenge_key(uint32_t * RandomChallenge, uint32_t * HashWorking)
1055{
1056	*HashWorking = (*RandomChallenge ^ *HashWorking);
1057}
1058
1059/************************************************************************/
1060/*                                                                      */
1061/*   lpfc_hba_init                                                      */
1062/*                                                                      */
1063/************************************************************************/
1064void
1065lpfc_hba_init(struct lpfc_hba *phba, uint32_t *hbainit)
1066{
1067	int t;
1068	uint32_t *HashWorking;
1069	uint32_t *pwwnn = phba->wwnn;
1070
1071	HashWorking = kmalloc(80 * sizeof(uint32_t), GFP_KERNEL);
1072	if (!HashWorking)
1073		return;
1074
1075	memset(HashWorking, 0, (80 * sizeof(uint32_t)));
1076	HashWorking[0] = HashWorking[78] = *pwwnn++;
1077	HashWorking[1] = HashWorking[79] = *pwwnn;
1078
1079	for (t = 0; t < 7; t++)
1080		lpfc_challenge_key(phba->RandomData + t, HashWorking + t);
1081
1082	lpfc_sha_init(hbainit);
1083	lpfc_sha_iterate(hbainit, HashWorking);
1084	kfree(HashWorking);
1085}
1086
1087static void
1088lpfc_cleanup(struct lpfc_hba * phba, uint32_t save_bind)
1089{
1090	struct lpfc_nodelist *ndlp, *next_ndlp;
1091
1092	/* clean up phba - lpfc specific */
1093	lpfc_can_disctmo(phba);
1094	list_for_each_entry_safe(ndlp, next_ndlp, &phba->fc_nlpunmap_list,
1095				nlp_listp) {
1096		lpfc_nlp_remove(phba, ndlp);
1097	}
1098
1099	list_for_each_entry_safe(ndlp, next_ndlp, &phba->fc_nlpmap_list,
1100				 nlp_listp) {
1101		lpfc_nlp_remove(phba, ndlp);
1102	}
1103
1104	list_for_each_entry_safe(ndlp, next_ndlp, &phba->fc_unused_list,
1105				nlp_listp) {
1106		lpfc_nlp_list(phba, ndlp, NLP_NO_LIST);
1107	}
1108
1109	list_for_each_entry_safe(ndlp, next_ndlp, &phba->fc_plogi_list,
1110				nlp_listp) {
1111		lpfc_nlp_remove(phba, ndlp);
1112	}
1113
1114	list_for_each_entry_safe(ndlp, next_ndlp, &phba->fc_adisc_list,
1115				nlp_listp) {
1116		lpfc_nlp_remove(phba, ndlp);
1117	}
1118
1119	list_for_each_entry_safe(ndlp, next_ndlp, &phba->fc_reglogin_list,
1120				nlp_listp) {
1121		lpfc_nlp_remove(phba, ndlp);
1122	}
1123
1124	list_for_each_entry_safe(ndlp, next_ndlp, &phba->fc_prli_list,
1125				nlp_listp) {
1126		lpfc_nlp_remove(phba, ndlp);
1127	}
1128
1129	list_for_each_entry_safe(ndlp, next_ndlp, &phba->fc_npr_list,
1130				nlp_listp) {
1131		lpfc_nlp_remove(phba, ndlp);
1132	}
1133
1134	INIT_LIST_HEAD(&phba->fc_nlpmap_list);
1135	INIT_LIST_HEAD(&phba->fc_nlpunmap_list);
1136	INIT_LIST_HEAD(&phba->fc_unused_list);
1137	INIT_LIST_HEAD(&phba->fc_plogi_list);
1138	INIT_LIST_HEAD(&phba->fc_adisc_list);
1139	INIT_LIST_HEAD(&phba->fc_reglogin_list);
1140	INIT_LIST_HEAD(&phba->fc_prli_list);
1141	INIT_LIST_HEAD(&phba->fc_npr_list);
1142
1143	phba->fc_map_cnt   = 0;
1144	phba->fc_unmap_cnt = 0;
1145	phba->fc_plogi_cnt = 0;
1146	phba->fc_adisc_cnt = 0;
1147	phba->fc_reglogin_cnt = 0;
1148	phba->fc_prli_cnt  = 0;
1149	phba->fc_npr_cnt   = 0;
1150	phba->fc_unused_cnt= 0;
1151	return;
1152}
1153
1154static void
1155lpfc_establish_link_tmo(unsigned long ptr)
1156{
1157	struct lpfc_hba *phba = (struct lpfc_hba *)ptr;
1158	unsigned long iflag;
1159
1160
1161	/* Re-establishing Link, timer expired */
1162	lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
1163			"%d:1300 Re-establishing Link, timer expired "
1164			"Data: x%x x%x\n",
1165			phba->brd_no, phba->fc_flag, phba->hba_state);
1166	spin_lock_irqsave(phba->host->host_lock, iflag);
1167	phba->fc_flag &= ~FC_ESTABLISH_LINK;
1168	spin_unlock_irqrestore(phba->host->host_lock, iflag);
1169}
1170
1171static int
1172lpfc_stop_timer(struct lpfc_hba * phba)
1173{
1174	struct lpfc_sli *psli = &phba->sli;
1175
1176	/* Instead of a timer, this has been converted to a
1177	 * deferred procedding list.
1178	 */
1179	while (!list_empty(&phba->freebufList)) {
1180
1181		struct lpfc_dmabuf *mp = NULL;
1182
1183		list_remove_head((&phba->freebufList), mp,
1184				 struct lpfc_dmabuf, list);
1185		if (mp) {
1186			lpfc_mbuf_free(phba, mp->virt, mp->phys);
1187			kfree(mp);
1188		}
1189	}
1190
1191	del_timer_sync(&phba->fc_estabtmo);
1192	del_timer_sync(&phba->fc_disctmo);
1193	del_timer_sync(&phba->fc_fdmitmo);
1194	del_timer_sync(&phba->els_tmofunc);
1195	psli = &phba->sli;
1196	del_timer_sync(&psli->mbox_tmo);
1197	return(1);
1198}
1199
1200int
1201lpfc_online(struct lpfc_hba * phba)
1202{
1203	if (!phba)
1204		return 0;
1205
1206	if (!(phba->fc_flag & FC_OFFLINE_MODE))
1207		return 0;
1208
1209	lpfc_printf_log(phba,
1210		       KERN_WARNING,
1211		       LOG_INIT,
1212		       "%d:0458 Bring Adapter online\n",
1213		       phba->brd_no);
1214
1215	if (!lpfc_sli_queue_setup(phba))
1216		return 1;
1217
1218	if (lpfc_sli_hba_setup(phba))	/* Initialize the HBA */
1219		return 1;
1220
1221	spin_lock_irq(phba->host->host_lock);
1222	phba->fc_flag &= ~FC_OFFLINE_MODE;
1223	spin_unlock_irq(phba->host->host_lock);
1224
1225	/*
1226	 * Restart all traffic to this host.  Since the fc_transport block
1227	 * functions (future) were not called in lpfc_offline, don't call them
1228	 * here.
1229	 */
1230	scsi_unblock_requests(phba->host);
1231	return 0;
1232}
1233
1234int
1235lpfc_offline(struct lpfc_hba * phba)
1236{
1237	struct lpfc_sli_ring *pring;
1238	struct lpfc_sli *psli;
1239	unsigned long iflag;
1240	int i = 0;
1241
1242	if (!phba)
1243		return 0;
1244
1245	if (phba->fc_flag & FC_OFFLINE_MODE)
1246		return 0;
1247
1248	/*
1249	 * Don't call the fc_transport block api (future).  The device is
1250	 * going offline and causing a timer to fire in the midlayer is
1251	 * unproductive.  Just block all new requests until the driver
1252	 * comes back online.
1253	 */
1254	scsi_block_requests(phba->host);
1255	psli = &phba->sli;
1256	pring = &psli->ring[psli->fcp_ring];
1257
1258	lpfc_linkdown(phba);
1259
1260	/* The linkdown event takes 30 seconds to timeout. */
1261	while (pring->txcmplq_cnt) {
1262		mdelay(10);
1263		if (i++ > 3000)
1264			break;
1265	}
1266
1267	/* stop all timers associated with this hba */
1268	lpfc_stop_timer(phba);
1269	phba->work_hba_events = 0;
1270
1271	lpfc_printf_log(phba,
1272		       KERN_WARNING,
1273		       LOG_INIT,
1274		       "%d:0460 Bring Adapter offline\n",
1275		       phba->brd_no);
1276
1277	/* Bring down the SLI Layer and cleanup.  The HBA is offline
1278	   now.  */
1279	lpfc_sli_hba_down(phba);
1280	lpfc_cleanup(phba, 1);
1281	spin_lock_irqsave(phba->host->host_lock, iflag);
1282	phba->fc_flag |= FC_OFFLINE_MODE;
1283	spin_unlock_irqrestore(phba->host->host_lock, iflag);
1284	return 0;
1285}
1286
1287/******************************************************************************
1288* Function name: lpfc_scsi_free
1289*
1290* Description: Called from lpfc_pci_remove_one free internal driver resources
1291*
1292******************************************************************************/
1293static int
1294lpfc_scsi_free(struct lpfc_hba * phba)
1295{
1296	struct lpfc_scsi_buf *sb, *sb_next;
1297	struct lpfc_iocbq *io, *io_next;
1298
1299	spin_lock_irq(phba->host->host_lock);
1300	/* Release all the lpfc_scsi_bufs maintained by this host. */
1301	list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list, list) {
1302		list_del(&sb->list);
1303		pci_pool_free(phba->lpfc_scsi_dma_buf_pool, sb->data,
1304								sb->dma_handle);
1305		kfree(sb);
1306		phba->total_scsi_bufs--;
1307	}
1308
1309	/* Release all the lpfc_iocbq entries maintained by this host. */
1310	list_for_each_entry_safe(io, io_next, &phba->lpfc_iocb_list, list) {
1311		list_del(&io->list);
1312		kfree(io);
1313		phba->total_iocbq_bufs--;
1314	}
1315
1316	spin_unlock_irq(phba->host->host_lock);
1317
1318	return 0;
1319}
1320
1321
1322static int __devinit
1323lpfc_pci_probe_one(struct pci_dev *pdev, const struct pci_device_id *pid)
1324{
1325	struct Scsi_Host *host;
1326	struct lpfc_hba  *phba;
1327	struct lpfc_sli  *psli;
1328	struct lpfc_iocbq *iocbq_entry = NULL, *iocbq_next = NULL;
1329	unsigned long bar0map_len, bar2map_len;
1330	int error = -ENODEV, retval;
1331	int i;
1332	u64 wwname;
1333
1334	if (pci_enable_device(pdev))
1335		goto out;
1336	if (pci_request_regions(pdev, LPFC_DRIVER_NAME))
1337		goto out_disable_device;
1338
1339	host = scsi_host_alloc(&lpfc_template,
1340			sizeof (struct lpfc_hba) + sizeof (unsigned long));
1341	if (!host)
1342		goto out_release_regions;
1343
1344	phba = (struct lpfc_hba*)host->hostdata;
1345	memset(phba, 0, sizeof (struct lpfc_hba));
1346	phba->link_stats = (void *)&phba[1];
1347	phba->host = host;
1348
1349	phba->fc_flag |= FC_LOADING;
1350	phba->pcidev = pdev;
1351
1352	/* Assign an unused board number */
1353	if (!idr_pre_get(&lpfc_hba_index, GFP_KERNEL))
1354		goto out_put_host;
1355
1356	error = idr_get_new(&lpfc_hba_index, NULL, &phba->brd_no);
1357	if (error)
1358		goto out_put_host;
1359
1360	host->unique_id = phba->brd_no;
1361
1362	INIT_LIST_HEAD(&phba->ctrspbuflist);
1363	INIT_LIST_HEAD(&phba->rnidrspbuflist);
1364	INIT_LIST_HEAD(&phba->freebufList);
1365
1366	/* Initialize timers used by driver */
1367	init_timer(&phba->fc_estabtmo);
1368	phba->fc_estabtmo.function = lpfc_establish_link_tmo;
1369	phba->fc_estabtmo.data = (unsigned long)phba;
1370	init_timer(&phba->fc_disctmo);
1371	phba->fc_disctmo.function = lpfc_disc_timeout;
1372	phba->fc_disctmo.data = (unsigned long)phba;
1373
1374	init_timer(&phba->fc_fdmitmo);
1375	phba->fc_fdmitmo.function = lpfc_fdmi_tmo;
1376	phba->fc_fdmitmo.data = (unsigned long)phba;
1377	init_timer(&phba->els_tmofunc);
1378	phba->els_tmofunc.function = lpfc_els_timeout;
1379	phba->els_tmofunc.data = (unsigned long)phba;
1380	psli = &phba->sli;
1381	init_timer(&psli->mbox_tmo);
1382	psli->mbox_tmo.function = lpfc_mbox_timeout;
1383	psli->mbox_tmo.data = (unsigned long)phba;
1384
1385	/*
1386	 * Get all the module params for configuring this host and then
1387	 * establish the host parameters.
1388	 */
1389	lpfc_get_cfgparam(phba);
1390
1391	host->max_id = LPFC_MAX_TARGET;
1392	host->max_lun = phba->cfg_max_luns;
1393	host->this_id = -1;
1394
1395	/* Initialize all internally managed lists. */
1396	INIT_LIST_HEAD(&phba->fc_nlpmap_list);
1397	INIT_LIST_HEAD(&phba->fc_nlpunmap_list);
1398	INIT_LIST_HEAD(&phba->fc_unused_list);
1399	INIT_LIST_HEAD(&phba->fc_plogi_list);
1400	INIT_LIST_HEAD(&phba->fc_adisc_list);
1401	INIT_LIST_HEAD(&phba->fc_reglogin_list);
1402	INIT_LIST_HEAD(&phba->fc_prli_list);
1403	INIT_LIST_HEAD(&phba->fc_npr_list);
1404
1405
1406	pci_set_master(pdev);
1407	retval = pci_set_mwi(pdev);
1408	if (retval)
1409		dev_printk(KERN_WARNING, &pdev->dev,
1410			   "Warning: pci_set_mwi returned %d\n", retval);
1411
1412	if (pci_set_dma_mask(phba->pcidev, DMA_64BIT_MASK) != 0)
1413		if (pci_set_dma_mask(phba->pcidev, DMA_32BIT_MASK) != 0)
1414			goto out_idr_remove;
1415
1416	/*
1417	 * Get the bus address of Bar0 and Bar2 and the number of bytes
1418	 * required by each mapping.
1419	 */
1420	phba->pci_bar0_map = pci_resource_start(phba->pcidev, 0);
1421	bar0map_len        = pci_resource_len(phba->pcidev, 0);
1422
1423	phba->pci_bar2_map = pci_resource_start(phba->pcidev, 2);
1424	bar2map_len        = pci_resource_len(phba->pcidev, 2);
1425
1426	/* Map HBA SLIM and Control Registers to a kernel virtual address. */
1427	phba->slim_memmap_p      = ioremap(phba->pci_bar0_map, bar0map_len);
1428	phba->ctrl_regs_memmap_p = ioremap(phba->pci_bar2_map, bar2map_len);
1429
1430	/* Allocate memory for SLI-2 structures */
1431	phba->slim2p = dma_alloc_coherent(&phba->pcidev->dev, SLI2_SLIM_SIZE,
1432					  &phba->slim2p_mapping, GFP_KERNEL);
1433	if (!phba->slim2p)
1434		goto out_iounmap;
1435
1436
1437	/* Initialize the SLI Layer to run with lpfc HBAs. */
1438	lpfc_sli_setup(phba);
1439	lpfc_sli_queue_setup(phba);
1440
1441	error = lpfc_mem_alloc(phba);
1442	if (error)
1443		goto out_free_slim;
1444
1445	/* Initialize and populate the iocb list per host.  */
1446	INIT_LIST_HEAD(&phba->lpfc_iocb_list);
1447	for (i = 0; i < LPFC_IOCB_LIST_CNT; i++) {
1448		iocbq_entry = kmalloc(sizeof(struct lpfc_iocbq), GFP_KERNEL);
1449		if (iocbq_entry == NULL) {
1450			printk(KERN_ERR "%s: only allocated %d iocbs of "
1451				"expected %d count. Unloading driver.\n",
1452				__FUNCTION__, i, LPFC_IOCB_LIST_CNT);
1453			error = -ENOMEM;
1454			goto out_free_iocbq;
1455		}
1456
1457		memset(iocbq_entry, 0, sizeof(struct lpfc_iocbq));
1458		spin_lock_irq(phba->host->host_lock);
1459		list_add(&iocbq_entry->list, &phba->lpfc_iocb_list);
1460		phba->total_iocbq_bufs++;
1461		spin_unlock_irq(phba->host->host_lock);
1462	}
1463
1464	/* Initialize HBA structure */
1465	phba->fc_edtov = FF_DEF_EDTOV;
1466	phba->fc_ratov = FF_DEF_RATOV;
1467	phba->fc_altov = FF_DEF_ALTOV;
1468	phba->fc_arbtov = FF_DEF_ARBTOV;
1469
1470	INIT_LIST_HEAD(&phba->work_list);
1471	phba->work_ha_mask = (HA_ERATT|HA_MBATT|HA_LATT);
1472	phba->work_ha_mask |= (HA_RXMASK << (LPFC_ELS_RING * 4));
1473
1474	/* Startup the kernel thread for this host adapter. */
1475	phba->worker_thread = kthread_run(lpfc_do_work, phba,
1476				       "lpfc_worker_%d", phba->brd_no);
1477	if (IS_ERR(phba->worker_thread)) {
1478		error = PTR_ERR(phba->worker_thread);
1479		goto out_free_iocbq;
1480	}
1481
1482	/* We can rely on a queue depth attribute only after SLI HBA setup */
1483	host->can_queue = phba->cfg_hba_queue_depth - 10;
1484
1485	/* Tell the midlayer we support 16 byte commands */
1486	host->max_cmd_len = 16;
1487
1488	/* Initialize the list of scsi buffers used by driver for scsi IO. */
1489	INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list);
1490
1491	host->transportt = lpfc_transport_template;
1492	host->hostdata[0] = (unsigned long)phba;
1493	pci_set_drvdata(pdev, host);
1494	error = scsi_add_host(host, &pdev->dev);
1495	if (error)
1496		goto out_kthread_stop;
1497
1498	error = lpfc_alloc_sysfs_attr(phba);
1499	if (error)
1500		goto out_kthread_stop;
1501
1502	error =	request_irq(phba->pcidev->irq, lpfc_intr_handler, SA_SHIRQ,
1503							LPFC_DRIVER_NAME, phba);
1504	if (error) {
1505		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1506			"%d:0451 Enable interrupt handler failed\n",
1507			phba->brd_no);
1508		goto out_free_sysfs_attr;
1509	}
1510	phba->MBslimaddr = phba->slim_memmap_p;
1511	phba->HAregaddr = phba->ctrl_regs_memmap_p + HA_REG_OFFSET;
1512	phba->CAregaddr = phba->ctrl_regs_memmap_p + CA_REG_OFFSET;
1513	phba->HSregaddr = phba->ctrl_regs_memmap_p + HS_REG_OFFSET;
1514	phba->HCregaddr = phba->ctrl_regs_memmap_p + HC_REG_OFFSET;
1515
1516	error = lpfc_sli_hba_setup(phba);
1517	if (error)
1518		goto out_free_irq;
1519
1520	/*
1521	 * set fixed host attributes
1522	 * Must done after lpfc_sli_hba_setup()
1523	 */
1524
1525	memcpy(&wwname, &phba->fc_nodename, sizeof(u64));
1526	fc_host_node_name(host) = be64_to_cpu(wwname);
1527	memcpy(&wwname, &phba->fc_portname, sizeof(u64));
1528	fc_host_port_name(host) = be64_to_cpu(wwname);
1529	fc_host_supported_classes(host) = FC_COS_CLASS3;
1530
1531	memset(fc_host_supported_fc4s(host), 0,
1532		sizeof(fc_host_supported_fc4s(host)));
1533	fc_host_supported_fc4s(host)[2] = 1;
1534	fc_host_supported_fc4s(host)[7] = 1;
1535
1536	lpfc_get_hba_sym_node_name(phba, fc_host_symbolic_name(host));
1537
1538	fc_host_supported_speeds(host) = 0;
1539	switch (FC_JEDEC_ID(phba->vpd.rev.biuRev)) {
1540	case VIPER_JEDEC_ID:
1541		fc_host_supported_speeds(host) |= FC_PORTSPEED_10GBIT;
1542		break;
1543	case HELIOS_JEDEC_ID:
1544		fc_host_supported_speeds(host) |= FC_PORTSPEED_4GBIT;
1545		/* Fall through */
1546	case CENTAUR_2G_JEDEC_ID:
1547	case PEGASUS_JEDEC_ID:
1548	case THOR_JEDEC_ID:
1549		fc_host_supported_speeds(host) |= FC_PORTSPEED_2GBIT;
1550		/* Fall through */
1551	default:
1552		fc_host_supported_speeds(host) = FC_PORTSPEED_1GBIT;
1553	}
1554
1555	fc_host_maxframe_size(host) =
1556		((((uint32_t) phba->fc_sparam.cmn.bbRcvSizeMsb & 0x0F) << 8) |
1557		 (uint32_t) phba->fc_sparam.cmn.bbRcvSizeLsb);
1558
1559	/* This value is also unchanging */
1560	memset(fc_host_active_fc4s(host), 0,
1561		sizeof(fc_host_active_fc4s(host)));
1562	fc_host_active_fc4s(host)[2] = 1;
1563	fc_host_active_fc4s(host)[7] = 1;
1564
1565	spin_lock_irq(phba->host->host_lock);
1566	phba->fc_flag &= ~FC_LOADING;
1567	spin_unlock_irq(phba->host->host_lock);
1568	return 0;
1569
1570out_free_irq:
1571	lpfc_stop_timer(phba);
1572	phba->work_hba_events = 0;
1573	free_irq(phba->pcidev->irq, phba);
1574out_free_sysfs_attr:
1575	lpfc_free_sysfs_attr(phba);
1576out_kthread_stop:
1577	kthread_stop(phba->worker_thread);
1578out_free_iocbq:
1579	list_for_each_entry_safe(iocbq_entry, iocbq_next,
1580						&phba->lpfc_iocb_list, list) {
1581		spin_lock_irq(phba->host->host_lock);
1582		kfree(iocbq_entry);
1583		phba->total_iocbq_bufs--;
1584		spin_unlock_irq(phba->host->host_lock);
1585	}
1586	lpfc_mem_free(phba);
1587out_free_slim:
1588	dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE, phba->slim2p,
1589							phba->slim2p_mapping);
1590out_iounmap:
1591	iounmap(phba->ctrl_regs_memmap_p);
1592	iounmap(phba->slim_memmap_p);
1593out_idr_remove:
1594	idr_remove(&lpfc_hba_index, phba->brd_no);
1595out_put_host:
1596	scsi_host_put(host);
1597out_release_regions:
1598	pci_release_regions(pdev);
1599out_disable_device:
1600	pci_disable_device(pdev);
1601out:
1602	return error;
1603}
1604
1605static void __devexit
1606lpfc_pci_remove_one(struct pci_dev *pdev)
1607{
1608	struct Scsi_Host   *host = pci_get_drvdata(pdev);
1609	struct lpfc_hba    *phba = (struct lpfc_hba *)host->hostdata[0];
1610	unsigned long iflag;
1611
1612	lpfc_free_sysfs_attr(phba);
1613
1614	spin_lock_irqsave(phba->host->host_lock, iflag);
1615	phba->fc_flag |= FC_UNLOADING;
1616
1617	spin_unlock_irqrestore(phba->host->host_lock, iflag);
1618
1619	fc_remove_host(phba->host);
1620	scsi_remove_host(phba->host);
1621
1622	kthread_stop(phba->worker_thread);
1623
1624	/*
1625	 * Bring down the SLI Layer. This step disable all interrupts,
1626	 * clears the rings, discards all mailbox commands, and resets
1627	 * the HBA.
1628	 */
1629	lpfc_sli_hba_down(phba);
1630
1631	/* Release the irq reservation */
1632	free_irq(phba->pcidev->irq, phba);
1633
1634	lpfc_cleanup(phba, 0);
1635	lpfc_stop_timer(phba);
1636	phba->work_hba_events = 0;
1637
1638	/*
1639	 * Call scsi_free before mem_free since scsi bufs are released to their
1640	 * corresponding pools here.
1641	 */
1642	lpfc_scsi_free(phba);
1643	lpfc_mem_free(phba);
1644
1645	/* Free resources associated with SLI2 interface */
1646	dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
1647			  phba->slim2p, phba->slim2p_mapping);
1648
1649	/* unmap adapter SLIM and Control Registers */
1650	iounmap(phba->ctrl_regs_memmap_p);
1651	iounmap(phba->slim_memmap_p);
1652
1653	pci_release_regions(phba->pcidev);
1654	pci_disable_device(phba->pcidev);
1655
1656	idr_remove(&lpfc_hba_index, phba->brd_no);
1657	scsi_host_put(phba->host);
1658
1659	pci_set_drvdata(pdev, NULL);
1660}
1661
1662static struct pci_device_id lpfc_id_table[] = {
1663	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_VIPER,
1664		PCI_ANY_ID, PCI_ANY_ID, },
1665	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_THOR,
1666		PCI_ANY_ID, PCI_ANY_ID, },
1667	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PEGASUS,
1668		PCI_ANY_ID, PCI_ANY_ID, },
1669	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_CENTAUR,
1670		PCI_ANY_ID, PCI_ANY_ID, },
1671	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_DRAGONFLY,
1672		PCI_ANY_ID, PCI_ANY_ID, },
1673	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SUPERFLY,
1674		PCI_ANY_ID, PCI_ANY_ID, },
1675	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_RFLY,
1676		PCI_ANY_ID, PCI_ANY_ID, },
1677	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PFLY,
1678		PCI_ANY_ID, PCI_ANY_ID, },
1679	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS,
1680		PCI_ANY_ID, PCI_ANY_ID, },
1681	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BMID,
1682		PCI_ANY_ID, PCI_ANY_ID, },
1683	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BSMB,
1684		PCI_ANY_ID, PCI_ANY_ID, },
1685	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR,
1686		PCI_ANY_ID, PCI_ANY_ID, },
1687	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZMID,
1688		PCI_ANY_ID, PCI_ANY_ID, },
1689	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZSMB,
1690		PCI_ANY_ID, PCI_ANY_ID, },
1691	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_TFLY,
1692		PCI_ANY_ID, PCI_ANY_ID, },
1693	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP101,
1694		PCI_ANY_ID, PCI_ANY_ID, },
1695	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP10000S,
1696		PCI_ANY_ID, PCI_ANY_ID, },
1697	{ 0 }
1698};
1699
1700MODULE_DEVICE_TABLE(pci, lpfc_id_table);
1701
1702static struct pci_driver lpfc_driver = {
1703	.name		= LPFC_DRIVER_NAME,
1704	.id_table	= lpfc_id_table,
1705	.probe		= lpfc_pci_probe_one,
1706	.remove		= __devexit_p(lpfc_pci_remove_one),
1707};
1708
1709static int __init
1710lpfc_init(void)
1711{
1712	int error = 0;
1713
1714	printk(LPFC_MODULE_DESC "\n");
1715
1716	lpfc_transport_template =
1717				fc_attach_transport(&lpfc_transport_functions);
1718	if (!lpfc_transport_template)
1719		return -ENOMEM;
1720	error = pci_register_driver(&lpfc_driver);
1721	if (error)
1722		fc_release_transport(lpfc_transport_template);
1723
1724	return error;
1725}
1726
1727static void __exit
1728lpfc_exit(void)
1729{
1730	pci_unregister_driver(&lpfc_driver);
1731	fc_release_transport(lpfc_transport_template);
1732}
1733
1734module_init(lpfc_init);
1735module_exit(lpfc_exit);
1736MODULE_LICENSE("GPL");
1737MODULE_DESCRIPTION(LPFC_MODULE_DESC);
1738MODULE_AUTHOR("Emulex Corporation - tech.support@emulex.com");
1739MODULE_VERSION("0:" LPFC_DRIVER_VERSION);
1740