xref: /openbmc/linux/drivers/scsi/lpfc/lpfc_init.c (revision e59058c4)
1 /*******************************************************************
2  * This file is part of the Emulex Linux Device Driver for         *
3  * Fibre Channel Host Bus Adapters.                                *
4  * Copyright (C) 2004-2008 Emulex.  All rights reserved.           *
5  * EMULEX and SLI are trademarks of Emulex.                        *
6  * www.emulex.com                                                  *
7  * Portions Copyright (C) 2004-2005 Christoph Hellwig              *
8  *                                                                 *
9  * This program is free software; you can redistribute it and/or   *
10  * modify it under the terms of version 2 of the GNU General       *
11  * Public License as published by the Free Software Foundation.    *
12  * This program is distributed in the hope that it will be useful. *
13  * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND          *
14  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,  *
15  * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE      *
16  * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
17  * TO BE LEGALLY INVALID.  See the GNU General Public License for  *
18  * more details, a copy of which can be found in the file COPYING  *
19  * included with this package.                                     *
20  *******************************************************************/
21 
22 #include <linux/blkdev.h>
23 #include <linux/delay.h>
24 #include <linux/dma-mapping.h>
25 #include <linux/idr.h>
26 #include <linux/interrupt.h>
27 #include <linux/kthread.h>
28 #include <linux/pci.h>
29 #include <linux/spinlock.h>
30 #include <linux/ctype.h>
31 
32 #include <scsi/scsi.h>
33 #include <scsi/scsi_device.h>
34 #include <scsi/scsi_host.h>
35 #include <scsi/scsi_transport_fc.h>
36 
37 #include "lpfc_hw.h"
38 #include "lpfc_sli.h"
39 #include "lpfc_disc.h"
40 #include "lpfc_scsi.h"
41 #include "lpfc.h"
42 #include "lpfc_logmsg.h"
43 #include "lpfc_crtn.h"
44 #include "lpfc_vport.h"
45 #include "lpfc_version.h"
46 
47 static int lpfc_parse_vpd(struct lpfc_hba *, uint8_t *, int);
48 static void lpfc_get_hba_model_desc(struct lpfc_hba *, uint8_t *, uint8_t *);
49 static int lpfc_post_rcv_buf(struct lpfc_hba *);
50 
51 static struct scsi_transport_template *lpfc_transport_template = NULL;
52 static struct scsi_transport_template *lpfc_vport_transport_template = NULL;
53 static DEFINE_IDR(lpfc_hba_index);
54 
55 /**
56  * lpfc_config_port_prep: Perform lpfc initialization prior to config port.
57  * @phba: pointer to lpfc hba data structure.
58  *
59  * This routine will do LPFC initialization prior to issuing the CONFIG_PORT
60  * mailbox command. It retrieves the revision information from the HBA and
61  * collects the Vital Product Data (VPD) about the HBA for preparing the
62  * configuration of the HBA.
63  *
64  * Return codes:
65  *   0 - success.
66  *   -ERESTART - requests the SLI layer to reset the HBA and try again.
67  *   Any other value - indicates an error.
68  **/
69 int
70 lpfc_config_port_prep(struct lpfc_hba *phba)
71 {
72 	lpfc_vpd_t *vp = &phba->vpd;
73 	int i = 0, rc;
74 	LPFC_MBOXQ_t *pmb;
75 	MAILBOX_t *mb;
76 	char *lpfc_vpd_data = NULL;
77 	uint16_t offset = 0;
78 	static char licensed[56] =
79 		    "key unlock for use with gnu public licensed code only\0";
80 	static int init_key = 1;
81 
82 	pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
83 	if (!pmb) {
84 		phba->link_state = LPFC_HBA_ERROR;
85 		return -ENOMEM;
86 	}
87 
88 	mb = &pmb->mb;
89 	phba->link_state = LPFC_INIT_MBX_CMDS;
90 
91 	if (lpfc_is_LC_HBA(phba->pcidev->device)) {
92 		if (init_key) {
93 			uint32_t *ptext = (uint32_t *) licensed;
94 
95 			for (i = 0; i < 56; i += sizeof (uint32_t), ptext++)
96 				*ptext = cpu_to_be32(*ptext);
97 			init_key = 0;
98 		}
99 
100 		lpfc_read_nv(phba, pmb);
101 		memset((char*)mb->un.varRDnvp.rsvd3, 0,
102 			sizeof (mb->un.varRDnvp.rsvd3));
103 		memcpy((char*)mb->un.varRDnvp.rsvd3, licensed,
104 			 sizeof (licensed));
105 
106 		rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
107 
108 		if (rc != MBX_SUCCESS) {
109 			lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
110 					"0324 Config Port initialization "
111 					"error, mbxCmd x%x READ_NVPARM, "
112 					"mbxStatus x%x\n",
113 					mb->mbxCommand, mb->mbxStatus);
114 			mempool_free(pmb, phba->mbox_mem_pool);
115 			return -ERESTART;
116 		}
117 		memcpy(phba->wwnn, (char *)mb->un.varRDnvp.nodename,
118 		       sizeof(phba->wwnn));
119 		memcpy(phba->wwpn, (char *)mb->un.varRDnvp.portname,
120 		       sizeof(phba->wwpn));
121 	}
122 
123 	phba->sli3_options = 0x0;
124 
125 	/* Setup and issue mailbox READ REV command */
126 	lpfc_read_rev(phba, pmb);
127 	rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
128 	if (rc != MBX_SUCCESS) {
129 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
130 				"0439 Adapter failed to init, mbxCmd x%x "
131 				"READ_REV, mbxStatus x%x\n",
132 				mb->mbxCommand, mb->mbxStatus);
133 		mempool_free( pmb, phba->mbox_mem_pool);
134 		return -ERESTART;
135 	}
136 
137 
138 	/*
139 	 * The value of rr must be 1 since the driver set the cv field to 1.
140 	 * This setting requires the FW to set all revision fields.
141 	 */
142 	if (mb->un.varRdRev.rr == 0) {
143 		vp->rev.rBit = 0;
144 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
145 				"0440 Adapter failed to init, READ_REV has "
146 				"missing revision information.\n");
147 		mempool_free(pmb, phba->mbox_mem_pool);
148 		return -ERESTART;
149 	}
150 
151 	if (phba->sli_rev == 3 && !mb->un.varRdRev.v3rsp) {
152 		mempool_free(pmb, phba->mbox_mem_pool);
153 		return -EINVAL;
154 	}
155 
156 	/* Save information as VPD data */
157 	vp->rev.rBit = 1;
158 	memcpy(&vp->sli3Feat, &mb->un.varRdRev.sli3Feat, sizeof(uint32_t));
159 	vp->rev.sli1FwRev = mb->un.varRdRev.sli1FwRev;
160 	memcpy(vp->rev.sli1FwName, (char*) mb->un.varRdRev.sli1FwName, 16);
161 	vp->rev.sli2FwRev = mb->un.varRdRev.sli2FwRev;
162 	memcpy(vp->rev.sli2FwName, (char *) mb->un.varRdRev.sli2FwName, 16);
163 	vp->rev.biuRev = mb->un.varRdRev.biuRev;
164 	vp->rev.smRev = mb->un.varRdRev.smRev;
165 	vp->rev.smFwRev = mb->un.varRdRev.un.smFwRev;
166 	vp->rev.endecRev = mb->un.varRdRev.endecRev;
167 	vp->rev.fcphHigh = mb->un.varRdRev.fcphHigh;
168 	vp->rev.fcphLow = mb->un.varRdRev.fcphLow;
169 	vp->rev.feaLevelHigh = mb->un.varRdRev.feaLevelHigh;
170 	vp->rev.feaLevelLow = mb->un.varRdRev.feaLevelLow;
171 	vp->rev.postKernRev = mb->un.varRdRev.postKernRev;
172 	vp->rev.opFwRev = mb->un.varRdRev.opFwRev;
173 
174 	/* If the sli feature level is less then 9, we must
175 	 * tear down all RPIs and VPIs on link down if NPIV
176 	 * is enabled.
177 	 */
178 	if (vp->rev.feaLevelHigh < 9)
179 		phba->sli3_options |= LPFC_SLI3_VPORT_TEARDOWN;
180 
181 	if (lpfc_is_LC_HBA(phba->pcidev->device))
182 		memcpy(phba->RandomData, (char *)&mb->un.varWords[24],
183 						sizeof (phba->RandomData));
184 
185 	/* Get adapter VPD information */
186 	pmb->context2 = kmalloc(DMP_RSP_SIZE, GFP_KERNEL);
187 	if (!pmb->context2)
188 		goto out_free_mbox;
189 	lpfc_vpd_data = kmalloc(DMP_VPD_SIZE, GFP_KERNEL);
190 	if (!lpfc_vpd_data)
191 		goto out_free_context2;
192 
193 	do {
194 		lpfc_dump_mem(phba, pmb, offset);
195 		rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
196 
197 		if (rc != MBX_SUCCESS) {
198 			lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
199 					"0441 VPD not present on adapter, "
200 					"mbxCmd x%x DUMP VPD, mbxStatus x%x\n",
201 					mb->mbxCommand, mb->mbxStatus);
202 			mb->un.varDmp.word_cnt = 0;
203 		}
204 		if (mb->un.varDmp.word_cnt > DMP_VPD_SIZE - offset)
205 			mb->un.varDmp.word_cnt = DMP_VPD_SIZE - offset;
206 		lpfc_sli_pcimem_bcopy(pmb->context2, lpfc_vpd_data + offset,
207 				      mb->un.varDmp.word_cnt);
208 		offset += mb->un.varDmp.word_cnt;
209 	} while (mb->un.varDmp.word_cnt && offset < DMP_VPD_SIZE);
210 	lpfc_parse_vpd(phba, lpfc_vpd_data, offset);
211 
212 	kfree(lpfc_vpd_data);
213 out_free_context2:
214 	kfree(pmb->context2);
215 out_free_mbox:
216 	mempool_free(pmb, phba->mbox_mem_pool);
217 	return 0;
218 }
219 
220 /**
221  * lpfc_config_async_cmpl: Completion handler for config async event mbox cmd.
222  * @phba: pointer to lpfc hba data structure.
223  * @pmboxq: pointer to the driver internal queue element for mailbox command.
224  *
225  * This is the completion handler for driver's configuring asynchronous event
226  * mailbox command to the device. If the mailbox command returns successfully,
227  * it will set internal async event support flag to 1; otherwise, it will
228  * set internal async event support flag to 0.
229  **/
230 static void
231 lpfc_config_async_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
232 {
233 	if (pmboxq->mb.mbxStatus == MBX_SUCCESS)
234 		phba->temp_sensor_support = 1;
235 	else
236 		phba->temp_sensor_support = 0;
237 	mempool_free(pmboxq, phba->mbox_mem_pool);
238 	return;
239 }
240 
241 /**
242  * lpfc_config_port_post: Perform lpfc initialization after config port.
243  * @phba: pointer to lpfc hba data structure.
244  *
245  * This routine will do LPFC initialization after the CONFIG_PORT mailbox
246  * command call. It performs all internal resource and state setups on the
247  * port: post IOCB buffers, enable appropriate host interrupt attentions,
248  * ELS ring timers, etc.
249  *
250  * Return codes
251  *   0 - success.
252  *   Any other value - error.
253  **/
254 int
255 lpfc_config_port_post(struct lpfc_hba *phba)
256 {
257 	struct lpfc_vport *vport = phba->pport;
258 	LPFC_MBOXQ_t *pmb;
259 	MAILBOX_t *mb;
260 	struct lpfc_dmabuf *mp;
261 	struct lpfc_sli *psli = &phba->sli;
262 	uint32_t status, timeout;
263 	int i, j;
264 	int rc;
265 
266 	spin_lock_irq(&phba->hbalock);
267 	/*
268 	 * If the Config port completed correctly the HBA is not
269 	 * over heated any more.
270 	 */
271 	if (phba->over_temp_state == HBA_OVER_TEMP)
272 		phba->over_temp_state = HBA_NORMAL_TEMP;
273 	spin_unlock_irq(&phba->hbalock);
274 
275 	pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
276 	if (!pmb) {
277 		phba->link_state = LPFC_HBA_ERROR;
278 		return -ENOMEM;
279 	}
280 	mb = &pmb->mb;
281 
282 	/* Get login parameters for NID.  */
283 	lpfc_read_sparam(phba, pmb, 0);
284 	pmb->vport = vport;
285 	if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
286 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
287 				"0448 Adapter failed init, mbxCmd x%x "
288 				"READ_SPARM mbxStatus x%x\n",
289 				mb->mbxCommand, mb->mbxStatus);
290 		phba->link_state = LPFC_HBA_ERROR;
291 		mp = (struct lpfc_dmabuf *) pmb->context1;
292 		mempool_free( pmb, phba->mbox_mem_pool);
293 		lpfc_mbuf_free(phba, mp->virt, mp->phys);
294 		kfree(mp);
295 		return -EIO;
296 	}
297 
298 	mp = (struct lpfc_dmabuf *) pmb->context1;
299 
300 	memcpy(&vport->fc_sparam, mp->virt, sizeof (struct serv_parm));
301 	lpfc_mbuf_free(phba, mp->virt, mp->phys);
302 	kfree(mp);
303 	pmb->context1 = NULL;
304 
305 	if (phba->cfg_soft_wwnn)
306 		u64_to_wwn(phba->cfg_soft_wwnn,
307 			   vport->fc_sparam.nodeName.u.wwn);
308 	if (phba->cfg_soft_wwpn)
309 		u64_to_wwn(phba->cfg_soft_wwpn,
310 			   vport->fc_sparam.portName.u.wwn);
311 	memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
312 	       sizeof (struct lpfc_name));
313 	memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
314 	       sizeof (struct lpfc_name));
315 	/* If no serial number in VPD data, use low 6 bytes of WWNN */
316 	/* This should be consolidated into parse_vpd ? - mr */
317 	if (phba->SerialNumber[0] == 0) {
318 		uint8_t *outptr;
319 
320 		outptr = &vport->fc_nodename.u.s.IEEE[0];
321 		for (i = 0; i < 12; i++) {
322 			status = *outptr++;
323 			j = ((status & 0xf0) >> 4);
324 			if (j <= 9)
325 				phba->SerialNumber[i] =
326 				    (char)((uint8_t) 0x30 + (uint8_t) j);
327 			else
328 				phba->SerialNumber[i] =
329 				    (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
330 			i++;
331 			j = (status & 0xf);
332 			if (j <= 9)
333 				phba->SerialNumber[i] =
334 				    (char)((uint8_t) 0x30 + (uint8_t) j);
335 			else
336 				phba->SerialNumber[i] =
337 				    (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
338 		}
339 	}
340 
341 	lpfc_read_config(phba, pmb);
342 	pmb->vport = vport;
343 	if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
344 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
345 				"0453 Adapter failed to init, mbxCmd x%x "
346 				"READ_CONFIG, mbxStatus x%x\n",
347 				mb->mbxCommand, mb->mbxStatus);
348 		phba->link_state = LPFC_HBA_ERROR;
349 		mempool_free( pmb, phba->mbox_mem_pool);
350 		return -EIO;
351 	}
352 
353 	/* Reset the DFT_HBA_Q_DEPTH to the max xri  */
354 	if (phba->cfg_hba_queue_depth > (mb->un.varRdConfig.max_xri+1))
355 		phba->cfg_hba_queue_depth =
356 			mb->un.varRdConfig.max_xri + 1;
357 
358 	phba->lmt = mb->un.varRdConfig.lmt;
359 
360 	/* Get the default values for Model Name and Description */
361 	lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
362 
363 	if ((phba->cfg_link_speed > LINK_SPEED_10G)
364 	    || ((phba->cfg_link_speed == LINK_SPEED_1G)
365 		&& !(phba->lmt & LMT_1Gb))
366 	    || ((phba->cfg_link_speed == LINK_SPEED_2G)
367 		&& !(phba->lmt & LMT_2Gb))
368 	    || ((phba->cfg_link_speed == LINK_SPEED_4G)
369 		&& !(phba->lmt & LMT_4Gb))
370 	    || ((phba->cfg_link_speed == LINK_SPEED_8G)
371 		&& !(phba->lmt & LMT_8Gb))
372 	    || ((phba->cfg_link_speed == LINK_SPEED_10G)
373 		&& !(phba->lmt & LMT_10Gb))) {
374 		/* Reset link speed to auto */
375 		lpfc_printf_log(phba, KERN_WARNING, LOG_LINK_EVENT,
376 			"1302 Invalid speed for this board: "
377 			"Reset link speed to auto: x%x\n",
378 			phba->cfg_link_speed);
379 			phba->cfg_link_speed = LINK_SPEED_AUTO;
380 	}
381 
382 	phba->link_state = LPFC_LINK_DOWN;
383 
384 	/* Only process IOCBs on ELS ring till hba_state is READY */
385 	if (psli->ring[psli->extra_ring].cmdringaddr)
386 		psli->ring[psli->extra_ring].flag |= LPFC_STOP_IOCB_EVENT;
387 	if (psli->ring[psli->fcp_ring].cmdringaddr)
388 		psli->ring[psli->fcp_ring].flag |= LPFC_STOP_IOCB_EVENT;
389 	if (psli->ring[psli->next_ring].cmdringaddr)
390 		psli->ring[psli->next_ring].flag |= LPFC_STOP_IOCB_EVENT;
391 
392 	/* Post receive buffers for desired rings */
393 	if (phba->sli_rev != 3)
394 		lpfc_post_rcv_buf(phba);
395 
396 	/* Enable appropriate host interrupts */
397 	spin_lock_irq(&phba->hbalock);
398 	status = readl(phba->HCregaddr);
399 	status |= HC_MBINT_ENA | HC_ERINT_ENA | HC_LAINT_ENA;
400 	if (psli->num_rings > 0)
401 		status |= HC_R0INT_ENA;
402 	if (psli->num_rings > 1)
403 		status |= HC_R1INT_ENA;
404 	if (psli->num_rings > 2)
405 		status |= HC_R2INT_ENA;
406 	if (psli->num_rings > 3)
407 		status |= HC_R3INT_ENA;
408 
409 	if ((phba->cfg_poll & ENABLE_FCP_RING_POLLING) &&
410 	    (phba->cfg_poll & DISABLE_FCP_RING_INT))
411 		status &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
412 
413 	writel(status, phba->HCregaddr);
414 	readl(phba->HCregaddr); /* flush */
415 	spin_unlock_irq(&phba->hbalock);
416 
417 	/*
418 	 * Setup the ring 0 (els)  timeout handler
419 	 */
420 	timeout = phba->fc_ratov << 1;
421 	mod_timer(&vport->els_tmofunc, jiffies + HZ * timeout);
422 	mod_timer(&phba->hb_tmofunc, jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
423 	phba->hb_outstanding = 0;
424 	phba->last_completion_time = jiffies;
425 
426 	lpfc_init_link(phba, pmb, phba->cfg_topology, phba->cfg_link_speed);
427 	pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
428 	pmb->vport = vport;
429 	rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
430 	lpfc_set_loopback_flag(phba);
431 	if (rc != MBX_SUCCESS) {
432 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
433 				"0454 Adapter failed to init, mbxCmd x%x "
434 				"INIT_LINK, mbxStatus x%x\n",
435 				mb->mbxCommand, mb->mbxStatus);
436 
437 		/* Clear all interrupt enable conditions */
438 		writel(0, phba->HCregaddr);
439 		readl(phba->HCregaddr); /* flush */
440 		/* Clear all pending interrupts */
441 		writel(0xffffffff, phba->HAregaddr);
442 		readl(phba->HAregaddr); /* flush */
443 
444 		phba->link_state = LPFC_HBA_ERROR;
445 		if (rc != MBX_BUSY)
446 			mempool_free(pmb, phba->mbox_mem_pool);
447 		return -EIO;
448 	}
449 	/* MBOX buffer will be freed in mbox compl */
450 	pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
451 	lpfc_config_async(phba, pmb, LPFC_ELS_RING);
452 	pmb->mbox_cmpl = lpfc_config_async_cmpl;
453 	pmb->vport = phba->pport;
454 	rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
455 
456 	if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
457 		lpfc_printf_log(phba,
458 				KERN_ERR,
459 				LOG_INIT,
460 				"0456 Adapter failed to issue "
461 				"ASYNCEVT_ENABLE mbox status x%x \n.",
462 				rc);
463 		mempool_free(pmb, phba->mbox_mem_pool);
464 	}
465 	return (0);
466 }
467 
468 /**
469  * lpfc_hba_down_prep: Perform lpfc uninitialization prior to HBA reset.
470  * @phba: pointer to lpfc HBA data structure.
471  *
472  * This routine will do LPFC uninitialization before the HBA is reset when
473  * bringing down the SLI Layer.
474  *
475  * Return codes
476  *   0 - success.
477  *   Any other value - error.
478  **/
479 int
480 lpfc_hba_down_prep(struct lpfc_hba *phba)
481 {
482 	struct lpfc_vport **vports;
483 	int i;
484 	/* Disable interrupts */
485 	writel(0, phba->HCregaddr);
486 	readl(phba->HCregaddr); /* flush */
487 
488 	if (phba->pport->load_flag & FC_UNLOADING)
489 		lpfc_cleanup_discovery_resources(phba->pport);
490 	else {
491 		vports = lpfc_create_vport_work_array(phba);
492 		if (vports != NULL)
493 			for(i = 0; i <= phba->max_vpi && vports[i] != NULL; i++)
494 				lpfc_cleanup_discovery_resources(vports[i]);
495 		lpfc_destroy_vport_work_array(phba, vports);
496 	}
497 	return 0;
498 }
499 
500 /**
501  * lpfc_hba_down_post: Perform lpfc uninitialization after HBA reset.
502  * @phba: pointer to lpfc HBA data structure.
503  *
504  * This routine will do uninitialization after the HBA is reset when bring
505  * down the SLI Layer.
506  *
507  * Return codes
508  *   0 - sucess.
509  *   Any other value - error.
510  **/
511 int
512 lpfc_hba_down_post(struct lpfc_hba *phba)
513 {
514 	struct lpfc_sli *psli = &phba->sli;
515 	struct lpfc_sli_ring *pring;
516 	struct lpfc_dmabuf *mp, *next_mp;
517 	struct lpfc_iocbq *iocb;
518 	IOCB_t *cmd = NULL;
519 	LIST_HEAD(completions);
520 	int i;
521 
522 	if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)
523 		lpfc_sli_hbqbuf_free_all(phba);
524 	else {
525 		/* Cleanup preposted buffers on the ELS ring */
526 		pring = &psli->ring[LPFC_ELS_RING];
527 		list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
528 			list_del(&mp->list);
529 			pring->postbufq_cnt--;
530 			lpfc_mbuf_free(phba, mp->virt, mp->phys);
531 			kfree(mp);
532 		}
533 	}
534 
535 	spin_lock_irq(&phba->hbalock);
536 	for (i = 0; i < psli->num_rings; i++) {
537 		pring = &psli->ring[i];
538 
539 		/* At this point in time the HBA is either reset or DOA. Either
540 		 * way, nothing should be on txcmplq as it will NEVER complete.
541 		 */
542 		list_splice_init(&pring->txcmplq, &completions);
543 		pring->txcmplq_cnt = 0;
544 		spin_unlock_irq(&phba->hbalock);
545 
546 		while (!list_empty(&completions)) {
547 			iocb = list_get_first(&completions, struct lpfc_iocbq,
548 				list);
549 			cmd = &iocb->iocb;
550 			list_del_init(&iocb->list);
551 
552 			if (!iocb->iocb_cmpl)
553 				lpfc_sli_release_iocbq(phba, iocb);
554 			else {
555 				cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
556 				cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
557 				(iocb->iocb_cmpl) (phba, iocb, iocb);
558 			}
559 		}
560 
561 		lpfc_sli_abort_iocb_ring(phba, pring);
562 		spin_lock_irq(&phba->hbalock);
563 	}
564 	spin_unlock_irq(&phba->hbalock);
565 
566 	return 0;
567 }
568 
569 /**
570  * lpfc_hb_timeout: The HBA-timer timeout handler.
571  * @ptr: unsigned long holds the pointer to lpfc hba data structure.
572  *
573  * This is the HBA-timer timeout handler registered to the lpfc driver. When
574  * this timer fires, a HBA timeout event shall be posted to the lpfc driver
575  * work-port-events bitmap and the worker thread is notified. This timeout
576  * event will be used by the worker thread to invoke the actual timeout
577  * handler routine, lpfc_hb_timeout_handler. Any periodical operations will
578  * be performed in the timeout handler and the HBA timeout event bit shall
579  * be cleared by the worker thread after it has taken the event bitmap out.
580  **/
581 static void
582 lpfc_hb_timeout(unsigned long ptr)
583 {
584 	struct lpfc_hba *phba;
585 	uint32_t tmo_posted;
586 	unsigned long iflag;
587 
588 	phba = (struct lpfc_hba *)ptr;
589 	spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
590 	tmo_posted = phba->pport->work_port_events & WORKER_HB_TMO;
591 	if (!tmo_posted)
592 		phba->pport->work_port_events |= WORKER_HB_TMO;
593 	spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
594 
595 	if (!tmo_posted)
596 		lpfc_worker_wake_up(phba);
597 	return;
598 }
599 
600 /**
601  * lpfc_hb_mbox_cmpl: The lpfc heart-beat mailbox command callback function.
602  * @phba: pointer to lpfc hba data structure.
603  * @pmboxq: pointer to the driver internal queue element for mailbox command.
604  *
605  * This is the callback function to the lpfc heart-beat mailbox command.
606  * If configured, the lpfc driver issues the heart-beat mailbox command to
607  * the HBA every LPFC_HB_MBOX_INTERVAL (current 5) seconds. At the time the
608  * heart-beat mailbox command is issued, the driver shall set up heart-beat
609  * timeout timer to LPFC_HB_MBOX_TIMEOUT (current 30) seconds and marks
610  * heart-beat outstanding state. Once the mailbox command comes back and
611  * no error conditions detected, the heart-beat mailbox command timer is
612  * reset to LPFC_HB_MBOX_INTERVAL seconds and the heart-beat outstanding
613  * state is cleared for the next heart-beat. If the timer expired with the
614  * heart-beat outstanding state set, the driver will put the HBA offline.
615  **/
616 static void
617 lpfc_hb_mbox_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
618 {
619 	unsigned long drvr_flag;
620 
621 	spin_lock_irqsave(&phba->hbalock, drvr_flag);
622 	phba->hb_outstanding = 0;
623 	spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
624 
625 	mempool_free(pmboxq, phba->mbox_mem_pool);
626 	if (!(phba->pport->fc_flag & FC_OFFLINE_MODE) &&
627 		!(phba->link_state == LPFC_HBA_ERROR) &&
628 		!(phba->pport->load_flag & FC_UNLOADING))
629 		mod_timer(&phba->hb_tmofunc,
630 			jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
631 	return;
632 }
633 
634 /**
635  * lpfc_hb_timeout_handler: The HBA-timer timeout handler.
636  * @phba: pointer to lpfc hba data structure.
637  *
638  * This is the actual HBA-timer timeout handler to be invoked by the worker
639  * thread whenever the HBA timer fired and HBA-timeout event posted. This
640  * handler performs any periodic operations needed for the device. If such
641  * periodic event has already been attended to either in the interrupt handler
642  * or by processing slow-ring or fast-ring events within the HBA-timer
643  * timeout window (LPFC_HB_MBOX_INTERVAL), this handler just simply resets
644  * the timer for the next timeout period. If lpfc heart-beat mailbox command
645  * is configured and there is no heart-beat mailbox command outstanding, a
646  * heart-beat mailbox is issued and timer set properly. Otherwise, if there
647  * has been a heart-beat mailbox command outstanding, the HBA shall be put
648  * to offline.
649  **/
650 void
651 lpfc_hb_timeout_handler(struct lpfc_hba *phba)
652 {
653 	LPFC_MBOXQ_t *pmboxq;
654 	struct lpfc_dmabuf *buf_ptr;
655 	int retval;
656 	struct lpfc_sli *psli = &phba->sli;
657 	LIST_HEAD(completions);
658 
659 	if ((phba->link_state == LPFC_HBA_ERROR) ||
660 		(phba->pport->load_flag & FC_UNLOADING) ||
661 		(phba->pport->fc_flag & FC_OFFLINE_MODE))
662 		return;
663 
664 	spin_lock_irq(&phba->pport->work_port_lock);
665 	/* If the timer is already canceled do nothing */
666 	if (!(phba->pport->work_port_events & WORKER_HB_TMO)) {
667 		spin_unlock_irq(&phba->pport->work_port_lock);
668 		return;
669 	}
670 
671 	if (time_after(phba->last_completion_time + LPFC_HB_MBOX_INTERVAL * HZ,
672 		jiffies)) {
673 		spin_unlock_irq(&phba->pport->work_port_lock);
674 		if (!phba->hb_outstanding)
675 			mod_timer(&phba->hb_tmofunc,
676 				jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
677 		else
678 			mod_timer(&phba->hb_tmofunc,
679 				jiffies + HZ * LPFC_HB_MBOX_TIMEOUT);
680 		return;
681 	}
682 	spin_unlock_irq(&phba->pport->work_port_lock);
683 
684 	if (phba->elsbuf_cnt &&
685 		(phba->elsbuf_cnt == phba->elsbuf_prev_cnt)) {
686 		spin_lock_irq(&phba->hbalock);
687 		list_splice_init(&phba->elsbuf, &completions);
688 		phba->elsbuf_cnt = 0;
689 		phba->elsbuf_prev_cnt = 0;
690 		spin_unlock_irq(&phba->hbalock);
691 
692 		while (!list_empty(&completions)) {
693 			list_remove_head(&completions, buf_ptr,
694 				struct lpfc_dmabuf, list);
695 			lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
696 			kfree(buf_ptr);
697 		}
698 	}
699 	phba->elsbuf_prev_cnt = phba->elsbuf_cnt;
700 
701 	/* If there is no heart beat outstanding, issue a heartbeat command */
702 	if (phba->cfg_enable_hba_heartbeat) {
703 		if (!phba->hb_outstanding) {
704 			pmboxq = mempool_alloc(phba->mbox_mem_pool,GFP_KERNEL);
705 			if (!pmboxq) {
706 				mod_timer(&phba->hb_tmofunc,
707 					  jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
708 				return;
709 			}
710 
711 			lpfc_heart_beat(phba, pmboxq);
712 			pmboxq->mbox_cmpl = lpfc_hb_mbox_cmpl;
713 			pmboxq->vport = phba->pport;
714 			retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
715 
716 			if (retval != MBX_BUSY && retval != MBX_SUCCESS) {
717 				mempool_free(pmboxq, phba->mbox_mem_pool);
718 				mod_timer(&phba->hb_tmofunc,
719 					  jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
720 				return;
721 			}
722 			mod_timer(&phba->hb_tmofunc,
723 				  jiffies + HZ * LPFC_HB_MBOX_TIMEOUT);
724 			phba->hb_outstanding = 1;
725 			return;
726 		} else {
727 			/*
728 			* If heart beat timeout called with hb_outstanding set
729 			* we need to take the HBA offline.
730 			*/
731 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
732 					"0459 Adapter heartbeat failure, "
733 					"taking this port offline.\n");
734 
735 			spin_lock_irq(&phba->hbalock);
736 			psli->sli_flag &= ~LPFC_SLI2_ACTIVE;
737 			spin_unlock_irq(&phba->hbalock);
738 
739 			lpfc_offline_prep(phba);
740 			lpfc_offline(phba);
741 			lpfc_unblock_mgmt_io(phba);
742 			phba->link_state = LPFC_HBA_ERROR;
743 			lpfc_hba_down_post(phba);
744 		}
745 	}
746 }
747 
748 /**
749  * lpfc_offline_eratt: Bring lpfc offline on hardware error attention.
750  * @phba: pointer to lpfc hba data structure.
751  *
752  * This routine is called to bring the HBA offline when HBA hardware error
753  * other than Port Error 6 has been detected.
754  **/
755 static void
756 lpfc_offline_eratt(struct lpfc_hba *phba)
757 {
758 	struct lpfc_sli   *psli = &phba->sli;
759 
760 	spin_lock_irq(&phba->hbalock);
761 	psli->sli_flag &= ~LPFC_SLI2_ACTIVE;
762 	spin_unlock_irq(&phba->hbalock);
763 	lpfc_offline_prep(phba);
764 
765 	lpfc_offline(phba);
766 	lpfc_reset_barrier(phba);
767 	lpfc_sli_brdreset(phba);
768 	lpfc_hba_down_post(phba);
769 	lpfc_sli_brdready(phba, HS_MBRDY);
770 	lpfc_unblock_mgmt_io(phba);
771 	phba->link_state = LPFC_HBA_ERROR;
772 	return;
773 }
774 
775 /**
776  * lpfc_handle_eratt: The HBA hardware error handler.
777  * @phba: pointer to lpfc hba data structure.
778  *
779  * This routine is invoked to handle the following HBA hardware error
780  * conditions:
781  * 1 - HBA error attention interrupt
782  * 2 - DMA ring index out of range
783  * 3 - Mailbox command came back as unknown
784  **/
785 void
786 lpfc_handle_eratt(struct lpfc_hba *phba)
787 {
788 	struct lpfc_vport *vport = phba->pport;
789 	struct lpfc_sli   *psli = &phba->sli;
790 	struct lpfc_sli_ring  *pring;
791 	uint32_t event_data;
792 	unsigned long temperature;
793 	struct temp_event temp_event_data;
794 	struct Scsi_Host  *shost;
795 
796 	/* If the pci channel is offline, ignore possible errors,
797 	 * since we cannot communicate with the pci card anyway. */
798 	if (pci_channel_offline(phba->pcidev))
799 		return;
800 	/* If resets are disabled then leave the HBA alone and return */
801 	if (!phba->cfg_enable_hba_reset)
802 		return;
803 
804 	if (phba->work_hs & HS_FFER6) {
805 		/* Re-establishing Link */
806 		lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
807 				"1301 Re-establishing Link "
808 				"Data: x%x x%x x%x\n",
809 				phba->work_hs,
810 				phba->work_status[0], phba->work_status[1]);
811 
812 		spin_lock_irq(&phba->hbalock);
813 		psli->sli_flag &= ~LPFC_SLI2_ACTIVE;
814 		spin_unlock_irq(&phba->hbalock);
815 
816 		/*
817 		* Firmware stops when it triggled erratt with HS_FFER6.
818 		* That could cause the I/Os dropped by the firmware.
819 		* Error iocb (I/O) on txcmplq and let the SCSI layer
820 		* retry it after re-establishing link.
821 		*/
822 		pring = &psli->ring[psli->fcp_ring];
823 		lpfc_sli_abort_iocb_ring(phba, pring);
824 
825 		/*
826 		 * There was a firmware error.  Take the hba offline and then
827 		 * attempt to restart it.
828 		 */
829 		lpfc_offline_prep(phba);
830 		lpfc_offline(phba);
831 		lpfc_sli_brdrestart(phba);
832 		if (lpfc_online(phba) == 0) {	/* Initialize the HBA */
833 			lpfc_unblock_mgmt_io(phba);
834 			return;
835 		}
836 		lpfc_unblock_mgmt_io(phba);
837 	} else if (phba->work_hs & HS_CRIT_TEMP) {
838 		temperature = readl(phba->MBslimaddr + TEMPERATURE_OFFSET);
839 		temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
840 		temp_event_data.event_code = LPFC_CRIT_TEMP;
841 		temp_event_data.data = (uint32_t)temperature;
842 
843 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
844 				"0459 Adapter maximum temperature exceeded "
845 				"(%ld), taking this port offline "
846 				"Data: x%x x%x x%x\n",
847 				temperature, phba->work_hs,
848 				phba->work_status[0], phba->work_status[1]);
849 
850 		shost = lpfc_shost_from_vport(phba->pport);
851 		fc_host_post_vendor_event(shost, fc_get_event_number(),
852 					  sizeof(temp_event_data),
853 					  (char *) &temp_event_data,
854 					  SCSI_NL_VID_TYPE_PCI
855 					  | PCI_VENDOR_ID_EMULEX);
856 
857 		spin_lock_irq(&phba->hbalock);
858 		phba->over_temp_state = HBA_OVER_TEMP;
859 		spin_unlock_irq(&phba->hbalock);
860 		lpfc_offline_eratt(phba);
861 
862 	} else {
863 		/* The if clause above forces this code path when the status
864 		 * failure is a value other than FFER6.  Do not call the offline
865 		 *  twice. This is the adapter hardware error path.
866 		 */
867 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
868 				"0457 Adapter Hardware Error "
869 				"Data: x%x x%x x%x\n",
870 				phba->work_hs,
871 				phba->work_status[0], phba->work_status[1]);
872 
873 		event_data = FC_REG_DUMP_EVENT;
874 		shost = lpfc_shost_from_vport(vport);
875 		fc_host_post_vendor_event(shost, fc_get_event_number(),
876 				sizeof(event_data), (char *) &event_data,
877 				SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
878 
879 		lpfc_offline_eratt(phba);
880 	}
881 }
882 
883 /**
884  * lpfc_handle_latt: The HBA link event handler.
885  * @phba: pointer to lpfc hba data structure.
886  *
887  * This routine is invoked from the worker thread to handle a HBA host
888  * attention link event.
889  **/
890 void
891 lpfc_handle_latt(struct lpfc_hba *phba)
892 {
893 	struct lpfc_vport *vport = phba->pport;
894 	struct lpfc_sli   *psli = &phba->sli;
895 	LPFC_MBOXQ_t *pmb;
896 	volatile uint32_t control;
897 	struct lpfc_dmabuf *mp;
898 	int rc = 0;
899 
900 	pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
901 	if (!pmb) {
902 		rc = 1;
903 		goto lpfc_handle_latt_err_exit;
904 	}
905 
906 	mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
907 	if (!mp) {
908 		rc = 2;
909 		goto lpfc_handle_latt_free_pmb;
910 	}
911 
912 	mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
913 	if (!mp->virt) {
914 		rc = 3;
915 		goto lpfc_handle_latt_free_mp;
916 	}
917 
918 	/* Cleanup any outstanding ELS commands */
919 	lpfc_els_flush_all_cmd(phba);
920 
921 	psli->slistat.link_event++;
922 	lpfc_read_la(phba, pmb, mp);
923 	pmb->mbox_cmpl = lpfc_mbx_cmpl_read_la;
924 	pmb->vport = vport;
925 	/* Block ELS IOCBs until we have processed this mbox command */
926 	phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
927 	rc = lpfc_sli_issue_mbox (phba, pmb, MBX_NOWAIT);
928 	if (rc == MBX_NOT_FINISHED) {
929 		rc = 4;
930 		goto lpfc_handle_latt_free_mbuf;
931 	}
932 
933 	/* Clear Link Attention in HA REG */
934 	spin_lock_irq(&phba->hbalock);
935 	writel(HA_LATT, phba->HAregaddr);
936 	readl(phba->HAregaddr); /* flush */
937 	spin_unlock_irq(&phba->hbalock);
938 
939 	return;
940 
941 lpfc_handle_latt_free_mbuf:
942 	phba->sli.ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
943 	lpfc_mbuf_free(phba, mp->virt, mp->phys);
944 lpfc_handle_latt_free_mp:
945 	kfree(mp);
946 lpfc_handle_latt_free_pmb:
947 	mempool_free(pmb, phba->mbox_mem_pool);
948 lpfc_handle_latt_err_exit:
949 	/* Enable Link attention interrupts */
950 	spin_lock_irq(&phba->hbalock);
951 	psli->sli_flag |= LPFC_PROCESS_LA;
952 	control = readl(phba->HCregaddr);
953 	control |= HC_LAINT_ENA;
954 	writel(control, phba->HCregaddr);
955 	readl(phba->HCregaddr); /* flush */
956 
957 	/* Clear Link Attention in HA REG */
958 	writel(HA_LATT, phba->HAregaddr);
959 	readl(phba->HAregaddr); /* flush */
960 	spin_unlock_irq(&phba->hbalock);
961 	lpfc_linkdown(phba);
962 	phba->link_state = LPFC_HBA_ERROR;
963 
964 	lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
965 		     "0300 LATT: Cannot issue READ_LA: Data:%d\n", rc);
966 
967 	return;
968 }
969 
970 /**
971  * lpfc_parse_vpd: Parse VPD (Vital Product Data).
972  * @phba: pointer to lpfc hba data structure.
973  * @vpd: pointer to the vital product data.
974  * @len: length of the vital product data in bytes.
975  *
976  * This routine parses the Vital Product Data (VPD). The VPD is treated as
977  * an array of characters. In this routine, the ModelName, ProgramType, and
978  * ModelDesc, etc. fields of the phba data structure will be populated.
979  *
980  * Return codes
981  *   0 - pointer to the VPD passed in is NULL
982  *   1 - success
983  **/
984 static int
985 lpfc_parse_vpd(struct lpfc_hba *phba, uint8_t *vpd, int len)
986 {
987 	uint8_t lenlo, lenhi;
988 	int Length;
989 	int i, j;
990 	int finished = 0;
991 	int index = 0;
992 
993 	if (!vpd)
994 		return 0;
995 
996 	/* Vital Product */
997 	lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
998 			"0455 Vital Product Data: x%x x%x x%x x%x\n",
999 			(uint32_t) vpd[0], (uint32_t) vpd[1], (uint32_t) vpd[2],
1000 			(uint32_t) vpd[3]);
1001 	while (!finished && (index < (len - 4))) {
1002 		switch (vpd[index]) {
1003 		case 0x82:
1004 		case 0x91:
1005 			index += 1;
1006 			lenlo = vpd[index];
1007 			index += 1;
1008 			lenhi = vpd[index];
1009 			index += 1;
1010 			i = ((((unsigned short)lenhi) << 8) + lenlo);
1011 			index += i;
1012 			break;
1013 		case 0x90:
1014 			index += 1;
1015 			lenlo = vpd[index];
1016 			index += 1;
1017 			lenhi = vpd[index];
1018 			index += 1;
1019 			Length = ((((unsigned short)lenhi) << 8) + lenlo);
1020 			if (Length > len - index)
1021 				Length = len - index;
1022 			while (Length > 0) {
1023 			/* Look for Serial Number */
1024 			if ((vpd[index] == 'S') && (vpd[index+1] == 'N')) {
1025 				index += 2;
1026 				i = vpd[index];
1027 				index += 1;
1028 				j = 0;
1029 				Length -= (3+i);
1030 				while(i--) {
1031 					phba->SerialNumber[j++] = vpd[index++];
1032 					if (j == 31)
1033 						break;
1034 				}
1035 				phba->SerialNumber[j] = 0;
1036 				continue;
1037 			}
1038 			else if ((vpd[index] == 'V') && (vpd[index+1] == '1')) {
1039 				phba->vpd_flag |= VPD_MODEL_DESC;
1040 				index += 2;
1041 				i = vpd[index];
1042 				index += 1;
1043 				j = 0;
1044 				Length -= (3+i);
1045 				while(i--) {
1046 					phba->ModelDesc[j++] = vpd[index++];
1047 					if (j == 255)
1048 						break;
1049 				}
1050 				phba->ModelDesc[j] = 0;
1051 				continue;
1052 			}
1053 			else if ((vpd[index] == 'V') && (vpd[index+1] == '2')) {
1054 				phba->vpd_flag |= VPD_MODEL_NAME;
1055 				index += 2;
1056 				i = vpd[index];
1057 				index += 1;
1058 				j = 0;
1059 				Length -= (3+i);
1060 				while(i--) {
1061 					phba->ModelName[j++] = vpd[index++];
1062 					if (j == 79)
1063 						break;
1064 				}
1065 				phba->ModelName[j] = 0;
1066 				continue;
1067 			}
1068 			else if ((vpd[index] == 'V') && (vpd[index+1] == '3')) {
1069 				phba->vpd_flag |= VPD_PROGRAM_TYPE;
1070 				index += 2;
1071 				i = vpd[index];
1072 				index += 1;
1073 				j = 0;
1074 				Length -= (3+i);
1075 				while(i--) {
1076 					phba->ProgramType[j++] = vpd[index++];
1077 					if (j == 255)
1078 						break;
1079 				}
1080 				phba->ProgramType[j] = 0;
1081 				continue;
1082 			}
1083 			else if ((vpd[index] == 'V') && (vpd[index+1] == '4')) {
1084 				phba->vpd_flag |= VPD_PORT;
1085 				index += 2;
1086 				i = vpd[index];
1087 				index += 1;
1088 				j = 0;
1089 				Length -= (3+i);
1090 				while(i--) {
1091 				phba->Port[j++] = vpd[index++];
1092 				if (j == 19)
1093 					break;
1094 				}
1095 				phba->Port[j] = 0;
1096 				continue;
1097 			}
1098 			else {
1099 				index += 2;
1100 				i = vpd[index];
1101 				index += 1;
1102 				index += i;
1103 				Length -= (3 + i);
1104 			}
1105 		}
1106 		finished = 0;
1107 		break;
1108 		case 0x78:
1109 			finished = 1;
1110 			break;
1111 		default:
1112 			index ++;
1113 			break;
1114 		}
1115 	}
1116 
1117 	return(1);
1118 }
1119 
1120 /**
1121  * lpfc_get_hba_model_desc: Retrieve HBA device model name and description.
1122  * @phba: pointer to lpfc hba data structure.
1123  * @mdp: pointer to the data structure to hold the derived model name.
1124  * @descp: pointer to the data structure to hold the derived description.
1125  *
1126  * This routine retrieves HBA's description based on its registered PCI device
1127  * ID. The @descp passed into this function points to an array of 256 chars. It
1128  * shall be returned with the model name, maximum speed, and the host bus type.
1129  * The @mdp passed into this function points to an array of 80 chars. When the
1130  * function returns, the @mdp will be filled with the model name.
1131  **/
1132 static void
1133 lpfc_get_hba_model_desc(struct lpfc_hba *phba, uint8_t *mdp, uint8_t *descp)
1134 {
1135 	lpfc_vpd_t *vp;
1136 	uint16_t dev_id = phba->pcidev->device;
1137 	int max_speed;
1138 	struct {
1139 		char * name;
1140 		int    max_speed;
1141 		char * bus;
1142 	} m = {"<Unknown>", 0, ""};
1143 
1144 	if (mdp && mdp[0] != '\0'
1145 		&& descp && descp[0] != '\0')
1146 		return;
1147 
1148 	if (phba->lmt & LMT_10Gb)
1149 		max_speed = 10;
1150 	else if (phba->lmt & LMT_8Gb)
1151 		max_speed = 8;
1152 	else if (phba->lmt & LMT_4Gb)
1153 		max_speed = 4;
1154 	else if (phba->lmt & LMT_2Gb)
1155 		max_speed = 2;
1156 	else
1157 		max_speed = 1;
1158 
1159 	vp = &phba->vpd;
1160 
1161 	switch (dev_id) {
1162 	case PCI_DEVICE_ID_FIREFLY:
1163 		m = (typeof(m)){"LP6000", max_speed, "PCI"};
1164 		break;
1165 	case PCI_DEVICE_ID_SUPERFLY:
1166 		if (vp->rev.biuRev >= 1 && vp->rev.biuRev <= 3)
1167 			m = (typeof(m)){"LP7000", max_speed,  "PCI"};
1168 		else
1169 			m = (typeof(m)){"LP7000E", max_speed, "PCI"};
1170 		break;
1171 	case PCI_DEVICE_ID_DRAGONFLY:
1172 		m = (typeof(m)){"LP8000", max_speed, "PCI"};
1173 		break;
1174 	case PCI_DEVICE_ID_CENTAUR:
1175 		if (FC_JEDEC_ID(vp->rev.biuRev) == CENTAUR_2G_JEDEC_ID)
1176 			m = (typeof(m)){"LP9002", max_speed, "PCI"};
1177 		else
1178 			m = (typeof(m)){"LP9000", max_speed, "PCI"};
1179 		break;
1180 	case PCI_DEVICE_ID_RFLY:
1181 		m = (typeof(m)){"LP952", max_speed, "PCI"};
1182 		break;
1183 	case PCI_DEVICE_ID_PEGASUS:
1184 		m = (typeof(m)){"LP9802", max_speed, "PCI-X"};
1185 		break;
1186 	case PCI_DEVICE_ID_THOR:
1187 		m = (typeof(m)){"LP10000", max_speed, "PCI-X"};
1188 		break;
1189 	case PCI_DEVICE_ID_VIPER:
1190 		m = (typeof(m)){"LPX1000", max_speed,  "PCI-X"};
1191 		break;
1192 	case PCI_DEVICE_ID_PFLY:
1193 		m = (typeof(m)){"LP982", max_speed, "PCI-X"};
1194 		break;
1195 	case PCI_DEVICE_ID_TFLY:
1196 		m = (typeof(m)){"LP1050", max_speed, "PCI-X"};
1197 		break;
1198 	case PCI_DEVICE_ID_HELIOS:
1199 		m = (typeof(m)){"LP11000", max_speed, "PCI-X2"};
1200 		break;
1201 	case PCI_DEVICE_ID_HELIOS_SCSP:
1202 		m = (typeof(m)){"LP11000-SP", max_speed, "PCI-X2"};
1203 		break;
1204 	case PCI_DEVICE_ID_HELIOS_DCSP:
1205 		m = (typeof(m)){"LP11002-SP", max_speed, "PCI-X2"};
1206 		break;
1207 	case PCI_DEVICE_ID_NEPTUNE:
1208 		m = (typeof(m)){"LPe1000", max_speed, "PCIe"};
1209 		break;
1210 	case PCI_DEVICE_ID_NEPTUNE_SCSP:
1211 		m = (typeof(m)){"LPe1000-SP", max_speed, "PCIe"};
1212 		break;
1213 	case PCI_DEVICE_ID_NEPTUNE_DCSP:
1214 		m = (typeof(m)){"LPe1002-SP", max_speed, "PCIe"};
1215 		break;
1216 	case PCI_DEVICE_ID_BMID:
1217 		m = (typeof(m)){"LP1150", max_speed, "PCI-X2"};
1218 		break;
1219 	case PCI_DEVICE_ID_BSMB:
1220 		m = (typeof(m)){"LP111", max_speed, "PCI-X2"};
1221 		break;
1222 	case PCI_DEVICE_ID_ZEPHYR:
1223 		m = (typeof(m)){"LPe11000", max_speed, "PCIe"};
1224 		break;
1225 	case PCI_DEVICE_ID_ZEPHYR_SCSP:
1226 		m = (typeof(m)){"LPe11000", max_speed, "PCIe"};
1227 		break;
1228 	case PCI_DEVICE_ID_ZEPHYR_DCSP:
1229 		m = (typeof(m)){"LPe11002-SP", max_speed, "PCIe"};
1230 		break;
1231 	case PCI_DEVICE_ID_ZMID:
1232 		m = (typeof(m)){"LPe1150", max_speed, "PCIe"};
1233 		break;
1234 	case PCI_DEVICE_ID_ZSMB:
1235 		m = (typeof(m)){"LPe111", max_speed, "PCIe"};
1236 		break;
1237 	case PCI_DEVICE_ID_LP101:
1238 		m = (typeof(m)){"LP101", max_speed, "PCI-X"};
1239 		break;
1240 	case PCI_DEVICE_ID_LP10000S:
1241 		m = (typeof(m)){"LP10000-S", max_speed, "PCI"};
1242 		break;
1243 	case PCI_DEVICE_ID_LP11000S:
1244 		m = (typeof(m)){"LP11000-S", max_speed,
1245 			"PCI-X2"};
1246 		break;
1247 	case PCI_DEVICE_ID_LPE11000S:
1248 		m = (typeof(m)){"LPe11000-S", max_speed,
1249 			"PCIe"};
1250 		break;
1251 	case PCI_DEVICE_ID_SAT:
1252 		m = (typeof(m)){"LPe12000", max_speed, "PCIe"};
1253 		break;
1254 	case PCI_DEVICE_ID_SAT_MID:
1255 		m = (typeof(m)){"LPe1250", max_speed, "PCIe"};
1256 		break;
1257 	case PCI_DEVICE_ID_SAT_SMB:
1258 		m = (typeof(m)){"LPe121", max_speed, "PCIe"};
1259 		break;
1260 	case PCI_DEVICE_ID_SAT_DCSP:
1261 		m = (typeof(m)){"LPe12002-SP", max_speed, "PCIe"};
1262 		break;
1263 	case PCI_DEVICE_ID_SAT_SCSP:
1264 		m = (typeof(m)){"LPe12000-SP", max_speed, "PCIe"};
1265 		break;
1266 	case PCI_DEVICE_ID_SAT_S:
1267 		m = (typeof(m)){"LPe12000-S", max_speed, "PCIe"};
1268 		break;
1269 	default:
1270 		m = (typeof(m)){ NULL };
1271 		break;
1272 	}
1273 
1274 	if (mdp && mdp[0] == '\0')
1275 		snprintf(mdp, 79,"%s", m.name);
1276 	if (descp && descp[0] == '\0')
1277 		snprintf(descp, 255,
1278 			 "Emulex %s %dGb %s Fibre Channel Adapter",
1279 			 m.name, m.max_speed, m.bus);
1280 }
1281 
1282 /**
1283  * lpfc_post_buffer: Post IOCB(s) with DMA buffer descriptor(s) to a IOCB ring.
1284  * @phba: pointer to lpfc hba data structure.
1285  * @pring: pointer to a IOCB ring.
1286  * @cnt: the number of IOCBs to be posted to the IOCB ring.
1287  *
1288  * This routine posts a given number of IOCBs with the associated DMA buffer
1289  * descriptors specified by the cnt argument to the given IOCB ring.
1290  *
1291  * Return codes
1292  *   The number of IOCBs NOT able to be posted to the IOCB ring.
1293  **/
1294 int
1295 lpfc_post_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring, int cnt)
1296 {
1297 	IOCB_t *icmd;
1298 	struct lpfc_iocbq *iocb;
1299 	struct lpfc_dmabuf *mp1, *mp2;
1300 
1301 	cnt += pring->missbufcnt;
1302 
1303 	/* While there are buffers to post */
1304 	while (cnt > 0) {
1305 		/* Allocate buffer for  command iocb */
1306 		iocb = lpfc_sli_get_iocbq(phba);
1307 		if (iocb == NULL) {
1308 			pring->missbufcnt = cnt;
1309 			return cnt;
1310 		}
1311 		icmd = &iocb->iocb;
1312 
1313 		/* 2 buffers can be posted per command */
1314 		/* Allocate buffer to post */
1315 		mp1 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
1316 		if (mp1)
1317 		    mp1->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &mp1->phys);
1318 		if (!mp1 || !mp1->virt) {
1319 			kfree(mp1);
1320 			lpfc_sli_release_iocbq(phba, iocb);
1321 			pring->missbufcnt = cnt;
1322 			return cnt;
1323 		}
1324 
1325 		INIT_LIST_HEAD(&mp1->list);
1326 		/* Allocate buffer to post */
1327 		if (cnt > 1) {
1328 			mp2 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
1329 			if (mp2)
1330 				mp2->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
1331 							    &mp2->phys);
1332 			if (!mp2 || !mp2->virt) {
1333 				kfree(mp2);
1334 				lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
1335 				kfree(mp1);
1336 				lpfc_sli_release_iocbq(phba, iocb);
1337 				pring->missbufcnt = cnt;
1338 				return cnt;
1339 			}
1340 
1341 			INIT_LIST_HEAD(&mp2->list);
1342 		} else {
1343 			mp2 = NULL;
1344 		}
1345 
1346 		icmd->un.cont64[0].addrHigh = putPaddrHigh(mp1->phys);
1347 		icmd->un.cont64[0].addrLow = putPaddrLow(mp1->phys);
1348 		icmd->un.cont64[0].tus.f.bdeSize = FCELSSIZE;
1349 		icmd->ulpBdeCount = 1;
1350 		cnt--;
1351 		if (mp2) {
1352 			icmd->un.cont64[1].addrHigh = putPaddrHigh(mp2->phys);
1353 			icmd->un.cont64[1].addrLow = putPaddrLow(mp2->phys);
1354 			icmd->un.cont64[1].tus.f.bdeSize = FCELSSIZE;
1355 			cnt--;
1356 			icmd->ulpBdeCount = 2;
1357 		}
1358 
1359 		icmd->ulpCommand = CMD_QUE_RING_BUF64_CN;
1360 		icmd->ulpLe = 1;
1361 
1362 		if (lpfc_sli_issue_iocb(phba, pring, iocb, 0) == IOCB_ERROR) {
1363 			lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
1364 			kfree(mp1);
1365 			cnt++;
1366 			if (mp2) {
1367 				lpfc_mbuf_free(phba, mp2->virt, mp2->phys);
1368 				kfree(mp2);
1369 				cnt++;
1370 			}
1371 			lpfc_sli_release_iocbq(phba, iocb);
1372 			pring->missbufcnt = cnt;
1373 			return cnt;
1374 		}
1375 		lpfc_sli_ringpostbuf_put(phba, pring, mp1);
1376 		if (mp2)
1377 			lpfc_sli_ringpostbuf_put(phba, pring, mp2);
1378 	}
1379 	pring->missbufcnt = 0;
1380 	return 0;
1381 }
1382 
1383 /**
1384  * lpfc_post_rcv_buf: Post the initial receive IOCB buffers to ELS ring.
1385  * @phba: pointer to lpfc hba data structure.
1386  *
1387  * This routine posts initial receive IOCB buffers to the ELS ring. The
1388  * current number of initial IOCB buffers specified by LPFC_BUF_RING0 is
1389  * set to 64 IOCBs.
1390  *
1391  * Return codes
1392  *   0 - success (currently always success)
1393  **/
1394 static int
1395 lpfc_post_rcv_buf(struct lpfc_hba *phba)
1396 {
1397 	struct lpfc_sli *psli = &phba->sli;
1398 
1399 	/* Ring 0, ELS / CT buffers */
1400 	lpfc_post_buffer(phba, &psli->ring[LPFC_ELS_RING], LPFC_BUF_RING0);
1401 	/* Ring 2 - FCP no buffers needed */
1402 
1403 	return 0;
1404 }
1405 
1406 #define S(N,V) (((V)<<(N))|((V)>>(32-(N))))
1407 
1408 /**
1409  * lpfc_sha_init: Set up initial array of hash table entries.
1410  * @HashResultPointer: pointer to an array as hash table.
1411  *
1412  * This routine sets up the initial values to the array of hash table entries
1413  * for the LC HBAs.
1414  **/
1415 static void
1416 lpfc_sha_init(uint32_t * HashResultPointer)
1417 {
1418 	HashResultPointer[0] = 0x67452301;
1419 	HashResultPointer[1] = 0xEFCDAB89;
1420 	HashResultPointer[2] = 0x98BADCFE;
1421 	HashResultPointer[3] = 0x10325476;
1422 	HashResultPointer[4] = 0xC3D2E1F0;
1423 }
1424 
1425 /**
1426  * lpfc_sha_iterate: Iterate initial hash table with the working hash table.
1427  * @HashResultPointer: pointer to an initial/result hash table.
1428  * @HashWorkingPointer: pointer to an working hash table.
1429  *
1430  * This routine iterates an initial hash table pointed by @HashResultPointer
1431  * with the values from the working hash table pointeed by @HashWorkingPointer.
1432  * The results are putting back to the initial hash table, returned through
1433  * the @HashResultPointer as the result hash table.
1434  **/
1435 static void
1436 lpfc_sha_iterate(uint32_t * HashResultPointer, uint32_t * HashWorkingPointer)
1437 {
1438 	int t;
1439 	uint32_t TEMP;
1440 	uint32_t A, B, C, D, E;
1441 	t = 16;
1442 	do {
1443 		HashWorkingPointer[t] =
1444 		    S(1,
1445 		      HashWorkingPointer[t - 3] ^ HashWorkingPointer[t -
1446 								     8] ^
1447 		      HashWorkingPointer[t - 14] ^ HashWorkingPointer[t - 16]);
1448 	} while (++t <= 79);
1449 	t = 0;
1450 	A = HashResultPointer[0];
1451 	B = HashResultPointer[1];
1452 	C = HashResultPointer[2];
1453 	D = HashResultPointer[3];
1454 	E = HashResultPointer[4];
1455 
1456 	do {
1457 		if (t < 20) {
1458 			TEMP = ((B & C) | ((~B) & D)) + 0x5A827999;
1459 		} else if (t < 40) {
1460 			TEMP = (B ^ C ^ D) + 0x6ED9EBA1;
1461 		} else if (t < 60) {
1462 			TEMP = ((B & C) | (B & D) | (C & D)) + 0x8F1BBCDC;
1463 		} else {
1464 			TEMP = (B ^ C ^ D) + 0xCA62C1D6;
1465 		}
1466 		TEMP += S(5, A) + E + HashWorkingPointer[t];
1467 		E = D;
1468 		D = C;
1469 		C = S(30, B);
1470 		B = A;
1471 		A = TEMP;
1472 	} while (++t <= 79);
1473 
1474 	HashResultPointer[0] += A;
1475 	HashResultPointer[1] += B;
1476 	HashResultPointer[2] += C;
1477 	HashResultPointer[3] += D;
1478 	HashResultPointer[4] += E;
1479 
1480 }
1481 
1482 /**
1483  * lpfc_challenge_key: Create challenge key based on WWPN of the HBA.
1484  * @RandomChallenge: pointer to the entry of host challenge random number array.
1485  * @HashWorking: pointer to the entry of the working hash array.
1486  *
1487  * This routine calculates the working hash array referred by @HashWorking
1488  * from the challenge random numbers associated with the host, referred by
1489  * @RandomChallenge. The result is put into the entry of the working hash
1490  * array and returned by reference through @HashWorking.
1491  **/
1492 static void
1493 lpfc_challenge_key(uint32_t * RandomChallenge, uint32_t * HashWorking)
1494 {
1495 	*HashWorking = (*RandomChallenge ^ *HashWorking);
1496 }
1497 
1498 /**
1499  * lpfc_hba_init: Perform special handling for LC HBA initialization.
1500  * @phba: pointer to lpfc hba data structure.
1501  * @hbainit: pointer to an array of unsigned 32-bit integers.
1502  *
1503  * This routine performs the special handling for LC HBA initialization.
1504  **/
1505 void
1506 lpfc_hba_init(struct lpfc_hba *phba, uint32_t *hbainit)
1507 {
1508 	int t;
1509 	uint32_t *HashWorking;
1510 	uint32_t *pwwnn = (uint32_t *) phba->wwnn;
1511 
1512 	HashWorking = kcalloc(80, sizeof(uint32_t), GFP_KERNEL);
1513 	if (!HashWorking)
1514 		return;
1515 
1516 	HashWorking[0] = HashWorking[78] = *pwwnn++;
1517 	HashWorking[1] = HashWorking[79] = *pwwnn;
1518 
1519 	for (t = 0; t < 7; t++)
1520 		lpfc_challenge_key(phba->RandomData + t, HashWorking + t);
1521 
1522 	lpfc_sha_init(hbainit);
1523 	lpfc_sha_iterate(hbainit, HashWorking);
1524 	kfree(HashWorking);
1525 }
1526 
1527 /**
1528  * lpfc_cleanup: Performs vport cleanups before deleting a vport.
1529  * @vport: pointer to a virtual N_Port data structure.
1530  *
1531  * This routine performs the necessary cleanups before deleting the @vport.
1532  * It invokes the discovery state machine to perform necessary state
1533  * transitions and to release the ndlps associated with the @vport. Note,
1534  * the physical port is treated as @vport 0.
1535  **/
1536 void
1537 lpfc_cleanup(struct lpfc_vport *vport)
1538 {
1539 	struct lpfc_hba   *phba = vport->phba;
1540 	struct lpfc_nodelist *ndlp, *next_ndlp;
1541 	int i = 0;
1542 
1543 	if (phba->link_state > LPFC_LINK_DOWN)
1544 		lpfc_port_link_failure(vport);
1545 
1546 	list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
1547 		if (!NLP_CHK_NODE_ACT(ndlp)) {
1548 			ndlp = lpfc_enable_node(vport, ndlp,
1549 						NLP_STE_UNUSED_NODE);
1550 			if (!ndlp)
1551 				continue;
1552 			spin_lock_irq(&phba->ndlp_lock);
1553 			NLP_SET_FREE_REQ(ndlp);
1554 			spin_unlock_irq(&phba->ndlp_lock);
1555 			/* Trigger the release of the ndlp memory */
1556 			lpfc_nlp_put(ndlp);
1557 			continue;
1558 		}
1559 		spin_lock_irq(&phba->ndlp_lock);
1560 		if (NLP_CHK_FREE_REQ(ndlp)) {
1561 			/* The ndlp should not be in memory free mode already */
1562 			spin_unlock_irq(&phba->ndlp_lock);
1563 			continue;
1564 		} else
1565 			/* Indicate request for freeing ndlp memory */
1566 			NLP_SET_FREE_REQ(ndlp);
1567 		spin_unlock_irq(&phba->ndlp_lock);
1568 
1569 		if (vport->port_type != LPFC_PHYSICAL_PORT &&
1570 		    ndlp->nlp_DID == Fabric_DID) {
1571 			/* Just free up ndlp with Fabric_DID for vports */
1572 			lpfc_nlp_put(ndlp);
1573 			continue;
1574 		}
1575 
1576 		if (ndlp->nlp_type & NLP_FABRIC)
1577 			lpfc_disc_state_machine(vport, ndlp, NULL,
1578 					NLP_EVT_DEVICE_RECOVERY);
1579 
1580 		lpfc_disc_state_machine(vport, ndlp, NULL,
1581 					     NLP_EVT_DEVICE_RM);
1582 
1583 		/* nlp_type zero is not defined, nlp_flag zero also not defined,
1584 		 * nlp_state is unused, this happens when
1585 		 * an initiator has logged
1586 		 * into us so cleanup this ndlp.
1587 		 */
1588 		if ((ndlp->nlp_type == 0) && (ndlp->nlp_flag == 0) &&
1589 			(ndlp->nlp_state == 0))
1590 			lpfc_nlp_put(ndlp);
1591 	}
1592 
1593 	/* At this point, ALL ndlp's should be gone
1594 	 * because of the previous NLP_EVT_DEVICE_RM.
1595 	 * Lets wait for this to happen, if needed.
1596 	 */
1597 	while (!list_empty(&vport->fc_nodes)) {
1598 
1599 		if (i++ > 3000) {
1600 			lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
1601 				"0233 Nodelist not empty\n");
1602 			list_for_each_entry_safe(ndlp, next_ndlp,
1603 						&vport->fc_nodes, nlp_listp) {
1604 				lpfc_printf_vlog(ndlp->vport, KERN_ERR,
1605 						LOG_NODE,
1606 						"0282: did:x%x ndlp:x%p "
1607 						"usgmap:x%x refcnt:%d\n",
1608 						ndlp->nlp_DID, (void *)ndlp,
1609 						ndlp->nlp_usg_map,
1610 						atomic_read(
1611 							&ndlp->kref.refcount));
1612 			}
1613 			break;
1614 		}
1615 
1616 		/* Wait for any activity on ndlps to settle */
1617 		msleep(10);
1618 	}
1619 	return;
1620 }
1621 
1622 /**
1623  * lpfc_stop_vport_timers: Stop all the timers associated with a vport.
1624  * @vport: pointer to a virtual N_Port data structure.
1625  *
1626  * This routine stops all the timers associated with a @vport. This function
1627  * is invoked before disabling or deleting a @vport. Note that the physical
1628  * port is treated as @vport 0.
1629  **/
1630 void
1631 lpfc_stop_vport_timers(struct lpfc_vport *vport)
1632 {
1633 	del_timer_sync(&vport->els_tmofunc);
1634 	del_timer_sync(&vport->fc_fdmitmo);
1635 	lpfc_can_disctmo(vport);
1636 	return;
1637 }
1638 
1639 /**
1640  * lpfc_stop_phba_timers: Stop all the timers associated with an HBA.
1641  * @phba: pointer to lpfc hba data structure.
1642  *
1643  * This routine stops all the timers associated with a HBA. This function is
1644  * invoked before either putting a HBA offline or unloading the driver.
1645  **/
1646 static void
1647 lpfc_stop_phba_timers(struct lpfc_hba *phba)
1648 {
1649 	del_timer_sync(&phba->fcp_poll_timer);
1650 	lpfc_stop_vport_timers(phba->pport);
1651 	del_timer_sync(&phba->sli.mbox_tmo);
1652 	del_timer_sync(&phba->fabric_block_timer);
1653 	phba->hb_outstanding = 0;
1654 	del_timer_sync(&phba->hb_tmofunc);
1655 	return;
1656 }
1657 
1658 /**
1659  * lpfc_block_mgmt_io: Mark a HBA's management interface as blocked.
1660  * @phba: pointer to lpfc hba data structure.
1661  *
1662  * This routine marks a HBA's management interface as blocked. Once the HBA's
1663  * management interface is marked as blocked, all the user space access to
1664  * the HBA, whether they are from sysfs interface or libdfc interface will
1665  * all be blocked. The HBA is set to block the management interface when the
1666  * driver prepares the HBA interface for online or offline.
1667  **/
1668 static void
1669 lpfc_block_mgmt_io(struct lpfc_hba * phba)
1670 {
1671 	unsigned long iflag;
1672 
1673 	spin_lock_irqsave(&phba->hbalock, iflag);
1674 	phba->sli.sli_flag |= LPFC_BLOCK_MGMT_IO;
1675 	spin_unlock_irqrestore(&phba->hbalock, iflag);
1676 }
1677 
1678 /**
1679  * lpfc_online: Initialize and bring a HBA online.
1680  * @phba: pointer to lpfc hba data structure.
1681  *
1682  * This routine initializes the HBA and brings a HBA online. During this
1683  * process, the management interface is blocked to prevent user space access
1684  * to the HBA interfering with the driver initialization.
1685  *
1686  * Return codes
1687  *   0 - successful
1688  *   1 - failed
1689  **/
1690 int
1691 lpfc_online(struct lpfc_hba *phba)
1692 {
1693 	struct lpfc_vport *vport = phba->pport;
1694 	struct lpfc_vport **vports;
1695 	int i;
1696 
1697 	if (!phba)
1698 		return 0;
1699 
1700 	if (!(vport->fc_flag & FC_OFFLINE_MODE))
1701 		return 0;
1702 
1703 	lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
1704 			"0458 Bring Adapter online\n");
1705 
1706 	lpfc_block_mgmt_io(phba);
1707 
1708 	if (!lpfc_sli_queue_setup(phba)) {
1709 		lpfc_unblock_mgmt_io(phba);
1710 		return 1;
1711 	}
1712 
1713 	if (lpfc_sli_hba_setup(phba)) {	/* Initialize the HBA */
1714 		lpfc_unblock_mgmt_io(phba);
1715 		return 1;
1716 	}
1717 
1718 	vports = lpfc_create_vport_work_array(phba);
1719 	if (vports != NULL)
1720 		for(i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
1721 			struct Scsi_Host *shost;
1722 			shost = lpfc_shost_from_vport(vports[i]);
1723 			spin_lock_irq(shost->host_lock);
1724 			vports[i]->fc_flag &= ~FC_OFFLINE_MODE;
1725 			if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
1726 				vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
1727 			spin_unlock_irq(shost->host_lock);
1728 		}
1729 		lpfc_destroy_vport_work_array(phba, vports);
1730 
1731 	lpfc_unblock_mgmt_io(phba);
1732 	return 0;
1733 }
1734 
1735 /**
1736  * lpfc_unblock_mgmt_io: Mark a HBA's management interface to be not blocked.
1737  * @phba: pointer to lpfc hba data structure.
1738  *
1739  * This routine marks a HBA's management interface as not blocked. Once the
1740  * HBA's management interface is marked as not blocked, all the user space
1741  * access to the HBA, whether they are from sysfs interface or libdfc
1742  * interface will be allowed. The HBA is set to block the management interface
1743  * when the driver prepares the HBA interface for online or offline and then
1744  * set to unblock the management interface afterwards.
1745  **/
1746 void
1747 lpfc_unblock_mgmt_io(struct lpfc_hba * phba)
1748 {
1749 	unsigned long iflag;
1750 
1751 	spin_lock_irqsave(&phba->hbalock, iflag);
1752 	phba->sli.sli_flag &= ~LPFC_BLOCK_MGMT_IO;
1753 	spin_unlock_irqrestore(&phba->hbalock, iflag);
1754 }
1755 
1756 /**
1757  * lpfc_offline_prep: Prepare a HBA to be brought offline.
1758  * @phba: pointer to lpfc hba data structure.
1759  *
1760  * This routine is invoked to prepare a HBA to be brought offline. It performs
1761  * unregistration login to all the nodes on all vports and flushes the mailbox
1762  * queue to make it ready to be brought offline.
1763  **/
1764 void
1765 lpfc_offline_prep(struct lpfc_hba * phba)
1766 {
1767 	struct lpfc_vport *vport = phba->pport;
1768 	struct lpfc_nodelist  *ndlp, *next_ndlp;
1769 	struct lpfc_vport **vports;
1770 	int i;
1771 
1772 	if (vport->fc_flag & FC_OFFLINE_MODE)
1773 		return;
1774 
1775 	lpfc_block_mgmt_io(phba);
1776 
1777 	lpfc_linkdown(phba);
1778 
1779 	/* Issue an unreg_login to all nodes on all vports */
1780 	vports = lpfc_create_vport_work_array(phba);
1781 	if (vports != NULL) {
1782 		for(i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
1783 			struct Scsi_Host *shost;
1784 
1785 			if (vports[i]->load_flag & FC_UNLOADING)
1786 				continue;
1787 			shost =	lpfc_shost_from_vport(vports[i]);
1788 			list_for_each_entry_safe(ndlp, next_ndlp,
1789 						 &vports[i]->fc_nodes,
1790 						 nlp_listp) {
1791 				if (!NLP_CHK_NODE_ACT(ndlp))
1792 					continue;
1793 				if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
1794 					continue;
1795 				if (ndlp->nlp_type & NLP_FABRIC) {
1796 					lpfc_disc_state_machine(vports[i], ndlp,
1797 						NULL, NLP_EVT_DEVICE_RECOVERY);
1798 					lpfc_disc_state_machine(vports[i], ndlp,
1799 						NULL, NLP_EVT_DEVICE_RM);
1800 				}
1801 				spin_lock_irq(shost->host_lock);
1802 				ndlp->nlp_flag &= ~NLP_NPR_ADISC;
1803 				spin_unlock_irq(shost->host_lock);
1804 				lpfc_unreg_rpi(vports[i], ndlp);
1805 			}
1806 		}
1807 	}
1808 	lpfc_destroy_vport_work_array(phba, vports);
1809 
1810 	lpfc_sli_flush_mbox_queue(phba);
1811 }
1812 
1813 /**
1814  * lpfc_offline: Bring a HBA offline.
1815  * @phba: pointer to lpfc hba data structure.
1816  *
1817  * This routine actually brings a HBA offline. It stops all the timers
1818  * associated with the HBA, brings down the SLI layer, and eventually
1819  * marks the HBA as in offline state for the upper layer protocol.
1820  **/
1821 void
1822 lpfc_offline(struct lpfc_hba *phba)
1823 {
1824 	struct Scsi_Host  *shost;
1825 	struct lpfc_vport **vports;
1826 	int i;
1827 
1828 	if (phba->pport->fc_flag & FC_OFFLINE_MODE)
1829 		return;
1830 
1831 	/* stop all timers associated with this hba */
1832 	lpfc_stop_phba_timers(phba);
1833 	vports = lpfc_create_vport_work_array(phba);
1834 	if (vports != NULL)
1835 		for(i = 0; i <= phba->max_vpi && vports[i] != NULL; i++)
1836 			lpfc_stop_vport_timers(vports[i]);
1837 	lpfc_destroy_vport_work_array(phba, vports);
1838 	lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
1839 			"0460 Bring Adapter offline\n");
1840 	/* Bring down the SLI Layer and cleanup.  The HBA is offline
1841 	   now.  */
1842 	lpfc_sli_hba_down(phba);
1843 	spin_lock_irq(&phba->hbalock);
1844 	phba->work_ha = 0;
1845 	spin_unlock_irq(&phba->hbalock);
1846 	vports = lpfc_create_vport_work_array(phba);
1847 	if (vports != NULL)
1848 		for(i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
1849 			shost = lpfc_shost_from_vport(vports[i]);
1850 			spin_lock_irq(shost->host_lock);
1851 			vports[i]->work_port_events = 0;
1852 			vports[i]->fc_flag |= FC_OFFLINE_MODE;
1853 			spin_unlock_irq(shost->host_lock);
1854 		}
1855 	lpfc_destroy_vport_work_array(phba, vports);
1856 }
1857 
1858 /**
1859  * lpfc_scsi_free: Free all the SCSI buffers and IOCBs from driver lists.
1860  * @phba: pointer to lpfc hba data structure.
1861  *
1862  * This routine is to free all the SCSI buffers and IOCBs from the driver
1863  * list back to kernel. It is called from lpfc_pci_remove_one to free
1864  * the internal resources before the device is removed from the system.
1865  *
1866  * Return codes
1867  *   0 - successful (for now, it always returns 0)
1868  **/
1869 static int
1870 lpfc_scsi_free(struct lpfc_hba *phba)
1871 {
1872 	struct lpfc_scsi_buf *sb, *sb_next;
1873 	struct lpfc_iocbq *io, *io_next;
1874 
1875 	spin_lock_irq(&phba->hbalock);
1876 	/* Release all the lpfc_scsi_bufs maintained by this host. */
1877 	list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list, list) {
1878 		list_del(&sb->list);
1879 		pci_pool_free(phba->lpfc_scsi_dma_buf_pool, sb->data,
1880 			      sb->dma_handle);
1881 		kfree(sb);
1882 		phba->total_scsi_bufs--;
1883 	}
1884 
1885 	/* Release all the lpfc_iocbq entries maintained by this host. */
1886 	list_for_each_entry_safe(io, io_next, &phba->lpfc_iocb_list, list) {
1887 		list_del(&io->list);
1888 		kfree(io);
1889 		phba->total_iocbq_bufs--;
1890 	}
1891 
1892 	spin_unlock_irq(&phba->hbalock);
1893 
1894 	return 0;
1895 }
1896 
1897 /**
1898  * lpfc_create_port: Create an FC port.
1899  * @phba: pointer to lpfc hba data structure.
1900  * @instance: a unique integer ID to this FC port.
1901  * @dev: pointer to the device data structure.
1902  *
1903  * This routine creates a FC port for the upper layer protocol. The FC port
1904  * can be created on top of either a physical port or a virtual port provided
1905  * by the HBA. This routine also allocates a SCSI host data structure (shost)
1906  * and associates the FC port created before adding the shost into the SCSI
1907  * layer.
1908  *
1909  * Return codes
1910  *   @vport - pointer to the virtual N_Port data structure.
1911  *   NULL - port create failed.
1912  **/
1913 struct lpfc_vport *
1914 lpfc_create_port(struct lpfc_hba *phba, int instance, struct device *dev)
1915 {
1916 	struct lpfc_vport *vport;
1917 	struct Scsi_Host  *shost;
1918 	int error = 0;
1919 
1920 	if (dev != &phba->pcidev->dev)
1921 		shost = scsi_host_alloc(&lpfc_vport_template,
1922 					sizeof(struct lpfc_vport));
1923 	else
1924 		shost = scsi_host_alloc(&lpfc_template,
1925 					sizeof(struct lpfc_vport));
1926 	if (!shost)
1927 		goto out;
1928 
1929 	vport = (struct lpfc_vport *) shost->hostdata;
1930 	vport->phba = phba;
1931 	vport->load_flag |= FC_LOADING;
1932 	vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
1933 	vport->fc_rscn_flush = 0;
1934 
1935 	lpfc_get_vport_cfgparam(vport);
1936 	shost->unique_id = instance;
1937 	shost->max_id = LPFC_MAX_TARGET;
1938 	shost->max_lun = vport->cfg_max_luns;
1939 	shost->this_id = -1;
1940 	shost->max_cmd_len = 16;
1941 	/*
1942 	 * Set initial can_queue value since 0 is no longer supported and
1943 	 * scsi_add_host will fail. This will be adjusted later based on the
1944 	 * max xri value determined in hba setup.
1945 	 */
1946 	shost->can_queue = phba->cfg_hba_queue_depth - 10;
1947 	if (dev != &phba->pcidev->dev) {
1948 		shost->transportt = lpfc_vport_transport_template;
1949 		vport->port_type = LPFC_NPIV_PORT;
1950 	} else {
1951 		shost->transportt = lpfc_transport_template;
1952 		vport->port_type = LPFC_PHYSICAL_PORT;
1953 	}
1954 
1955 	/* Initialize all internally managed lists. */
1956 	INIT_LIST_HEAD(&vport->fc_nodes);
1957 	spin_lock_init(&vport->work_port_lock);
1958 
1959 	init_timer(&vport->fc_disctmo);
1960 	vport->fc_disctmo.function = lpfc_disc_timeout;
1961 	vport->fc_disctmo.data = (unsigned long)vport;
1962 
1963 	init_timer(&vport->fc_fdmitmo);
1964 	vport->fc_fdmitmo.function = lpfc_fdmi_tmo;
1965 	vport->fc_fdmitmo.data = (unsigned long)vport;
1966 
1967 	init_timer(&vport->els_tmofunc);
1968 	vport->els_tmofunc.function = lpfc_els_timeout;
1969 	vport->els_tmofunc.data = (unsigned long)vport;
1970 
1971 	error = scsi_add_host(shost, dev);
1972 	if (error)
1973 		goto out_put_shost;
1974 
1975 	spin_lock_irq(&phba->hbalock);
1976 	list_add_tail(&vport->listentry, &phba->port_list);
1977 	spin_unlock_irq(&phba->hbalock);
1978 	return vport;
1979 
1980 out_put_shost:
1981 	scsi_host_put(shost);
1982 out:
1983 	return NULL;
1984 }
1985 
1986 /**
1987  * destroy_port: Destroy an FC port.
1988  * @vport: pointer to an lpfc virtual N_Port data structure.
1989  *
1990  * This routine destroys a FC port from the upper layer protocol. All the
1991  * resources associated with the port are released.
1992  **/
1993 void
1994 destroy_port(struct lpfc_vport *vport)
1995 {
1996 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1997 	struct lpfc_hba  *phba = vport->phba;
1998 
1999 	kfree(vport->vname);
2000 
2001 	lpfc_debugfs_terminate(vport);
2002 	fc_remove_host(shost);
2003 	scsi_remove_host(shost);
2004 
2005 	spin_lock_irq(&phba->hbalock);
2006 	list_del_init(&vport->listentry);
2007 	spin_unlock_irq(&phba->hbalock);
2008 
2009 	lpfc_cleanup(vport);
2010 	return;
2011 }
2012 
2013 /**
2014  * lpfc_get_instance: Get a unique integer ID.
2015  *
2016  * This routine allocates a unique integer ID from lpfc_hba_index pool. It
2017  * uses the kernel idr facility to perform the task.
2018  *
2019  * Return codes:
2020  *   instance - a unique integer ID allocated as the new instance.
2021  *   -1 - lpfc get instance failed.
2022  **/
2023 int
2024 lpfc_get_instance(void)
2025 {
2026 	int instance = 0;
2027 
2028 	/* Assign an unused number */
2029 	if (!idr_pre_get(&lpfc_hba_index, GFP_KERNEL))
2030 		return -1;
2031 	if (idr_get_new(&lpfc_hba_index, NULL, &instance))
2032 		return -1;
2033 	return instance;
2034 }
2035 
2036 /**
2037  * lpfc_scan_finished: method for SCSI layer to detect whether scan is done.
2038  * @shost: pointer to SCSI host data structure.
2039  * @time: elapsed time of the scan in jiffies.
2040  *
2041  * This routine is called by the SCSI layer with a SCSI host to determine
2042  * whether the scan host is finished.
2043  *
2044  * Note: there is no scan_start function as adapter initialization will have
2045  * asynchronously kicked off the link initialization.
2046  *
2047  * Return codes
2048  *   0 - SCSI host scan is not over yet.
2049  *   1 - SCSI host scan is over.
2050  **/
2051 int lpfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
2052 {
2053 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2054 	struct lpfc_hba   *phba = vport->phba;
2055 	int stat = 0;
2056 
2057 	spin_lock_irq(shost->host_lock);
2058 
2059 	if (vport->load_flag & FC_UNLOADING) {
2060 		stat = 1;
2061 		goto finished;
2062 	}
2063 	if (time >= 30 * HZ) {
2064 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
2065 				"0461 Scanning longer than 30 "
2066 				"seconds.  Continuing initialization\n");
2067 		stat = 1;
2068 		goto finished;
2069 	}
2070 	if (time >= 15 * HZ && phba->link_state <= LPFC_LINK_DOWN) {
2071 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
2072 				"0465 Link down longer than 15 "
2073 				"seconds.  Continuing initialization\n");
2074 		stat = 1;
2075 		goto finished;
2076 	}
2077 
2078 	if (vport->port_state != LPFC_VPORT_READY)
2079 		goto finished;
2080 	if (vport->num_disc_nodes || vport->fc_prli_sent)
2081 		goto finished;
2082 	if (vport->fc_map_cnt == 0 && time < 2 * HZ)
2083 		goto finished;
2084 	if ((phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) != 0)
2085 		goto finished;
2086 
2087 	stat = 1;
2088 
2089 finished:
2090 	spin_unlock_irq(shost->host_lock);
2091 	return stat;
2092 }
2093 
2094 /**
2095  * lpfc_host_attrib_init: Initialize SCSI host attributes on a FC port.
2096  * @shost: pointer to SCSI host data structure.
2097  *
2098  * This routine initializes a given SCSI host attributes on a FC port. The
2099  * SCSI host can be either on top of a physical port or a virtual port.
2100  **/
2101 void lpfc_host_attrib_init(struct Scsi_Host *shost)
2102 {
2103 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2104 	struct lpfc_hba   *phba = vport->phba;
2105 	/*
2106 	 * Set fixed host attributes.  Must done after lpfc_sli_hba_setup().
2107 	 */
2108 
2109 	fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
2110 	fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
2111 	fc_host_supported_classes(shost) = FC_COS_CLASS3;
2112 
2113 	memset(fc_host_supported_fc4s(shost), 0,
2114 	       sizeof(fc_host_supported_fc4s(shost)));
2115 	fc_host_supported_fc4s(shost)[2] = 1;
2116 	fc_host_supported_fc4s(shost)[7] = 1;
2117 
2118 	lpfc_vport_symbolic_node_name(vport, fc_host_symbolic_name(shost),
2119 				 sizeof fc_host_symbolic_name(shost));
2120 
2121 	fc_host_supported_speeds(shost) = 0;
2122 	if (phba->lmt & LMT_10Gb)
2123 		fc_host_supported_speeds(shost) |= FC_PORTSPEED_10GBIT;
2124 	if (phba->lmt & LMT_8Gb)
2125 		fc_host_supported_speeds(shost) |= FC_PORTSPEED_8GBIT;
2126 	if (phba->lmt & LMT_4Gb)
2127 		fc_host_supported_speeds(shost) |= FC_PORTSPEED_4GBIT;
2128 	if (phba->lmt & LMT_2Gb)
2129 		fc_host_supported_speeds(shost) |= FC_PORTSPEED_2GBIT;
2130 	if (phba->lmt & LMT_1Gb)
2131 		fc_host_supported_speeds(shost) |= FC_PORTSPEED_1GBIT;
2132 
2133 	fc_host_maxframe_size(shost) =
2134 		(((uint32_t) vport->fc_sparam.cmn.bbRcvSizeMsb & 0x0F) << 8) |
2135 		(uint32_t) vport->fc_sparam.cmn.bbRcvSizeLsb;
2136 
2137 	/* This value is also unchanging */
2138 	memset(fc_host_active_fc4s(shost), 0,
2139 	       sizeof(fc_host_active_fc4s(shost)));
2140 	fc_host_active_fc4s(shost)[2] = 1;
2141 	fc_host_active_fc4s(shost)[7] = 1;
2142 
2143 	fc_host_max_npiv_vports(shost) = phba->max_vpi;
2144 	spin_lock_irq(shost->host_lock);
2145 	vport->load_flag &= ~FC_LOADING;
2146 	spin_unlock_irq(shost->host_lock);
2147 }
2148 
2149 /**
2150  * lpfc_enable_msix: Enable MSI-X interrupt mode.
2151  * @phba: pointer to lpfc hba data structure.
2152  *
2153  * This routine is invoked to enable the MSI-X interrupt vectors. The kernel
2154  * function pci_enable_msix() is called to enable the MSI-X vectors. Note that
2155  * pci_enable_msix(), once invoked, enables either all or nothing, depending
2156  * on the current availability of PCI vector resources. The device driver is
2157  * responsible for calling the individual request_irq() to register each MSI-X
2158  * vector with a interrupt handler, which is done in this function. Note that
2159  * later when device is unloading, the driver should always call free_irq()
2160  * on all MSI-X vectors it has done request_irq() on before calling
2161  * pci_disable_msix(). Failure to do so results in a BUG_ON() and a device
2162  * will be left with MSI-X enabled and leaks its vectors.
2163  *
2164  * Return codes
2165  *   0 - sucessful
2166  *   other values - error
2167  **/
2168 static int
2169 lpfc_enable_msix(struct lpfc_hba *phba)
2170 {
2171 	int error;
2172 
2173 	phba->msix_entries[0].entry = 0;
2174 	phba->msix_entries[0].vector = 0;
2175 
2176 	error = pci_enable_msix(phba->pcidev, phba->msix_entries,
2177 				ARRAY_SIZE(phba->msix_entries));
2178 	if (error) {
2179 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
2180 				"0420 Enable MSI-X failed (%d), continuing "
2181 				"with MSI\n", error);
2182 		pci_disable_msix(phba->pcidev);
2183 		return error;
2184 	}
2185 
2186 	error =	request_irq(phba->msix_entries[0].vector, lpfc_intr_handler, 0,
2187 			    LPFC_DRIVER_NAME, phba);
2188 	if (error) {
2189 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2190 				"0421 MSI-X request_irq failed (%d), "
2191 				"continuing with MSI\n", error);
2192 		pci_disable_msix(phba->pcidev);
2193 	}
2194 	return error;
2195 }
2196 
2197 /**
2198  * lpfc_disable_msix: Disable MSI-X interrupt mode.
2199  * @phba: pointer to lpfc hba data structure.
2200  *
2201  * This routine is invoked to release the MSI-X vectors and then disable the
2202  * MSI-X interrupt mode.
2203  **/
2204 static void
2205 lpfc_disable_msix(struct lpfc_hba *phba)
2206 {
2207 	free_irq(phba->msix_entries[0].vector, phba);
2208 	pci_disable_msix(phba->pcidev);
2209 }
2210 
2211 /**
2212  * lpfc_pci_probe_one: lpfc PCI probe func to register device to PCI subsystem.
2213  * @pdev: pointer to PCI device
2214  * @pid: pointer to PCI device identifier
2215  *
2216  * This routine is to be registered to the kernel's PCI subsystem. When an
2217  * Emulex HBA is presented in PCI bus, the kernel PCI subsystem looks at
2218  * PCI device-specific information of the device and driver to see if the
2219  * driver state that it can support this kind of device. If the match is
2220  * successful, the driver core invokes this routine. If this routine
2221  * determines it can claim the HBA, it does all the initialization that it
2222  * needs to do to handle the HBA properly.
2223  *
2224  * Return code
2225  *   0 - driver can claim the device
2226  *   negative value - driver can not claim the device
2227  **/
2228 static int __devinit
2229 lpfc_pci_probe_one(struct pci_dev *pdev, const struct pci_device_id *pid)
2230 {
2231 	struct lpfc_vport *vport = NULL;
2232 	struct lpfc_hba   *phba;
2233 	struct lpfc_sli   *psli;
2234 	struct lpfc_iocbq *iocbq_entry = NULL, *iocbq_next = NULL;
2235 	struct Scsi_Host  *shost = NULL;
2236 	void *ptr;
2237 	unsigned long bar0map_len, bar2map_len;
2238 	int error = -ENODEV, retval;
2239 	int  i, hbq_count;
2240 	uint16_t iotag;
2241 	int bars = pci_select_bars(pdev, IORESOURCE_MEM);
2242 
2243 	if (pci_enable_device_mem(pdev))
2244 		goto out;
2245 	if (pci_request_selected_regions(pdev, bars, LPFC_DRIVER_NAME))
2246 		goto out_disable_device;
2247 
2248 	phba = kzalloc(sizeof (struct lpfc_hba), GFP_KERNEL);
2249 	if (!phba)
2250 		goto out_release_regions;
2251 
2252 	spin_lock_init(&phba->hbalock);
2253 
2254 	/* Initialize ndlp management spinlock */
2255 	spin_lock_init(&phba->ndlp_lock);
2256 
2257 	phba->pcidev = pdev;
2258 
2259 	/* Assign an unused board number */
2260 	if ((phba->brd_no = lpfc_get_instance()) < 0)
2261 		goto out_free_phba;
2262 
2263 	INIT_LIST_HEAD(&phba->port_list);
2264 	/*
2265 	 * Get all the module params for configuring this host and then
2266 	 * establish the host.
2267 	 */
2268 	lpfc_get_cfgparam(phba);
2269 	phba->max_vpi = LPFC_MAX_VPI;
2270 
2271 	/* Initialize timers used by driver */
2272 	init_timer(&phba->hb_tmofunc);
2273 	phba->hb_tmofunc.function = lpfc_hb_timeout;
2274 	phba->hb_tmofunc.data = (unsigned long)phba;
2275 
2276 	psli = &phba->sli;
2277 	init_timer(&psli->mbox_tmo);
2278 	psli->mbox_tmo.function = lpfc_mbox_timeout;
2279 	psli->mbox_tmo.data = (unsigned long) phba;
2280 	init_timer(&phba->fcp_poll_timer);
2281 	phba->fcp_poll_timer.function = lpfc_poll_timeout;
2282 	phba->fcp_poll_timer.data = (unsigned long) phba;
2283 	init_timer(&phba->fabric_block_timer);
2284 	phba->fabric_block_timer.function = lpfc_fabric_block_timeout;
2285 	phba->fabric_block_timer.data = (unsigned long) phba;
2286 
2287 	pci_set_master(pdev);
2288 	pci_try_set_mwi(pdev);
2289 
2290 	if (pci_set_dma_mask(phba->pcidev, DMA_64BIT_MASK) != 0)
2291 		if (pci_set_dma_mask(phba->pcidev, DMA_32BIT_MASK) != 0)
2292 			goto out_idr_remove;
2293 
2294 	/*
2295 	 * Get the bus address of Bar0 and Bar2 and the number of bytes
2296 	 * required by each mapping.
2297 	 */
2298 	phba->pci_bar0_map = pci_resource_start(phba->pcidev, 0);
2299 	bar0map_len        = pci_resource_len(phba->pcidev, 0);
2300 
2301 	phba->pci_bar2_map = pci_resource_start(phba->pcidev, 2);
2302 	bar2map_len        = pci_resource_len(phba->pcidev, 2);
2303 
2304 	/* Map HBA SLIM to a kernel virtual address. */
2305 	phba->slim_memmap_p      = ioremap(phba->pci_bar0_map, bar0map_len);
2306 	if (!phba->slim_memmap_p) {
2307 		error = -ENODEV;
2308 		dev_printk(KERN_ERR, &pdev->dev,
2309 			   "ioremap failed for SLIM memory.\n");
2310 		goto out_idr_remove;
2311 	}
2312 
2313 	/* Map HBA Control Registers to a kernel virtual address. */
2314 	phba->ctrl_regs_memmap_p = ioremap(phba->pci_bar2_map, bar2map_len);
2315 	if (!phba->ctrl_regs_memmap_p) {
2316 		error = -ENODEV;
2317 		dev_printk(KERN_ERR, &pdev->dev,
2318 			   "ioremap failed for HBA control registers.\n");
2319 		goto out_iounmap_slim;
2320 	}
2321 
2322 	/* Allocate memory for SLI-2 structures */
2323 	phba->slim2p = dma_alloc_coherent(&phba->pcidev->dev, SLI2_SLIM_SIZE,
2324 					  &phba->slim2p_mapping, GFP_KERNEL);
2325 	if (!phba->slim2p)
2326 		goto out_iounmap;
2327 
2328 	memset(phba->slim2p, 0, SLI2_SLIM_SIZE);
2329 
2330 	phba->hbqslimp.virt = dma_alloc_coherent(&phba->pcidev->dev,
2331 						 lpfc_sli_hbq_size(),
2332 						 &phba->hbqslimp.phys,
2333 						 GFP_KERNEL);
2334 	if (!phba->hbqslimp.virt)
2335 		goto out_free_slim;
2336 
2337 	hbq_count = lpfc_sli_hbq_count();
2338 	ptr = phba->hbqslimp.virt;
2339 	for (i = 0; i < hbq_count; ++i) {
2340 		phba->hbqs[i].hbq_virt = ptr;
2341 		INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
2342 		ptr += (lpfc_hbq_defs[i]->entry_count *
2343 			sizeof(struct lpfc_hbq_entry));
2344 	}
2345 	phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_els_hbq_alloc;
2346 	phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer  = lpfc_els_hbq_free;
2347 
2348 	memset(phba->hbqslimp.virt, 0, lpfc_sli_hbq_size());
2349 
2350 	INIT_LIST_HEAD(&phba->hbqbuf_in_list);
2351 
2352 	/* Initialize the SLI Layer to run with lpfc HBAs. */
2353 	lpfc_sli_setup(phba);
2354 	lpfc_sli_queue_setup(phba);
2355 
2356 	retval = lpfc_mem_alloc(phba);
2357 	if (retval) {
2358 		error = retval;
2359 		goto out_free_hbqslimp;
2360 	}
2361 
2362 	/* Initialize and populate the iocb list per host.  */
2363 	INIT_LIST_HEAD(&phba->lpfc_iocb_list);
2364 	for (i = 0; i < LPFC_IOCB_LIST_CNT; i++) {
2365 		iocbq_entry = kzalloc(sizeof(struct lpfc_iocbq), GFP_KERNEL);
2366 		if (iocbq_entry == NULL) {
2367 			printk(KERN_ERR "%s: only allocated %d iocbs of "
2368 				"expected %d count. Unloading driver.\n",
2369 				__func__, i, LPFC_IOCB_LIST_CNT);
2370 			error = -ENOMEM;
2371 			goto out_free_iocbq;
2372 		}
2373 
2374 		iotag = lpfc_sli_next_iotag(phba, iocbq_entry);
2375 		if (iotag == 0) {
2376 			kfree (iocbq_entry);
2377 			printk(KERN_ERR "%s: failed to allocate IOTAG. "
2378 			       "Unloading driver.\n",
2379 				__func__);
2380 			error = -ENOMEM;
2381 			goto out_free_iocbq;
2382 		}
2383 
2384 		spin_lock_irq(&phba->hbalock);
2385 		list_add(&iocbq_entry->list, &phba->lpfc_iocb_list);
2386 		phba->total_iocbq_bufs++;
2387 		spin_unlock_irq(&phba->hbalock);
2388 	}
2389 
2390 	/* Initialize HBA structure */
2391 	phba->fc_edtov = FF_DEF_EDTOV;
2392 	phba->fc_ratov = FF_DEF_RATOV;
2393 	phba->fc_altov = FF_DEF_ALTOV;
2394 	phba->fc_arbtov = FF_DEF_ARBTOV;
2395 
2396 	INIT_LIST_HEAD(&phba->work_list);
2397 	phba->work_ha_mask = (HA_ERATT|HA_MBATT|HA_LATT);
2398 	phba->work_ha_mask |= (HA_RXMASK << (LPFC_ELS_RING * 4));
2399 
2400 	/* Initialize the wait queue head for the kernel thread */
2401 	init_waitqueue_head(&phba->work_waitq);
2402 
2403 	/* Startup the kernel thread for this host adapter. */
2404 	phba->worker_thread = kthread_run(lpfc_do_work, phba,
2405 				       "lpfc_worker_%d", phba->brd_no);
2406 	if (IS_ERR(phba->worker_thread)) {
2407 		error = PTR_ERR(phba->worker_thread);
2408 		goto out_free_iocbq;
2409 	}
2410 
2411 	/* Initialize the list of scsi buffers used by driver for scsi IO. */
2412 	spin_lock_init(&phba->scsi_buf_list_lock);
2413 	INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list);
2414 
2415 	/* Initialize list of fabric iocbs */
2416 	INIT_LIST_HEAD(&phba->fabric_iocb_list);
2417 
2418 	/* Initialize list to save ELS buffers */
2419 	INIT_LIST_HEAD(&phba->elsbuf);
2420 
2421 	vport = lpfc_create_port(phba, phba->brd_no, &phba->pcidev->dev);
2422 	if (!vport)
2423 		goto out_kthread_stop;
2424 
2425 	shost = lpfc_shost_from_vport(vport);
2426 	phba->pport = vport;
2427 	lpfc_debugfs_initialize(vport);
2428 
2429 	pci_set_drvdata(pdev, shost);
2430 	phba->intr_type = NONE;
2431 
2432 	if (phba->cfg_use_msi == 2) {
2433 		error = lpfc_enable_msix(phba);
2434 		if (!error)
2435 			phba->intr_type = MSIX;
2436 	}
2437 
2438 	/* Fallback to MSI if MSI-X initialization failed */
2439 	if (phba->cfg_use_msi >= 1 && phba->intr_type == NONE) {
2440 		retval = pci_enable_msi(phba->pcidev);
2441 		if (!retval)
2442 			phba->intr_type = MSI;
2443 		else
2444 			lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
2445 					"0452 Enable MSI failed, continuing "
2446 					"with IRQ\n");
2447 	}
2448 
2449 	/* MSI-X is the only case the doesn't need to call request_irq */
2450 	if (phba->intr_type != MSIX) {
2451 		retval = request_irq(phba->pcidev->irq, lpfc_intr_handler,
2452 				     IRQF_SHARED, LPFC_DRIVER_NAME, phba);
2453 		if (retval) {
2454 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT, "0451 Enable "
2455 					"interrupt handler failed\n");
2456 			error = retval;
2457 			goto out_disable_msi;
2458 		} else if (phba->intr_type != MSI)
2459 			phba->intr_type = INTx;
2460 	}
2461 
2462 	phba->MBslimaddr = phba->slim_memmap_p;
2463 	phba->HAregaddr = phba->ctrl_regs_memmap_p + HA_REG_OFFSET;
2464 	phba->CAregaddr = phba->ctrl_regs_memmap_p + CA_REG_OFFSET;
2465 	phba->HSregaddr = phba->ctrl_regs_memmap_p + HS_REG_OFFSET;
2466 	phba->HCregaddr = phba->ctrl_regs_memmap_p + HC_REG_OFFSET;
2467 
2468 	if (lpfc_alloc_sysfs_attr(vport)) {
2469 		error = -ENOMEM;
2470 		goto out_free_irq;
2471 	}
2472 
2473 	if (lpfc_sli_hba_setup(phba)) {
2474 		error = -ENODEV;
2475 		goto out_remove_device;
2476 	}
2477 
2478 	/*
2479 	 * hba setup may have changed the hba_queue_depth so we need to adjust
2480 	 * the value of can_queue.
2481 	 */
2482 	shost->can_queue = phba->cfg_hba_queue_depth - 10;
2483 
2484 	lpfc_host_attrib_init(shost);
2485 
2486 	if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
2487 		spin_lock_irq(shost->host_lock);
2488 		lpfc_poll_start_timer(phba);
2489 		spin_unlock_irq(shost->host_lock);
2490 	}
2491 
2492 	scsi_scan_host(shost);
2493 
2494 	return 0;
2495 
2496 out_remove_device:
2497 	lpfc_free_sysfs_attr(vport);
2498 	spin_lock_irq(shost->host_lock);
2499 	vport->load_flag |= FC_UNLOADING;
2500 	spin_unlock_irq(shost->host_lock);
2501 out_free_irq:
2502 	lpfc_stop_phba_timers(phba);
2503 	phba->pport->work_port_events = 0;
2504 
2505 	if (phba->intr_type == MSIX)
2506 		lpfc_disable_msix(phba);
2507 	else
2508 		free_irq(phba->pcidev->irq, phba);
2509 
2510 out_disable_msi:
2511 	if (phba->intr_type == MSI)
2512 		pci_disable_msi(phba->pcidev);
2513 	destroy_port(vport);
2514 out_kthread_stop:
2515 	kthread_stop(phba->worker_thread);
2516 out_free_iocbq:
2517 	list_for_each_entry_safe(iocbq_entry, iocbq_next,
2518 						&phba->lpfc_iocb_list, list) {
2519 		kfree(iocbq_entry);
2520 		phba->total_iocbq_bufs--;
2521 	}
2522 	lpfc_mem_free(phba);
2523 out_free_hbqslimp:
2524 	dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(), phba->hbqslimp.virt,
2525 			  phba->hbqslimp.phys);
2526 out_free_slim:
2527 	dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE, phba->slim2p,
2528 							phba->slim2p_mapping);
2529 out_iounmap:
2530 	iounmap(phba->ctrl_regs_memmap_p);
2531 out_iounmap_slim:
2532 	iounmap(phba->slim_memmap_p);
2533 out_idr_remove:
2534 	idr_remove(&lpfc_hba_index, phba->brd_no);
2535 out_free_phba:
2536 	kfree(phba);
2537 out_release_regions:
2538 	pci_release_selected_regions(pdev, bars);
2539 out_disable_device:
2540 	pci_disable_device(pdev);
2541 out:
2542 	pci_set_drvdata(pdev, NULL);
2543 	if (shost)
2544 		scsi_host_put(shost);
2545 	return error;
2546 }
2547 
2548 /**
2549  * lpfc_pci_remove_one: lpfc PCI func to unregister device from PCI subsystem.
2550  * @pdev: pointer to PCI device
2551  *
2552  * This routine is to be registered to the kernel's PCI subsystem. When an
2553  * Emulex HBA is removed from PCI bus. It perform all the necessary cleanup
2554  * for the HBA device to be removed from the PCI subsystem properly.
2555  **/
2556 static void __devexit
2557 lpfc_pci_remove_one(struct pci_dev *pdev)
2558 {
2559 	struct Scsi_Host  *shost = pci_get_drvdata(pdev);
2560 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2561 	struct lpfc_hba   *phba = vport->phba;
2562 	int bars = pci_select_bars(pdev, IORESOURCE_MEM);
2563 
2564 	spin_lock_irq(&phba->hbalock);
2565 	vport->load_flag |= FC_UNLOADING;
2566 	spin_unlock_irq(&phba->hbalock);
2567 
2568 	kfree(vport->vname);
2569 	lpfc_free_sysfs_attr(vport);
2570 
2571 	kthread_stop(phba->worker_thread);
2572 
2573 	fc_remove_host(shost);
2574 	scsi_remove_host(shost);
2575 	lpfc_cleanup(vport);
2576 
2577 	/*
2578 	 * Bring down the SLI Layer. This step disable all interrupts,
2579 	 * clears the rings, discards all mailbox commands, and resets
2580 	 * the HBA.
2581 	 */
2582 	lpfc_sli_hba_down(phba);
2583 	lpfc_sli_brdrestart(phba);
2584 
2585 	lpfc_stop_phba_timers(phba);
2586 	spin_lock_irq(&phba->hbalock);
2587 	list_del_init(&vport->listentry);
2588 	spin_unlock_irq(&phba->hbalock);
2589 
2590 	lpfc_debugfs_terminate(vport);
2591 
2592 	if (phba->intr_type == MSIX)
2593 		lpfc_disable_msix(phba);
2594 	else {
2595 		free_irq(phba->pcidev->irq, phba);
2596 		if (phba->intr_type == MSI)
2597 			pci_disable_msi(phba->pcidev);
2598 	}
2599 
2600 	pci_set_drvdata(pdev, NULL);
2601 	scsi_host_put(shost);
2602 
2603 	/*
2604 	 * Call scsi_free before mem_free since scsi bufs are released to their
2605 	 * corresponding pools here.
2606 	 */
2607 	lpfc_scsi_free(phba);
2608 	lpfc_mem_free(phba);
2609 
2610 	dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(), phba->hbqslimp.virt,
2611 			  phba->hbqslimp.phys);
2612 
2613 	/* Free resources associated with SLI2 interface */
2614 	dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
2615 			  phba->slim2p, phba->slim2p_mapping);
2616 
2617 	/* unmap adapter SLIM and Control Registers */
2618 	iounmap(phba->ctrl_regs_memmap_p);
2619 	iounmap(phba->slim_memmap_p);
2620 
2621 	idr_remove(&lpfc_hba_index, phba->brd_no);
2622 
2623 	kfree(phba);
2624 
2625 	pci_release_selected_regions(pdev, bars);
2626 	pci_disable_device(pdev);
2627 }
2628 
2629 /**
2630  * lpfc_io_error_detected: Driver method for handling PCI I/O error detected.
2631  * @pdev: pointer to PCI device.
2632  * @state: the current PCI connection state.
2633  *
2634  * This routine is registered to the PCI subsystem for error handling. This
2635  * function is called by the PCI subsystem after a PCI bus error affecting
2636  * this device has been detected. When this function is invoked, it will
2637  * need to stop all the I/Os and interrupt(s) to the device. Once that is
2638  * done, it will return PCI_ERS_RESULT_NEED_RESET for the PCI subsystem to
2639  * perform proper recovery as desired.
2640  *
2641  * Return codes
2642  *   PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
2643  *   PCI_ERS_RESULT_DISCONNECT - device could not be recovered
2644  **/
2645 static pci_ers_result_t lpfc_io_error_detected(struct pci_dev *pdev,
2646 				pci_channel_state_t state)
2647 {
2648 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
2649 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
2650 	struct lpfc_sli *psli = &phba->sli;
2651 	struct lpfc_sli_ring  *pring;
2652 
2653 	if (state == pci_channel_io_perm_failure)
2654 		return PCI_ERS_RESULT_DISCONNECT;
2655 
2656 	pci_disable_device(pdev);
2657 	/*
2658 	 * There may be I/Os dropped by the firmware.
2659 	 * Error iocb (I/O) on txcmplq and let the SCSI layer
2660 	 * retry it after re-establishing link.
2661 	 */
2662 	pring = &psli->ring[psli->fcp_ring];
2663 	lpfc_sli_abort_iocb_ring(phba, pring);
2664 
2665 	if (phba->intr_type == MSIX)
2666 		lpfc_disable_msix(phba);
2667 	else {
2668 		free_irq(phba->pcidev->irq, phba);
2669 		if (phba->intr_type == MSI)
2670 			pci_disable_msi(phba->pcidev);
2671 	}
2672 
2673 	/* Request a slot reset. */
2674 	return PCI_ERS_RESULT_NEED_RESET;
2675 }
2676 
2677 /**
2678  * lpfc_io_slot_reset: Restart a PCI device from scratch.
2679  * @pdev: pointer to PCI device.
2680  *
2681  * This routine is registered to the PCI subsystem for error handling. This is
2682  * called after PCI bus has been reset to restart the PCI card from scratch,
2683  * as if from a cold-boot. During the PCI subsystem error recovery, after the
2684  * driver returns PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform
2685  * proper error recovery and then call this routine before calling the .resume
2686  * method to recover the device. This function will initialize the HBA device,
2687  * enable the interrupt, but it will just put the HBA to offline state without
2688  * passing any I/O traffic.
2689  *
2690  * Return codes
2691  *   PCI_ERS_RESULT_RECOVERED - the device has been recovered
2692  *   PCI_ERS_RESULT_DISCONNECT - device could not be recovered
2693  */
2694 static pci_ers_result_t lpfc_io_slot_reset(struct pci_dev *pdev)
2695 {
2696 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
2697 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
2698 	struct lpfc_sli *psli = &phba->sli;
2699 	int error, retval;
2700 
2701 	dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
2702 	if (pci_enable_device_mem(pdev)) {
2703 		printk(KERN_ERR "lpfc: Cannot re-enable "
2704 			"PCI device after reset.\n");
2705 		return PCI_ERS_RESULT_DISCONNECT;
2706 	}
2707 
2708 	pci_set_master(pdev);
2709 
2710 	spin_lock_irq(&phba->hbalock);
2711 	psli->sli_flag &= ~LPFC_SLI2_ACTIVE;
2712 	spin_unlock_irq(&phba->hbalock);
2713 
2714 	/* Enable configured interrupt method */
2715 	phba->intr_type = NONE;
2716 	if (phba->cfg_use_msi == 2) {
2717 		error = lpfc_enable_msix(phba);
2718 		if (!error)
2719 			phba->intr_type = MSIX;
2720 	}
2721 
2722 	/* Fallback to MSI if MSI-X initialization failed */
2723 	if (phba->cfg_use_msi >= 1 && phba->intr_type == NONE) {
2724 		retval = pci_enable_msi(phba->pcidev);
2725 		if (!retval)
2726 			phba->intr_type = MSI;
2727 		else
2728 			lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
2729 					"0470 Enable MSI failed, continuing "
2730 					"with IRQ\n");
2731 	}
2732 
2733 	/* MSI-X is the only case the doesn't need to call request_irq */
2734 	if (phba->intr_type != MSIX) {
2735 		retval = request_irq(phba->pcidev->irq, lpfc_intr_handler,
2736 				     IRQF_SHARED, LPFC_DRIVER_NAME, phba);
2737 		if (retval) {
2738 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2739 					"0471 Enable interrupt handler "
2740 					"failed\n");
2741 		} else if (phba->intr_type != MSI)
2742 			phba->intr_type = INTx;
2743 	}
2744 
2745 	/* Take device offline; this will perform cleanup */
2746 	lpfc_offline(phba);
2747 	lpfc_sli_brdrestart(phba);
2748 
2749 	return PCI_ERS_RESULT_RECOVERED;
2750 }
2751 
2752 /**
2753  * lpfc_io_resume: Resume PCI I/O operation.
2754  * @pdev: pointer to PCI device
2755  *
2756  * This routine is registered to the PCI subsystem for error handling. It is
2757  * called when kernel error recovery tells the lpfc driver that it is ok to
2758  * resume normal PCI operation after PCI bus error recovery. After this call,
2759  * traffic can start to flow from this device again.
2760  */
2761 static void lpfc_io_resume(struct pci_dev *pdev)
2762 {
2763 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
2764 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
2765 
2766 	lpfc_online(phba);
2767 }
2768 
2769 static struct pci_device_id lpfc_id_table[] = {
2770 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_VIPER,
2771 		PCI_ANY_ID, PCI_ANY_ID, },
2772 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_FIREFLY,
2773 		PCI_ANY_ID, PCI_ANY_ID, },
2774 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_THOR,
2775 		PCI_ANY_ID, PCI_ANY_ID, },
2776 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PEGASUS,
2777 		PCI_ANY_ID, PCI_ANY_ID, },
2778 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_CENTAUR,
2779 		PCI_ANY_ID, PCI_ANY_ID, },
2780 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_DRAGONFLY,
2781 		PCI_ANY_ID, PCI_ANY_ID, },
2782 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SUPERFLY,
2783 		PCI_ANY_ID, PCI_ANY_ID, },
2784 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_RFLY,
2785 		PCI_ANY_ID, PCI_ANY_ID, },
2786 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PFLY,
2787 		PCI_ANY_ID, PCI_ANY_ID, },
2788 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE,
2789 		PCI_ANY_ID, PCI_ANY_ID, },
2790 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_SCSP,
2791 		PCI_ANY_ID, PCI_ANY_ID, },
2792 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_DCSP,
2793 		PCI_ANY_ID, PCI_ANY_ID, },
2794 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS,
2795 		PCI_ANY_ID, PCI_ANY_ID, },
2796 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_SCSP,
2797 		PCI_ANY_ID, PCI_ANY_ID, },
2798 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_DCSP,
2799 		PCI_ANY_ID, PCI_ANY_ID, },
2800 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BMID,
2801 		PCI_ANY_ID, PCI_ANY_ID, },
2802 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BSMB,
2803 		PCI_ANY_ID, PCI_ANY_ID, },
2804 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR,
2805 		PCI_ANY_ID, PCI_ANY_ID, },
2806 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_SCSP,
2807 		PCI_ANY_ID, PCI_ANY_ID, },
2808 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_DCSP,
2809 		PCI_ANY_ID, PCI_ANY_ID, },
2810 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZMID,
2811 		PCI_ANY_ID, PCI_ANY_ID, },
2812 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZSMB,
2813 		PCI_ANY_ID, PCI_ANY_ID, },
2814 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_TFLY,
2815 		PCI_ANY_ID, PCI_ANY_ID, },
2816 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP101,
2817 		PCI_ANY_ID, PCI_ANY_ID, },
2818 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP10000S,
2819 		PCI_ANY_ID, PCI_ANY_ID, },
2820 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP11000S,
2821 		PCI_ANY_ID, PCI_ANY_ID, },
2822 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LPE11000S,
2823 		PCI_ANY_ID, PCI_ANY_ID, },
2824 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT,
2825 		PCI_ANY_ID, PCI_ANY_ID, },
2826 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_MID,
2827 		PCI_ANY_ID, PCI_ANY_ID, },
2828 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SMB,
2829 		PCI_ANY_ID, PCI_ANY_ID, },
2830 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_DCSP,
2831 		PCI_ANY_ID, PCI_ANY_ID, },
2832 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SCSP,
2833 		PCI_ANY_ID, PCI_ANY_ID, },
2834 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_S,
2835 		PCI_ANY_ID, PCI_ANY_ID, },
2836 	{ 0 }
2837 };
2838 
2839 MODULE_DEVICE_TABLE(pci, lpfc_id_table);
2840 
2841 static struct pci_error_handlers lpfc_err_handler = {
2842 	.error_detected = lpfc_io_error_detected,
2843 	.slot_reset = lpfc_io_slot_reset,
2844 	.resume = lpfc_io_resume,
2845 };
2846 
2847 static struct pci_driver lpfc_driver = {
2848 	.name		= LPFC_DRIVER_NAME,
2849 	.id_table	= lpfc_id_table,
2850 	.probe		= lpfc_pci_probe_one,
2851 	.remove		= __devexit_p(lpfc_pci_remove_one),
2852 	.err_handler    = &lpfc_err_handler,
2853 };
2854 
2855 /**
2856  * lpfc_init: lpfc module initialization routine.
2857  *
2858  * This routine is to be invoked when the lpfc module is loaded into the
2859  * kernel. The special kernel macro module_init() is used to indicate the
2860  * role of this routine to the kernel as lpfc module entry point.
2861  *
2862  * Return codes
2863  *   0 - successful
2864  *   -ENOMEM - FC attach transport failed
2865  *   all others - failed
2866  */
2867 static int __init
2868 lpfc_init(void)
2869 {
2870 	int error = 0;
2871 
2872 	printk(LPFC_MODULE_DESC "\n");
2873 	printk(LPFC_COPYRIGHT "\n");
2874 
2875 	if (lpfc_enable_npiv) {
2876 		lpfc_transport_functions.vport_create = lpfc_vport_create;
2877 		lpfc_transport_functions.vport_delete = lpfc_vport_delete;
2878 	}
2879 	lpfc_transport_template =
2880 				fc_attach_transport(&lpfc_transport_functions);
2881 	if (lpfc_transport_template == NULL)
2882 		return -ENOMEM;
2883 	if (lpfc_enable_npiv) {
2884 		lpfc_vport_transport_template =
2885 			fc_attach_transport(&lpfc_vport_transport_functions);
2886 		if (lpfc_vport_transport_template == NULL) {
2887 			fc_release_transport(lpfc_transport_template);
2888 			return -ENOMEM;
2889 		}
2890 	}
2891 	error = pci_register_driver(&lpfc_driver);
2892 	if (error) {
2893 		fc_release_transport(lpfc_transport_template);
2894 		fc_release_transport(lpfc_vport_transport_template);
2895 	}
2896 
2897 	return error;
2898 }
2899 
2900 /**
2901  * lpfc_exit: lpfc module removal routine.
2902  *
2903  * This routine is invoked when the lpfc module is removed from the kernel.
2904  * The special kernel macro module_exit() is used to indicate the role of
2905  * this routine to the kernel as lpfc module exit point.
2906  */
2907 static void __exit
2908 lpfc_exit(void)
2909 {
2910 	pci_unregister_driver(&lpfc_driver);
2911 	fc_release_transport(lpfc_transport_template);
2912 	if (lpfc_enable_npiv)
2913 		fc_release_transport(lpfc_vport_transport_template);
2914 }
2915 
2916 module_init(lpfc_init);
2917 module_exit(lpfc_exit);
2918 MODULE_LICENSE("GPL");
2919 MODULE_DESCRIPTION(LPFC_MODULE_DESC);
2920 MODULE_AUTHOR("Emulex Corporation - tech.support@emulex.com");
2921 MODULE_VERSION("0:" LPFC_DRIVER_VERSION);
2922