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