xref: /openbmc/linux/drivers/scsi/lpfc/lpfc_init.c (revision 90160e01)
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 	}
1584 
1585 	/* At this point, ALL ndlp's should be gone
1586 	 * because of the previous NLP_EVT_DEVICE_RM.
1587 	 * Lets wait for this to happen, if needed.
1588 	 */
1589 	while (!list_empty(&vport->fc_nodes)) {
1590 
1591 		if (i++ > 3000) {
1592 			lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
1593 				"0233 Nodelist not empty\n");
1594 			list_for_each_entry_safe(ndlp, next_ndlp,
1595 						&vport->fc_nodes, nlp_listp) {
1596 				lpfc_printf_vlog(ndlp->vport, KERN_ERR,
1597 						LOG_NODE,
1598 						"0282: did:x%x ndlp:x%p "
1599 						"usgmap:x%x refcnt:%d\n",
1600 						ndlp->nlp_DID, (void *)ndlp,
1601 						ndlp->nlp_usg_map,
1602 						atomic_read(
1603 							&ndlp->kref.refcount));
1604 			}
1605 			break;
1606 		}
1607 
1608 		/* Wait for any activity on ndlps to settle */
1609 		msleep(10);
1610 	}
1611 	return;
1612 }
1613 
1614 /**
1615  * lpfc_stop_vport_timers: Stop all the timers associated with a vport.
1616  * @vport: pointer to a virtual N_Port data structure.
1617  *
1618  * This routine stops all the timers associated with a @vport. This function
1619  * is invoked before disabling or deleting a @vport. Note that the physical
1620  * port is treated as @vport 0.
1621  **/
1622 void
1623 lpfc_stop_vport_timers(struct lpfc_vport *vport)
1624 {
1625 	del_timer_sync(&vport->els_tmofunc);
1626 	del_timer_sync(&vport->fc_fdmitmo);
1627 	lpfc_can_disctmo(vport);
1628 	return;
1629 }
1630 
1631 /**
1632  * lpfc_stop_phba_timers: Stop all the timers associated with an HBA.
1633  * @phba: pointer to lpfc hba data structure.
1634  *
1635  * This routine stops all the timers associated with a HBA. This function is
1636  * invoked before either putting a HBA offline or unloading the driver.
1637  **/
1638 static void
1639 lpfc_stop_phba_timers(struct lpfc_hba *phba)
1640 {
1641 	del_timer_sync(&phba->fcp_poll_timer);
1642 	lpfc_stop_vport_timers(phba->pport);
1643 	del_timer_sync(&phba->sli.mbox_tmo);
1644 	del_timer_sync(&phba->fabric_block_timer);
1645 	phba->hb_outstanding = 0;
1646 	del_timer_sync(&phba->hb_tmofunc);
1647 	return;
1648 }
1649 
1650 /**
1651  * lpfc_block_mgmt_io: Mark a HBA's management interface as blocked.
1652  * @phba: pointer to lpfc hba data structure.
1653  *
1654  * This routine marks a HBA's management interface as blocked. Once the HBA's
1655  * management interface is marked as blocked, all the user space access to
1656  * the HBA, whether they are from sysfs interface or libdfc interface will
1657  * all be blocked. The HBA is set to block the management interface when the
1658  * driver prepares the HBA interface for online or offline.
1659  **/
1660 static void
1661 lpfc_block_mgmt_io(struct lpfc_hba * phba)
1662 {
1663 	unsigned long iflag;
1664 
1665 	spin_lock_irqsave(&phba->hbalock, iflag);
1666 	phba->sli.sli_flag |= LPFC_BLOCK_MGMT_IO;
1667 	spin_unlock_irqrestore(&phba->hbalock, iflag);
1668 }
1669 
1670 /**
1671  * lpfc_online: Initialize and bring a HBA online.
1672  * @phba: pointer to lpfc hba data structure.
1673  *
1674  * This routine initializes the HBA and brings a HBA online. During this
1675  * process, the management interface is blocked to prevent user space access
1676  * to the HBA interfering with the driver initialization.
1677  *
1678  * Return codes
1679  *   0 - successful
1680  *   1 - failed
1681  **/
1682 int
1683 lpfc_online(struct lpfc_hba *phba)
1684 {
1685 	struct lpfc_vport *vport = phba->pport;
1686 	struct lpfc_vport **vports;
1687 	int i;
1688 
1689 	if (!phba)
1690 		return 0;
1691 
1692 	if (!(vport->fc_flag & FC_OFFLINE_MODE))
1693 		return 0;
1694 
1695 	lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
1696 			"0458 Bring Adapter online\n");
1697 
1698 	lpfc_block_mgmt_io(phba);
1699 
1700 	if (!lpfc_sli_queue_setup(phba)) {
1701 		lpfc_unblock_mgmt_io(phba);
1702 		return 1;
1703 	}
1704 
1705 	if (lpfc_sli_hba_setup(phba)) {	/* Initialize the HBA */
1706 		lpfc_unblock_mgmt_io(phba);
1707 		return 1;
1708 	}
1709 
1710 	vports = lpfc_create_vport_work_array(phba);
1711 	if (vports != NULL)
1712 		for(i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
1713 			struct Scsi_Host *shost;
1714 			shost = lpfc_shost_from_vport(vports[i]);
1715 			spin_lock_irq(shost->host_lock);
1716 			vports[i]->fc_flag &= ~FC_OFFLINE_MODE;
1717 			if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
1718 				vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
1719 			spin_unlock_irq(shost->host_lock);
1720 		}
1721 		lpfc_destroy_vport_work_array(phba, vports);
1722 
1723 	lpfc_unblock_mgmt_io(phba);
1724 	return 0;
1725 }
1726 
1727 /**
1728  * lpfc_unblock_mgmt_io: Mark a HBA's management interface to be not blocked.
1729  * @phba: pointer to lpfc hba data structure.
1730  *
1731  * This routine marks a HBA's management interface as not blocked. Once the
1732  * HBA's management interface is marked as not blocked, all the user space
1733  * access to the HBA, whether they are from sysfs interface or libdfc
1734  * interface will be allowed. The HBA is set to block the management interface
1735  * when the driver prepares the HBA interface for online or offline and then
1736  * set to unblock the management interface afterwards.
1737  **/
1738 void
1739 lpfc_unblock_mgmt_io(struct lpfc_hba * phba)
1740 {
1741 	unsigned long iflag;
1742 
1743 	spin_lock_irqsave(&phba->hbalock, iflag);
1744 	phba->sli.sli_flag &= ~LPFC_BLOCK_MGMT_IO;
1745 	spin_unlock_irqrestore(&phba->hbalock, iflag);
1746 }
1747 
1748 /**
1749  * lpfc_offline_prep: Prepare a HBA to be brought offline.
1750  * @phba: pointer to lpfc hba data structure.
1751  *
1752  * This routine is invoked to prepare a HBA to be brought offline. It performs
1753  * unregistration login to all the nodes on all vports and flushes the mailbox
1754  * queue to make it ready to be brought offline.
1755  **/
1756 void
1757 lpfc_offline_prep(struct lpfc_hba * phba)
1758 {
1759 	struct lpfc_vport *vport = phba->pport;
1760 	struct lpfc_nodelist  *ndlp, *next_ndlp;
1761 	struct lpfc_vport **vports;
1762 	int i;
1763 
1764 	if (vport->fc_flag & FC_OFFLINE_MODE)
1765 		return;
1766 
1767 	lpfc_block_mgmt_io(phba);
1768 
1769 	lpfc_linkdown(phba);
1770 
1771 	/* Issue an unreg_login to all nodes on all vports */
1772 	vports = lpfc_create_vport_work_array(phba);
1773 	if (vports != NULL) {
1774 		for(i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
1775 			struct Scsi_Host *shost;
1776 
1777 			if (vports[i]->load_flag & FC_UNLOADING)
1778 				continue;
1779 			shost =	lpfc_shost_from_vport(vports[i]);
1780 			list_for_each_entry_safe(ndlp, next_ndlp,
1781 						 &vports[i]->fc_nodes,
1782 						 nlp_listp) {
1783 				if (!NLP_CHK_NODE_ACT(ndlp))
1784 					continue;
1785 				if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
1786 					continue;
1787 				if (ndlp->nlp_type & NLP_FABRIC) {
1788 					lpfc_disc_state_machine(vports[i], ndlp,
1789 						NULL, NLP_EVT_DEVICE_RECOVERY);
1790 					lpfc_disc_state_machine(vports[i], ndlp,
1791 						NULL, NLP_EVT_DEVICE_RM);
1792 				}
1793 				spin_lock_irq(shost->host_lock);
1794 				ndlp->nlp_flag &= ~NLP_NPR_ADISC;
1795 				spin_unlock_irq(shost->host_lock);
1796 				lpfc_unreg_rpi(vports[i], ndlp);
1797 			}
1798 		}
1799 	}
1800 	lpfc_destroy_vport_work_array(phba, vports);
1801 
1802 	lpfc_sli_flush_mbox_queue(phba);
1803 }
1804 
1805 /**
1806  * lpfc_offline: Bring a HBA offline.
1807  * @phba: pointer to lpfc hba data structure.
1808  *
1809  * This routine actually brings a HBA offline. It stops all the timers
1810  * associated with the HBA, brings down the SLI layer, and eventually
1811  * marks the HBA as in offline state for the upper layer protocol.
1812  **/
1813 void
1814 lpfc_offline(struct lpfc_hba *phba)
1815 {
1816 	struct Scsi_Host  *shost;
1817 	struct lpfc_vport **vports;
1818 	int i;
1819 
1820 	if (phba->pport->fc_flag & FC_OFFLINE_MODE)
1821 		return;
1822 
1823 	/* stop all timers associated with this hba */
1824 	lpfc_stop_phba_timers(phba);
1825 	vports = lpfc_create_vport_work_array(phba);
1826 	if (vports != NULL)
1827 		for(i = 0; i <= phba->max_vpi && vports[i] != NULL; i++)
1828 			lpfc_stop_vport_timers(vports[i]);
1829 	lpfc_destroy_vport_work_array(phba, vports);
1830 	lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
1831 			"0460 Bring Adapter offline\n");
1832 	/* Bring down the SLI Layer and cleanup.  The HBA is offline
1833 	   now.  */
1834 	lpfc_sli_hba_down(phba);
1835 	spin_lock_irq(&phba->hbalock);
1836 	phba->work_ha = 0;
1837 	spin_unlock_irq(&phba->hbalock);
1838 	vports = lpfc_create_vport_work_array(phba);
1839 	if (vports != NULL)
1840 		for(i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
1841 			shost = lpfc_shost_from_vport(vports[i]);
1842 			spin_lock_irq(shost->host_lock);
1843 			vports[i]->work_port_events = 0;
1844 			vports[i]->fc_flag |= FC_OFFLINE_MODE;
1845 			spin_unlock_irq(shost->host_lock);
1846 		}
1847 	lpfc_destroy_vport_work_array(phba, vports);
1848 }
1849 
1850 /**
1851  * lpfc_scsi_free: Free all the SCSI buffers and IOCBs from driver lists.
1852  * @phba: pointer to lpfc hba data structure.
1853  *
1854  * This routine is to free all the SCSI buffers and IOCBs from the driver
1855  * list back to kernel. It is called from lpfc_pci_remove_one to free
1856  * the internal resources before the device is removed from the system.
1857  *
1858  * Return codes
1859  *   0 - successful (for now, it always returns 0)
1860  **/
1861 static int
1862 lpfc_scsi_free(struct lpfc_hba *phba)
1863 {
1864 	struct lpfc_scsi_buf *sb, *sb_next;
1865 	struct lpfc_iocbq *io, *io_next;
1866 
1867 	spin_lock_irq(&phba->hbalock);
1868 	/* Release all the lpfc_scsi_bufs maintained by this host. */
1869 	list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list, list) {
1870 		list_del(&sb->list);
1871 		pci_pool_free(phba->lpfc_scsi_dma_buf_pool, sb->data,
1872 			      sb->dma_handle);
1873 		kfree(sb);
1874 		phba->total_scsi_bufs--;
1875 	}
1876 
1877 	/* Release all the lpfc_iocbq entries maintained by this host. */
1878 	list_for_each_entry_safe(io, io_next, &phba->lpfc_iocb_list, list) {
1879 		list_del(&io->list);
1880 		kfree(io);
1881 		phba->total_iocbq_bufs--;
1882 	}
1883 
1884 	spin_unlock_irq(&phba->hbalock);
1885 
1886 	return 0;
1887 }
1888 
1889 /**
1890  * lpfc_create_port: Create an FC port.
1891  * @phba: pointer to lpfc hba data structure.
1892  * @instance: a unique integer ID to this FC port.
1893  * @dev: pointer to the device data structure.
1894  *
1895  * This routine creates a FC port for the upper layer protocol. The FC port
1896  * can be created on top of either a physical port or a virtual port provided
1897  * by the HBA. This routine also allocates a SCSI host data structure (shost)
1898  * and associates the FC port created before adding the shost into the SCSI
1899  * layer.
1900  *
1901  * Return codes
1902  *   @vport - pointer to the virtual N_Port data structure.
1903  *   NULL - port create failed.
1904  **/
1905 struct lpfc_vport *
1906 lpfc_create_port(struct lpfc_hba *phba, int instance, struct device *dev)
1907 {
1908 	struct lpfc_vport *vport;
1909 	struct Scsi_Host  *shost;
1910 	int error = 0;
1911 
1912 	if (dev != &phba->pcidev->dev)
1913 		shost = scsi_host_alloc(&lpfc_vport_template,
1914 					sizeof(struct lpfc_vport));
1915 	else
1916 		shost = scsi_host_alloc(&lpfc_template,
1917 					sizeof(struct lpfc_vport));
1918 	if (!shost)
1919 		goto out;
1920 
1921 	vport = (struct lpfc_vport *) shost->hostdata;
1922 	vport->phba = phba;
1923 	vport->load_flag |= FC_LOADING;
1924 	vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
1925 	vport->fc_rscn_flush = 0;
1926 
1927 	lpfc_get_vport_cfgparam(vport);
1928 	shost->unique_id = instance;
1929 	shost->max_id = LPFC_MAX_TARGET;
1930 	shost->max_lun = vport->cfg_max_luns;
1931 	shost->this_id = -1;
1932 	shost->max_cmd_len = 16;
1933 	/*
1934 	 * Set initial can_queue value since 0 is no longer supported and
1935 	 * scsi_add_host will fail. This will be adjusted later based on the
1936 	 * max xri value determined in hba setup.
1937 	 */
1938 	shost->can_queue = phba->cfg_hba_queue_depth - 10;
1939 	if (dev != &phba->pcidev->dev) {
1940 		shost->transportt = lpfc_vport_transport_template;
1941 		vport->port_type = LPFC_NPIV_PORT;
1942 	} else {
1943 		shost->transportt = lpfc_transport_template;
1944 		vport->port_type = LPFC_PHYSICAL_PORT;
1945 	}
1946 
1947 	/* Initialize all internally managed lists. */
1948 	INIT_LIST_HEAD(&vport->fc_nodes);
1949 	spin_lock_init(&vport->work_port_lock);
1950 
1951 	init_timer(&vport->fc_disctmo);
1952 	vport->fc_disctmo.function = lpfc_disc_timeout;
1953 	vport->fc_disctmo.data = (unsigned long)vport;
1954 
1955 	init_timer(&vport->fc_fdmitmo);
1956 	vport->fc_fdmitmo.function = lpfc_fdmi_tmo;
1957 	vport->fc_fdmitmo.data = (unsigned long)vport;
1958 
1959 	init_timer(&vport->els_tmofunc);
1960 	vport->els_tmofunc.function = lpfc_els_timeout;
1961 	vport->els_tmofunc.data = (unsigned long)vport;
1962 
1963 	error = scsi_add_host(shost, dev);
1964 	if (error)
1965 		goto out_put_shost;
1966 
1967 	spin_lock_irq(&phba->hbalock);
1968 	list_add_tail(&vport->listentry, &phba->port_list);
1969 	spin_unlock_irq(&phba->hbalock);
1970 	return vport;
1971 
1972 out_put_shost:
1973 	scsi_host_put(shost);
1974 out:
1975 	return NULL;
1976 }
1977 
1978 /**
1979  * destroy_port: Destroy an FC port.
1980  * @vport: pointer to an lpfc virtual N_Port data structure.
1981  *
1982  * This routine destroys a FC port from the upper layer protocol. All the
1983  * resources associated with the port are released.
1984  **/
1985 void
1986 destroy_port(struct lpfc_vport *vport)
1987 {
1988 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1989 	struct lpfc_hba  *phba = vport->phba;
1990 
1991 	kfree(vport->vname);
1992 
1993 	lpfc_debugfs_terminate(vport);
1994 	fc_remove_host(shost);
1995 	scsi_remove_host(shost);
1996 
1997 	spin_lock_irq(&phba->hbalock);
1998 	list_del_init(&vport->listentry);
1999 	spin_unlock_irq(&phba->hbalock);
2000 
2001 	lpfc_cleanup(vport);
2002 	return;
2003 }
2004 
2005 /**
2006  * lpfc_get_instance: Get a unique integer ID.
2007  *
2008  * This routine allocates a unique integer ID from lpfc_hba_index pool. It
2009  * uses the kernel idr facility to perform the task.
2010  *
2011  * Return codes:
2012  *   instance - a unique integer ID allocated as the new instance.
2013  *   -1 - lpfc get instance failed.
2014  **/
2015 int
2016 lpfc_get_instance(void)
2017 {
2018 	int instance = 0;
2019 
2020 	/* Assign an unused number */
2021 	if (!idr_pre_get(&lpfc_hba_index, GFP_KERNEL))
2022 		return -1;
2023 	if (idr_get_new(&lpfc_hba_index, NULL, &instance))
2024 		return -1;
2025 	return instance;
2026 }
2027 
2028 /**
2029  * lpfc_scan_finished: method for SCSI layer to detect whether scan is done.
2030  * @shost: pointer to SCSI host data structure.
2031  * @time: elapsed time of the scan in jiffies.
2032  *
2033  * This routine is called by the SCSI layer with a SCSI host to determine
2034  * whether the scan host is finished.
2035  *
2036  * Note: there is no scan_start function as adapter initialization will have
2037  * asynchronously kicked off the link initialization.
2038  *
2039  * Return codes
2040  *   0 - SCSI host scan is not over yet.
2041  *   1 - SCSI host scan is over.
2042  **/
2043 int lpfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
2044 {
2045 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2046 	struct lpfc_hba   *phba = vport->phba;
2047 	int stat = 0;
2048 
2049 	spin_lock_irq(shost->host_lock);
2050 
2051 	if (vport->load_flag & FC_UNLOADING) {
2052 		stat = 1;
2053 		goto finished;
2054 	}
2055 	if (time >= 30 * HZ) {
2056 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
2057 				"0461 Scanning longer than 30 "
2058 				"seconds.  Continuing initialization\n");
2059 		stat = 1;
2060 		goto finished;
2061 	}
2062 	if (time >= 15 * HZ && phba->link_state <= LPFC_LINK_DOWN) {
2063 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
2064 				"0465 Link down longer than 15 "
2065 				"seconds.  Continuing initialization\n");
2066 		stat = 1;
2067 		goto finished;
2068 	}
2069 
2070 	if (vport->port_state != LPFC_VPORT_READY)
2071 		goto finished;
2072 	if (vport->num_disc_nodes || vport->fc_prli_sent)
2073 		goto finished;
2074 	if (vport->fc_map_cnt == 0 && time < 2 * HZ)
2075 		goto finished;
2076 	if ((phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) != 0)
2077 		goto finished;
2078 
2079 	stat = 1;
2080 
2081 finished:
2082 	spin_unlock_irq(shost->host_lock);
2083 	return stat;
2084 }
2085 
2086 /**
2087  * lpfc_host_attrib_init: Initialize SCSI host attributes on a FC port.
2088  * @shost: pointer to SCSI host data structure.
2089  *
2090  * This routine initializes a given SCSI host attributes on a FC port. The
2091  * SCSI host can be either on top of a physical port or a virtual port.
2092  **/
2093 void lpfc_host_attrib_init(struct Scsi_Host *shost)
2094 {
2095 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2096 	struct lpfc_hba   *phba = vport->phba;
2097 	/*
2098 	 * Set fixed host attributes.  Must done after lpfc_sli_hba_setup().
2099 	 */
2100 
2101 	fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
2102 	fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
2103 	fc_host_supported_classes(shost) = FC_COS_CLASS3;
2104 
2105 	memset(fc_host_supported_fc4s(shost), 0,
2106 	       sizeof(fc_host_supported_fc4s(shost)));
2107 	fc_host_supported_fc4s(shost)[2] = 1;
2108 	fc_host_supported_fc4s(shost)[7] = 1;
2109 
2110 	lpfc_vport_symbolic_node_name(vport, fc_host_symbolic_name(shost),
2111 				 sizeof fc_host_symbolic_name(shost));
2112 
2113 	fc_host_supported_speeds(shost) = 0;
2114 	if (phba->lmt & LMT_10Gb)
2115 		fc_host_supported_speeds(shost) |= FC_PORTSPEED_10GBIT;
2116 	if (phba->lmt & LMT_8Gb)
2117 		fc_host_supported_speeds(shost) |= FC_PORTSPEED_8GBIT;
2118 	if (phba->lmt & LMT_4Gb)
2119 		fc_host_supported_speeds(shost) |= FC_PORTSPEED_4GBIT;
2120 	if (phba->lmt & LMT_2Gb)
2121 		fc_host_supported_speeds(shost) |= FC_PORTSPEED_2GBIT;
2122 	if (phba->lmt & LMT_1Gb)
2123 		fc_host_supported_speeds(shost) |= FC_PORTSPEED_1GBIT;
2124 
2125 	fc_host_maxframe_size(shost) =
2126 		(((uint32_t) vport->fc_sparam.cmn.bbRcvSizeMsb & 0x0F) << 8) |
2127 		(uint32_t) vport->fc_sparam.cmn.bbRcvSizeLsb;
2128 
2129 	/* This value is also unchanging */
2130 	memset(fc_host_active_fc4s(shost), 0,
2131 	       sizeof(fc_host_active_fc4s(shost)));
2132 	fc_host_active_fc4s(shost)[2] = 1;
2133 	fc_host_active_fc4s(shost)[7] = 1;
2134 
2135 	fc_host_max_npiv_vports(shost) = phba->max_vpi;
2136 	spin_lock_irq(shost->host_lock);
2137 	vport->load_flag &= ~FC_LOADING;
2138 	spin_unlock_irq(shost->host_lock);
2139 }
2140 
2141 /**
2142  * lpfc_enable_msix: Enable MSI-X interrupt mode.
2143  * @phba: pointer to lpfc hba data structure.
2144  *
2145  * This routine is invoked to enable the MSI-X interrupt vectors. The kernel
2146  * function pci_enable_msix() is called to enable the MSI-X vectors. Note that
2147  * pci_enable_msix(), once invoked, enables either all or nothing, depending
2148  * on the current availability of PCI vector resources. The device driver is
2149  * responsible for calling the individual request_irq() to register each MSI-X
2150  * vector with a interrupt handler, which is done in this function. Note that
2151  * later when device is unloading, the driver should always call free_irq()
2152  * on all MSI-X vectors it has done request_irq() on before calling
2153  * pci_disable_msix(). Failure to do so results in a BUG_ON() and a device
2154  * will be left with MSI-X enabled and leaks its vectors.
2155  *
2156  * Return codes
2157  *   0 - sucessful
2158  *   other values - error
2159  **/
2160 static int
2161 lpfc_enable_msix(struct lpfc_hba *phba)
2162 {
2163 	int error;
2164 
2165 	phba->msix_entries[0].entry = 0;
2166 	phba->msix_entries[0].vector = 0;
2167 
2168 	error = pci_enable_msix(phba->pcidev, phba->msix_entries,
2169 				ARRAY_SIZE(phba->msix_entries));
2170 	if (error) {
2171 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
2172 				"0420 Enable MSI-X failed (%d), continuing "
2173 				"with MSI\n", error);
2174 		pci_disable_msix(phba->pcidev);
2175 		return error;
2176 	}
2177 
2178 	error =	request_irq(phba->msix_entries[0].vector, lpfc_intr_handler, 0,
2179 			    LPFC_DRIVER_NAME, phba);
2180 	if (error) {
2181 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2182 				"0421 MSI-X request_irq failed (%d), "
2183 				"continuing with MSI\n", error);
2184 		pci_disable_msix(phba->pcidev);
2185 	}
2186 	return error;
2187 }
2188 
2189 /**
2190  * lpfc_disable_msix: Disable MSI-X interrupt mode.
2191  * @phba: pointer to lpfc hba data structure.
2192  *
2193  * This routine is invoked to release the MSI-X vectors and then disable the
2194  * MSI-X interrupt mode.
2195  **/
2196 static void
2197 lpfc_disable_msix(struct lpfc_hba *phba)
2198 {
2199 	free_irq(phba->msix_entries[0].vector, phba);
2200 	pci_disable_msix(phba->pcidev);
2201 }
2202 
2203 /**
2204  * lpfc_pci_probe_one: lpfc PCI probe func to register device to PCI subsystem.
2205  * @pdev: pointer to PCI device
2206  * @pid: pointer to PCI device identifier
2207  *
2208  * This routine is to be registered to the kernel's PCI subsystem. When an
2209  * Emulex HBA is presented in PCI bus, the kernel PCI subsystem looks at
2210  * PCI device-specific information of the device and driver to see if the
2211  * driver state that it can support this kind of device. If the match is
2212  * successful, the driver core invokes this routine. If this routine
2213  * determines it can claim the HBA, it does all the initialization that it
2214  * needs to do to handle the HBA properly.
2215  *
2216  * Return code
2217  *   0 - driver can claim the device
2218  *   negative value - driver can not claim the device
2219  **/
2220 static int __devinit
2221 lpfc_pci_probe_one(struct pci_dev *pdev, const struct pci_device_id *pid)
2222 {
2223 	struct lpfc_vport *vport = NULL;
2224 	struct lpfc_hba   *phba;
2225 	struct lpfc_sli   *psli;
2226 	struct lpfc_iocbq *iocbq_entry = NULL, *iocbq_next = NULL;
2227 	struct Scsi_Host  *shost = NULL;
2228 	void *ptr;
2229 	unsigned long bar0map_len, bar2map_len;
2230 	int error = -ENODEV, retval;
2231 	int  i, hbq_count;
2232 	uint16_t iotag;
2233 	int bars = pci_select_bars(pdev, IORESOURCE_MEM);
2234 
2235 	if (pci_enable_device_mem(pdev))
2236 		goto out;
2237 	if (pci_request_selected_regions(pdev, bars, LPFC_DRIVER_NAME))
2238 		goto out_disable_device;
2239 
2240 	phba = kzalloc(sizeof (struct lpfc_hba), GFP_KERNEL);
2241 	if (!phba)
2242 		goto out_release_regions;
2243 
2244 	spin_lock_init(&phba->hbalock);
2245 
2246 	/* Initialize ndlp management spinlock */
2247 	spin_lock_init(&phba->ndlp_lock);
2248 
2249 	phba->pcidev = pdev;
2250 
2251 	/* Assign an unused board number */
2252 	if ((phba->brd_no = lpfc_get_instance()) < 0)
2253 		goto out_free_phba;
2254 
2255 	INIT_LIST_HEAD(&phba->port_list);
2256 	/*
2257 	 * Get all the module params for configuring this host and then
2258 	 * establish the host.
2259 	 */
2260 	lpfc_get_cfgparam(phba);
2261 	phba->max_vpi = LPFC_MAX_VPI;
2262 
2263 	/* Initialize timers used by driver */
2264 	init_timer(&phba->hb_tmofunc);
2265 	phba->hb_tmofunc.function = lpfc_hb_timeout;
2266 	phba->hb_tmofunc.data = (unsigned long)phba;
2267 
2268 	psli = &phba->sli;
2269 	init_timer(&psli->mbox_tmo);
2270 	psli->mbox_tmo.function = lpfc_mbox_timeout;
2271 	psli->mbox_tmo.data = (unsigned long) phba;
2272 	init_timer(&phba->fcp_poll_timer);
2273 	phba->fcp_poll_timer.function = lpfc_poll_timeout;
2274 	phba->fcp_poll_timer.data = (unsigned long) phba;
2275 	init_timer(&phba->fabric_block_timer);
2276 	phba->fabric_block_timer.function = lpfc_fabric_block_timeout;
2277 	phba->fabric_block_timer.data = (unsigned long) phba;
2278 
2279 	pci_set_master(pdev);
2280 	pci_try_set_mwi(pdev);
2281 
2282 	if (pci_set_dma_mask(phba->pcidev, DMA_64BIT_MASK) != 0)
2283 		if (pci_set_dma_mask(phba->pcidev, DMA_32BIT_MASK) != 0)
2284 			goto out_idr_remove;
2285 
2286 	/*
2287 	 * Get the bus address of Bar0 and Bar2 and the number of bytes
2288 	 * required by each mapping.
2289 	 */
2290 	phba->pci_bar0_map = pci_resource_start(phba->pcidev, 0);
2291 	bar0map_len        = pci_resource_len(phba->pcidev, 0);
2292 
2293 	phba->pci_bar2_map = pci_resource_start(phba->pcidev, 2);
2294 	bar2map_len        = pci_resource_len(phba->pcidev, 2);
2295 
2296 	/* Map HBA SLIM to a kernel virtual address. */
2297 	phba->slim_memmap_p      = ioremap(phba->pci_bar0_map, bar0map_len);
2298 	if (!phba->slim_memmap_p) {
2299 		error = -ENODEV;
2300 		dev_printk(KERN_ERR, &pdev->dev,
2301 			   "ioremap failed for SLIM memory.\n");
2302 		goto out_idr_remove;
2303 	}
2304 
2305 	/* Map HBA Control Registers to a kernel virtual address. */
2306 	phba->ctrl_regs_memmap_p = ioremap(phba->pci_bar2_map, bar2map_len);
2307 	if (!phba->ctrl_regs_memmap_p) {
2308 		error = -ENODEV;
2309 		dev_printk(KERN_ERR, &pdev->dev,
2310 			   "ioremap failed for HBA control registers.\n");
2311 		goto out_iounmap_slim;
2312 	}
2313 
2314 	/* Allocate memory for SLI-2 structures */
2315 	phba->slim2p = dma_alloc_coherent(&phba->pcidev->dev, SLI2_SLIM_SIZE,
2316 					  &phba->slim2p_mapping, GFP_KERNEL);
2317 	if (!phba->slim2p)
2318 		goto out_iounmap;
2319 
2320 	memset(phba->slim2p, 0, SLI2_SLIM_SIZE);
2321 
2322 	phba->hbqslimp.virt = dma_alloc_coherent(&phba->pcidev->dev,
2323 						 lpfc_sli_hbq_size(),
2324 						 &phba->hbqslimp.phys,
2325 						 GFP_KERNEL);
2326 	if (!phba->hbqslimp.virt)
2327 		goto out_free_slim;
2328 
2329 	hbq_count = lpfc_sli_hbq_count();
2330 	ptr = phba->hbqslimp.virt;
2331 	for (i = 0; i < hbq_count; ++i) {
2332 		phba->hbqs[i].hbq_virt = ptr;
2333 		INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
2334 		ptr += (lpfc_hbq_defs[i]->entry_count *
2335 			sizeof(struct lpfc_hbq_entry));
2336 	}
2337 	phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_els_hbq_alloc;
2338 	phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer  = lpfc_els_hbq_free;
2339 
2340 	memset(phba->hbqslimp.virt, 0, lpfc_sli_hbq_size());
2341 
2342 	INIT_LIST_HEAD(&phba->hbqbuf_in_list);
2343 
2344 	/* Initialize the SLI Layer to run with lpfc HBAs. */
2345 	lpfc_sli_setup(phba);
2346 	lpfc_sli_queue_setup(phba);
2347 
2348 	retval = lpfc_mem_alloc(phba);
2349 	if (retval) {
2350 		error = retval;
2351 		goto out_free_hbqslimp;
2352 	}
2353 
2354 	/* Initialize and populate the iocb list per host.  */
2355 	INIT_LIST_HEAD(&phba->lpfc_iocb_list);
2356 	for (i = 0; i < LPFC_IOCB_LIST_CNT; i++) {
2357 		iocbq_entry = kzalloc(sizeof(struct lpfc_iocbq), GFP_KERNEL);
2358 		if (iocbq_entry == NULL) {
2359 			printk(KERN_ERR "%s: only allocated %d iocbs of "
2360 				"expected %d count. Unloading driver.\n",
2361 				__func__, i, LPFC_IOCB_LIST_CNT);
2362 			error = -ENOMEM;
2363 			goto out_free_iocbq;
2364 		}
2365 
2366 		iotag = lpfc_sli_next_iotag(phba, iocbq_entry);
2367 		if (iotag == 0) {
2368 			kfree (iocbq_entry);
2369 			printk(KERN_ERR "%s: failed to allocate IOTAG. "
2370 			       "Unloading driver.\n",
2371 				__func__);
2372 			error = -ENOMEM;
2373 			goto out_free_iocbq;
2374 		}
2375 
2376 		spin_lock_irq(&phba->hbalock);
2377 		list_add(&iocbq_entry->list, &phba->lpfc_iocb_list);
2378 		phba->total_iocbq_bufs++;
2379 		spin_unlock_irq(&phba->hbalock);
2380 	}
2381 
2382 	/* Initialize HBA structure */
2383 	phba->fc_edtov = FF_DEF_EDTOV;
2384 	phba->fc_ratov = FF_DEF_RATOV;
2385 	phba->fc_altov = FF_DEF_ALTOV;
2386 	phba->fc_arbtov = FF_DEF_ARBTOV;
2387 
2388 	INIT_LIST_HEAD(&phba->work_list);
2389 	phba->work_ha_mask = (HA_ERATT|HA_MBATT|HA_LATT);
2390 	phba->work_ha_mask |= (HA_RXMASK << (LPFC_ELS_RING * 4));
2391 
2392 	/* Initialize the wait queue head for the kernel thread */
2393 	init_waitqueue_head(&phba->work_waitq);
2394 
2395 	/* Startup the kernel thread for this host adapter. */
2396 	phba->worker_thread = kthread_run(lpfc_do_work, phba,
2397 				       "lpfc_worker_%d", phba->brd_no);
2398 	if (IS_ERR(phba->worker_thread)) {
2399 		error = PTR_ERR(phba->worker_thread);
2400 		goto out_free_iocbq;
2401 	}
2402 
2403 	/* Initialize the list of scsi buffers used by driver for scsi IO. */
2404 	spin_lock_init(&phba->scsi_buf_list_lock);
2405 	INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list);
2406 
2407 	/* Initialize list of fabric iocbs */
2408 	INIT_LIST_HEAD(&phba->fabric_iocb_list);
2409 
2410 	/* Initialize list to save ELS buffers */
2411 	INIT_LIST_HEAD(&phba->elsbuf);
2412 
2413 	vport = lpfc_create_port(phba, phba->brd_no, &phba->pcidev->dev);
2414 	if (!vport)
2415 		goto out_kthread_stop;
2416 
2417 	shost = lpfc_shost_from_vport(vport);
2418 	phba->pport = vport;
2419 	lpfc_debugfs_initialize(vport);
2420 
2421 	pci_set_drvdata(pdev, shost);
2422 	phba->intr_type = NONE;
2423 
2424 	if (phba->cfg_use_msi == 2) {
2425 		error = lpfc_enable_msix(phba);
2426 		if (!error)
2427 			phba->intr_type = MSIX;
2428 	}
2429 
2430 	/* Fallback to MSI if MSI-X initialization failed */
2431 	if (phba->cfg_use_msi >= 1 && phba->intr_type == NONE) {
2432 		retval = pci_enable_msi(phba->pcidev);
2433 		if (!retval)
2434 			phba->intr_type = MSI;
2435 		else
2436 			lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
2437 					"0452 Enable MSI failed, continuing "
2438 					"with IRQ\n");
2439 	}
2440 
2441 	/* MSI-X is the only case the doesn't need to call request_irq */
2442 	if (phba->intr_type != MSIX) {
2443 		retval = request_irq(phba->pcidev->irq, lpfc_intr_handler,
2444 				     IRQF_SHARED, LPFC_DRIVER_NAME, phba);
2445 		if (retval) {
2446 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT, "0451 Enable "
2447 					"interrupt handler failed\n");
2448 			error = retval;
2449 			goto out_disable_msi;
2450 		} else if (phba->intr_type != MSI)
2451 			phba->intr_type = INTx;
2452 	}
2453 
2454 	phba->MBslimaddr = phba->slim_memmap_p;
2455 	phba->HAregaddr = phba->ctrl_regs_memmap_p + HA_REG_OFFSET;
2456 	phba->CAregaddr = phba->ctrl_regs_memmap_p + CA_REG_OFFSET;
2457 	phba->HSregaddr = phba->ctrl_regs_memmap_p + HS_REG_OFFSET;
2458 	phba->HCregaddr = phba->ctrl_regs_memmap_p + HC_REG_OFFSET;
2459 
2460 	if (lpfc_alloc_sysfs_attr(vport)) {
2461 		error = -ENOMEM;
2462 		goto out_free_irq;
2463 	}
2464 
2465 	if (lpfc_sli_hba_setup(phba)) {
2466 		error = -ENODEV;
2467 		goto out_remove_device;
2468 	}
2469 
2470 	/*
2471 	 * hba setup may have changed the hba_queue_depth so we need to adjust
2472 	 * the value of can_queue.
2473 	 */
2474 	shost->can_queue = phba->cfg_hba_queue_depth - 10;
2475 
2476 	lpfc_host_attrib_init(shost);
2477 
2478 	if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
2479 		spin_lock_irq(shost->host_lock);
2480 		lpfc_poll_start_timer(phba);
2481 		spin_unlock_irq(shost->host_lock);
2482 	}
2483 
2484 	scsi_scan_host(shost);
2485 
2486 	return 0;
2487 
2488 out_remove_device:
2489 	lpfc_free_sysfs_attr(vport);
2490 	spin_lock_irq(shost->host_lock);
2491 	vport->load_flag |= FC_UNLOADING;
2492 	spin_unlock_irq(shost->host_lock);
2493 out_free_irq:
2494 	lpfc_stop_phba_timers(phba);
2495 	phba->pport->work_port_events = 0;
2496 
2497 	if (phba->intr_type == MSIX)
2498 		lpfc_disable_msix(phba);
2499 	else
2500 		free_irq(phba->pcidev->irq, phba);
2501 
2502 out_disable_msi:
2503 	if (phba->intr_type == MSI)
2504 		pci_disable_msi(phba->pcidev);
2505 	destroy_port(vport);
2506 out_kthread_stop:
2507 	kthread_stop(phba->worker_thread);
2508 out_free_iocbq:
2509 	list_for_each_entry_safe(iocbq_entry, iocbq_next,
2510 						&phba->lpfc_iocb_list, list) {
2511 		kfree(iocbq_entry);
2512 		phba->total_iocbq_bufs--;
2513 	}
2514 	lpfc_mem_free(phba);
2515 out_free_hbqslimp:
2516 	dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(), phba->hbqslimp.virt,
2517 			  phba->hbqslimp.phys);
2518 out_free_slim:
2519 	dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE, phba->slim2p,
2520 							phba->slim2p_mapping);
2521 out_iounmap:
2522 	iounmap(phba->ctrl_regs_memmap_p);
2523 out_iounmap_slim:
2524 	iounmap(phba->slim_memmap_p);
2525 out_idr_remove:
2526 	idr_remove(&lpfc_hba_index, phba->brd_no);
2527 out_free_phba:
2528 	kfree(phba);
2529 out_release_regions:
2530 	pci_release_selected_regions(pdev, bars);
2531 out_disable_device:
2532 	pci_disable_device(pdev);
2533 out:
2534 	pci_set_drvdata(pdev, NULL);
2535 	if (shost)
2536 		scsi_host_put(shost);
2537 	return error;
2538 }
2539 
2540 /**
2541  * lpfc_pci_remove_one: lpfc PCI func to unregister device from PCI subsystem.
2542  * @pdev: pointer to PCI device
2543  *
2544  * This routine is to be registered to the kernel's PCI subsystem. When an
2545  * Emulex HBA is removed from PCI bus. It perform all the necessary cleanup
2546  * for the HBA device to be removed from the PCI subsystem properly.
2547  **/
2548 static void __devexit
2549 lpfc_pci_remove_one(struct pci_dev *pdev)
2550 {
2551 	struct Scsi_Host  *shost = pci_get_drvdata(pdev);
2552 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2553 	struct lpfc_hba   *phba = vport->phba;
2554 	int bars = pci_select_bars(pdev, IORESOURCE_MEM);
2555 
2556 	spin_lock_irq(&phba->hbalock);
2557 	vport->load_flag |= FC_UNLOADING;
2558 	spin_unlock_irq(&phba->hbalock);
2559 
2560 	kfree(vport->vname);
2561 	lpfc_free_sysfs_attr(vport);
2562 
2563 	kthread_stop(phba->worker_thread);
2564 
2565 	fc_remove_host(shost);
2566 	scsi_remove_host(shost);
2567 	lpfc_cleanup(vport);
2568 
2569 	/*
2570 	 * Bring down the SLI Layer. This step disable all interrupts,
2571 	 * clears the rings, discards all mailbox commands, and resets
2572 	 * the HBA.
2573 	 */
2574 	lpfc_sli_hba_down(phba);
2575 	lpfc_sli_brdrestart(phba);
2576 
2577 	lpfc_stop_phba_timers(phba);
2578 	spin_lock_irq(&phba->hbalock);
2579 	list_del_init(&vport->listentry);
2580 	spin_unlock_irq(&phba->hbalock);
2581 
2582 	lpfc_debugfs_terminate(vport);
2583 
2584 	if (phba->intr_type == MSIX)
2585 		lpfc_disable_msix(phba);
2586 	else {
2587 		free_irq(phba->pcidev->irq, phba);
2588 		if (phba->intr_type == MSI)
2589 			pci_disable_msi(phba->pcidev);
2590 	}
2591 
2592 	pci_set_drvdata(pdev, NULL);
2593 	scsi_host_put(shost);
2594 
2595 	/*
2596 	 * Call scsi_free before mem_free since scsi bufs are released to their
2597 	 * corresponding pools here.
2598 	 */
2599 	lpfc_scsi_free(phba);
2600 	lpfc_mem_free(phba);
2601 
2602 	dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(), phba->hbqslimp.virt,
2603 			  phba->hbqslimp.phys);
2604 
2605 	/* Free resources associated with SLI2 interface */
2606 	dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
2607 			  phba->slim2p, phba->slim2p_mapping);
2608 
2609 	/* unmap adapter SLIM and Control Registers */
2610 	iounmap(phba->ctrl_regs_memmap_p);
2611 	iounmap(phba->slim_memmap_p);
2612 
2613 	idr_remove(&lpfc_hba_index, phba->brd_no);
2614 
2615 	kfree(phba);
2616 
2617 	pci_release_selected_regions(pdev, bars);
2618 	pci_disable_device(pdev);
2619 }
2620 
2621 /**
2622  * lpfc_io_error_detected: Driver method for handling PCI I/O error detected.
2623  * @pdev: pointer to PCI device.
2624  * @state: the current PCI connection state.
2625  *
2626  * This routine is registered to the PCI subsystem for error handling. This
2627  * function is called by the PCI subsystem after a PCI bus error affecting
2628  * this device has been detected. When this function is invoked, it will
2629  * need to stop all the I/Os and interrupt(s) to the device. Once that is
2630  * done, it will return PCI_ERS_RESULT_NEED_RESET for the PCI subsystem to
2631  * perform proper recovery as desired.
2632  *
2633  * Return codes
2634  *   PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
2635  *   PCI_ERS_RESULT_DISCONNECT - device could not be recovered
2636  **/
2637 static pci_ers_result_t lpfc_io_error_detected(struct pci_dev *pdev,
2638 				pci_channel_state_t state)
2639 {
2640 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
2641 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
2642 	struct lpfc_sli *psli = &phba->sli;
2643 	struct lpfc_sli_ring  *pring;
2644 
2645 	if (state == pci_channel_io_perm_failure)
2646 		return PCI_ERS_RESULT_DISCONNECT;
2647 
2648 	pci_disable_device(pdev);
2649 	/*
2650 	 * There may be I/Os dropped by the firmware.
2651 	 * Error iocb (I/O) on txcmplq and let the SCSI layer
2652 	 * retry it after re-establishing link.
2653 	 */
2654 	pring = &psli->ring[psli->fcp_ring];
2655 	lpfc_sli_abort_iocb_ring(phba, pring);
2656 
2657 	if (phba->intr_type == MSIX)
2658 		lpfc_disable_msix(phba);
2659 	else {
2660 		free_irq(phba->pcidev->irq, phba);
2661 		if (phba->intr_type == MSI)
2662 			pci_disable_msi(phba->pcidev);
2663 	}
2664 
2665 	/* Request a slot reset. */
2666 	return PCI_ERS_RESULT_NEED_RESET;
2667 }
2668 
2669 /**
2670  * lpfc_io_slot_reset: Restart a PCI device from scratch.
2671  * @pdev: pointer to PCI device.
2672  *
2673  * This routine is registered to the PCI subsystem for error handling. This is
2674  * called after PCI bus has been reset to restart the PCI card from scratch,
2675  * as if from a cold-boot. During the PCI subsystem error recovery, after the
2676  * driver returns PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform
2677  * proper error recovery and then call this routine before calling the .resume
2678  * method to recover the device. This function will initialize the HBA device,
2679  * enable the interrupt, but it will just put the HBA to offline state without
2680  * passing any I/O traffic.
2681  *
2682  * Return codes
2683  *   PCI_ERS_RESULT_RECOVERED - the device has been recovered
2684  *   PCI_ERS_RESULT_DISCONNECT - device could not be recovered
2685  */
2686 static pci_ers_result_t lpfc_io_slot_reset(struct pci_dev *pdev)
2687 {
2688 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
2689 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
2690 	struct lpfc_sli *psli = &phba->sli;
2691 	int error, retval;
2692 
2693 	dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
2694 	if (pci_enable_device_mem(pdev)) {
2695 		printk(KERN_ERR "lpfc: Cannot re-enable "
2696 			"PCI device after reset.\n");
2697 		return PCI_ERS_RESULT_DISCONNECT;
2698 	}
2699 
2700 	pci_set_master(pdev);
2701 
2702 	spin_lock_irq(&phba->hbalock);
2703 	psli->sli_flag &= ~LPFC_SLI2_ACTIVE;
2704 	spin_unlock_irq(&phba->hbalock);
2705 
2706 	/* Enable configured interrupt method */
2707 	phba->intr_type = NONE;
2708 	if (phba->cfg_use_msi == 2) {
2709 		error = lpfc_enable_msix(phba);
2710 		if (!error)
2711 			phba->intr_type = MSIX;
2712 	}
2713 
2714 	/* Fallback to MSI if MSI-X initialization failed */
2715 	if (phba->cfg_use_msi >= 1 && phba->intr_type == NONE) {
2716 		retval = pci_enable_msi(phba->pcidev);
2717 		if (!retval)
2718 			phba->intr_type = MSI;
2719 		else
2720 			lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
2721 					"0470 Enable MSI failed, continuing "
2722 					"with IRQ\n");
2723 	}
2724 
2725 	/* MSI-X is the only case the doesn't need to call request_irq */
2726 	if (phba->intr_type != MSIX) {
2727 		retval = request_irq(phba->pcidev->irq, lpfc_intr_handler,
2728 				     IRQF_SHARED, LPFC_DRIVER_NAME, phba);
2729 		if (retval) {
2730 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2731 					"0471 Enable interrupt handler "
2732 					"failed\n");
2733 		} else if (phba->intr_type != MSI)
2734 			phba->intr_type = INTx;
2735 	}
2736 
2737 	/* Take device offline; this will perform cleanup */
2738 	lpfc_offline(phba);
2739 	lpfc_sli_brdrestart(phba);
2740 
2741 	return PCI_ERS_RESULT_RECOVERED;
2742 }
2743 
2744 /**
2745  * lpfc_io_resume: Resume PCI I/O operation.
2746  * @pdev: pointer to PCI device
2747  *
2748  * This routine is registered to the PCI subsystem for error handling. It is
2749  * called when kernel error recovery tells the lpfc driver that it is ok to
2750  * resume normal PCI operation after PCI bus error recovery. After this call,
2751  * traffic can start to flow from this device again.
2752  */
2753 static void lpfc_io_resume(struct pci_dev *pdev)
2754 {
2755 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
2756 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
2757 
2758 	lpfc_online(phba);
2759 }
2760 
2761 static struct pci_device_id lpfc_id_table[] = {
2762 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_VIPER,
2763 		PCI_ANY_ID, PCI_ANY_ID, },
2764 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_FIREFLY,
2765 		PCI_ANY_ID, PCI_ANY_ID, },
2766 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_THOR,
2767 		PCI_ANY_ID, PCI_ANY_ID, },
2768 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PEGASUS,
2769 		PCI_ANY_ID, PCI_ANY_ID, },
2770 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_CENTAUR,
2771 		PCI_ANY_ID, PCI_ANY_ID, },
2772 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_DRAGONFLY,
2773 		PCI_ANY_ID, PCI_ANY_ID, },
2774 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SUPERFLY,
2775 		PCI_ANY_ID, PCI_ANY_ID, },
2776 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_RFLY,
2777 		PCI_ANY_ID, PCI_ANY_ID, },
2778 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PFLY,
2779 		PCI_ANY_ID, PCI_ANY_ID, },
2780 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE,
2781 		PCI_ANY_ID, PCI_ANY_ID, },
2782 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_SCSP,
2783 		PCI_ANY_ID, PCI_ANY_ID, },
2784 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_DCSP,
2785 		PCI_ANY_ID, PCI_ANY_ID, },
2786 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS,
2787 		PCI_ANY_ID, PCI_ANY_ID, },
2788 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_SCSP,
2789 		PCI_ANY_ID, PCI_ANY_ID, },
2790 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_DCSP,
2791 		PCI_ANY_ID, PCI_ANY_ID, },
2792 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BMID,
2793 		PCI_ANY_ID, PCI_ANY_ID, },
2794 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BSMB,
2795 		PCI_ANY_ID, PCI_ANY_ID, },
2796 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR,
2797 		PCI_ANY_ID, PCI_ANY_ID, },
2798 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_SCSP,
2799 		PCI_ANY_ID, PCI_ANY_ID, },
2800 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_DCSP,
2801 		PCI_ANY_ID, PCI_ANY_ID, },
2802 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZMID,
2803 		PCI_ANY_ID, PCI_ANY_ID, },
2804 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZSMB,
2805 		PCI_ANY_ID, PCI_ANY_ID, },
2806 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_TFLY,
2807 		PCI_ANY_ID, PCI_ANY_ID, },
2808 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP101,
2809 		PCI_ANY_ID, PCI_ANY_ID, },
2810 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP10000S,
2811 		PCI_ANY_ID, PCI_ANY_ID, },
2812 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP11000S,
2813 		PCI_ANY_ID, PCI_ANY_ID, },
2814 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LPE11000S,
2815 		PCI_ANY_ID, PCI_ANY_ID, },
2816 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT,
2817 		PCI_ANY_ID, PCI_ANY_ID, },
2818 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_MID,
2819 		PCI_ANY_ID, PCI_ANY_ID, },
2820 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SMB,
2821 		PCI_ANY_ID, PCI_ANY_ID, },
2822 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_DCSP,
2823 		PCI_ANY_ID, PCI_ANY_ID, },
2824 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SCSP,
2825 		PCI_ANY_ID, PCI_ANY_ID, },
2826 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_S,
2827 		PCI_ANY_ID, PCI_ANY_ID, },
2828 	{ 0 }
2829 };
2830 
2831 MODULE_DEVICE_TABLE(pci, lpfc_id_table);
2832 
2833 static struct pci_error_handlers lpfc_err_handler = {
2834 	.error_detected = lpfc_io_error_detected,
2835 	.slot_reset = lpfc_io_slot_reset,
2836 	.resume = lpfc_io_resume,
2837 };
2838 
2839 static struct pci_driver lpfc_driver = {
2840 	.name		= LPFC_DRIVER_NAME,
2841 	.id_table	= lpfc_id_table,
2842 	.probe		= lpfc_pci_probe_one,
2843 	.remove		= __devexit_p(lpfc_pci_remove_one),
2844 	.err_handler    = &lpfc_err_handler,
2845 };
2846 
2847 /**
2848  * lpfc_init: lpfc module initialization routine.
2849  *
2850  * This routine is to be invoked when the lpfc module is loaded into the
2851  * kernel. The special kernel macro module_init() is used to indicate the
2852  * role of this routine to the kernel as lpfc module entry point.
2853  *
2854  * Return codes
2855  *   0 - successful
2856  *   -ENOMEM - FC attach transport failed
2857  *   all others - failed
2858  */
2859 static int __init
2860 lpfc_init(void)
2861 {
2862 	int error = 0;
2863 
2864 	printk(LPFC_MODULE_DESC "\n");
2865 	printk(LPFC_COPYRIGHT "\n");
2866 
2867 	if (lpfc_enable_npiv) {
2868 		lpfc_transport_functions.vport_create = lpfc_vport_create;
2869 		lpfc_transport_functions.vport_delete = lpfc_vport_delete;
2870 	}
2871 	lpfc_transport_template =
2872 				fc_attach_transport(&lpfc_transport_functions);
2873 	if (lpfc_transport_template == NULL)
2874 		return -ENOMEM;
2875 	if (lpfc_enable_npiv) {
2876 		lpfc_vport_transport_template =
2877 			fc_attach_transport(&lpfc_vport_transport_functions);
2878 		if (lpfc_vport_transport_template == NULL) {
2879 			fc_release_transport(lpfc_transport_template);
2880 			return -ENOMEM;
2881 		}
2882 	}
2883 	error = pci_register_driver(&lpfc_driver);
2884 	if (error) {
2885 		fc_release_transport(lpfc_transport_template);
2886 		fc_release_transport(lpfc_vport_transport_template);
2887 	}
2888 
2889 	return error;
2890 }
2891 
2892 /**
2893  * lpfc_exit: lpfc module removal routine.
2894  *
2895  * This routine is invoked when the lpfc module is removed from the kernel.
2896  * The special kernel macro module_exit() is used to indicate the role of
2897  * this routine to the kernel as lpfc module exit point.
2898  */
2899 static void __exit
2900 lpfc_exit(void)
2901 {
2902 	pci_unregister_driver(&lpfc_driver);
2903 	fc_release_transport(lpfc_transport_template);
2904 	if (lpfc_enable_npiv)
2905 		fc_release_transport(lpfc_vport_transport_template);
2906 }
2907 
2908 module_init(lpfc_init);
2909 module_exit(lpfc_exit);
2910 MODULE_LICENSE("GPL");
2911 MODULE_DESCRIPTION(LPFC_MODULE_DESC);
2912 MODULE_AUTHOR("Emulex Corporation - tech.support@emulex.com");
2913 MODULE_VERSION("0:" LPFC_DRIVER_VERSION);
2914