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