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